• 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_copy(const ACameraMetadata * src)124 ACameraMetadata* ACameraMetadata_copy(const ACameraMetadata* src) {
125     ATRACE_CALL();
126     if (src == nullptr) {
127         ALOGE("%s: src is null!", __FUNCTION__);
128         return nullptr;
129     }
130     ACameraMetadata* copy = new ACameraMetadata(*src);
131     copy->incStrong(/*id=*/(void*) ACameraMetadata_copy);
132     return copy;
133 }
134 
135 EXPORT
ACameraMetadata_free(ACameraMetadata * metadata)136 void ACameraMetadata_free(ACameraMetadata* metadata) {
137     ATRACE_CALL();
138     if (metadata != nullptr) {
139         metadata->decStrong((void*) ACameraMetadata_free);
140     }
141 }
142 
143 EXPORT
ACameraMetadata_isLogicalMultiCamera(const ACameraMetadata * staticMetadata,size_t * numPhysicalCameras,const char * const ** physicalCameraIds)144 bool ACameraMetadata_isLogicalMultiCamera(const ACameraMetadata* staticMetadata,
145         /*out*/size_t* numPhysicalCameras, /*out*/const char*const** physicalCameraIds) {
146     ATRACE_CALL();
147     if (numPhysicalCameras == nullptr || physicalCameraIds == nullptr) {
148         ALOGE("%s: Invalid input: numPhysicalCameras %p, physicalCameraIds %p",
149                  __FUNCTION__, numPhysicalCameras, physicalCameraIds);
150         return false;
151     }
152     if (staticMetadata == nullptr) {
153         ALOGE("%s: Invalid input: staticMetadata is null.", __FUNCTION__);
154         return false;
155     }
156 
157     return staticMetadata->isLogicalMultiCamera(numPhysicalCameras, physicalCameraIds);
158 }
159 
160 #ifndef __ANDROID_VNDK__
161 EXPORT
ACameraMetadata_fromCameraMetadata(JNIEnv * env,jobject cameraMetadata)162 ACameraMetadata* ACameraMetadata_fromCameraMetadata(JNIEnv* env, jobject cameraMetadata) {
163     ATRACE_CALL();
164 
165     const bool ok = InitJni(env);
166     LOG_ALWAYS_FATAL_IF(!ok, "Failed to find CameraMetadata Java classes.");
167 
168     if (cameraMetadata == nullptr) {
169         return nullptr;
170     }
171 
172     ACameraMetadata::ACAMERA_METADATA_TYPE type;
173     if (env->IsInstanceOf(cameraMetadata,
174         android_hardware_camera2_CameraCharacteristics_clazz)) {
175         type = ACameraMetadata::ACM_CHARACTERISTICS;
176     } else if (env->IsInstanceOf(cameraMetadata,
177         android_hardware_camera2_CaptureResult_clazz)) {
178         type = ACameraMetadata::ACM_RESULT;
179     } else {
180         return nullptr;
181     }
182 
183     auto sharedData = CameraMetadata_getNativeMetadataPtr(env, cameraMetadata);
184     ACameraMetadata* output = new ACameraMetadata(*sharedData, type);
185     output->incStrong(/*id=*/(void*) ACameraMetadata_fromCameraMetadata);
186     return output;
187 }
188 #endif  /* __ANDROID_VNDK__ */
189