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 CPP_EVS_MANAGER_AIDL_WRAPPERS_INCLUDE_HIDLHWCAMERA_H
18 #define CPP_EVS_MANAGER_AIDL_WRAPPERS_INCLUDE_HIDLHWCAMERA_H
19 
20 #include "HidlCameraStream.h"
21 
22 #include <aidl/android/hardware/automotive/evs/BnEvsCamera.h>
23 #include <aidl/android/hardware/automotive/evs/BufferDesc.h>
24 #include <aidl/android/hardware/automotive/evs/CameraDesc.h>
25 #include <aidl/android/hardware/automotive/evs/CameraParam.h>
26 #include <aidl/android/hardware/automotive/evs/EvsEventDesc.h>
27 #include <aidl/android/hardware/automotive/evs/EvsResult.h>
28 #include <aidl/android/hardware/automotive/evs/IEvsCameraStream.h>
29 #include <aidl/android/hardware/automotive/evs/IEvsDisplay.h>
30 #include <aidl/android/hardware/automotive/evs/ParameterRange.h>
31 #include <aidl/android/hardware/automotive/evs/Stream.h>
32 #include <android/hardware/automotive/evs/1.1/IEvsCamera.h>
33 
34 #include <unordered_map>
35 
36 namespace aidl::android::automotive::evs::implementation {
37 
38 namespace aidlevs = ::aidl::android::hardware::automotive::evs;
39 namespace hidlevs = ::android::hardware::automotive::evs;
40 
41 class AidlCamera : public ::aidl::android::hardware::automotive::evs::BnEvsCamera {
42 public:
43     // Methods from ::android::hardware::automotive::evs::IEvsCamera follow.
44     ::ndk::ScopedAStatus doneWithFrame(const std::vector<aidlevs::BufferDesc>& buffers) override;
45     ::ndk::ScopedAStatus forcePrimaryClient(
46             const std::shared_ptr<aidlevs::IEvsDisplay>& display) override;
47     ::ndk::ScopedAStatus getCameraInfo(aidlevs::CameraDesc* _aidl_return) override;
48     ::ndk::ScopedAStatus getExtendedInfo(int32_t opaqueIdentifier,
49                                          std::vector<uint8_t>* value) override;
50     ::ndk::ScopedAStatus getIntParameter(aidlevs::CameraParam id,
51                                          std::vector<int32_t>* value) override;
52     ::ndk::ScopedAStatus getIntParameterRange(aidlevs::CameraParam id,
53                                               aidlevs::ParameterRange* _aidl_return) override;
54     ::ndk::ScopedAStatus getParameterList(std::vector<aidlevs::CameraParam>* _aidl_return) override;
55     ::ndk::ScopedAStatus getPhysicalCameraInfo(const std::string& deviceId,
56                                                aidlevs::CameraDesc* _aidl_return) override;
57     ::ndk::ScopedAStatus importExternalBuffers(const std::vector<aidlevs::BufferDesc>& buffers,
58                                                int32_t* _aidl_return) override;
59     ::ndk::ScopedAStatus pauseVideoStream() override;
60     ::ndk::ScopedAStatus resumeVideoStream() override;
61     ::ndk::ScopedAStatus setExtendedInfo(int32_t opaqueIdentifier,
62                                          const std::vector<uint8_t>& opaqueValue) override;
63     ::ndk::ScopedAStatus setIntParameter(aidlevs::CameraParam id, int32_t value,
64                                          std::vector<int32_t>* effectiveValue) override;
65     ::ndk::ScopedAStatus setPrimaryClient() override;
66     ::ndk::ScopedAStatus setMaxFramesInFlight(int32_t bufferCount) override;
67     ::ndk::ScopedAStatus startVideoStream(
68             const std::shared_ptr<aidlevs::IEvsCameraStream>& receiver) override;
69     ::ndk::ScopedAStatus stopVideoStream() override;
70     ::ndk::ScopedAStatus unsetPrimaryClient() override;
71 
72     explicit AidlCamera(const ::android::sp<hidlevs::V1_0::IEvsCamera>& camera,
73                         bool forceV1_0 = false);
~AidlCamera()74     virtual ~AidlCamera() { mImpl = nullptr; }
75 
76     const ::android::sp<hidlevs::V1_0::IEvsCamera> getHidlCamera() const;
77 
78 private:
79     class IHidlCamera;
80     class ImplV0;
81     class ImplV1;
82     std::shared_ptr<IHidlCamera> mImpl;
83 };
84 
85 class AidlCamera::IHidlCamera {
86 public:
87     virtual ::ndk::ScopedAStatus doneWithFrame(const std::vector<aidlevs::BufferDesc>& buffers) = 0;
88     virtual ::ndk::ScopedAStatus forcePrimaryClient(
89             const std::shared_ptr<aidlevs::IEvsDisplay>& display) = 0;
90     virtual ::ndk::ScopedAStatus getCameraInfo(aidlevs::CameraDesc* _aidl_return) = 0;
91     virtual ::ndk::ScopedAStatus getExtendedInfo(int32_t opaqueIdentifier,
92                                                  std::vector<uint8_t>* value) = 0;
93     virtual ::ndk::ScopedAStatus getIntParameter(aidlevs::CameraParam id,
94                                                  std::vector<int32_t>* value) = 0;
95     virtual ::ndk::ScopedAStatus getIntParameterRange(aidlevs::CameraParam id,
96                                                       aidlevs::ParameterRange* _aidl_return) = 0;
97     virtual ::ndk::ScopedAStatus getParameterList(
98             std::vector<aidlevs::CameraParam>* _aidl_return) = 0;
99     virtual ::ndk::ScopedAStatus getPhysicalCameraInfo(const std::string& deviceId,
100                                                        aidlevs::CameraDesc* _aidl_return) = 0;
101     virtual ::ndk::ScopedAStatus importExternalBuffers(
102             const std::vector<aidlevs::BufferDesc>& buffers, int32_t* _aidl_return) = 0;
103     virtual ::ndk::ScopedAStatus pauseVideoStream() = 0;
104     virtual ::ndk::ScopedAStatus resumeVideoStream() = 0;
105     virtual ::ndk::ScopedAStatus setExtendedInfo(int32_t opaqueIdentifier,
106                                                  const std::vector<uint8_t>& opaqueValue) = 0;
107     virtual ::ndk::ScopedAStatus setIntParameter(aidlevs::CameraParam id, int32_t value,
108                                                  std::vector<int32_t>* effectiveValue) = 0;
109     virtual ::ndk::ScopedAStatus setPrimaryClient() = 0;
110     virtual ::ndk::ScopedAStatus setMaxFramesInFlight(int32_t bufferCount) = 0;
111     virtual ::ndk::ScopedAStatus startVideoStream(
112             const std::shared_ptr<aidlevs::IEvsCameraStream>& receiver) = 0;
113     virtual ::ndk::ScopedAStatus stopVideoStream() = 0;
114     virtual ::ndk::ScopedAStatus unsetPrimaryClient() = 0;
115     virtual const ::android::sp<hidlevs::V1_0::IEvsCamera> getHidlCamera() const = 0;
116 
IHidlCamera(const::android::sp<hidlevs::V1_0::IEvsCamera> & camera)117     explicit IHidlCamera(const ::android::sp<hidlevs::V1_0::IEvsCamera>& camera) :
118           mHidlCamera(camera) {}
~IHidlCamera()119     virtual ~IHidlCamera() {
120         mHidlCamera = nullptr;
121         mHidlStream = nullptr;
122     }
123 
124 protected:
125     // The low level camera interface that backs this proxy
126     ::android::sp<hidlevs::V1_0::IEvsCamera> mHidlCamera;
127     ::android::sp<HidlCameraStream> mHidlStream;
128     std::string mId;
129 };
130 
131 class AidlCamera::ImplV0 final : public IHidlCamera {
132 public:
133     ::ndk::ScopedAStatus doneWithFrame(const std::vector<aidlevs::BufferDesc>& buffers) override;
134     ::ndk::ScopedAStatus forcePrimaryClient(
135             const std::shared_ptr<aidlevs::IEvsDisplay>& display) override;
136     ::ndk::ScopedAStatus getCameraInfo(aidlevs::CameraDesc* _aidl_return) override;
137     ::ndk::ScopedAStatus getExtendedInfo(int32_t opaqueIdentifier,
138                                          std::vector<uint8_t>* value) override;
139     ::ndk::ScopedAStatus getIntParameter(aidlevs::CameraParam id,
140                                          std::vector<int32_t>* value) override;
141     ::ndk::ScopedAStatus getIntParameterRange(aidlevs::CameraParam id,
142                                               aidlevs::ParameterRange* _aidl_return) override;
143     ::ndk::ScopedAStatus getParameterList(std::vector<aidlevs::CameraParam>* _aidl_return) override;
144     ::ndk::ScopedAStatus getPhysicalCameraInfo(const std::string& deviceId,
145                                                aidlevs::CameraDesc* _aidl_return) override;
146     ::ndk::ScopedAStatus importExternalBuffers(const std::vector<aidlevs::BufferDesc>& buffers,
147                                                int32_t* _aidl_return) override;
148     ::ndk::ScopedAStatus pauseVideoStream() override;
149     ::ndk::ScopedAStatus resumeVideoStream() override;
150     ::ndk::ScopedAStatus setExtendedInfo(int32_t opaqueIdentifier,
151                                          const std::vector<uint8_t>& opaqueValue) override;
152     ::ndk::ScopedAStatus setIntParameter(aidlevs::CameraParam id, int32_t value,
153                                          std::vector<int32_t>* effectiveValue) override;
154     ::ndk::ScopedAStatus setPrimaryClient() override;
155     ::ndk::ScopedAStatus setMaxFramesInFlight(int32_t bufferCount) override;
156     ::ndk::ScopedAStatus startVideoStream(
157             const std::shared_ptr<aidlevs::IEvsCameraStream>& receiver) override;
158     ::ndk::ScopedAStatus stopVideoStream() override;
159     ::ndk::ScopedAStatus unsetPrimaryClient() override;
160 
161     explicit ImplV0(const ::android::sp<hidlevs::V1_0::IEvsCamera>& camera);
~ImplV0()162     virtual ~ImplV0() { mHidlCamera = nullptr; };
163 
getHidlCamera()164     const ::android::sp<hidlevs::V1_0::IEvsCamera> getHidlCamera() const override {
165         return mHidlCamera;
166     }
167 };
168 
169 class AidlCamera::ImplV1 final : public IHidlCamera {
170 public:
171     ::ndk::ScopedAStatus doneWithFrame(const std::vector<aidlevs::BufferDesc>& buffers) override;
172     ::ndk::ScopedAStatus forcePrimaryClient(
173             const std::shared_ptr<aidlevs::IEvsDisplay>& display) override;
174     ::ndk::ScopedAStatus getCameraInfo(aidlevs::CameraDesc* _aidl_return) override;
175     ::ndk::ScopedAStatus getExtendedInfo(int32_t opaqueIdentifier,
176                                          std::vector<uint8_t>* value) override;
177     ::ndk::ScopedAStatus getIntParameter(aidlevs::CameraParam id,
178                                          std::vector<int32_t>* value) override;
179     ::ndk::ScopedAStatus getIntParameterRange(aidlevs::CameraParam id,
180                                               aidlevs::ParameterRange* _aidl_return) override;
181     ::ndk::ScopedAStatus getParameterList(std::vector<aidlevs::CameraParam>* _aidl_return) override;
182     ::ndk::ScopedAStatus getPhysicalCameraInfo(const std::string& deviceId,
183                                                aidlevs::CameraDesc* _aidl_return) override;
184     ::ndk::ScopedAStatus importExternalBuffers(const std::vector<aidlevs::BufferDesc>& buffers,
185                                                int32_t* _aidl_return) override;
186     ::ndk::ScopedAStatus pauseVideoStream() override;
187     ::ndk::ScopedAStatus resumeVideoStream() override;
188     ::ndk::ScopedAStatus setExtendedInfo(int32_t opaqueIdentifier,
189                                          const std::vector<uint8_t>& opaqueValue) override;
190     ::ndk::ScopedAStatus setIntParameter(aidlevs::CameraParam id, int32_t value,
191                                          std::vector<int32_t>* effectiveValue) override;
192     ::ndk::ScopedAStatus setPrimaryClient() override;
193     ::ndk::ScopedAStatus setMaxFramesInFlight(int32_t bufferCount) override;
194     ::ndk::ScopedAStatus startVideoStream(
195             const std::shared_ptr<aidlevs::IEvsCameraStream>& receiver) override;
196     ::ndk::ScopedAStatus stopVideoStream() override;
197     ::ndk::ScopedAStatus unsetPrimaryClient() override;
198 
199     explicit ImplV1(const ::android::sp<hidlevs::V1_1::IEvsCamera>& camera);
~ImplV1()200     virtual ~ImplV1() { mHidlCamera = nullptr; }
201 
getHidlCamera()202     const ::android::sp<hidlevs::V1_0::IEvsCamera> getHidlCamera() const override {
203         return mHidlCamera;
204     }
205 
206 private:
207     ::android::sp<hidlevs::V1_1::IEvsCamera> mHidlCamera;
208 };
209 
210 }  // namespace aidl::android::automotive::evs::implementation
211 
212 #endif  // CPP_EVS_MANAGER_AIDL_WRAPPERS_INCLUDE_HIDLHWCAMERA_H
213