1 /*
2  * Copyright (C) 2015 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 #ifndef _ACAMERA_METADATA_H
17 #define _ACAMERA_METADATA_H
18 
19 #include <unordered_set>
20 #include <vector>
21 
22 #include <sys/types.h>
23 #include <utils/Mutex.h>
24 #include <utils/RefBase.h>
25 #include <utils/Vector.h>
26 
27 #ifdef __ANDROID_VNDK__
28 #include <CameraMetadata.h>
29 using CameraMetadata = android::hardware::camera::common::V1_0::helper::CameraMetadata;
30 #else
31 #include <camera/CameraMetadata.h>
32 #endif
33 
34 #include <camera/NdkCameraMetadata.h>
35 
36 using namespace android;
37 
38 /**
39  * ACameraMetadata opaque struct definition
40  * Leave outside of android namespace because it's NDK struct
41  */
42 struct ACameraMetadata : public RefBase {
43   public:
44     typedef enum {
45         ACM_CHARACTERISTICS, // Read only
46         ACM_REQUEST,         // Read/Write
47         ACM_RESULT,          // Read only
48     } ACAMERA_METADATA_TYPE;
49 
50     // Takes ownership of pass-in buffer
51     ACameraMetadata(camera_metadata_t *buffer, ACAMERA_METADATA_TYPE type);
52     // Clone
ACameraMetadataACameraMetadata53     ACameraMetadata(const ACameraMetadata& other) :
54             mData(other.mData), mType(other.mType) {};
55 
56     camera_status_t getConstEntry(uint32_t tag, ACameraMetadata_const_entry* entry) const;
57 
58     camera_status_t update(uint32_t tag, uint32_t count, const uint8_t* data);
59     camera_status_t update(uint32_t tag, uint32_t count, const int32_t* data);
60     camera_status_t update(uint32_t tag, uint32_t count, const float* data);
61     camera_status_t update(uint32_t tag, uint32_t count, const double* data);
62     camera_status_t update(uint32_t tag, uint32_t count, const int64_t* data);
63     camera_status_t update(uint32_t tag, uint32_t count, const ACameraMetadata_rational* data);
64 
65     camera_status_t getTags(/*out*/int32_t* numTags,
66                             /*out*/const uint32_t** tags) const;
67 
68     const CameraMetadata& getInternalData() const;
69     bool isLogicalMultiCamera(size_t* count, const char* const** physicalCameraIds) const;
70 
71   private:
72 
73     // This function does not check whether the capability passed to it is valid.
74     // The caller must make sure that it is.
75     bool isNdkSupportedCapability(const int32_t capability);
76     static inline bool isVendorTag(const uint32_t tag);
77     static bool isCaptureRequestTag(const uint32_t tag);
78     void filterUnsupportedFeatures(); // Hide features not yet supported by NDK
79     void filterStreamConfigurations(); // Hide input streams, translate hal format to NDK formats
80     void filterDurations(uint32_t tag); // translate hal format to NDK formats
81     void derivePhysicalCameraIds(); // Derive array of physical ids.
82 
83     template<typename INTERNAL_T, typename NDK_T>
updateImplACameraMetadata84     camera_status_t updateImpl(uint32_t tag, uint32_t count, const NDK_T* data) {
85         if (mType != ACM_REQUEST) {
86             ALOGE("Error: Write to metadata is only allowed for capture request!");
87             return ACAMERA_ERROR_INVALID_PARAMETER;
88         }
89         if (!isCaptureRequestTag(tag)) {
90             ALOGE("Error: tag %d is not writable!", tag);
91             return ACAMERA_ERROR_INVALID_PARAMETER;
92         }
93 
94         Mutex::Autolock _l(mLock);
95 
96         status_t ret = OK;
97         if (count == 0 && data == nullptr) {
98             ret = mData.erase(tag);
99         } else {
100             // Here we have to use reinterpret_cast because the NDK data type is
101             // exact copy of internal data type but they do not inherit from each other
102             ret = mData.update(tag, reinterpret_cast<const INTERNAL_T*>(data), count);
103         }
104 
105         if (ret == OK) {
106             mTags.clear();
107             return ACAMERA_OK;
108         } else {
109             return ACAMERA_ERROR_INVALID_PARAMETER;
110         }
111     }
112 
113     // guard access of public APIs: get/update/getTags
114     mutable Mutex    mLock;
115     CameraMetadata   mData;
116     mutable Vector<uint32_t> mTags; // updated in getTags, cleared by update
117     const ACAMERA_METADATA_TYPE mType;
118 
119     static std::unordered_set<uint32_t> sSystemTags;
120 
121     std::vector<const char*> mStaticPhysicalCameraIds;
122     std::vector<String8> mStaticPhysicalCameraIdValues;
123 };
124 
125 #endif // _ACAMERA_METADATA_H
126