1 /*
2  * Copyright (C) 2021 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 #pragma once
17 #include <android/hardware/camera/device/3.2/ICameraDevice.h>
18 #include <android/hardware/camera/device/3.4/ICameraDeviceSession.h>
19 #include <android/hardware/graphics/mapper/2.0/IMapper.h>
20 #include <fmq/MessageQueue.h>
21 #include <queue>
22 #include <thread>
23 #include "stream_buffer_cache.h"
24 #include "vsock_camera_metadata.h"
25 #include "vsock_frame_provider.h"
26 
27 namespace android::hardware::camera::device::V3_4::implementation {
28 using ::android::sp;
29 using ::android::hardware::hidl_vec;
30 using ::android::hardware::kSynchronizedReadWrite;
31 using ::android::hardware::MessageQueue;
32 using ::android::hardware::Return;
33 using ::android::hardware::camera::common::V1_0::Status;
34 using ::android::hardware::camera::device::V3_2::BufferCache;
35 using ::android::hardware::camera::device::V3_2::CaptureRequest;
36 using ::android::hardware::camera::device::V3_2::ErrorCode;
37 using ::android::hardware::camera::device::V3_2::ICameraDeviceCallback;
38 using ::android::hardware::camera::device::V3_2::RequestTemplate;
39 using ::android::hardware::camera::device::V3_2::Stream;
40 using ::android::hardware::camera::device::V3_4::ICameraDeviceSession;
41 using ::android::hardware::camera::device::V3_4::StreamConfiguration;
42 using ::android::hardware::graphics::mapper::V2_0::YCbCrLayout;
43 
44 class VsockCameraDeviceSession : public ICameraDeviceSession {
45  public:
46   VsockCameraDeviceSession(
47       VsockCameraMetadata camera_characteristics,
48       std::shared_ptr<cuttlefish::VsockFrameProvider> frame_provider,
49       const sp<ICameraDeviceCallback>& callback);
50 
51   ~VsockCameraDeviceSession();
52 
53   Return<void> constructDefaultRequestSettings(
54       RequestTemplate,
55       ICameraDeviceSession::constructDefaultRequestSettings_cb _hidl_cb);
56 
57   Return<void> configureStreams(const V3_2::StreamConfiguration&,
58                                 ICameraDeviceSession::configureStreams_cb);
59 
60   Return<void> configureStreams_3_3(
61       const V3_2::StreamConfiguration&,
62       ICameraDeviceSession::configureStreams_3_3_cb);
63 
64   Return<void> configureStreams_3_4(
65       const V3_4::StreamConfiguration& requestedConfiguration,
66       ICameraDeviceSession::configureStreams_3_4_cb _hidl_cb);
67 
68   Return<void> getCaptureRequestMetadataQueue(
69       ICameraDeviceSession::getCaptureRequestMetadataQueue_cb);
70 
71   Return<void> getCaptureResultMetadataQueue(
72       ICameraDeviceSession::getCaptureResultMetadataQueue_cb);
73 
74   Return<void> processCaptureRequest(
75       const hidl_vec<CaptureRequest>&, const hidl_vec<BufferCache>&,
76       ICameraDeviceSession::processCaptureRequest_cb);
77 
78   Return<Status> flush();
79   Return<void> close();
80 
81   Return<void> processCaptureRequest_3_4(
82       const hidl_vec<V3_4::CaptureRequest>& requests,
83       const hidl_vec<V3_2::BufferCache>& cachesToRemove,
84       ICameraDeviceSession::processCaptureRequest_3_4_cb _hidl_cb);
85 
86  private:
87   struct ReadVsockRequest {
88     std::vector<uint64_t> buffer_ids;
89     uint32_t frame_number;
90     nsecs_t timestamp;
91     common::V1_0::helper::CameraMetadata settings;
92     uint32_t buffer_count;
93   };
94   struct VsockRequestComparator {
operatorVsockRequestComparator95     bool operator()(const ReadVsockRequest& lhs, const ReadVsockRequest& rhs) {
96       return lhs.frame_number > rhs.frame_number;
97     }
98   };
99   void updateBufferCaches(const hidl_vec<BufferCache>& to_remove);
100   Status configureStreams(const V3_2::StreamConfiguration& config,
101                           V3_3::HalStreamConfiguration* out);
102   unsigned int getBlobSize(
103       const V3_4::StreamConfiguration& requestedConfiguration);
104   Status isStreamConfigurationSupported(
105       const V3_2::StreamConfiguration& config);
106   void updateStreamInfo(const V3_2::StreamConfiguration& config);
107   Status processOneCaptureRequest(const CaptureRequest& request);
108   bool getRequestSettingsFmq(uint64_t size,
109                              V3_2::CameraMetadata& hidl_settings);
110   void processRequestLoop(unsigned int timeout);
111   bool getRequestFromQueue(ReadVsockRequest& request, unsigned int timeout_ms);
112   void putRequestToQueue(const ReadVsockRequest& request);
113   void fillCaptureResult(common::V1_0::helper::CameraMetadata& md,
114                          nsecs_t timestamp);
115   void notifyShutter(uint32_t frame_number, nsecs_t timestamp);
116   void notifyError(uint32_t frame_number, int32_t stream_id, ErrorCode code);
117   void tryWriteFmqResult(V3_2::CaptureResult& result);
118   VsockCameraMetadata camera_characteristics_;
119   std::shared_ptr<cuttlefish::VsockFrameProvider> frame_provider_;
120   const sp<ICameraDeviceCallback> callback_;
121   std::unique_ptr<MessageQueue<uint8_t, kSynchronizedReadWrite>> request_queue_;
122   std::shared_ptr<MessageQueue<uint8_t, kSynchronizedReadWrite>> result_queue_;
123   std::mutex settings_mutex_;
124   common::V1_0::helper::CameraMetadata latest_request_settings_;
125 
126   StreamBufferCache buffer_cache_;
127   std::map<int32_t, Stream> stream_cache_;
128 
129   std::mutex request_mutex_;
130   std::condition_variable request_available_;
131   std::condition_variable queue_empty_;
132   std::priority_queue<ReadVsockRequest, std::vector<ReadVsockRequest>,
133                       VsockRequestComparator>
134       pending_requests_;
135   std::thread request_processor_;
136   std::atomic<bool> process_requests_;
137   std::atomic<bool> flushing_requests_;
138 
139   unsigned int max_blob_size_;
140 };
141 
142 }  // namespace android::hardware::camera::device::V3_4::implementation
143