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 "NdkCaptureRequest"
19 #define ATRACE_TAG ATRACE_TAG_CAMERA
20 
21 #include <utils/Log.h>
22 #include <utils/Trace.h>
23 
24 #include <camera/NdkCaptureRequest.h>
25 #include "impl/ACameraMetadata.h"
26 #include "impl/ACaptureRequest.h"
27 
28 EXPORT
ACameraOutputTarget_create(ACameraWindowType * window,ACameraOutputTarget ** out)29 camera_status_t ACameraOutputTarget_create(
30         ACameraWindowType* window, ACameraOutputTarget** out) {
31     ATRACE_CALL();
32     if (window == nullptr) {
33         ALOGE("%s: Error: input window is null", __FUNCTION__);
34         return ACAMERA_ERROR_INVALID_PARAMETER;
35     }
36     *out = new ACameraOutputTarget(window);
37     return ACAMERA_OK;
38 }
39 
40 EXPORT
ACameraOutputTarget_free(ACameraOutputTarget * target)41 void ACameraOutputTarget_free(ACameraOutputTarget* target) {
42     ATRACE_CALL();
43     if (target != nullptr) {
44         delete target;
45     }
46     return;
47 }
48 
49 EXPORT
ACaptureRequest_addTarget(ACaptureRequest * req,const ACameraOutputTarget * target)50 camera_status_t ACaptureRequest_addTarget(
51         ACaptureRequest* req, const ACameraOutputTarget* target) {
52     ATRACE_CALL();
53     if (req == nullptr || req->targets == nullptr || target == nullptr) {
54         void* req_targets;
55         if (req != nullptr)
56             req_targets = req->targets;
57         else
58             req_targets = nullptr;
59         ALOGE("%s: Error: invalid input: req %p, req-targets %p, target %p",
60                 __FUNCTION__, req, req_targets, target);
61         return ACAMERA_ERROR_INVALID_PARAMETER;
62     }
63     auto pair = req->targets->mOutputs.insert(*target);
64     if (!pair.second) {
65         ALOGW("%s: target %p already exists!", __FUNCTION__, target);
66     }
67     return ACAMERA_OK;
68 }
69 
70 EXPORT
ACaptureRequest_removeTarget(ACaptureRequest * req,const ACameraOutputTarget * target)71 camera_status_t ACaptureRequest_removeTarget(
72         ACaptureRequest* req, const ACameraOutputTarget* target) {
73     ATRACE_CALL();
74     if (req == nullptr || req->targets == nullptr || target == nullptr) {
75         void* req_targets;
76         if (req != nullptr)
77             req_targets = req->targets;
78         else
79             req_targets = nullptr;
80         ALOGE("%s: Error: invalid input: req %p, req-targets %p, target %p",
81                 __FUNCTION__, req, req_targets, target);
82         return ACAMERA_ERROR_INVALID_PARAMETER;
83     }
84     req->targets->mOutputs.erase(*target);
85     return ACAMERA_OK;
86 }
87 
88 EXPORT
ACaptureRequest_getConstEntry(const ACaptureRequest * req,uint32_t tag,ACameraMetadata_const_entry * entry)89 camera_status_t ACaptureRequest_getConstEntry(
90         const ACaptureRequest* req, uint32_t tag, ACameraMetadata_const_entry* entry) {
91     ATRACE_CALL();
92     if (req == nullptr || entry == nullptr) {
93         ALOGE("%s: invalid argument! req 0x%p, tag 0x%x, entry 0x%p",
94                __FUNCTION__, req, tag, entry);
95         return ACAMERA_ERROR_INVALID_PARAMETER;
96     }
97     return req->settings->getConstEntry(tag, entry);
98 }
99 
100 EXPORT
ACaptureRequest_getConstEntry_physicalCamera(const ACaptureRequest * req,const char * physicalId,uint32_t tag,ACameraMetadata_const_entry * entry)101 camera_status_t ACaptureRequest_getConstEntry_physicalCamera(
102         const ACaptureRequest* req, const char* physicalId,
103         uint32_t tag, ACameraMetadata_const_entry* entry) {
104     ATRACE_CALL();
105     if (req == nullptr || entry == nullptr || physicalId == nullptr) {
106         ALOGE("%s: invalid argument! req %p, tag 0x%x, entry %p, physicalId %p",
107                __FUNCTION__, req, tag, entry, physicalId);
108         return ACAMERA_ERROR_INVALID_PARAMETER;
109     }
110 
111     const auto& physicalSettings = req->physicalSettings.find(physicalId);
112     if (physicalSettings == req->physicalSettings.end()) {
113         ALOGE("%s: Failed to find metadata for physical camera id  %s",
114                 __FUNCTION__, physicalId);
115         return ACAMERA_ERROR_INVALID_PARAMETER;
116     }
117 
118     return physicalSettings->second->getConstEntry(tag, entry);
119 }
120 
121 EXPORT
ACaptureRequest_getAllTags(const ACaptureRequest * req,int32_t * numTags,const uint32_t ** tags)122 camera_status_t ACaptureRequest_getAllTags(
123         const ACaptureRequest* req, /*out*/int32_t* numTags, /*out*/const uint32_t** tags) {
124     ATRACE_CALL();
125     if (req == nullptr || numTags == nullptr || tags == nullptr) {
126         ALOGE("%s: invalid argument! request %p, numTags %p, tags %p",
127                __FUNCTION__, req, numTags, tags);
128         return ACAMERA_ERROR_INVALID_PARAMETER;
129     }
130     return req->settings->getTags(numTags, tags);
131 }
132 
133 #define SET_ENTRY(NAME,NDK_TYPE)                                                        \
134 EXPORT                                                                                  \
135 camera_status_t ACaptureRequest_setEntry_##NAME(                                        \
136         ACaptureRequest* req, uint32_t tag, uint32_t count, const NDK_TYPE* data) {     \
137     ATRACE_CALL();                                                                      \
138     if (req == nullptr || (count > 0 && data == nullptr)) {                             \
139         ALOGE("%s: invalid argument! req %p, tag 0x%x, count %d, data 0x%p",            \
140                __FUNCTION__, req, tag, count, data);                                    \
141         return ACAMERA_ERROR_INVALID_PARAMETER;                                         \
142     }                                                                                   \
143     return req->settings->update(tag, count, data);                                     \
144 }
145 
SET_ENTRY(u8,uint8_t)146 SET_ENTRY(u8,uint8_t)
147 SET_ENTRY(i32,int32_t)
148 SET_ENTRY(float,float)
149 SET_ENTRY(double,double)
150 SET_ENTRY(i64,int64_t)
151 SET_ENTRY(rational,ACameraMetadata_rational)
152 
153 #undef SET_ENTRY
154 
155 #define SET_PHYSICAL_ENTRY(NAME,NDK_TYPE)                                               \
156 EXPORT                                                                                  \
157 camera_status_t ACaptureRequest_setEntry_physicalCamera_##NAME(                         \
158         ACaptureRequest* req, const char* physicalId, uint32_t tag,                     \
159         uint32_t count, const NDK_TYPE* data) {                                         \
160     ATRACE_CALL();                                                                      \
161     if (req == nullptr || (count > 0 && data == nullptr) || physicalId == nullptr) {    \
162         ALOGE("%s: invalid argument! req %p, tag 0x%x, count %d, data 0x%p, physicalId %p", \
163                __FUNCTION__, req, tag, count, data, physicalId);                        \
164         return ACAMERA_ERROR_INVALID_PARAMETER;                                         \
165     }                                                                                   \
166     if (req->physicalSettings.find(physicalId) == req->physicalSettings.end()) {        \
167         ALOGE("%s: Failed to find metadata for physical camera id %s",                  \
168             __FUNCTION__, physicalId);                                                  \
169       return ACAMERA_ERROR_INVALID_PARAMETER;                                           \
170     }                                                                                   \
171     return req->physicalSettings[physicalId]->update(tag, count, data);                 \
172 }
173 
174 SET_PHYSICAL_ENTRY(u8,uint8_t)
175 SET_PHYSICAL_ENTRY(i32,int32_t)
176 SET_PHYSICAL_ENTRY(float,float)
177 SET_PHYSICAL_ENTRY(double,double)
178 SET_PHYSICAL_ENTRY(i64,int64_t)
179 SET_PHYSICAL_ENTRY(rational,ACameraMetadata_rational)
180 
181 #undef SET_PHYSICAL_ENTRY
182 
183 EXPORT
184 void ACaptureRequest_free(ACaptureRequest* request) {
185     ATRACE_CALL();
186     if (request == nullptr) {
187         return;
188     }
189     request->settings.clear();
190     request->physicalSettings.clear();
191     delete request->targets;
192     delete request;
193     return;
194 }
195 
196 EXPORT
ACaptureRequest_setUserContext(ACaptureRequest * request,void * context)197 camera_status_t ACaptureRequest_setUserContext(
198         ACaptureRequest* request, void* context) {
199     if (request == nullptr) {
200         ALOGE("%s: invalid argument! request is NULL", __FUNCTION__);
201         return ACAMERA_ERROR_INVALID_PARAMETER;
202     }
203     return request->setContext(context);
204 }
205 
206 EXPORT
ACaptureRequest_getUserContext(const ACaptureRequest * request,void ** context)207 camera_status_t ACaptureRequest_getUserContext(
208         const ACaptureRequest* request, /*out*/void** context) {
209     if (request == nullptr || context == nullptr) {
210         ALOGE("%s: invalid argument! request %p, context %p",
211                 __FUNCTION__, request, context);
212         return ACAMERA_ERROR_INVALID_PARAMETER;
213     }
214     return request->getContext(context);
215 }
216 
217 EXPORT
ACaptureRequest_copy(const ACaptureRequest * src)218 ACaptureRequest* ACaptureRequest_copy(const ACaptureRequest* src) {
219     ATRACE_CALL();
220     if (src == nullptr) {
221         ALOGE("%s: src is null!", __FUNCTION__);
222         return nullptr;
223     }
224 
225     ACaptureRequest* pRequest = new ACaptureRequest();
226     pRequest->settings = new ACameraMetadata(*(src->settings));
227     for (const auto& entry : src->physicalSettings) {
228         pRequest->physicalSettings[entry.first] = new ACameraMetadata(*(entry.second));
229     }
230     pRequest->targets  = new ACameraOutputTargets();
231     *(pRequest->targets)  = *(src->targets);
232     pRequest->context = src->context;
233     return pRequest;
234 }
235