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_RealtimeZslResultProcessor"
19 #define ATRACE_TAG ATRACE_TAG_CAMERA
20 
21 #include "realtime_zsl_result_processor.h"
22 
23 #include <inttypes.h>
24 #include <log/log.h>
25 #include <utils/Trace.h>
26 
27 #include "hal_utils.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 
41   auto result_processor = std::unique_ptr<RealtimeZslResultProcessor>(
42       new RealtimeZslResultProcessor(internal_stream_manager, stream_id,
43                                      pixel_format, partial_result_count));
44   if (result_processor == nullptr) {
45     ALOGE("%s: Creating RealtimeZslResultProcessor failed.", __FUNCTION__);
46     return nullptr;
47   }
48 
49   return result_processor;
50 }
51 
RealtimeZslResultProcessor(InternalStreamManager * internal_stream_manager,int32_t stream_id,android_pixel_format_t pixel_format,uint32_t partial_result_count)52 RealtimeZslResultProcessor::RealtimeZslResultProcessor(
53     InternalStreamManager* internal_stream_manager, int32_t stream_id,
54     android_pixel_format_t pixel_format, uint32_t partial_result_count) {
55   internal_stream_manager_ = internal_stream_manager;
56   stream_id_ = stream_id;
57   pixel_format_ = pixel_format;
58   partial_result_count_ = partial_result_count;
59 }
60 
SetResultCallback(ProcessCaptureResultFunc process_capture_result,NotifyFunc notify)61 void RealtimeZslResultProcessor::SetResultCallback(
62     ProcessCaptureResultFunc process_capture_result, NotifyFunc notify) {
63   std::lock_guard<std::mutex> lock(callback_lock_);
64   process_capture_result_ = process_capture_result;
65   notify_ = notify;
66 }
67 
SaveLsForHdrplus(const CaptureRequest & request)68 void RealtimeZslResultProcessor::SaveLsForHdrplus(const CaptureRequest& request) {
69   if (request.settings != nullptr) {
70     uint8_t lens_shading_map_mode;
71     status_t res =
72         hal_utils::GetLensShadingMapMode(request, &lens_shading_map_mode);
73     if (res == OK) {
74       current_lens_shading_map_mode_ = lens_shading_map_mode;
75     }
76   }
77 
78   {
79     std::lock_guard<std::mutex> lock(lens_shading_lock_);
80     requested_lens_shading_map_modes_.emplace(request.frame_number,
81                                               current_lens_shading_map_mode_);
82   }
83 }
84 
HandleLsResultForHdrplus(uint32_t frameNumber,HalCameraMetadata * metadata)85 status_t RealtimeZslResultProcessor::HandleLsResultForHdrplus(
86     uint32_t frameNumber, HalCameraMetadata* metadata) {
87   if (metadata == nullptr) {
88     ALOGE("%s: metadata is nullptr", __FUNCTION__);
89     return BAD_VALUE;
90   }
91   std::lock_guard<std::mutex> lock(lens_shading_lock_);
92   auto iter = requested_lens_shading_map_modes_.find(frameNumber);
93   if (iter == requested_lens_shading_map_modes_.end()) {
94     ALOGW("%s: can't find frame (%d)", __FUNCTION__, frameNumber);
95     return OK;
96   }
97 
98   if (iter->second == ANDROID_STATISTICS_LENS_SHADING_MAP_MODE_OFF) {
99     status_t res = hal_utils::RemoveLsInfoFromResult(metadata);
100     if (res != OK) {
101       ALOGW("%s: RemoveLsInfoFromResult fail", __FUNCTION__);
102     }
103   }
104   requested_lens_shading_map_modes_.erase(iter);
105 
106   return OK;
107 }
108 
SaveFdForHdrplus(const CaptureRequest & request)109 void RealtimeZslResultProcessor::SaveFdForHdrplus(const CaptureRequest& request) {
110   // Enable face detect mode for internal use
111   if (request.settings != nullptr) {
112     uint8_t fd_mode;
113     status_t res = hal_utils::GetFdMode(request, &fd_mode);
114     if (res == OK) {
115       current_face_detect_mode_ = fd_mode;
116     }
117   }
118 
119   {
120     std::lock_guard<std::mutex> lock(face_detect_lock_);
121     requested_face_detect_modes_.emplace(request.frame_number,
122                                          current_face_detect_mode_);
123   }
124 }
125 
HandleFdResultForHdrplus(uint32_t frameNumber,HalCameraMetadata * metadata)126 status_t RealtimeZslResultProcessor::HandleFdResultForHdrplus(
127     uint32_t frameNumber, HalCameraMetadata* metadata) {
128   if (metadata == nullptr) {
129     ALOGE("%s: metadata is nullptr", __FUNCTION__);
130     return BAD_VALUE;
131   }
132   std::lock_guard<std::mutex> lock(face_detect_lock_);
133   auto iter = requested_face_detect_modes_.find(frameNumber);
134   if (iter == requested_face_detect_modes_.end()) {
135     ALOGW("%s: can't find frame (%d)", __FUNCTION__, frameNumber);
136     return OK;
137   }
138 
139   if (iter->second == ANDROID_STATISTICS_FACE_DETECT_MODE_OFF) {
140     status_t res = hal_utils::RemoveFdInfoFromResult(metadata);
141     if (res != OK) {
142       ALOGW("%s: RestoreFdMetadataForHdrplus fail", __FUNCTION__);
143     }
144   }
145   requested_face_detect_modes_.erase(iter);
146 
147   return OK;
148 }
149 
AddPendingRequests(const std::vector<ProcessBlockRequest> & process_block_requests,const CaptureRequest & remaining_session_request)150 status_t RealtimeZslResultProcessor::AddPendingRequests(
151     const std::vector<ProcessBlockRequest>& process_block_requests,
152     const CaptureRequest& remaining_session_request) {
153   ATRACE_CALL();
154   // This is the last result processor. Sanity check if requests contains
155   // all remaining output buffers.
156   if (!hal_utils::AreAllRemainingBuffersRequested(process_block_requests,
157                                                   remaining_session_request)) {
158     ALOGE("%s: Some output buffers will not be completed.", __FUNCTION__);
159     return BAD_VALUE;
160   }
161 
162   if (pixel_format_ == HAL_PIXEL_FORMAT_RAW10) {
163     SaveFdForHdrplus(remaining_session_request);
164     SaveLsForHdrplus(remaining_session_request);
165   }
166 
167   return OK;
168 }
169 
ProcessResult(ProcessBlockResult block_result)170 void RealtimeZslResultProcessor::ProcessResult(ProcessBlockResult block_result) {
171   ATRACE_CALL();
172   std::lock_guard<std::mutex> lock(callback_lock_);
173   std::unique_ptr<CaptureResult> result = std::move(block_result.result);
174   if (result == nullptr) {
175     ALOGW("%s: Received a nullptr result.", __FUNCTION__);
176     return;
177   }
178 
179   if (process_capture_result_ == nullptr) {
180     ALOGE("%s: process_capture_result_ is nullptr. Dropping a result.",
181           __FUNCTION__);
182     return;
183   }
184 
185   // Return filled raw buffer to internal stream manager
186   // And remove raw buffer from result
187   bool returned_output = false;
188   status_t res;
189   std::vector<StreamBuffer> modified_output_buffers;
190   for (uint32_t i = 0; i < result->output_buffers.size(); i++) {
191     if (stream_id_ == result->output_buffers[i].stream_id) {
192       returned_output = true;
193       res = internal_stream_manager_->ReturnFilledBuffer(
194           result->frame_number, result->output_buffers[i]);
195       if (res != OK) {
196         ALOGW("%s: (%d)ReturnStreamBuffer fail", __FUNCTION__,
197               result->frame_number);
198       }
199     } else {
200       modified_output_buffers.push_back(result->output_buffers[i]);
201     }
202   }
203 
204   if (result->output_buffers.size() > 0) {
205     result->output_buffers.clear();
206     result->output_buffers = modified_output_buffers;
207   }
208 
209   if (result->result_metadata) {
210     result->result_metadata->Erase(ANDROID_CONTROL_ENABLE_ZSL);
211 
212     res = internal_stream_manager_->ReturnMetadata(
213         stream_id_, result->frame_number, result->result_metadata.get(),
214         result->partial_result);
215     if (res != OK) {
216       ALOGW("%s: (%d)ReturnMetadata fail", __FUNCTION__, result->frame_number);
217     }
218 
219     if (result->partial_result == partial_result_count_) {
220       res =
221           hal_utils::SetEnableZslMetadata(result->result_metadata.get(), false);
222       if (res != OK) {
223         ALOGW("%s: SetEnableZslMetadata (%d) fail", __FUNCTION__,
224               result->frame_number);
225       }
226 
227       if (pixel_format_ == HAL_PIXEL_FORMAT_RAW10) {
228         res = HandleFdResultForHdrplus(result->frame_number,
229                                        result->result_metadata.get());
230         if (res != OK) {
231           ALOGE("%s: HandleFdResultForHdrplus(%d) fail", __FUNCTION__,
232                 result->frame_number);
233           return;
234         }
235 
236         res = HandleLsResultForHdrplus(result->frame_number,
237                                        result->result_metadata.get());
238         if (res != OK) {
239           ALOGE("%s: HandleLsResultForHdrplus(%d) fail", __FUNCTION__,
240                 result->frame_number);
241           return;
242         }
243       }
244     }
245   }
246 
247   // Don't send result to framework if only internal raw callback
248   if (returned_output && result->result_metadata == nullptr &&
249       result->output_buffers.size() == 0) {
250     return;
251   }
252   process_capture_result_(std::move(result));
253 }
254 
Notify(const ProcessBlockNotifyMessage & block_message)255 void RealtimeZslResultProcessor::Notify(
256     const ProcessBlockNotifyMessage& block_message) {
257   ATRACE_CALL();
258   std::lock_guard<std::mutex> lock(callback_lock_);
259   const NotifyMessage& message = block_message.message;
260   if (notify_ == nullptr) {
261     ALOGE("%s: notify_ is nullptr. Dropping a message.", __FUNCTION__);
262     return;
263   }
264 
265   notify_(message);
266 }
267 
FlushPendingRequests()268 status_t RealtimeZslResultProcessor::FlushPendingRequests() {
269   ATRACE_CALL();
270   return INVALID_OPERATION;
271 }
272 
273 }  // namespace google_camera_hal
274 }  // namespace android