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;
28 
29 EXPORT
ACameraDevice_close(ACameraDevice * device)30 camera_status_t ACameraDevice_close(ACameraDevice* device) {
31     ATRACE_CALL();
32     if (device == nullptr) {
33         ALOGE("%s: invalid argument! device is null", __FUNCTION__);
34         return ACAMERA_ERROR_INVALID_PARAMETER;
35     }
36     delete device;
37     return ACAMERA_OK;
38 }
39 
40 EXPORT
ACameraDevice_getId(const ACameraDevice * device)41 const char* ACameraDevice_getId(const ACameraDevice* device) {
42     ATRACE_CALL();
43     if (device == nullptr) {
44         ALOGE("%s: invalid argument! device is null", __FUNCTION__);
45         return nullptr;
46     }
47     return device->getId();
48 }
49 
50 EXPORT
ACameraDevice_createCaptureRequest(const ACameraDevice * device,ACameraDevice_request_template templateId,ACaptureRequest ** request)51 camera_status_t ACameraDevice_createCaptureRequest(
52         const ACameraDevice* device,
53         ACameraDevice_request_template templateId,
54         ACaptureRequest** request) {
55     ATRACE_CALL();
56     if (device == nullptr || request == nullptr) {
57         ALOGE("%s: invalid argument! device %p request %p",
58                 __FUNCTION__, device, request);
59         return ACAMERA_ERROR_INVALID_PARAMETER;
60     }
61     switch (templateId) {
62         case TEMPLATE_PREVIEW:
63         case TEMPLATE_STILL_CAPTURE:
64         case TEMPLATE_RECORD:
65         case TEMPLATE_VIDEO_SNAPSHOT:
66         case TEMPLATE_ZERO_SHUTTER_LAG:
67         case TEMPLATE_MANUAL:
68             break;
69         default:
70             ALOGE("%s: unknown template ID %d", __FUNCTION__, templateId);
71             return ACAMERA_ERROR_INVALID_PARAMETER;
72     }
73     return device->createCaptureRequest(templateId, request);
74 }
75 
76 EXPORT
ACaptureSessionOutputContainer_create(ACaptureSessionOutputContainer ** out)77 camera_status_t ACaptureSessionOutputContainer_create(
78         /*out*/ACaptureSessionOutputContainer** out) {
79     ATRACE_CALL();
80     if (out == nullptr) {
81         ALOGE("%s: Error: out null", __FUNCTION__);
82         return ACAMERA_ERROR_INVALID_PARAMETER;
83     }
84     *out = new ACaptureSessionOutputContainer();
85     return ACAMERA_OK;
86 }
87 
88 EXPORT
ACaptureSessionOutputContainer_free(ACaptureSessionOutputContainer * container)89 void ACaptureSessionOutputContainer_free(ACaptureSessionOutputContainer* container) {
90     ATRACE_CALL();
91     if (container != nullptr) {
92         delete container;
93     }
94     return;
95 }
96 
97 EXPORT
ACaptureSessionOutput_create(ANativeWindow * window,ACaptureSessionOutput ** out)98 camera_status_t ACaptureSessionOutput_create(
99         ANativeWindow* window, /*out*/ACaptureSessionOutput** out) {
100     ATRACE_CALL();
101     if (window == nullptr || out == nullptr) {
102         ALOGE("%s: Error: bad argument. window %p, out %p",
103                 __FUNCTION__, window, out);
104         return ACAMERA_ERROR_INVALID_PARAMETER;
105     }
106     *out = new ACaptureSessionOutput(window, false);
107     return ACAMERA_OK;
108 }
109 
110 EXPORT
ACaptureSessionSharedOutput_create(ANativeWindow * window,ACaptureSessionOutput ** out)111 camera_status_t ACaptureSessionSharedOutput_create(
112         ANativeWindow* window, /*out*/ACaptureSessionOutput** out) {
113     ATRACE_CALL();
114     if (window == nullptr || out == nullptr) {
115         ALOGE("%s: Error: bad argument. window %p, out %p",
116                 __FUNCTION__, window, out);
117         return ACAMERA_ERROR_INVALID_PARAMETER;
118     }
119     *out = new ACaptureSessionOutput(window, true);
120     return ACAMERA_OK;
121 }
122 
123 EXPORT
ACaptureSessionSharedOutput_add(ACaptureSessionOutput * out,ANativeWindow * window)124 camera_status_t ACaptureSessionSharedOutput_add(ACaptureSessionOutput *out,
125         ANativeWindow* window) {
126     ATRACE_CALL();
127     if ((window == nullptr) || (out == nullptr)) {
128         ALOGE("%s: Error: bad argument. window %p, out %p",
129                 __FUNCTION__, window, out);
130         return ACAMERA_ERROR_INVALID_PARAMETER;
131     }
132     if (!out->mIsShared) {
133         ALOGE("%s: Error trying to insert a new window in non-shared output configuration",
134                 __FUNCTION__);
135         return ACAMERA_ERROR_INVALID_OPERATION;
136     }
137     if (out->mWindow == window) {
138         ALOGE("%s: Error trying to add the same window associated with the output configuration",
139                 __FUNCTION__);
140         return ACAMERA_ERROR_INVALID_PARAMETER;
141     }
142 
143     auto insert = out->mSharedWindows.insert(window);
144     camera_status_t ret = (insert.second) ? ACAMERA_OK : ACAMERA_ERROR_INVALID_PARAMETER;
145     return ret;
146 }
147 
148 EXPORT
ACaptureSessionSharedOutput_remove(ACaptureSessionOutput * out,ANativeWindow * window)149 camera_status_t ACaptureSessionSharedOutput_remove(ACaptureSessionOutput *out,
150         ANativeWindow* window) {
151     ATRACE_CALL();
152     if ((window == nullptr) || (out == nullptr)) {
153         ALOGE("%s: Error: bad argument. window %p, out %p",
154                 __FUNCTION__, window, out);
155         return ACAMERA_ERROR_INVALID_PARAMETER;
156     }
157     if (!out->mIsShared) {
158         ALOGE("%s: Error trying to remove a  window in non-shared output configuration",
159                 __FUNCTION__);
160         return ACAMERA_ERROR_INVALID_OPERATION;
161     }
162     if (out->mWindow == window) {
163         ALOGE("%s: Error trying to remove the same window associated with the output configuration",
164                 __FUNCTION__);
165         return ACAMERA_ERROR_INVALID_PARAMETER;
166     }
167 
168     auto remove = out->mSharedWindows.erase(window);
169     camera_status_t ret = (remove) ? ACAMERA_OK : ACAMERA_ERROR_INVALID_PARAMETER;
170     return ret;
171 }
172 
173 EXPORT
ACaptureSessionOutput_free(ACaptureSessionOutput * output)174 void ACaptureSessionOutput_free(ACaptureSessionOutput* output) {
175     ATRACE_CALL();
176     if (output != nullptr) {
177         delete output;
178     }
179     return;
180 }
181 
182 EXPORT
ACaptureSessionOutputContainer_add(ACaptureSessionOutputContainer * container,const ACaptureSessionOutput * output)183 camera_status_t ACaptureSessionOutputContainer_add(
184         ACaptureSessionOutputContainer* container, const ACaptureSessionOutput* output) {
185     ATRACE_CALL();
186     if (container == nullptr || output == nullptr) {
187         ALOGE("%s: Error: invalid input: container %p, output %p",
188                 __FUNCTION__, container, output);
189         return ACAMERA_ERROR_INVALID_PARAMETER;
190     }
191     auto pair = container->mOutputs.insert(*output);
192     if (!pair.second) {
193         ALOGW("%s: output %p already exists!", __FUNCTION__, output);
194     }
195     return ACAMERA_OK;
196 }
197 
198 EXPORT
ACaptureSessionOutputContainer_remove(ACaptureSessionOutputContainer * container,const ACaptureSessionOutput * output)199 camera_status_t ACaptureSessionOutputContainer_remove(
200         ACaptureSessionOutputContainer* container, const ACaptureSessionOutput* output) {
201     ATRACE_CALL();
202     if (container == nullptr || output == nullptr) {
203         ALOGE("%s: Error: invalid input: container %p, output %p",
204                 __FUNCTION__, container, output);
205         return ACAMERA_ERROR_INVALID_PARAMETER;
206     }
207     container->mOutputs.erase(*output);
208     return ACAMERA_OK;
209 }
210 
211 EXPORT
ACameraDevice_createCaptureSession(ACameraDevice * device,const ACaptureSessionOutputContainer * outputs,const ACameraCaptureSession_stateCallbacks * callbacks,ACameraCaptureSession ** session)212 camera_status_t ACameraDevice_createCaptureSession(
213         ACameraDevice* device,
214         const ACaptureSessionOutputContainer*       outputs,
215         const ACameraCaptureSession_stateCallbacks* callbacks,
216         /*out*/ACameraCaptureSession** session) {
217     ATRACE_CALL();
218     if (device == nullptr || outputs == nullptr || callbacks == nullptr || session == nullptr) {
219         ALOGE("%s: Error: invalid input: device %p, outputs %p, callbacks %p, session %p",
220                 __FUNCTION__, device, outputs, callbacks, session);
221         return ACAMERA_ERROR_INVALID_PARAMETER;
222     }
223     return device->createCaptureSession(outputs, nullptr, callbacks, session);
224 }
225 
226 EXPORT
ACameraDevice_createCaptureSessionWithSessionParameters(ACameraDevice * device,const ACaptureSessionOutputContainer * outputs,const ACaptureRequest * sessionParameters,const ACameraCaptureSession_stateCallbacks * callbacks,ACameraCaptureSession ** session)227 camera_status_t ACameraDevice_createCaptureSessionWithSessionParameters(
228         ACameraDevice* device,
229         const ACaptureSessionOutputContainer*       outputs,
230         const ACaptureRequest* sessionParameters,
231         const ACameraCaptureSession_stateCallbacks* callbacks,
232         /*out*/ACameraCaptureSession** session) {
233     ATRACE_CALL();
234     if (device == nullptr || outputs == nullptr || callbacks == nullptr || session == nullptr) {
235         ALOGE("%s: Error: invalid input: device %p, outputs %p, callbacks %p, session %p",
236                 __FUNCTION__, device, outputs, callbacks, session);
237         return ACAMERA_ERROR_INVALID_PARAMETER;
238     }
239     return device->createCaptureSession(outputs, sessionParameters, callbacks, session);
240 }
241