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