blob: ba7ed9ce35745b467b331c424ad2f638d8e75127 [file] [log] [blame]
Alex Rayb0be1032013-05-28 15:52:47 -07001/*
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 Rayea803822013-10-14 15:56:43 -070025#include <utils/Trace.h>
Alex Rayb0be1032013-05-28 15:52:47 -070026
27#include "Metadata.h"
28
29namespace default_camera_hal {
30
31Metadata::Metadata()
32 : mHead(NULL),
33 mTail(NULL),
34 mEntryCount(0),
Alex Ray0d2a5222013-07-02 15:47:24 -070035 mDataCount(0),
36 mGenerated(NULL),
37 mDirty(true)
Alex Rayb0be1032013-05-28 15:52:47 -070038{
39 // NULL (default) pthread mutex attributes
40 pthread_mutex_init(&mMutex, NULL);
41}
42
43Metadata::~Metadata()
44{
Alex Ray90c0af72013-05-31 16:51:39 -070045 Entry *current = mHead;
46
47 while (current != NULL) {
48 Entry *tmp = current;
49 current = current->mNext;
50 delete tmp;
51 }
52
Alex Ray0d2a5222013-07-02 15:47:24 -070053 if (mGenerated != NULL)
54 free_camera_metadata(mGenerated);
Alex Ray90c0af72013-05-31 16:51:39 -070055
56 pthread_mutex_destroy(&mMutex);
Alex Rayb0be1032013-05-28 15:52:47 -070057}
58
Alex Ray89a82662013-05-28 20:32:48 -070059Metadata::Metadata(uint8_t mode, uint8_t intent)
60 : mHead(NULL),
61 mTail(NULL),
62 mEntryCount(0),
Alex Ray0d2a5222013-07-02 15:47:24 -070063 mDataCount(0),
64 mGenerated(NULL),
65 mDirty(true)
Alex Ray89a82662013-05-28 20:32:48 -070066{
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 Rayb0be1032013-05-28 15:52:47 -070088int 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
94int 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
100int 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
106int 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
112int 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
118int 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
125bool 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
147int 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 Ray89a82662013-05-28 20:32:48 -0700158 pthread_mutex_lock(&mMutex);
Alex Rayb0be1032013-05-28 15:52:47 -0700159 mEntryCount++;
160 mDataCount += calculate_camera_metadata_entry_data_size(tag_type, count);
161 push(new Entry(tag, data, count));
Alex Ray0d2a5222013-07-02 15:47:24 -0700162 mDirty = true;
Alex Ray89a82662013-05-28 20:32:48 -0700163 pthread_mutex_unlock(&mMutex);
Alex Rayb0be1032013-05-28 15:52:47 -0700164 return 0;
165}
166
167camera_metadata_t* Metadata::generate()
168{
Alex Rayb0be1032013-05-28 15:52:47 -0700169 pthread_mutex_lock(&mMutex);
Alex Ray0d2a5222013-07-02 15:47:24 -0700170 // 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 Rayb0be1032013-05-28 15:52:47 -0700185 if (mGenerated == NULL) {
Alex Ray0d2a5222013-07-02 15:47:24 -0700186 ALOGE("%s: Failed to allocate metadata (%d entries %d data)",
187 __func__, mEntryCount, mDataCount);
188 goto out;
Alex Rayb0be1032013-05-28 15:52:47 -0700189 }
190 // Walk list of entries adding each one to newly allocated metadata
Alex Ray9cd49c92013-07-09 12:19:49 -0700191 for (Entry *current = mHead; current != NULL; current = current->mNext) {
Alex Ray0d2a5222013-07-02 15:47:24 -0700192 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 Rayb0be1032013-05-28 15:52:47 -0700200 }
Alex Rayb0be1032013-05-28 15:52:47 -0700201
Alex Ray0d2a5222013-07-02 15:47:24 -0700202out:
203 pthread_mutex_unlock(&mMutex);
Alex Rayb0be1032013-05-28 15:52:47 -0700204 return mGenerated;
205}
206
207Metadata::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
216void 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
226Metadata::Entry::~Entry()
227{
228 if (mNext != NULL)
229 mNext->mPrev = mPrev;
230 if (mPrev != NULL)
231 mPrev->mNext = mNext;
232}
233
234void 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