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