Alex Ray | b0be103 | 2013-05-28 15:52:47 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013 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 | #include <pthread.h> |
| 18 | #include <system/camera_metadata.h> |
| 19 | |
| 20 | //#define LOG_NDEBUG 0 |
| 21 | #define LOG_TAG "Metadata" |
| 22 | #include <cutils/log.h> |
| 23 | |
| 24 | #define ATRACE_TAG (ATRACE_TAG_CAMERA | ATRACE_TAG_HAL) |
Alex Ray | ea80382 | 2013-10-14 15:56:43 -0700 | [diff] [blame^] | 25 | #include <utils/Trace.h> |
Alex Ray | b0be103 | 2013-05-28 15:52:47 -0700 | [diff] [blame] | 26 | |
| 27 | #include "Metadata.h" |
| 28 | |
| 29 | namespace default_camera_hal { |
| 30 | |
| 31 | Metadata::Metadata() |
| 32 | : mHead(NULL), |
| 33 | mTail(NULL), |
| 34 | mEntryCount(0), |
Alex Ray | 0d2a522 | 2013-07-02 15:47:24 -0700 | [diff] [blame] | 35 | mDataCount(0), |
| 36 | mGenerated(NULL), |
| 37 | mDirty(true) |
Alex Ray | b0be103 | 2013-05-28 15:52:47 -0700 | [diff] [blame] | 38 | { |
| 39 | // NULL (default) pthread mutex attributes |
| 40 | pthread_mutex_init(&mMutex, NULL); |
| 41 | } |
| 42 | |
| 43 | Metadata::~Metadata() |
| 44 | { |
Alex Ray | 90c0af7 | 2013-05-31 16:51:39 -0700 | [diff] [blame] | 45 | Entry *current = mHead; |
| 46 | |
| 47 | while (current != NULL) { |
| 48 | Entry *tmp = current; |
| 49 | current = current->mNext; |
| 50 | delete tmp; |
| 51 | } |
| 52 | |
Alex Ray | 0d2a522 | 2013-07-02 15:47:24 -0700 | [diff] [blame] | 53 | if (mGenerated != NULL) |
| 54 | free_camera_metadata(mGenerated); |
Alex Ray | 90c0af7 | 2013-05-31 16:51:39 -0700 | [diff] [blame] | 55 | |
| 56 | pthread_mutex_destroy(&mMutex); |
Alex Ray | b0be103 | 2013-05-28 15:52:47 -0700 | [diff] [blame] | 57 | } |
| 58 | |
Alex Ray | 89a8266 | 2013-05-28 20:32:48 -0700 | [diff] [blame] | 59 | Metadata::Metadata(uint8_t mode, uint8_t intent) |
| 60 | : mHead(NULL), |
| 61 | mTail(NULL), |
| 62 | mEntryCount(0), |
Alex Ray | 0d2a522 | 2013-07-02 15:47:24 -0700 | [diff] [blame] | 63 | mDataCount(0), |
| 64 | mGenerated(NULL), |
| 65 | mDirty(true) |
Alex Ray | 89a8266 | 2013-05-28 20:32:48 -0700 | [diff] [blame] | 66 | { |
| 67 | pthread_mutex_init(&mMutex, NULL); |
| 68 | |
| 69 | if (validate(ANDROID_CONTROL_MODE, TYPE_BYTE, 1)) { |
| 70 | int res = add(ANDROID_CONTROL_MODE, 1, &mode); |
| 71 | if (res != 0) { |
| 72 | ALOGE("%s: Unable to add mode to template!", __func__); |
| 73 | } |
| 74 | } else { |
| 75 | ALOGE("%s: Invalid mode constructing template!", __func__); |
| 76 | } |
| 77 | |
| 78 | if (validate(ANDROID_CONTROL_CAPTURE_INTENT, TYPE_BYTE, 1)) { |
| 79 | int res = add(ANDROID_CONTROL_CAPTURE_INTENT, 1, &intent); |
| 80 | if (res != 0) { |
| 81 | ALOGE("%s: Unable to add capture intent to template!", __func__); |
| 82 | } |
| 83 | } else { |
| 84 | ALOGE("%s: Invalid capture intent constructing template!", __func__); |
| 85 | } |
| 86 | } |
| 87 | |
Alex Ray | b0be103 | 2013-05-28 15:52:47 -0700 | [diff] [blame] | 88 | int Metadata::addUInt8(uint32_t tag, int count, uint8_t *data) |
| 89 | { |
| 90 | if (!validate(tag, TYPE_BYTE, count)) return -EINVAL; |
| 91 | return add(tag, count, data); |
| 92 | } |
| 93 | |
| 94 | int Metadata::addInt32(uint32_t tag, int count, int32_t *data) |
| 95 | { |
| 96 | if (!validate(tag, TYPE_INT32, count)) return -EINVAL; |
| 97 | return add(tag, count, data); |
| 98 | } |
| 99 | |
| 100 | int Metadata::addFloat(uint32_t tag, int count, float *data) |
| 101 | { |
| 102 | if (!validate(tag, TYPE_FLOAT, count)) return -EINVAL; |
| 103 | return add(tag, count, data); |
| 104 | } |
| 105 | |
| 106 | int Metadata::addInt64(uint32_t tag, int count, int64_t *data) |
| 107 | { |
| 108 | if (!validate(tag, TYPE_INT64, count)) return -EINVAL; |
| 109 | return add(tag, count, data); |
| 110 | } |
| 111 | |
| 112 | int Metadata::addDouble(uint32_t tag, int count, double *data) |
| 113 | { |
| 114 | if (!validate(tag, TYPE_DOUBLE, count)) return -EINVAL; |
| 115 | return add(tag, count, data); |
| 116 | } |
| 117 | |
| 118 | int Metadata::addRational(uint32_t tag, int count, |
| 119 | camera_metadata_rational_t *data) |
| 120 | { |
| 121 | if (!validate(tag, TYPE_RATIONAL, count)) return -EINVAL; |
| 122 | return add(tag, count, data); |
| 123 | } |
| 124 | |
| 125 | bool Metadata::validate(uint32_t tag, int tag_type, int count) |
| 126 | { |
| 127 | if (get_camera_metadata_tag_type(tag) < 0) { |
| 128 | ALOGE("%s: Invalid metadata entry tag: %d", __func__, tag); |
| 129 | return false; |
| 130 | } |
| 131 | if (tag_type < 0 || tag_type >= NUM_TYPES) { |
| 132 | ALOGE("%s: Invalid metadata entry tag type: %d", __func__, tag_type); |
| 133 | return false; |
| 134 | } |
| 135 | if (tag_type != get_camera_metadata_tag_type(tag)) { |
| 136 | ALOGE("%s: Tag %d called with incorrect type: %s(%d)", __func__, tag, |
| 137 | camera_metadata_type_names[tag_type], tag_type); |
| 138 | return false; |
| 139 | } |
| 140 | if (count < 1) { |
| 141 | ALOGE("%s: Invalid metadata entry count: %d", __func__, count); |
| 142 | return false; |
| 143 | } |
| 144 | return true; |
| 145 | } |
| 146 | |
| 147 | int Metadata::add(uint32_t tag, int count, void *tag_data) |
| 148 | { |
| 149 | int tag_type = get_camera_metadata_tag_type(tag); |
| 150 | size_t type_sz = camera_metadata_type_size[tag_type]; |
| 151 | |
| 152 | // Allocate array to hold new metadata |
| 153 | void *data = malloc(count * type_sz); |
| 154 | if (data == NULL) |
| 155 | return -ENOMEM; |
| 156 | memcpy(data, tag_data, count * type_sz); |
| 157 | |
Alex Ray | 89a8266 | 2013-05-28 20:32:48 -0700 | [diff] [blame] | 158 | pthread_mutex_lock(&mMutex); |
Alex Ray | b0be103 | 2013-05-28 15:52:47 -0700 | [diff] [blame] | 159 | mEntryCount++; |
| 160 | mDataCount += calculate_camera_metadata_entry_data_size(tag_type, count); |
| 161 | push(new Entry(tag, data, count)); |
Alex Ray | 0d2a522 | 2013-07-02 15:47:24 -0700 | [diff] [blame] | 162 | mDirty = true; |
Alex Ray | 89a8266 | 2013-05-28 20:32:48 -0700 | [diff] [blame] | 163 | pthread_mutex_unlock(&mMutex); |
Alex Ray | b0be103 | 2013-05-28 15:52:47 -0700 | [diff] [blame] | 164 | return 0; |
| 165 | } |
| 166 | |
| 167 | camera_metadata_t* Metadata::generate() |
| 168 | { |
Alex Ray | b0be103 | 2013-05-28 15:52:47 -0700 | [diff] [blame] | 169 | pthread_mutex_lock(&mMutex); |
Alex Ray | 0d2a522 | 2013-07-02 15:47:24 -0700 | [diff] [blame] | 170 | // Reuse if old generated metadata still valid |
| 171 | if (!mDirty && mGenerated != NULL) { |
| 172 | ALOGV("%s: Reusing generated metadata at %p", __func__, mGenerated); |
| 173 | goto out; |
| 174 | } |
| 175 | // Destroy old metadata |
| 176 | if (mGenerated != NULL) { |
| 177 | ALOGV("%s: Freeing generated metadata at %p", __func__, mGenerated); |
| 178 | free_camera_metadata(mGenerated); |
| 179 | mGenerated = NULL; |
| 180 | } |
| 181 | // Generate new metadata structure |
| 182 | ALOGV("%s: Generating new camera metadata structure, Entries:%d Data:%d", |
| 183 | __func__, mEntryCount, mDataCount); |
| 184 | mGenerated = allocate_camera_metadata(mEntryCount, mDataCount); |
Alex Ray | b0be103 | 2013-05-28 15:52:47 -0700 | [diff] [blame] | 185 | if (mGenerated == NULL) { |
Alex Ray | 0d2a522 | 2013-07-02 15:47:24 -0700 | [diff] [blame] | 186 | ALOGE("%s: Failed to allocate metadata (%d entries %d data)", |
| 187 | __func__, mEntryCount, mDataCount); |
| 188 | goto out; |
Alex Ray | b0be103 | 2013-05-28 15:52:47 -0700 | [diff] [blame] | 189 | } |
| 190 | // Walk list of entries adding each one to newly allocated metadata |
Alex Ray | 9cd49c9 | 2013-07-09 12:19:49 -0700 | [diff] [blame] | 191 | for (Entry *current = mHead; current != NULL; current = current->mNext) { |
Alex Ray | 0d2a522 | 2013-07-02 15:47:24 -0700 | [diff] [blame] | 192 | int res = add_camera_metadata_entry(mGenerated, current->mTag, |
| 193 | current->mData, current->mCount); |
| 194 | if (res != 0) { |
| 195 | ALOGE("%s: Failed to add camera metadata: %d", __func__, res); |
| 196 | free_camera_metadata(mGenerated); |
| 197 | mGenerated = NULL; |
| 198 | goto out; |
| 199 | } |
Alex Ray | b0be103 | 2013-05-28 15:52:47 -0700 | [diff] [blame] | 200 | } |
Alex Ray | b0be103 | 2013-05-28 15:52:47 -0700 | [diff] [blame] | 201 | |
Alex Ray | 0d2a522 | 2013-07-02 15:47:24 -0700 | [diff] [blame] | 202 | out: |
| 203 | pthread_mutex_unlock(&mMutex); |
Alex Ray | b0be103 | 2013-05-28 15:52:47 -0700 | [diff] [blame] | 204 | return mGenerated; |
| 205 | } |
| 206 | |
| 207 | Metadata::Entry::Entry(uint32_t tag, void *data, int count) |
| 208 | : mNext(NULL), |
| 209 | mPrev(NULL), |
| 210 | mTag(tag), |
| 211 | mData(data), |
| 212 | mCount(count) |
| 213 | { |
| 214 | } |
| 215 | |
| 216 | void Metadata::push(Entry *e) |
| 217 | { |
| 218 | if (mHead == NULL) { |
| 219 | mHead = mTail = e; |
| 220 | } else { |
| 221 | mTail->insertAfter(e); |
| 222 | mTail = e; |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | Metadata::Entry::~Entry() |
| 227 | { |
| 228 | if (mNext != NULL) |
| 229 | mNext->mPrev = mPrev; |
| 230 | if (mPrev != NULL) |
| 231 | mPrev->mNext = mNext; |
| 232 | } |
| 233 | |
| 234 | void Metadata::Entry::insertAfter(Entry *e) |
| 235 | { |
| 236 | if (e == NULL) |
| 237 | return; |
| 238 | if (mNext != NULL) |
| 239 | mNext->mPrev = e; |
| 240 | e->mNext = mNext; |
| 241 | e->mPrev = this; |
| 242 | mNext = e; |
| 243 | } |
| 244 | |
| 245 | } // namespace default_camera_hal |