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