• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include <memory>
22 
23 #include <sys/types.h>
24 #include <utils/Mutex.h>
25 #include <utils/RefBase.h>
26 #include <utils/Vector.h>
27 
28 #ifdef __ANDROID_VNDK__
29 #include <CameraMetadata.h>
30 #include <aidl/android/frameworks/cameraservice/common/VendorTag.h>
31 #include <aidl/android/frameworks/cameraservice/common/VendorTagSection.h>
32 #include <aidl/android/frameworks/cameraservice/common/ProviderIdAndVendorTagSections.h>
33 #include <VendorTagDescriptor.h>
34 using CameraMetadata = android::hardware::camera::common::V1_0::helper::CameraMetadata;
35 using ::aidl::android::frameworks::cameraservice::common::ProviderIdAndVendorTagSections;
36 using ::android::hardware::camera::common::V1_0::helper::VendorTagDescriptor;
37 using ::android::hardware::camera::common::V1_0::helper::VendorTagDescriptorCache;
38 #else
39 #include <camera/CameraMetadata.h>
40 #include <camera/VendorTagDescriptor.h>
41 #endif
42 
43 #include <camera/NdkCameraMetadata.h>
44 
45 using namespace android;
46 
47 /**
48  * ACameraMetadata is an opaque struct definition.
49  * It is intentionally left outside of the android namespace because it's NDK struct.
50  */
51 struct ACameraMetadata : public RefBase {
52   public:
53     typedef enum {
54         ACM_CHARACTERISTICS, // Read only
55         ACM_REQUEST,         // Read/Write
56         ACM_RESULT,          // Read only
57     } ACAMERA_METADATA_TYPE;
58 
59     // Constructs a ACameraMetadata that takes ownership of `buffer`.
60     ACameraMetadata(camera_metadata_t* buffer, ACAMERA_METADATA_TYPE type);
61 
62     // Constructs a ACameraMetadata that shares its data with something else, like a Java object
63     ACameraMetadata(const std::shared_ptr<CameraMetadata>& cameraMetadata,
64             ACAMERA_METADATA_TYPE type);
65 
66     // Copy constructor.
67     //
68     // Always makes a deep copy.
69     ACameraMetadata(const ACameraMetadata& other);
70 
71     ~ACameraMetadata();
72 
73     camera_status_t getConstEntry(uint32_t tag, ACameraMetadata_const_entry* entry) const;
74 
75     camera_status_t update(uint32_t tag, uint32_t count, const uint8_t* data);
76     camera_status_t update(uint32_t tag, uint32_t count, const int32_t* data);
77     camera_status_t update(uint32_t tag, uint32_t count, const float* data);
78     camera_status_t update(uint32_t tag, uint32_t count, const double* data);
79     camera_status_t update(uint32_t tag, uint32_t count, const int64_t* data);
80     camera_status_t update(uint32_t tag, uint32_t count, const ACameraMetadata_rational* data);
81 
82     camera_status_t getTags(/*out*/int32_t* numTags,
83                             /*out*/const uint32_t** tags) const;
84     camera_status_t
85     getTagFromName(const char *name, uint32_t *tag) const;
86 
87     const CameraMetadata& getInternalData() const;
88     bool isLogicalMultiCamera(size_t* count, const char* const** physicalCameraIds) const;
89 
90   private:
91 
92     // Common code called by constructors.
93     void init();
94 
95     // This function does not check whether the capability passed to it is valid.
96     // The caller must make sure that it is.
97     bool isNdkSupportedCapability(const int32_t capability);
98     static inline bool isVendorTag(const uint32_t tag);
99     static bool isCaptureRequestTag(const uint32_t tag);
100     void filterUnsupportedFeatures(); // Hide features not yet supported by NDK
101     void filterStreamConfigurations(); // Hide input streams, translate hal format to NDK formats
102     void filterDurations(uint32_t tag); // translate hal format to NDK formats
103     void derivePhysicalCameraIds(); // Derive array of physical ids.
104 
105     template<typename INTERNAL_T, typename NDK_T>
updateImplACameraMetadata106     camera_status_t updateImpl(uint32_t tag, uint32_t count, const NDK_T* data) {
107         if (mType != ACM_REQUEST) {
108             ALOGE("Error: Write to metadata is only allowed for capture request!");
109             return ACAMERA_ERROR_INVALID_PARAMETER;
110         }
111         if (!isCaptureRequestTag(tag)) {
112             ALOGE("Error: tag %d is not writable!", tag);
113             return ACAMERA_ERROR_INVALID_PARAMETER;
114         }
115 
116         Mutex::Autolock _l(mLock);
117 
118         status_t ret = OK;
119         if (count == 0 && data == nullptr) {
120             ret = mData->erase(tag);
121         } else {
122             // Here we have to use reinterpret_cast because the NDK data type is
123             // exact copy of internal data type but they do not inherit from each other
124             ret = mData->update(tag, reinterpret_cast<const INTERNAL_T*>(data), count);
125         }
126 
127         if (ret == OK) {
128             mTags.clear();
129             return ACAMERA_OK;
130         } else {
131             return ACAMERA_ERROR_INVALID_PARAMETER;
132         }
133     }
134 
135     // Guard access of public APIs: get/update/getTags.
136     mutable Mutex mLock;
137 
138     std::shared_ptr<CameraMetadata> mData;
139 
140     mutable Vector<uint32_t> mTags; // Updated by `getTags()`, cleared by `update()`.
141     const ACAMERA_METADATA_TYPE mType;
142 
143     static std::unordered_set<uint32_t> sSystemTags;
144 
145     std::vector<const char*> mStaticPhysicalCameraIds;
146     std::vector<String8> mStaticPhysicalCameraIdValues;
147     sp<VendorTagDescriptor> mVTags = nullptr;
148 };
149 
150 #endif // _ACAMERA_METADATA_H
151