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 <log/log.h>
21 #include <utils/Trace.h>
22 
23 #include <inttypes.h>
24 
25 #include "hal_utils.h"
26 #include "hdrplus_result_processor.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)54 void HdrplusResultProcessor::SetResultCallback(
55     ProcessCaptureResultFunc process_capture_result, NotifyFunc notify) {
56   ATRACE_CALL();
57   std::lock_guard<std::mutex> lock(callback_lock_);
58   process_capture_result_ = process_capture_result;
59   notify_ = notify;
60 }
61 
AddPendingRequests(const std::vector<ProcessBlockRequest> & process_block_requests,const CaptureRequest & remaining_session_request)62 status_t HdrplusResultProcessor::AddPendingRequests(
63     const std::vector<ProcessBlockRequest>& process_block_requests,
64     const CaptureRequest& remaining_session_request) {
65   ATRACE_CALL();
66   // This is the last result processor. Sanity check if requests contains
67   // all remaining output buffers.
68   if (!hal_utils::AreAllRemainingBuffersRequested(process_block_requests,
69                                                   remaining_session_request)) {
70     ALOGE("%s: Some output buffers will not be completed.", __FUNCTION__);
71     return BAD_VALUE;
72   }
73 
74   return OK;
75 }
76 
ProcessResult(ProcessBlockResult block_result)77 void HdrplusResultProcessor::ProcessResult(ProcessBlockResult block_result) {
78   ATRACE_CALL();
79   std::lock_guard<std::mutex> lock(callback_lock_);
80 
81   std::unique_ptr<CaptureResult> result = std::move(block_result.result);
82   if (result == nullptr) {
83     ALOGW("%s: Received a nullptr result.", __FUNCTION__);
84     return;
85   }
86 
87   if (process_capture_result_ == nullptr) {
88     ALOGE("%s: process_capture_result_ is nullptr. Dropping a result.",
89           __FUNCTION__);
90     return;
91   }
92 
93   // Return raw buffer to internal stream manager and remove it from result
94   status_t res;
95   if (result->output_buffers.size() != 0 &&
96       internal_stream_manager_->IsPendingBufferEmpty(raw_stream_id_) == false) {
97     res = internal_stream_manager_->ReturnZslStreamBuffers(result->frame_number,
98                                                            raw_stream_id_);
99     if (res != OK) {
100       ALOGE("%s: (%d)ReturnZslStreamBuffers fail", __FUNCTION__,
101             result->frame_number);
102       return;
103     } else {
104       ALOGI("%s: (%d)ReturnZslStreamBuffers ok", __FUNCTION__,
105             result->frame_number);
106     }
107     result->input_buffers.clear();
108   }
109 
110   if (result->result_metadata) {
111     res = hal_utils::SetEnableZslMetadata(result->result_metadata.get(), true);
112     if (res != OK) {
113       ALOGW("%s: SetEnableZslMetadata (%d) fail", __FUNCTION__,
114             result->frame_number);
115     }
116   }
117 
118   process_capture_result_(std::move(result));
119 }
120 
Notify(const ProcessBlockNotifyMessage & block_message)121 void HdrplusResultProcessor::Notify(
122     const ProcessBlockNotifyMessage& block_message) {
123   ATRACE_CALL();
124   std::lock_guard<std::mutex> lock(callback_lock_);
125   if (notify_ == nullptr) {
126     ALOGE("%s: notify_ is nullptr. Dropping a message.", __FUNCTION__);
127     return;
128   }
129 
130   notify_(block_message.message);
131 }
132 
FlushPendingRequests()133 status_t HdrplusResultProcessor::FlushPendingRequests() {
134   ATRACE_CALL();
135   return INVALID_OPERATION;
136 }
137 
138 }  // namespace google_camera_hal
139 }  // namespace android