1 /*
2  * Copyright (C) 2022 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_AIDL_SERVICE_AIDL_CAMERA_DEVICE_SESSION_H_
18 #define HARDWARE_GOOGLE_CAMERA_HAL_AIDL_SERVICE_AIDL_CAMERA_DEVICE_SESSION_H_
19 
20 #include <aidl/android/hardware/camera/device/BnCameraDeviceSession.h>
21 #include <aidl/android/hardware/camera/device/ICameraDevice.h>
22 #include <aidl/android/hardware/camera/device/ICameraDeviceCallback.h>
23 #include <aidl/android/hardware/thermal/IThermal.h>
24 #include <android-base/thread_annotations.h>
25 #include <fmq/AidlMessageQueue.h>
26 #include <utils/StrongPointer.h>
27 
28 #include <shared_mutex>
29 #include <vector>
30 
31 #include "aidl_profiler.h"
32 #include "camera_device_session.h"
33 #include "hal_types.h"
34 
35 namespace android {
36 namespace hardware {
37 namespace camera {
38 namespace device {
39 namespace implementation {
40 
41 // AidlCameraDeviceSession implements the AIDL camera device session interface,
42 // ICameraDeviceSession, that contains the methods to configure and request
43 // captures from an active camera device.
44 class AidlCameraDeviceSession
45     : public aidl::android::hardware::camera::device::BnCameraDeviceSession {
46  public:
47   // Create a AidlCameraDeviceSession.
48   // device_session is a google camera device session that
49   // AidlCameraDeviceSession is going to manage. Creating a
50   // AidlCameraDeviceSession will fail if device_session is
51   // nullptr.
52   static std::shared_ptr<AidlCameraDeviceSession> Create(
53       const std::shared_ptr<
54           aidl::android::hardware::camera::device::ICameraDeviceCallback>& callback,
55       std::unique_ptr<google_camera_hal::CameraDeviceSession> device_session,
56       std::shared_ptr<android::google_camera_hal::AidlProfiler> aidl_profiler);
57 
58   virtual ~AidlCameraDeviceSession();
59 
60   // functions in ICameraDeviceSession
61 
62   ndk::ScopedAStatus close() override;
63 
64   ndk::ScopedAStatus configureStreams(
65       const aidl::android::hardware::camera::device::StreamConfiguration&,
66       std::vector<aidl::android::hardware::camera::device::HalStream>*) override;
67 
68   ndk::ScopedAStatus constructDefaultRequestSettings(
69       aidl::android::hardware::camera::device::RequestTemplate in_type,
70       aidl::android::hardware::camera::device::CameraMetadata* aidl_return)
71       override;
72 
73   ndk::ScopedAStatus flush() override;
74 
75   ndk::ScopedAStatus getCaptureRequestMetadataQueue(
76       aidl::android::hardware::common::fmq::MQDescriptor<
77           int8_t, aidl::android::hardware::common::fmq::SynchronizedReadWrite>*
78           aidl_return) override;
79 
80   ndk::ScopedAStatus getCaptureResultMetadataQueue(
81       aidl::android::hardware::common::fmq::MQDescriptor<
82           int8_t, aidl::android::hardware::common::fmq::SynchronizedReadWrite>*
83           aidl_return) override;
84 
85   ndk::ScopedAStatus isReconfigurationRequired(
86       const aidl::android::hardware::camera::device::CameraMetadata&
87           in_oldSessionParams,
88       const aidl::android::hardware::camera::device::CameraMetadata&
89           in_newSessionParams,
90       bool* aidl_return) override;
91 
92   ndk::ScopedAStatus processCaptureRequest(
93       const std::vector<aidl::android::hardware::camera::device::CaptureRequest>&
94           in_requests,
95       const std::vector<aidl::android::hardware::camera::device::BufferCache>&
96           in_cachesToRemove,
97       int32_t* aidl_return) override;
98 
99   ndk::ScopedAStatus signalStreamFlush(const std::vector<int32_t>& in_streamIds,
100                                        int32_t in_streamConfigCounter) override;
101 
102   ndk::ScopedAStatus switchToOffline(
103       const std::vector<int32_t>& in_streamsToKeep,
104       aidl::android::hardware::camera::device::CameraOfflineSessionInfo*
105           out_offlineSessionInfo,
106       std::shared_ptr<
107           aidl::android::hardware::camera::device::ICameraOfflineSession>*
108           aidl_return) override;
109 
110   ndk::ScopedAStatus repeatingRequestEnd(
111       int32_t in_frameNumber, const std::vector<int32_t>& in_streamIds) override;
112 
113   ndk::ScopedAStatus configureStreamsV2(
114       const aidl::android::hardware::camera::device::StreamConfiguration&,
115       aidl::android::hardware::camera::device::ConfigureStreamsRet*) override;
116   AidlCameraDeviceSession() = default;
117 
118  protected:
119   ndk::SpAIBinder createBinder() override;
120 
121  private:
122   using MetadataQueue = AidlMessageQueue<
123       int8_t, aidl::android::hardware::common::fmq::SynchronizedReadWrite>;
124 
125   static constexpr uint32_t kRequestMetadataQueueSizeBytes = 1 << 20;  // 1MB
126   static constexpr uint32_t kResultMetadataQueueSizeBytes = 1 << 20;   // 1MB
127 
128   // Initialize the latest available gralloc buffer mapper.
129   status_t InitializeBufferMapper();
130 
131   // Initialize AidlCameraDeviceSession with a CameraDeviceSession.
132   status_t Initialize(
133       const std::shared_ptr<
134           aidl::android::hardware::camera::device::ICameraDeviceCallback>& callback,
135       std::unique_ptr<google_camera_hal::CameraDeviceSession> device_session,
136       std::shared_ptr<android::google_camera_hal::AidlProfiler> aidl_profiler);
137 
138   // Create a metadata queue.
139   // If override_size_property contains a valid size, it will create a metadata
140   // queue of that size. If it override_size_property doesn't contain a valid
141   // size, it will create a metadata queue of the default size.
142   // default_size_bytes is the default size of the message queue in bytes.
143   // override_size_property is the name of the system property that contains
144   // the message queue size.
145   status_t CreateMetadataQueue(std::unique_ptr<MetadataQueue>* metadata_queue,
146                                uint32_t default_size_bytes,
147                                const char* override_size_property);
148 
149   // Invoked when receiving a result from HAL.
150   void ProcessCaptureResult(
151       std::unique_ptr<google_camera_hal::CaptureResult> hal_result);
152 
153   // Invoked when receiving a batched result from HAL.
154   void ProcessBatchCaptureResult(
155       std::vector<std::unique_ptr<google_camera_hal::CaptureResult>> hal_results);
156 
157   // TODO b/311263114: Remove this method once the feature flag is enabled.
158   // This is needed since the framework has the feature support flagged. The HAL
159   // should not switch HAL buffer on / off is the framework doesn't support them
160   // (flag is off). Since aconfig flags are not shared between the framework and
161   // the HAL - the HAL can know about framework support through knowing whether
162   // configureStreamsV2 was called or not.
163   ndk::ScopedAStatus configureStreamsImpl(
164       const aidl::android::hardware::camera::device::StreamConfiguration&,
165       bool v2, aidl::android::hardware::camera::device::ConfigureStreamsRet*);
166   // Invoked when receiving a message from HAL.
167   void NotifyHalMessage(const google_camera_hal::NotifyMessage& hal_message);
168 
169   // Invoked when requesting stream buffers from HAL.
170   google_camera_hal::BufferRequestStatus RequestStreamBuffers(
171       const std::vector<google_camera_hal::BufferRequest>& hal_buffer_requests,
172       std::vector<google_camera_hal::BufferReturn>* hal_buffer_returns);
173 
174   // Invoked when returning stream buffers from HAL.
175   void ReturnStreamBuffers(
176       const std::vector<google_camera_hal::StreamBuffer>& return_hal_buffers);
177 
178   // Set camera device session callbacks.
179   void SetSessionCallbacks();
180 
181   // Register a thermal changed callback.
182   // notify_throttling will be invoked when thermal status changes.
183   // If filter_type is false, type will be ignored and all types will be
184   // monitored.
185   // If filter_type is true, only type will be monitored.
186   status_t RegisterThermalChangedCallback(
187       google_camera_hal::NotifyThrottlingFunc notify_throttling,
188       bool filter_type, google_camera_hal::TemperatureType type);
189 
190   // Unregister thermal changed callback.
191   void UnregisterThermalChangedCallback();
192 
193   // Log when the first frame buffers are all received.
194   void TryLogFirstFrameDone(const google_camera_hal::CaptureResult& result,
195                             const char* caller_func_name);
196 
197   std::unique_ptr<google_camera_hal::CameraDeviceSession> device_session_;
198 
199   // Metadata queue to read the request metadata from.
200   std::unique_ptr<MetadataQueue> request_metadata_queue_;
201 
202   // Metadata queue to write the result metadata to.
203   std::unique_ptr<MetadataQueue> result_metadata_queue_;
204 
205   // Assuming callbacks to framework is thread-safe, the shared mutex is only
206   // used to protect member variable writing and reading.
207   std::shared_mutex aidl_device_callback_lock_;
208   // Protected by aidl_device_callback_lock_
209   std::shared_ptr<aidl::android::hardware::camera::device::ICameraDeviceCallback>
210       aidl_device_callback_;
211 
212   std::mutex aidl_thermal_mutex_;
213   std::shared_ptr<aidl::android::hardware::thermal::IThermal> thermal_;
214 
215   // Must be protected by hidl_thermal_mutex_.
216   std::shared_ptr<aidl::android::hardware::thermal::IThermalChangedCallback>
217       thermal_changed_callback_ GUARDED_BY(aidl_thermal_mutex_);
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<android::google_camera_hal::AidlProfiler> aidl_profiler_;
231 
232   // The ID of preview stream.
233   int32_t preview_stream_id_ = -1;
234 
235   // The timestamp of last preview image actually sent by HAL.
236   uint32_t preview_timestamp_last_ = 0;
237 };
238 
239 }  // namespace implementation
240 }  // namespace device
241 }  // namespace camera
242 }  // namespace hardware
243 }  // namespace android
244 
245 #endif  // HARDWARE_GOOGLE_CAMERA_HAL_AIDL_SERVICE_AIDL_CAMERA_DEVICE_SESSION_H_
246