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 
21 #include <sys/types.h>
22 #include <utils/Mutex.h>
23 #include <utils/RefBase.h>
24 #include <utils/Vector.h>
25 #include <camera/CameraMetadata.h>
26 
27 #include "NdkCameraMetadata.h"
28 
29 using namespace android;
30 
31 /**
32  * ACameraMetadata opaque struct definition
33  * Leave outside of android namespace because it's NDK struct
34  */
35 struct ACameraMetadata : public RefBase {
36   public:
37     typedef enum {
38         ACM_CHARACTERISTICS, // Read only
39         ACM_REQUEST,         // Read/Write
40         ACM_RESULT,          // Read only
41     } ACAMERA_METADATA_TYPE;
42 
43     // Takes ownership of pass-in buffer
44     ACameraMetadata(camera_metadata_t *buffer, ACAMERA_METADATA_TYPE type);
45     // Clone
ACameraMetadataACameraMetadata46     ACameraMetadata(const ACameraMetadata& other) :
47             mData(other.mData), mType(other.mType) {};
48 
49     camera_status_t getConstEntry(uint32_t tag, ACameraMetadata_const_entry* entry) const;
50 
51     camera_status_t update(uint32_t tag, uint32_t count, const uint8_t* data);
52     camera_status_t update(uint32_t tag, uint32_t count, const int32_t* data);
53     camera_status_t update(uint32_t tag, uint32_t count, const float* data);
54     camera_status_t update(uint32_t tag, uint32_t count, const double* data);
55     camera_status_t update(uint32_t tag, uint32_t count, const int64_t* data);
56     camera_status_t update(uint32_t tag, uint32_t count, const ACameraMetadata_rational* data);
57 
58     camera_status_t getTags(/*out*/int32_t* numTags,
59                             /*out*/const uint32_t** tags) const;
60 
61     bool isNdkSupportedCapability(const int32_t capability);
62     static inline bool isVendorTag(const uint32_t tag);
63     static bool isCaptureRequestTag(const uint32_t tag);
64     void filterUnsupportedFeatures(); // Hide features not yet supported by NDK
65     void filterStreamConfigurations(); // Hide input streams, translate hal format to NDK formats
66 
67     const CameraMetadata& getInternalData();
68 
69     template<typename INTERNAL_T, typename NDK_T>
updateImplACameraMetadata70     camera_status_t updateImpl(uint32_t tag, uint32_t count, const NDK_T* data) {
71         if (mType != ACM_REQUEST) {
72             ALOGE("Error: Write to metadata is only allowed for capture request!");
73             return ACAMERA_ERROR_INVALID_PARAMETER;
74         }
75         if (!isCaptureRequestTag(tag)) {
76             ALOGE("Error: tag %d is not writable!", tag);
77             return ACAMERA_ERROR_INVALID_PARAMETER;
78         }
79 
80         Mutex::Autolock _l(mLock);
81 
82         status_t ret = OK;
83         if (count == 0 && data == nullptr) {
84             ret = mData.erase(tag);
85         } else {
86             // Here we have to use reinterpret_cast because the NDK data type is
87             // exact copy of internal data type but they do not inherit from each other
88             ret = mData.update(tag, reinterpret_cast<const INTERNAL_T*>(data), count);
89         }
90 
91         if (ret == OK) {
92             mTags.clear();
93             return ACAMERA_OK;
94         } else {
95             return ACAMERA_ERROR_INVALID_PARAMETER;
96         }
97     }
98 
99   private:
100     // guard access of public APIs: get/update/getTags
101     mutable Mutex    mLock;
102     CameraMetadata   mData;
103     mutable Vector<uint32_t> mTags; // updated in getTags, cleared by update
104     const ACAMERA_METADATA_TYPE mType;
105 
106     static std::unordered_set<uint32_t> sSystemTags;
107 };
108 
109 #endif // _ACAMERA_METADATA_H
110