1 /*
2  * Copyright (C) 2015 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 //#define LOG_NDEBUG 0
18 #define LOG_TAG "NdkCameraDevice"
19 #define ATRACE_TAG ATRACE_TAG_CAMERA
20 
21 #include <utils/Log.h>
22 #include <utils/Trace.h>
23 
24 #include <camera/NdkCameraDevice.h>
25 #include "impl/ACameraCaptureSession.h"
26 
27 using namespace android::acam;
28 
areWindowTypesEqual(ACameraWindowType * a,ACameraWindowType * b)29 bool areWindowTypesEqual(ACameraWindowType *a, ACameraWindowType *b) {
30 #ifdef __ANDROID_VNDK__
31     return utils::isWindowNativeHandleEqual(a, b);
32 #else
33     return a == b;
34 #endif
35 }
36 
37 EXPORT
ACameraDevice_close(ACameraDevice * device)38 camera_status_t ACameraDevice_close(ACameraDevice* device) {
39     ATRACE_CALL();
40     if (device == nullptr) {
41         ALOGE("%s: invalid argument! device is null", __FUNCTION__);
42         return ACAMERA_ERROR_INVALID_PARAMETER;
43     }
44     delete device;
45     return ACAMERA_OK;
46 }
47 
48 EXPORT
ACameraDevice_getId(const ACameraDevice * device)49 const char* ACameraDevice_getId(const ACameraDevice* device) {
50     ATRACE_CALL();
51     if (device == nullptr) {
52         ALOGE("%s: invalid argument! device is null", __FUNCTION__);
53         return nullptr;
54     }
55     return device->getId();
56 }
57 
58 EXPORT
ACameraDevice_createCaptureRequest(const ACameraDevice * device,ACameraDevice_request_template templateId,ACaptureRequest ** request)59 camera_status_t ACameraDevice_createCaptureRequest(
60         const ACameraDevice* device,
61         ACameraDevice_request_template templateId,
62         ACaptureRequest** request) {
63     ATRACE_CALL();
64     if (device == nullptr || request == nullptr) {
65         ALOGE("%s: invalid argument! device %p request %p",
66                 __FUNCTION__, device, request);
67         return ACAMERA_ERROR_INVALID_PARAMETER;
68     }
69     switch (templateId) {
70         case TEMPLATE_PREVIEW:
71         case TEMPLATE_STILL_CAPTURE:
72         case TEMPLATE_RECORD:
73         case TEMPLATE_VIDEO_SNAPSHOT:
74         case TEMPLATE_ZERO_SHUTTER_LAG:
75         case TEMPLATE_MANUAL:
76             break;
77         default:
78             ALOGE("%s: unknown template ID %d", __FUNCTION__, templateId);
79             return ACAMERA_ERROR_INVALID_PARAMETER;
80     }
81     return device->createCaptureRequest(templateId, nullptr /*physicalIdList*/, request);
82 }
83 
84 EXPORT
ACameraDevice_createCaptureRequest_withPhysicalIds(const ACameraDevice * device,ACameraDevice_request_template templateId,const ACameraIdList * physicalCameraIdList,ACaptureRequest ** request)85 camera_status_t ACameraDevice_createCaptureRequest_withPhysicalIds(
86         const ACameraDevice* device,
87         ACameraDevice_request_template templateId,
88         const ACameraIdList* physicalCameraIdList,
89         ACaptureRequest** request) {
90     ATRACE_CALL();
91     if (device == nullptr || request == nullptr || physicalCameraIdList == nullptr) {
92         ALOGE("%s: invalid argument! device %p request %p, physicalCameraIdList %p",
93                 __FUNCTION__, device, request, physicalCameraIdList);
94         return ACAMERA_ERROR_INVALID_PARAMETER;
95     }
96     switch (templateId) {
97         case TEMPLATE_PREVIEW:
98         case TEMPLATE_STILL_CAPTURE:
99         case TEMPLATE_RECORD:
100         case TEMPLATE_VIDEO_SNAPSHOT:
101         case TEMPLATE_ZERO_SHUTTER_LAG:
102         case TEMPLATE_MANUAL:
103             break;
104         default:
105             ALOGE("%s: unknown template ID %d", __FUNCTION__, templateId);
106             return ACAMERA_ERROR_INVALID_PARAMETER;
107     }
108     return device->createCaptureRequest(templateId, physicalCameraIdList, request);
109 }
110 
111 EXPORT
ACaptureSessionOutputContainer_create(ACaptureSessionOutputContainer ** out)112 camera_status_t ACaptureSessionOutputContainer_create(
113         /*out*/ACaptureSessionOutputContainer** out) {
114     ATRACE_CALL();
115     if (out == nullptr) {
116         ALOGE("%s: Error: out null", __FUNCTION__);
117         return ACAMERA_ERROR_INVALID_PARAMETER;
118     }
119     *out = new ACaptureSessionOutputContainer();
120     return ACAMERA_OK;
121 }
122 
123 EXPORT
ACaptureSessionOutputContainer_free(ACaptureSessionOutputContainer * container)124 void ACaptureSessionOutputContainer_free(ACaptureSessionOutputContainer* container) {
125     ATRACE_CALL();
126     if (container != nullptr) {
127         delete container;
128     }
129     return;
130 }
131 
132 EXPORT
ACaptureSessionOutput_create(ACameraWindowType * window,ACaptureSessionOutput ** out)133 camera_status_t ACaptureSessionOutput_create(
134         ACameraWindowType* window, /*out*/ACaptureSessionOutput** out) {
135     ATRACE_CALL();
136     if (window == nullptr || out == nullptr) {
137         ALOGE("%s: Error: bad argument. window %p, out %p",
138                 __FUNCTION__, window, out);
139         return ACAMERA_ERROR_INVALID_PARAMETER;
140     }
141     *out = new ACaptureSessionOutput(window, false);
142     return ACAMERA_OK;
143 }
144 
145 EXPORT
ACaptureSessionSharedOutput_create(ACameraWindowType * window,ACaptureSessionOutput ** out)146 camera_status_t ACaptureSessionSharedOutput_create(
147         ACameraWindowType* window, /*out*/ACaptureSessionOutput** out) {
148     ATRACE_CALL();
149     if (window == nullptr || out == nullptr) {
150         ALOGE("%s: Error: bad argument. window %p, out %p",
151                 __FUNCTION__, window, out);
152         return ACAMERA_ERROR_INVALID_PARAMETER;
153     }
154     *out = new ACaptureSessionOutput(window, true);
155     return ACAMERA_OK;
156 }
157 
158 EXPORT
ACaptureSessionPhysicalOutput_create(ACameraWindowType * window,const char * physicalId,ACaptureSessionOutput ** out)159 camera_status_t ACaptureSessionPhysicalOutput_create(
160         ACameraWindowType* window, const char* physicalId,
161         /*out*/ACaptureSessionOutput** out) {
162     ATRACE_CALL();
163     if (window == nullptr || physicalId == nullptr || out == nullptr) {
164         ALOGE("%s: Error: bad argument. window %p, physicalId %p, out %p",
165                 __FUNCTION__, window, physicalId, out);
166         return ACAMERA_ERROR_INVALID_PARAMETER;
167     }
168     *out = new ACaptureSessionOutput(window, false, physicalId);
169     return ACAMERA_OK;
170 }
171 
172 EXPORT
ACaptureSessionSharedOutput_add(ACaptureSessionOutput * out,ACameraWindowType * window)173 camera_status_t ACaptureSessionSharedOutput_add(ACaptureSessionOutput *out,
174         ACameraWindowType* window) {
175     ATRACE_CALL();
176     if ((window == nullptr) || (out == nullptr)) {
177         ALOGE("%s: Error: bad argument. window %p, out %p",
178                 __FUNCTION__, window, out);
179         return ACAMERA_ERROR_INVALID_PARAMETER;
180     }
181     if (!out->mIsShared) {
182         ALOGE("%s: Error trying to insert a new window in non-shared output configuration",
183                 __FUNCTION__);
184         return ACAMERA_ERROR_INVALID_OPERATION;
185     }
186     if (areWindowTypesEqual(out->mWindow, window)) {
187         ALOGE("%s: Error trying to add the same window associated with the output configuration",
188                 __FUNCTION__);
189         return ACAMERA_ERROR_INVALID_PARAMETER;
190     }
191 
192     auto insert = out->mSharedWindows.insert(window);
193     camera_status_t ret = (insert.second) ? ACAMERA_OK : ACAMERA_ERROR_INVALID_PARAMETER;
194     return ret;
195 }
196 
197 EXPORT
ACaptureSessionSharedOutput_remove(ACaptureSessionOutput * out,ACameraWindowType * window)198 camera_status_t ACaptureSessionSharedOutput_remove(ACaptureSessionOutput *out,
199         ACameraWindowType* window) {
200     ATRACE_CALL();
201     if ((window == nullptr) || (out == nullptr)) {
202         ALOGE("%s: Error: bad argument. window %p, out %p",
203                 __FUNCTION__, window, out);
204         return ACAMERA_ERROR_INVALID_PARAMETER;
205     }
206     if (!out->mIsShared) {
207         ALOGE("%s: Error trying to remove a  window in non-shared output configuration",
208                 __FUNCTION__);
209         return ACAMERA_ERROR_INVALID_OPERATION;
210     }
211     if (areWindowTypesEqual(out->mWindow, window)) {
212         ALOGE("%s: Error trying to remove the same window associated with the output configuration",
213                 __FUNCTION__);
214         return ACAMERA_ERROR_INVALID_PARAMETER;
215     }
216 
217     auto remove = out->mSharedWindows.erase(window);
218     camera_status_t ret = (remove) ? ACAMERA_OK : ACAMERA_ERROR_INVALID_PARAMETER;
219     return ret;
220 }
221 
222 EXPORT
ACaptureSessionOutput_free(ACaptureSessionOutput * output)223 void ACaptureSessionOutput_free(ACaptureSessionOutput* output) {
224     ATRACE_CALL();
225     if (output != nullptr) {
226         delete output;
227     }
228     return;
229 }
230 
231 EXPORT
ACaptureSessionOutputContainer_add(ACaptureSessionOutputContainer * container,const ACaptureSessionOutput * output)232 camera_status_t ACaptureSessionOutputContainer_add(
233         ACaptureSessionOutputContainer* container, const ACaptureSessionOutput* output) {
234     ATRACE_CALL();
235     if (container == nullptr || output == nullptr) {
236         ALOGE("%s: Error: invalid input: container %p, output %p",
237                 __FUNCTION__, container, output);
238         return ACAMERA_ERROR_INVALID_PARAMETER;
239     }
240     auto pair = container->mOutputs.insert(*output);
241     if (!pair.second) {
242         ALOGW("%s: output %p already exists!", __FUNCTION__, output);
243     }
244     return ACAMERA_OK;
245 }
246 
247 EXPORT
ACaptureSessionOutputContainer_remove(ACaptureSessionOutputContainer * container,const ACaptureSessionOutput * output)248 camera_status_t ACaptureSessionOutputContainer_remove(
249         ACaptureSessionOutputContainer* container, const ACaptureSessionOutput* output) {
250     ATRACE_CALL();
251     if (container == nullptr || output == nullptr) {
252         ALOGE("%s: Error: invalid input: container %p, output %p",
253                 __FUNCTION__, container, output);
254         return ACAMERA_ERROR_INVALID_PARAMETER;
255     }
256     container->mOutputs.erase(*output);
257     return ACAMERA_OK;
258 }
259 
260 EXPORT
ACameraDevice_createCaptureSession(ACameraDevice * device,const ACaptureSessionOutputContainer * outputs,const ACameraCaptureSession_stateCallbacks * callbacks,ACameraCaptureSession ** session)261 camera_status_t ACameraDevice_createCaptureSession(
262         ACameraDevice* device,
263         const ACaptureSessionOutputContainer*       outputs,
264         const ACameraCaptureSession_stateCallbacks* callbacks,
265         /*out*/ACameraCaptureSession** session) {
266     ATRACE_CALL();
267     if (device == nullptr || outputs == nullptr || callbacks == nullptr || session == nullptr) {
268         ALOGE("%s: Error: invalid input: device %p, outputs %p, callbacks %p, session %p",
269                 __FUNCTION__, device, outputs, callbacks, session);
270         return ACAMERA_ERROR_INVALID_PARAMETER;
271     }
272     return device->createCaptureSession(outputs, nullptr, callbacks, session);
273 }
274 
275 EXPORT
ACameraDevice_createCaptureSessionWithSessionParameters(ACameraDevice * device,const ACaptureSessionOutputContainer * outputs,const ACaptureRequest * sessionParameters,const ACameraCaptureSession_stateCallbacks * callbacks,ACameraCaptureSession ** session)276 camera_status_t ACameraDevice_createCaptureSessionWithSessionParameters(
277         ACameraDevice* device,
278         const ACaptureSessionOutputContainer*       outputs,
279         const ACaptureRequest* sessionParameters,
280         const ACameraCaptureSession_stateCallbacks* callbacks,
281         /*out*/ACameraCaptureSession** session) {
282     ATRACE_CALL();
283     if (device == nullptr || outputs == nullptr || callbacks == nullptr || session == nullptr) {
284         ALOGE("%s: Error: invalid input: device %p, outputs %p, callbacks %p, session %p",
285                 __FUNCTION__, device, outputs, callbacks, session);
286         return ACAMERA_ERROR_INVALID_PARAMETER;
287     }
288     return device->createCaptureSession(outputs, sessionParameters, callbacks, session);
289 }
290 
291 EXPORT
ACameraDevice_isSessionConfigurationSupported(const ACameraDevice * device,const ACaptureSessionOutputContainer * sessionOutputContainer)292 camera_status_t ACameraDevice_isSessionConfigurationSupported(
293         const ACameraDevice* device,
294         const ACaptureSessionOutputContainer* sessionOutputContainer) {
295     ATRACE_CALL();
296     if (device == nullptr || sessionOutputContainer == nullptr) {
297         ALOGE("%s: Error: invalid input: device %p, sessionOutputContainer %p",
298                 __FUNCTION__, device, sessionOutputContainer);
299         return ACAMERA_ERROR_INVALID_PARAMETER;
300     }
301     return device->isSessionConfigurationSupported(sessionOutputContainer);
302 }
303