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_HdrplusRequestProcessor"
19 #define ATRACE_TAG ATRACE_TAG_CAMERA
20 #include <log/log.h>
21 #include <utils/Trace.h>
22 
23 #include "hdrplus_request_processor.h"
24 #include "vendor_tag_defs.h"
25 
26 namespace android {
27 namespace google_camera_hal {
28 
Create(CameraDeviceSessionHwl * device_session_hwl,int32_t raw_stream_id,uint32_t physical_camera_id)29 std::unique_ptr<HdrplusRequestProcessor> HdrplusRequestProcessor::Create(
30     CameraDeviceSessionHwl* device_session_hwl, int32_t raw_stream_id,
31     uint32_t physical_camera_id) {
32   ATRACE_CALL();
33   if (device_session_hwl == nullptr) {
34     ALOGE("%s: device_session_hwl (%p) is nullptr", __FUNCTION__,
35           device_session_hwl);
36     return nullptr;
37   }
38 
39   auto request_processor = std::unique_ptr<HdrplusRequestProcessor>(
40       new HdrplusRequestProcessor(physical_camera_id));
41   if (request_processor == nullptr) {
42     ALOGE("%s: Creating HdrplusRequestProcessor failed.", __FUNCTION__);
43     return nullptr;
44   }
45 
46   status_t res =
47       request_processor->Initialize(device_session_hwl, raw_stream_id);
48   if (res != OK) {
49     ALOGE("%s: Initializing HdrplusRequestProcessor failed: %s (%d).",
50           __FUNCTION__, strerror(-res), res);
51     return nullptr;
52   }
53 
54   return request_processor;
55 }
56 
Initialize(CameraDeviceSessionHwl * device_session_hwl,int32_t raw_stream_id)57 status_t HdrplusRequestProcessor::Initialize(
58     CameraDeviceSessionHwl* device_session_hwl, int32_t raw_stream_id) {
59   ATRACE_CALL();
60   std::unique_ptr<HalCameraMetadata> characteristics;
61   status_t res = NO_INIT;
62   uint32_t num_physical_cameras =
63       device_session_hwl->GetPhysicalCameraIds().size();
64   if (num_physical_cameras > 0) {
65     res = device_session_hwl->GetPhysicalCameraCharacteristics(
66         kCameraId, &characteristics);
67     if (res != OK) {
68       ALOGE("%s: GetPhysicalCameraCharacteristics failed.", __FUNCTION__);
69       return BAD_VALUE;
70     }
71   } else {
72     res = device_session_hwl->GetCameraCharacteristics(&characteristics);
73     if (res != OK) {
74       ALOGE("%s: GetCameraCharacteristics failed.", __FUNCTION__);
75       return BAD_VALUE;
76     }
77   }
78 
79   camera_metadata_ro_entry entry;
80   res = characteristics->Get(
81       ANDROID_SENSOR_INFO_PRE_CORRECTION_ACTIVE_ARRAY_SIZE, &entry);
82   if (res == OK) {
83     active_array_width_ = entry.data.i32[2];
84     active_array_height_ = entry.data.i32[3];
85     ALOGI("%s Active size (%d x %d).", __FUNCTION__, active_array_width_,
86           active_array_height_);
87   } else {
88     ALOGE("%s Get active size failed: %s (%d).", __FUNCTION__, strerror(-res),
89           res);
90     return res;
91   }
92 
93   res = characteristics->Get(VendorTagIds::kHdrplusPayloadFrames, &entry);
94   if (res != OK || entry.data.i32[0] <= 0) {
95     ALOGE("%s: Getting kHdrplusPayloadFrames failed or number <= 0",
96           __FUNCTION__);
97     return BAD_VALUE;
98   }
99   payload_frames_ = entry.data.i32[0];
100   ALOGI("%s: HDR+ payload_frames_: %d", __FUNCTION__, payload_frames_);
101   raw_stream_id_ = raw_stream_id;
102 
103   return OK;
104 }
105 
ConfigureStreams(InternalStreamManager * internal_stream_manager,const StreamConfiguration & stream_config,StreamConfiguration * process_block_stream_config)106 status_t HdrplusRequestProcessor::ConfigureStreams(
107     InternalStreamManager* internal_stream_manager,
108     const StreamConfiguration& stream_config,
109     StreamConfiguration* process_block_stream_config) {
110   ATRACE_CALL();
111   if (process_block_stream_config == nullptr ||
112       internal_stream_manager == nullptr) {
113     ALOGE(
114         "%s: process_block_stream_config (%p) is nullptr or "
115         "internal_stream_manager (%p) is nullptr",
116         __FUNCTION__, process_block_stream_config, internal_stream_manager);
117     return BAD_VALUE;
118   }
119 
120   internal_stream_manager_ = internal_stream_manager;
121 
122   Stream raw_stream;
123   raw_stream.stream_type = StreamType::kInput;
124   raw_stream.width = active_array_width_;
125   raw_stream.height = active_array_height_;
126   raw_stream.format = HAL_PIXEL_FORMAT_RAW10;
127   raw_stream.usage = 0;
128   raw_stream.rotation = StreamRotation::kRotation0;
129   raw_stream.data_space = HAL_DATASPACE_ARBITRARY;
130   // Set id back to raw_stream and then HWL can get correct HAL stream ID
131   raw_stream.id = raw_stream_id_;
132 
133   process_block_stream_config->streams = stream_config.streams;
134   // Add internal RAW stream
135   process_block_stream_config->streams.push_back(raw_stream);
136   process_block_stream_config->operation_mode = stream_config.operation_mode;
137   process_block_stream_config->session_params =
138       HalCameraMetadata::Clone(stream_config.session_params.get());
139   process_block_stream_config->stream_config_counter =
140       stream_config.stream_config_counter;
141   process_block_stream_config->log_id = stream_config.log_id;
142 
143   return OK;
144 }
145 
SetProcessBlock(std::unique_ptr<ProcessBlock> process_block)146 status_t HdrplusRequestProcessor::SetProcessBlock(
147     std::unique_ptr<ProcessBlock> process_block) {
148   ATRACE_CALL();
149   if (process_block == nullptr) {
150     ALOGE("%s: process_block is nullptr", __FUNCTION__);
151     return BAD_VALUE;
152   }
153 
154   std::lock_guard<std::mutex> lock(process_block_lock_);
155   if (process_block_ != nullptr) {
156     ALOGE("%s: Already configured.", __FUNCTION__);
157     return ALREADY_EXISTS;
158   }
159 
160   process_block_ = std::move(process_block);
161   return OK;
162 }
163 
IsReadyForNextRequest()164 bool HdrplusRequestProcessor::IsReadyForNextRequest() {
165   ATRACE_CALL();
166   if (internal_stream_manager_ == nullptr) {
167     ALOGW("%s: internal_stream_manager_ nullptr", __FUNCTION__);
168     return false;
169   }
170   if (internal_stream_manager_->IsPendingBufferEmpty(raw_stream_id_) == false) {
171     return false;
172   }
173   return true;
174 }
175 
RemoveJpegMetadata(std::vector<std::unique_ptr<HalCameraMetadata>> * metadata)176 void HdrplusRequestProcessor::RemoveJpegMetadata(
177     std::vector<std::unique_ptr<HalCameraMetadata>>* metadata) {
178   const uint32_t tags[] = {
179       ANDROID_JPEG_THUMBNAIL_SIZE,  ANDROID_JPEG_ORIENTATION,
180       ANDROID_JPEG_QUALITY,         ANDROID_JPEG_THUMBNAIL_QUALITY,
181       ANDROID_JPEG_GPS_COORDINATES, ANDROID_JPEG_GPS_PROCESSING_METHOD,
182       ANDROID_JPEG_GPS_TIMESTAMP};
183   if (metadata == nullptr) {
184     ALOGW("%s: metadata is nullptr", __FUNCTION__);
185     return;
186   }
187 
188   for (uint32_t i = 0; i < metadata->size(); i++) {
189     for (uint32_t tag_index = 0; tag_index < sizeof(tags) / sizeof(uint32_t);
190          tag_index++) {
191       if (metadata->at(i) == nullptr) {
192         continue;
193       }
194       status_t res = metadata->at(i)->Erase(tags[tag_index]);
195       if (res != OK) {
196         ALOGW("%s: (%d)erase index(%d) failed: %s(%d)", __FUNCTION__, i,
197               tag_index, strerror(-res), res);
198       }
199     }
200   }
201 }
202 
ProcessRequest(const CaptureRequest & request)203 status_t HdrplusRequestProcessor::ProcessRequest(const CaptureRequest& request) {
204   ATRACE_CALL();
205   std::lock_guard<std::mutex> lock(process_block_lock_);
206   if (process_block_ == nullptr) {
207     ALOGE("%s: Not configured yet.", __FUNCTION__);
208     return NO_INIT;
209   }
210 
211   if (IsReadyForNextRequest() == false) {
212     return BAD_VALUE;
213   }
214 
215   CaptureRequest block_request;
216   block_request.frame_number = request.frame_number;
217   block_request.settings = HalCameraMetadata::Clone(request.settings.get());
218   block_request.output_buffers = request.output_buffers;
219   for (auto& [camera_id, physical_metadata] : request.physical_camera_settings) {
220     block_request.physical_camera_settings[camera_id] =
221         HalCameraMetadata::Clone(physical_metadata.get());
222   }
223 
224   // Get multiple raw buffer and metadata from internal stream as input
225   status_t result = internal_stream_manager_->GetMostRecentStreamBuffer(
226       raw_stream_id_, &(block_request.input_buffers),
227       &(block_request.input_buffer_metadata), payload_frames_);
228   if (result != OK) {
229     ALOGE("%s: frame:%d GetStreamBuffer failed.", __FUNCTION__,
230           request.frame_number);
231     return UNKNOWN_ERROR;
232   }
233 
234   RemoveJpegMetadata(&(block_request.input_buffer_metadata));
235   std::vector<ProcessBlockRequest> block_requests(1);
236   block_requests[0].request = std::move(block_request);
237   ALOGD("%s: frame number %u is an HDR+ request.", __FUNCTION__,
238         request.frame_number);
239 
240   return process_block_->ProcessRequests(block_requests, request);
241 }
242 
Flush()243 status_t HdrplusRequestProcessor::Flush() {
244   ATRACE_CALL();
245   std::lock_guard<std::mutex> lock(process_block_lock_);
246   if (process_block_ == nullptr) {
247     return OK;
248   }
249 
250   return process_block_->Flush();
251 }
252 
253 }  // namespace google_camera_hal
254 }  // namespace android