• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2018 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 #include <android/frameworks/cameraservice/service/2.0/ICameraService.h>
18 #include <android/frameworks/cameraservice/device/2.0/ICameraDeviceUser.h>
19 #include <android/frameworks/cameraservice/device/2.0/types.h>
20 #include <camera/NdkCameraDevice.h>
21 #include <CameraMetadata.h>
22 #include <hardware/camera3.h>
23 
24 #ifndef CAMERA_NDK_VENDOR_H
25 #define CAMERA_NDK_VENDOR_H
26 
27 using android::hardware::hidl_vec;
28 using android::hardware::hidl_handle;
29 
30 namespace android {
31 namespace acam {
32 namespace utils {
33 
34 using CameraMetadata = hardware::camera::common::V1_0::helper::CameraMetadata;
35 using HCameraMetadata  = frameworks::cameraservice::service::V2_0::CameraMetadata;
36 using Status = frameworks::cameraservice::common::V2_0::Status;
37 using TemplateId = frameworks::cameraservice::device::V2_0::TemplateId;
38 using PhysicalCameraSettings = frameworks::cameraservice::device::V2_0::PhysicalCameraSettings;
39 using HRotation = frameworks::cameraservice::device::V2_0::OutputConfiguration::Rotation;
40 using OutputConfiguration = frameworks::cameraservice::device::V2_0::OutputConfiguration;
41 
42 // Utility class so that CaptureRequest can be stored by sp<>
43 struct CaptureRequest : public RefBase {
44   frameworks::cameraservice::device::V2_0::CaptureRequest mCaptureRequest;
45   std::vector<const native_handle_t *> mSurfaceList;
46   //Physical camera settings metadata is stored here, since the capture request
47   //might not contain it. That's since, fmq might have consumed it.
48   hidl_vec<PhysicalCameraSettings> mPhysicalCameraSettings;
49 };
50 
51 bool areWindowNativeHandlesEqual(hidl_vec<hidl_handle> handles1, hidl_vec<hidl_handle>handles2);
52 
53 bool areWindowNativeHandlesLessThan(hidl_vec<hidl_handle> handles1, hidl_vec<hidl_handle>handles2);
54 
55 bool isWindowNativeHandleEqual(const native_handle_t *nh1, const native_handle_t *nh2);
56 
57 bool isWindowNativeHandleLessThan(const native_handle_t *nh1, const native_handle_t *nh2);
58 
59 // Convenience wrapper over isWindowNativeHandleLessThan and isWindowNativeHandleEqual
60 bool isWindowNativeHandleGreaterThan(const native_handle_t *nh1, const native_handle_t *nh2);
61 
62 // Utility class so the native_handle_t can be compared with  its contents instead
63 // of just raw pointer comparisons.
64 struct native_handle_ptr_wrapper {
65     const native_handle_t *mWindow = nullptr;
66 
native_handle_ptr_wrappernative_handle_ptr_wrapper67     native_handle_ptr_wrapper(const native_handle_t *nh) : mWindow(nh) { }
68 
69     native_handle_ptr_wrapper() = default;
70 
71     operator const native_handle_t *() const { return mWindow; }
72 
73     bool operator ==(const native_handle_ptr_wrapper other) const {
74         return isWindowNativeHandleEqual(mWindow, other.mWindow);
75     }
76 
77     bool operator != (const native_handle_ptr_wrapper& other) const {
78         return !isWindowNativeHandleEqual(mWindow, other.mWindow);
79     }
80 
81     bool operator < (const native_handle_ptr_wrapper& other) const {
82         return isWindowNativeHandleLessThan(mWindow, other.mWindow);
83     }
84 
85     bool operator > (const native_handle_ptr_wrapper& other) const {
86         return !isWindowNativeHandleGreaterThan(mWindow, other.mWindow);
87     }
88 
89 };
90 
91 // Wrapper around OutputConfiguration. This is needed since HIDL
92 // OutputConfiguration is auto-generated and marked final. Therefore, operator
93 // overloads outside the class, will not get picked by clang while trying to
94 // store OutputConfiguration in maps/sets.
95 struct OutputConfigurationWrapper {
96     OutputConfiguration mOutputConfiguration;
97 
98     operator const OutputConfiguration &() const {
99         return mOutputConfiguration;
100     }
101 
OutputConfigurationWrapperOutputConfigurationWrapper102     OutputConfigurationWrapper() {
103         mOutputConfiguration.rotation = OutputConfiguration::Rotation::R0;
104         // The ndk currently doesn't support deferred surfaces
105         mOutputConfiguration.isDeferred = false;
106         mOutputConfiguration.width = 0;
107         mOutputConfiguration.height = 0;
108         // ndk doesn't support inter OutputConfiguration buffer sharing.
109         mOutputConfiguration.windowGroupId = -1;
110     };
111 
OutputConfigurationWrapperOutputConfigurationWrapper112     OutputConfigurationWrapper(const OutputConfigurationWrapper &other) {
113         *this = other;
114     }
115 
116     // Needed to make sure that OutputConfiguration in
117     // OutputConfigurationWrapper, when copied doesn't call hidl_handle's
118     // assignment operator / copy constructor, which will lead to native handle
119     // cloning, which is not what we want for app callbacks which have the native
120     // handle as parameter.
121     OutputConfigurationWrapper &operator=(const OutputConfigurationWrapper &other) {
122         const OutputConfiguration &outputConfiguration = other.mOutputConfiguration;
123         mOutputConfiguration.rotation = outputConfiguration.rotation;
124         mOutputConfiguration.isDeferred = outputConfiguration.isDeferred;
125         mOutputConfiguration.width = outputConfiguration.width;
126         mOutputConfiguration.height = outputConfiguration.height;
127         mOutputConfiguration.windowGroupId = outputConfiguration.windowGroupId;
128         mOutputConfiguration.windowHandles.resize(outputConfiguration.windowHandles.size());
129         mOutputConfiguration.physicalCameraId = outputConfiguration.physicalCameraId;
130         size_t i = 0;
131         for (const auto &handle : outputConfiguration.windowHandles) {
132             mOutputConfiguration.windowHandles[i++] = handle.getNativeHandle();
133         }
134         return *this;
135     }
136 
137     bool operator ==(const OutputConfiguration &other) const {
138         const OutputConfiguration &self = mOutputConfiguration;
139         return self.rotation == other.rotation && self.windowGroupId == other.windowGroupId &&
140                 self.physicalCameraId == other.physicalCameraId && self.width == other.width &&
141                 self.height == other.height && self.isDeferred == other.isDeferred &&
142                 areWindowNativeHandlesEqual(self.windowHandles, other.windowHandles);
143     }
144 
145     bool operator < (const OutputConfiguration &other) const {
146         if (*this == other) {
147             return false;
148         }
149         const OutputConfiguration &self = mOutputConfiguration;
150         if (self.windowGroupId != other.windowGroupId) {
151             return self.windowGroupId < other.windowGroupId;
152         }
153 
154         if (self.width != other.width) {
155             return self.width < other.width;
156         }
157 
158         if (self.height != other.height) {
159             return self.height < other.height;
160         }
161 
162         if (self.rotation != other.rotation) {
163             return static_cast<uint32_t>(self.rotation) < static_cast<uint32_t>(other.rotation);
164         }
165 
166         if (self.isDeferred != other.isDeferred) {
167             return self.isDeferred < other.isDeferred;
168         }
169 
170         if (self.physicalCameraId != other.physicalCameraId) {
171             return self.physicalCameraId < other.physicalCameraId;
172         }
173         return areWindowNativeHandlesLessThan(self.windowHandles, other.windowHandles);
174     }
175 
176     bool operator != (const OutputConfiguration &other) const {
177         return !(*this == other);
178     }
179 
180     bool operator > (const OutputConfiguration &other) const {
181         return (*this != other) && !(*this < other);
182     }
183 };
184 
185 // Convert CaptureRequest wrappable by sp<> to hidl CaptureRequest.
186 frameworks::cameraservice::device::V2_0::CaptureRequest convertToHidl(
187     const CaptureRequest *captureRequest);
188 
189 HRotation convertToHidl(int rotation);
190 
191 bool convertFromHidlCloned(const HCameraMetadata &metadata, CameraMetadata *rawMetadata);
192 
193 // Note: existing data in dst will be gone.
194 void convertToHidl(const camera_metadata_t *src, HCameraMetadata* dst, bool shouldOwn = false);
195 
196 TemplateId convertToHidl(ACameraDevice_request_template templateId);
197 
198 camera_status_t convertFromHidl(Status status);
199 
200 } // namespace utils
201 } // namespace acam
202 } // namespace android
203 
204 #endif // CAMERA_NDK_VENDOR_H
205