1 /*
2  * Copyright (C) 2019 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 // #define LOG_NDEBUG 0
18 #define LOG_TAG "GCH_HdrplusResultProcessor"
19 #define ATRACE_TAG ATRACE_TAG_CAMERA
20 #include "hdrplus_result_processor.h"
21 
22 #include <inttypes.h>
23 #include <log/log.h>
24 #include <utils/Trace.h>
25 
26 #include "hal_utils.h"
27 
28 namespace android {
29 namespace google_camera_hal {
30 
Create(InternalStreamManager * internal_stream_manager,int32_t raw_stream_id)31 std::unique_ptr<HdrplusResultProcessor> HdrplusResultProcessor::Create(
32     InternalStreamManager* internal_stream_manager, int32_t raw_stream_id) {
33   ATRACE_CALL();
34   if (internal_stream_manager == nullptr) {
35     ALOGE("%s: internal_stream_manager nullptr.", __FUNCTION__);
36     return nullptr;
37   }
38 
39   auto result_processor = std::unique_ptr<HdrplusResultProcessor>(
40       new HdrplusResultProcessor(internal_stream_manager, raw_stream_id));
41   if (result_processor == nullptr) {
42     ALOGE("%s: Creating HdrplusResultProcessor failed.", __FUNCTION__);
43     return nullptr;
44   }
45 
46   return result_processor;
47 }
48 
HdrplusResultProcessor(InternalStreamManager * internal_stream_manager,int32_t raw_stream_id)49 HdrplusResultProcessor::HdrplusResultProcessor(
50     InternalStreamManager* internal_stream_manager, int32_t raw_stream_id) {
51   internal_stream_manager_ = internal_stream_manager;
52   raw_stream_id_ = raw_stream_id;
53 }
SetResultCallback(ProcessCaptureResultFunc process_capture_result,NotifyFunc notify,ProcessBatchCaptureResultFunc)54 void HdrplusResultProcessor::SetResultCallback(
55     ProcessCaptureResultFunc process_capture_result, NotifyFunc notify,
56     ProcessBatchCaptureResultFunc /*process_batch_capture_result*/) {
57   ATRACE_CALL();
58   std::lock_guard<std::mutex> lock(callback_lock_);
59   process_capture_result_ = process_capture_result;
60   notify_ = notify;
61 }
62 
AddPendingRequests(const std::vector<ProcessBlockRequest> & process_block_requests,const CaptureRequest & remaining_session_request)63 status_t HdrplusResultProcessor::AddPendingRequests(
64     const std::vector<ProcessBlockRequest>& process_block_requests,
65     const CaptureRequest& remaining_session_request) {
66   ATRACE_CALL();
67   // This is the last result processor. Sanity check if requests contains
68   // all remaining output buffers.
69   if (!hal_utils::AreAllRemainingBuffersRequested(process_block_requests,
70                                                   remaining_session_request)) {
71     ALOGE("%s: Some output buffers will not be completed.", __FUNCTION__);
72     return BAD_VALUE;
73   }
74 
75   return OK;
76 }
77 
ProcessResult(ProcessBlockResult block_result)78 void HdrplusResultProcessor::ProcessResult(ProcessBlockResult block_result) {
79   ATRACE_CALL();
80   std::lock_guard<std::mutex> lock(callback_lock_);
81 
82   std::unique_ptr<CaptureResult> result = std::move(block_result.result);
83   if (result == nullptr) {
84     ALOGW("%s: Received a nullptr result.", __FUNCTION__);
85     return;
86   }
87 
88   if (process_capture_result_ == nullptr) {
89     ALOGE("%s: process_capture_result_ is nullptr. Dropping a result.",
90           __FUNCTION__);
91     return;
92   }
93 
94   // Return raw buffer to internal stream manager and remove it from result
95   status_t res;
96   if (result->output_buffers.size() != 0 &&
97       internal_stream_manager_->IsPendingBufferEmpty(raw_stream_id_) == false) {
98     res = internal_stream_manager_->ReturnZslStreamBuffers(result->frame_number,
99                                                            raw_stream_id_);
100     if (res != OK) {
101       ALOGE("%s: (%d)ReturnZslStreamBuffers fail", __FUNCTION__,
102             result->frame_number);
103       return;
104     } else {
105       ALOGI("%s: (%d)ReturnZslStreamBuffers ok", __FUNCTION__,
106             result->frame_number);
107     }
108     result->input_buffers.clear();
109   }
110 
111   if (result->result_metadata) {
112     res = hal_utils::SetEnableZslMetadata(result->result_metadata.get(), true);
113     if (res != OK) {
114       ALOGW("%s: SetEnableZslMetadata (%d) fail", __FUNCTION__,
115             result->frame_number);
116     }
117   }
118 
119   process_capture_result_(std::move(result));
120 }
121 
Notify(const ProcessBlockNotifyMessage & block_message)122 void HdrplusResultProcessor::Notify(
123     const ProcessBlockNotifyMessage& block_message) {
124   ATRACE_CALL();
125   std::lock_guard<std::mutex> lock(callback_lock_);
126   if (notify_ == nullptr) {
127     ALOGE("%s: notify_ is nullptr. Dropping a message.", __FUNCTION__);
128     return;
129   }
130 
131   notify_(block_message.message);
132 }
133 
FlushPendingRequests()134 status_t HdrplusResultProcessor::FlushPendingRequests() {
135   ATRACE_CALL();
136   return INVALID_OPERATION;
137 }
138 
139 }  // namespace google_camera_hal
140 }  // namespace android