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