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_BasicRequestProcessor"
19 #define ATRACE_TAG ATRACE_TAG_CAMERA
20 #include "basic_request_processor.h"
21 
22 #include <log/log.h>
23 #include <utils/Trace.h>
24 
25 namespace android {
26 namespace google_camera_hal {
27 
Create(CameraDeviceSessionHwl * device_session_hwl)28 std::unique_ptr<BasicRequestProcessor> BasicRequestProcessor::Create(
29     CameraDeviceSessionHwl* device_session_hwl) {
30   ATRACE_CALL();
31   if (device_session_hwl == nullptr) {
32     ALOGE("%s: device_session_hwl is nullptr", __FUNCTION__);
33     return nullptr;
34   }
35 
36   auto request_processor =
37       std::unique_ptr<BasicRequestProcessor>(new BasicRequestProcessor());
38   if (request_processor == nullptr) {
39     ALOGE("%s: Creating BasicRequestProcessor failed.", __FUNCTION__);
40     return nullptr;
41   }
42 
43   return request_processor;
44 }
45 
ConfigureStreams(InternalStreamManager *,const StreamConfiguration & stream_config,StreamConfiguration * process_block_stream_config)46 status_t BasicRequestProcessor::ConfigureStreams(
47     InternalStreamManager* /*internal_stream_manager*/,
48     const StreamConfiguration& stream_config,
49     StreamConfiguration* process_block_stream_config) {
50   ATRACE_CALL();
51   if (process_block_stream_config == nullptr) {
52     ALOGE("%s: process_block_stream_config is nullptr", __FUNCTION__);
53     return BAD_VALUE;
54   }
55 
56   process_block_stream_config->streams = stream_config.streams;
57   process_block_stream_config->operation_mode = stream_config.operation_mode;
58   process_block_stream_config->session_params =
59       HalCameraMetadata::Clone(stream_config.session_params.get());
60   process_block_stream_config->stream_config_counter =
61       stream_config.stream_config_counter;
62   process_block_stream_config->multi_resolution_input_image =
63       stream_config.multi_resolution_input_image;
64   process_block_stream_config->log_id = stream_config.log_id;
65 
66   return OK;
67 }
68 
SetProcessBlock(std::unique_ptr<ProcessBlock> process_block)69 status_t BasicRequestProcessor::SetProcessBlock(
70     std::unique_ptr<ProcessBlock> process_block) {
71   ATRACE_CALL();
72   if (process_block == nullptr) {
73     ALOGE("%s: process_block is nullptr", __FUNCTION__);
74     return BAD_VALUE;
75   }
76 
77   std::lock_guard lock(process_block_shared_lock_);
78   if (process_block_ != nullptr) {
79     ALOGE("%s: Already configured.", __FUNCTION__);
80     return ALREADY_EXISTS;
81   }
82 
83   process_block_ = std::move(process_block);
84   return OK;
85 }
86 
ProcessRequest(const CaptureRequest & request)87 status_t BasicRequestProcessor::ProcessRequest(const CaptureRequest& request) {
88   ATRACE_CALL();
89   std::shared_lock lock(process_block_shared_lock_);
90   if (process_block_ == nullptr) {
91     ALOGE("%s: Not configured yet.", __FUNCTION__);
92     return NO_INIT;
93   }
94 
95   CaptureRequest block_request;
96   block_request.frame_number = request.frame_number;
97   block_request.settings = HalCameraMetadata::Clone(request.settings.get());
98   block_request.input_buffers = request.input_buffers;
99   block_request.input_width = request.input_width;
100   block_request.input_height = request.input_height;
101 
102   for (auto& metadata : request.input_buffer_metadata) {
103     block_request.input_buffer_metadata.push_back(
104         HalCameraMetadata::Clone(metadata.get()));
105   }
106 
107   block_request.output_buffers = request.output_buffers;
108   for (auto& [camera_id, physical_metadata] : request.physical_camera_settings) {
109     block_request.physical_camera_settings[camera_id] =
110         HalCameraMetadata::Clone(physical_metadata.get());
111   }
112 
113   std::vector<ProcessBlockRequest> block_requests(1);
114   block_requests[0].request = std::move(block_request);
115 
116   return process_block_->ProcessRequests(block_requests, request);
117 }
118 
Flush()119 status_t BasicRequestProcessor::Flush() {
120   ATRACE_CALL();
121   std::shared_lock lock(process_block_shared_lock_);
122   if (process_block_ == nullptr) {
123     return OK;
124   }
125 
126   return process_block_->Flush();
127 }
128 
RepeatingRequestEnd(int32_t frame_number,const std::vector<int32_t> & stream_ids)129 void BasicRequestProcessor::RepeatingRequestEnd(
130     int32_t frame_number, const std::vector<int32_t>& stream_ids) {
131   ATRACE_CALL();
132   std::shared_lock lock(process_block_shared_lock_);
133   if (process_block_ != nullptr) {
134     process_block_->RepeatingRequestEnd(frame_number, stream_ids);
135   }
136 }
137 
138 }  // namespace google_camera_hal
139 }  // namespace android
140