• 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 
17 //#define LOG_NDEBUG 0
18 #define LOG_TAG "NdkCameraMetadata"
19 #define ATRACE_TAG ATRACE_TAG_CAMERA
20 
21 #include <utils/Log.h>
22 #include <utils/Trace.h>
23 
24 #include <camera/NdkCameraMetadata.h>
25 #include "impl/ACameraMetadata.h"
26 
27 using namespace android;
28 
29 #ifndef __ANDROID_VNDK__
30 namespace {
31 
32 constexpr const char* android_hardware_camera2_CameraMetadata_jniClassName =
33     "android/hardware/camera2/CameraMetadata";
34 constexpr const char* android_hardware_camera2_CameraCharacteristics_jniClassName =
35     "android/hardware/camera2/CameraCharacteristics";
36 constexpr const char* android_hardware_camera2_CaptureResult_jniClassName =
37     "android/hardware/camera2/CaptureResult";
38 
39 jclass android_hardware_camera2_CameraCharacteristics_clazz = nullptr;
40 jclass android_hardware_camera2_CaptureResult_clazz = nullptr;
41 jmethodID android_hardware_camera2_CameraMetadata_getNativeMetadataPtr = nullptr;
42 
43 // Called at most once to initializes global variables used by JNI.
InitJni(JNIEnv * env)44 bool InitJni(JNIEnv* env) {
45     // From C++11 onward, static initializers are guaranteed to be executed at most once,
46     // even if called from multiple threads.
47     static bool ok = [env]() -> bool {
48         const jclass cameraMetadataClazz = env->FindClass(
49             android_hardware_camera2_CameraMetadata_jniClassName);
50         if (cameraMetadataClazz == nullptr) {
51             return false;
52         }
53         const jmethodID cameraMetadata_getNativeMetadataPtr =
54             env->GetMethodID(cameraMetadataClazz, "getNativeMetadataPtr", "()J");
55         if (cameraMetadata_getNativeMetadataPtr == nullptr) {
56             return false;
57         }
58 
59         const jclass cameraCharacteristics_clazz = env->FindClass(
60             android_hardware_camera2_CameraCharacteristics_jniClassName);
61         if (cameraCharacteristics_clazz == nullptr) {
62             return false;
63         }
64 
65         const jclass captureResult_clazz = env->FindClass(
66             android_hardware_camera2_CaptureResult_jniClassName);
67         if (captureResult_clazz == nullptr) {
68             return false;
69         }
70 
71         android_hardware_camera2_CameraMetadata_getNativeMetadataPtr =
72             cameraMetadata_getNativeMetadataPtr;
73         android_hardware_camera2_CameraCharacteristics_clazz =
74             static_cast<jclass>(env->NewGlobalRef(cameraCharacteristics_clazz));
75         android_hardware_camera2_CaptureResult_clazz =
76             static_cast<jclass>(env->NewGlobalRef(captureResult_clazz));
77 
78         return true;
79     }();
80     return ok;
81 }
82 
83 // Given cameraMetadata, an instance of android.hardware.camera2.CameraMetadata, invokes
84 // cameraMetadata.getNativeMetadataPtr() and returns it as a std::shared_ptr<CameraMetadata>*.
CameraMetadata_getNativeMetadataPtr(JNIEnv * env,jobject cameraMetadata)85 std::shared_ptr<CameraMetadata>* CameraMetadata_getNativeMetadataPtr(JNIEnv* env,
86         jobject cameraMetadata) {
87     if (cameraMetadata == nullptr) {
88         ALOGE("%s: Invalid Java CameraMetadata object.", __FUNCTION__);
89         return nullptr;
90     }
91     jlong ret = env->CallLongMethod(cameraMetadata,
92                                     android_hardware_camera2_CameraMetadata_getNativeMetadataPtr);
93     return reinterpret_cast<std::shared_ptr<CameraMetadata>* >(ret);
94 }
95 
96 }  // namespace
97 #endif  /* __ANDROID_VNDK__ */
98 
99 EXPORT
ACameraMetadata_getConstEntry(const ACameraMetadata * acm,uint32_t tag,ACameraMetadata_const_entry * entry)100 camera_status_t ACameraMetadata_getConstEntry(
101         const ACameraMetadata* acm, uint32_t tag, ACameraMetadata_const_entry* entry) {
102     ATRACE_CALL();
103     if (acm == nullptr || entry == nullptr) {
104         ALOGE("%s: invalid argument! metadata %p, tag 0x%x, entry %p",
105                __FUNCTION__, acm, tag, entry);
106         return ACAMERA_ERROR_INVALID_PARAMETER;
107     }
108     return acm->getConstEntry(tag, entry);
109 }
110 
111 EXPORT
ACameraMetadata_getAllTags(const ACameraMetadata * acm,int32_t * numTags,const uint32_t ** tags)112 camera_status_t ACameraMetadata_getAllTags(
113         const ACameraMetadata* acm, /*out*/int32_t* numTags, /*out*/const uint32_t** tags) {
114     ATRACE_CALL();
115     if (acm == nullptr || numTags == nullptr || tags == nullptr) {
116         ALOGE("%s: invalid argument! metadata %p, numTags %p, tags %p",
117                __FUNCTION__, acm, numTags, tags);
118         return ACAMERA_ERROR_INVALID_PARAMETER;
119     }
120     return acm->getTags(numTags, tags);
121 }
122 
123 EXPORT
ACameraMetadata_getTagFromName(const ACameraMetadata * acm,const char * name,uint32_t * tag)124 camera_status_t ACameraMetadata_getTagFromName(
125         const ACameraMetadata* acm, const char* name, uint32_t* tag) {
126     ATRACE_CALL();
127     if (acm == nullptr || name == nullptr || tag == nullptr) {
128         ALOGE("%s: invalid argument! metadata %p, name %p, tag %p",
129                __FUNCTION__, acm, name, tag);
130         return ACAMERA_ERROR_INVALID_PARAMETER;
131     }
132     return acm->getTagFromName(name, tag);
133 }
134 
135 EXPORT
ACameraMetadata_copy(const ACameraMetadata * src)136 ACameraMetadata* ACameraMetadata_copy(const ACameraMetadata* src) {
137     ATRACE_CALL();
138     if (src == nullptr) {
139         ALOGE("%s: src is null!", __FUNCTION__);
140         return nullptr;
141     }
142     ACameraMetadata* copy = new ACameraMetadata(*src);
143     copy->incStrong(/*id=*/(void*) ACameraMetadata_copy);
144     return copy;
145 }
146 
147 EXPORT
ACameraMetadata_free(ACameraMetadata * metadata)148 void ACameraMetadata_free(ACameraMetadata* metadata) {
149     ATRACE_CALL();
150     if (metadata != nullptr) {
151         metadata->decStrong((void*) ACameraMetadata_free);
152     }
153 }
154 
155 EXPORT
ACameraMetadata_isLogicalMultiCamera(const ACameraMetadata * staticMetadata,size_t * numPhysicalCameras,const char * const ** physicalCameraIds)156 bool ACameraMetadata_isLogicalMultiCamera(const ACameraMetadata* staticMetadata,
157         /*out*/size_t* numPhysicalCameras, /*out*/const char*const** physicalCameraIds) {
158     ATRACE_CALL();
159     if (numPhysicalCameras == nullptr || physicalCameraIds == nullptr) {
160         ALOGE("%s: Invalid input: numPhysicalCameras %p, physicalCameraIds %p",
161                  __FUNCTION__, numPhysicalCameras, physicalCameraIds);
162         return false;
163     }
164     if (staticMetadata == nullptr) {
165         ALOGE("%s: Invalid input: staticMetadata is null.", __FUNCTION__);
166         return false;
167     }
168 
169     return staticMetadata->isLogicalMultiCamera(numPhysicalCameras, physicalCameraIds);
170 }
171 
172 #ifndef __ANDROID_VNDK__
173 EXPORT
ACameraMetadata_fromCameraMetadata(JNIEnv * env,jobject cameraMetadata)174 ACameraMetadata* ACameraMetadata_fromCameraMetadata(JNIEnv* env, jobject cameraMetadata) {
175     ATRACE_CALL();
176 
177     const bool ok = InitJni(env);
178     LOG_ALWAYS_FATAL_IF(!ok, "Failed to find CameraMetadata Java classes.");
179 
180     if (cameraMetadata == nullptr) {
181         return nullptr;
182     }
183 
184     ACameraMetadata::ACAMERA_METADATA_TYPE type;
185     if (env->IsInstanceOf(cameraMetadata,
186         android_hardware_camera2_CameraCharacteristics_clazz)) {
187         type = ACameraMetadata::ACM_CHARACTERISTICS;
188     } else if (env->IsInstanceOf(cameraMetadata,
189         android_hardware_camera2_CaptureResult_clazz)) {
190         type = ACameraMetadata::ACM_RESULT;
191     } else {
192         return nullptr;
193     }
194 
195     auto sharedData = CameraMetadata_getNativeMetadataPtr(env, cameraMetadata);
196     ACameraMetadata* output = new ACameraMetadata(*sharedData, type);
197     output->incStrong(/*id=*/(void*) ACameraMetadata_fromCameraMetadata);
198     return output;
199 }
200 #endif  /* __ANDROID_VNDK__ */
201