android_hardware_camera2_legacy_LegacyCameraDevice.cpp revision a9bc3559109836efe7479a3279713bd58810b153
1/* 2 * Copyright (C) 2014 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_TAG "Legacy-CameraDevice-JNI" 18// #define LOG_NDEBUG 0 19#include <utils/Log.h> 20#include <utils/Errors.h> 21#include <utils/Trace.h> 22#include <camera/CameraUtils.h> 23 24#include "jni.h" 25#include "JNIHelp.h" 26#include "android_runtime/AndroidRuntime.h" 27#include "android_runtime/android_view_Surface.h" 28#include "android_runtime/android_graphics_SurfaceTexture.h" 29 30#include <gui/Surface.h> 31#include <gui/IGraphicBufferProducer.h> 32#include <ui/GraphicBuffer.h> 33#include <system/window.h> 34#include <hardware/camera3.h> 35#include <system/camera_metadata.h> 36 37#include <stdint.h> 38#include <inttypes.h> 39 40using namespace android; 41 42// fully-qualified class name 43#define CAMERA_DEVICE_CLASS_NAME "android/hardware/camera2/legacy/LegacyCameraDevice" 44#define CAMERA_DEVICE_BUFFER_SLACK 3 45#define DONT_CARE 0 46 47#define ARRAY_SIZE(a) (sizeof(a)/sizeof(*(a))) 48 49#define ALIGN(x, mask) ( ((x) + (mask) - 1) & ~((mask) - 1) ) 50 51/** 52 * Convert from RGB 888 to Y'CbCr using the conversion specified in ITU-R BT.601 for 53 * digital RGB with K_b = 0.114, and K_r = 0.299. 54 */ 55static void rgbToYuv420(uint8_t* rgbBuf, int32_t width, int32_t height, uint8_t* yPlane, 56 uint8_t* uPlane, uint8_t* vPlane, size_t chromaStep, size_t yStride, size_t chromaStride) { 57 uint8_t R, G, B; 58 size_t index = 0; 59 60 int32_t cStrideDiff = chromaStride - width; 61 62 for (int32_t j = 0; j < height; j++) { 63 for (int32_t i = 0; i < width; i++) { 64 R = rgbBuf[index++]; 65 G = rgbBuf[index++]; 66 B = rgbBuf[index++]; 67 *(yPlane + i) = ((66 * R + 129 * G + 25 * B + 128) >> 8) + 16; 68 69 if (j % 2 == 0 && i % 2 == 0){ 70 *uPlane = (( -38 * R - 74 * G + 112 * B + 128) >> 8) + 128; 71 *vPlane = (( 112 * R - 94 * G - 18 * B + 128) >> 8) + 128; 72 uPlane += chromaStep; 73 vPlane += chromaStep; 74 } 75 // Skip alpha 76 index++; 77 } 78 yPlane += yStride; 79 if (j % 2 == 0) { 80 uPlane += cStrideDiff; 81 vPlane += cStrideDiff; 82 } 83 } 84} 85 86static void rgbToYuv420(uint8_t* rgbBuf, int32_t width, int32_t height, android_ycbcr* ycbcr) { 87 size_t cStep = ycbcr->chroma_step; 88 size_t cStride = ycbcr->cstride; 89 size_t yStride = ycbcr->ystride; 90 rgbToYuv420(rgbBuf, width, height, reinterpret_cast<uint8_t*>(ycbcr->y), 91 reinterpret_cast<uint8_t*>(ycbcr->cb), reinterpret_cast<uint8_t*>(ycbcr->cr), 92 cStep, yStride, cStride); 93} 94 95static status_t configureSurface(const sp<ANativeWindow>& anw, 96 int32_t width, 97 int32_t height, 98 int32_t pixelFmt, 99 int32_t maxBufferSlack) { 100 status_t err = NO_ERROR; 101 err = native_window_set_buffers_dimensions(anw.get(), width, height); 102 if (err != NO_ERROR) { 103 ALOGE("%s: Failed to set native window buffer dimensions, error %s (%d).", __FUNCTION__, 104 strerror(-err), err); 105 return err; 106 } 107 108 err = native_window_set_buffers_format(anw.get(), pixelFmt); 109 if (err != NO_ERROR) { 110 ALOGE("%s: Failed to set native window buffer format, error %s (%d).", __FUNCTION__, 111 strerror(-err), err); 112 return err; 113 } 114 115 err = native_window_set_usage(anw.get(), GRALLOC_USAGE_SW_WRITE_OFTEN); 116 if (err != NO_ERROR) { 117 ALOGE("%s: Failed to set native window usage flag, error %s (%d).", __FUNCTION__, 118 strerror(-err), err); 119 return err; 120 } 121 122 int minUndequeuedBuffers; 123 err = anw.get()->query(anw.get(), 124 NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS, 125 &minUndequeuedBuffers); 126 if (err != NO_ERROR) { 127 ALOGE("%s: Failed to get native window min undequeued buffers, error %s (%d).", 128 __FUNCTION__, strerror(-err), err); 129 return err; 130 } 131 132 ALOGV("%s: Setting buffer count to %d, size to (%dx%d), fmt (0x%x)", __FUNCTION__, 133 maxBufferSlack + 1 + minUndequeuedBuffers, 134 width, height, pixelFmt); 135 err = native_window_set_buffer_count(anw.get(), maxBufferSlack + 1 + minUndequeuedBuffers); 136 if (err != NO_ERROR) { 137 ALOGE("%s: Failed to set native window buffer count, error %s (%d).", __FUNCTION__, 138 strerror(-err), err); 139 return err; 140 } 141 return NO_ERROR; 142} 143 144/** 145 * Produce a frame in the given surface. 146 * 147 * Args: 148 * anw - a surface to produce a frame in. 149 * pixelBuffer - image buffer to generate a frame from. 150 * width - width of the pixelBuffer in pixels. 151 * height - height of the pixelBuffer in pixels. 152 * pixelFmt - format of the pixelBuffer, one of: 153 * HAL_PIXEL_FORMAT_YCrCb_420_SP, 154 * HAL_PIXEL_FORMAT_YCbCr_420_888, 155 * HAL_PIXEL_FORMAT_BLOB 156 * bufSize - the size of the pixelBuffer in bytes. 157 */ 158static status_t produceFrame(const sp<ANativeWindow>& anw, 159 uint8_t* pixelBuffer, 160 int32_t width, // Width of the pixelBuffer 161 int32_t height, // Height of the pixelBuffer 162 int32_t pixelFmt, // Format of the pixelBuffer 163 int32_t bufSize) { 164 ATRACE_CALL(); 165 status_t err = NO_ERROR; 166 ANativeWindowBuffer* anb; 167 ALOGV("%s: Dequeue buffer from %p %dx%d (fmt=%x, size=%x)", 168 __FUNCTION__, anw.get(), width, height, pixelFmt, bufSize); 169 170 if (anw == 0) { 171 ALOGE("%s: anw must not be NULL", __FUNCTION__); 172 return BAD_VALUE; 173 } else if (pixelBuffer == NULL) { 174 ALOGE("%s: pixelBuffer must not be NULL", __FUNCTION__); 175 return BAD_VALUE; 176 } else if (width < 0) { 177 ALOGE("%s: width must be non-negative", __FUNCTION__); 178 return BAD_VALUE; 179 } else if (height < 0) { 180 ALOGE("%s: height must be non-negative", __FUNCTION__); 181 return BAD_VALUE; 182 } else if (bufSize < 0) { 183 ALOGE("%s: bufSize must be non-negative", __FUNCTION__); 184 return BAD_VALUE; 185 } 186 187 if (width < 0 || height < 0 || bufSize < 0) { 188 ALOGE("%s: Illegal argument, negative dimension passed to produceFrame", __FUNCTION__); 189 return BAD_VALUE; 190 } 191 192 // TODO: Switch to using Surface::lock and Surface::unlockAndPost 193 err = native_window_dequeue_buffer_and_wait(anw.get(), &anb); 194 if (err != NO_ERROR) return err; 195 196 // TODO: check anb is large enough to store the results 197 198 sp<GraphicBuffer> buf(new GraphicBuffer(anb, /*keepOwnership*/false)); 199 200 switch(pixelFmt) { 201 case HAL_PIXEL_FORMAT_YCrCb_420_SP: { 202 if (bufSize < width * height * 4) { 203 ALOGE("%s: PixelBuffer size %" PRId32 " to small for given dimensions", 204 __FUNCTION__, bufSize); 205 return BAD_VALUE; 206 } 207 uint8_t* img = NULL; 208 ALOGV("%s: Lock buffer from %p for write", __FUNCTION__, anw.get()); 209 err = buf->lock(GRALLOC_USAGE_SW_WRITE_OFTEN, (void**)(&img)); 210 if (err != NO_ERROR) return err; 211 212 uint8_t* yPlane = img; 213 uint8_t* uPlane = img + height * width; 214 uint8_t* vPlane = uPlane + 1; 215 size_t chromaStep = 2; 216 size_t yStride = width; 217 size_t chromaStride = width; 218 219 rgbToYuv420(pixelBuffer, width, height, yPlane, 220 uPlane, vPlane, chromaStep, yStride, chromaStride); 221 break; 222 } 223 case HAL_PIXEL_FORMAT_YV12: { 224 if (bufSize < width * height * 4) { 225 ALOGE("%s: PixelBuffer size %" PRId32 " to small for given dimensions", 226 __FUNCTION__, bufSize); 227 return BAD_VALUE; 228 } 229 230 if ((width & 1) || (height & 1)) { 231 ALOGE("%s: Dimens %dx%d are not divisible by 2.", __FUNCTION__, width, height); 232 return BAD_VALUE; 233 } 234 235 uint8_t* img = NULL; 236 ALOGV("%s: Lock buffer from %p for write", __FUNCTION__, anw.get()); 237 err = buf->lock(GRALLOC_USAGE_SW_WRITE_OFTEN, (void**)(&img)); 238 if (err != NO_ERROR) { 239 ALOGE("%s: Error %s (%d) while locking gralloc buffer for write.", __FUNCTION__, 240 strerror(-err), err); 241 return err; 242 } 243 244 uint32_t stride = buf->getStride(); 245 LOG_ALWAYS_FATAL_IF(stride % 16, "Stride is not 16 pixel aligned %d", stride); 246 247 uint32_t cStride = ALIGN(stride / 2, 16); 248 size_t chromaStep = 1; 249 250 uint8_t* yPlane = img; 251 uint8_t* crPlane = img + static_cast<uint32_t>(height) * stride; 252 uint8_t* cbPlane = crPlane + cStride * static_cast<uint32_t>(height) / 2; 253 254 rgbToYuv420(pixelBuffer, width, height, yPlane, 255 crPlane, cbPlane, chromaStep, stride, cStride); 256 break; 257 } 258 case HAL_PIXEL_FORMAT_YCbCr_420_888: { 259 // Software writes with YCbCr_420_888 format are unsupported 260 // by the gralloc module for now 261 if (bufSize < width * height * 4) { 262 ALOGE("%s: PixelBuffer size %" PRId32 " to small for given dimensions", 263 __FUNCTION__, bufSize); 264 return BAD_VALUE; 265 } 266 android_ycbcr ycbcr = android_ycbcr(); 267 ALOGV("%s: Lock buffer from %p for write", __FUNCTION__, anw.get()); 268 269 err = buf->lockYCbCr(GRALLOC_USAGE_SW_WRITE_OFTEN, &ycbcr); 270 if (err != NO_ERROR) { 271 ALOGE("%s: Failed to lock ycbcr buffer, error %s (%d).", __FUNCTION__, 272 strerror(-err), err); 273 return err; 274 } 275 rgbToYuv420(pixelBuffer, width, height, &ycbcr); 276 break; 277 } 278 case HAL_PIXEL_FORMAT_BLOB: { 279 if (bufSize != width || height != 1) { 280 ALOGE("%s: Incorrect pixelBuffer size: %" PRId32, __FUNCTION__, bufSize); 281 return BAD_VALUE; 282 } 283 int8_t* img = NULL; 284 285 ALOGV("%s: Lock buffer from %p for write", __FUNCTION__, anw.get()); 286 err = buf->lock(GRALLOC_USAGE_SW_WRITE_OFTEN, (void**)(&img)); 287 if (err != NO_ERROR) { 288 ALOGE("%s: Failed to lock buffer, error %s (%d).", __FUNCTION__, strerror(-err), 289 err); 290 return err; 291 } 292 struct camera3_jpeg_blob footer = { 293 jpeg_blob_id: CAMERA3_JPEG_BLOB_ID, 294 jpeg_size: (uint32_t)width 295 }; 296 memcpy(img, pixelBuffer, width); 297 memcpy(img + anb->width - sizeof(footer), &footer, sizeof(footer)); 298 break; 299 } 300 default: { 301 ALOGE("%s: Invalid pixel format in produceFrame: %x", __FUNCTION__, pixelFmt); 302 return BAD_VALUE; 303 } 304 } 305 306 ALOGV("%s: Unlock buffer from %p", __FUNCTION__, anw.get()); 307 err = buf->unlock(); 308 if (err != NO_ERROR) { 309 ALOGE("%s: Failed to unlock buffer, error %s (%d).", __FUNCTION__, strerror(-err), err); 310 return err; 311 } 312 313 ALOGV("%s: Queue buffer to %p", __FUNCTION__, anw.get()); 314 err = anw->queueBuffer(anw.get(), buf->getNativeBuffer(), /*fenceFd*/-1); 315 if (err != NO_ERROR) { 316 ALOGE("%s: Failed to queue buffer, error %s (%d).", __FUNCTION__, strerror(-err), err); 317 return err; 318 } 319 return NO_ERROR; 320} 321 322static sp<ANativeWindow> getNativeWindow(JNIEnv* env, jobject surface) { 323 sp<ANativeWindow> anw; 324 if (surface) { 325 anw = android_view_Surface_getNativeWindow(env, surface); 326 if (env->ExceptionCheck()) { 327 return NULL; 328 } 329 } else { 330 jniThrowNullPointerException(env, "surface"); 331 return NULL; 332 } 333 if (anw == NULL) { 334 jniThrowExceptionFmt(env, "java/lang/IllegalArgumentException", 335 "Surface had no valid native window."); 336 return NULL; 337 } 338 return anw; 339} 340 341static sp<ANativeWindow> getNativeWindowFromTexture(JNIEnv* env, jobject surfaceTexture) { 342 sp<ANativeWindow> anw; 343 if (surfaceTexture) { 344 anw = android_SurfaceTexture_getNativeWindow(env, surfaceTexture); 345 if (env->ExceptionCheck()) { 346 return NULL; 347 } 348 } else { 349 jniThrowNullPointerException(env, "surfaceTexture"); 350 return NULL; 351 } 352 if (anw == NULL) { 353 jniThrowExceptionFmt(env, "java/lang/IllegalArgumentException", 354 "SurfaceTexture had no valid native window."); 355 return NULL; 356 } 357 return anw; 358} 359 360static sp<Surface> getSurface(JNIEnv* env, jobject surface) { 361 sp<Surface> s; 362 if (surface) { 363 s = android_view_Surface_getSurface(env, surface); 364 if (env->ExceptionCheck()) { 365 return NULL; 366 } 367 } else { 368 jniThrowNullPointerException(env, "surface"); 369 return NULL; 370 } 371 if (s == NULL) { 372 jniThrowExceptionFmt(env, "java/lang/IllegalArgumentException", 373 "Surface had no valid native Surface."); 374 return NULL; 375 } 376 return s; 377} 378 379extern "C" { 380 381static jint LegacyCameraDevice_nativeDetectSurfaceType(JNIEnv* env, jobject thiz, jobject surface) { 382 ALOGV("nativeDetectSurfaceType"); 383 sp<ANativeWindow> anw; 384 if ((anw = getNativeWindow(env, surface)) == NULL) { 385 ALOGE("%s: Could not retrieve native window from surface.", __FUNCTION__); 386 return BAD_VALUE; 387 } 388 int32_t fmt = 0; 389 status_t err = anw->query(anw.get(), NATIVE_WINDOW_FORMAT, &fmt); 390 if(err != NO_ERROR) { 391 ALOGE("%s: Error while querying surface pixel format %s (%d).", __FUNCTION__, strerror(-err), 392 err); 393 return err; 394 } 395 return fmt; 396} 397 398static jint LegacyCameraDevice_nativeDetectSurfaceDimens(JNIEnv* env, jobject thiz, 399 jobject surface, jintArray dimens) { 400 ALOGV("nativeGetSurfaceDimens"); 401 402 if (dimens == NULL) { 403 ALOGE("%s: Null dimens argument passed to nativeDetectSurfaceDimens", __FUNCTION__); 404 return BAD_VALUE; 405 } 406 407 if (env->GetArrayLength(dimens) < 2) { 408 ALOGE("%s: Invalid length of dimens argument in nativeDetectSurfaceDimens", __FUNCTION__); 409 return BAD_VALUE; 410 } 411 412 sp<ANativeWindow> anw; 413 if ((anw = getNativeWindow(env, surface)) == NULL) { 414 ALOGE("%s: Could not retrieve native window from surface.", __FUNCTION__); 415 return BAD_VALUE; 416 } 417 int32_t dimenBuf[2]; 418 status_t err = anw->query(anw.get(), NATIVE_WINDOW_WIDTH, dimenBuf); 419 if(err != NO_ERROR) { 420 ALOGE("%s: Error while querying surface width %s (%d).", __FUNCTION__, strerror(-err), 421 err); 422 return err; 423 } 424 err = anw->query(anw.get(), NATIVE_WINDOW_HEIGHT, dimenBuf + 1); 425 if(err != NO_ERROR) { 426 ALOGE("%s: Error while querying surface height %s (%d).", __FUNCTION__, strerror(-err), 427 err); 428 return err; 429 } 430 env->SetIntArrayRegion(dimens, /*start*/0, /*length*/ARRAY_SIZE(dimenBuf), dimenBuf); 431 return NO_ERROR; 432} 433 434static jint LegacyCameraDevice_nativeDetectTextureDimens(JNIEnv* env, jobject thiz, 435 jobject surfaceTexture, jintArray dimens) { 436 ALOGV("nativeDetectTextureDimens"); 437 sp<ANativeWindow> anw; 438 if ((anw = getNativeWindowFromTexture(env, surfaceTexture)) == NULL) { 439 ALOGE("%s: Could not retrieve native window from SurfaceTexture.", __FUNCTION__); 440 return BAD_VALUE; 441 } 442 443 int32_t dimenBuf[2]; 444 status_t err = anw->query(anw.get(), NATIVE_WINDOW_WIDTH, dimenBuf); 445 if(err != NO_ERROR) { 446 ALOGE("%s: Error while querying SurfaceTexture width %s (%d)", __FUNCTION__, 447 strerror(-err), err); 448 return err; 449 } 450 451 err = anw->query(anw.get(), NATIVE_WINDOW_HEIGHT, dimenBuf + 1); 452 if(err != NO_ERROR) { 453 ALOGE("%s: Error while querying SurfaceTexture height %s (%d)", __FUNCTION__, 454 strerror(-err), err); 455 return err; 456 } 457 458 env->SetIntArrayRegion(dimens, /*start*/0, /*length*/ARRAY_SIZE(dimenBuf), dimenBuf); 459 if (env->ExceptionCheck()) { 460 return BAD_VALUE; 461 } 462 return NO_ERROR; 463} 464 465static jint LegacyCameraDevice_nativeConfigureSurface(JNIEnv* env, jobject thiz, jobject surface, 466 jint width, jint height, jint pixelFormat) { 467 ALOGV("nativeConfigureSurface"); 468 sp<ANativeWindow> anw; 469 if ((anw = getNativeWindow(env, surface)) == NULL) { 470 ALOGE("%s: Could not retrieve native window from surface.", __FUNCTION__); 471 return BAD_VALUE; 472 } 473 status_t err = configureSurface(anw, width, height, pixelFormat, CAMERA_DEVICE_BUFFER_SLACK); 474 if (err != NO_ERROR) { 475 ALOGE("%s: Error while configuring surface %s (%d).", __FUNCTION__, strerror(-err), err); 476 return err; 477 } 478 return NO_ERROR; 479} 480 481static jint LegacyCameraDevice_nativeProduceFrame(JNIEnv* env, jobject thiz, jobject surface, 482 jbyteArray pixelBuffer, jint width, jint height, jint pixelFormat) { 483 ALOGV("nativeProduceFrame"); 484 sp<ANativeWindow> anw; 485 486 if ((anw = getNativeWindow(env, surface)) == NULL) { 487 ALOGE("%s: Could not retrieve native window from surface.", __FUNCTION__); 488 return BAD_VALUE; 489 } 490 491 if (pixelBuffer == NULL) { 492 jniThrowNullPointerException(env, "pixelBuffer"); 493 return DONT_CARE; 494 } 495 496 int32_t bufSize = static_cast<int32_t>(env->GetArrayLength(pixelBuffer)); 497 jbyte* pixels = env->GetByteArrayElements(pixelBuffer, /*is_copy*/NULL); 498 499 if (pixels == NULL) { 500 jniThrowNullPointerException(env, "pixels"); 501 return DONT_CARE; 502 } 503 504 status_t err = produceFrame(anw, reinterpret_cast<uint8_t*>(pixels), width, height, 505 pixelFormat, bufSize); 506 env->ReleaseByteArrayElements(pixelBuffer, pixels, JNI_ABORT); 507 508 if (err != NO_ERROR) { 509 ALOGE("%s: Error while producing frame %s (%d).", __FUNCTION__, strerror(-err), err); 510 return err; 511 } 512 return NO_ERROR; 513} 514 515static jint LegacyCameraDevice_nativeSetSurfaceFormat(JNIEnv* env, jobject thiz, jobject surface, 516 jint pixelFormat) { 517 ALOGV("nativeSetSurfaceType"); 518 sp<ANativeWindow> anw; 519 if ((anw = getNativeWindow(env, surface)) == NULL) { 520 ALOGE("%s: Could not retrieve native window from surface.", __FUNCTION__); 521 return BAD_VALUE; 522 } 523 status_t err = native_window_set_buffers_format(anw.get(), pixelFormat); 524 if (err != NO_ERROR) { 525 ALOGE("%s: Error while setting surface format %s (%d).", __FUNCTION__, strerror(-err), err); 526 return err; 527 } 528 return NO_ERROR; 529} 530 531static jint LegacyCameraDevice_nativeSetSurfaceDimens(JNIEnv* env, jobject thiz, jobject surface, 532 jint width, jint height) { 533 ALOGV("nativeSetSurfaceDimens"); 534 sp<ANativeWindow> anw; 535 if ((anw = getNativeWindow(env, surface)) == NULL) { 536 ALOGE("%s: Could not retrieve native window from surface.", __FUNCTION__); 537 return BAD_VALUE; 538 } 539 status_t err = native_window_set_buffers_dimensions(anw.get(), width, height); 540 if (err != NO_ERROR) { 541 ALOGE("%s: Error while setting surface dimens %s (%d).", __FUNCTION__, strerror(-err), err); 542 return err; 543 } 544 545 // WAR - Set user dimensions also to avoid incorrect scaling after TextureView orientation 546 // change. 547 err = native_window_set_buffers_user_dimensions(anw.get(), width, height); 548 if (err != NO_ERROR) { 549 ALOGE("%s: Error while setting surface user dimens %s (%d).", __FUNCTION__, strerror(-err), 550 err); 551 return err; 552 } 553 return NO_ERROR; 554} 555 556static jlong LegacyCameraDevice_nativeGetSurfaceId(JNIEnv* env, jobject thiz, jobject surface) { 557 ALOGV("nativeGetSurfaceId"); 558 sp<Surface> s; 559 if ((s = getSurface(env, surface)) == NULL) { 560 ALOGE("%s: Could not retrieve native Surface from surface.", __FUNCTION__); 561 return 0; 562 } 563 sp<IGraphicBufferProducer> gbp = s->getIGraphicBufferProducer(); 564 if (gbp == NULL) { 565 ALOGE("%s: Could not retrieve IGraphicBufferProducer from surface.", __FUNCTION__); 566 return 0; 567 } 568 sp<IBinder> b = gbp->asBinder(); 569 if (b == NULL) { 570 ALOGE("%s: Could not retrieve IBinder from surface.", __FUNCTION__); 571 return 0; 572 } 573 /* 574 * FIXME: Use better unique ID for surfaces than native IBinder pointer. Fix also in the camera 575 * service (CameraDeviceClient.h). 576 */ 577 return reinterpret_cast<jlong>(b.get()); 578} 579 580static jint LegacyCameraDevice_nativeSetSurfaceOrientation(JNIEnv* env, jobject thiz, 581 jobject surface, jint facing, jint orientation) { 582 ALOGV("nativeSetSurfaceOrientation"); 583 sp<ANativeWindow> anw; 584 if ((anw = getNativeWindow(env, surface)) == NULL) { 585 ALOGE("%s: Could not retrieve native window from surface.", __FUNCTION__); 586 return BAD_VALUE; 587 } 588 589 status_t err = NO_ERROR; 590 CameraMetadata staticMetadata; 591 592 int32_t orientVal = static_cast<int32_t>(orientation); 593 uint8_t facingVal = static_cast<uint8_t>(facing); 594 staticMetadata.update(ANDROID_SENSOR_ORIENTATION, &orientVal, 1); 595 staticMetadata.update(ANDROID_LENS_FACING, &facingVal, 1); 596 597 int32_t transform = 0; 598 599 if ((err = CameraUtils::getRotationTransform(staticMetadata, /*out*/&transform)) != NO_ERROR) { 600 ALOGE("%s: Invalid rotation transform %s (%d)", __FUNCTION__, strerror(-err), 601 err); 602 return err; 603 } 604 605 ALOGV("%s: Setting buffer sticky transform to %d", __FUNCTION__, transform); 606 607 if ((err = native_window_set_buffers_sticky_transform(anw.get(), transform)) != NO_ERROR) { 608 ALOGE("%s: Unable to configure surface transform, error %s (%d)", __FUNCTION__, 609 strerror(-err), err); 610 return err; 611 } 612 613 return NO_ERROR; 614} 615 616static jint LegacyCameraDevice_nativeSetNextTimestamp(JNIEnv* env, jobject thiz, jobject surface, 617 jlong timestamp) { 618 ALOGV("nativeSetNextTimestamp"); 619 sp<ANativeWindow> anw; 620 if ((anw = getNativeWindow(env, surface)) == NULL) { 621 ALOGE("%s: Could not retrieve native window from surface.", __FUNCTION__); 622 return BAD_VALUE; 623 } 624 625 status_t err = NO_ERROR; 626 627 if ((err = native_window_set_buffers_timestamp(anw.get(), static_cast<int64_t>(timestamp))) != 628 NO_ERROR) { 629 ALOGE("%s: Unable to set surface timestamp, error %s (%d)", __FUNCTION__, strerror(-err), 630 err); 631 return err; 632 } 633 return NO_ERROR; 634} 635 636} // extern "C" 637 638static JNINativeMethod gCameraDeviceMethods[] = { 639 { "nativeDetectSurfaceType", 640 "(Landroid/view/Surface;)I", 641 (void *)LegacyCameraDevice_nativeDetectSurfaceType }, 642 { "nativeDetectSurfaceDimens", 643 "(Landroid/view/Surface;[I)I", 644 (void *)LegacyCameraDevice_nativeDetectSurfaceDimens }, 645 { "nativeConfigureSurface", 646 "(Landroid/view/Surface;III)I", 647 (void *)LegacyCameraDevice_nativeConfigureSurface }, 648 { "nativeProduceFrame", 649 "(Landroid/view/Surface;[BIII)I", 650 (void *)LegacyCameraDevice_nativeProduceFrame }, 651 { "nativeSetSurfaceFormat", 652 "(Landroid/view/Surface;I)I", 653 (void *)LegacyCameraDevice_nativeSetSurfaceFormat }, 654 { "nativeSetSurfaceDimens", 655 "(Landroid/view/Surface;II)I", 656 (void *)LegacyCameraDevice_nativeSetSurfaceDimens }, 657 { "nativeGetSurfaceId", 658 "(Landroid/view/Surface;)J", 659 (void *)LegacyCameraDevice_nativeGetSurfaceId }, 660 { "nativeDetectTextureDimens", 661 "(Landroid/graphics/SurfaceTexture;[I)I", 662 (void *)LegacyCameraDevice_nativeDetectTextureDimens }, 663 { "nativeSetSurfaceOrientation", 664 "(Landroid/view/Surface;II)I", 665 (void *)LegacyCameraDevice_nativeSetSurfaceOrientation }, 666 { "nativeSetNextTimestamp", 667 "(Landroid/view/Surface;J)I", 668 (void *)LegacyCameraDevice_nativeSetNextTimestamp }, 669}; 670 671// Get all the required offsets in java class and register native functions 672int register_android_hardware_camera2_legacy_LegacyCameraDevice(JNIEnv* env) 673{ 674 // Register native functions 675 return AndroidRuntime::registerNativeMethods(env, 676 CAMERA_DEVICE_CLASS_NAME, 677 gCameraDeviceMethods, 678 NELEM(gCameraDeviceMethods)); 679} 680 681