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 <memory> 22 #include <mutex> 23 #include <queue> 24 #include <thread> 25 26 #include "EmulatedLogicalRequestState.h" 27 #include "EmulatedSensor.h" 28 #include "HandleImporter.h" 29 #include "android/frameworks/sensorservice/1.0/ISensorManager.h" 30 #include "android/frameworks/sensorservice/1.0/types.h" 31 #include "hwl_types.h" 32 33 namespace android { 34 35 using ::android::frameworks::sensorservice::V1_0::IEventQueue; 36 using ::android::frameworks::sensorservice::V1_0::IEventQueueCallback; 37 using ::android::hardware::Return; 38 using ::android::hardware::Void; 39 using android::hardware::camera::common::V1_0::helper::HandleImporter; 40 using ::android::hardware::sensors::V1_0::Event; 41 using google_camera_hal::HalCameraMetadata; 42 using google_camera_hal::HwlPipelineRequest; 43 using google_camera_hal::HwlSessionCallback; 44 using google_camera_hal::RequestTemplate; 45 using google_camera_hal::StreamBuffer; 46 47 struct PendingRequest { 48 uint32_t frame_number; 49 uint32_t pipeline_id; 50 HwlPipelineCallback callback; 51 std::unique_ptr<HalCameraMetadata> settings; 52 std::unique_ptr<Buffers> input_buffers; 53 std::unique_ptr<Buffers> output_buffers; 54 }; 55 56 struct OverrideRequest { 57 uint32_t frame_number; 58 std::unique_ptr<HalCameraMetadata> settings; 59 }; 60 61 class EmulatedRequestProcessor { 62 public: 63 EmulatedRequestProcessor(uint32_t camera_id, sp<EmulatedSensor> sensor, 64 const HwlSessionCallback& session_callback); 65 virtual ~EmulatedRequestProcessor(); 66 67 // Process given pipeline requests and invoke the respective callback in a 68 // separate thread 69 status_t ProcessPipelineRequests( 70 uint32_t frame_number, std::vector<HwlPipelineRequest>& requests, 71 const std::vector<EmulatedPipeline>& pipelines, 72 const DynamicStreamIdMapType& dynamic_stream_id_map, 73 bool use_default_physical_camera); 74 75 status_t GetDefaultRequest( 76 RequestTemplate type, 77 std::unique_ptr<HalCameraMetadata>* default_settings); 78 79 status_t Flush(); 80 81 status_t Initialize(std::unique_ptr<EmulatedCameraDeviceInfo> device_info, 82 PhysicalDeviceMapPtr physical_devices); 83 void InitializeSensorQueue(std::weak_ptr<EmulatedRequestProcessor> processor); 84 85 void SetSessionCallback(const HwlSessionCallback& hwl_session_callback); 86 87 private: 88 class SensorHandler : public IEventQueueCallback { 89 public: SensorHandler(std::weak_ptr<EmulatedRequestProcessor> processor)90 SensorHandler(std::weak_ptr<EmulatedRequestProcessor> processor) 91 : processor_(processor) { 92 } 93 94 // IEventQueueCallback interface 95 Return<void> onEvent(const Event& e) override; 96 97 private: 98 std::weak_ptr<EmulatedRequestProcessor> processor_; 99 }; 100 101 int32_t sensor_handle_; 102 sp<IEventQueue> sensor_event_queue_; 103 std::atomic_uint32_t screen_rotation_; 104 105 void RequestProcessorLoop(); 106 107 std::thread request_thread_; 108 std::atomic_bool processor_done_ = false; 109 110 // Speed up zoom by 2 frames using settings override 111 const static uint32_t kZoomSpeedup = 2; 112 113 // helper methods AlignTo(uint32_t value,uint32_t alignment)114 static uint32_t inline AlignTo(uint32_t value, uint32_t alignment) { 115 uint32_t delta = value % alignment; 116 return (delta == 0) ? value : (value + (alignment - delta)); 117 } 118 119 // Return buffer size and row stride in bytes 120 status_t GetBufferSizeAndStride(const EmulatedStream& stream, 121 buffer_handle_t buffer, uint32_t* size /*out*/, 122 uint32_t* stride /*out*/); 123 status_t LockSensorBuffer(const EmulatedStream& stream, 124 buffer_handle_t buffer, int32_t width, 125 int32_t height, SensorBuffer* sensor_buffer /*out*/); 126 std::unique_ptr<Buffers> CreateSensorBuffers( 127 uint32_t frame_number, const std::vector<StreamBuffer>& buffers, 128 const std::unordered_map<uint32_t, EmulatedStream>& streams, 129 uint32_t pipeline_id, HwlPipelineCallback cb, int32_t override_width, 130 int32_t override_height); 131 std::unique_ptr<SensorBuffer> CreateSensorBuffer( 132 uint32_t frame_number, const EmulatedStream& stream, uint32_t pipeline_id, 133 HwlPipelineCallback callback, StreamBuffer stream_buffer, 134 int32_t override_width, int32_t override_height); 135 std::unique_ptr<Buffers> AcquireBuffers(Buffers* buffers); 136 void NotifyFailedRequest(const PendingRequest& request); 137 uint32_t ApplyOverrideSettings( 138 uint32_t frame_number, 139 const std::unique_ptr<HalCameraMetadata>& request_settings); 140 void ApplyOverrideZoom( 141 const std::unique_ptr<HalCameraMetadata>& override_setting, 142 const std::unique_ptr<HalCameraMetadata>& request_settings, 143 camera_metadata_tag tag); 144 145 std::mutex process_mutex_; 146 std::condition_variable request_condition_; 147 std::queue<PendingRequest> pending_requests_; 148 std::queue<OverrideRequest> override_settings_; 149 uint32_t camera_id_; 150 sp<EmulatedSensor> sensor_; 151 HwlSessionCallback session_callback_; 152 std::unique_ptr<EmulatedLogicalRequestState> 153 request_state_; // Stores and handles 3A and related camera states. 154 std::unique_ptr<HalCameraMetadata> last_settings_; 155 std::unique_ptr<HalCameraMetadata> last_override_settings_; 156 std::shared_ptr<HandleImporter> importer_; 157 158 EmulatedRequestProcessor(const EmulatedRequestProcessor&) = delete; 159 EmulatedRequestProcessor& operator=(const EmulatedRequestProcessor&) = delete; 160 }; 161 162 } // namespace android 163 164 #endif // EMULATOR_CAMERA_HAL_HWL_REQUEST_PROCESSOR_H 165