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 HARDWARE_GOOGLE_CAMERA_HAL_HIDL_SERVICE_HIDL_CAMERA_DEVICE_SESSION_H_ 18 #define HARDWARE_GOOGLE_CAMERA_HAL_HIDL_SERVICE_HIDL_CAMERA_DEVICE_SESSION_H_ 19 20 #include <android/hardware/camera/device/3.5/ICameraDeviceCallback.h> 21 #include <android/hardware/camera/device/3.7/ICameraDevice.h> 22 #include <android/hardware/camera/device/3.7/ICameraDeviceSession.h> 23 #include <android/hardware/thermal/2.0/IThermal.h> 24 #include <fmq/MessageQueue.h> 25 26 #include <shared_mutex> 27 28 #include "camera_device_session.h" 29 #include "hidl_profiler.h" 30 #include "hidl_thermal_utils.h" 31 32 namespace android { 33 namespace hardware { 34 namespace camera { 35 namespace device { 36 namespace V3_7 { 37 namespace implementation { 38 39 using ::android::hardware::camera::common::V1_0::Status; 40 using ::android::hardware::camera::device::V3_2::BufferCache; 41 using ::android::hardware::camera::device::V3_2::RequestTemplate; 42 using ::android::hardware::camera::device::V3_5::ICameraDeviceCallback; 43 using ::android::hardware::camera::device::V3_7::CaptureRequest; 44 using ::android::hardware::camera::device::V3_7::ICameraDeviceSession; 45 using ::android::hardware::camera::device::V3_7::StreamConfiguration; 46 using ::android::hardware::camera::implementation::HidlProfiler; 47 48 using MetadataQueue = 49 ::android::hardware::MessageQueue<uint8_t, kSynchronizedReadWrite>; 50 51 // HidlCameraDeviceSession implements the HIDL camera device session interface, 52 // ICameraDeviceSession, that contains the methods to configure and request 53 // captures from an active camera device. 54 class HidlCameraDeviceSession : public ICameraDeviceSession { 55 public: 56 // Create a HidlCameraDeviceSession. 57 // device_session is a google camera device session that 58 // HidlCameraDeviceSession is going to manage. Creating a 59 // HidlCameraDeviceSession will fail if device_session is 60 // nullptr. 61 static std::unique_ptr<HidlCameraDeviceSession> Create( 62 const sp<V3_2::ICameraDeviceCallback>& callback, 63 std::unique_ptr<google_camera_hal::CameraDeviceSession> device_session, 64 std::shared_ptr<HidlProfiler> hidl_profiler); 65 66 virtual ~HidlCameraDeviceSession(); 67 68 // Override functions in ICameraDeviceSession 69 Return<void> configureStreams_3_7( 70 const StreamConfiguration& requestedConfiguration, 71 configureStreams_3_7_cb _hidl_cb) override; 72 73 Return<void> processCaptureRequest_3_7( 74 const hidl_vec<CaptureRequest>& requests, 75 const hidl_vec<BufferCache>& cachesToRemove, 76 processCaptureRequest_3_7_cb _hidl_cb) override; 77 78 Return<void> configureStreams_3_6( 79 const V3_5::StreamConfiguration& requestedConfiguration, 80 ICameraDeviceSession::configureStreams_3_6_cb _hidl_cb) override; 81 82 Return<void> switchToOffline(const hidl_vec<int32_t>& streamsToKeep, 83 switchToOffline_cb _hidl_cb) override; 84 85 Return<void> constructDefaultRequestSettings( 86 RequestTemplate type, 87 ICameraDeviceSession::constructDefaultRequestSettings_cb _hidl_cb) override; 88 89 Return<void> configureStreams_3_5( 90 const V3_5::StreamConfiguration& requestedConfiguration, 91 ICameraDeviceSession::configureStreams_3_5_cb _hidl_cb) override; 92 93 Return<void> getCaptureRequestMetadataQueue( 94 ICameraDeviceSession::getCaptureRequestMetadataQueue_cb _hidl_cb) override; 95 96 Return<void> getCaptureResultMetadataQueue( 97 ICameraDeviceSession::getCaptureResultMetadataQueue_cb _hidl_cb) override; 98 99 Return<void> processCaptureRequest_3_4( 100 const hidl_vec<V3_4::CaptureRequest>& requests, 101 const hidl_vec<BufferCache>& cachesToRemove, 102 processCaptureRequest_3_4_cb _hidl_cb) override; 103 104 Return<void> signalStreamFlush(const hidl_vec<int32_t>& streamIds, 105 uint32_t streamConfigCounter) override; 106 107 Return<void> isReconfigurationRequired(const V3_2::CameraMetadata& oldSessionParams, 108 const V3_2::CameraMetadata& newSessionParams, 109 ICameraDeviceSession::isReconfigurationRequired_cb _hidl_cb) override; 110 111 Return<Status> flush() override; 112 113 Return<void> close() override; 114 115 // Legacy methods 116 Return<void> configureStreams(const V3_2::StreamConfiguration&, 117 configureStreams_cb _hidl_cb) override; 118 119 Return<void> configureStreams_3_3(const V3_2::StreamConfiguration&, 120 configureStreams_3_3_cb _hidl_cb) override; 121 122 Return<void> configureStreams_3_4(const V3_4::StreamConfiguration&, 123 configureStreams_3_4_cb _hidl_cb) override; 124 125 Return<void> processCaptureRequest( 126 const hidl_vec<V3_2::CaptureRequest>& requests, 127 const hidl_vec<BufferCache>& cachesToRemove, 128 processCaptureRequest_cb _hidl_cb) override; 129 // End of override functions in ICameraDeviceSession 130 131 protected: 132 HidlCameraDeviceSession() = default; 133 134 private: 135 static constexpr uint32_t kRequestMetadataQueueSizeBytes = 1 << 20; // 1MB 136 static constexpr uint32_t kResultMetadataQueueSizeBytes = 1 << 20; // 1MB 137 138 // Initialize the latest available gralloc buffer mapper. 139 status_t InitializeBufferMapper(); 140 141 // Initialize HidlCameraDeviceSession with a CameraDeviceSession. 142 status_t Initialize( 143 const sp<V3_2::ICameraDeviceCallback>& callback, 144 std::unique_ptr<google_camera_hal::CameraDeviceSession> device_session, 145 std::shared_ptr<HidlProfiler> hidl_profiler); 146 147 // Create a metadata queue. 148 // If override_size_property contains a valid size, it will create a metadata 149 // queue of that size. If it override_size_property doesn't contain a valid 150 // size, it will create a metadata queue of the default size. 151 // default_size_bytes is the default size of the message queue in bytes. 152 // override_size_property is the name of the system property that contains 153 // the message queue size. 154 status_t CreateMetadataQueue(std::unique_ptr<MetadataQueue>* metadata_queue, 155 uint32_t default_size_bytes, 156 const char* override_size_property); 157 158 // Invoked when receiving a result from HAL. 159 void ProcessCaptureResult( 160 std::unique_ptr<google_camera_hal::CaptureResult> hal_result); 161 162 // Invoked when reciving a message from HAL. 163 void NotifyHalMessage(const google_camera_hal::NotifyMessage& hal_message); 164 165 // Invoked when requesting stream buffers from HAL. 166 google_camera_hal::BufferRequestStatus RequestStreamBuffers( 167 const std::vector<google_camera_hal::BufferRequest>& hal_buffer_requests, 168 std::vector<google_camera_hal::BufferReturn>* hal_buffer_returns); 169 170 // Invoked when returning stream buffers from HAL. 171 void ReturnStreamBuffers( 172 const std::vector<google_camera_hal::StreamBuffer>& return_hal_buffers); 173 174 // Import a buffer handle. 175 template <class T, class U> 176 buffer_handle_t ImportBufferHandle(const sp<T> buffer_mapper_, 177 const hidl_handle& buffer_hidl_handle); 178 179 // Set camera device session callbacks. 180 void SetSessionCallbacks(); 181 182 // Register a thermal changed callback. 183 // notify_throttling will be invoked when thermal status changes. 184 // If filter_type is false, type will be ignored and all types will be 185 // monitored. 186 // If filter_type is true, only type will be monitored. 187 status_t RegisterThermalChangedCallback( 188 google_camera_hal::NotifyThrottlingFunc notify_throttling, 189 bool filter_type, google_camera_hal::TemperatureType type); 190 191 // Unregister thermal changed callback. 192 void UnregisterThermalChangedCallback(); 193 194 std::unique_ptr<google_camera_hal::CameraDeviceSession> device_session_; 195 196 // Metadata queue to read the request metadata from. 197 std::unique_ptr<MetadataQueue> request_metadata_queue_; 198 199 // Metadata queue to write the result metadata to. 200 std::unique_ptr<MetadataQueue> result_metadata_queue_; 201 202 // Assuming callbacks to framework is thread-safe, the shared mutex is only 203 // used to protect member variable writing and reading. 204 std::shared_mutex hidl_device_callback_lock_; 205 // Protected by hidl_device_callback_lock_ 206 sp<ICameraDeviceCallback> hidl_device_callback_; 207 208 sp<android::hardware::graphics::mapper::V2_0::IMapper> buffer_mapper_v2_; 209 sp<android::hardware::graphics::mapper::V3_0::IMapper> buffer_mapper_v3_; 210 sp<android::hardware::graphics::mapper::V4_0::IMapper> buffer_mapper_v4_; 211 212 std::mutex hidl_thermal_mutex_; 213 sp<android::hardware::thermal::V2_0::IThermal> thermal_; 214 215 // Must be protected by hidl_thermal_mutex_. 216 sp<android::hardware::thermal::V2_0::IThermalChangedCallback> 217 thermal_changed_callback_; 218 219 // Flag for profiling first frame processing time. 220 bool first_frame_requested_ = false; 221 222 // The frame number of first capture request after configure stream 223 uint32_t first_request_frame_number_ = 0; 224 225 std::mutex pending_first_frame_buffers_mutex_; 226 // Profiling first frame process time. Stop timer when it become 0. 227 // Must be protected by pending_first_frame_buffers_mutex_ 228 size_t num_pending_first_frame_buffers_ = 0; 229 230 std::shared_ptr<HidlProfiler> hidl_profiler_; 231 }; 232 233 } // namespace implementation 234 } // namespace V3_7 235 } // namespace device 236 } // namespace camera 237 } // namespace hardware 238 } // namespace android 239 240 #endif // HARDWARE_GOOGLE_CAMERA_HAL_HIDL_SERVICE_HIDL_CAMERA_DEVICE_SESSION_H_ 241