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