blob: 96ea5f24482ad909f6afea696deb6c31ad7c5bee [file] [log] [blame]
Eino-Ville Talvalacab96a42012-08-24 11:29:22 -07001/*
2 * Copyright (C) 2012 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
Igor Murashkine2d1e3d2013-04-30 18:18:06 -070017// #define LOG_NDEBUG 0
18
Eino-Ville Talvala4bb81182012-09-24 09:46:53 -070019#define LOG_TAG "Camera2-Metadata"
Eino-Ville Talvalacab96a42012-08-24 11:29:22 -070020#include <utils/Log.h>
21#include <utils/Errors.h>
22
Igor Murashkine7ee7632013-06-11 18:10:18 -070023#include <binder/Parcel.h>
Eino-Ville Talvala4d453832016-07-15 11:56:53 -070024#include <camera/CameraMetadata.h>
Emilian Peev0ec234e2020-08-24 11:48:50 -070025#include <camera_metadata_hidden.h>
Eino-Ville Talvalacab96a42012-08-24 11:29:22 -070026
27namespace android {
28
Zhijun He146aed12013-12-05 07:46:51 -080029#define ALIGN_TO(val, alignment) \
30 (((uintptr_t)(val) + ((alignment) - 1)) & ~((alignment) - 1))
31
Igor Murashkine7ee7632013-06-11 18:10:18 -070032typedef Parcel::WritableBlob WritableBlob;
33typedef Parcel::ReadableBlob ReadableBlob;
34
Eino-Ville Talvalacab96a42012-08-24 11:29:22 -070035CameraMetadata::CameraMetadata() :
Eino-Ville Talvala3b53bc92013-02-27 18:02:26 -080036 mBuffer(NULL), mLocked(false) {
Eino-Ville Talvalacab96a42012-08-24 11:29:22 -070037}
38
Eino-Ville Talvala3b53bc92013-02-27 18:02:26 -080039CameraMetadata::CameraMetadata(size_t entryCapacity, size_t dataCapacity) :
40 mLocked(false)
Eino-Ville Talvalacab96a42012-08-24 11:29:22 -070041{
42 mBuffer = allocate_camera_metadata(entryCapacity, dataCapacity);
43}
44
Eino-Ville Talvala3b53bc92013-02-27 18:02:26 -080045CameraMetadata::CameraMetadata(const CameraMetadata &other) :
46 mLocked(false) {
Eino-Ville Talvalacab96a42012-08-24 11:29:22 -070047 mBuffer = clone_camera_metadata(other.mBuffer);
48}
49
Jayant Chowdhary8a0be292020-01-08 13:10:38 -080050CameraMetadata::CameraMetadata(CameraMetadata &&other) :mBuffer(NULL), mLocked(false) {
51 acquire(other);
52}
53
54CameraMetadata &CameraMetadata::operator=(CameraMetadata &&other) {
55 acquire(other);
56 return *this;
57}
58
Eino-Ville Talvala3b53bc92013-02-27 18:02:26 -080059CameraMetadata::CameraMetadata(camera_metadata_t *buffer) :
60 mBuffer(NULL), mLocked(false) {
Igor Murashkin7efa5202013-02-13 15:53:56 -080061 acquire(buffer);
62}
63
Eino-Ville Talvalacab96a42012-08-24 11:29:22 -070064CameraMetadata &CameraMetadata::operator=(const CameraMetadata &other) {
65 return operator=(other.mBuffer);
66}
67
68CameraMetadata &CameraMetadata::operator=(const camera_metadata_t *buffer) {
Eino-Ville Talvala3b53bc92013-02-27 18:02:26 -080069 if (mLocked) {
70 ALOGE("%s: Assignment to a locked CameraMetadata!", __FUNCTION__);
71 return *this;
72 }
73
Eino-Ville Talvalacab96a42012-08-24 11:29:22 -070074 if (CC_LIKELY(buffer != mBuffer)) {
75 camera_metadata_t *newBuffer = clone_camera_metadata(buffer);
76 clear();
77 mBuffer = newBuffer;
78 }
79 return *this;
80}
81
82CameraMetadata::~CameraMetadata() {
Eino-Ville Talvala3b53bc92013-02-27 18:02:26 -080083 mLocked = false;
Eino-Ville Talvalacab96a42012-08-24 11:29:22 -070084 clear();
85}
86
Yin-Chia Yeh54298b32015-03-24 16:51:41 -070087const camera_metadata_t* CameraMetadata::getAndLock() const {
Eino-Ville Talvala3b53bc92013-02-27 18:02:26 -080088 mLocked = true;
89 return mBuffer;
90}
91
Yin-Chia Yeh8aac03f2016-03-03 15:45:23 -080092status_t CameraMetadata::unlock(const camera_metadata_t *buffer) const {
Eino-Ville Talvala3b53bc92013-02-27 18:02:26 -080093 if (!mLocked) {
94 ALOGE("%s: Can't unlock a non-locked CameraMetadata!", __FUNCTION__);
95 return INVALID_OPERATION;
96 }
97 if (buffer != mBuffer) {
98 ALOGE("%s: Can't unlock CameraMetadata with wrong pointer!",
99 __FUNCTION__);
100 return BAD_VALUE;
101 }
102 mLocked = false;
103 return OK;
104}
105
Eino-Ville Talvalacab96a42012-08-24 11:29:22 -0700106camera_metadata_t* CameraMetadata::release() {
Eino-Ville Talvala3b53bc92013-02-27 18:02:26 -0800107 if (mLocked) {
108 ALOGE("%s: CameraMetadata is locked", __FUNCTION__);
109 return NULL;
110 }
Eino-Ville Talvalacab96a42012-08-24 11:29:22 -0700111 camera_metadata_t *released = mBuffer;
112 mBuffer = NULL;
113 return released;
114}
115
116void CameraMetadata::clear() {
Eino-Ville Talvala3b53bc92013-02-27 18:02:26 -0800117 if (mLocked) {
118 ALOGE("%s: CameraMetadata is locked", __FUNCTION__);
119 return;
120 }
Eino-Ville Talvalacab96a42012-08-24 11:29:22 -0700121 if (mBuffer) {
122 free_camera_metadata(mBuffer);
123 mBuffer = NULL;
124 }
125}
126
127void CameraMetadata::acquire(camera_metadata_t *buffer) {
Eino-Ville Talvala3b53bc92013-02-27 18:02:26 -0800128 if (mLocked) {
129 ALOGE("%s: CameraMetadata is locked", __FUNCTION__);
130 return;
131 }
Eino-Ville Talvalacab96a42012-08-24 11:29:22 -0700132 clear();
133 mBuffer = buffer;
Igor Murashkine2d1e3d2013-04-30 18:18:06 -0700134
135 ALOGE_IF(validate_camera_metadata_structure(mBuffer, /*size*/NULL) != OK,
136 "%s: Failed to validate metadata structure %p",
137 __FUNCTION__, buffer);
Eino-Ville Talvalacab96a42012-08-24 11:29:22 -0700138}
139
140void CameraMetadata::acquire(CameraMetadata &other) {
Eino-Ville Talvala3b53bc92013-02-27 18:02:26 -0800141 if (mLocked) {
142 ALOGE("%s: CameraMetadata is locked", __FUNCTION__);
143 return;
144 }
Eino-Ville Talvalacab96a42012-08-24 11:29:22 -0700145 acquire(other.release());
146}
147
148status_t CameraMetadata::append(const CameraMetadata &other) {
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -0700149 return append(other.mBuffer);
150}
151
152status_t CameraMetadata::append(const camera_metadata_t* other) {
Eino-Ville Talvala3b53bc92013-02-27 18:02:26 -0800153 if (mLocked) {
154 ALOGE("%s: CameraMetadata is locked", __FUNCTION__);
155 return INVALID_OPERATION;
156 }
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -0700157 size_t extraEntries = get_camera_metadata_entry_count(other);
158 size_t extraData = get_camera_metadata_data_count(other);
159 resizeIfNeeded(extraEntries, extraData);
160
161 return append_camera_metadata(mBuffer, other);
Eino-Ville Talvalacab96a42012-08-24 11:29:22 -0700162}
163
164size_t CameraMetadata::entryCount() const {
165 return (mBuffer == NULL) ? 0 :
166 get_camera_metadata_entry_count(mBuffer);
167}
168
Eino-Ville Talvalada6665c2012-08-29 17:37:16 -0700169bool CameraMetadata::isEmpty() const {
170 return entryCount() == 0;
171}
172
Emilian Peev5f268f32020-09-09 17:29:48 -0700173size_t CameraMetadata::bufferSize() const {
174 return (mBuffer == NULL) ? 0 :
175 get_camera_metadata_size(mBuffer);
176}
177
Eino-Ville Talvalacab96a42012-08-24 11:29:22 -0700178status_t CameraMetadata::sort() {
Eino-Ville Talvala3b53bc92013-02-27 18:02:26 -0800179 if (mLocked) {
180 ALOGE("%s: CameraMetadata is locked", __FUNCTION__);
181 return INVALID_OPERATION;
182 }
Eino-Ville Talvalacab96a42012-08-24 11:29:22 -0700183 return sort_camera_metadata(mBuffer);
184}
185
186status_t CameraMetadata::checkType(uint32_t tag, uint8_t expectedType) {
Emilian Peev71c73a22017-03-21 16:35:51 +0000187 int tagType = get_local_camera_metadata_tag_type(tag, mBuffer);
Eino-Ville Talvalacab96a42012-08-24 11:29:22 -0700188 if ( CC_UNLIKELY(tagType == -1)) {
189 ALOGE("Update metadata entry: Unknown tag %d", tag);
190 return INVALID_OPERATION;
191 }
192 if ( CC_UNLIKELY(tagType != expectedType) ) {
193 ALOGE("Mismatched tag type when updating entry %s (%d) of type %s; "
194 "got type %s data instead ",
Emilian Peev71c73a22017-03-21 16:35:51 +0000195 get_local_camera_metadata_tag_name(tag, mBuffer), tag,
Eino-Ville Talvalacab96a42012-08-24 11:29:22 -0700196 camera_metadata_type_names[tagType],
197 camera_metadata_type_names[expectedType]);
198 return INVALID_OPERATION;
199 }
200 return OK;
201}
202
203status_t CameraMetadata::update(uint32_t tag,
204 const int32_t *data, size_t data_count) {
205 status_t res;
Eino-Ville Talvala3b53bc92013-02-27 18:02:26 -0800206 if (mLocked) {
207 ALOGE("%s: CameraMetadata is locked", __FUNCTION__);
208 return INVALID_OPERATION;
209 }
Eino-Ville Talvalacab96a42012-08-24 11:29:22 -0700210 if ( (res = checkType(tag, TYPE_INT32)) != OK) {
211 return res;
212 }
Eino-Ville Talvala3b53bc92013-02-27 18:02:26 -0800213 return updateImpl(tag, (const void*)data, data_count);
Eino-Ville Talvalacab96a42012-08-24 11:29:22 -0700214}
215
216status_t CameraMetadata::update(uint32_t tag,
217 const uint8_t *data, size_t data_count) {
218 status_t res;
Eino-Ville Talvala3b53bc92013-02-27 18:02:26 -0800219 if (mLocked) {
220 ALOGE("%s: CameraMetadata is locked", __FUNCTION__);
221 return INVALID_OPERATION;
222 }
Eino-Ville Talvalacab96a42012-08-24 11:29:22 -0700223 if ( (res = checkType(tag, TYPE_BYTE)) != OK) {
224 return res;
225 }
Eino-Ville Talvala3b53bc92013-02-27 18:02:26 -0800226 return updateImpl(tag, (const void*)data, data_count);
Eino-Ville Talvalacab96a42012-08-24 11:29:22 -0700227}
228
229status_t CameraMetadata::update(uint32_t tag,
230 const float *data, size_t data_count) {
231 status_t res;
Eino-Ville Talvala3b53bc92013-02-27 18:02:26 -0800232 if (mLocked) {
233 ALOGE("%s: CameraMetadata is locked", __FUNCTION__);
234 return INVALID_OPERATION;
235 }
Eino-Ville Talvalacab96a42012-08-24 11:29:22 -0700236 if ( (res = checkType(tag, TYPE_FLOAT)) != OK) {
237 return res;
238 }
Eino-Ville Talvala3b53bc92013-02-27 18:02:26 -0800239 return updateImpl(tag, (const void*)data, data_count);
Eino-Ville Talvalacab96a42012-08-24 11:29:22 -0700240}
241
242status_t CameraMetadata::update(uint32_t tag,
243 const int64_t *data, size_t data_count) {
244 status_t res;
Eino-Ville Talvala3b53bc92013-02-27 18:02:26 -0800245 if (mLocked) {
246 ALOGE("%s: CameraMetadata is locked", __FUNCTION__);
247 return INVALID_OPERATION;
248 }
Eino-Ville Talvalacab96a42012-08-24 11:29:22 -0700249 if ( (res = checkType(tag, TYPE_INT64)) != OK) {
250 return res;
251 }
Eino-Ville Talvala3b53bc92013-02-27 18:02:26 -0800252 return updateImpl(tag, (const void*)data, data_count);
Eino-Ville Talvalacab96a42012-08-24 11:29:22 -0700253}
254
255status_t CameraMetadata::update(uint32_t tag,
256 const double *data, size_t data_count) {
257 status_t res;
Eino-Ville Talvala3b53bc92013-02-27 18:02:26 -0800258 if (mLocked) {
259 ALOGE("%s: CameraMetadata is locked", __FUNCTION__);
260 return INVALID_OPERATION;
261 }
Eino-Ville Talvalacab96a42012-08-24 11:29:22 -0700262 if ( (res = checkType(tag, TYPE_DOUBLE)) != OK) {
263 return res;
264 }
Eino-Ville Talvala3b53bc92013-02-27 18:02:26 -0800265 return updateImpl(tag, (const void*)data, data_count);
Eino-Ville Talvalacab96a42012-08-24 11:29:22 -0700266}
267
268status_t CameraMetadata::update(uint32_t tag,
269 const camera_metadata_rational_t *data, size_t data_count) {
270 status_t res;
Eino-Ville Talvala3b53bc92013-02-27 18:02:26 -0800271 if (mLocked) {
272 ALOGE("%s: CameraMetadata is locked", __FUNCTION__);
273 return INVALID_OPERATION;
274 }
Eino-Ville Talvalacab96a42012-08-24 11:29:22 -0700275 if ( (res = checkType(tag, TYPE_RATIONAL)) != OK) {
276 return res;
277 }
Eino-Ville Talvala3b53bc92013-02-27 18:02:26 -0800278 return updateImpl(tag, (const void*)data, data_count);
Eino-Ville Talvalacab96a42012-08-24 11:29:22 -0700279}
280
281status_t CameraMetadata::update(uint32_t tag,
282 const String8 &string) {
283 status_t res;
Eino-Ville Talvala3b53bc92013-02-27 18:02:26 -0800284 if (mLocked) {
285 ALOGE("%s: CameraMetadata is locked", __FUNCTION__);
286 return INVALID_OPERATION;
287 }
Eino-Ville Talvalacab96a42012-08-24 11:29:22 -0700288 if ( (res = checkType(tag, TYPE_BYTE)) != OK) {
289 return res;
290 }
Zhijun He7595c472014-03-27 16:46:15 -0700291 // string.size() doesn't count the null termination character.
292 return updateImpl(tag, (const void*)string.string(), string.size() + 1);
Eino-Ville Talvalacab96a42012-08-24 11:29:22 -0700293}
294
Eino-Ville Talvala4d453832016-07-15 11:56:53 -0700295status_t CameraMetadata::update(const camera_metadata_ro_entry &entry) {
296 status_t res;
297 if (mLocked) {
298 ALOGE("%s: CameraMetadata is locked", __FUNCTION__);
299 return INVALID_OPERATION;
300 }
301 if ( (res = checkType(entry.tag, entry.type)) != OK) {
302 return res;
303 }
304 return updateImpl(entry.tag, (const void*)entry.data.u8, entry.count);
305}
306
Eino-Ville Talvala3b53bc92013-02-27 18:02:26 -0800307status_t CameraMetadata::updateImpl(uint32_t tag, const void *data,
Eino-Ville Talvalacab96a42012-08-24 11:29:22 -0700308 size_t data_count) {
309 status_t res;
Eino-Ville Talvala3b53bc92013-02-27 18:02:26 -0800310 if (mLocked) {
311 ALOGE("%s: CameraMetadata is locked", __FUNCTION__);
312 return INVALID_OPERATION;
313 }
Emilian Peev71c73a22017-03-21 16:35:51 +0000314 int type = get_local_camera_metadata_tag_type(tag, mBuffer);
Eino-Ville Talvalacab96a42012-08-24 11:29:22 -0700315 if (type == -1) {
316 ALOGE("%s: Tag %d not found", __FUNCTION__, tag);
317 return BAD_VALUE;
318 }
Eino-Ville Talvalae2b60c82015-07-17 16:21:44 -0700319 // Safety check - ensure that data isn't pointing to this metadata, since
320 // that would get invalidated if a resize is needed
321 size_t bufferSize = get_camera_metadata_size(mBuffer);
322 uintptr_t bufAddr = reinterpret_cast<uintptr_t>(mBuffer);
323 uintptr_t dataAddr = reinterpret_cast<uintptr_t>(data);
324 if (dataAddr > bufAddr && dataAddr < (bufAddr + bufferSize)) {
325 ALOGE("%s: Update attempted with data from the same metadata buffer!",
326 __FUNCTION__);
327 return INVALID_OPERATION;
328 }
329
Eino-Ville Talvalacab96a42012-08-24 11:29:22 -0700330 size_t data_size = calculate_camera_metadata_entry_data_size(type,
331 data_count);
332
333 res = resizeIfNeeded(1, data_size);
334
335 if (res == OK) {
336 camera_metadata_entry_t entry;
337 res = find_camera_metadata_entry(mBuffer, tag, &entry);
338 if (res == NAME_NOT_FOUND) {
339 res = add_camera_metadata_entry(mBuffer,
340 tag, data, data_count);
341 } else if (res == OK) {
342 res = update_camera_metadata_entry(mBuffer,
343 entry.index, data, data_count, NULL);
344 }
345 }
346
347 if (res != OK) {
348 ALOGE("%s: Unable to update metadata entry %s.%s (%x): %s (%d)",
Emilian Peev71c73a22017-03-21 16:35:51 +0000349 __FUNCTION__, get_local_camera_metadata_section_name(tag, mBuffer),
350 get_local_camera_metadata_tag_name(tag, mBuffer), tag,
351 strerror(-res), res);
Eino-Ville Talvalacab96a42012-08-24 11:29:22 -0700352 }
Igor Murashkine2d1e3d2013-04-30 18:18:06 -0700353
354 IF_ALOGV() {
355 ALOGE_IF(validate_camera_metadata_structure(mBuffer, /*size*/NULL) !=
356 OK,
357
358 "%s: Failed to validate metadata structure after update %p",
359 __FUNCTION__, mBuffer);
360 }
361
Eino-Ville Talvalacab96a42012-08-24 11:29:22 -0700362 return res;
363}
364
Igor Murashkinfc42642a2013-02-13 18:23:39 -0800365bool CameraMetadata::exists(uint32_t tag) const {
366 camera_metadata_ro_entry entry;
367 return find_camera_metadata_ro_entry(mBuffer, tag, &entry) == 0;
368}
369
Eino-Ville Talvalacab96a42012-08-24 11:29:22 -0700370camera_metadata_entry_t CameraMetadata::find(uint32_t tag) {
371 status_t res;
372 camera_metadata_entry entry;
Eino-Ville Talvala3b53bc92013-02-27 18:02:26 -0800373 if (mLocked) {
374 ALOGE("%s: CameraMetadata is locked", __FUNCTION__);
375 entry.count = 0;
376 return entry;
377 }
Eino-Ville Talvalacab96a42012-08-24 11:29:22 -0700378 res = find_camera_metadata_entry(mBuffer, tag, &entry);
379 if (CC_UNLIKELY( res != OK )) {
380 entry.count = 0;
381 entry.data.u8 = NULL;
382 }
383 return entry;
384}
385
386camera_metadata_ro_entry_t CameraMetadata::find(uint32_t tag) const {
387 status_t res;
388 camera_metadata_ro_entry entry;
389 res = find_camera_metadata_ro_entry(mBuffer, tag, &entry);
390 if (CC_UNLIKELY( res != OK )) {
391 entry.count = 0;
392 entry.data.u8 = NULL;
393 }
394 return entry;
395}
396
397status_t CameraMetadata::erase(uint32_t tag) {
398 camera_metadata_entry_t entry;
399 status_t res;
Eino-Ville Talvala3b53bc92013-02-27 18:02:26 -0800400 if (mLocked) {
401 ALOGE("%s: CameraMetadata is locked", __FUNCTION__);
402 return INVALID_OPERATION;
403 }
Eino-Ville Talvalacab96a42012-08-24 11:29:22 -0700404 res = find_camera_metadata_entry(mBuffer, tag, &entry);
405 if (res == NAME_NOT_FOUND) {
406 return OK;
407 } else if (res != OK) {
408 ALOGE("%s: Error looking for entry %s.%s (%x): %s %d",
409 __FUNCTION__,
Emilian Peev71c73a22017-03-21 16:35:51 +0000410 get_local_camera_metadata_section_name(tag, mBuffer),
411 get_local_camera_metadata_tag_name(tag, mBuffer),
412 tag, strerror(-res), res);
Eino-Ville Talvalacab96a42012-08-24 11:29:22 -0700413 return res;
414 }
415 res = delete_camera_metadata_entry(mBuffer, entry.index);
416 if (res != OK) {
417 ALOGE("%s: Error deleting entry %s.%s (%x): %s %d",
418 __FUNCTION__,
Emilian Peev71c73a22017-03-21 16:35:51 +0000419 get_local_camera_metadata_section_name(tag, mBuffer),
420 get_local_camera_metadata_tag_name(tag, mBuffer),
421 tag, strerror(-res), res);
Eino-Ville Talvalacab96a42012-08-24 11:29:22 -0700422 }
423 return res;
424}
425
Emilian Peeve20c6372018-08-14 18:45:53 +0100426status_t CameraMetadata::removePermissionEntries(metadata_vendor_id_t vendorId,
427 std::vector<int32_t> *tagsRemoved) {
428 uint32_t tagCount = 0;
429 std::vector<uint32_t> tagsToRemove;
430
431 if (tagsRemoved == nullptr) {
432 return BAD_VALUE;
433 }
434
435 sp<VendorTagDescriptor> vTags = VendorTagDescriptor::getGlobalVendorTagDescriptor();
436 if ((nullptr == vTags.get()) || (0 >= vTags->getTagCount())) {
437 sp<VendorTagDescriptorCache> cache =
438 VendorTagDescriptorCache::getGlobalVendorTagCache();
439 if (cache.get()) {
440 cache->getVendorTagDescriptor(vendorId, &vTags);
441 }
442 }
443
444 if ((nullptr != vTags.get()) && (vTags->getTagCount() > 0)) {
445 tagCount = vTags->getTagCount();
446 uint32_t *vendorTags = new uint32_t[tagCount];
447 if (nullptr == vendorTags) {
448 return NO_MEMORY;
449 }
450 vTags->getTagArray(vendorTags);
451
452 tagsToRemove.reserve(tagCount);
453 tagsToRemove.insert(tagsToRemove.begin(), vendorTags, vendorTags + tagCount);
454
455 delete [] vendorTags;
456 tagCount = 0;
457 }
458
459 auto tagsNeedingPermission = get_camera_metadata_permission_needed(&tagCount);
460 if (tagCount > 0) {
461 tagsToRemove.reserve(tagsToRemove.capacity() + tagCount);
462 tagsToRemove.insert(tagsToRemove.end(), tagsNeedingPermission,
463 tagsNeedingPermission + tagCount);
464 }
465
466 tagsRemoved->reserve(tagsToRemove.size());
467 for (const auto &it : tagsToRemove) {
468 if (exists(it)) {
469 auto rc = erase(it);
470 if (NO_ERROR != rc) {
471 ALOGE("%s: Failed to erase tag: %x", __func__, it);
472 return rc;
473 }
474 tagsRemoved->push_back(it);
475 }
476 }
477
478 // Update the available characterstics accordingly
479 if (exists(ANDROID_REQUEST_AVAILABLE_CHARACTERISTICS_KEYS)) {
480 std::vector<uint32_t> currentKeys;
481
482 std::sort(tagsRemoved->begin(), tagsRemoved->end());
483 auto keys = find(ANDROID_REQUEST_AVAILABLE_CHARACTERISTICS_KEYS);
484 currentKeys.reserve(keys.count);
485 currentKeys.insert(currentKeys.end(), keys.data.i32, keys.data.i32 + keys.count);
486 std::sort(currentKeys.begin(), currentKeys.end());
487
488 std::vector<int32_t> newKeys(keys.count);
489 auto end = std::set_difference(currentKeys.begin(), currentKeys.end(), tagsRemoved->begin(),
490 tagsRemoved->end(), newKeys.begin());
491 newKeys.resize(end - newKeys.begin());
492
493 update(ANDROID_REQUEST_AVAILABLE_CHARACTERISTICS_KEYS, newKeys.data(), newKeys.size());
494 }
495
496 return NO_ERROR;
497}
498
Eino-Ville Talvalacab96a42012-08-24 11:29:22 -0700499void CameraMetadata::dump(int fd, int verbosity, int indentation) const {
500 dump_indented_camera_metadata(mBuffer, fd, verbosity, indentation);
501}
502
503status_t CameraMetadata::resizeIfNeeded(size_t extraEntries, size_t extraData) {
504 if (mBuffer == NULL) {
505 mBuffer = allocate_camera_metadata(extraEntries * 2, extraData * 2);
506 if (mBuffer == NULL) {
507 ALOGE("%s: Can't allocate larger metadata buffer", __FUNCTION__);
508 return NO_MEMORY;
509 }
510 } else {
511 size_t currentEntryCount = get_camera_metadata_entry_count(mBuffer);
512 size_t currentEntryCap = get_camera_metadata_entry_capacity(mBuffer);
513 size_t newEntryCount = currentEntryCount +
514 extraEntries;
515 newEntryCount = (newEntryCount > currentEntryCap) ?
516 newEntryCount * 2 : currentEntryCap;
517
518 size_t currentDataCount = get_camera_metadata_data_count(mBuffer);
519 size_t currentDataCap = get_camera_metadata_data_capacity(mBuffer);
520 size_t newDataCount = currentDataCount +
521 extraData;
522 newDataCount = (newDataCount > currentDataCap) ?
523 newDataCount * 2 : currentDataCap;
524
525 if (newEntryCount > currentEntryCap ||
526 newDataCount > currentDataCap) {
527 camera_metadata_t *oldBuffer = mBuffer;
528 mBuffer = allocate_camera_metadata(newEntryCount,
529 newDataCount);
530 if (mBuffer == NULL) {
531 ALOGE("%s: Can't allocate larger metadata buffer", __FUNCTION__);
532 return NO_MEMORY;
533 }
534 append_camera_metadata(mBuffer, oldBuffer);
535 free_camera_metadata(oldBuffer);
536 }
537 }
538 return OK;
539}
540
Igor Murashkine7ee7632013-06-11 18:10:18 -0700541status_t CameraMetadata::readFromParcel(const Parcel& data,
542 camera_metadata_t** out) {
Jayant Chowdhary2e310da2020-05-12 12:40:36 -0700543
Igor Murashkine7ee7632013-06-11 18:10:18 -0700544 status_t err = OK;
545
546 camera_metadata_t* metadata = NULL;
547
548 if (out) {
549 *out = NULL;
550 }
551
Zhijun He146aed12013-12-05 07:46:51 -0800552 // See CameraMetadata::writeToParcel for parcel data layout diagram and explanation.
553 // arg0 = blobSize (int32)
554 int32_t blobSizeTmp = -1;
555 if ((err = data.readInt32(&blobSizeTmp)) != OK) {
Igor Murashkine7ee7632013-06-11 18:10:18 -0700556 ALOGE("%s: Failed to read metadata size (error %d %s)",
557 __FUNCTION__, err, strerror(-err));
558 return err;
559 }
Zhijun He146aed12013-12-05 07:46:51 -0800560 const size_t blobSize = static_cast<size_t>(blobSizeTmp);
561 const size_t alignment = get_camera_metadata_alignment();
Igor Murashkine7ee7632013-06-11 18:10:18 -0700562
Zhijun He146aed12013-12-05 07:46:51 -0800563 // Special case: zero blob size means zero sized (NULL) metadata.
564 if (blobSize == 0) {
Igor Murashkine7ee7632013-06-11 18:10:18 -0700565 ALOGV("%s: Read 0-sized metadata", __FUNCTION__);
566 return OK;
567 }
568
Zhijun He146aed12013-12-05 07:46:51 -0800569 if (blobSize <= alignment) {
570 ALOGE("%s: metadata blob is malformed, blobSize(%zu) should be larger than alignment(%zu)",
571 __FUNCTION__, blobSize, alignment);
572 return BAD_VALUE;
573 }
574
575 const size_t metadataSize = blobSize - alignment;
576
577 // NOTE: this doesn't make sense to me. shouldn't the blob
Igor Murashkine7ee7632013-06-11 18:10:18 -0700578 // know how big it is? why do we have to specify the size
579 // to Parcel::readBlob ?
Igor Murashkine7ee7632013-06-11 18:10:18 -0700580 ReadableBlob blob;
581 // arg1 = metadata (blob)
582 do {
Zhijun He146aed12013-12-05 07:46:51 -0800583 if ((err = data.readBlob(blobSize, &blob)) != OK) {
584 ALOGE("%s: Failed to read metadata blob (sized %zu). Possible "
Igor Murashkine7ee7632013-06-11 18:10:18 -0700585 " serialization bug. Error %d %s",
Zhijun He146aed12013-12-05 07:46:51 -0800586 __FUNCTION__, blobSize, err, strerror(-err));
Igor Murashkine7ee7632013-06-11 18:10:18 -0700587 break;
588 }
Igor Murashkine7ee7632013-06-11 18:10:18 -0700589
Zhijun He146aed12013-12-05 07:46:51 -0800590 // arg2 = offset (blob)
591 // Must be after blob since we don't know offset until after writeBlob.
592 int32_t offsetTmp;
593 if ((err = data.readInt32(&offsetTmp)) != OK) {
594 ALOGE("%s: Failed to read metadata offsetTmp (error %d %s)",
595 __FUNCTION__, err, strerror(-err));
596 break;
597 }
598 const size_t offset = static_cast<size_t>(offsetTmp);
599 if (offset >= alignment) {
600 ALOGE("%s: metadata offset(%zu) should be less than alignment(%zu)",
601 __FUNCTION__, blobSize, alignment);
602 err = BAD_VALUE;
603 break;
604 }
605
606 const uintptr_t metadataStart = reinterpret_cast<uintptr_t>(blob.data()) + offset;
607 const camera_metadata_t* tmp =
608 reinterpret_cast<const camera_metadata_t*>(metadataStart);
609 ALOGV("%s: alignment is: %zu, metadata start: %p, offset: %zu",
610 __FUNCTION__, alignment, tmp, offset);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700611 metadata = allocate_copy_camera_metadata_checked(tmp, metadataSize);
612 if (metadata == NULL) {
613 // We consider that allocation only fails if the validation
614 // also failed, therefore the readFromParcel was a failure.
Zhijun He146aed12013-12-05 07:46:51 -0800615 ALOGE("%s: metadata allocation and copy failed", __FUNCTION__);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700616 err = BAD_VALUE;
617 }
618 } while(0);
619 blob.release();
620
621 if (out) {
622 ALOGV("%s: Set out metadata to %p", __FUNCTION__, metadata);
623 *out = metadata;
624 } else if (metadata != NULL) {
625 ALOGV("%s: Freed camera metadata at %p", __FUNCTION__, metadata);
626 free_camera_metadata(metadata);
627 }
628
629 return err;
630}
631
632status_t CameraMetadata::writeToParcel(Parcel& data,
633 const camera_metadata_t* metadata) {
634 status_t res = OK;
635
Zhijun He146aed12013-12-05 07:46:51 -0800636 /**
637 * Below is the camera metadata parcel layout:
638 *
639 * |--------------------------------------------|
640 * | arg0: blobSize |
641 * | (length = 4) |
642 * |--------------------------------------------|<--Skip the rest if blobSize == 0.
643 * | |
644 * | |
645 * | arg1: blob |
646 * | (length = variable, see arg1 layout below) |
647 * | |
648 * | |
649 * |--------------------------------------------|
650 * | arg2: offset |
651 * | (length = 4) |
652 * |--------------------------------------------|
653 */
Igor Murashkine7ee7632013-06-11 18:10:18 -0700654
Zhijun He146aed12013-12-05 07:46:51 -0800655 // arg0 = blobSize (int32)
Igor Murashkine7ee7632013-06-11 18:10:18 -0700656 if (metadata == NULL) {
Zhijun He146aed12013-12-05 07:46:51 -0800657 // Write zero blobSize for null metadata.
Igor Murashkine7ee7632013-06-11 18:10:18 -0700658 return data.writeInt32(0);
659 }
660
Zhijun He146aed12013-12-05 07:46:51 -0800661 /**
662 * Always make the blob size sufficiently larger, as we need put alignment
663 * padding and metadata into the blob. Since we don't know the alignment
664 * offset before writeBlob. Then write the metadata to aligned offset.
665 */
Igor Murashkine7ee7632013-06-11 18:10:18 -0700666 const size_t metadataSize = get_camera_metadata_compact_size(metadata);
Zhijun He146aed12013-12-05 07:46:51 -0800667 const size_t alignment = get_camera_metadata_alignment();
668 const size_t blobSize = metadataSize + alignment;
669 res = data.writeInt32(static_cast<int32_t>(blobSize));
Igor Murashkine7ee7632013-06-11 18:10:18 -0700670 if (res != OK) {
671 return res;
672 }
673
Zhijun He146aed12013-12-05 07:46:51 -0800674 size_t offset = 0;
675 /**
676 * arg1 = metadata (blob).
677 *
678 * The blob size is the sum of front padding size, metadata size and back padding
679 * size, which is equal to metadataSize + alignment.
680 *
681 * The blob layout is:
682 * |------------------------------------|<----Start address of the blob (unaligned).
683 * | front padding |
684 * | (size = offset) |
685 * |------------------------------------|<----Aligned start address of metadata.
686 * | |
687 * | |
688 * | metadata |
689 * | (size = metadataSize) |
690 * | |
691 * | |
692 * |------------------------------------|
693 * | back padding |
694 * | (size = alignment - offset) |
695 * |------------------------------------|<----End address of blob.
696 * (Blob start address + blob size).
697 */
Igor Murashkine7ee7632013-06-11 18:10:18 -0700698 WritableBlob blob;
699 do {
Jeff Browne8df5392015-06-05 15:10:44 -0700700 res = data.writeBlob(blobSize, false, &blob);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700701 if (res != OK) {
702 break;
703 }
Zhijun He146aed12013-12-05 07:46:51 -0800704 const uintptr_t metadataStart = ALIGN_TO(blob.data(), alignment);
705 offset = metadataStart - reinterpret_cast<uintptr_t>(blob.data());
706 ALOGV("%s: alignment is: %zu, metadata start: %p, offset: %zu",
Mark Salyzyn1a93f0c2014-06-09 16:34:58 -0700707 __FUNCTION__, alignment,
708 reinterpret_cast<const void *>(metadataStart), offset);
Zhijun He146aed12013-12-05 07:46:51 -0800709 copy_camera_metadata(reinterpret_cast<void*>(metadataStart), metadataSize, metadata);
Igor Murashkine7ee7632013-06-11 18:10:18 -0700710
711 // Not too big of a problem since receiving side does hard validation
712 // Don't check the size since the compact size could be larger
713 if (validate_camera_metadata_structure(metadata, /*size*/NULL) != OK) {
714 ALOGW("%s: Failed to validate metadata %p before writing blob",
715 __FUNCTION__, metadata);
716 }
717
718 } while(false);
719 blob.release();
720
Zhijun He146aed12013-12-05 07:46:51 -0800721 // arg2 = offset (int32)
722 res = data.writeInt32(static_cast<int32_t>(offset));
723
Igor Murashkine7ee7632013-06-11 18:10:18 -0700724 return res;
725}
726
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800727status_t CameraMetadata::readFromParcel(const Parcel *parcel) {
Jayant Chowdhary2e310da2020-05-12 12:40:36 -0700728
Igor Murashkine7ee7632013-06-11 18:10:18 -0700729 ALOGV("%s: parcel = %p", __FUNCTION__, parcel);
730
731 status_t res = OK;
732
733 if (parcel == NULL) {
734 ALOGE("%s: parcel is null", __FUNCTION__);
735 return BAD_VALUE;
736 }
737
738 if (mLocked) {
739 ALOGE("%s: CameraMetadata is locked", __FUNCTION__);
740 return INVALID_OPERATION;
741 }
742
743 camera_metadata *buffer = NULL;
744 // TODO: reading should return a status code, in case validation fails
745 res = CameraMetadata::readFromParcel(*parcel, &buffer);
746
747 if (res != NO_ERROR) {
748 ALOGE("%s: Failed to read from parcel. Metadata is unchanged.",
749 __FUNCTION__);
750 return res;
751 }
752
753 clear();
754 mBuffer = buffer;
755
756 return OK;
757}
758
759status_t CameraMetadata::writeToParcel(Parcel *parcel) const {
Jayant Chowdhary2e310da2020-05-12 12:40:36 -0700760
Igor Murashkine7ee7632013-06-11 18:10:18 -0700761 ALOGV("%s: parcel = %p", __FUNCTION__, parcel);
762
763 if (parcel == NULL) {
764 ALOGE("%s: parcel is null", __FUNCTION__);
765 return BAD_VALUE;
766 }
767
768 return CameraMetadata::writeToParcel(*parcel, mBuffer);
769}
770
771void CameraMetadata::swap(CameraMetadata& other) {
772 if (mLocked) {
773 ALOGE("%s: CameraMetadata is locked", __FUNCTION__);
774 return;
775 } else if (other.mLocked) {
776 ALOGE("%s: Other CameraMetadata is locked", __FUNCTION__);
777 return;
778 }
779
780 camera_metadata* thisBuf = mBuffer;
781 camera_metadata* otherBuf = other.mBuffer;
782
783 other.mBuffer = thisBuf;
784 mBuffer = otherBuf;
785}
786
Eino-Ville Talvala4d453832016-07-15 11:56:53 -0700787status_t CameraMetadata::getTagFromName(const char *name,
788 const VendorTagDescriptor* vTags, uint32_t *tag) {
Jayant Chowdhary2e310da2020-05-12 12:40:36 -0700789
Eino-Ville Talvala4d453832016-07-15 11:56:53 -0700790 if (name == nullptr || tag == nullptr) return BAD_VALUE;
791
792 size_t nameLength = strlen(name);
793
794 const SortedVector<String8> *vendorSections;
795 size_t vendorSectionCount = 0;
796
797 if (vTags != NULL) {
798 vendorSections = vTags->getAllSectionNames();
799 vendorSectionCount = vendorSections->size();
800 }
801
802 // First, find the section by the longest string match
803 const char *section = NULL;
804 size_t sectionIndex = 0;
805 size_t sectionLength = 0;
806 size_t totalSectionCount = ANDROID_SECTION_COUNT + vendorSectionCount;
807 for (size_t i = 0; i < totalSectionCount; ++i) {
808
809 const char *str = (i < ANDROID_SECTION_COUNT) ? camera_metadata_section_names[i] :
810 (*vendorSections)[i - ANDROID_SECTION_COUNT].string();
811
812 ALOGV("%s: Trying to match against section '%s'", __FUNCTION__, str);
813
814 if (strstr(name, str) == name) { // name begins with the section name
815 size_t strLength = strlen(str);
816
817 ALOGV("%s: Name begins with section name", __FUNCTION__);
818
819 // section name is the longest we've found so far
820 if (section == NULL || sectionLength < strLength) {
821 section = str;
822 sectionIndex = i;
823 sectionLength = strLength;
824
825 ALOGV("%s: Found new best section (%s)", __FUNCTION__, section);
826 }
827 }
828 }
829
830 // TODO: Make above get_camera_metadata_section_from_name ?
831
832 if (section == NULL) {
833 return NAME_NOT_FOUND;
834 } else {
835 ALOGV("%s: Found matched section '%s' (%zu)",
836 __FUNCTION__, section, sectionIndex);
837 }
838
839 // Get the tag name component of the name
840 const char *nameTagName = name + sectionLength + 1; // x.y.z -> z
841 if (sectionLength + 1 >= nameLength) {
842 return BAD_VALUE;
843 }
844
845 // Match rest of name against the tag names in that section only
846 uint32_t candidateTag = 0;
847 if (sectionIndex < ANDROID_SECTION_COUNT) {
848 // Match built-in tags (typically android.*)
849 uint32_t tagBegin, tagEnd; // [tagBegin, tagEnd)
850 tagBegin = camera_metadata_section_bounds[sectionIndex][0];
851 tagEnd = camera_metadata_section_bounds[sectionIndex][1];
852
853 for (candidateTag = tagBegin; candidateTag < tagEnd; ++candidateTag) {
854 const char *tagName = get_camera_metadata_tag_name(candidateTag);
855
856 if (strcmp(nameTagName, tagName) == 0) {
857 ALOGV("%s: Found matched tag '%s' (%d)",
858 __FUNCTION__, tagName, candidateTag);
859 break;
860 }
861 }
862
863 if (candidateTag == tagEnd) {
864 return NAME_NOT_FOUND;
865 }
866 } else if (vTags != NULL) {
867 // Match vendor tags (typically com.*)
868 const String8 sectionName(section);
869 const String8 tagName(nameTagName);
870
871 status_t res = OK;
872 if ((res = vTags->lookupTag(tagName, sectionName, &candidateTag)) != OK) {
873 return NAME_NOT_FOUND;
874 }
875 }
876
877 *tag = candidateTag;
878 return OK;
879}
880
Emilian Peev0ec234e2020-08-24 11:48:50 -0700881metadata_vendor_id_t CameraMetadata::getVendorId() {
882 return get_camera_metadata_vendor_id(mBuffer);
883}
Eino-Ville Talvala4d453832016-07-15 11:56:53 -0700884
Eino-Ville Talvalacab96a42012-08-24 11:29:22 -0700885}; // namespace android