1 /*
2  * Copyright (C) 2016 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 #ifndef CAMERA_COMMON_1_0_CAMERAMETADATA_H
18 #define CAMERA_COMMON_1_0_CAMERAMETADATA_H
19 
20 #include "system/camera_metadata.h"
21 
22 #include <utils/String8.h>
23 #include <utils/Vector.h>
24 
25 namespace android {
26 namespace hardware {
27 namespace camera {
28 namespace common {
29 namespace helper {
30 
31 class VendorTagDescriptor;
32 
33 /**
34  * A convenience wrapper around the C-based camera_metadata_t library.
35  */
36 class CameraMetadata {
37   public:
38     /** Creates an empty object; best used when expecting to acquire contents
39      * from elsewhere */
40     CameraMetadata();
41     /** Creates an object with space for entryCapacity entries, with
42      * dataCapacity extra storage */
43     CameraMetadata(size_t entryCapacity, size_t dataCapacity = 10);
44 
45     ~CameraMetadata();
46 
47     /** Takes ownership of passed-in buffer */
48     CameraMetadata(camera_metadata_t* buffer);
49     /** Clones the metadata */
50     CameraMetadata(const CameraMetadata& other);
51 
52     /**
53      * Assignment clones metadata buffer.
54      */
55     CameraMetadata& operator=(const CameraMetadata& other);
56     CameraMetadata& operator=(const camera_metadata_t* buffer);
57 
58     /**
59      * Get reference to the underlying metadata buffer. Ownership remains with
60      * the CameraMetadata object, but non-const CameraMetadata methods will not
61      * work until unlock() is called. Note that the lock has nothing to do with
62      * thread-safety, it simply prevents the camera_metadata_t pointer returned
63      * here from being accidentally invalidated by CameraMetadata operations.
64      */
65     const camera_metadata_t* getAndLock() const;
66 
67     /**
68      * Unlock the CameraMetadata for use again. After this unlock, the pointer
69      * given from getAndLock() may no longer be used. The pointer passed out
70      * from getAndLock must be provided to guarantee that the right object is
71      * being unlocked.
72      */
73     status_t unlock(const camera_metadata_t* buffer) const;
74 
75     /**
76      * Release a raw metadata buffer to the caller. After this call,
77      * CameraMetadata no longer references the buffer, and the caller takes
78      * responsibility for freeing the raw metadata buffer (using
79      * free_camera_metadata()), or for handing it to another CameraMetadata
80      * instance.
81      */
82     camera_metadata_t* release();
83 
84     /**
85      * Clear the metadata buffer and free all storage used by it
86      */
87     void clear();
88 
89     /**
90      * Acquire a raw metadata buffer from the caller. After this call,
91      * the caller no longer owns the raw buffer, and must not free or manipulate it.
92      * If CameraMetadata already contains metadata, it is freed.
93      */
94     void acquire(camera_metadata_t* buffer);
95 
96     /**
97      * Acquires raw buffer from other CameraMetadata object. After the call, the argument
98      * object no longer has any metadata.
99      */
100     void acquire(CameraMetadata& other);
101 
102     /**
103      * Append metadata from another CameraMetadata object.
104      */
105     status_t append(const CameraMetadata& other);
106 
107     /**
108      * Append metadata from a raw camera_metadata buffer
109      */
110     status_t append(const camera_metadata* other);
111 
112     /**
113      * Number of metadata entries.
114      */
115     size_t entryCount() const;
116 
117     /**
118      * Is the buffer empty (no entires)
119      */
120     bool isEmpty() const;
121 
122     /**
123      * Sort metadata buffer for faster find
124      */
125     status_t sort();
126 
127     /**
128      * Update metadata entry. Will create entry if it doesn't exist already, and
129      * will reallocate the buffer if insufficient space exists. Overloaded for
130      * the various types of valid data.
131      */
132     status_t update(uint32_t tag, const uint8_t* data, size_t data_count);
133     status_t update(uint32_t tag, const int32_t* data, size_t data_count);
134     status_t update(uint32_t tag, const float* data, size_t data_count);
135     status_t update(uint32_t tag, const int64_t* data, size_t data_count);
136     status_t update(uint32_t tag, const double* data, size_t data_count);
137     status_t update(uint32_t tag, const camera_metadata_rational_t* data, size_t data_count);
138     status_t update(uint32_t tag, const String8& string);
139     status_t update(const camera_metadata_ro_entry& entry);
140 
141     template <typename T>
update(uint32_t tag,Vector<T> data)142     status_t update(uint32_t tag, Vector<T> data) {
143         return update(tag, data.array(), data.size());
144     }
145 
146     /**
147      * Check if a metadata entry exists for a given tag id
148      *
149      */
150     bool exists(uint32_t tag) const;
151 
152     /**
153      * Get metadata entry by tag id
154      */
155     camera_metadata_entry find(uint32_t tag);
156 
157     /**
158      * Get metadata entry by tag id, with no editing
159      */
160     camera_metadata_ro_entry find(uint32_t tag) const;
161 
162     /**
163      * Delete metadata entry by tag
164      */
165     status_t erase(uint32_t tag);
166 
167     /**
168      * Swap the underlying camera metadata between this and the other
169      * metadata object.
170      */
171     void swap(CameraMetadata& other);
172 
173     /**
174      * Dump contents into FD for debugging. The verbosity levels are
175      * 0: Tag entry information only, no data values
176      * 1: Level 0 plus at most 16 data values per entry
177      * 2: All information
178      *
179      * The indentation parameter sets the number of spaces to add to the start
180      * each line of output.
181      */
182     void dump(int fd, int verbosity = 1, int indentation = 0) const;
183 
184     /**
185      * Find tag id for a given tag name, also checking vendor tags if available.
186      * On success, returns OK and writes the tag id into tag.
187      *
188      * This is a slow method.
189      */
190     static status_t getTagFromName(const char* name, const VendorTagDescriptor* vTags,
191                                    uint32_t* tag);
192 
193   private:
194     camera_metadata_t* mBuffer;
195     mutable bool mLocked;
196 
197     /**
198      * Check if tag has a given type
199      */
200     status_t checkType(uint32_t tag, uint8_t expectedType);
201 
202     /**
203      * Base update entry method
204      */
205     status_t updateImpl(uint32_t tag, const void* data, size_t data_count);
206 
207     /**
208      * Resize metadata buffer if needed by reallocating it and copying it over.
209      */
210     status_t resizeIfNeeded(size_t extraEntries, size_t extraData);
211 };
212 
213 }  // namespace helper
214 
215 // NOTE: Deprecated namespace. This namespace should no longer be used.
216 namespace V1_0::helper {
217 // Export symbols to the old namespace to preserve compatibility
218 typedef android::hardware::camera::common::helper::CameraMetadata CameraMetadata;
219 }  // namespace V1_0::helper
220 
221 }  // namespace common
222 }  // namespace camera
223 }  // namespace hardware
224 }  // namespace android
225 
226 #endif
227