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), |
| 36 | mDataCount(0) |
| 37 | { |
| 38 | // NULL (default) pthread mutex attributes |
| 39 | pthread_mutex_init(&mMutex, NULL); |
| 40 | } |
| 41 | |
| 42 | Metadata::~Metadata() |
| 43 | { |
| 44 | } |
| 45 | |
| 46 | int Metadata::addUInt8(uint32_t tag, int count, uint8_t *data) |
| 47 | { |
| 48 | if (!validate(tag, TYPE_BYTE, count)) return -EINVAL; |
| 49 | return add(tag, count, data); |
| 50 | } |
| 51 | |
| 52 | int Metadata::addInt32(uint32_t tag, int count, int32_t *data) |
| 53 | { |
| 54 | if (!validate(tag, TYPE_INT32, count)) return -EINVAL; |
| 55 | return add(tag, count, data); |
| 56 | } |
| 57 | |
| 58 | int Metadata::addFloat(uint32_t tag, int count, float *data) |
| 59 | { |
| 60 | if (!validate(tag, TYPE_FLOAT, count)) return -EINVAL; |
| 61 | return add(tag, count, data); |
| 62 | } |
| 63 | |
| 64 | int Metadata::addInt64(uint32_t tag, int count, int64_t *data) |
| 65 | { |
| 66 | if (!validate(tag, TYPE_INT64, count)) return -EINVAL; |
| 67 | return add(tag, count, data); |
| 68 | } |
| 69 | |
| 70 | int Metadata::addDouble(uint32_t tag, int count, double *data) |
| 71 | { |
| 72 | if (!validate(tag, TYPE_DOUBLE, count)) return -EINVAL; |
| 73 | return add(tag, count, data); |
| 74 | } |
| 75 | |
| 76 | int Metadata::addRational(uint32_t tag, int count, |
| 77 | camera_metadata_rational_t *data) |
| 78 | { |
| 79 | if (!validate(tag, TYPE_RATIONAL, count)) return -EINVAL; |
| 80 | return add(tag, count, data); |
| 81 | } |
| 82 | |
| 83 | bool Metadata::validate(uint32_t tag, int tag_type, int count) |
| 84 | { |
| 85 | if (get_camera_metadata_tag_type(tag) < 0) { |
| 86 | ALOGE("%s: Invalid metadata entry tag: %d", __func__, tag); |
| 87 | return false; |
| 88 | } |
| 89 | if (tag_type < 0 || tag_type >= NUM_TYPES) { |
| 90 | ALOGE("%s: Invalid metadata entry tag type: %d", __func__, tag_type); |
| 91 | return false; |
| 92 | } |
| 93 | if (tag_type != get_camera_metadata_tag_type(tag)) { |
| 94 | ALOGE("%s: Tag %d called with incorrect type: %s(%d)", __func__, tag, |
| 95 | camera_metadata_type_names[tag_type], tag_type); |
| 96 | return false; |
| 97 | } |
| 98 | if (count < 1) { |
| 99 | ALOGE("%s: Invalid metadata entry count: %d", __func__, count); |
| 100 | return false; |
| 101 | } |
| 102 | return true; |
| 103 | } |
| 104 | |
| 105 | int Metadata::add(uint32_t tag, int count, void *tag_data) |
| 106 | { |
| 107 | int tag_type = get_camera_metadata_tag_type(tag); |
| 108 | size_t type_sz = camera_metadata_type_size[tag_type]; |
| 109 | |
| 110 | // Allocate array to hold new metadata |
| 111 | void *data = malloc(count * type_sz); |
| 112 | if (data == NULL) |
| 113 | return -ENOMEM; |
| 114 | memcpy(data, tag_data, count * type_sz); |
| 115 | |
| 116 | mEntryCount++; |
| 117 | mDataCount += calculate_camera_metadata_entry_data_size(tag_type, count); |
| 118 | push(new Entry(tag, data, count)); |
| 119 | return 0; |
| 120 | } |
| 121 | |
| 122 | camera_metadata_t* Metadata::generate() |
| 123 | { |
| 124 | Entry *current = mHead; |
| 125 | |
| 126 | pthread_mutex_lock(&mMutex); |
| 127 | if (mGenerated == NULL) { |
| 128 | ALOGV("Generating new camera metadata structure"); |
| 129 | mGenerated = allocate_camera_metadata(mEntryCount, mDataCount); |
| 130 | if (mGenerated == NULL) { |
| 131 | ALOGE("%s: Failed to allocate metadata (%d entries %d data)", |
| 132 | __func__, mEntryCount, mDataCount); |
| 133 | } |
| 134 | } |
| 135 | // Walk list of entries adding each one to newly allocated metadata |
| 136 | while (current != NULL) { |
| 137 | add_camera_metadata_entry(mGenerated, current->mTag, current->mData, |
| 138 | current->mCount); |
| 139 | current = current->mNext; |
| 140 | } |
| 141 | pthread_mutex_unlock(&mMutex); |
| 142 | |
| 143 | return mGenerated; |
| 144 | } |
| 145 | |
| 146 | Metadata::Entry::Entry(uint32_t tag, void *data, int count) |
| 147 | : mNext(NULL), |
| 148 | mPrev(NULL), |
| 149 | mTag(tag), |
| 150 | mData(data), |
| 151 | mCount(count) |
| 152 | { |
| 153 | } |
| 154 | |
| 155 | void Metadata::push(Entry *e) |
| 156 | { |
| 157 | if (mHead == NULL) { |
| 158 | mHead = mTail = e; |
| 159 | } else { |
| 160 | mTail->insertAfter(e); |
| 161 | mTail = e; |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | Metadata::Entry::~Entry() |
| 166 | { |
| 167 | if (mNext != NULL) |
| 168 | mNext->mPrev = mPrev; |
| 169 | if (mPrev != NULL) |
| 170 | mPrev->mNext = mNext; |
| 171 | } |
| 172 | |
| 173 | void Metadata::Entry::insertAfter(Entry *e) |
| 174 | { |
| 175 | if (e == NULL) |
| 176 | return; |
| 177 | if (mNext != NULL) |
| 178 | mNext->mPrev = e; |
| 179 | e->mNext = mNext; |
| 180 | e->mPrev = this; |
| 181 | mNext = e; |
| 182 | } |
| 183 | |
| 184 | } // namespace default_camera_hal |