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 #include "hal_types.h"
19 #define LOG_TAG "GCH_RealtimeZslResultProcessor"
20 #define ATRACE_TAG ATRACE_TAG_CAMERA
21
22 #include <inttypes.h>
23 #include <log/log.h>
24 #include <utils/Trace.h>
25
26 #include "hal_utils.h"
27 #include "realtime_zsl_result_processor.h"
28
29 namespace android {
30 namespace google_camera_hal {
31
Create(InternalStreamManager * internal_stream_manager,int32_t stream_id,android_pixel_format_t pixel_format,uint32_t partial_result_count)32 std::unique_ptr<RealtimeZslResultProcessor> RealtimeZslResultProcessor::Create(
33 InternalStreamManager* internal_stream_manager, int32_t stream_id,
34 android_pixel_format_t pixel_format, uint32_t partial_result_count) {
35 ATRACE_CALL();
36 if (internal_stream_manager == nullptr) {
37 ALOGE("%s: internal_stream_manager is nullptr.", __FUNCTION__);
38 return nullptr;
39 }
40 if (pixel_format != android_pixel_format_t::HAL_PIXEL_FORMAT_YCBCR_420_888) {
41 ALOGE("%s: only YCBCR_420_888 is supported for YUV ZSL", __FUNCTION__);
42 return nullptr;
43 }
44
45 auto result_processor =
46 std::unique_ptr<RealtimeZslResultProcessor>(new RealtimeZslResultProcessor(
47 internal_stream_manager, stream_id, partial_result_count));
48 if (result_processor == nullptr) {
49 ALOGE("%s: Creating RealtimeZslResultProcessor failed.", __FUNCTION__);
50 return nullptr;
51 }
52
53 return result_processor;
54 }
55
RealtimeZslResultProcessor(InternalStreamManager * internal_stream_manager,int32_t stream_id,uint32_t partial_result_count)56 RealtimeZslResultProcessor::RealtimeZslResultProcessor(
57 InternalStreamManager* internal_stream_manager, int32_t stream_id,
58 uint32_t partial_result_count) {
59 internal_stream_manager_ = internal_stream_manager;
60 stream_id_ = stream_id;
61 partial_result_count_ = partial_result_count;
62 }
63
SetResultCallback(ProcessCaptureResultFunc process_capture_result,NotifyFunc notify,ProcessBatchCaptureResultFunc,NotifyBatchFunc)64 void RealtimeZslResultProcessor::SetResultCallback(
65 ProcessCaptureResultFunc process_capture_result, NotifyFunc notify,
66 ProcessBatchCaptureResultFunc /*process_batch_capture_result*/,
67 NotifyBatchFunc /*notify_batch*/) {
68 std::lock_guard<std::mutex> lock(callback_lock_);
69 process_capture_result_ = process_capture_result;
70 notify_ = notify;
71 }
72
AddPendingRequests(const std::vector<ProcessBlockRequest> & process_block_requests,const CaptureRequest & remaining_session_request)73 status_t RealtimeZslResultProcessor::AddPendingRequests(
74 const std::vector<ProcessBlockRequest>& process_block_requests,
75 const CaptureRequest& remaining_session_request) {
76 ATRACE_CALL();
77 // This is the last result processor. Sanity check if requests contains
78 // all remaining output buffers.
79 if (!hal_utils::AreAllRemainingBuffersRequested(process_block_requests,
80 remaining_session_request)) {
81 ALOGE("%s: Some output buffers will not be completed.", __FUNCTION__);
82 return BAD_VALUE;
83 }
84
85 return OK;
86 }
87
ProcessResult(ProcessBlockResult block_result)88 void RealtimeZslResultProcessor::ProcessResult(ProcessBlockResult block_result) {
89 ATRACE_CALL();
90 std::lock_guard<std::mutex> lock(callback_lock_);
91 std::unique_ptr<CaptureResult> result = std::move(block_result.result);
92 if (result == nullptr) {
93 ALOGW("%s: Received a nullptr result.", __FUNCTION__);
94 return;
95 }
96
97 if (process_capture_result_ == nullptr) {
98 ALOGE("%s: process_capture_result_ is nullptr. Dropping a result.",
99 __FUNCTION__);
100 return;
101 }
102
103 // Return filled buffer to internal stream manager
104 // And remove buffer from result
105 bool returned_output = false;
106 status_t res;
107 std::vector<StreamBuffer> modified_output_buffers;
108 for (uint32_t i = 0; i < result->output_buffers.size(); i++) {
109 if (stream_id_ == result->output_buffers[i].stream_id) {
110 returned_output = true;
111 res = internal_stream_manager_->ReturnFilledBuffer(
112 result->frame_number, result->output_buffers[i]);
113 if (res != OK) {
114 ALOGW("%s: (%d)ReturnStreamBuffer fail", __FUNCTION__,
115 result->frame_number);
116 }
117 } else {
118 modified_output_buffers.push_back(result->output_buffers[i]);
119 }
120 }
121
122 if (result->output_buffers.size() > 0) {
123 result->output_buffers.clear();
124 result->output_buffers = modified_output_buffers;
125 }
126
127 if (result->result_metadata) {
128 result->result_metadata->Erase(ANDROID_CONTROL_ENABLE_ZSL);
129
130 res = internal_stream_manager_->ReturnMetadata(
131 stream_id_, result->frame_number, result->result_metadata.get(),
132 result->partial_result);
133 if (res != OK) {
134 ALOGW("%s: (%d)ReturnMetadata fail", __FUNCTION__, result->frame_number);
135 }
136
137 if (result->partial_result == partial_result_count_) {
138 res =
139 hal_utils::SetEnableZslMetadata(result->result_metadata.get(), false);
140 if (res != OK) {
141 ALOGW("%s: SetEnableZslMetadata (%d) fail", __FUNCTION__,
142 result->frame_number);
143 }
144 }
145 }
146
147 // Don't send result to framework if only internal callback
148 if (returned_output && result->result_metadata == nullptr &&
149 result->output_buffers.size() == 0) {
150 return;
151 }
152
153 process_capture_result_(std::move(result));
154 }
155
Notify(const ProcessBlockNotifyMessage & block_message)156 void RealtimeZslResultProcessor::Notify(
157 const ProcessBlockNotifyMessage& block_message) {
158 ATRACE_CALL();
159 std::lock_guard<std::mutex> lock(callback_lock_);
160 const NotifyMessage& message = block_message.message;
161 if (notify_ == nullptr) {
162 ALOGE("%s: notify_ is nullptr. Dropping a message.", __FUNCTION__);
163 return;
164 }
165
166 // Do not notify errors for internal streams
167 if (message.type == MessageType::kError &&
168 message.message.error.error_stream_id == stream_id_) {
169 return;
170 }
171
172 notify_(message);
173 }
174
FlushPendingRequests()175 status_t RealtimeZslResultProcessor::FlushPendingRequests() {
176 ATRACE_CALL();
177 return INVALID_OPERATION;
178 }
179
180 } // namespace google_camera_hal
181 } // namespace android
182