1 /*
2  * Copyright (C) 2021 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_SnapshotResultProcessor"
19 #define ATRACE_TAG ATRACE_TAG_CAMERA
20 #include "snapshot_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 yuv_stream_id)31 std::unique_ptr<SnapshotResultProcessor> SnapshotResultProcessor::Create(
32     InternalStreamManager* internal_stream_manager, int32_t yuv_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<SnapshotResultProcessor>(
40       new SnapshotResultProcessor(internal_stream_manager, yuv_stream_id));
41   if (result_processor == nullptr) {
42     ALOGE("%s: Creating SnapshotResultProcessor failed.", __FUNCTION__);
43     return nullptr;
44   }
45 
46   return result_processor;
47 }
48 
SnapshotResultProcessor(InternalStreamManager * internal_stream_manager,int32_t yuv_stream_id)49 SnapshotResultProcessor::SnapshotResultProcessor(
50     InternalStreamManager* internal_stream_manager, int32_t yuv_stream_id) {
51   internal_stream_manager_ = internal_stream_manager;
52   yuv_stream_id_ = yuv_stream_id;
53 }
SetResultCallback(ProcessCaptureResultFunc process_capture_result,NotifyFunc notify,ProcessBatchCaptureResultFunc)54 void SnapshotResultProcessor::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 SnapshotResultProcessor::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. Validity 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 SnapshotResultProcessor::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 yuv 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(yuv_stream_id_) == false) {
98     res = internal_stream_manager_->ReturnZslStreamBuffers(result->frame_number,
99                                                            yuv_stream_id_);
100     if (res != OK) {
101       ALOGE("%s: (%d)ReturnZslStreamBuffers fail", __FUNCTION__,
102             result->frame_number);
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 SnapshotResultProcessor::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 SnapshotResultProcessor::FlushPendingRequests() {
134   ATRACE_CALL();
135   return INVALID_OPERATION;
136 }
137 
138 }  // namespace google_camera_hal
139 }  // namespace android