android_view_SurfaceControl.cpp revision 49d438ebdfcb7f2c202c80c820e32d1cde4bcf36
1/* 2 * Copyright (C) 2013 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 "SurfaceControl" 18 19#include "android_os_Parcel.h" 20#include "android_util_Binder.h" 21#include "android/graphics/Bitmap.h" 22#include "android/graphics/GraphicsJNI.h" 23#include "android/graphics/Region.h" 24#include "core_jni_helpers.h" 25 26#include <JNIHelp.h> 27#include <ScopedUtfChars.h> 28#include <android_runtime/android_view_Surface.h> 29#include <android_runtime/android_view_SurfaceSession.h> 30#include <gui/Surface.h> 31#include <gui/SurfaceComposerClient.h> 32#include <jni.h> 33#include <memory> 34#include <stdio.h> 35#include <ui/DisplayInfo.h> 36#include <ui/HdrCapabilities.h> 37#include <ui/FrameStats.h> 38#include <ui/Rect.h> 39#include <ui/Region.h> 40#include <utils/Log.h> 41 42// ---------------------------------------------------------------------------- 43 44namespace android { 45 46static const char* const OutOfResourcesException = 47 "android/view/Surface$OutOfResourcesException"; 48 49static struct { 50 jclass clazz; 51 jmethodID ctor; 52 jfieldID width; 53 jfieldID height; 54 jfieldID refreshRate; 55 jfieldID density; 56 jfieldID xDpi; 57 jfieldID yDpi; 58 jfieldID secure; 59 jfieldID appVsyncOffsetNanos; 60 jfieldID presentationDeadlineNanos; 61 jfieldID colorTransform; 62} gPhysicalDisplayInfoClassInfo; 63 64static struct { 65 jfieldID bottom; 66 jfieldID left; 67 jfieldID right; 68 jfieldID top; 69} gRectClassInfo; 70 71// Implements SkMallocPixelRef::ReleaseProc, to delete the screenshot on unref. 72void DeleteScreenshot(void* addr, void* context) { 73 SkASSERT(addr == ((ScreenshotClient*) context)->getPixels()); 74 delete ((ScreenshotClient*) context); 75} 76 77static struct { 78 nsecs_t UNDEFINED_TIME_NANO; 79 jmethodID init; 80} gWindowContentFrameStatsClassInfo; 81 82static struct { 83 nsecs_t UNDEFINED_TIME_NANO; 84 jmethodID init; 85} gWindowAnimationFrameStatsClassInfo; 86 87static struct { 88 jclass clazz; 89 jmethodID ctor; 90} gHdrCapabilitiesClassInfo; 91 92// ---------------------------------------------------------------------------- 93 94static jlong nativeCreate(JNIEnv* env, jclass clazz, jobject sessionObj, 95 jstring nameStr, jint w, jint h, jint format, jint flags) { 96 ScopedUtfChars name(env, nameStr); 97 sp<SurfaceComposerClient> client(android_view_SurfaceSession_getClient(env, sessionObj)); 98 sp<SurfaceControl> surface = client->createSurface( 99 String8(name.c_str()), w, h, format, flags); 100 if (surface == NULL) { 101 jniThrowException(env, OutOfResourcesException, NULL); 102 return 0; 103 } 104 surface->incStrong((void *)nativeCreate); 105 return reinterpret_cast<jlong>(surface.get()); 106} 107 108static void nativeRelease(JNIEnv* env, jclass clazz, jlong nativeObject) { 109 sp<SurfaceControl> ctrl(reinterpret_cast<SurfaceControl *>(nativeObject)); 110 ctrl->decStrong((void *)nativeCreate); 111} 112 113static void nativeDestroy(JNIEnv* env, jclass clazz, jlong nativeObject) { 114 sp<SurfaceControl> ctrl(reinterpret_cast<SurfaceControl *>(nativeObject)); 115 ctrl->clear(); 116 ctrl->decStrong((void *)nativeCreate); 117} 118 119static void nativeDisconnect(JNIEnv* env, jclass clazz, jlong nativeObject) { 120 SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject); 121 if (ctrl != NULL) { 122 ctrl->disconnect(); 123 } 124} 125 126static jobject nativeScreenshotBitmap(JNIEnv* env, jclass clazz, 127 jobject displayTokenObj, jobject sourceCropObj, jint width, jint height, 128 jint minLayer, jint maxLayer, bool allLayers, bool useIdentityTransform, 129 int rotation) { 130 sp<IBinder> displayToken = ibinderForJavaObject(env, displayTokenObj); 131 if (displayToken == NULL) { 132 return NULL; 133 } 134 135 int left = env->GetIntField(sourceCropObj, gRectClassInfo.left); 136 int top = env->GetIntField(sourceCropObj, gRectClassInfo.top); 137 int right = env->GetIntField(sourceCropObj, gRectClassInfo.right); 138 int bottom = env->GetIntField(sourceCropObj, gRectClassInfo.bottom); 139 Rect sourceCrop(left, top, right, bottom); 140 141 std::unique_ptr<ScreenshotClient> screenshot(new ScreenshotClient()); 142 status_t res; 143 if (allLayers) { 144 minLayer = 0; 145 maxLayer = -1; 146 } 147 148 res = screenshot->update(displayToken, sourceCrop, width, height, 149 minLayer, maxLayer, useIdentityTransform, static_cast<uint32_t>(rotation)); 150 if (res != NO_ERROR) { 151 return NULL; 152 } 153 154 SkColorType colorType; 155 SkAlphaType alphaType; 156 switch (screenshot->getFormat()) { 157 case PIXEL_FORMAT_RGBX_8888: { 158 colorType = kRGBA_8888_SkColorType; 159 alphaType = kOpaque_SkAlphaType; 160 break; 161 } 162 case PIXEL_FORMAT_RGBA_8888: { 163 colorType = kRGBA_8888_SkColorType; 164 alphaType = kPremul_SkAlphaType; 165 break; 166 } 167 case PIXEL_FORMAT_RGB_565: { 168 colorType = kRGB_565_SkColorType; 169 alphaType = kOpaque_SkAlphaType; 170 break; 171 } 172 default: { 173 return NULL; 174 } 175 } 176 SkImageInfo screenshotInfo = SkImageInfo::Make(screenshot->getWidth(), 177 screenshot->getHeight(), 178 colorType, alphaType); 179 180 const size_t rowBytes = 181 screenshot->getStride() * android::bytesPerPixel(screenshot->getFormat()); 182 183 if (!screenshotInfo.width() || !screenshotInfo.height()) { 184 return NULL; 185 } 186 187 Bitmap* bitmap = new Bitmap( 188 (void*) screenshot->getPixels(), (void*) screenshot.get(), DeleteScreenshot, 189 screenshotInfo, rowBytes, nullptr); 190 screenshot.release(); 191 bitmap->peekAtPixelRef()->setImmutable(); 192 193 return GraphicsJNI::createBitmap(env, bitmap, 194 GraphicsJNI::kBitmapCreateFlag_Premultiplied, NULL); 195} 196 197static void nativeScreenshot(JNIEnv* env, jclass clazz, jobject displayTokenObj, 198 jobject surfaceObj, jobject sourceCropObj, jint width, jint height, 199 jint minLayer, jint maxLayer, bool allLayers, bool useIdentityTransform) { 200 sp<IBinder> displayToken = ibinderForJavaObject(env, displayTokenObj); 201 if (displayToken != NULL) { 202 sp<Surface> consumer = android_view_Surface_getSurface(env, surfaceObj); 203 if (consumer != NULL) { 204 int left = env->GetIntField(sourceCropObj, gRectClassInfo.left); 205 int top = env->GetIntField(sourceCropObj, gRectClassInfo.top); 206 int right = env->GetIntField(sourceCropObj, gRectClassInfo.right); 207 int bottom = env->GetIntField(sourceCropObj, gRectClassInfo.bottom); 208 Rect sourceCrop(left, top, right, bottom); 209 210 if (allLayers) { 211 minLayer = 0; 212 maxLayer = -1; 213 } 214 ScreenshotClient::capture(displayToken, 215 consumer->getIGraphicBufferProducer(), sourceCrop, 216 width, height, uint32_t(minLayer), uint32_t(maxLayer), 217 useIdentityTransform); 218 } 219 } 220} 221 222static void nativeOpenTransaction(JNIEnv* env, jclass clazz) { 223 SurfaceComposerClient::openGlobalTransaction(); 224} 225 226static void nativeCloseTransaction(JNIEnv* env, jclass clazz) { 227 SurfaceComposerClient::closeGlobalTransaction(); 228} 229 230static void nativeSetAnimationTransaction(JNIEnv* env, jclass clazz) { 231 SurfaceComposerClient::setAnimationTransaction(); 232} 233 234static void nativeSetLayer(JNIEnv* env, jclass clazz, jlong nativeObject, jint zorder) { 235 SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject); 236 status_t err = ctrl->setLayer(zorder); 237 if (err < 0 && err != NO_INIT) { 238 doThrowIAE(env); 239 } 240} 241 242static void nativeSetPosition(JNIEnv* env, jclass clazz, jlong nativeObject, jfloat x, jfloat y) { 243 SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject); 244 status_t err = ctrl->setPosition(x, y); 245 if (err < 0 && err != NO_INIT) { 246 doThrowIAE(env); 247 } 248} 249 250static void nativeSetSize(JNIEnv* env, jclass clazz, jlong nativeObject, jint w, jint h) { 251 SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject); 252 status_t err = ctrl->setSize(w, h); 253 if (err < 0 && err != NO_INIT) { 254 doThrowIAE(env); 255 } 256} 257 258static void nativeSetFlags(JNIEnv* env, jclass clazz, jlong nativeObject, jint flags, jint mask) { 259 SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject); 260 status_t err = ctrl->setFlags(flags, mask); 261 if (err < 0 && err != NO_INIT) { 262 doThrowIAE(env); 263 } 264} 265 266static void nativeSetTransparentRegionHint(JNIEnv* env, jclass clazz, jlong nativeObject, jobject regionObj) { 267 SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject); 268 SkRegion* region = android_graphics_Region_getSkRegion(env, regionObj); 269 if (!region) { 270 doThrowIAE(env); 271 return; 272 } 273 274 const SkIRect& b(region->getBounds()); 275 Region reg(Rect(b.fLeft, b.fTop, b.fRight, b.fBottom)); 276 if (region->isComplex()) { 277 SkRegion::Iterator it(*region); 278 while (!it.done()) { 279 const SkIRect& r(it.rect()); 280 reg.addRectUnchecked(r.fLeft, r.fTop, r.fRight, r.fBottom); 281 it.next(); 282 } 283 } 284 285 status_t err = ctrl->setTransparentRegionHint(reg); 286 if (err < 0 && err != NO_INIT) { 287 doThrowIAE(env); 288 } 289} 290 291static void nativeSetAlpha(JNIEnv* env, jclass clazz, jlong nativeObject, jfloat alpha) { 292 SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject); 293 status_t err = ctrl->setAlpha(alpha); 294 if (err < 0 && err != NO_INIT) { 295 doThrowIAE(env); 296 } 297} 298 299static void nativeSetMatrix(JNIEnv* env, jclass clazz, jlong nativeObject, 300 jfloat dsdx, jfloat dtdx, jfloat dsdy, jfloat dtdy) { 301 SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject); 302 status_t err = ctrl->setMatrix(dsdx, dtdx, dsdy, dtdy); 303 if (err < 0 && err != NO_INIT) { 304 doThrowIAE(env); 305 } 306} 307 308static void nativeSetWindowCrop(JNIEnv* env, jclass clazz, jlong nativeObject, 309 jint l, jint t, jint r, jint b) { 310 SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject); 311 Rect crop(l, t, r, b); 312 status_t err = ctrl->setCrop(crop); 313 if (err < 0 && err != NO_INIT) { 314 doThrowIAE(env); 315 } 316} 317 318static void nativeSetFinalCrop(JNIEnv* env, jclass clazz, jlong nativeObject, 319 jint l, jint t, jint r, jint b) { 320 SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject); 321 Rect crop(l, t, r, b); 322 status_t err = ctrl->setFinalCrop(crop); 323 if (err < 0 && err != NO_INIT) { 324 doThrowIAE(env); 325 } 326} 327 328static void nativeSetLayerStack(JNIEnv* env, jclass clazz, jlong nativeObject, jint layerStack) { 329 SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject); 330 status_t err = ctrl->setLayerStack(layerStack); 331 if (err < 0 && err != NO_INIT) { 332 doThrowIAE(env); 333 } 334} 335 336static jobject nativeGetBuiltInDisplay(JNIEnv* env, jclass clazz, jint id) { 337 sp<IBinder> token(SurfaceComposerClient::getBuiltInDisplay(id)); 338 return javaObjectForIBinder(env, token); 339} 340 341static jobject nativeCreateDisplay(JNIEnv* env, jclass clazz, jstring nameObj, 342 jboolean secure) { 343 ScopedUtfChars name(env, nameObj); 344 sp<IBinder> token(SurfaceComposerClient::createDisplay( 345 String8(name.c_str()), bool(secure))); 346 return javaObjectForIBinder(env, token); 347} 348 349static void nativeDestroyDisplay(JNIEnv* env, jclass clazz, jobject tokenObj) { 350 sp<IBinder> token(ibinderForJavaObject(env, tokenObj)); 351 if (token == NULL) return; 352 SurfaceComposerClient::destroyDisplay(token); 353} 354 355static void nativeSetDisplaySurface(JNIEnv* env, jclass clazz, 356 jobject tokenObj, jlong nativeSurfaceObject) { 357 sp<IBinder> token(ibinderForJavaObject(env, tokenObj)); 358 if (token == NULL) return; 359 sp<IGraphicBufferProducer> bufferProducer; 360 sp<Surface> sur(reinterpret_cast<Surface *>(nativeSurfaceObject)); 361 if (sur != NULL) { 362 bufferProducer = sur->getIGraphicBufferProducer(); 363 } 364 SurfaceComposerClient::setDisplaySurface(token, bufferProducer); 365} 366 367static void nativeSetDisplayLayerStack(JNIEnv* env, jclass clazz, 368 jobject tokenObj, jint layerStack) { 369 sp<IBinder> token(ibinderForJavaObject(env, tokenObj)); 370 if (token == NULL) return; 371 372 SurfaceComposerClient::setDisplayLayerStack(token, layerStack); 373} 374 375static void nativeSetDisplayProjection(JNIEnv* env, jclass clazz, 376 jobject tokenObj, jint orientation, 377 jint layerStackRect_left, jint layerStackRect_top, jint layerStackRect_right, jint layerStackRect_bottom, 378 jint displayRect_left, jint displayRect_top, jint displayRect_right, jint displayRect_bottom) { 379 sp<IBinder> token(ibinderForJavaObject(env, tokenObj)); 380 if (token == NULL) return; 381 Rect layerStackRect(layerStackRect_left, layerStackRect_top, layerStackRect_right, layerStackRect_bottom); 382 Rect displayRect(displayRect_left, displayRect_top, displayRect_right, displayRect_bottom); 383 SurfaceComposerClient::setDisplayProjection(token, orientation, layerStackRect, displayRect); 384} 385 386static void nativeSetDisplaySize(JNIEnv* env, jclass clazz, 387 jobject tokenObj, jint width, jint height) { 388 sp<IBinder> token(ibinderForJavaObject(env, tokenObj)); 389 if (token == NULL) return; 390 SurfaceComposerClient::setDisplaySize(token, width, height); 391} 392 393static jobjectArray nativeGetDisplayConfigs(JNIEnv* env, jclass clazz, 394 jobject tokenObj) { 395 sp<IBinder> token(ibinderForJavaObject(env, tokenObj)); 396 if (token == NULL) return NULL; 397 398 Vector<DisplayInfo> configs; 399 if (SurfaceComposerClient::getDisplayConfigs(token, &configs) != NO_ERROR || 400 configs.size() == 0) { 401 return NULL; 402 } 403 404 jobjectArray configArray = env->NewObjectArray(configs.size(), 405 gPhysicalDisplayInfoClassInfo.clazz, NULL); 406 407 for (size_t c = 0; c < configs.size(); ++c) { 408 const DisplayInfo& info = configs[c]; 409 jobject infoObj = env->NewObject(gPhysicalDisplayInfoClassInfo.clazz, 410 gPhysicalDisplayInfoClassInfo.ctor); 411 env->SetIntField(infoObj, gPhysicalDisplayInfoClassInfo.width, info.w); 412 env->SetIntField(infoObj, gPhysicalDisplayInfoClassInfo.height, info.h); 413 env->SetFloatField(infoObj, gPhysicalDisplayInfoClassInfo.refreshRate, info.fps); 414 env->SetFloatField(infoObj, gPhysicalDisplayInfoClassInfo.density, info.density); 415 env->SetFloatField(infoObj, gPhysicalDisplayInfoClassInfo.xDpi, info.xdpi); 416 env->SetFloatField(infoObj, gPhysicalDisplayInfoClassInfo.yDpi, info.ydpi); 417 env->SetBooleanField(infoObj, gPhysicalDisplayInfoClassInfo.secure, info.secure); 418 env->SetLongField(infoObj, gPhysicalDisplayInfoClassInfo.appVsyncOffsetNanos, 419 info.appVsyncOffset); 420 env->SetLongField(infoObj, gPhysicalDisplayInfoClassInfo.presentationDeadlineNanos, 421 info.presentationDeadline); 422 env->SetIntField(infoObj, gPhysicalDisplayInfoClassInfo.colorTransform, 423 info.colorTransform); 424 env->SetObjectArrayElement(configArray, static_cast<jsize>(c), infoObj); 425 env->DeleteLocalRef(infoObj); 426 } 427 428 return configArray; 429} 430 431static jint nativeGetActiveConfig(JNIEnv* env, jclass clazz, jobject tokenObj) { 432 sp<IBinder> token(ibinderForJavaObject(env, tokenObj)); 433 if (token == NULL) return -1; 434 return static_cast<jint>(SurfaceComposerClient::getActiveConfig(token)); 435} 436 437static jboolean nativeSetActiveConfig(JNIEnv* env, jclass clazz, jobject tokenObj, jint id) { 438 sp<IBinder> token(ibinderForJavaObject(env, tokenObj)); 439 if (token == NULL) return JNI_FALSE; 440 status_t err = SurfaceComposerClient::setActiveConfig(token, static_cast<int>(id)); 441 return err == NO_ERROR ? JNI_TRUE : JNI_FALSE; 442} 443 444static void nativeSetDisplayPowerMode(JNIEnv* env, jclass clazz, jobject tokenObj, jint mode) { 445 sp<IBinder> token(ibinderForJavaObject(env, tokenObj)); 446 if (token == NULL) return; 447 448 ALOGD_IF_SLOW(100, "Excessive delay in setPowerMode()"); 449 SurfaceComposerClient::setDisplayPowerMode(token, mode); 450} 451 452static jboolean nativeClearContentFrameStats(JNIEnv* env, jclass clazz, jlong nativeObject) { 453 SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject); 454 status_t err = ctrl->clearLayerFrameStats(); 455 456 if (err < 0 && err != NO_INIT) { 457 doThrowIAE(env); 458 } 459 460 // The other end is not ready, just report we failed. 461 if (err == NO_INIT) { 462 return JNI_FALSE; 463 } 464 465 return JNI_TRUE; 466} 467 468static jboolean nativeGetContentFrameStats(JNIEnv* env, jclass clazz, jlong nativeObject, 469 jobject outStats) { 470 FrameStats stats; 471 472 SurfaceControl* const ctrl = reinterpret_cast<SurfaceControl *>(nativeObject); 473 status_t err = ctrl->getLayerFrameStats(&stats); 474 if (err < 0 && err != NO_INIT) { 475 doThrowIAE(env); 476 } 477 478 // The other end is not ready, fine just return empty stats. 479 if (err == NO_INIT) { 480 return JNI_FALSE; 481 } 482 483 jlong refreshPeriodNano = static_cast<jlong>(stats.refreshPeriodNano); 484 size_t frameCount = stats.desiredPresentTimesNano.size(); 485 486 jlongArray postedTimesNanoDst = env->NewLongArray(frameCount); 487 if (postedTimesNanoDst == NULL) { 488 return JNI_FALSE; 489 } 490 491 jlongArray presentedTimesNanoDst = env->NewLongArray(frameCount); 492 if (presentedTimesNanoDst == NULL) { 493 return JNI_FALSE; 494 } 495 496 jlongArray readyTimesNanoDst = env->NewLongArray(frameCount); 497 if (readyTimesNanoDst == NULL) { 498 return JNI_FALSE; 499 } 500 501 nsecs_t postedTimesNanoSrc[frameCount]; 502 nsecs_t presentedTimesNanoSrc[frameCount]; 503 nsecs_t readyTimesNanoSrc[frameCount]; 504 505 for (size_t i = 0; i < frameCount; i++) { 506 nsecs_t postedTimeNano = stats.desiredPresentTimesNano[i]; 507 if (postedTimeNano == INT64_MAX) { 508 postedTimeNano = gWindowContentFrameStatsClassInfo.UNDEFINED_TIME_NANO; 509 } 510 postedTimesNanoSrc[i] = postedTimeNano; 511 512 nsecs_t presentedTimeNano = stats.actualPresentTimesNano[i]; 513 if (presentedTimeNano == INT64_MAX) { 514 presentedTimeNano = gWindowContentFrameStatsClassInfo.UNDEFINED_TIME_NANO; 515 } 516 presentedTimesNanoSrc[i] = presentedTimeNano; 517 518 nsecs_t readyTimeNano = stats.frameReadyTimesNano[i]; 519 if (readyTimeNano == INT64_MAX) { 520 readyTimeNano = gWindowContentFrameStatsClassInfo.UNDEFINED_TIME_NANO; 521 } 522 readyTimesNanoSrc[i] = readyTimeNano; 523 } 524 525 env->SetLongArrayRegion(postedTimesNanoDst, 0, frameCount, postedTimesNanoSrc); 526 env->SetLongArrayRegion(presentedTimesNanoDst, 0, frameCount, presentedTimesNanoSrc); 527 env->SetLongArrayRegion(readyTimesNanoDst, 0, frameCount, readyTimesNanoSrc); 528 529 env->CallVoidMethod(outStats, gWindowContentFrameStatsClassInfo.init, refreshPeriodNano, 530 postedTimesNanoDst, presentedTimesNanoDst, readyTimesNanoDst); 531 532 if (env->ExceptionCheck()) { 533 return JNI_FALSE; 534 } 535 536 return JNI_TRUE; 537} 538 539static jboolean nativeClearAnimationFrameStats(JNIEnv* env, jclass clazz) { 540 status_t err = SurfaceComposerClient::clearAnimationFrameStats(); 541 542 if (err < 0 && err != NO_INIT) { 543 doThrowIAE(env); 544 } 545 546 // The other end is not ready, just report we failed. 547 if (err == NO_INIT) { 548 return JNI_FALSE; 549 } 550 551 return JNI_TRUE; 552} 553 554static jboolean nativeGetAnimationFrameStats(JNIEnv* env, jclass clazz, jobject outStats) { 555 FrameStats stats; 556 557 status_t err = SurfaceComposerClient::getAnimationFrameStats(&stats); 558 if (err < 0 && err != NO_INIT) { 559 doThrowIAE(env); 560 } 561 562 // The other end is not ready, fine just return empty stats. 563 if (err == NO_INIT) { 564 return JNI_FALSE; 565 } 566 567 jlong refreshPeriodNano = static_cast<jlong>(stats.refreshPeriodNano); 568 size_t frameCount = stats.desiredPresentTimesNano.size(); 569 570 jlongArray presentedTimesNanoDst = env->NewLongArray(frameCount); 571 if (presentedTimesNanoDst == NULL) { 572 return JNI_FALSE; 573 } 574 575 nsecs_t presentedTimesNanoSrc[frameCount]; 576 577 for (size_t i = 0; i < frameCount; i++) { 578 nsecs_t presentedTimeNano = stats.actualPresentTimesNano[i]; 579 if (presentedTimeNano == INT64_MAX) { 580 presentedTimeNano = gWindowContentFrameStatsClassInfo.UNDEFINED_TIME_NANO; 581 } 582 presentedTimesNanoSrc[i] = presentedTimeNano; 583 } 584 585 env->SetLongArrayRegion(presentedTimesNanoDst, 0, frameCount, presentedTimesNanoSrc); 586 587 env->CallVoidMethod(outStats, gWindowAnimationFrameStatsClassInfo.init, refreshPeriodNano, 588 presentedTimesNanoDst); 589 590 if (env->ExceptionCheck()) { 591 return JNI_FALSE; 592 } 593 594 return JNI_TRUE; 595} 596 597 598static void nativeDeferTransactionUntil(JNIEnv* env, jclass clazz, jlong nativeObject, 599 jobject handleObject, jlong frameNumber) { 600 auto ctrl = reinterpret_cast<SurfaceControl *>(nativeObject); 601 sp<IBinder> handle = ibinderForJavaObject(env, handleObject); 602 603 ctrl->deferTransactionUntil(handle, frameNumber); 604} 605 606static jobject nativeGetHandle(JNIEnv* env, jclass clazz, jlong nativeObject) { 607 auto ctrl = reinterpret_cast<SurfaceControl *>(nativeObject); 608 609 return javaObjectForIBinder(env, ctrl->getHandle()); 610} 611 612static jobject nativeGetHdrCapabilities(JNIEnv* env, jclass clazz, jobject tokenObject) { 613 sp<IBinder> token(ibinderForJavaObject(env, tokenObject)); 614 if (token == NULL) return NULL; 615 616 HdrCapabilities capabilities; 617 SurfaceComposerClient::getHdrCapabilities(token, &capabilities); 618 619 const auto& types = capabilities.getSupportedHdrTypes(); 620 auto typesArray = env->NewIntArray(types.size()); 621 env->SetIntArrayRegion(typesArray, 0, types.size(), types.data()); 622 623 return env->NewObject(gHdrCapabilitiesClassInfo.clazz, gPhysicalDisplayInfoClassInfo.ctor, 624 typesArray, capabilities.getDesiredMaxLuminance(), 625 capabilities.getDesiredMaxAverageLuminance(), capabilities.getDesiredMinLuminance()); 626} 627 628// ---------------------------------------------------------------------------- 629 630static const JNINativeMethod sSurfaceControlMethods[] = { 631 {"nativeCreate", "(Landroid/view/SurfaceSession;Ljava/lang/String;IIII)J", 632 (void*)nativeCreate }, 633 {"nativeRelease", "(J)V", 634 (void*)nativeRelease }, 635 {"nativeDestroy", "(J)V", 636 (void*)nativeDestroy }, 637 {"nativeDisconnect", "(J)V", 638 (void*)nativeDisconnect }, 639 {"nativeScreenshot", "(Landroid/os/IBinder;Landroid/graphics/Rect;IIIIZZI)Landroid/graphics/Bitmap;", 640 (void*)nativeScreenshotBitmap }, 641 {"nativeScreenshot", "(Landroid/os/IBinder;Landroid/view/Surface;Landroid/graphics/Rect;IIIIZZ)V", 642 (void*)nativeScreenshot }, 643 {"nativeOpenTransaction", "()V", 644 (void*)nativeOpenTransaction }, 645 {"nativeCloseTransaction", "()V", 646 (void*)nativeCloseTransaction }, 647 {"nativeSetAnimationTransaction", "()V", 648 (void*)nativeSetAnimationTransaction }, 649 {"nativeSetLayer", "(JI)V", 650 (void*)nativeSetLayer }, 651 {"nativeSetPosition", "(JFF)V", 652 (void*)nativeSetPosition }, 653 {"nativeSetSize", "(JII)V", 654 (void*)nativeSetSize }, 655 {"nativeSetTransparentRegionHint", "(JLandroid/graphics/Region;)V", 656 (void*)nativeSetTransparentRegionHint }, 657 {"nativeSetAlpha", "(JF)V", 658 (void*)nativeSetAlpha }, 659 {"nativeSetMatrix", "(JFFFF)V", 660 (void*)nativeSetMatrix }, 661 {"nativeSetFlags", "(JII)V", 662 (void*)nativeSetFlags }, 663 {"nativeSetWindowCrop", "(JIIII)V", 664 (void*)nativeSetWindowCrop }, 665 {"nativeSetFinalCrop", "(JIIII)V", 666 (void*)nativeSetFinalCrop }, 667 {"nativeSetLayerStack", "(JI)V", 668 (void*)nativeSetLayerStack }, 669 {"nativeGetBuiltInDisplay", "(I)Landroid/os/IBinder;", 670 (void*)nativeGetBuiltInDisplay }, 671 {"nativeCreateDisplay", "(Ljava/lang/String;Z)Landroid/os/IBinder;", 672 (void*)nativeCreateDisplay }, 673 {"nativeDestroyDisplay", "(Landroid/os/IBinder;)V", 674 (void*)nativeDestroyDisplay }, 675 {"nativeSetDisplaySurface", "(Landroid/os/IBinder;J)V", 676 (void*)nativeSetDisplaySurface }, 677 {"nativeSetDisplayLayerStack", "(Landroid/os/IBinder;I)V", 678 (void*)nativeSetDisplayLayerStack }, 679 {"nativeSetDisplayProjection", "(Landroid/os/IBinder;IIIIIIIII)V", 680 (void*)nativeSetDisplayProjection }, 681 {"nativeSetDisplaySize", "(Landroid/os/IBinder;II)V", 682 (void*)nativeSetDisplaySize }, 683 {"nativeGetDisplayConfigs", "(Landroid/os/IBinder;)[Landroid/view/SurfaceControl$PhysicalDisplayInfo;", 684 (void*)nativeGetDisplayConfigs }, 685 {"nativeGetActiveConfig", "(Landroid/os/IBinder;)I", 686 (void*)nativeGetActiveConfig }, 687 {"nativeSetActiveConfig", "(Landroid/os/IBinder;I)Z", 688 (void*)nativeSetActiveConfig }, 689 {"nativeClearContentFrameStats", "(J)Z", 690 (void*)nativeClearContentFrameStats }, 691 {"nativeGetContentFrameStats", "(JLandroid/view/WindowContentFrameStats;)Z", 692 (void*)nativeGetContentFrameStats }, 693 {"nativeClearAnimationFrameStats", "()Z", 694 (void*)nativeClearAnimationFrameStats }, 695 {"nativeGetAnimationFrameStats", "(Landroid/view/WindowAnimationFrameStats;)Z", 696 (void*)nativeGetAnimationFrameStats }, 697 {"nativeSetDisplayPowerMode", "(Landroid/os/IBinder;I)V", 698 (void*)nativeSetDisplayPowerMode }, 699 {"nativeDeferTransactionUntil", "(JLandroid/os/IBinder;J)V", 700 (void*)nativeDeferTransactionUntil }, 701 {"nativeGetHandle", "(J)Landroid/os/IBinder;", 702 (void*)nativeGetHandle } 703}; 704 705int register_android_view_SurfaceControl(JNIEnv* env) 706{ 707 int err = RegisterMethodsOrDie(env, "android/view/SurfaceControl", 708 sSurfaceControlMethods, NELEM(sSurfaceControlMethods)); 709 710 jclass clazz = FindClassOrDie(env, "android/view/SurfaceControl$PhysicalDisplayInfo"); 711 gPhysicalDisplayInfoClassInfo.clazz = MakeGlobalRefOrDie(env, clazz); 712 gPhysicalDisplayInfoClassInfo.ctor = GetMethodIDOrDie(env, 713 gPhysicalDisplayInfoClassInfo.clazz, "<init>", "()V"); 714 gPhysicalDisplayInfoClassInfo.width = GetFieldIDOrDie(env, clazz, "width", "I"); 715 gPhysicalDisplayInfoClassInfo.height = GetFieldIDOrDie(env, clazz, "height", "I"); 716 gPhysicalDisplayInfoClassInfo.refreshRate = GetFieldIDOrDie(env, clazz, "refreshRate", "F"); 717 gPhysicalDisplayInfoClassInfo.density = GetFieldIDOrDie(env, clazz, "density", "F"); 718 gPhysicalDisplayInfoClassInfo.xDpi = GetFieldIDOrDie(env, clazz, "xDpi", "F"); 719 gPhysicalDisplayInfoClassInfo.yDpi = GetFieldIDOrDie(env, clazz, "yDpi", "F"); 720 gPhysicalDisplayInfoClassInfo.secure = GetFieldIDOrDie(env, clazz, "secure", "Z"); 721 gPhysicalDisplayInfoClassInfo.appVsyncOffsetNanos = GetFieldIDOrDie(env, 722 clazz, "appVsyncOffsetNanos", "J"); 723 gPhysicalDisplayInfoClassInfo.presentationDeadlineNanos = GetFieldIDOrDie(env, 724 clazz, "presentationDeadlineNanos", "J"); 725 gPhysicalDisplayInfoClassInfo.colorTransform = GetFieldIDOrDie(env, clazz, 726 "colorTransform", "I"); 727 728 jclass rectClazz = FindClassOrDie(env, "android/graphics/Rect"); 729 gRectClassInfo.bottom = GetFieldIDOrDie(env, rectClazz, "bottom", "I"); 730 gRectClassInfo.left = GetFieldIDOrDie(env, rectClazz, "left", "I"); 731 gRectClassInfo.right = GetFieldIDOrDie(env, rectClazz, "right", "I"); 732 gRectClassInfo.top = GetFieldIDOrDie(env, rectClazz, "top", "I"); 733 734 jclass frameStatsClazz = FindClassOrDie(env, "android/view/FrameStats"); 735 jfieldID undefined_time_nano_field = GetStaticFieldIDOrDie(env, 736 frameStatsClazz, "UNDEFINED_TIME_NANO", "J"); 737 nsecs_t undefined_time_nano = env->GetStaticLongField(frameStatsClazz, undefined_time_nano_field); 738 739 jclass contFrameStatsClazz = FindClassOrDie(env, "android/view/WindowContentFrameStats"); 740 gWindowContentFrameStatsClassInfo.init = GetMethodIDOrDie(env, 741 contFrameStatsClazz, "init", "(J[J[J[J)V"); 742 gWindowContentFrameStatsClassInfo.UNDEFINED_TIME_NANO = undefined_time_nano; 743 744 jclass animFrameStatsClazz = FindClassOrDie(env, "android/view/WindowAnimationFrameStats"); 745 gWindowAnimationFrameStatsClassInfo.init = GetMethodIDOrDie(env, 746 animFrameStatsClazz, "init", "(J[J)V"); 747 gWindowAnimationFrameStatsClassInfo.UNDEFINED_TIME_NANO = undefined_time_nano; 748 749 jclass hdrCapabilitiesClazz = FindClassOrDie(env, "android/view/Display$HdrCapabilities"); 750 gHdrCapabilitiesClassInfo.clazz = MakeGlobalRefOrDie(env, hdrCapabilitiesClazz); 751 gHdrCapabilitiesClassInfo.ctor = GetMethodIDOrDie(env, hdrCapabilitiesClazz, "<init>", 752 "([IFFF)V"); 753 754 return err; 755} 756 757}; 758