1 /* 2 * Copyright (C) 2007 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 "Surface" 18 19 #include <stdio.h> 20 21 #include "jni.h" 22 #include "JNIHelp.h" 23 #include "android_os_Parcel.h" 24 #include "android/graphics/GraphicsJNI.h" 25 26 #include "core_jni_helpers.h" 27 #include <android_runtime/android_view_Surface.h> 28 #include <android_runtime/android_graphics_SurfaceTexture.h> 29 #include <android_runtime/Log.h> 30 31 #include <binder/Parcel.h> 32 33 #include <gui/Surface.h> 34 #include <gui/SurfaceControl.h> 35 #include <gui/GLConsumer.h> 36 37 #include <ui/Rect.h> 38 #include <ui/Region.h> 39 40 #include <SkCanvas.h> 41 #include <SkBitmap.h> 42 #include <SkImage.h> 43 #include <SkRegion.h> 44 45 #include <utils/misc.h> 46 #include <utils/Log.h> 47 48 #include <ScopedUtfChars.h> 49 50 #include <AnimationContext.h> 51 #include <FrameInfo.h> 52 #include <RenderNode.h> 53 #include <renderthread/RenderProxy.h> 54 55 // ---------------------------------------------------------------------------- 56 57 namespace android { 58 59 static const char* const OutOfResourcesException = 60 "android/view/Surface$OutOfResourcesException"; 61 62 static struct { 63 jclass clazz; 64 jfieldID mNativeObject; 65 jfieldID mLock; 66 jmethodID ctor; 67 } gSurfaceClassInfo; 68 69 static struct { 70 jfieldID left; 71 jfieldID top; 72 jfieldID right; 73 jfieldID bottom; 74 } gRectClassInfo; 75 76 // ---------------------------------------------------------------------------- 77 78 // this is just a pointer we use to pass to inc/decStrong 79 static const void *sRefBaseOwner; 80 81 bool android_view_Surface_isInstanceOf(JNIEnv* env, jobject obj) { 82 return env->IsInstanceOf(obj, gSurfaceClassInfo.clazz); 83 } 84 85 sp<ANativeWindow> android_view_Surface_getNativeWindow(JNIEnv* env, jobject surfaceObj) { 86 return android_view_Surface_getSurface(env, surfaceObj); 87 } 88 89 sp<Surface> android_view_Surface_getSurface(JNIEnv* env, jobject surfaceObj) { 90 sp<Surface> sur; 91 jobject lock = env->GetObjectField(surfaceObj, 92 gSurfaceClassInfo.mLock); 93 if (env->MonitorEnter(lock) == JNI_OK) { 94 sur = reinterpret_cast<Surface *>( 95 env->GetLongField(surfaceObj, gSurfaceClassInfo.mNativeObject)); 96 env->MonitorExit(lock); 97 } 98 env->DeleteLocalRef(lock); 99 return sur; 100 } 101 102 jobject android_view_Surface_createFromIGraphicBufferProducer(JNIEnv* env, 103 const sp<IGraphicBufferProducer>& bufferProducer) { 104 if (bufferProducer == NULL) { 105 return NULL; 106 } 107 108 sp<Surface> surface(new Surface(bufferProducer, true)); 109 if (surface == NULL) { 110 return NULL; 111 } 112 113 jobject surfaceObj = env->NewObject(gSurfaceClassInfo.clazz, 114 gSurfaceClassInfo.ctor, (jlong)surface.get()); 115 if (surfaceObj == NULL) { 116 if (env->ExceptionCheck()) { 117 ALOGE("Could not create instance of Surface from IGraphicBufferProducer."); 118 LOGE_EX(env); 119 env->ExceptionClear(); 120 } 121 return NULL; 122 } 123 surface->incStrong(&sRefBaseOwner); 124 return surfaceObj; 125 } 126 127 int android_view_Surface_mapPublicFormatToHalFormat(PublicFormat f) { 128 129 switch(f) { 130 case PublicFormat::JPEG: 131 case PublicFormat::DEPTH_POINT_CLOUD: 132 return HAL_PIXEL_FORMAT_BLOB; 133 case PublicFormat::DEPTH16: 134 return HAL_PIXEL_FORMAT_Y16; 135 case PublicFormat::RAW_SENSOR: 136 return HAL_PIXEL_FORMAT_RAW16; 137 default: 138 // Most formats map 1:1 139 return static_cast<int>(f); 140 } 141 } 142 143 android_dataspace android_view_Surface_mapPublicFormatToHalDataspace( 144 PublicFormat f) { 145 switch(f) { 146 case PublicFormat::JPEG: 147 return HAL_DATASPACE_JFIF; 148 case PublicFormat::DEPTH_POINT_CLOUD: 149 case PublicFormat::DEPTH16: 150 return HAL_DATASPACE_DEPTH; 151 case PublicFormat::RAW_SENSOR: 152 case PublicFormat::RAW10: 153 return HAL_DATASPACE_ARBITRARY; 154 case PublicFormat::YUV_420_888: 155 case PublicFormat::NV21: 156 case PublicFormat::YV12: 157 return HAL_DATASPACE_JFIF; 158 default: 159 // Most formats map to UNKNOWN 160 return HAL_DATASPACE_UNKNOWN; 161 } 162 } 163 164 PublicFormat android_view_Surface_mapHalFormatDataspaceToPublicFormat( 165 int format, android_dataspace dataSpace) { 166 switch(format) { 167 case HAL_PIXEL_FORMAT_RGBA_8888: 168 case HAL_PIXEL_FORMAT_RGBX_8888: 169 case HAL_PIXEL_FORMAT_RGB_888: 170 case HAL_PIXEL_FORMAT_RGB_565: 171 case HAL_PIXEL_FORMAT_Y8: 172 case HAL_PIXEL_FORMAT_RAW10: 173 case HAL_PIXEL_FORMAT_YCbCr_420_888: 174 case HAL_PIXEL_FORMAT_YV12: 175 // Enums overlap in both name and value 176 return static_cast<PublicFormat>(format); 177 case HAL_PIXEL_FORMAT_RAW16: 178 // Name differs, though value is the same 179 return PublicFormat::RAW_SENSOR; 180 case HAL_PIXEL_FORMAT_YCbCr_422_SP: 181 // Name differs, though the value is the same 182 return PublicFormat::NV16; 183 case HAL_PIXEL_FORMAT_YCrCb_420_SP: 184 // Name differs, though the value is the same 185 return PublicFormat::NV21; 186 case HAL_PIXEL_FORMAT_YCbCr_422_I: 187 // Name differs, though the value is the same 188 return PublicFormat::YUY2; 189 case HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED: 190 // Name differs, though the value is the same 191 return PublicFormat::PRIVATE; 192 case HAL_PIXEL_FORMAT_Y16: 193 // Dataspace-dependent 194 switch (dataSpace) { 195 case HAL_DATASPACE_DEPTH: 196 return PublicFormat::DEPTH16; 197 default: 198 // Assume non-depth Y16 is just Y16. 199 return PublicFormat::Y16; 200 } 201 break; 202 case HAL_PIXEL_FORMAT_BLOB: 203 // Dataspace-dependent 204 switch (dataSpace) { 205 case HAL_DATASPACE_DEPTH: 206 return PublicFormat::DEPTH_POINT_CLOUD; 207 case HAL_DATASPACE_JFIF: 208 return PublicFormat::JPEG; 209 default: 210 // Assume otherwise-marked blobs are also JPEG 211 return PublicFormat::JPEG; 212 } 213 break; 214 case HAL_PIXEL_FORMAT_BGRA_8888: 215 case HAL_PIXEL_FORMAT_RAW_OPAQUE: 216 // Not defined in public API 217 return PublicFormat::UNKNOWN; 218 219 default: 220 return PublicFormat::UNKNOWN; 221 } 222 } 223 // ---------------------------------------------------------------------------- 224 225 static inline bool isSurfaceValid(const sp<Surface>& sur) { 226 return Surface::isValid(sur); 227 } 228 229 // ---------------------------------------------------------------------------- 230 231 static jlong nativeCreateFromSurfaceTexture(JNIEnv* env, jclass clazz, 232 jobject surfaceTextureObj) { 233 sp<IGraphicBufferProducer> producer(SurfaceTexture_getProducer(env, surfaceTextureObj)); 234 if (producer == NULL) { 235 jniThrowException(env, "java/lang/IllegalArgumentException", 236 "SurfaceTexture has already been released"); 237 return 0; 238 } 239 240 sp<Surface> surface(new Surface(producer, true)); 241 if (surface == NULL) { 242 jniThrowException(env, OutOfResourcesException, NULL); 243 return 0; 244 } 245 246 surface->incStrong(&sRefBaseOwner); 247 return jlong(surface.get()); 248 } 249 250 static void nativeRelease(JNIEnv* env, jclass clazz, jlong nativeObject) { 251 sp<Surface> sur(reinterpret_cast<Surface *>(nativeObject)); 252 sur->decStrong(&sRefBaseOwner); 253 } 254 255 static jboolean nativeIsValid(JNIEnv* env, jclass clazz, jlong nativeObject) { 256 sp<Surface> sur(reinterpret_cast<Surface *>(nativeObject)); 257 return isSurfaceValid(sur) ? JNI_TRUE : JNI_FALSE; 258 } 259 260 static jboolean nativeIsConsumerRunningBehind(JNIEnv* env, jclass clazz, jlong nativeObject) { 261 sp<Surface> sur(reinterpret_cast<Surface *>(nativeObject)); 262 if (!isSurfaceValid(sur)) { 263 doThrowIAE(env); 264 return JNI_FALSE; 265 } 266 int value = 0; 267 ANativeWindow* anw = static_cast<ANativeWindow*>(sur.get()); 268 anw->query(anw, NATIVE_WINDOW_CONSUMER_RUNNING_BEHIND, &value); 269 return value; 270 } 271 272 static inline SkColorType convertPixelFormat(PixelFormat format) { 273 /* note: if PIXEL_FORMAT_RGBX_8888 means that all alpha bytes are 0xFF, then 274 we can map to kN32_SkColorType, and optionally call 275 bitmap.setAlphaType(kOpaque_SkAlphaType) on the resulting SkBitmap 276 (as an accelerator) 277 */ 278 switch (format) { 279 case PIXEL_FORMAT_RGBX_8888: return kN32_SkColorType; 280 case PIXEL_FORMAT_RGBA_8888: return kN32_SkColorType; 281 case PIXEL_FORMAT_RGB_565: return kRGB_565_SkColorType; 282 default: return kUnknown_SkColorType; 283 } 284 } 285 286 static jlong nativeLockCanvas(JNIEnv* env, jclass clazz, 287 jlong nativeObject, jobject canvasObj, jobject dirtyRectObj) { 288 sp<Surface> surface(reinterpret_cast<Surface *>(nativeObject)); 289 290 if (!isSurfaceValid(surface)) { 291 doThrowIAE(env); 292 return 0; 293 } 294 295 Rect dirtyRect; 296 Rect* dirtyRectPtr = NULL; 297 298 if (dirtyRectObj) { 299 dirtyRect.left = env->GetIntField(dirtyRectObj, gRectClassInfo.left); 300 dirtyRect.top = env->GetIntField(dirtyRectObj, gRectClassInfo.top); 301 dirtyRect.right = env->GetIntField(dirtyRectObj, gRectClassInfo.right); 302 dirtyRect.bottom = env->GetIntField(dirtyRectObj, gRectClassInfo.bottom); 303 dirtyRectPtr = &dirtyRect; 304 } 305 306 ANativeWindow_Buffer outBuffer; 307 status_t err = surface->lock(&outBuffer, dirtyRectPtr); 308 if (err < 0) { 309 const char* const exception = (err == NO_MEMORY) ? 310 OutOfResourcesException : 311 "java/lang/IllegalArgumentException"; 312 jniThrowException(env, exception, NULL); 313 return 0; 314 } 315 316 SkImageInfo info = SkImageInfo::Make(outBuffer.width, outBuffer.height, 317 convertPixelFormat(outBuffer.format), 318 kPremul_SkAlphaType); 319 if (outBuffer.format == PIXEL_FORMAT_RGBX_8888) { 320 info.fAlphaType = kOpaque_SkAlphaType; 321 } 322 323 SkBitmap bitmap; 324 ssize_t bpr = outBuffer.stride * bytesPerPixel(outBuffer.format); 325 bitmap.setInfo(info, bpr); 326 if (outBuffer.width > 0 && outBuffer.height > 0) { 327 bitmap.setPixels(outBuffer.bits); 328 } else { 329 // be safe with an empty bitmap. 330 bitmap.setPixels(NULL); 331 } 332 333 Canvas* nativeCanvas = GraphicsJNI::getNativeCanvas(env, canvasObj); 334 nativeCanvas->setBitmap(bitmap); 335 336 if (dirtyRectPtr) { 337 nativeCanvas->clipRect(dirtyRect.left, dirtyRect.top, 338 dirtyRect.right, dirtyRect.bottom); 339 } 340 341 if (dirtyRectObj) { 342 env->SetIntField(dirtyRectObj, gRectClassInfo.left, dirtyRect.left); 343 env->SetIntField(dirtyRectObj, gRectClassInfo.top, dirtyRect.top); 344 env->SetIntField(dirtyRectObj, gRectClassInfo.right, dirtyRect.right); 345 env->SetIntField(dirtyRectObj, gRectClassInfo.bottom, dirtyRect.bottom); 346 } 347 348 // Create another reference to the surface and return it. This reference 349 // should be passed to nativeUnlockCanvasAndPost in place of mNativeObject, 350 // because the latter could be replaced while the surface is locked. 351 sp<Surface> lockedSurface(surface); 352 lockedSurface->incStrong(&sRefBaseOwner); 353 return (jlong) lockedSurface.get(); 354 } 355 356 static void nativeUnlockCanvasAndPost(JNIEnv* env, jclass clazz, 357 jlong nativeObject, jobject canvasObj) { 358 sp<Surface> surface(reinterpret_cast<Surface *>(nativeObject)); 359 if (!isSurfaceValid(surface)) { 360 return; 361 } 362 363 // detach the canvas from the surface 364 Canvas* nativeCanvas = GraphicsJNI::getNativeCanvas(env, canvasObj); 365 nativeCanvas->setBitmap(SkBitmap()); 366 367 // unlock surface 368 status_t err = surface->unlockAndPost(); 369 if (err < 0) { 370 doThrowIAE(env); 371 } 372 } 373 374 static void nativeAllocateBuffers(JNIEnv* /* env */ , jclass /* clazz */, 375 jlong nativeObject) { 376 sp<Surface> surface(reinterpret_cast<Surface *>(nativeObject)); 377 if (!isSurfaceValid(surface)) { 378 return; 379 } 380 381 surface->allocateBuffers(); 382 } 383 384 // ---------------------------------------------------------------------------- 385 386 static jlong nativeCreateFromSurfaceControl(JNIEnv* env, jclass clazz, 387 jlong surfaceControlNativeObj) { 388 /* 389 * This is used by the WindowManagerService just after constructing 390 * a Surface and is necessary for returning the Surface reference to 391 * the caller. At this point, we should only have a SurfaceControl. 392 */ 393 394 sp<SurfaceControl> ctrl(reinterpret_cast<SurfaceControl *>(surfaceControlNativeObj)); 395 sp<Surface> surface(ctrl->getSurface()); 396 if (surface != NULL) { 397 surface->incStrong(&sRefBaseOwner); 398 } 399 return reinterpret_cast<jlong>(surface.get()); 400 } 401 402 static jlong nativeReadFromParcel(JNIEnv* env, jclass clazz, 403 jlong nativeObject, jobject parcelObj) { 404 Parcel* parcel = parcelForJavaObject(env, parcelObj); 405 if (parcel == NULL) { 406 doThrowNPE(env); 407 return 0; 408 } 409 410 sp<Surface> self(reinterpret_cast<Surface *>(nativeObject)); 411 sp<IBinder> binder(parcel->readStrongBinder()); 412 413 // update the Surface only if the underlying IGraphicBufferProducer 414 // has changed. 415 if (self != NULL 416 && (IInterface::asBinder(self->getIGraphicBufferProducer()) == binder)) { 417 // same IGraphicBufferProducer, return ourselves 418 return jlong(self.get()); 419 } 420 421 sp<Surface> sur; 422 sp<IGraphicBufferProducer> gbp(interface_cast<IGraphicBufferProducer>(binder)); 423 if (gbp != NULL) { 424 // we have a new IGraphicBufferProducer, create a new Surface for it 425 sur = new Surface(gbp, true); 426 // and keep a reference before passing to java 427 sur->incStrong(&sRefBaseOwner); 428 } 429 430 if (self != NULL) { 431 // and loose the java reference to ourselves 432 self->decStrong(&sRefBaseOwner); 433 } 434 435 return jlong(sur.get()); 436 } 437 438 static void nativeWriteToParcel(JNIEnv* env, jclass clazz, 439 jlong nativeObject, jobject parcelObj) { 440 Parcel* parcel = parcelForJavaObject(env, parcelObj); 441 if (parcel == NULL) { 442 doThrowNPE(env); 443 return; 444 } 445 sp<Surface> self(reinterpret_cast<Surface *>(nativeObject)); 446 parcel->writeStrongBinder( self != 0 ? IInterface::asBinder(self->getIGraphicBufferProducer()) : NULL); 447 } 448 449 static jint nativeGetWidth(JNIEnv* env, jclass clazz, jlong nativeObject) { 450 Surface* surface = reinterpret_cast<Surface*>(nativeObject); 451 ANativeWindow* anw = static_cast<ANativeWindow*>(surface); 452 int value = 0; 453 anw->query(anw, NATIVE_WINDOW_WIDTH, &value); 454 return value; 455 } 456 457 static jint nativeGetHeight(JNIEnv* env, jclass clazz, jlong nativeObject) { 458 Surface* surface = reinterpret_cast<Surface*>(nativeObject); 459 ANativeWindow* anw = static_cast<ANativeWindow*>(surface); 460 int value = 0; 461 anw->query(anw, NATIVE_WINDOW_HEIGHT, &value); 462 return value; 463 } 464 465 namespace uirenderer { 466 467 using namespace android::uirenderer::renderthread; 468 469 class ContextFactory : public IContextFactory { 470 public: 471 virtual AnimationContext* createAnimationContext(renderthread::TimeLord& clock) { 472 return new AnimationContext(clock); 473 } 474 }; 475 476 static jlong create(JNIEnv* env, jclass clazz, jlong rootNodePtr, jlong surfacePtr) { 477 RenderNode* rootNode = reinterpret_cast<RenderNode*>(rootNodePtr); 478 sp<Surface> surface(reinterpret_cast<Surface*>(surfacePtr)); 479 ContextFactory factory; 480 RenderProxy* proxy = new RenderProxy(false, rootNode, &factory); 481 proxy->loadSystemProperties(); 482 proxy->setSwapBehavior(kSwap_discardBuffer); 483 proxy->initialize(surface); 484 // Shadows can't be used via this interface, so just set the light source 485 // to all 0s. (and width & height are unused, TODO remove them) 486 proxy->setup(0, 0, 0, 0, 0); 487 proxy->setLightCenter((Vector3){0, 0, 0}); 488 return (jlong) proxy; 489 } 490 491 static void setSurface(JNIEnv* env, jclass clazz, jlong rendererPtr, jlong surfacePtr) { 492 RenderProxy* proxy = reinterpret_cast<RenderProxy*>(rendererPtr); 493 sp<Surface> surface(reinterpret_cast<Surface*>(surfacePtr)); 494 proxy->updateSurface(surface); 495 } 496 497 static void draw(JNIEnv* env, jclass clazz, jlong rendererPtr) { 498 RenderProxy* proxy = reinterpret_cast<RenderProxy*>(rendererPtr); 499 nsecs_t vsync = systemTime(CLOCK_MONOTONIC); 500 UiFrameInfoBuilder(proxy->frameInfo()) 501 .setVsync(vsync, vsync) 502 .addFlag(FrameInfoFlags::SurfaceCanvas); 503 proxy->syncAndDrawFrame(); 504 } 505 506 static void destroy(JNIEnv* env, jclass clazz, jlong rendererPtr) { 507 RenderProxy* proxy = reinterpret_cast<RenderProxy*>(rendererPtr); 508 delete proxy; 509 } 510 511 } // uirenderer 512 513 // ---------------------------------------------------------------------------- 514 515 namespace hwui = android::uirenderer; 516 517 static JNINativeMethod gSurfaceMethods[] = { 518 {"nativeCreateFromSurfaceTexture", "(Landroid/graphics/SurfaceTexture;)J", 519 (void*)nativeCreateFromSurfaceTexture }, 520 {"nativeRelease", "(J)V", 521 (void*)nativeRelease }, 522 {"nativeIsValid", "(J)Z", 523 (void*)nativeIsValid }, 524 {"nativeIsConsumerRunningBehind", "(J)Z", 525 (void*)nativeIsConsumerRunningBehind }, 526 {"nativeLockCanvas", "(JLandroid/graphics/Canvas;Landroid/graphics/Rect;)J", 527 (void*)nativeLockCanvas }, 528 {"nativeUnlockCanvasAndPost", "(JLandroid/graphics/Canvas;)V", 529 (void*)nativeUnlockCanvasAndPost }, 530 {"nativeAllocateBuffers", "(J)V", 531 (void*)nativeAllocateBuffers }, 532 {"nativeCreateFromSurfaceControl", "(J)J", 533 (void*)nativeCreateFromSurfaceControl }, 534 {"nativeReadFromParcel", "(JLandroid/os/Parcel;)J", 535 (void*)nativeReadFromParcel }, 536 {"nativeWriteToParcel", "(JLandroid/os/Parcel;)V", 537 (void*)nativeWriteToParcel }, 538 {"nativeGetWidth", "(J)I", (void*)nativeGetWidth }, 539 {"nativeGetHeight", "(J)I", (void*)nativeGetHeight }, 540 541 // HWUI context 542 {"nHwuiCreate", "(JJ)J", (void*) hwui::create }, 543 {"nHwuiSetSurface", "(JJ)V", (void*) hwui::setSurface }, 544 {"nHwuiDraw", "(J)V", (void*) hwui::draw }, 545 {"nHwuiDestroy", "(J)V", (void*) hwui::destroy }, 546 }; 547 548 int register_android_view_Surface(JNIEnv* env) 549 { 550 int err = RegisterMethodsOrDie(env, "android/view/Surface", 551 gSurfaceMethods, NELEM(gSurfaceMethods)); 552 553 jclass clazz = FindClassOrDie(env, "android/view/Surface"); 554 gSurfaceClassInfo.clazz = MakeGlobalRefOrDie(env, clazz); 555 gSurfaceClassInfo.mNativeObject = GetFieldIDOrDie(env, 556 gSurfaceClassInfo.clazz, "mNativeObject", "J"); 557 gSurfaceClassInfo.mLock = GetFieldIDOrDie(env, 558 gSurfaceClassInfo.clazz, "mLock", "Ljava/lang/Object;"); 559 gSurfaceClassInfo.ctor = GetMethodIDOrDie(env, gSurfaceClassInfo.clazz, "<init>", "(J)V"); 560 561 clazz = FindClassOrDie(env, "android/graphics/Rect"); 562 gRectClassInfo.left = GetFieldIDOrDie(env, clazz, "left", "I"); 563 gRectClassInfo.top = GetFieldIDOrDie(env, clazz, "top", "I"); 564 gRectClassInfo.right = GetFieldIDOrDie(env, clazz, "right", "I"); 565 gRectClassInfo.bottom = GetFieldIDOrDie(env, clazz, "bottom", "I"); 566 567 return err; 568 } 569 570 }; 571