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 "Metadata" 19 #include <cutils/log.h> 20 21 #include <system/camera_metadata.h> 22 23 #define ATRACE_TAG (ATRACE_TAG_CAMERA | ATRACE_TAG_HAL) 24 #include <utils/Trace.h> 25 26 #include "Metadata.h" 27 28 namespace usb_camera_hal { 29 30 Metadata::Metadata(): 31 mData(NULL) { 32 } 33 34 Metadata::~Metadata() { 35 replace(NULL); 36 } 37 38 void Metadata::replace(camera_metadata_t *m) { 39 if (m == mData) { 40 return; 41 } 42 if (mData) 43 free_camera_metadata(mData); 44 mData = m; 45 } 46 47 int Metadata::init(const camera_metadata_t *metadata) { 48 camera_metadata_t* tmp; 49 50 if (!validate_camera_metadata_structure(metadata, NULL)) 51 return -EINVAL; 52 53 tmp = clone_camera_metadata(metadata); 54 if (tmp == NULL) 55 return -EINVAL; 56 57 replace(tmp); 58 return 0; 59 } 60 61 int Metadata::addUInt8(uint32_t tag, int count, const uint8_t *data) { 62 if (!validate(tag, TYPE_BYTE, count)) return -EINVAL; 63 return add(tag, count, data); 64 } 65 66 int Metadata::add1UInt8(uint32_t tag, const uint8_t data) { 67 return addUInt8(tag, 1, &data); 68 } 69 70 int Metadata::addInt32(uint32_t tag, int count, const int32_t *data) { 71 if (!validate(tag, TYPE_INT32, count)) return -EINVAL; 72 return add(tag, count, data); 73 } 74 75 int Metadata::addFloat(uint32_t tag, int count, const float *data) { 76 if (!validate(tag, TYPE_FLOAT, count)) return -EINVAL; 77 return add(tag, count, data); 78 } 79 80 int Metadata::addInt64(uint32_t tag, int count, const int64_t *data) { 81 if (!validate(tag, TYPE_INT64, count)) return -EINVAL; 82 return add(tag, count, data); 83 } 84 85 int Metadata::addDouble(uint32_t tag, int count, const double *data) { 86 if (!validate(tag, TYPE_DOUBLE, count)) return -EINVAL; 87 return add(tag, count, data); 88 } 89 90 int Metadata::addRational(uint32_t tag, int count, 91 const camera_metadata_rational_t *data) { 92 if (!validate(tag, TYPE_RATIONAL, count)) return -EINVAL; 93 return add(tag, count, data); 94 } 95 96 bool Metadata::validate(uint32_t tag, int tag_type, int count) { 97 if (get_camera_metadata_tag_type(tag) < 0) { 98 ALOGE("%s: Invalid metadata entry tag: %d", __func__, tag); 99 return false; 100 } 101 if (tag_type < 0 || tag_type >= NUM_TYPES) { 102 ALOGE("%s: Invalid metadata entry tag type: %d", __func__, tag_type); 103 return false; 104 } 105 if (tag_type != get_camera_metadata_tag_type(tag)) { 106 ALOGE("%s: Tag %d called with incorrect type: %s(%d)", __func__, tag, 107 camera_metadata_type_names[tag_type], tag_type); 108 return false; 109 } 110 if (count < 1) { 111 ALOGE("%s: Invalid metadata entry count: %d", __func__, count); 112 return false; 113 } 114 return true; 115 } 116 117 int Metadata::add(uint32_t tag, int count, const void *tag_data) { 118 // Opportunistically attempt to add if metadata has room for it 119 if (!add_camera_metadata_entry(mData, tag, tag_data, count)) { 120 return 0; 121 } 122 123 int res; 124 camera_metadata_t* tmp; 125 int tag_type = get_camera_metadata_tag_type(tag); 126 size_t size = calculate_camera_metadata_entry_data_size(tag_type, count); 127 size_t entry_capacity = get_camera_metadata_entry_count(mData) + 1; 128 size_t data_capacity = get_camera_metadata_data_count(mData) + size; 129 130 // Double new dimensions to minimize future reallocations 131 tmp = allocate_camera_metadata(entry_capacity * 2, data_capacity * 2); 132 if (tmp == NULL) { 133 ALOGE("%s: Failed to allocate new metadata with %zu entries, %zu data", 134 __func__, entry_capacity, data_capacity); 135 return -ENOMEM; 136 } 137 // Append the current metadata to the new (empty) metadata 138 res = append_camera_metadata(tmp, mData); 139 if (res) { 140 ALOGE("%s: Failed to append old metadata %p to new %p", 141 __func__, mData, tmp); 142 return res; 143 } 144 // Add the remaining new item 145 res = add_camera_metadata_entry(tmp, tag, tag_data, count); 146 if (res) { 147 ALOGE("%s: Failed to add new entry (%d, %p, %d) to metadata %p", 148 __func__, tag, tag_data, count, tmp); 149 return res; 150 } 151 152 replace(tmp); 153 return 0; 154 } 155 156 camera_metadata_t* Metadata::get() { 157 return mData; 158 } 159 160 } // namespace usb_camera_hal 161