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