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