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)54 void SnapshotResultProcessor::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 SnapshotResultProcessor::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. Validity 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 SnapshotResultProcessor::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 yuv 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(yuv_stream_id_) == false) {
97     res = internal_stream_manager_->ReturnZslStreamBuffers(result->frame_number,
98                                                            yuv_stream_id_);
99     if (res != OK) {
100       ALOGE("%s: (%d)ReturnZslStreamBuffers fail", __FUNCTION__,
101             result->frame_number);
102     } else {
103       ALOGI("%s: (%d)ReturnZslStreamBuffers ok", __FUNCTION__,
104             result->frame_number);
105     }
106     result->input_buffers.clear();
107   }
108 
109   if (result->result_metadata) {
110     res = hal_utils::SetEnableZslMetadata(result->result_metadata.get(), true);
111     if (res != OK) {
112       ALOGW("%s: SetEnableZslMetadata (%d) fail", __FUNCTION__,
113             result->frame_number);
114     }
115   }
116 
117   process_capture_result_(std::move(result));
118 }
119 
Notify(const ProcessBlockNotifyMessage & block_message)120 void SnapshotResultProcessor::Notify(
121     const ProcessBlockNotifyMessage& block_message) {
122   ATRACE_CALL();
123   std::lock_guard<std::mutex> lock(callback_lock_);
124   if (notify_ == nullptr) {
125     ALOGE("%s: notify_ is nullptr. Dropping a message.", __FUNCTION__);
126     return;
127   }
128 
129   notify_(block_message.message);
130 }
131 
FlushPendingRequests()132 status_t SnapshotResultProcessor::FlushPendingRequests() {
133   ATRACE_CALL();
134   return INVALID_OPERATION;
135 }
136 
137 }  // namespace google_camera_hal
138 }  // namespace android