The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | ** |
| 3 | ** Copyright 2008, The Android Open Source Project |
| 4 | ** |
| 5 | ** Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | ** you may not use this file except in compliance with the License. |
| 7 | ** You may obtain a copy of the License at |
| 8 | ** |
| 9 | ** http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | ** |
| 11 | ** Unless required by applicable law or agreed to in writing, software |
| 12 | ** distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | ** See the License for the specific language governing permissions and |
| 15 | ** limitations under the License. |
| 16 | */ |
| 17 | |
| 18 | //#define LOG_NDEBUG 0 |
| 19 | #define LOG_TAG "Camera-JNI" |
| 20 | #include <utils/Log.h> |
| 21 | |
| 22 | #include "jni.h" |
| 23 | #include "JNIHelp.h" |
| 24 | #include "android_runtime/AndroidRuntime.h" |
| 25 | |
Andrew Harp | 94927df | 2009-10-20 01:47:05 -0400 | [diff] [blame] | 26 | #include <utils/Vector.h> |
| 27 | |
Jamie Gennis | fd6f39e | 2010-12-20 12:15:00 -0800 | [diff] [blame^] | 28 | #include <gui/SurfaceTexture.h> |
Mathias Agopian | 000479f | 2010-02-09 17:46:37 -0800 | [diff] [blame] | 29 | #include <surfaceflinger/Surface.h> |
| 30 | #include <camera/Camera.h> |
Mathias Agopian | 0795272 | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 31 | #include <binder/IMemory.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 32 | |
| 33 | using namespace android; |
| 34 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 35 | struct fields_t { |
| 36 | jfieldID context; |
| 37 | jfieldID surface; |
Jamie Gennis | fd6f39e | 2010-12-20 12:15:00 -0800 | [diff] [blame^] | 38 | jfieldID surfaceTexture; |
Chih-Chung Chang | b8bb78f | 2010-06-10 13:32:16 +0800 | [diff] [blame] | 39 | jfieldID facing; |
| 40 | jfieldID orientation; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 41 | jmethodID post_event; |
| 42 | }; |
| 43 | |
| 44 | static fields_t fields; |
| 45 | static Mutex sLock; |
| 46 | |
Dave Sparks | 5e27115 | 2009-06-23 17:30:11 -0700 | [diff] [blame] | 47 | // provides persistent context for calls from native code to Java |
| 48 | class JNICameraContext: public CameraListener |
| 49 | { |
| 50 | public: |
| 51 | JNICameraContext(JNIEnv* env, jobject weak_this, jclass clazz, const sp<Camera>& camera); |
| 52 | ~JNICameraContext() { release(); } |
| 53 | virtual void notify(int32_t msgType, int32_t ext1, int32_t ext2); |
| 54 | virtual void postData(int32_t msgType, const sp<IMemory>& dataPtr); |
Dave Sparks | 59c1a93 | 2009-07-08 15:56:53 -0700 | [diff] [blame] | 55 | virtual void postDataTimestamp(nsecs_t timestamp, int32_t msgType, const sp<IMemory>& dataPtr); |
Andrew Harp | 94927df | 2009-10-20 01:47:05 -0400 | [diff] [blame] | 56 | void addCallbackBuffer(JNIEnv *env, jbyteArray cbb); |
| 57 | void setCallbackMode(JNIEnv *env, bool installed, bool manualMode); |
Dave Sparks | 5e27115 | 2009-06-23 17:30:11 -0700 | [diff] [blame] | 58 | sp<Camera> getCamera() { Mutex::Autolock _l(mLock); return mCamera; } |
| 59 | void release(); |
| 60 | |
| 61 | private: |
| 62 | void copyAndPost(JNIEnv* env, const sp<IMemory>& dataPtr, int msgType); |
Andrew Harp | 94927df | 2009-10-20 01:47:05 -0400 | [diff] [blame] | 63 | void clearCallbackBuffers_l(JNIEnv *env); |
Dave Sparks | 5e27115 | 2009-06-23 17:30:11 -0700 | [diff] [blame] | 64 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 65 | jobject mCameraJObjectWeak; // weak reference to java object |
| 66 | jclass mCameraJClass; // strong reference to java class |
Wu-cheng Li | ffe1cf2 | 2009-09-10 16:49:17 +0800 | [diff] [blame] | 67 | sp<Camera> mCamera; // strong reference to native object |
Dave Sparks | 5e27115 | 2009-06-23 17:30:11 -0700 | [diff] [blame] | 68 | Mutex mLock; |
Andrew Harp | 94927df | 2009-10-20 01:47:05 -0400 | [diff] [blame] | 69 | |
| 70 | Vector<jbyteArray> mCallbackBuffers; // Global reference application managed byte[] |
| 71 | bool mManualBufferMode; // Whether to use application managed buffers. |
| 72 | bool mManualCameraCallbackSet; // Whether the callback has been set, used to reduce unnecessary calls to set the callback. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 73 | }; |
| 74 | |
Dave Sparks | 5e27115 | 2009-06-23 17:30:11 -0700 | [diff] [blame] | 75 | sp<Camera> get_native_camera(JNIEnv *env, jobject thiz, JNICameraContext** pContext) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 76 | { |
| 77 | sp<Camera> camera; |
| 78 | Mutex::Autolock _l(sLock); |
Dave Sparks | 5e27115 | 2009-06-23 17:30:11 -0700 | [diff] [blame] | 79 | JNICameraContext* context = reinterpret_cast<JNICameraContext*>(env->GetIntField(thiz, fields.context)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 80 | if (context != NULL) { |
Dave Sparks | 5e27115 | 2009-06-23 17:30:11 -0700 | [diff] [blame] | 81 | camera = context->getCamera(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 82 | } |
| 83 | LOGV("get_native_camera: context=%p, camera=%p", context, camera.get()); |
| 84 | if (camera == 0) { |
| 85 | jniThrowException(env, "java/lang/RuntimeException", "Method called after release()"); |
| 86 | } |
| 87 | |
| 88 | if (pContext != NULL) *pContext = context; |
| 89 | return camera; |
| 90 | } |
| 91 | |
Dave Sparks | 5e27115 | 2009-06-23 17:30:11 -0700 | [diff] [blame] | 92 | JNICameraContext::JNICameraContext(JNIEnv* env, jobject weak_this, jclass clazz, const sp<Camera>& camera) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 93 | { |
Dave Sparks | 5e27115 | 2009-06-23 17:30:11 -0700 | [diff] [blame] | 94 | mCameraJObjectWeak = env->NewGlobalRef(weak_this); |
| 95 | mCameraJClass = (jclass)env->NewGlobalRef(clazz); |
| 96 | mCamera = camera; |
Andrew Harp | 94927df | 2009-10-20 01:47:05 -0400 | [diff] [blame] | 97 | |
| 98 | mManualBufferMode = false; |
| 99 | mManualCameraCallbackSet = false; |
Dave Sparks | 5e27115 | 2009-06-23 17:30:11 -0700 | [diff] [blame] | 100 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 101 | |
Dave Sparks | 5e27115 | 2009-06-23 17:30:11 -0700 | [diff] [blame] | 102 | void JNICameraContext::release() |
| 103 | { |
| 104 | LOGV("release"); |
| 105 | Mutex::Autolock _l(mLock); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 106 | JNIEnv *env = AndroidRuntime::getJNIEnv(); |
Dave Sparks | 5e27115 | 2009-06-23 17:30:11 -0700 | [diff] [blame] | 107 | |
| 108 | if (mCameraJObjectWeak != NULL) { |
| 109 | env->DeleteGlobalRef(mCameraJObjectWeak); |
| 110 | mCameraJObjectWeak = NULL; |
| 111 | } |
| 112 | if (mCameraJClass != NULL) { |
| 113 | env->DeleteGlobalRef(mCameraJClass); |
| 114 | mCameraJClass = NULL; |
| 115 | } |
Andrew Harp | 94927df | 2009-10-20 01:47:05 -0400 | [diff] [blame] | 116 | clearCallbackBuffers_l(env); |
Dave Sparks | 5e27115 | 2009-06-23 17:30:11 -0700 | [diff] [blame] | 117 | mCamera.clear(); |
| 118 | } |
| 119 | |
| 120 | void JNICameraContext::notify(int32_t msgType, int32_t ext1, int32_t ext2) |
| 121 | { |
| 122 | LOGV("notify"); |
| 123 | |
| 124 | // VM pointer will be NULL if object is released |
| 125 | Mutex::Autolock _l(mLock); |
| 126 | if (mCameraJObjectWeak == NULL) { |
| 127 | LOGW("callback on dead camera object"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 128 | return; |
| 129 | } |
Dave Sparks | 5e27115 | 2009-06-23 17:30:11 -0700 | [diff] [blame] | 130 | JNIEnv *env = AndroidRuntime::getJNIEnv(); |
Dave Sparks | c62f9bd | 2009-06-26 13:33:32 -0700 | [diff] [blame] | 131 | env->CallStaticVoidMethod(mCameraJClass, fields.post_event, |
Chih-Chung Chang | 6157de0 | 2009-09-24 15:29:35 -0700 | [diff] [blame] | 132 | mCameraJObjectWeak, msgType, ext1, ext2, NULL); |
Dave Sparks | 5e27115 | 2009-06-23 17:30:11 -0700 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | void JNICameraContext::copyAndPost(JNIEnv* env, const sp<IMemory>& dataPtr, int msgType) |
| 136 | { |
| 137 | jbyteArray obj = NULL; |
| 138 | |
| 139 | // allocate Java byte array and copy data |
| 140 | if (dataPtr != NULL) { |
| 141 | ssize_t offset; |
| 142 | size_t size; |
| 143 | sp<IMemoryHeap> heap = dataPtr->getMemory(&offset, &size); |
| 144 | LOGV("postData: off=%d, size=%d", offset, size); |
| 145 | uint8_t *heapBase = (uint8_t*)heap->base(); |
| 146 | |
| 147 | if (heapBase != NULL) { |
Dave Sparks | c4ca420 | 2009-07-13 09:38:20 -0700 | [diff] [blame] | 148 | const jbyte* data = reinterpret_cast<const jbyte*>(heapBase + offset); |
Andrew Harp | 94927df | 2009-10-20 01:47:05 -0400 | [diff] [blame] | 149 | |
| 150 | if (!mManualBufferMode) { |
| 151 | LOGV("Allocating callback buffer"); |
| 152 | obj = env->NewByteArray(size); |
| 153 | } else { |
| 154 | // Vector access should be protected by lock in postData() |
| 155 | if(!mCallbackBuffers.isEmpty()) { |
| 156 | LOGV("Using callback buffer from queue of length %d", mCallbackBuffers.size()); |
| 157 | jbyteArray globalBuffer = mCallbackBuffers.itemAt(0); |
| 158 | mCallbackBuffers.removeAt(0); |
| 159 | |
| 160 | obj = (jbyteArray)env->NewLocalRef(globalBuffer); |
| 161 | env->DeleteGlobalRef(globalBuffer); |
| 162 | |
| 163 | if (obj != NULL) { |
| 164 | jsize bufferLength = env->GetArrayLength(obj); |
| 165 | if ((int)bufferLength < (int)size) { |
| 166 | LOGE("Manually set buffer was too small! Expected %d bytes, but got %d!", |
| 167 | size, bufferLength); |
| 168 | env->DeleteLocalRef(obj); |
| 169 | return; |
| 170 | } |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | if(mCallbackBuffers.isEmpty()) { |
Wu-cheng Li | f5dbf6a | 2010-01-30 22:34:48 +0800 | [diff] [blame] | 175 | LOGV("Out of buffers, clearing callback!"); |
Andrew Harp | 94927df | 2009-10-20 01:47:05 -0400 | [diff] [blame] | 176 | mCamera->setPreviewCallbackFlags(FRAME_CALLBACK_FLAG_NOOP); |
| 177 | mManualCameraCallbackSet = false; |
| 178 | |
| 179 | if (obj == NULL) { |
| 180 | return; |
| 181 | } |
| 182 | } |
| 183 | } |
| 184 | |
Dave Sparks | 5e27115 | 2009-06-23 17:30:11 -0700 | [diff] [blame] | 185 | if (obj == NULL) { |
| 186 | LOGE("Couldn't allocate byte array for JPEG data"); |
| 187 | env->ExceptionClear(); |
| 188 | } else { |
Dave Sparks | a95f495 | 2009-07-10 18:13:36 -0700 | [diff] [blame] | 189 | env->SetByteArrayRegion(obj, 0, size, data); |
Dave Sparks | 5e27115 | 2009-06-23 17:30:11 -0700 | [diff] [blame] | 190 | } |
| 191 | } else { |
| 192 | LOGE("image heap is NULL"); |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | // post image data to Java |
| 197 | env->CallStaticVoidMethod(mCameraJClass, fields.post_event, |
| 198 | mCameraJObjectWeak, msgType, 0, 0, obj); |
| 199 | if (obj) { |
| 200 | env->DeleteLocalRef(obj); |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | void JNICameraContext::postData(int32_t msgType, const sp<IMemory>& dataPtr) |
| 205 | { |
| 206 | // VM pointer will be NULL if object is released |
| 207 | Mutex::Autolock _l(mLock); |
| 208 | JNIEnv *env = AndroidRuntime::getJNIEnv(); |
Dave Sparks | d0cbb1a | 2009-06-29 19:03:33 -0700 | [diff] [blame] | 209 | if (mCameraJObjectWeak == NULL) { |
| 210 | LOGW("callback on dead camera object"); |
| 211 | return; |
| 212 | } |
Dave Sparks | 5e27115 | 2009-06-23 17:30:11 -0700 | [diff] [blame] | 213 | |
| 214 | // return data based on callback type |
| 215 | switch(msgType) { |
Dave Sparks | 5e27115 | 2009-06-23 17:30:11 -0700 | [diff] [blame] | 216 | case CAMERA_MSG_VIDEO_FRAME: |
Dave Sparks | c62f9bd | 2009-06-26 13:33:32 -0700 | [diff] [blame] | 217 | // should never happen |
Dave Sparks | 5e27115 | 2009-06-23 17:30:11 -0700 | [diff] [blame] | 218 | break; |
Dave Sparks | c62f9bd | 2009-06-26 13:33:32 -0700 | [diff] [blame] | 219 | // don't return raw data to Java |
Dave Sparks | 5e27115 | 2009-06-23 17:30:11 -0700 | [diff] [blame] | 220 | case CAMERA_MSG_RAW_IMAGE: |
| 221 | LOGV("rawCallback"); |
| 222 | env->CallStaticVoidMethod(mCameraJClass, fields.post_event, |
Dave Sparks | c62f9bd | 2009-06-26 13:33:32 -0700 | [diff] [blame] | 223 | mCameraJObjectWeak, msgType, 0, 0, NULL); |
Dave Sparks | 5e27115 | 2009-06-23 17:30:11 -0700 | [diff] [blame] | 224 | break; |
| 225 | default: |
Dave Sparks | da57856 | 2009-07-21 08:43:29 -0700 | [diff] [blame] | 226 | // TODO: Change to LOGV |
Dave Sparks | c95a176 | 2010-01-04 08:55:04 -0800 | [diff] [blame] | 227 | LOGV("dataCallback(%d, %p)", msgType, dataPtr.get()); |
Dave Sparks | c62f9bd | 2009-06-26 13:33:32 -0700 | [diff] [blame] | 228 | copyAndPost(env, dataPtr, msgType); |
Dave Sparks | 5e27115 | 2009-06-23 17:30:11 -0700 | [diff] [blame] | 229 | break; |
| 230 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 231 | } |
| 232 | |
Dave Sparks | 59c1a93 | 2009-07-08 15:56:53 -0700 | [diff] [blame] | 233 | void JNICameraContext::postDataTimestamp(nsecs_t timestamp, int32_t msgType, const sp<IMemory>& dataPtr) |
| 234 | { |
| 235 | // TODO: plumb up to Java. For now, just drop the timestamp |
| 236 | postData(msgType, dataPtr); |
| 237 | } |
| 238 | |
Andrew Harp | 94927df | 2009-10-20 01:47:05 -0400 | [diff] [blame] | 239 | void JNICameraContext::setCallbackMode(JNIEnv *env, bool installed, bool manualMode) |
| 240 | { |
| 241 | Mutex::Autolock _l(mLock); |
| 242 | mManualBufferMode = manualMode; |
| 243 | mManualCameraCallbackSet = false; |
| 244 | |
| 245 | // In order to limit the over usage of binder threads, all non-manual buffer |
| 246 | // callbacks use FRAME_CALLBACK_FLAG_BARCODE_SCANNER mode now. |
| 247 | // |
| 248 | // Continuous callbacks will have the callback re-registered from handleMessage. |
| 249 | // Manual buffer mode will operate as fast as possible, relying on the finite supply |
| 250 | // of buffers for throttling. |
| 251 | |
| 252 | if (!installed) { |
| 253 | mCamera->setPreviewCallbackFlags(FRAME_CALLBACK_FLAG_NOOP); |
| 254 | clearCallbackBuffers_l(env); |
| 255 | } else if (mManualBufferMode) { |
| 256 | if (!mCallbackBuffers.isEmpty()) { |
| 257 | mCamera->setPreviewCallbackFlags(FRAME_CALLBACK_FLAG_CAMERA); |
| 258 | mManualCameraCallbackSet = true; |
| 259 | } |
| 260 | } else { |
| 261 | mCamera->setPreviewCallbackFlags(FRAME_CALLBACK_FLAG_BARCODE_SCANNER); |
| 262 | clearCallbackBuffers_l(env); |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | void JNICameraContext::addCallbackBuffer(JNIEnv *env, jbyteArray cbb) |
| 267 | { |
| 268 | if (cbb != NULL) { |
| 269 | Mutex::Autolock _l(mLock); |
| 270 | jbyteArray callbackBuffer = (jbyteArray)env->NewGlobalRef(cbb); |
| 271 | mCallbackBuffers.push(cbb); |
| 272 | |
| 273 | LOGV("Adding callback buffer to queue, %d total", mCallbackBuffers.size()); |
| 274 | |
| 275 | // We want to make sure the camera knows we're ready for the next frame. |
| 276 | // This may have come unset had we not had a callbackbuffer ready for it last time. |
| 277 | if (mManualBufferMode && !mManualCameraCallbackSet) { |
| 278 | mCamera->setPreviewCallbackFlags(FRAME_CALLBACK_FLAG_CAMERA); |
| 279 | mManualCameraCallbackSet = true; |
| 280 | } |
| 281 | } else { |
| 282 | LOGE("Null byte array!"); |
| 283 | } |
| 284 | } |
| 285 | |
| 286 | void JNICameraContext::clearCallbackBuffers_l(JNIEnv *env) |
| 287 | { |
| 288 | LOGV("Clearing callback buffers, %d remained", mCallbackBuffers.size()); |
| 289 | while(!mCallbackBuffers.isEmpty()) { |
| 290 | env->DeleteGlobalRef(mCallbackBuffers.top()); |
| 291 | mCallbackBuffers.pop(); |
| 292 | } |
| 293 | } |
| 294 | |
Chih-Chung Chang | e25cc65 | 2010-05-06 16:36:58 +0800 | [diff] [blame] | 295 | static jint android_hardware_Camera_getNumberOfCameras(JNIEnv *env, jobject thiz) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 296 | { |
Chih-Chung Chang | e25cc65 | 2010-05-06 16:36:58 +0800 | [diff] [blame] | 297 | return Camera::getNumberOfCameras(); |
| 298 | } |
| 299 | |
Chih-Chung Chang | b8bb78f | 2010-06-10 13:32:16 +0800 | [diff] [blame] | 300 | static void android_hardware_Camera_getCameraInfo(JNIEnv *env, jobject thiz, |
| 301 | jint cameraId, jobject info_obj) |
| 302 | { |
| 303 | CameraInfo cameraInfo; |
| 304 | status_t rc = Camera::getCameraInfo(cameraId, &cameraInfo); |
| 305 | if (rc != NO_ERROR) { |
| 306 | jniThrowException(env, "java/lang/RuntimeException", |
| 307 | "Fail to get camera info"); |
| 308 | return; |
| 309 | } |
| 310 | env->SetIntField(info_obj, fields.facing, cameraInfo.facing); |
| 311 | env->SetIntField(info_obj, fields.orientation, cameraInfo.orientation); |
| 312 | } |
| 313 | |
Chih-Chung Chang | e25cc65 | 2010-05-06 16:36:58 +0800 | [diff] [blame] | 314 | // connect to camera service |
| 315 | static void android_hardware_Camera_native_setup(JNIEnv *env, jobject thiz, |
| 316 | jobject weak_this, jint cameraId) |
| 317 | { |
| 318 | sp<Camera> camera = Camera::connect(cameraId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 319 | |
| 320 | if (camera == NULL) { |
Wu-cheng Li | fa3e556 | 2009-05-04 19:38:43 +0800 | [diff] [blame] | 321 | jniThrowException(env, "java/lang/RuntimeException", |
| 322 | "Fail to connect to camera service"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 323 | return; |
| 324 | } |
| 325 | |
| 326 | // make sure camera hardware is alive |
| 327 | if (camera->getStatus() != NO_ERROR) { |
Wu-cheng Li | ba55b36 | 2009-06-10 16:55:39 +0800 | [diff] [blame] | 328 | jniThrowException(env, "java/lang/RuntimeException", "Camera initialization failed"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 329 | return; |
| 330 | } |
| 331 | |
| 332 | jclass clazz = env->GetObjectClass(thiz); |
| 333 | if (clazz == NULL) { |
Wu-cheng Li | ba55b36 | 2009-06-10 16:55:39 +0800 | [diff] [blame] | 334 | jniThrowException(env, "java/lang/RuntimeException", "Can't find android/hardware/Camera"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 335 | return; |
| 336 | } |
| 337 | |
| 338 | // We use a weak reference so the Camera object can be garbage collected. |
| 339 | // The reference is only used as a proxy for callbacks. |
Dave Sparks | 5e27115 | 2009-06-23 17:30:11 -0700 | [diff] [blame] | 340 | sp<JNICameraContext> context = new JNICameraContext(env, weak_this, clazz, camera); |
| 341 | context->incStrong(thiz); |
| 342 | camera->setListener(context); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 343 | |
| 344 | // save context in opaque field |
Dave Sparks | 5e27115 | 2009-06-23 17:30:11 -0700 | [diff] [blame] | 345 | env->SetIntField(thiz, fields.context, (int)context.get()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 346 | } |
| 347 | |
| 348 | // disconnect from camera service |
| 349 | // It's okay to call this when the native camera context is already null. |
| 350 | // This handles the case where the user has called release() and the |
| 351 | // finalizer is invoked later. |
| 352 | static void android_hardware_Camera_release(JNIEnv *env, jobject thiz) |
| 353 | { |
Dave Sparks | da57856 | 2009-07-21 08:43:29 -0700 | [diff] [blame] | 354 | // TODO: Change to LOGV |
Joe Onorato | f5d95cb | 2010-01-07 21:48:32 -0500 | [diff] [blame] | 355 | LOGV("release camera"); |
Dave Sparks | 5e27115 | 2009-06-23 17:30:11 -0700 | [diff] [blame] | 356 | JNICameraContext* context = NULL; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 357 | sp<Camera> camera; |
| 358 | { |
| 359 | Mutex::Autolock _l(sLock); |
Dave Sparks | 5e27115 | 2009-06-23 17:30:11 -0700 | [diff] [blame] | 360 | context = reinterpret_cast<JNICameraContext*>(env->GetIntField(thiz, fields.context)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 361 | |
| 362 | // Make sure we do not attempt to callback on a deleted Java object. |
| 363 | env->SetIntField(thiz, fields.context, 0); |
| 364 | } |
| 365 | |
| 366 | // clean up if release has not been called before |
| 367 | if (context != NULL) { |
Dave Sparks | 5e27115 | 2009-06-23 17:30:11 -0700 | [diff] [blame] | 368 | camera = context->getCamera(); |
| 369 | context->release(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 370 | LOGV("native_release: context=%p camera=%p", context, camera.get()); |
| 371 | |
| 372 | // clear callbacks |
| 373 | if (camera != NULL) { |
Dave Sparks | 5e27115 | 2009-06-23 17:30:11 -0700 | [diff] [blame] | 374 | camera->setPreviewCallbackFlags(FRAME_CALLBACK_FLAG_NOOP); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 375 | camera->disconnect(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 376 | } |
| 377 | |
| 378 | // remove context to prevent further Java access |
Dave Sparks | 5e27115 | 2009-06-23 17:30:11 -0700 | [diff] [blame] | 379 | context->decStrong(thiz); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 380 | } |
| 381 | } |
| 382 | |
| 383 | static void android_hardware_Camera_setPreviewDisplay(JNIEnv *env, jobject thiz, jobject jSurface) |
| 384 | { |
| 385 | LOGV("setPreviewDisplay"); |
| 386 | sp<Camera> camera = get_native_camera(env, thiz, NULL); |
| 387 | if (camera == 0) return; |
| 388 | |
Wu-cheng Li | b8a10fe | 2009-06-23 23:37:36 +0800 | [diff] [blame] | 389 | sp<Surface> surface = NULL; |
| 390 | if (jSurface != NULL) { |
| 391 | surface = reinterpret_cast<Surface*>(env->GetIntField(jSurface, fields.surface)); |
| 392 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 393 | if (camera->setPreviewDisplay(surface) != NO_ERROR) { |
| 394 | jniThrowException(env, "java/io/IOException", "setPreviewDisplay failed"); |
| 395 | } |
| 396 | } |
| 397 | |
Jamie Gennis | fd6f39e | 2010-12-20 12:15:00 -0800 | [diff] [blame^] | 398 | static void android_hardware_Camera_setPreviewTexture(JNIEnv *env, |
| 399 | jobject thiz, jobject jSurfaceTexture) |
| 400 | { |
| 401 | LOGV("setPreviewTexture"); |
| 402 | sp<Camera> camera = get_native_camera(env, thiz, NULL); |
| 403 | if (camera == 0) return; |
| 404 | |
| 405 | sp<SurfaceTexture> surfaceTexture = NULL; |
| 406 | if (jSurfaceTexture != NULL) { |
| 407 | surfaceTexture = reinterpret_cast<SurfaceTexture*>(env->GetIntField( |
| 408 | jSurfaceTexture, fields.surfaceTexture)); |
| 409 | } |
| 410 | if (camera->setPreviewTexture(surfaceTexture) != NO_ERROR) { |
| 411 | jniThrowException(env, "java/io/IOException", |
| 412 | "setPreviewTexture failed"); |
| 413 | } |
| 414 | } |
| 415 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 416 | static void android_hardware_Camera_startPreview(JNIEnv *env, jobject thiz) |
| 417 | { |
| 418 | LOGV("startPreview"); |
| 419 | sp<Camera> camera = get_native_camera(env, thiz, NULL); |
| 420 | if (camera == 0) return; |
| 421 | |
| 422 | if (camera->startPreview() != NO_ERROR) { |
Wu-cheng Li | ba55b36 | 2009-06-10 16:55:39 +0800 | [diff] [blame] | 423 | jniThrowException(env, "java/lang/RuntimeException", "startPreview failed"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 424 | return; |
| 425 | } |
| 426 | } |
| 427 | |
| 428 | static void android_hardware_Camera_stopPreview(JNIEnv *env, jobject thiz) |
| 429 | { |
| 430 | LOGV("stopPreview"); |
| 431 | sp<Camera> c = get_native_camera(env, thiz, NULL); |
| 432 | if (c == 0) return; |
| 433 | |
| 434 | c->stopPreview(); |
| 435 | } |
| 436 | |
| 437 | static bool android_hardware_Camera_previewEnabled(JNIEnv *env, jobject thiz) |
| 438 | { |
| 439 | LOGV("previewEnabled"); |
| 440 | sp<Camera> c = get_native_camera(env, thiz, NULL); |
| 441 | if (c == 0) return false; |
| 442 | |
| 443 | return c->previewEnabled(); |
| 444 | } |
| 445 | |
Andrew Harp | 94927df | 2009-10-20 01:47:05 -0400 | [diff] [blame] | 446 | static void android_hardware_Camera_setHasPreviewCallback(JNIEnv *env, jobject thiz, jboolean installed, jboolean manualBuffer) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 447 | { |
Andrew Harp | 94927df | 2009-10-20 01:47:05 -0400 | [diff] [blame] | 448 | LOGV("setHasPreviewCallback: installed:%d, manualBuffer:%d", (int)installed, (int)manualBuffer); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 449 | // Important: Only install preview_callback if the Java code has called |
| 450 | // setPreviewCallback() with a non-null value, otherwise we'd pay to memcpy |
| 451 | // each preview frame for nothing. |
Dave Sparks | 5e27115 | 2009-06-23 17:30:11 -0700 | [diff] [blame] | 452 | JNICameraContext* context; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 453 | sp<Camera> camera = get_native_camera(env, thiz, &context); |
| 454 | if (camera == 0) return; |
| 455 | |
Andrew Harp | 94927df | 2009-10-20 01:47:05 -0400 | [diff] [blame] | 456 | // setCallbackMode will take care of setting the context flags and calling |
| 457 | // camera->setPreviewCallbackFlags within a mutex for us. |
| 458 | context->setCallbackMode(env, installed, manualBuffer); |
| 459 | } |
| 460 | |
| 461 | static void android_hardware_Camera_addCallbackBuffer(JNIEnv *env, jobject thiz, jbyteArray bytes) { |
| 462 | LOGV("addCallbackBuffer"); |
| 463 | |
| 464 | JNICameraContext* context = reinterpret_cast<JNICameraContext*>(env->GetIntField(thiz, fields.context)); |
| 465 | |
| 466 | if (context != NULL) { |
| 467 | context->addCallbackBuffer(env, bytes); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 468 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 469 | } |
| 470 | |
| 471 | static void android_hardware_Camera_autoFocus(JNIEnv *env, jobject thiz) |
| 472 | { |
| 473 | LOGV("autoFocus"); |
Dave Sparks | 5e27115 | 2009-06-23 17:30:11 -0700 | [diff] [blame] | 474 | JNICameraContext* context; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 475 | sp<Camera> c = get_native_camera(env, thiz, &context); |
| 476 | if (c == 0) return; |
| 477 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 478 | if (c->autoFocus() != NO_ERROR) { |
Wu-cheng Li | ba55b36 | 2009-06-10 16:55:39 +0800 | [diff] [blame] | 479 | jniThrowException(env, "java/lang/RuntimeException", "autoFocus failed"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 480 | } |
| 481 | } |
| 482 | |
Chih-Chung Chang | 244f8c2 | 2009-09-15 14:51:56 +0800 | [diff] [blame] | 483 | static void android_hardware_Camera_cancelAutoFocus(JNIEnv *env, jobject thiz) |
| 484 | { |
| 485 | LOGV("cancelAutoFocus"); |
| 486 | JNICameraContext* context; |
| 487 | sp<Camera> c = get_native_camera(env, thiz, &context); |
| 488 | if (c == 0) return; |
| 489 | |
| 490 | if (c->cancelAutoFocus() != NO_ERROR) { |
| 491 | jniThrowException(env, "java/lang/RuntimeException", "cancelAutoFocus failed"); |
| 492 | } |
| 493 | } |
| 494 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 495 | static void android_hardware_Camera_takePicture(JNIEnv *env, jobject thiz) |
| 496 | { |
| 497 | LOGV("takePicture"); |
Dave Sparks | 5e27115 | 2009-06-23 17:30:11 -0700 | [diff] [blame] | 498 | JNICameraContext* context; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 499 | sp<Camera> camera = get_native_camera(env, thiz, &context); |
| 500 | if (camera == 0) return; |
| 501 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 502 | if (camera->takePicture() != NO_ERROR) { |
Wu-cheng Li | ba55b36 | 2009-06-10 16:55:39 +0800 | [diff] [blame] | 503 | jniThrowException(env, "java/lang/RuntimeException", "takePicture failed"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 504 | return; |
| 505 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 506 | } |
| 507 | |
| 508 | static void android_hardware_Camera_setParameters(JNIEnv *env, jobject thiz, jstring params) |
| 509 | { |
| 510 | LOGV("setParameters"); |
| 511 | sp<Camera> camera = get_native_camera(env, thiz, NULL); |
| 512 | if (camera == 0) return; |
| 513 | |
| 514 | const jchar* str = env->GetStringCritical(params, 0); |
| 515 | String8 params8; |
| 516 | if (params) { |
| 517 | params8 = String8(str, env->GetStringLength(params)); |
| 518 | env->ReleaseStringCritical(params, str); |
| 519 | } |
| 520 | if (camera->setParameters(params8) != NO_ERROR) { |
Wu-cheng Li | ba55b36 | 2009-06-10 16:55:39 +0800 | [diff] [blame] | 521 | jniThrowException(env, "java/lang/RuntimeException", "setParameters failed"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 522 | return; |
| 523 | } |
| 524 | } |
| 525 | |
| 526 | static jstring android_hardware_Camera_getParameters(JNIEnv *env, jobject thiz) |
| 527 | { |
| 528 | LOGV("getParameters"); |
| 529 | sp<Camera> camera = get_native_camera(env, thiz, NULL); |
| 530 | if (camera == 0) return 0; |
| 531 | |
| 532 | return env->NewStringUTF(camera->getParameters().string()); |
| 533 | } |
| 534 | |
| 535 | static void android_hardware_Camera_reconnect(JNIEnv *env, jobject thiz) |
| 536 | { |
| 537 | LOGV("reconnect"); |
| 538 | sp<Camera> camera = get_native_camera(env, thiz, NULL); |
| 539 | if (camera == 0) return; |
| 540 | |
| 541 | if (camera->reconnect() != NO_ERROR) { |
| 542 | jniThrowException(env, "java/io/IOException", "reconnect failed"); |
| 543 | return; |
| 544 | } |
| 545 | } |
| 546 | |
Wu-cheng Li | ffe1cf2 | 2009-09-10 16:49:17 +0800 | [diff] [blame] | 547 | static void android_hardware_Camera_lock(JNIEnv *env, jobject thiz) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 548 | { |
| 549 | LOGV("lock"); |
| 550 | sp<Camera> camera = get_native_camera(env, thiz, NULL); |
Wu-cheng Li | ffe1cf2 | 2009-09-10 16:49:17 +0800 | [diff] [blame] | 551 | if (camera == 0) return; |
| 552 | |
| 553 | if (camera->lock() != NO_ERROR) { |
| 554 | jniThrowException(env, "java/lang/RuntimeException", "lock failed"); |
| 555 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 556 | } |
| 557 | |
Wu-cheng Li | ffe1cf2 | 2009-09-10 16:49:17 +0800 | [diff] [blame] | 558 | static void android_hardware_Camera_unlock(JNIEnv *env, jobject thiz) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 559 | { |
| 560 | LOGV("unlock"); |
| 561 | sp<Camera> camera = get_native_camera(env, thiz, NULL); |
Wu-cheng Li | ffe1cf2 | 2009-09-10 16:49:17 +0800 | [diff] [blame] | 562 | if (camera == 0) return; |
| 563 | |
| 564 | if (camera->unlock() != NO_ERROR) { |
| 565 | jniThrowException(env, "java/lang/RuntimeException", "unlock failed"); |
| 566 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 567 | } |
| 568 | |
Wu-cheng Li | 36f68b8 | 2009-09-28 16:14:58 -0700 | [diff] [blame] | 569 | static void android_hardware_Camera_startSmoothZoom(JNIEnv *env, jobject thiz, jint value) |
| 570 | { |
Joe Onorato | f5d95cb | 2010-01-07 21:48:32 -0500 | [diff] [blame] | 571 | LOGV("startSmoothZoom"); |
Wu-cheng Li | 36f68b8 | 2009-09-28 16:14:58 -0700 | [diff] [blame] | 572 | sp<Camera> camera = get_native_camera(env, thiz, NULL); |
| 573 | if (camera == 0) return; |
| 574 | |
Wu-cheng Li | 0ca2519 | 2010-03-29 16:21:12 +0800 | [diff] [blame] | 575 | status_t rc = camera->sendCommand(CAMERA_CMD_START_SMOOTH_ZOOM, value, 0); |
| 576 | if (rc == BAD_VALUE) { |
| 577 | char msg[64]; |
| 578 | sprintf(msg, "invalid zoom value=%d", value); |
| 579 | jniThrowException(env, "java/lang/IllegalArgumentException", msg); |
| 580 | } else if (rc != NO_ERROR) { |
Wu-cheng Li | 36f68b8 | 2009-09-28 16:14:58 -0700 | [diff] [blame] | 581 | jniThrowException(env, "java/lang/RuntimeException", "start smooth zoom failed"); |
| 582 | } |
| 583 | } |
| 584 | |
| 585 | static void android_hardware_Camera_stopSmoothZoom(JNIEnv *env, jobject thiz) |
| 586 | { |
Joe Onorato | f5d95cb | 2010-01-07 21:48:32 -0500 | [diff] [blame] | 587 | LOGV("stopSmoothZoom"); |
Wu-cheng Li | 36f68b8 | 2009-09-28 16:14:58 -0700 | [diff] [blame] | 588 | sp<Camera> camera = get_native_camera(env, thiz, NULL); |
| 589 | if (camera == 0) return; |
| 590 | |
| 591 | if (camera->sendCommand(CAMERA_CMD_STOP_SMOOTH_ZOOM, 0, 0) != NO_ERROR) { |
| 592 | jniThrowException(env, "java/lang/RuntimeException", "stop smooth zoom failed"); |
| 593 | } |
| 594 | } |
| 595 | |
Chih-Chung Chang | d1d7706 | 2010-01-22 17:49:48 -0800 | [diff] [blame] | 596 | static void android_hardware_Camera_setDisplayOrientation(JNIEnv *env, jobject thiz, |
| 597 | jint value) |
| 598 | { |
| 599 | LOGV("setDisplayOrientation"); |
| 600 | sp<Camera> camera = get_native_camera(env, thiz, NULL); |
| 601 | if (camera == 0) return; |
| 602 | |
| 603 | if (camera->sendCommand(CAMERA_CMD_SET_DISPLAY_ORIENTATION, value, 0) != NO_ERROR) { |
| 604 | jniThrowException(env, "java/lang/RuntimeException", "set display orientation failed"); |
| 605 | } |
| 606 | } |
| 607 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 608 | //------------------------------------------------- |
| 609 | |
| 610 | static JNINativeMethod camMethods[] = { |
Chih-Chung Chang | e25cc65 | 2010-05-06 16:36:58 +0800 | [diff] [blame] | 611 | { "getNumberOfCameras", |
| 612 | "()I", |
| 613 | (void *)android_hardware_Camera_getNumberOfCameras }, |
Chih-Chung Chang | b8bb78f | 2010-06-10 13:32:16 +0800 | [diff] [blame] | 614 | { "getCameraInfo", |
| 615 | "(ILandroid/hardware/Camera$CameraInfo;)V", |
| 616 | (void*)android_hardware_Camera_getCameraInfo }, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 617 | { "native_setup", |
Chih-Chung Chang | e25cc65 | 2010-05-06 16:36:58 +0800 | [diff] [blame] | 618 | "(Ljava/lang/Object;I)V", |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 619 | (void*)android_hardware_Camera_native_setup }, |
| 620 | { "native_release", |
| 621 | "()V", |
| 622 | (void*)android_hardware_Camera_release }, |
| 623 | { "setPreviewDisplay", |
| 624 | "(Landroid/view/Surface;)V", |
| 625 | (void *)android_hardware_Camera_setPreviewDisplay }, |
Jamie Gennis | fd6f39e | 2010-12-20 12:15:00 -0800 | [diff] [blame^] | 626 | { "setPreviewTexture", |
| 627 | "(Landroid/graphics/SurfaceTexture;)V", |
| 628 | (void *)android_hardware_Camera_setPreviewTexture }, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 629 | { "startPreview", |
| 630 | "()V", |
| 631 | (void *)android_hardware_Camera_startPreview }, |
| 632 | { "stopPreview", |
| 633 | "()V", |
| 634 | (void *)android_hardware_Camera_stopPreview }, |
| 635 | { "previewEnabled", |
| 636 | "()Z", |
| 637 | (void *)android_hardware_Camera_previewEnabled }, |
| 638 | { "setHasPreviewCallback", |
| 639 | "(ZZ)V", |
| 640 | (void *)android_hardware_Camera_setHasPreviewCallback }, |
Andrew Harp | 94927df | 2009-10-20 01:47:05 -0400 | [diff] [blame] | 641 | { "addCallbackBuffer", |
| 642 | "([B)V", |
| 643 | (void *)android_hardware_Camera_addCallbackBuffer }, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 644 | { "native_autoFocus", |
| 645 | "()V", |
| 646 | (void *)android_hardware_Camera_autoFocus }, |
Chih-Chung Chang | 244f8c2 | 2009-09-15 14:51:56 +0800 | [diff] [blame] | 647 | { "native_cancelAutoFocus", |
| 648 | "()V", |
| 649 | (void *)android_hardware_Camera_cancelAutoFocus }, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 650 | { "native_takePicture", |
| 651 | "()V", |
| 652 | (void *)android_hardware_Camera_takePicture }, |
| 653 | { "native_setParameters", |
| 654 | "(Ljava/lang/String;)V", |
| 655 | (void *)android_hardware_Camera_setParameters }, |
| 656 | { "native_getParameters", |
| 657 | "()Ljava/lang/String;", |
| 658 | (void *)android_hardware_Camera_getParameters }, |
| 659 | { "reconnect", |
| 660 | "()V", |
| 661 | (void*)android_hardware_Camera_reconnect }, |
| 662 | { "lock", |
Wu-cheng Li | ffe1cf2 | 2009-09-10 16:49:17 +0800 | [diff] [blame] | 663 | "()V", |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 664 | (void*)android_hardware_Camera_lock }, |
| 665 | { "unlock", |
Wu-cheng Li | ffe1cf2 | 2009-09-10 16:49:17 +0800 | [diff] [blame] | 666 | "()V", |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 667 | (void*)android_hardware_Camera_unlock }, |
Wu-cheng Li | 36f68b8 | 2009-09-28 16:14:58 -0700 | [diff] [blame] | 668 | { "startSmoothZoom", |
| 669 | "(I)V", |
| 670 | (void *)android_hardware_Camera_startSmoothZoom }, |
| 671 | { "stopSmoothZoom", |
| 672 | "()V", |
| 673 | (void *)android_hardware_Camera_stopSmoothZoom }, |
Chih-Chung Chang | d1d7706 | 2010-01-22 17:49:48 -0800 | [diff] [blame] | 674 | { "setDisplayOrientation", |
| 675 | "(I)V", |
| 676 | (void *)android_hardware_Camera_setDisplayOrientation }, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 677 | }; |
| 678 | |
| 679 | struct field { |
| 680 | const char *class_name; |
| 681 | const char *field_name; |
| 682 | const char *field_type; |
| 683 | jfieldID *jfield; |
| 684 | }; |
| 685 | |
| 686 | static int find_fields(JNIEnv *env, field *fields, int count) |
| 687 | { |
| 688 | for (int i = 0; i < count; i++) { |
| 689 | field *f = &fields[i]; |
| 690 | jclass clazz = env->FindClass(f->class_name); |
| 691 | if (clazz == NULL) { |
| 692 | LOGE("Can't find %s", f->class_name); |
| 693 | return -1; |
| 694 | } |
| 695 | |
| 696 | jfieldID field = env->GetFieldID(clazz, f->field_name, f->field_type); |
| 697 | if (field == NULL) { |
| 698 | LOGE("Can't find %s.%s", f->class_name, f->field_name); |
| 699 | return -1; |
| 700 | } |
| 701 | |
| 702 | *(f->jfield) = field; |
| 703 | } |
| 704 | |
| 705 | return 0; |
| 706 | } |
| 707 | |
| 708 | // Get all the required offsets in java class and register native functions |
| 709 | int register_android_hardware_Camera(JNIEnv *env) |
| 710 | { |
| 711 | field fields_to_find[] = { |
| 712 | { "android/hardware/Camera", "mNativeContext", "I", &fields.context }, |
Chih-Chung Chang | b8bb78f | 2010-06-10 13:32:16 +0800 | [diff] [blame] | 713 | { "android/view/Surface", ANDROID_VIEW_SURFACE_JNI_ID, "I", &fields.surface }, |
Jamie Gennis | fd6f39e | 2010-12-20 12:15:00 -0800 | [diff] [blame^] | 714 | { "android/graphics/SurfaceTexture", |
| 715 | ANDROID_GRAPHICS_SURFACETEXTURE_JNI_ID, "I", &fields.surfaceTexture }, |
Wu-cheng Li | 3fd5fa4 | 2010-09-15 16:06:20 -0700 | [diff] [blame] | 716 | { "android/hardware/Camera$CameraInfo", "facing", "I", &fields.facing }, |
| 717 | { "android/hardware/Camera$CameraInfo", "orientation", "I", &fields.orientation }, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 718 | }; |
| 719 | |
| 720 | if (find_fields(env, fields_to_find, NELEM(fields_to_find)) < 0) |
| 721 | return -1; |
| 722 | |
| 723 | jclass clazz = env->FindClass("android/hardware/Camera"); |
| 724 | fields.post_event = env->GetStaticMethodID(clazz, "postEventFromNative", |
| 725 | "(Ljava/lang/Object;IIILjava/lang/Object;)V"); |
| 726 | if (fields.post_event == NULL) { |
| 727 | LOGE("Can't find android/hardware/Camera.postEventFromNative"); |
| 728 | return -1; |
| 729 | } |
| 730 | |
| 731 | |
| 732 | // Register native functions |
| 733 | return AndroidRuntime::registerNativeMethods(env, "android/hardware/Camera", |
| 734 | camMethods, NELEM(camMethods)); |
| 735 | } |