Eino-Ville Talvala | cab96a4 | 2012-08-24 11:29:22 -0700 | [diff] [blame] | 1 | /* |
| 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 Murashkin | 7efa520 | 2013-02-13 15:53:56 -0800 | [diff] [blame] | 17 | #ifndef ANDROID_CLIENT_CAMERA2_CAMERAMETADATA_CPP |
| 18 | #define ANDROID_CLIENT_CAMERA2_CAMERAMETADATA_CPP |
Eino-Ville Talvala | cab96a4 | 2012-08-24 11:29:22 -0700 | [diff] [blame] | 19 | |
| 20 | #include "system/camera_metadata.h" |
Eino-Ville Talvala | 4d45383 | 2016-07-15 11:56:53 -0700 | [diff] [blame] | 21 | |
Eino-Ville Talvala | cab96a4 | 2012-08-24 11:29:22 -0700 | [diff] [blame] | 22 | #include <utils/String8.h> |
| 23 | #include <utils/Vector.h> |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 24 | #include <binder/Parcelable.h> |
Eino-Ville Talvala | cab96a4 | 2012-08-24 11:29:22 -0700 | [diff] [blame] | 25 | |
| 26 | namespace android { |
| 27 | |
Eino-Ville Talvala | 4d45383 | 2016-07-15 11:56:53 -0700 | [diff] [blame] | 28 | class VendorTagDescriptor; |
| 29 | |
Eino-Ville Talvala | cab96a4 | 2012-08-24 11:29:22 -0700 | [diff] [blame] | 30 | /** |
| 31 | * A convenience wrapper around the C-based camera_metadata_t library. |
| 32 | */ |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 33 | class CameraMetadata: public Parcelable { |
Eino-Ville Talvala | cab96a4 | 2012-08-24 11:29:22 -0700 | [diff] [blame] | 34 | public: |
| 35 | /** Creates an empty object; best used when expecting to acquire contents |
| 36 | * from elsewhere */ |
| 37 | CameraMetadata(); |
| 38 | /** Creates an object with space for entryCapacity entries, with |
| 39 | * dataCapacity extra storage */ |
| 40 | CameraMetadata(size_t entryCapacity, size_t dataCapacity = 10); |
| 41 | |
| 42 | ~CameraMetadata(); |
| 43 | |
| 44 | /** Takes ownership of passed-in buffer */ |
| 45 | CameraMetadata(camera_metadata_t *buffer); |
| 46 | /** Clones the metadata */ |
| 47 | CameraMetadata(const CameraMetadata &other); |
| 48 | |
| 49 | /** |
| 50 | * Assignment clones metadata buffer. |
| 51 | */ |
| 52 | CameraMetadata &operator=(const CameraMetadata &other); |
| 53 | CameraMetadata &operator=(const camera_metadata_t *buffer); |
| 54 | |
| 55 | /** |
Eino-Ville Talvala | 3b53bc9 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 56 | * Get reference to the underlying metadata buffer. Ownership remains with |
| 57 | * the CameraMetadata object, but non-const CameraMetadata methods will not |
| 58 | * work until unlock() is called. Note that the lock has nothing to do with |
| 59 | * thread-safety, it simply prevents the camera_metadata_t pointer returned |
| 60 | * here from being accidentally invalidated by CameraMetadata operations. |
| 61 | */ |
Yin-Chia Yeh | 54298b3 | 2015-03-24 16:51:41 -0700 | [diff] [blame] | 62 | const camera_metadata_t* getAndLock() const; |
Eino-Ville Talvala | 3b53bc9 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 63 | |
| 64 | /** |
| 65 | * Unlock the CameraMetadata for use again. After this unlock, the pointer |
| 66 | * given from getAndLock() may no longer be used. The pointer passed out |
| 67 | * from getAndLock must be provided to guarantee that the right object is |
| 68 | * being unlocked. |
| 69 | */ |
Yin-Chia Yeh | 8aac03f | 2016-03-03 15:45:23 -0800 | [diff] [blame] | 70 | status_t unlock(const camera_metadata_t *buffer) const; |
Eino-Ville Talvala | 3b53bc9 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 71 | |
| 72 | /** |
Eino-Ville Talvala | cab96a4 | 2012-08-24 11:29:22 -0700 | [diff] [blame] | 73 | * Release a raw metadata buffer to the caller. After this call, |
| 74 | * CameraMetadata no longer references the buffer, and the caller takes |
| 75 | * responsibility for freeing the raw metadata buffer (using |
| 76 | * free_camera_metadata()), or for handing it to another CameraMetadata |
| 77 | * instance. |
| 78 | */ |
| 79 | camera_metadata_t* release(); |
| 80 | |
| 81 | /** |
| 82 | * Clear the metadata buffer and free all storage used by it |
| 83 | */ |
| 84 | void clear(); |
| 85 | |
| 86 | /** |
| 87 | * Acquire a raw metadata buffer from the caller. After this call, |
| 88 | * the caller no longer owns the raw buffer, and must not free or manipulate it. |
| 89 | * If CameraMetadata already contains metadata, it is freed. |
| 90 | */ |
| 91 | void acquire(camera_metadata_t* buffer); |
| 92 | |
| 93 | /** |
| 94 | * Acquires raw buffer from other CameraMetadata object. After the call, the argument |
| 95 | * object no longer has any metadata. |
| 96 | */ |
| 97 | void acquire(CameraMetadata &other); |
| 98 | |
| 99 | /** |
| 100 | * Append metadata from another CameraMetadata object. |
| 101 | */ |
| 102 | status_t append(const CameraMetadata &other); |
| 103 | |
| 104 | /** |
Eino-Ville Talvala | fd6ecdd | 2013-10-11 09:51:09 -0700 | [diff] [blame] | 105 | * Append metadata from a raw camera_metadata buffer |
| 106 | */ |
| 107 | status_t append(const camera_metadata* other); |
| 108 | |
| 109 | /** |
Eino-Ville Talvala | cab96a4 | 2012-08-24 11:29:22 -0700 | [diff] [blame] | 110 | * Number of metadata entries. |
| 111 | */ |
| 112 | size_t entryCount() const; |
| 113 | |
| 114 | /** |
Eino-Ville Talvala | da6665c | 2012-08-29 17:37:16 -0700 | [diff] [blame] | 115 | * Is the buffer empty (no entires) |
| 116 | */ |
| 117 | bool isEmpty() const; |
| 118 | |
| 119 | /** |
Eino-Ville Talvala | cab96a4 | 2012-08-24 11:29:22 -0700 | [diff] [blame] | 120 | * Sort metadata buffer for faster find |
| 121 | */ |
| 122 | status_t sort(); |
| 123 | |
| 124 | /** |
| 125 | * Update metadata entry. Will create entry if it doesn't exist already, and |
| 126 | * will reallocate the buffer if insufficient space exists. Overloaded for |
| 127 | * the various types of valid data. |
| 128 | */ |
| 129 | status_t update(uint32_t tag, |
| 130 | const uint8_t *data, size_t data_count); |
| 131 | status_t update(uint32_t tag, |
| 132 | const int32_t *data, size_t data_count); |
| 133 | status_t update(uint32_t tag, |
| 134 | const float *data, size_t data_count); |
| 135 | status_t update(uint32_t tag, |
| 136 | const int64_t *data, size_t data_count); |
| 137 | status_t update(uint32_t tag, |
| 138 | const double *data, size_t data_count); |
| 139 | status_t update(uint32_t tag, |
| 140 | const camera_metadata_rational_t *data, size_t data_count); |
| 141 | status_t update(uint32_t tag, |
| 142 | const String8 &string); |
Eino-Ville Talvala | 4d45383 | 2016-07-15 11:56:53 -0700 | [diff] [blame] | 143 | status_t update(const camera_metadata_ro_entry &entry); |
| 144 | |
Eino-Ville Talvala | cab96a4 | 2012-08-24 11:29:22 -0700 | [diff] [blame] | 145 | |
| 146 | template<typename T> |
| 147 | status_t update(uint32_t tag, Vector<T> data) { |
| 148 | return update(tag, data.array(), data.size()); |
| 149 | } |
| 150 | |
| 151 | /** |
Igor Murashkin | fc42642a | 2013-02-13 18:23:39 -0800 | [diff] [blame] | 152 | * Check if a metadata entry exists for a given tag id |
| 153 | * |
| 154 | */ |
| 155 | bool exists(uint32_t tag) const; |
| 156 | |
| 157 | /** |
Eino-Ville Talvala | cab96a4 | 2012-08-24 11:29:22 -0700 | [diff] [blame] | 158 | * Get metadata entry by tag id |
| 159 | */ |
| 160 | camera_metadata_entry find(uint32_t tag); |
| 161 | |
| 162 | /** |
| 163 | * Get metadata entry by tag id, with no editing |
| 164 | */ |
| 165 | camera_metadata_ro_entry find(uint32_t tag) const; |
| 166 | |
| 167 | /** |
| 168 | * Delete metadata entry by tag |
| 169 | */ |
| 170 | status_t erase(uint32_t tag); |
| 171 | |
| 172 | /** |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 173 | * Swap the underlying camera metadata between this and the other |
| 174 | * metadata object. |
| 175 | */ |
| 176 | void swap(CameraMetadata &other); |
| 177 | |
| 178 | /** |
Eino-Ville Talvala | cab96a4 | 2012-08-24 11:29:22 -0700 | [diff] [blame] | 179 | * Dump contents into FD for debugging. The verbosity levels are |
| 180 | * 0: Tag entry information only, no data values |
| 181 | * 1: Level 0 plus at most 16 data values per entry |
| 182 | * 2: All information |
| 183 | * |
| 184 | * The indentation parameter sets the number of spaces to add to the start |
| 185 | * each line of output. |
| 186 | */ |
| 187 | void dump(int fd, int verbosity = 1, int indentation = 0) const; |
| 188 | |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 189 | /** |
| 190 | * Serialization over Binder |
| 191 | */ |
| 192 | |
| 193 | // Metadata object is unchanged when reading from parcel fails. |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 194 | virtual status_t readFromParcel(const Parcel *parcel) override; |
| 195 | virtual status_t writeToParcel(Parcel *parcel) const override; |
Igor Murashkin | e7ee763 | 2013-06-11 18:10:18 -0700 | [diff] [blame] | 196 | |
| 197 | /** |
| 198 | * Caller becomes the owner of the new metadata |
| 199 | * 'const Parcel' doesnt prevent us from calling the read functions. |
| 200 | * which is interesting since it changes the internal state |
| 201 | * |
| 202 | * NULL can be returned when no metadata was sent, OR if there was an issue |
| 203 | * unpacking the serialized data (i.e. bad parcel or invalid structure). |
| 204 | */ |
| 205 | static status_t readFromParcel(const Parcel &parcel, |
| 206 | camera_metadata_t** out); |
| 207 | /** |
| 208 | * Caller retains ownership of metadata |
| 209 | * - Write 2 (int32 + blob) args in the current position |
| 210 | */ |
| 211 | static status_t writeToParcel(Parcel &parcel, |
| 212 | const camera_metadata_t* metadata); |
| 213 | |
Eino-Ville Talvala | 4d45383 | 2016-07-15 11:56:53 -0700 | [diff] [blame] | 214 | /** |
| 215 | * Find tag id for a given tag name, also checking vendor tags if available. |
| 216 | * On success, returns OK and writes the tag id into tag. |
| 217 | * |
| 218 | * This is a slow method. |
| 219 | */ |
| 220 | static status_t getTagFromName(const char *name, |
| 221 | const VendorTagDescriptor* vTags, uint32_t *tag); |
| 222 | |
Eino-Ville Talvala | cab96a4 | 2012-08-24 11:29:22 -0700 | [diff] [blame] | 223 | private: |
| 224 | camera_metadata_t *mBuffer; |
Yin-Chia Yeh | 54298b3 | 2015-03-24 16:51:41 -0700 | [diff] [blame] | 225 | mutable bool mLocked; |
Eino-Ville Talvala | cab96a4 | 2012-08-24 11:29:22 -0700 | [diff] [blame] | 226 | |
| 227 | /** |
| 228 | * Check if tag has a given type |
| 229 | */ |
| 230 | status_t checkType(uint32_t tag, uint8_t expectedType); |
| 231 | |
| 232 | /** |
| 233 | * Base update entry method |
| 234 | */ |
Eino-Ville Talvala | 3b53bc9 | 2013-02-27 18:02:26 -0800 | [diff] [blame] | 235 | status_t updateImpl(uint32_t tag, const void *data, size_t data_count); |
Eino-Ville Talvala | cab96a4 | 2012-08-24 11:29:22 -0700 | [diff] [blame] | 236 | |
| 237 | /** |
| 238 | * Resize metadata buffer if needed by reallocating it and copying it over. |
| 239 | */ |
| 240 | status_t resizeIfNeeded(size_t extraEntries, size_t extraData); |
| 241 | |
| 242 | }; |
| 243 | |
Eino-Ville Talvala | d56db1d | 2015-12-17 16:50:35 -0800 | [diff] [blame] | 244 | namespace hardware { |
| 245 | namespace camera2 { |
| 246 | namespace impl { |
| 247 | using ::android::CameraMetadata; |
| 248 | typedef CameraMetadata CameraMetadataNative; |
| 249 | } |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | } // namespace android |
Eino-Ville Talvala | cab96a4 | 2012-08-24 11:29:22 -0700 | [diff] [blame] | 254 | |
| 255 | #endif |