| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.view; |
| 18 |
|
| 19 |
import android.annotation.NonNull; |
| 20 |
import android.annotation.Nullable; |
| 21 |
import android.app.ActivityManager; |
| 22 |
import android.content.Context; |
| 23 |
import android.content.res.TypedArray; |
| 24 |
import android.graphics.BLASTBufferQueue; |
| 25 |
import android.graphics.FrameInfo; |
| 26 |
import android.graphics.HardwareRenderer; |
| 27 |
import android.graphics.Picture; |
| 28 |
import android.graphics.RecordingCanvas; |
| 29 |
import android.graphics.Rect; |
| 30 |
import android.graphics.RenderNode; |
| 31 |
import android.os.Trace; |
| 32 |
import android.util.DisplayMetrics; |
| 33 |
import android.util.Log; |
| 34 |
import android.view.Surface.OutOfResourcesException; |
| 35 |
import android.view.View.AttachInfo; |
| 36 |
import android.view.animation.AnimationUtils; |
| 37 |
|
| 38 |
import com.android.internal.R; |
| 39 |
|
| 40 |
import java.io.FileDescriptor; |
| 41 |
import java.io.PrintWriter; |
| 42 |
import java.util.ArrayList; |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
public final class ThreadedRenderer extends HardwareRenderer { |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
public static final String PROFILE_PROPERTY = "debug.hwui.profile"; |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
public static final String PROFILE_PROPERTY_VISUALIZE_BARS = "visual_bars"; |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
static final String PROFILE_MAXFRAMES_PROPERTY = "debug.hwui.profile.maxframes"; |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
static final String PRINT_CONFIG_PROPERTY = "debug.hwui.print_config"; |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
public static final String DEBUG_DIRTY_REGIONS_PROPERTY = "debug.hwui.show_dirty_regions"; |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
public static final String DEBUG_SHOW_LAYERS_UPDATES_PROPERTY = |
| 135 |
"debug.hwui.show_layers_updates"; |
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
public static final String DEBUG_OVERDRAW_PROPERTY = "debug.hwui.overdraw"; |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
public static final String OVERDRAW_PROPERTY_SHOW = "show"; |
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
public static final String DEBUG_SHOW_NON_RECTANGULAR_CLIP_PROPERTY = |
| 168 |
"debug.hwui.show_non_rect_clip"; |
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
public static final String DEBUG_FPS_DIVISOR = "debug.hwui.fps_divisor"; |
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
public static final String DEBUG_FORCE_DARK = "debug.hwui.force_dark"; |
| 186 |
|
| 187 |
public static int EGL_CONTEXT_PRIORITY_REALTIME_NV = 0x3357; |
| 188 |
public static int EGL_CONTEXT_PRIORITY_HIGH_IMG = 0x3101; |
| 189 |
public static int EGL_CONTEXT_PRIORITY_MEDIUM_IMG = 0x3102; |
| 190 |
public static int EGL_CONTEXT_PRIORITY_LOW_IMG = 0x3103; |
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
public static boolean sRendererEnabled = true; |
| 198 |
|
| 199 |
public static boolean sTrimForeground = false; |
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
public static void enableForegroundTrimming() { |
| 208 |
sTrimForeground = true; |
| 209 |
} |
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
public static void initForSystemProcess() { |
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
if (!ActivityManager.isHighEndGfx()) { |
| 220 |
sRendererEnabled = false; |
| 221 |
} else { |
| 222 |
enableForegroundTrimming(); |
| 223 |
} |
| 224 |
} |
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
public static ThreadedRenderer create(Context context, boolean translucent, String name) { |
| 234 |
return new ThreadedRenderer(context, translucent, name); |
| 235 |
} |
| 236 |
|
| 237 |
private static final String[] VISUALIZERS = { |
| 238 |
PROFILE_PROPERTY_VISUALIZE_BARS, |
| 239 |
}; |
| 240 |
|
| 241 |
|
| 242 |
private int mWidth, mHeight; |
| 243 |
|
| 244 |
|
| 245 |
private int mSurfaceWidth, mSurfaceHeight; |
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
private int mInsetTop, mInsetLeft; |
| 250 |
|
| 251 |
|
| 252 |
private final float mLightY; |
| 253 |
private final float mLightZ; |
| 254 |
private final float mLightRadius; |
| 255 |
|
| 256 |
private boolean mInitialized = false; |
| 257 |
private boolean mRootNodeNeedsUpdate; |
| 258 |
|
| 259 |
private boolean mEnabled; |
| 260 |
private boolean mRequested = true; |
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
private static final class WebViewOverlayProvider implements |
| 270 |
PrepareSurfaceControlForWebviewCallback, ASurfaceTransactionCallback { |
| 271 |
private static final boolean sOverlaysAreEnabled = |
| 272 |
HardwareRenderer.isWebViewOverlaysEnabled(); |
| 273 |
private final SurfaceControl.Transaction mTransaction = new SurfaceControl.Transaction(); |
| 274 |
private boolean mHasWebViewOverlays = false; |
| 275 |
private BLASTBufferQueue mBLASTBufferQueue; |
| 276 |
private SurfaceControl mSurfaceControl; |
| 277 |
|
| 278 |
public boolean setSurfaceControlOpaque(boolean opaque) { |
| 279 |
synchronized (this) { |
| 280 |
if (mHasWebViewOverlays) return false; |
| 281 |
mTransaction.setOpaque(mSurfaceControl, opaque).apply(); |
| 282 |
} |
| 283 |
return opaque; |
| 284 |
} |
| 285 |
|
| 286 |
public boolean shouldEnableOverlaySupport() { |
| 287 |
return sOverlaysAreEnabled && mSurfaceControl != null && mBLASTBufferQueue != null; |
| 288 |
} |
| 289 |
|
| 290 |
public void setSurfaceControl(SurfaceControl surfaceControl) { |
| 291 |
synchronized (this) { |
| 292 |
mSurfaceControl = surfaceControl; |
| 293 |
if (mSurfaceControl != null && mHasWebViewOverlays) { |
| 294 |
mTransaction.setOpaque(surfaceControl, false).apply(); |
| 295 |
} |
| 296 |
} |
| 297 |
} |
| 298 |
|
| 299 |
public void setBLASTBufferQueue(BLASTBufferQueue bufferQueue) { |
| 300 |
synchronized (this) { |
| 301 |
mBLASTBufferQueue = bufferQueue; |
| 302 |
} |
| 303 |
} |
| 304 |
|
| 305 |
@Override |
| 306 |
public void prepare() { |
| 307 |
synchronized (this) { |
| 308 |
mHasWebViewOverlays = true; |
| 309 |
if (mSurfaceControl != null) { |
| 310 |
mTransaction.setOpaque(mSurfaceControl, false).apply(); |
| 311 |
} |
| 312 |
} |
| 313 |
} |
| 314 |
|
| 315 |
@Override |
| 316 |
public boolean onMergeTransaction(long nativeTransactionObj, |
| 317 |
long aSurfaceControlNativeObj, long frameNr) { |
| 318 |
synchronized (this) { |
| 319 |
if (mBLASTBufferQueue == null) { |
| 320 |
return false; |
| 321 |
} else { |
| 322 |
mBLASTBufferQueue.mergeWithNextTransaction(nativeTransactionObj, frameNr); |
| 323 |
return true; |
| 324 |
} |
| 325 |
} |
| 326 |
} |
| 327 |
} |
| 328 |
|
| 329 |
private final WebViewOverlayProvider mWebViewOverlayProvider = new WebViewOverlayProvider(); |
| 330 |
private boolean mWebViewOverlaysEnabled = false; |
| 331 |
|
| 332 |
@Nullable |
| 333 |
private ArrayList<FrameDrawingCallback> mNextRtFrameCallbacks; |
| 334 |
|
| 335 |
ThreadedRenderer(Context context, boolean translucent, String name) { |
| 336 |
super(); |
| 337 |
setName(name); |
| 338 |
setOpaque(!translucent); |
| 339 |
|
| 340 |
final TypedArray a = context.obtainStyledAttributes(null, R.styleable.Lighting, 0, 0); |
| 341 |
mLightY = a.getDimension(R.styleable.Lighting_lightY, 0); |
| 342 |
mLightZ = a.getDimension(R.styleable.Lighting_lightZ, 0); |
| 343 |
mLightRadius = a.getDimension(R.styleable.Lighting_lightRadius, 0); |
| 344 |
float ambientShadowAlpha = a.getFloat(R.styleable.Lighting_ambientShadowAlpha, 0); |
| 345 |
float spotShadowAlpha = a.getFloat(R.styleable.Lighting_spotShadowAlpha, 0); |
| 346 |
a.recycle(); |
| 347 |
setLightSourceAlpha(ambientShadowAlpha, spotShadowAlpha); |
| 348 |
} |
| 349 |
|
| 350 |
@Override |
| 351 |
public void destroy() { |
| 352 |
mInitialized = false; |
| 353 |
updateEnabledState(null); |
| 354 |
super.destroy(); |
| 355 |
} |
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
boolean isEnabled() { |
| 363 |
return mEnabled; |
| 364 |
} |
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
void setEnabled(boolean enabled) { |
| 372 |
mEnabled = enabled; |
| 373 |
} |
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
boolean isRequested() { |
| 382 |
return mRequested; |
| 383 |
} |
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
void setRequested(boolean requested) { |
| 390 |
mRequested = requested; |
| 391 |
} |
| 392 |
|
| 393 |
private void updateEnabledState(Surface surface) { |
| 394 |
if (surface == null || !surface.isValid()) { |
| 395 |
setEnabled(false); |
| 396 |
} else { |
| 397 |
setEnabled(mInitialized); |
| 398 |
} |
| 399 |
} |
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
boolean initialize(Surface surface) throws OutOfResourcesException { |
| 409 |
boolean status = !mInitialized; |
| 410 |
mInitialized = true; |
| 411 |
updateEnabledState(surface); |
| 412 |
setSurface(surface); |
| 413 |
return status; |
| 414 |
} |
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
boolean initializeIfNeeded(int width, int height, View.AttachInfo attachInfo, |
| 433 |
Surface surface, Rect surfaceInsets) throws OutOfResourcesException { |
| 434 |
if (isRequested()) { |
| 435 |
|
| 436 |
if (!isEnabled()) { |
| 437 |
if (initialize(surface)) { |
| 438 |
setup(width, height, attachInfo, surfaceInsets); |
| 439 |
return true; |
| 440 |
} |
| 441 |
} |
| 442 |
} |
| 443 |
return false; |
| 444 |
} |
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
void updateSurface(Surface surface) throws OutOfResourcesException { |
| 452 |
updateEnabledState(surface); |
| 453 |
setSurface(surface); |
| 454 |
} |
| 455 |
|
| 456 |
@Override |
| 457 |
public void setSurface(Surface surface) { |
| 458 |
|
| 459 |
|
| 460 |
if (surface != null && surface.isValid()) { |
| 461 |
super.setSurface(surface); |
| 462 |
} else { |
| 463 |
super.setSurface(null); |
| 464 |
} |
| 465 |
} |
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
void registerRtFrameCallback(@NonNull FrameDrawingCallback callback) { |
| 475 |
if (mNextRtFrameCallbacks == null) { |
| 476 |
mNextRtFrameCallbacks = new ArrayList<>(); |
| 477 |
} |
| 478 |
mNextRtFrameCallbacks.add(callback); |
| 479 |
} |
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
void unregisterRtFrameCallback(@NonNull FrameDrawingCallback callback) { |
| 488 |
if (mNextRtFrameCallbacks == null) { |
| 489 |
return; |
| 490 |
} |
| 491 |
mNextRtFrameCallbacks.remove(callback); |
| 492 |
} |
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
void destroyHardwareResources(View view) { |
| 501 |
destroyResources(view); |
| 502 |
clearContent(); |
| 503 |
} |
| 504 |
|
| 505 |
private static void destroyResources(View view) { |
| 506 |
view.destroyHardwareResources(); |
| 507 |
} |
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
void setup(int width, int height, AttachInfo attachInfo, Rect surfaceInsets) { |
| 518 |
mWidth = width; |
| 519 |
mHeight = height; |
| 520 |
|
| 521 |
if (surfaceInsets != null && (surfaceInsets.left != 0 || surfaceInsets.right != 0 |
| 522 |
|| surfaceInsets.top != 0 || surfaceInsets.bottom != 0)) { |
| 523 |
mInsetLeft = surfaceInsets.left; |
| 524 |
mInsetTop = surfaceInsets.top; |
| 525 |
mSurfaceWidth = width + mInsetLeft + surfaceInsets.right; |
| 526 |
mSurfaceHeight = height + mInsetTop + surfaceInsets.bottom; |
| 527 |
|
| 528 |
|
| 529 |
setOpaque(false); |
| 530 |
} else { |
| 531 |
mInsetLeft = 0; |
| 532 |
mInsetTop = 0; |
| 533 |
mSurfaceWidth = width; |
| 534 |
mSurfaceHeight = height; |
| 535 |
} |
| 536 |
|
| 537 |
mRootNode.setLeftTopRightBottom(-mInsetLeft, -mInsetTop, mSurfaceWidth, mSurfaceHeight); |
| 538 |
|
| 539 |
setLightCenter(attachInfo); |
| 540 |
} |
| 541 |
|
| 542 |
|
| 543 |
|
| 544 |
|
| 545 |
|
| 546 |
public boolean rendererOwnsSurfaceControlOpacity() { |
| 547 |
return mWebViewOverlayProvider.mSurfaceControl != null; |
| 548 |
} |
| 549 |
|
| 550 |
|
| 551 |
|
| 552 |
|
| 553 |
|
| 554 |
|
| 555 |
|
| 556 |
|
| 557 |
|
| 558 |
public boolean setSurfaceControlOpaque(boolean opaque) { |
| 559 |
return mWebViewOverlayProvider.setSurfaceControlOpaque(opaque); |
| 560 |
} |
| 561 |
|
| 562 |
private void updateWebViewOverlayCallbacks() { |
| 563 |
boolean shouldEnable = mWebViewOverlayProvider.shouldEnableOverlaySupport(); |
| 564 |
if (shouldEnable != mWebViewOverlaysEnabled) { |
| 565 |
mWebViewOverlaysEnabled = shouldEnable; |
| 566 |
if (shouldEnable) { |
| 567 |
setASurfaceTransactionCallback(mWebViewOverlayProvider); |
| 568 |
setPrepareSurfaceControlForWebviewCallback(mWebViewOverlayProvider); |
| 569 |
} else { |
| 570 |
setASurfaceTransactionCallback(null); |
| 571 |
setPrepareSurfaceControlForWebviewCallback(null); |
| 572 |
} |
| 573 |
} |
| 574 |
} |
| 575 |
|
| 576 |
@Override |
| 577 |
public void setSurfaceControl(@Nullable SurfaceControl surfaceControl) { |
| 578 |
super.setSurfaceControl(surfaceControl); |
| 579 |
mWebViewOverlayProvider.setSurfaceControl(surfaceControl); |
| 580 |
updateWebViewOverlayCallbacks(); |
| 581 |
} |
| 582 |
|
| 583 |
|
| 584 |
|
| 585 |
|
| 586 |
|
| 587 |
public void setBlastBufferQueue(@Nullable BLASTBufferQueue blastBufferQueue) { |
| 588 |
mWebViewOverlayProvider.setBLASTBufferQueue(blastBufferQueue); |
| 589 |
updateWebViewOverlayCallbacks(); |
| 590 |
} |
| 591 |
|
| 592 |
|
| 593 |
|
| 594 |
|
| 595 |
|
| 596 |
|
| 597 |
void setLightCenter(AttachInfo attachInfo) { |
| 598 |
|
| 599 |
DisplayMetrics displayMetrics = new DisplayMetrics(); |
| 600 |
attachInfo.mDisplay.getRealMetrics(displayMetrics); |
| 601 |
final float lightX = displayMetrics.widthPixels / 2f - attachInfo.mWindowLeft; |
| 602 |
final float lightY = mLightY - attachInfo.mWindowTop; |
| 603 |
|
| 604 |
|
| 605 |
final float zRatio = Math.min(displayMetrics.widthPixels, displayMetrics.heightPixels) |
| 606 |
/ (450f * displayMetrics.density); |
| 607 |
final float zWeightedAdjustment = (zRatio + 2) / 3f; |
| 608 |
final float lightZ = mLightZ * zWeightedAdjustment; |
| 609 |
|
| 610 |
setLightSourceGeometry(lightX, lightY, lightZ, mLightRadius); |
| 611 |
} |
| 612 |
|
| 613 |
|
| 614 |
|
| 615 |
|
| 616 |
|
| 617 |
|
| 618 |
|
| 619 |
int getWidth() { |
| 620 |
return mWidth; |
| 621 |
} |
| 622 |
|
| 623 |
|
| 624 |
|
| 625 |
|
| 626 |
|
| 627 |
|
| 628 |
|
| 629 |
int getHeight() { |
| 630 |
return mHeight; |
| 631 |
} |
| 632 |
|
| 633 |
private static int dumpArgsToFlags(String[] args) { |
| 634 |
|
| 635 |
|
| 636 |
|
| 637 |
if (args == null || args.length == 0) { |
| 638 |
return FLAG_DUMP_ALL; |
| 639 |
} |
| 640 |
int flags = 0; |
| 641 |
for (int i = 0; i < args.length; i++) { |
| 642 |
switch (args[i]) { |
| 643 |
case "framestats": |
| 644 |
flags |= FLAG_DUMP_FRAMESTATS; |
| 645 |
break; |
| 646 |
case "reset": |
| 647 |
flags |= FLAG_DUMP_RESET; |
| 648 |
break; |
| 649 |
case "-a": |
| 650 |
flags = FLAG_DUMP_ALL; |
| 651 |
break; |
| 652 |
} |
| 653 |
} |
| 654 |
return flags; |
| 655 |
} |
| 656 |
|
| 657 |
|
| 658 |
public static void handleDumpGfxInfo(FileDescriptor fd, String[] args) { |
| 659 |
dumpGlobalProfileInfo(fd, dumpArgsToFlags(args)); |
| 660 |
WindowManagerGlobal.getInstance().dumpGfxInfo(fd, args); |
| 661 |
} |
| 662 |
|
| 663 |
|
| 664 |
|
| 665 |
|
| 666 |
void dumpGfxInfo(PrintWriter pw, FileDescriptor fd, String[] args) { |
| 667 |
pw.flush(); |
| 668 |
dumpProfileInfo(fd, dumpArgsToFlags(args)); |
| 669 |
} |
| 670 |
|
| 671 |
Picture captureRenderingCommands() { |
| 672 |
return null; |
| 673 |
} |
| 674 |
|
| 675 |
@Override |
| 676 |
public boolean loadSystemProperties() { |
| 677 |
boolean changed = super.loadSystemProperties(); |
| 678 |
if (changed) { |
| 679 |
invalidateRoot(); |
| 680 |
} |
| 681 |
return changed; |
| 682 |
} |
| 683 |
|
| 684 |
private void updateViewTreeDisplayList(View view) { |
| 685 |
view.mPrivateFlags |= View.PFLAG_DRAWN; |
| 686 |
view.mRecreateDisplayList = (view.mPrivateFlags & View.PFLAG_INVALIDATED) |
| 687 |
== View.PFLAG_INVALIDATED; |
| 688 |
view.mPrivateFlags &= ~View.PFLAG_INVALIDATED; |
| 689 |
view.updateDisplayListIfDirty(); |
| 690 |
view.mRecreateDisplayList = false; |
| 691 |
} |
| 692 |
|
| 693 |
private void updateRootDisplayList(View view, DrawCallbacks callbacks) { |
| 694 |
Trace.traceBegin(Trace.TRACE_TAG_VIEW, "Record View#draw()"); |
| 695 |
updateViewTreeDisplayList(view); |
| 696 |
|
| 697 |
|
| 698 |
|
| 699 |
|
| 700 |
if (mNextRtFrameCallbacks != null) { |
| 701 |
final ArrayList<FrameDrawingCallback> frameCallbacks = mNextRtFrameCallbacks; |
| 702 |
mNextRtFrameCallbacks = null; |
| 703 |
setFrameCallback(new FrameDrawingCallback() { |
| 704 |
@Override |
| 705 |
public void onFrameDraw(long frame) { |
| 706 |
} |
| 707 |
|
| 708 |
@Override |
| 709 |
public FrameCommitCallback onFrameDraw(int syncResult, long frame) { |
| 710 |
ArrayList<FrameCommitCallback> frameCommitCallbacks = new ArrayList<>(); |
| 711 |
for (int i = 0; i < frameCallbacks.size(); ++i) { |
| 712 |
FrameCommitCallback frameCommitCallback = frameCallbacks.get(i) |
| 713 |
.onFrameDraw(syncResult, frame); |
| 714 |
if (frameCommitCallback != null) { |
| 715 |
frameCommitCallbacks.add(frameCommitCallback); |
| 716 |
} |
| 717 |
} |
| 718 |
|
| 719 |
if (frameCommitCallbacks.isEmpty()) { |
| 720 |
return null; |
| 721 |
} |
| 722 |
|
| 723 |
return didProduceBuffer -> { |
| 724 |
for (int i = 0; i < frameCommitCallbacks.size(); ++i) { |
| 725 |
frameCommitCallbacks.get(i).onFrameCommit(didProduceBuffer); |
| 726 |
} |
| 727 |
}; |
| 728 |
} |
| 729 |
}); |
| 730 |
} |
| 731 |
|
| 732 |
if (mRootNodeNeedsUpdate || !mRootNode.hasDisplayList()) { |
| 733 |
RecordingCanvas canvas = mRootNode.beginRecording(mSurfaceWidth, mSurfaceHeight); |
| 734 |
try { |
| 735 |
final int saveCount = canvas.save(); |
| 736 |
canvas.translate(mInsetLeft, mInsetTop); |
| 737 |
callbacks.onPreDraw(canvas); |
| 738 |
|
| 739 |
canvas.enableZ(); |
| 740 |
canvas.drawRenderNode(view.updateDisplayListIfDirty()); |
| 741 |
canvas.disableZ(); |
| 742 |
|
| 743 |
callbacks.onPostDraw(canvas); |
| 744 |
canvas.restoreToCount(saveCount); |
| 745 |
mRootNodeNeedsUpdate = false; |
| 746 |
} finally { |
| 747 |
mRootNode.endRecording(); |
| 748 |
} |
| 749 |
} |
| 750 |
Trace.traceEnd(Trace.TRACE_TAG_VIEW); |
| 751 |
} |
| 752 |
|
| 753 |
|
| 754 |
|
| 755 |
|
| 756 |
|
| 757 |
interface DrawCallbacks { |
| 758 |
|
| 759 |
|
| 760 |
|
| 761 |
|
| 762 |
|
| 763 |
|
| 764 |
|
| 765 |
void onPreDraw(RecordingCanvas canvas); |
| 766 |
|
| 767 |
|
| 768 |
|
| 769 |
|
| 770 |
|
| 771 |
|
| 772 |
|
| 773 |
void onPostDraw(RecordingCanvas canvas); |
| 774 |
} |
| 775 |
|
| 776 |
|
| 777 |
|
| 778 |
|
| 779 |
|
| 780 |
void invalidateRoot() { |
| 781 |
mRootNodeNeedsUpdate = true; |
| 782 |
} |
| 783 |
|
| 784 |
|
| 785 |
|
| 786 |
|
| 787 |
|
| 788 |
|
| 789 |
|
| 790 |
void draw(View view, AttachInfo attachInfo, DrawCallbacks callbacks) { |
| 791 |
attachInfo.mViewRootImpl.mViewFrameInfo.markDrawStart(); |
| 792 |
|
| 793 |
updateRootDisplayList(view, callbacks); |
| 794 |
|
| 795 |
|
| 796 |
|
| 797 |
if (attachInfo.mPendingAnimatingRenderNodes != null) { |
| 798 |
final int count = attachInfo.mPendingAnimatingRenderNodes.size(); |
| 799 |
for (int i = 0; i < count; i++) { |
| 800 |
registerAnimatingRenderNode( |
| 801 |
attachInfo.mPendingAnimatingRenderNodes.get(i)); |
| 802 |
} |
| 803 |
attachInfo.mPendingAnimatingRenderNodes.clear(); |
| 804 |
|
| 805 |
|
| 806 |
attachInfo.mPendingAnimatingRenderNodes = null; |
| 807 |
} |
| 808 |
|
| 809 |
final FrameInfo frameInfo = attachInfo.mViewRootImpl.getUpdatedFrameInfo(); |
| 810 |
|
| 811 |
int syncResult = syncAndDrawFrame(frameInfo); |
| 812 |
if ((syncResult & SYNC_LOST_SURFACE_REWARD_IF_FOUND) != 0) { |
| 813 |
Log.w("OpenGLRenderer", "Surface lost, forcing relayout"); |
| 814 |
|
| 815 |
|
| 816 |
attachInfo.mViewRootImpl.mForceNextWindowRelayout = true; |
| 817 |
attachInfo.mViewRootImpl.requestLayout(); |
| 818 |
} |
| 819 |
if ((syncResult & SYNC_REDRAW_REQUESTED) != 0) { |
| 820 |
attachInfo.mViewRootImpl.invalidate(); |
| 821 |
} |
| 822 |
} |
| 823 |
|
| 824 |
|
| 825 |
public @NonNull RenderNode getRootNode() { |
| 826 |
return mRootNode; |
| 827 |
} |
| 828 |
|
| 829 |
|
| 830 |
|
| 831 |
|
| 832 |
|
| 833 |
|
| 834 |
|
| 835 |
public static class SimpleRenderer extends HardwareRenderer { |
| 836 |
private final float mLightY, mLightZ, mLightRadius; |
| 837 |
|
| 838 |
public SimpleRenderer(final Context context, final String name, final Surface surface) { |
| 839 |
super(); |
| 840 |
setName(name); |
| 841 |
setOpaque(false); |
| 842 |
setSurface(surface); |
| 843 |
final TypedArray a = context.obtainStyledAttributes(null, R.styleable.Lighting, 0, 0); |
| 844 |
mLightY = a.getDimension(R.styleable.Lighting_lightY, 0); |
| 845 |
mLightZ = a.getDimension(R.styleable.Lighting_lightZ, 0); |
| 846 |
mLightRadius = a.getDimension(R.styleable.Lighting_lightRadius, 0); |
| 847 |
final float ambientShadowAlpha = a.getFloat(R.styleable.Lighting_ambientShadowAlpha, 0); |
| 848 |
final float spotShadowAlpha = a.getFloat(R.styleable.Lighting_spotShadowAlpha, 0); |
| 849 |
a.recycle(); |
| 850 |
setLightSourceAlpha(ambientShadowAlpha, spotShadowAlpha); |
| 851 |
} |
| 852 |
|
| 853 |
|
| 854 |
|
| 855 |
|
| 856 |
public void setLightCenter(final Display display, |
| 857 |
final int windowLeft, final int windowTop) { |
| 858 |
|
| 859 |
DisplayMetrics displayMetrics = new DisplayMetrics(); |
| 860 |
display.getRealMetrics(displayMetrics); |
| 861 |
final float lightX = displayMetrics.widthPixels / 2f - windowLeft; |
| 862 |
final float lightY = mLightY - windowTop; |
| 863 |
|
| 864 |
|
| 865 |
final float zRatio = Math.min(displayMetrics.widthPixels, displayMetrics.heightPixels) |
| 866 |
/ (450f * displayMetrics.density); |
| 867 |
final float zWeightedAdjustment = (zRatio + 2) / 3f; |
| 868 |
final float lightZ = mLightZ * zWeightedAdjustment; |
| 869 |
|
| 870 |
setLightSourceGeometry(lightX, lightY, lightZ, mLightRadius); |
| 871 |
} |
| 872 |
|
| 873 |
public RenderNode getRootNode() { |
| 874 |
return mRootNode; |
| 875 |
} |
| 876 |
|
| 877 |
|
| 878 |
|
| 879 |
|
| 880 |
public void draw(final FrameDrawingCallback callback) { |
| 881 |
final long vsync = AnimationUtils.currentAnimationTimeMillis() * 1000000L; |
| 882 |
if (callback != null) { |
| 883 |
setFrameCallback(callback); |
| 884 |
} |
| 885 |
createRenderRequest() |
| 886 |
.setVsyncTime(vsync) |
| 887 |
.syncAndDraw(); |
| 888 |
} |
| 889 |
} |
| 890 |
} |