blob: 9d1b5c7c046ee4d701297a6c3c61cdd54f4c38ae [file] [log] [blame]
Eino-Ville Talvalacab96a42012-08-24 11:29:22 -07001/*
2 * Copyright (C) 2012 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
Igor Murashkin7efa5202013-02-13 15:53:56 -080017#ifndef ANDROID_CLIENT_CAMERA2_CAMERAMETADATA_CPP
18#define ANDROID_CLIENT_CAMERA2_CAMERAMETADATA_CPP
Eino-Ville Talvalacab96a42012-08-24 11:29:22 -070019
20#include "system/camera_metadata.h"
Eino-Ville Talvala4d453832016-07-15 11:56:53 -070021
Eino-Ville Talvalacab96a42012-08-24 11:29:22 -070022#include <utils/String8.h>
23#include <utils/Vector.h>
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080024#include <binder/Parcelable.h>
Emilian Peeve20c6372018-08-14 18:45:53 +010025#include <camera/VendorTagDescriptor.h>
Eino-Ville Talvalacab96a42012-08-24 11:29:22 -070026
27namespace android {
28
Eino-Ville Talvala4d453832016-07-15 11:56:53 -070029class VendorTagDescriptor;
30
Eino-Ville Talvalacab96a42012-08-24 11:29:22 -070031/**
32 * A convenience wrapper around the C-based camera_metadata_t library.
33 */
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080034class CameraMetadata: public Parcelable {
Eino-Ville Talvalacab96a42012-08-24 11:29:22 -070035 public:
36 /** Creates an empty object; best used when expecting to acquire contents
37 * from elsewhere */
38 CameraMetadata();
39 /** Creates an object with space for entryCapacity entries, with
40 * dataCapacity extra storage */
41 CameraMetadata(size_t entryCapacity, size_t dataCapacity = 10);
42
Jayant Chowdhary8a0be292020-01-08 13:10:38 -080043 /**
44 * Move constructor, acquires other's metadata buffer
45 */
46 CameraMetadata(CameraMetadata &&other);
47
Eino-Ville Talvalacab96a42012-08-24 11:29:22 -070048 ~CameraMetadata();
49
50 /** Takes ownership of passed-in buffer */
51 CameraMetadata(camera_metadata_t *buffer);
52 /** Clones the metadata */
53 CameraMetadata(const CameraMetadata &other);
54
55 /**
56 * Assignment clones metadata buffer.
57 */
58 CameraMetadata &operator=(const CameraMetadata &other);
59 CameraMetadata &operator=(const camera_metadata_t *buffer);
60
61 /**
Jayant Chowdhary8a0be292020-01-08 13:10:38 -080062 * Move assignment operator, acquires other's metadata buffer
63 */
64 CameraMetadata &operator=(CameraMetadata &&other);
65
66 /**
Eino-Ville Talvala3b53bc92013-02-27 18:02:26 -080067 * Get reference to the underlying metadata buffer. Ownership remains with
68 * the CameraMetadata object, but non-const CameraMetadata methods will not
69 * work until unlock() is called. Note that the lock has nothing to do with
70 * thread-safety, it simply prevents the camera_metadata_t pointer returned
71 * here from being accidentally invalidated by CameraMetadata operations.
72 */
Yin-Chia Yeh54298b32015-03-24 16:51:41 -070073 const camera_metadata_t* getAndLock() const;
Eino-Ville Talvala3b53bc92013-02-27 18:02:26 -080074
75 /**
76 * Unlock the CameraMetadata for use again. After this unlock, the pointer
77 * given from getAndLock() may no longer be used. The pointer passed out
78 * from getAndLock must be provided to guarantee that the right object is
79 * being unlocked.
80 */
Yin-Chia Yeh8aac03f2016-03-03 15:45:23 -080081 status_t unlock(const camera_metadata_t *buffer) const;
Eino-Ville Talvala3b53bc92013-02-27 18:02:26 -080082
83 /**
Eino-Ville Talvalacab96a42012-08-24 11:29:22 -070084 * Release a raw metadata buffer to the caller. After this call,
85 * CameraMetadata no longer references the buffer, and the caller takes
86 * responsibility for freeing the raw metadata buffer (using
87 * free_camera_metadata()), or for handing it to another CameraMetadata
88 * instance.
89 */
90 camera_metadata_t* release();
91
92 /**
93 * Clear the metadata buffer and free all storage used by it
94 */
95 void clear();
96
97 /**
98 * Acquire a raw metadata buffer from the caller. After this call,
99 * the caller no longer owns the raw buffer, and must not free or manipulate it.
100 * If CameraMetadata already contains metadata, it is freed.
101 */
102 void acquire(camera_metadata_t* buffer);
103
104 /**
105 * Acquires raw buffer from other CameraMetadata object. After the call, the argument
106 * object no longer has any metadata.
107 */
108 void acquire(CameraMetadata &other);
109
110 /**
111 * Append metadata from another CameraMetadata object.
112 */
113 status_t append(const CameraMetadata &other);
114
115 /**
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -0700116 * Append metadata from a raw camera_metadata buffer
117 */
118 status_t append(const camera_metadata* other);
119
120 /**
Eino-Ville Talvalacab96a42012-08-24 11:29:22 -0700121 * Number of metadata entries.
122 */
123 size_t entryCount() const;
124
125 /**
Eino-Ville Talvalada6665c2012-08-29 17:37:16 -0700126 * Is the buffer empty (no entires)
127 */
128 bool isEmpty() const;
129
130 /**
Eino-Ville Talvalacab96a42012-08-24 11:29:22 -0700131 * Sort metadata buffer for faster find
132 */
133 status_t sort();
134
135 /**
136 * Update metadata entry. Will create entry if it doesn't exist already, and
137 * will reallocate the buffer if insufficient space exists. Overloaded for
138 * the various types of valid data.
139 */
140 status_t update(uint32_t tag,
141 const uint8_t *data, size_t data_count);
142 status_t update(uint32_t tag,
143 const int32_t *data, size_t data_count);
144 status_t update(uint32_t tag,
145 const float *data, size_t data_count);
146 status_t update(uint32_t tag,
147 const int64_t *data, size_t data_count);
148 status_t update(uint32_t tag,
149 const double *data, size_t data_count);
150 status_t update(uint32_t tag,
151 const camera_metadata_rational_t *data, size_t data_count);
152 status_t update(uint32_t tag,
153 const String8 &string);
Eino-Ville Talvala4d453832016-07-15 11:56:53 -0700154 status_t update(const camera_metadata_ro_entry &entry);
155
Eino-Ville Talvalacab96a42012-08-24 11:29:22 -0700156
157 template<typename T>
158 status_t update(uint32_t tag, Vector<T> data) {
159 return update(tag, data.array(), data.size());
160 }
161
162 /**
Igor Murashkinfc42642a2013-02-13 18:23:39 -0800163 * Check if a metadata entry exists for a given tag id
164 *
165 */
166 bool exists(uint32_t tag) const;
167
168 /**
Eino-Ville Talvalacab96a42012-08-24 11:29:22 -0700169 * Get metadata entry by tag id
170 */
171 camera_metadata_entry find(uint32_t tag);
172
173 /**
174 * Get metadata entry by tag id, with no editing
175 */
176 camera_metadata_ro_entry find(uint32_t tag) const;
177
178 /**
179 * Delete metadata entry by tag
180 */
181 status_t erase(uint32_t tag);
182
183 /**
Emilian Peeve20c6372018-08-14 18:45:53 +0100184 * Remove metadata entries that need additional permissions.
185 */
186 status_t removePermissionEntries(metadata_vendor_id_t vendorId,
187 std::vector<int32_t> *tagsRemoved /*out*/);
188
189 /**
Igor Murashkine7ee7632013-06-11 18:10:18 -0700190 * Swap the underlying camera metadata between this and the other
191 * metadata object.
192 */
193 void swap(CameraMetadata &other);
194
195 /**
Eino-Ville Talvalacab96a42012-08-24 11:29:22 -0700196 * Dump contents into FD for debugging. The verbosity levels are
197 * 0: Tag entry information only, no data values
198 * 1: Level 0 plus at most 16 data values per entry
199 * 2: All information
200 *
201 * The indentation parameter sets the number of spaces to add to the start
202 * each line of output.
203 */
204 void dump(int fd, int verbosity = 1, int indentation = 0) const;
205
Igor Murashkine7ee7632013-06-11 18:10:18 -0700206 /**
207 * Serialization over Binder
208 */
209
210 // Metadata object is unchanged when reading from parcel fails.
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800211 virtual status_t readFromParcel(const Parcel *parcel) override;
212 virtual status_t writeToParcel(Parcel *parcel) const override;
Igor Murashkine7ee7632013-06-11 18:10:18 -0700213
214 /**
215 * Caller becomes the owner of the new metadata
216 * 'const Parcel' doesnt prevent us from calling the read functions.
217 * which is interesting since it changes the internal state
218 *
219 * NULL can be returned when no metadata was sent, OR if there was an issue
220 * unpacking the serialized data (i.e. bad parcel or invalid structure).
221 */
222 static status_t readFromParcel(const Parcel &parcel,
223 camera_metadata_t** out);
224 /**
225 * Caller retains ownership of metadata
226 * - Write 2 (int32 + blob) args in the current position
227 */
228 static status_t writeToParcel(Parcel &parcel,
229 const camera_metadata_t* metadata);
230
Eino-Ville Talvala4d453832016-07-15 11:56:53 -0700231 /**
232 * Find tag id for a given tag name, also checking vendor tags if available.
233 * On success, returns OK and writes the tag id into tag.
234 *
235 * This is a slow method.
236 */
237 static status_t getTagFromName(const char *name,
238 const VendorTagDescriptor* vTags, uint32_t *tag);
239
Eino-Ville Talvalacab96a42012-08-24 11:29:22 -0700240 private:
241 camera_metadata_t *mBuffer;
Yin-Chia Yeh54298b32015-03-24 16:51:41 -0700242 mutable bool mLocked;
Eino-Ville Talvalacab96a42012-08-24 11:29:22 -0700243
244 /**
245 * Check if tag has a given type
246 */
247 status_t checkType(uint32_t tag, uint8_t expectedType);
248
249 /**
250 * Base update entry method
251 */
Eino-Ville Talvala3b53bc92013-02-27 18:02:26 -0800252 status_t updateImpl(uint32_t tag, const void *data, size_t data_count);
Eino-Ville Talvalacab96a42012-08-24 11:29:22 -0700253
254 /**
255 * Resize metadata buffer if needed by reallocating it and copying it over.
256 */
257 status_t resizeIfNeeded(size_t extraEntries, size_t extraData);
258
259};
260
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800261namespace hardware {
262namespace camera2 {
263namespace impl {
264using ::android::CameraMetadata;
265typedef CameraMetadata CameraMetadataNative;
266}
267}
268}
269
270} // namespace android
Eino-Ville Talvalacab96a42012-08-24 11:29:22 -0700271
272#endif