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 #ifndef EMULATOR_CAMERA_HAL_HWL_REQUEST_PROCESSOR_H
18 #define EMULATOR_CAMERA_HAL_HWL_REQUEST_PROCESSOR_H
19 
20 #include <condition_variable>
21 #include <mutex>
22 #include <queue>
23 #include <thread>
24 
25 #include "EmulatedLogicalRequestState.h"
26 #include "EmulatedSensor.h"
27 #include "hwl_types.h"
28 
29 namespace android {
30 
31 using google_camera_hal::HalCameraMetadata;
32 using google_camera_hal::HalStream;
33 using google_camera_hal::HwlPipelineCallback;
34 using google_camera_hal::HwlPipelineRequest;
35 using google_camera_hal::RequestTemplate;
36 using google_camera_hal::StreamBuffer;
37 
38 struct EmulatedStream : public HalStream {
39   uint32_t width, height;
40   size_t buffer_size;
41   bool is_input;
42 };
43 
44 struct EmulatedPipeline {
45   HwlPipelineCallback cb;
46   // stream id -> stream map
47   std::unordered_map<uint32_t, EmulatedStream> streams;
48   uint32_t physical_camera_id, pipeline_id;
49 };
50 
51 struct PendingRequest {
52   std::unique_ptr<HalCameraMetadata> settings;
53   std::unique_ptr<Buffers> input_buffers;
54   std::unique_ptr<Buffers> output_buffers;
55 };
56 
57 class EmulatedRequestProcessor {
58  public:
59   EmulatedRequestProcessor(uint32_t camera_id, sp<EmulatedSensor> sensor);
60   virtual ~EmulatedRequestProcessor();
61 
62   // Process given pipeline requests and invoke the respective callback in a
63   // separate thread
64   status_t ProcessPipelineRequests(
65       uint32_t frame_number, const std::vector<HwlPipelineRequest>& requests,
66       const std::vector<EmulatedPipeline>& pipelines);
67 
68   status_t GetDefaultRequest(
69       RequestTemplate type,
70       std::unique_ptr<HalCameraMetadata>* default_settings);
71 
72   status_t Flush();
73 
74   status_t Initialize(std::unique_ptr<HalCameraMetadata> static_meta,
75                       PhysicalDeviceMapPtr physical_devices);
76 
77  private:
78   void RequestProcessorLoop();
79 
80   std::thread request_thread_;
81   std::atomic_bool processor_done_ = false;
82 
83   // helper methods
AlignTo(uint32_t value,uint32_t alignment)84   static uint32_t inline AlignTo(uint32_t value, uint32_t alignment) {
85     uint32_t delta = value % alignment;
86     return (delta == 0) ? value : (value + (alignment - delta));
87   }
88 
89   status_t GetBufferSizeAndStride(const EmulatedStream& stream,
90                                   uint32_t* size /*out*/,
91                                   uint32_t* stride /*out*/);
92   status_t LockSensorBuffer(const EmulatedStream& stream,
93                             HandleImporter& importer /*in*/,
94                             buffer_handle_t buffer,
95                             SensorBuffer* sensor_buffer /*out*/);
96   std::unique_ptr<Buffers> CreateSensorBuffers(
97       uint32_t frame_number, const std::vector<StreamBuffer>& buffers,
98       const std::unordered_map<uint32_t, EmulatedStream>& streams,
99       uint32_t pipeline_id, HwlPipelineCallback cb);
100   std::unique_ptr<SensorBuffer> CreateSensorBuffer(uint32_t frame_number,
101                                                    const EmulatedStream& stream,
102                                                    uint32_t pipeline_id,
103                                                    HwlPipelineCallback callback,
104                                                    StreamBuffer stream_buffer);
105   std::unique_ptr<Buffers> AcquireBuffers(Buffers* buffers);
106   void NotifyFailedRequest(const PendingRequest& request);
107 
108   std::mutex process_mutex_;
109   std::condition_variable request_condition_;
110   std::queue<PendingRequest> pending_requests_;
111   uint32_t camera_id_;
112   sp<EmulatedSensor> sensor_;
113   std::unique_ptr<EmulatedLogicalRequestState>
114       request_state_;  // Stores and handles 3A and related camera states.
115   std::unique_ptr<HalCameraMetadata> last_settings_;
116 
117   EmulatedRequestProcessor(const EmulatedRequestProcessor&) = delete;
118   EmulatedRequestProcessor& operator=(const EmulatedRequestProcessor&) = delete;
119 };
120 
121 }  // namespace android
122 
123 #endif  // EMULATOR_CAMERA_HAL_HWL_REQUEST_PROCESSOR_H
124