blob: a57b2ffa49ffa0f1007b422017297e6063203948 [file] [log] [blame]
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -08001/*
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
Yin-Chia Yeh8aac03f2016-03-03 15:45:23 -080019#include <unordered_set>
Shuzhen Wang0ff9ae32018-12-05 18:06:12 -080020#include <vector>
Yin-Chia Yeh8aac03f2016-03-03 15:45:23 -080021
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -080022#include <sys/types.h>
Yin-Chia Yeh8aac03f2016-03-03 15:45:23 -080023#include <utils/Mutex.h>
Yin-Chia Yehead91462016-01-06 16:45:08 -080024#include <utils/RefBase.h>
Yin-Chia Yeh8aac03f2016-03-03 15:45:23 -080025#include <utils/Vector.h>
Jayant Chowdhary6df26072018-11-06 23:55:12 -080026
27#ifdef __ANDROID_VNDK__
28#include <CameraMetadata.h>
29using CameraMetadata = android::hardware::camera::common::V1_0::helper::CameraMetadata;
30#else
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -080031#include <camera/CameraMetadata.h>
Jayant Chowdhary6df26072018-11-06 23:55:12 -080032#endif
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -080033
Colin Cross7e8d4ba2017-05-04 16:17:42 -070034#include <camera/NdkCameraMetadata.h>
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -080035
36using namespace android;
37
38/**
Jiawen Chen9f713e82020-01-15 11:06:13 -050039 * ACameraMetadata is an opaque struct definition.
40 * It is intentionally left outside of the android namespace because it's NDK struct.
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -080041 */
Yin-Chia Yehead91462016-01-06 16:45:08 -080042struct ACameraMetadata : public RefBase {
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -080043 public:
44 typedef enum {
45 ACM_CHARACTERISTICS, // Read only
46 ACM_REQUEST, // Read/Write
47 ACM_RESULT, // Read only
48 } ACAMERA_METADATA_TYPE;
49
Jiawen Chen9f713e82020-01-15 11:06:13 -050050 // Constructs a ACameraMetadata that takes ownership of `buffer`.
51 ACameraMetadata(camera_metadata_t* buffer, ACAMERA_METADATA_TYPE type);
52
53 // Constructs a ACameraMetadata that is a view of `cameraMetadata`.
54 // `cameraMetadata` will not be deleted by ~ACameraMetadata().
55 ACameraMetadata(CameraMetadata* cameraMetadata, ACAMERA_METADATA_TYPE type);
56
57 // Copy constructor.
58 //
59 // If `other` owns its CameraMetadata, then makes a deep copy.
60 // Otherwise, the new instance is also a view of the same data.
61 ACameraMetadata(const ACameraMetadata& other);
62
63 ~ACameraMetadata();
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -080064
65 camera_status_t getConstEntry(uint32_t tag, ACameraMetadata_const_entry* entry) const;
66
67 camera_status_t update(uint32_t tag, uint32_t count, const uint8_t* data);
68 camera_status_t update(uint32_t tag, uint32_t count, const int32_t* data);
69 camera_status_t update(uint32_t tag, uint32_t count, const float* data);
70 camera_status_t update(uint32_t tag, uint32_t count, const double* data);
71 camera_status_t update(uint32_t tag, uint32_t count, const int64_t* data);
72 camera_status_t update(uint32_t tag, uint32_t count, const ACameraMetadata_rational* data);
73
Yin-Chia Yeh8aac03f2016-03-03 15:45:23 -080074 camera_status_t getTags(/*out*/int32_t* numTags,
75 /*out*/const uint32_t** tags) const;
76
Yin-Chia Yehe57e9a22018-08-23 15:36:41 -070077 const CameraMetadata& getInternalData() const;
Shuzhen Wang0ff9ae32018-12-05 18:06:12 -080078 bool isLogicalMultiCamera(size_t* count, const char* const** physicalCameraIds) const;
Yin-Chia Yehe57e9a22018-08-23 15:36:41 -070079
80 private:
81
Jiawen Chen9f713e82020-01-15 11:06:13 -050082 // Common code called by constructors.
83 void init();
84
Jayant Chowdhary62899df2019-04-02 16:53:34 -070085 // This function does not check whether the capability passed to it is valid.
86 // The caller must make sure that it is.
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -080087 bool isNdkSupportedCapability(const int32_t capability);
Yin-Chia Yeh8aac03f2016-03-03 15:45:23 -080088 static inline bool isVendorTag(const uint32_t tag);
89 static bool isCaptureRequestTag(const uint32_t tag);
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -080090 void filterUnsupportedFeatures(); // Hide features not yet supported by NDK
Yin-Chia Yehc3603822016-01-18 22:11:19 -080091 void filterStreamConfigurations(); // Hide input streams, translate hal format to NDK formats
Yin-Chia Yehe57e9a22018-08-23 15:36:41 -070092 void filterDurations(uint32_t tag); // translate hal format to NDK formats
Shuzhen Wang0ff9ae32018-12-05 18:06:12 -080093 void derivePhysicalCameraIds(); // Derive array of physical ids.
Yin-Chia Yeh8aac03f2016-03-03 15:45:23 -080094
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -080095 template<typename INTERNAL_T, typename NDK_T>
96 camera_status_t updateImpl(uint32_t tag, uint32_t count, const NDK_T* data) {
97 if (mType != ACM_REQUEST) {
98 ALOGE("Error: Write to metadata is only allowed for capture request!");
99 return ACAMERA_ERROR_INVALID_PARAMETER;
100 }
101 if (!isCaptureRequestTag(tag)) {
102 ALOGE("Error: tag %d is not writable!", tag);
103 return ACAMERA_ERROR_INVALID_PARAMETER;
104 }
105
Yin-Chia Yeh8aac03f2016-03-03 15:45:23 -0800106 Mutex::Autolock _l(mLock);
107
Yin-Chia Yeh1d0955c2016-05-16 01:14:13 -0700108 status_t ret = OK;
109 if (count == 0 && data == nullptr) {
Jiawen Chen9f713e82020-01-15 11:06:13 -0500110 ret = mData->erase(tag);
Yin-Chia Yeh1d0955c2016-05-16 01:14:13 -0700111 } else {
112 // Here we have to use reinterpret_cast because the NDK data type is
113 // exact copy of internal data type but they do not inherit from each other
Jiawen Chen9f713e82020-01-15 11:06:13 -0500114 ret = mData->update(tag, reinterpret_cast<const INTERNAL_T*>(data), count);
Yin-Chia Yeh1d0955c2016-05-16 01:14:13 -0700115 }
116
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -0800117 if (ret == OK) {
Yin-Chia Yeh8aac03f2016-03-03 15:45:23 -0800118 mTags.clear();
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -0800119 return ACAMERA_OK;
120 } else {
121 return ACAMERA_ERROR_INVALID_PARAMETER;
122 }
123 }
124
Jiawen Chen9f713e82020-01-15 11:06:13 -0500125 // Guard access of public APIs: get/update/getTags.
126 mutable Mutex mLock;
127
128 CameraMetadata* mData;
129 // If true, has ownership of mData. Otherwise, mData is a view of an external instance.
130 bool mOwnsData;
131
132 mutable Vector<uint32_t> mTags; // Updated by `getTags()`, cleared by `update()`.
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -0800133 const ACAMERA_METADATA_TYPE mType;
Yin-Chia Yeh8aac03f2016-03-03 15:45:23 -0800134
135 static std::unordered_set<uint32_t> sSystemTags;
Shuzhen Wang0ff9ae32018-12-05 18:06:12 -0800136
137 std::vector<const char*> mStaticPhysicalCameraIds;
Emilian Peev1da379a2019-02-05 15:36:31 -0800138 std::vector<String8> mStaticPhysicalCameraIdValues;
Yin-Chia Yeh0dea57f2015-12-09 16:46:07 -0800139};
140
141#endif // _ACAMERA_METADATA_H