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 
142   return OK;
143 }
144 
SetProcessBlock(std::unique_ptr<ProcessBlock> process_block)145 status_t HdrplusRequestProcessor::SetProcessBlock(
146     std::unique_ptr<ProcessBlock> process_block) {
147   ATRACE_CALL();
148   if (process_block == nullptr) {
149     ALOGE("%s: process_block is nullptr", __FUNCTION__);
150     return BAD_VALUE;
151   }
152 
153   std::lock_guard<std::mutex> lock(process_block_lock_);
154   if (process_block_ != nullptr) {
155     ALOGE("%s: Already configured.", __FUNCTION__);
156     return ALREADY_EXISTS;
157   }
158 
159   process_block_ = std::move(process_block);
160   return OK;
161 }
162 
IsReadyForNextRequest()163 bool HdrplusRequestProcessor::IsReadyForNextRequest() {
164   ATRACE_CALL();
165   if (internal_stream_manager_ == nullptr) {
166     ALOGW("%s: internal_stream_manager_ nullptr", __FUNCTION__);
167     return false;
168   }
169   if (internal_stream_manager_->IsPendingBufferEmpty(raw_stream_id_) == false) {
170     return false;
171   }
172   return true;
173 }
174 
RemoveJpegMetadata(std::vector<std::unique_ptr<HalCameraMetadata>> * metadata)175 void HdrplusRequestProcessor::RemoveJpegMetadata(
176     std::vector<std::unique_ptr<HalCameraMetadata>>* metadata) {
177   const uint32_t tags[] = {
178       ANDROID_JPEG_THUMBNAIL_SIZE,  ANDROID_JPEG_ORIENTATION,
179       ANDROID_JPEG_QUALITY,         ANDROID_JPEG_THUMBNAIL_QUALITY,
180       ANDROID_JPEG_GPS_COORDINATES, ANDROID_JPEG_GPS_PROCESSING_METHOD,
181       ANDROID_JPEG_GPS_TIMESTAMP};
182   if (metadata == nullptr) {
183     ALOGW("%s: metadata is nullptr", __FUNCTION__);
184     return;
185   }
186 
187   for (uint32_t i = 0; i < metadata->size(); i++) {
188     for (uint32_t tag_index = 0; tag_index < sizeof(tags) / sizeof(uint32_t);
189          tag_index++) {
190       if (metadata->at(i) == nullptr) {
191         continue;
192       }
193       status_t res = metadata->at(i)->Erase(tags[tag_index]);
194       if (res != OK) {
195         ALOGW("%s: (%d)erase index(%d) failed: %s(%d)", __FUNCTION__, i,
196               tag_index, strerror(-res), res);
197       }
198     }
199   }
200 }
201 
ProcessRequest(const CaptureRequest & request)202 status_t HdrplusRequestProcessor::ProcessRequest(const CaptureRequest& request) {
203   ATRACE_CALL();
204   std::lock_guard<std::mutex> lock(process_block_lock_);
205   if (process_block_ == nullptr) {
206     ALOGE("%s: Not configured yet.", __FUNCTION__);
207     return NO_INIT;
208   }
209 
210   if (IsReadyForNextRequest() == false) {
211     return BAD_VALUE;
212   }
213 
214   CaptureRequest block_request;
215   block_request.frame_number = request.frame_number;
216   block_request.settings = HalCameraMetadata::Clone(request.settings.get());
217   block_request.output_buffers = request.output_buffers;
218   for (auto& [camera_id, physical_metadata] : request.physical_camera_settings) {
219     block_request.physical_camera_settings[camera_id] =
220         HalCameraMetadata::Clone(physical_metadata.get());
221   }
222 
223   // Get multiple raw buffer and metadata from internal stream as input
224   status_t result = internal_stream_manager_->GetMostRecentStreamBuffer(
225       raw_stream_id_, &(block_request.input_buffers),
226       &(block_request.input_buffer_metadata), payload_frames_);
227   if (result != OK) {
228     ALOGE("%s: frame:%d GetStreamBuffer failed.", __FUNCTION__,
229           request.frame_number);
230     return UNKNOWN_ERROR;
231   }
232 
233   RemoveJpegMetadata(&(block_request.input_buffer_metadata));
234   std::vector<ProcessBlockRequest> block_requests(1);
235   block_requests[0].request = std::move(block_request);
236   ALOGD("%s: frame number %u is an HDR+ request.", __FUNCTION__,
237         request.frame_number);
238 
239   return process_block_->ProcessRequests(block_requests, request);
240 }
241 
Flush()242 status_t HdrplusRequestProcessor::Flush() {
243   ATRACE_CALL();
244   std::lock_guard<std::mutex> lock(process_block_lock_);
245   if (process_block_ == nullptr) {
246     return OK;
247   }
248 
249   return process_block_->Flush();
250 }
251 
252 }  // namespace google_camera_hal
253 }  // namespace android