| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.view; |
| 18 |
|
| 19 |
import static android.view.WindowManagerPolicyConstants.APPLICATION_MEDIA_OVERLAY_SUBLAYER; |
| 20 |
import static android.view.WindowManagerPolicyConstants.APPLICATION_MEDIA_SUBLAYER; |
| 21 |
import static android.view.WindowManagerPolicyConstants.APPLICATION_PANEL_SUBLAYER; |
| 22 |
|
| 23 |
import android.annotation.NonNull; |
| 24 |
import android.annotation.Nullable; |
| 25 |
import android.compat.annotation.UnsupportedAppUsage; |
| 26 |
import android.content.Context; |
| 27 |
import android.content.res.CompatibilityInfo.Translator; |
| 28 |
import android.graphics.BLASTBufferQueue; |
| 29 |
import android.graphics.BlendMode; |
| 30 |
import android.graphics.Canvas; |
| 31 |
import android.graphics.Color; |
| 32 |
import android.graphics.Matrix; |
| 33 |
import android.graphics.Paint; |
| 34 |
import android.graphics.PixelFormat; |
| 35 |
import android.graphics.Point; |
| 36 |
import android.graphics.Rect; |
| 37 |
import android.graphics.Region; |
| 38 |
import android.graphics.RenderNode; |
| 39 |
import android.os.Build; |
| 40 |
import android.os.Handler; |
| 41 |
import android.os.IBinder; |
| 42 |
import android.os.Looper; |
| 43 |
import android.os.SystemClock; |
| 44 |
import android.util.ArraySet; |
| 45 |
import android.util.AttributeSet; |
| 46 |
import android.util.Log; |
| 47 |
import android.view.SurfaceControl.Transaction; |
| 48 |
import android.view.accessibility.AccessibilityNodeInfo; |
| 49 |
import android.view.accessibility.IAccessibilityEmbeddedConnection; |
| 50 |
import android.window.SurfaceSyncer; |
| 51 |
|
| 52 |
import com.android.internal.view.SurfaceCallbackHelper; |
| 53 |
|
| 54 |
import java.util.ArrayList; |
| 55 |
import java.util.concurrent.CountDownLatch; |
| 56 |
import java.util.concurrent.locks.ReentrantLock; |
| 57 |
import java.util.function.Consumer; |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
public class SurfaceView extends View implements ViewRootImpl.SurfaceChangedCallback { |
| 112 |
private static final String TAG = "SurfaceView"; |
| 113 |
private static final boolean DEBUG = false; |
| 114 |
private static final boolean DEBUG_POSITION = false; |
| 115 |
|
| 116 |
@UnsupportedAppUsage |
| 117 |
final ArrayList<SurfaceHolder.Callback> mCallbacks = new ArrayList<>(); |
| 118 |
|
| 119 |
final int[] mLocation = new int[2]; |
| 120 |
|
| 121 |
@UnsupportedAppUsage |
| 122 |
final ReentrantLock mSurfaceLock = new ReentrantLock(); |
| 123 |
@UnsupportedAppUsage |
| 124 |
final Surface mSurface = new Surface(); |
| 125 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023) |
| 126 |
boolean mDrawingStopped = true; |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
boolean mDrawFinished = false; |
| 131 |
|
| 132 |
final Rect mScreenRect = new Rect(); |
| 133 |
private final SurfaceSession mSurfaceSession = new SurfaceSession(); |
| 134 |
|
| 135 |
SurfaceControl mSurfaceControl; |
| 136 |
SurfaceControl mBackgroundControl; |
| 137 |
private boolean mDisableBackgroundLayer = false; |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
final Object mSurfaceControlLock = new Object(); |
| 144 |
final Rect mTmpRect = new Rect(); |
| 145 |
|
| 146 |
Paint mRoundedViewportPaint; |
| 147 |
|
| 148 |
int mSubLayer = APPLICATION_MEDIA_SUBLAYER; |
| 149 |
|
| 150 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023) |
| 151 |
boolean mIsCreating = false; |
| 152 |
|
| 153 |
private final ViewTreeObserver.OnScrollChangedListener mScrollChangedListener = |
| 154 |
this::updateSurface; |
| 155 |
|
| 156 |
@UnsupportedAppUsage |
| 157 |
private final ViewTreeObserver.OnPreDrawListener mDrawListener = () -> { |
| 158 |
|
| 159 |
mHaveFrame = getWidth() > 0 && getHeight() > 0; |
| 160 |
updateSurface(); |
| 161 |
return true; |
| 162 |
}; |
| 163 |
|
| 164 |
boolean mRequestedVisible = false; |
| 165 |
boolean mWindowVisibility = false; |
| 166 |
boolean mLastWindowVisibility = false; |
| 167 |
boolean mViewVisibility = false; |
| 168 |
boolean mWindowStopped = false; |
| 169 |
|
| 170 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023) |
| 171 |
int mRequestedWidth = -1; |
| 172 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023) |
| 173 |
int mRequestedHeight = -1; |
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
@UnsupportedAppUsage |
| 178 |
int mRequestedFormat = PixelFormat.RGB_565; |
| 179 |
|
| 180 |
boolean mUseAlpha = false; |
| 181 |
float mSurfaceAlpha = 1f; |
| 182 |
boolean mClipSurfaceToBounds; |
| 183 |
int mBackgroundColor = Color.BLACK; |
| 184 |
|
| 185 |
@UnsupportedAppUsage |
| 186 |
boolean mHaveFrame = false; |
| 187 |
boolean mSurfaceCreated = false; |
| 188 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023) |
| 189 |
long mLastLockTime = 0; |
| 190 |
|
| 191 |
boolean mVisible = false; |
| 192 |
int mWindowSpaceLeft = -1; |
| 193 |
int mWindowSpaceTop = -1; |
| 194 |
int mSurfaceWidth = -1; |
| 195 |
int mSurfaceHeight = -1; |
| 196 |
float mCornerRadius; |
| 197 |
@UnsupportedAppUsage |
| 198 |
int mFormat = -1; |
| 199 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023) |
| 200 |
final Rect mSurfaceFrame = new Rect(); |
| 201 |
int mLastSurfaceWidth = -1, mLastSurfaceHeight = -1; |
| 202 |
@SurfaceControl.BufferTransform int mTransformHint = 0; |
| 203 |
|
| 204 |
private boolean mGlobalListenersAdded; |
| 205 |
private boolean mAttachedToWindow; |
| 206 |
|
| 207 |
private int mSurfaceFlags = SurfaceControl.HIDDEN; |
| 208 |
|
| 209 |
private final SurfaceSyncer mSurfaceSyncer = new SurfaceSyncer(); |
| 210 |
private final ArraySet<Integer> mSyncIds = new ArraySet<>(); |
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
private final SurfaceControl.Transaction mRtTransaction = new SurfaceControl.Transaction(); |
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
private final SurfaceControl.Transaction mFrameCallbackTransaction = |
| 224 |
new SurfaceControl.Transaction(); |
| 225 |
|
| 226 |
private int mParentSurfaceSequenceId; |
| 227 |
|
| 228 |
private RemoteAccessibilityController mRemoteAccessibilityController = |
| 229 |
new RemoteAccessibilityController(this); |
| 230 |
|
| 231 |
private final Matrix mTmpMatrix = new Matrix(); |
| 232 |
|
| 233 |
SurfaceControlViewHost.SurfacePackage mSurfacePackage; |
| 234 |
|
| 235 |
|
| 236 |
private SurfaceControl mBlastSurfaceControl; |
| 237 |
private BLASTBufferQueue mBlastBufferQueue; |
| 238 |
|
| 239 |
public SurfaceView(Context context) { |
| 240 |
this(context, null); |
| 241 |
} |
| 242 |
|
| 243 |
public SurfaceView(Context context, AttributeSet attrs) { |
| 244 |
this(context, attrs, 0); |
| 245 |
} |
| 246 |
|
| 247 |
public SurfaceView(Context context, AttributeSet attrs, int defStyleAttr) { |
| 248 |
this(context, attrs, defStyleAttr, 0); |
| 249 |
} |
| 250 |
|
| 251 |
public SurfaceView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { |
| 252 |
this(context, attrs, defStyleAttr, defStyleRes, false); |
| 253 |
} |
| 254 |
|
| 255 |
|
| 256 |
public SurfaceView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr, |
| 257 |
int defStyleRes, boolean disableBackgroundLayer) { |
| 258 |
super(context, attrs, defStyleAttr, defStyleRes); |
| 259 |
setWillNotDraw(true); |
| 260 |
mDisableBackgroundLayer = disableBackgroundLayer; |
| 261 |
} |
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
public SurfaceHolder getHolder() { |
| 270 |
return mSurfaceHolder; |
| 271 |
} |
| 272 |
|
| 273 |
private void updateRequestedVisibility() { |
| 274 |
mRequestedVisible = mViewVisibility && mWindowVisibility && !mWindowStopped; |
| 275 |
} |
| 276 |
|
| 277 |
private void setWindowStopped(boolean stopped) { |
| 278 |
mWindowStopped = stopped; |
| 279 |
updateRequestedVisibility(); |
| 280 |
updateSurface(); |
| 281 |
} |
| 282 |
|
| 283 |
@Override |
| 284 |
protected void onAttachedToWindow() { |
| 285 |
super.onAttachedToWindow(); |
| 286 |
|
| 287 |
getViewRootImpl().addSurfaceChangedCallback(this); |
| 288 |
mWindowStopped = false; |
| 289 |
|
| 290 |
mViewVisibility = getVisibility() == VISIBLE; |
| 291 |
updateRequestedVisibility(); |
| 292 |
|
| 293 |
mAttachedToWindow = true; |
| 294 |
mParent.requestTransparentRegion(SurfaceView.this); |
| 295 |
if (!mGlobalListenersAdded) { |
| 296 |
ViewTreeObserver observer = getViewTreeObserver(); |
| 297 |
observer.addOnScrollChangedListener(mScrollChangedListener); |
| 298 |
observer.addOnPreDrawListener(mDrawListener); |
| 299 |
mGlobalListenersAdded = true; |
| 300 |
} |
| 301 |
} |
| 302 |
|
| 303 |
@Override |
| 304 |
protected void onWindowVisibilityChanged(int visibility) { |
| 305 |
super.onWindowVisibilityChanged(visibility); |
| 306 |
mWindowVisibility = visibility == VISIBLE; |
| 307 |
updateRequestedVisibility(); |
| 308 |
updateSurface(); |
| 309 |
} |
| 310 |
|
| 311 |
@Override |
| 312 |
public void setVisibility(int visibility) { |
| 313 |
super.setVisibility(visibility); |
| 314 |
mViewVisibility = visibility == VISIBLE; |
| 315 |
boolean newRequestedVisible = mWindowVisibility && mViewVisibility && !mWindowStopped; |
| 316 |
if (newRequestedVisible != mRequestedVisible) { |
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
requestLayout(); |
| 324 |
} |
| 325 |
mRequestedVisible = newRequestedVisible; |
| 326 |
updateSurface(); |
| 327 |
} |
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
public void setUseAlpha() { |
| 339 |
if (!mUseAlpha) { |
| 340 |
mUseAlpha = true; |
| 341 |
updateSurfaceAlpha(); |
| 342 |
} |
| 343 |
} |
| 344 |
|
| 345 |
@Override |
| 346 |
public void setAlpha(float alpha) { |
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
if (DEBUG) { |
| 356 |
Log.d(TAG, System.identityHashCode(this) |
| 357 |
+ " setAlpha: mUseAlpha = " + mUseAlpha + " alpha=" + alpha); |
| 358 |
} |
| 359 |
super.setAlpha(alpha); |
| 360 |
updateSurfaceAlpha(); |
| 361 |
} |
| 362 |
|
| 363 |
private float getFixedAlpha() { |
| 364 |
|
| 365 |
final float alpha = getAlpha(); |
| 366 |
return mUseAlpha && (mSubLayer > 0 || alpha == 0f) ? alpha : 1f; |
| 367 |
} |
| 368 |
|
| 369 |
private void updateSurfaceAlpha() { |
| 370 |
if (!mUseAlpha || !mHaveFrame || mSurfaceControl == null) { |
| 371 |
return; |
| 372 |
} |
| 373 |
final float viewAlpha = getAlpha(); |
| 374 |
if (mSubLayer < 0 && 0f < viewAlpha && viewAlpha < 1f) { |
| 375 |
Log.w(TAG, System.identityHashCode(this) |
| 376 |
+ " updateSurfaceAlpha:" |
| 377 |
+ " translucent color is not supported for a surface placed z-below."); |
| 378 |
} |
| 379 |
final ViewRootImpl viewRoot = getViewRootImpl(); |
| 380 |
if (viewRoot == null) { |
| 381 |
return; |
| 382 |
} |
| 383 |
final float alpha = getFixedAlpha(); |
| 384 |
if (alpha != mSurfaceAlpha) { |
| 385 |
final Transaction transaction = new Transaction(); |
| 386 |
transaction.setAlpha(mSurfaceControl, alpha); |
| 387 |
viewRoot.applyTransactionOnDraw(transaction); |
| 388 |
damageInParent(); |
| 389 |
mSurfaceAlpha = alpha; |
| 390 |
} |
| 391 |
} |
| 392 |
|
| 393 |
private void performDrawFinished() { |
| 394 |
mDrawFinished = true; |
| 395 |
if (mAttachedToWindow) { |
| 396 |
mParent.requestTransparentRegion(SurfaceView.this); |
| 397 |
invalidate(); |
| 398 |
} |
| 399 |
} |
| 400 |
|
| 401 |
@Override |
| 402 |
protected void onDetachedFromWindow() { |
| 403 |
ViewRootImpl viewRoot = getViewRootImpl(); |
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
if (viewRoot != null) { |
| 410 |
viewRoot.removeSurfaceChangedCallback(this); |
| 411 |
} |
| 412 |
|
| 413 |
mAttachedToWindow = false; |
| 414 |
if (mGlobalListenersAdded) { |
| 415 |
ViewTreeObserver observer = getViewTreeObserver(); |
| 416 |
observer.removeOnScrollChangedListener(mScrollChangedListener); |
| 417 |
observer.removeOnPreDrawListener(mDrawListener); |
| 418 |
mGlobalListenersAdded = false; |
| 419 |
} |
| 420 |
|
| 421 |
mRequestedVisible = false; |
| 422 |
|
| 423 |
updateSurface(); |
| 424 |
releaseSurfaces(true ); |
| 425 |
|
| 426 |
mHaveFrame = false; |
| 427 |
super.onDetachedFromWindow(); |
| 428 |
} |
| 429 |
|
| 430 |
@Override |
| 431 |
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
| 432 |
int width = mRequestedWidth >= 0 |
| 433 |
? resolveSizeAndState(mRequestedWidth, widthMeasureSpec, 0) |
| 434 |
: getDefaultSize(0, widthMeasureSpec); |
| 435 |
int height = mRequestedHeight >= 0 |
| 436 |
? resolveSizeAndState(mRequestedHeight, heightMeasureSpec, 0) |
| 437 |
: getDefaultSize(0, heightMeasureSpec); |
| 438 |
setMeasuredDimension(width, height); |
| 439 |
} |
| 440 |
|
| 441 |
|
| 442 |
@Override |
| 443 |
@UnsupportedAppUsage |
| 444 |
protected boolean setFrame(int left, int top, int right, int bottom) { |
| 445 |
boolean result = super.setFrame(left, top, right, bottom); |
| 446 |
updateSurface(); |
| 447 |
return result; |
| 448 |
} |
| 449 |
|
| 450 |
@Override |
| 451 |
public boolean gatherTransparentRegion(Region region) { |
| 452 |
if (isAboveParent() || !mDrawFinished) { |
| 453 |
return super.gatherTransparentRegion(region); |
| 454 |
} |
| 455 |
|
| 456 |
boolean opaque = true; |
| 457 |
if ((mPrivateFlags & PFLAG_SKIP_DRAW) == 0) { |
| 458 |
|
| 459 |
opaque = super.gatherTransparentRegion(region); |
| 460 |
} else if (region != null) { |
| 461 |
int w = getWidth(); |
| 462 |
int h = getHeight(); |
| 463 |
if (w>0 && h>0) { |
| 464 |
getLocationInWindow(mLocation); |
| 465 |
|
| 466 |
int l = mLocation[0]; |
| 467 |
int t = mLocation[1]; |
| 468 |
region.op(l, t, l+w, t+h, Region.Op.UNION); |
| 469 |
} |
| 470 |
} |
| 471 |
if (PixelFormat.formatHasAlpha(mRequestedFormat)) { |
| 472 |
opaque = false; |
| 473 |
} |
| 474 |
return opaque; |
| 475 |
} |
| 476 |
|
| 477 |
@Override |
| 478 |
public void draw(Canvas canvas) { |
| 479 |
if (mDrawFinished && !isAboveParent()) { |
| 480 |
|
| 481 |
if ((mPrivateFlags & PFLAG_SKIP_DRAW) == 0) { |
| 482 |
|
| 483 |
clearSurfaceViewPort(canvas); |
| 484 |
} |
| 485 |
} |
| 486 |
super.draw(canvas); |
| 487 |
} |
| 488 |
|
| 489 |
@Override |
| 490 |
protected void dispatchDraw(Canvas canvas) { |
| 491 |
if (mDrawFinished && !isAboveParent()) { |
| 492 |
|
| 493 |
if ((mPrivateFlags & PFLAG_SKIP_DRAW) == PFLAG_SKIP_DRAW) { |
| 494 |
|
| 495 |
clearSurfaceViewPort(canvas); |
| 496 |
} |
| 497 |
} |
| 498 |
super.dispatchDraw(canvas); |
| 499 |
} |
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
public void setEnableSurfaceClipping(boolean enabled) { |
| 509 |
mClipSurfaceToBounds = enabled; |
| 510 |
invalidate(); |
| 511 |
} |
| 512 |
|
| 513 |
@Override |
| 514 |
public void setClipBounds(Rect clipBounds) { |
| 515 |
super.setClipBounds(clipBounds); |
| 516 |
|
| 517 |
if (!mClipSurfaceToBounds || mSurfaceControl == null) { |
| 518 |
return; |
| 519 |
} |
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
if (mCornerRadius > 0f && !isAboveParent()) { |
| 524 |
invalidate(); |
| 525 |
} |
| 526 |
|
| 527 |
if (mClipBounds != null) { |
| 528 |
mTmpRect.set(mClipBounds); |
| 529 |
} else { |
| 530 |
mTmpRect.set(0, 0, mSurfaceWidth, mSurfaceHeight); |
| 531 |
} |
| 532 |
final Transaction transaction = new Transaction(); |
| 533 |
transaction.setWindowCrop(mSurfaceControl, mTmpRect); |
| 534 |
applyTransactionOnVriDraw(transaction); |
| 535 |
invalidate(); |
| 536 |
} |
| 537 |
|
| 538 |
private void clearSurfaceViewPort(Canvas canvas) { |
| 539 |
if (mCornerRadius > 0f) { |
| 540 |
canvas.getClipBounds(mTmpRect); |
| 541 |
if (mClipSurfaceToBounds && mClipBounds != null) { |
| 542 |
mTmpRect.intersect(mClipBounds); |
| 543 |
} |
| 544 |
canvas.punchHole( |
| 545 |
mTmpRect.left, |
| 546 |
mTmpRect.top, |
| 547 |
mTmpRect.right, |
| 548 |
mTmpRect.bottom, |
| 549 |
mCornerRadius, |
| 550 |
mCornerRadius |
| 551 |
); |
| 552 |
} else { |
| 553 |
canvas.punchHole(0f, 0f, getWidth(), getHeight(), 0f, 0f); |
| 554 |
} |
| 555 |
} |
| 556 |
|
| 557 |
|
| 558 |
|
| 559 |
|
| 560 |
|
| 561 |
|
| 562 |
|
| 563 |
|
| 564 |
public void setCornerRadius(float cornerRadius) { |
| 565 |
mCornerRadius = cornerRadius; |
| 566 |
if (mCornerRadius > 0f && mRoundedViewportPaint == null) { |
| 567 |
mRoundedViewportPaint = new Paint(Paint.ANTI_ALIAS_FLAG); |
| 568 |
mRoundedViewportPaint.setBlendMode(BlendMode.CLEAR); |
| 569 |
mRoundedViewportPaint.setColor(0); |
| 570 |
} |
| 571 |
invalidate(); |
| 572 |
} |
| 573 |
|
| 574 |
|
| 575 |
|
| 576 |
|
| 577 |
|
| 578 |
|
| 579 |
|
| 580 |
public float getCornerRadius() { |
| 581 |
return mCornerRadius; |
| 582 |
} |
| 583 |
|
| 584 |
|
| 585 |
|
| 586 |
|
| 587 |
|
| 588 |
|
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
|
| 593 |
|
| 594 |
|
| 595 |
public void setZOrderMediaOverlay(boolean isMediaOverlay) { |
| 596 |
mSubLayer = isMediaOverlay |
| 597 |
? APPLICATION_MEDIA_OVERLAY_SUBLAYER : APPLICATION_MEDIA_SUBLAYER; |
| 598 |
} |
| 599 |
|
| 600 |
|
| 601 |
|
| 602 |
|
| 603 |
|
| 604 |
|
| 605 |
|
| 606 |
|
| 607 |
|
| 608 |
|
| 609 |
|
| 610 |
|
| 611 |
|
| 612 |
|
| 613 |
|
| 614 |
|
| 615 |
|
| 616 |
|
| 617 |
public void setZOrderOnTop(boolean onTop) { |
| 618 |
|
| 619 |
final boolean allowDynamicChange = getContext().getApplicationInfo().targetSdkVersion |
| 620 |
> Build.VERSION_CODES.Q; |
| 621 |
setZOrderedOnTop(onTop, allowDynamicChange); |
| 622 |
} |
| 623 |
|
| 624 |
|
| 625 |
|
| 626 |
|
| 627 |
|
| 628 |
|
| 629 |
public boolean isZOrderedOnTop() { |
| 630 |
return mSubLayer > 0; |
| 631 |
} |
| 632 |
|
| 633 |
|
| 634 |
|
| 635 |
|
| 636 |
|
| 637 |
|
| 638 |
|
| 639 |
|
| 640 |
|
| 641 |
|
| 642 |
|
| 643 |
|
| 644 |
|
| 645 |
|
| 646 |
|
| 647 |
|
| 648 |
|
| 649 |
public boolean setZOrderedOnTop(boolean onTop, boolean allowDynamicChange) { |
| 650 |
final int subLayer; |
| 651 |
if (onTop) { |
| 652 |
subLayer = APPLICATION_PANEL_SUBLAYER; |
| 653 |
} else { |
| 654 |
subLayer = APPLICATION_MEDIA_SUBLAYER; |
| 655 |
} |
| 656 |
if (mSubLayer == subLayer) { |
| 657 |
return false; |
| 658 |
} |
| 659 |
mSubLayer = subLayer; |
| 660 |
|
| 661 |
if (!allowDynamicChange) { |
| 662 |
return false; |
| 663 |
} |
| 664 |
if (mSurfaceControl == null) { |
| 665 |
return true; |
| 666 |
} |
| 667 |
final ViewRootImpl viewRoot = getViewRootImpl(); |
| 668 |
if (viewRoot == null) { |
| 669 |
return true; |
| 670 |
} |
| 671 |
final Transaction transaction = new SurfaceControl.Transaction(); |
| 672 |
updateRelativeZ(transaction); |
| 673 |
viewRoot.applyTransactionOnDraw(transaction); |
| 674 |
invalidate(); |
| 675 |
return true; |
| 676 |
} |
| 677 |
|
| 678 |
|
| 679 |
|
| 680 |
|
| 681 |
|
| 682 |
|
| 683 |
|
| 684 |
|
| 685 |
|
| 686 |
|
| 687 |
|
| 688 |
|
| 689 |
|
| 690 |
public void setSecure(boolean isSecure) { |
| 691 |
if (isSecure) { |
| 692 |
mSurfaceFlags |= SurfaceControl.SECURE; |
| 693 |
} else { |
| 694 |
mSurfaceFlags &= ~SurfaceControl.SECURE; |
| 695 |
} |
| 696 |
} |
| 697 |
|
| 698 |
private void updateOpaqueFlag() { |
| 699 |
if (!PixelFormat.formatHasAlpha(mRequestedFormat)) { |
| 700 |
mSurfaceFlags |= SurfaceControl.OPAQUE; |
| 701 |
} else { |
| 702 |
mSurfaceFlags &= ~SurfaceControl.OPAQUE; |
| 703 |
} |
| 704 |
} |
| 705 |
|
| 706 |
private void updateBackgroundVisibility(Transaction t) { |
| 707 |
if (mBackgroundControl == null) { |
| 708 |
return; |
| 709 |
} |
| 710 |
if ((mSubLayer < 0) && ((mSurfaceFlags & SurfaceControl.OPAQUE) != 0) |
| 711 |
&& !mDisableBackgroundLayer) { |
| 712 |
t.show(mBackgroundControl); |
| 713 |
} else { |
| 714 |
t.hide(mBackgroundControl); |
| 715 |
} |
| 716 |
} |
| 717 |
|
| 718 |
private Transaction updateBackgroundColor(Transaction t) { |
| 719 |
final float[] colorComponents = new float[] { Color.red(mBackgroundColor) / 255.f, |
| 720 |
Color.green(mBackgroundColor) / 255.f, Color.blue(mBackgroundColor) / 255.f }; |
| 721 |
t.setColor(mBackgroundControl, colorComponents); |
| 722 |
return t; |
| 723 |
} |
| 724 |
|
| 725 |
private void releaseSurfaces(boolean releaseSurfacePackage) { |
| 726 |
mSurfaceAlpha = 1f; |
| 727 |
|
| 728 |
synchronized (mSurfaceControlLock) { |
| 729 |
mSurface.destroy(); |
| 730 |
if (mBlastBufferQueue != null) { |
| 731 |
mBlastBufferQueue.destroy(); |
| 732 |
mBlastBufferQueue = null; |
| 733 |
} |
| 734 |
|
| 735 |
final Transaction transaction = new Transaction(); |
| 736 |
if (mSurfaceControl != null) { |
| 737 |
transaction.remove(mSurfaceControl); |
| 738 |
mSurfaceControl = null; |
| 739 |
} |
| 740 |
if (mBackgroundControl != null) { |
| 741 |
transaction.remove(mBackgroundControl); |
| 742 |
mBackgroundControl = null; |
| 743 |
} |
| 744 |
if (mBlastSurfaceControl != null) { |
| 745 |
transaction.remove(mBlastSurfaceControl); |
| 746 |
mBlastSurfaceControl = null; |
| 747 |
} |
| 748 |
|
| 749 |
if (releaseSurfacePackage && mSurfacePackage != null) { |
| 750 |
mSurfacePackage.release(); |
| 751 |
mSurfacePackage = null; |
| 752 |
} |
| 753 |
|
| 754 |
applyTransactionOnVriDraw(transaction); |
| 755 |
} |
| 756 |
} |
| 757 |
|
| 758 |
|
| 759 |
|
| 760 |
|
| 761 |
|
| 762 |
|
| 763 |
|
| 764 |
|
| 765 |
private void replacePositionUpdateListener(int surfaceWidth, int surfaceHeight) { |
| 766 |
if (mPositionListener != null) { |
| 767 |
mRenderNode.removePositionUpdateListener(mPositionListener); |
| 768 |
} |
| 769 |
mPositionListener = new SurfaceViewPositionUpdateListener(surfaceWidth, surfaceHeight); |
| 770 |
mRenderNode.addPositionUpdateListener(mPositionListener); |
| 771 |
} |
| 772 |
|
| 773 |
private boolean performSurfaceTransaction(ViewRootImpl viewRoot, Translator translator, |
| 774 |
boolean creating, boolean sizeChanged, boolean hintChanged, |
| 775 |
Transaction surfaceUpdateTransaction) { |
| 776 |
boolean realSizeChanged = false; |
| 777 |
|
| 778 |
mSurfaceLock.lock(); |
| 779 |
try { |
| 780 |
mDrawingStopped = !mVisible; |
| 781 |
|
| 782 |
if (DEBUG) Log.i(TAG, System.identityHashCode(this) + " " |
| 783 |
+ "Cur surface: " + mSurface); |
| 784 |
|
| 785 |
|
| 786 |
|
| 787 |
|
| 788 |
|
| 789 |
|
| 790 |
if (creating) { |
| 791 |
updateRelativeZ(surfaceUpdateTransaction); |
| 792 |
if (mSurfacePackage != null) { |
| 793 |
reparentSurfacePackage(surfaceUpdateTransaction, mSurfacePackage); |
| 794 |
} |
| 795 |
} |
| 796 |
mParentSurfaceSequenceId = viewRoot.getSurfaceSequenceId(); |
| 797 |
|
| 798 |
if (mViewVisibility) { |
| 799 |
surfaceUpdateTransaction.show(mSurfaceControl); |
| 800 |
} else { |
| 801 |
surfaceUpdateTransaction.hide(mSurfaceControl); |
| 802 |
} |
| 803 |
|
| 804 |
|
| 805 |
|
| 806 |
updateBackgroundVisibility(surfaceUpdateTransaction); |
| 807 |
updateBackgroundColor(surfaceUpdateTransaction); |
| 808 |
if (mUseAlpha) { |
| 809 |
float alpha = getFixedAlpha(); |
| 810 |
surfaceUpdateTransaction.setAlpha(mSurfaceControl, alpha); |
| 811 |
mSurfaceAlpha = alpha; |
| 812 |
} |
| 813 |
|
| 814 |
surfaceUpdateTransaction.setCornerRadius(mSurfaceControl, mCornerRadius); |
| 815 |
if ((sizeChanged || hintChanged) && !creating) { |
| 816 |
setBufferSize(surfaceUpdateTransaction); |
| 817 |
} |
| 818 |
if (sizeChanged || creating || !isHardwareAccelerated()) { |
| 819 |
|
| 820 |
|
| 821 |
|
| 822 |
|
| 823 |
|
| 824 |
if (mClipSurfaceToBounds && mClipBounds != null) { |
| 825 |
surfaceUpdateTransaction.setWindowCrop(mSurfaceControl, mClipBounds); |
| 826 |
} else { |
| 827 |
surfaceUpdateTransaction.setWindowCrop(mSurfaceControl, mSurfaceWidth, |
| 828 |
mSurfaceHeight); |
| 829 |
} |
| 830 |
|
| 831 |
surfaceUpdateTransaction.setDesintationFrame(mBlastSurfaceControl, mSurfaceWidth, |
| 832 |
mSurfaceHeight); |
| 833 |
|
| 834 |
if (isHardwareAccelerated()) { |
| 835 |
|
| 836 |
|
| 837 |
replacePositionUpdateListener(mSurfaceWidth, mSurfaceHeight); |
| 838 |
} else { |
| 839 |
onSetSurfacePositionAndScale(surfaceUpdateTransaction, mSurfaceControl, |
| 840 |
mScreenRect.left , |
| 841 |
mScreenRect.top , |
| 842 |
mScreenRect.width() / (float) mSurfaceWidth , |
| 843 |
mScreenRect.height() / (float) mSurfaceHeight ); |
| 844 |
} |
| 845 |
if (DEBUG_POSITION) { |
| 846 |
Log.d(TAG, String.format( |
| 847 |
"%d performSurfaceTransaction %s " |
| 848 |
+ "position = [%d, %d, %d, %d] surfaceSize = %dx%d", |
| 849 |
System.identityHashCode(this), |
| 850 |
isHardwareAccelerated() ? "RenderWorker" : "UI Thread", |
| 851 |
mScreenRect.left, mScreenRect.top, mScreenRect.right, |
| 852 |
mScreenRect.bottom, mSurfaceWidth, mSurfaceHeight)); |
| 853 |
} |
| 854 |
} |
| 855 |
applyTransactionOnVriDraw(surfaceUpdateTransaction); |
| 856 |
updateEmbeddedAccessibilityMatrix(false); |
| 857 |
|
| 858 |
mSurfaceFrame.left = 0; |
| 859 |
mSurfaceFrame.top = 0; |
| 860 |
if (translator == null) { |
| 861 |
mSurfaceFrame.right = mSurfaceWidth; |
| 862 |
mSurfaceFrame.bottom = mSurfaceHeight; |
| 863 |
} else { |
| 864 |
float appInvertedScale = translator.applicationInvertedScale; |
| 865 |
mSurfaceFrame.right = (int) (mSurfaceWidth * appInvertedScale + 0.5f); |
| 866 |
mSurfaceFrame.bottom = (int) (mSurfaceHeight * appInvertedScale + 0.5f); |
| 867 |
} |
| 868 |
final int surfaceWidth = mSurfaceFrame.right; |
| 869 |
final int surfaceHeight = mSurfaceFrame.bottom; |
| 870 |
realSizeChanged = mLastSurfaceWidth != surfaceWidth |
| 871 |
|| mLastSurfaceHeight != surfaceHeight; |
| 872 |
mLastSurfaceWidth = surfaceWidth; |
| 873 |
mLastSurfaceHeight = surfaceHeight; |
| 874 |
} finally { |
| 875 |
mSurfaceLock.unlock(); |
| 876 |
} |
| 877 |
return realSizeChanged; |
| 878 |
} |
| 879 |
|
| 880 |
|
| 881 |
protected void updateSurface() { |
| 882 |
if (!mHaveFrame) { |
| 883 |
if (DEBUG) { |
| 884 |
Log.d(TAG, System.identityHashCode(this) + " updateSurface: has no frame"); |
| 885 |
} |
| 886 |
return; |
| 887 |
} |
| 888 |
final ViewRootImpl viewRoot = getViewRootImpl(); |
| 889 |
|
| 890 |
if (viewRoot == null) { |
| 891 |
return; |
| 892 |
} |
| 893 |
|
| 894 |
if (viewRoot.mSurface == null || !viewRoot.mSurface.isValid()) { |
| 895 |
notifySurfaceDestroyed(); |
| 896 |
releaseSurfaces(false ); |
| 897 |
return; |
| 898 |
} |
| 899 |
|
| 900 |
final Translator translator = viewRoot.mTranslator; |
| 901 |
if (translator != null) { |
| 902 |
mSurface.setCompatibilityTranslator(translator); |
| 903 |
} |
| 904 |
|
| 905 |
int myWidth = mRequestedWidth; |
| 906 |
if (myWidth <= 0) myWidth = getWidth(); |
| 907 |
int myHeight = mRequestedHeight; |
| 908 |
if (myHeight <= 0) myHeight = getHeight(); |
| 909 |
|
| 910 |
final float alpha = getFixedAlpha(); |
| 911 |
final boolean formatChanged = mFormat != mRequestedFormat; |
| 912 |
final boolean visibleChanged = mVisible != mRequestedVisible; |
| 913 |
final boolean alphaChanged = mSurfaceAlpha != alpha; |
| 914 |
final boolean creating = (mSurfaceControl == null || formatChanged || visibleChanged) |
| 915 |
&& mRequestedVisible; |
| 916 |
final boolean sizeChanged = mSurfaceWidth != myWidth || mSurfaceHeight != myHeight; |
| 917 |
final boolean windowVisibleChanged = mWindowVisibility != mLastWindowVisibility; |
| 918 |
getLocationInSurface(mLocation); |
| 919 |
final boolean positionChanged = mWindowSpaceLeft != mLocation[0] |
| 920 |
|| mWindowSpaceTop != mLocation[1]; |
| 921 |
final boolean layoutSizeChanged = getWidth() != mScreenRect.width() |
| 922 |
|| getHeight() != mScreenRect.height(); |
| 923 |
final boolean hintChanged = (viewRoot.getBufferTransformHint() != mTransformHint) |
| 924 |
&& mRequestedVisible; |
| 925 |
|
| 926 |
if (creating || formatChanged || sizeChanged || visibleChanged || |
| 927 |
(mUseAlpha && alphaChanged) || windowVisibleChanged || |
| 928 |
positionChanged || layoutSizeChanged || hintChanged) { |
| 929 |
getLocationInWindow(mLocation); |
| 930 |
|
| 931 |
if (DEBUG) Log.i(TAG, System.identityHashCode(this) + " " |
| 932 |
+ "Changes: creating=" + creating |
| 933 |
+ " format=" + formatChanged + " size=" + sizeChanged |
| 934 |
+ " visible=" + visibleChanged + " alpha=" + alphaChanged |
| 935 |
+ " hint=" + hintChanged |
| 936 |
+ " mUseAlpha=" + mUseAlpha |
| 937 |
+ " visible=" + visibleChanged |
| 938 |
+ " left=" + (mWindowSpaceLeft != mLocation[0]) |
| 939 |
+ " top=" + (mWindowSpaceTop != mLocation[1])); |
| 940 |
|
| 941 |
try { |
| 942 |
mVisible = mRequestedVisible; |
| 943 |
mWindowSpaceLeft = mLocation[0]; |
| 944 |
mWindowSpaceTop = mLocation[1]; |
| 945 |
mSurfaceWidth = myWidth; |
| 946 |
mSurfaceHeight = myHeight; |
| 947 |
mFormat = mRequestedFormat; |
| 948 |
mLastWindowVisibility = mWindowVisibility; |
| 949 |
mTransformHint = viewRoot.getBufferTransformHint(); |
| 950 |
|
| 951 |
mScreenRect.left = mWindowSpaceLeft; |
| 952 |
mScreenRect.top = mWindowSpaceTop; |
| 953 |
mScreenRect.right = mWindowSpaceLeft + getWidth(); |
| 954 |
mScreenRect.bottom = mWindowSpaceTop + getHeight(); |
| 955 |
if (translator != null) { |
| 956 |
translator.translateRectInAppWindowToScreen(mScreenRect); |
| 957 |
} |
| 958 |
|
| 959 |
final Rect surfaceInsets = viewRoot.mWindowAttributes.surfaceInsets; |
| 960 |
mScreenRect.offset(surfaceInsets.left, surfaceInsets.top); |
| 961 |
|
| 962 |
|
| 963 |
final Transaction surfaceUpdateTransaction = new Transaction(); |
| 964 |
if (creating) { |
| 965 |
updateOpaqueFlag(); |
| 966 |
final String name = "SurfaceView[" + viewRoot.getTitle().toString() + "]"; |
| 967 |
createBlastSurfaceControls(viewRoot, name, surfaceUpdateTransaction); |
| 968 |
} else if (mSurfaceControl == null) { |
| 969 |
return; |
| 970 |
} |
| 971 |
|
| 972 |
final boolean redrawNeeded = sizeChanged || creating || hintChanged |
| 973 |
|| (mVisible && !mDrawFinished); |
| 974 |
boolean shouldSyncBuffer = |
| 975 |
redrawNeeded && viewRoot.wasRelayoutRequested() && viewRoot.isInLocalSync(); |
| 976 |
SyncBufferTransactionCallback syncBufferTransactionCallback = null; |
| 977 |
if (shouldSyncBuffer) { |
| 978 |
syncBufferTransactionCallback = new SyncBufferTransactionCallback(); |
| 979 |
mBlastBufferQueue.syncNextTransaction( |
| 980 |
false , |
| 981 |
syncBufferTransactionCallback::onTransactionReady); |
| 982 |
} |
| 983 |
|
| 984 |
final boolean realSizeChanged = performSurfaceTransaction(viewRoot, |
| 985 |
translator, creating, sizeChanged, hintChanged, surfaceUpdateTransaction); |
| 986 |
|
| 987 |
try { |
| 988 |
SurfaceHolder.Callback[] callbacks = null; |
| 989 |
|
| 990 |
final boolean surfaceChanged = creating; |
| 991 |
if (mSurfaceCreated && (surfaceChanged || (!mVisible && visibleChanged))) { |
| 992 |
mSurfaceCreated = false; |
| 993 |
notifySurfaceDestroyed(); |
| 994 |
} |
| 995 |
|
| 996 |
copySurface(creating , sizeChanged); |
| 997 |
|
| 998 |
if (mVisible && mSurface.isValid()) { |
| 999 |
if (!mSurfaceCreated && (surfaceChanged || visibleChanged)) { |
| 1000 |
mSurfaceCreated = true; |
| 1001 |
mIsCreating = true; |
| 1002 |
if (DEBUG) Log.i(TAG, System.identityHashCode(this) + " " |
| 1003 |
+ "visibleChanged -- surfaceCreated"); |
| 1004 |
callbacks = getSurfaceCallbacks(); |
| 1005 |
for (SurfaceHolder.Callback c : callbacks) { |
| 1006 |
c.surfaceCreated(mSurfaceHolder); |
| 1007 |
} |
| 1008 |
} |
| 1009 |
if (creating || formatChanged || sizeChanged || hintChanged |
| 1010 |
|| visibleChanged || realSizeChanged) { |
| 1011 |
if (DEBUG) Log.i(TAG, System.identityHashCode(this) + " " |
| 1012 |
+ "surfaceChanged -- format=" + mFormat |
| 1013 |
+ " w=" + myWidth + " h=" + myHeight); |
| 1014 |
if (callbacks == null) { |
| 1015 |
callbacks = getSurfaceCallbacks(); |
| 1016 |
} |
| 1017 |
for (SurfaceHolder.Callback c : callbacks) { |
| 1018 |
c.surfaceChanged(mSurfaceHolder, mFormat, myWidth, myHeight); |
| 1019 |
} |
| 1020 |
} |
| 1021 |
if (redrawNeeded) { |
| 1022 |
if (DEBUG) { |
| 1023 |
Log.i(TAG, System.identityHashCode(this) + " surfaceRedrawNeeded"); |
| 1024 |
} |
| 1025 |
if (callbacks == null) { |
| 1026 |
callbacks = getSurfaceCallbacks(); |
| 1027 |
} |
| 1028 |
|
| 1029 |
if (shouldSyncBuffer) { |
| 1030 |
handleSyncBufferCallback(callbacks, syncBufferTransactionCallback); |
| 1031 |
} else { |
| 1032 |
handleSyncNoBuffer(callbacks); |
| 1033 |
} |
| 1034 |
} |
| 1035 |
} |
| 1036 |
} finally { |
| 1037 |
mIsCreating = false; |
| 1038 |
if (mSurfaceControl != null && !mSurfaceCreated) { |
| 1039 |
releaseSurfaces(false ); |
| 1040 |
} |
| 1041 |
} |
| 1042 |
} catch (Exception ex) { |
| 1043 |
Log.e(TAG, "Exception configuring surface", ex); |
| 1044 |
} |
| 1045 |
if (DEBUG) Log.v( |
| 1046 |
TAG, "Layout: x=" + mScreenRect.left + " y=" + mScreenRect.top |
| 1047 |
+ " w=" + mScreenRect.width() + " h=" + mScreenRect.height() |
| 1048 |
+ ", frame=" + mSurfaceFrame); |
| 1049 |
} |
| 1050 |
} |
| 1051 |
|
| 1052 |
|
| 1053 |
|
| 1054 |
|
| 1055 |
|
| 1056 |
|
| 1057 |
|
| 1058 |
|
| 1059 |
|
| 1060 |
|
| 1061 |
private void handleSyncBufferCallback(SurfaceHolder.Callback[] callbacks, |
| 1062 |
SyncBufferTransactionCallback syncBufferTransactionCallback) { |
| 1063 |
|
| 1064 |
getViewRootImpl().addToSync(syncBufferCallback -> |
| 1065 |
redrawNeededAsync(callbacks, () -> { |
| 1066 |
Transaction t = null; |
| 1067 |
if (mBlastBufferQueue != null) { |
| 1068 |
mBlastBufferQueue.stopContinuousSyncTransaction(); |
| 1069 |
t = syncBufferTransactionCallback.waitForTransaction(); |
| 1070 |
} |
| 1071 |
|
| 1072 |
syncBufferCallback.onBufferReady(t); |
| 1073 |
onDrawFinished(); |
| 1074 |
})); |
| 1075 |
} |
| 1076 |
|
| 1077 |
private void handleSyncNoBuffer(SurfaceHolder.Callback[] callbacks) { |
| 1078 |
final int syncId = mSurfaceSyncer.setupSync(this::onDrawFinished); |
| 1079 |
|
| 1080 |
mSurfaceSyncer.addToSync(syncId, syncBufferCallback -> redrawNeededAsync(callbacks, |
| 1081 |
() -> { |
| 1082 |
syncBufferCallback.onBufferReady(null); |
| 1083 |
synchronized (mSyncIds) { |
| 1084 |
mSyncIds.remove(syncId); |
| 1085 |
} |
| 1086 |
})); |
| 1087 |
|
| 1088 |
mSurfaceSyncer.markSyncReady(syncId); |
| 1089 |
synchronized (mSyncIds) { |
| 1090 |
mSyncIds.add(syncId); |
| 1091 |
} |
| 1092 |
} |
| 1093 |
|
| 1094 |
private void redrawNeededAsync(SurfaceHolder.Callback[] callbacks, |
| 1095 |
Runnable callbacksCollected) { |
| 1096 |
SurfaceCallbackHelper sch = new SurfaceCallbackHelper(callbacksCollected); |
| 1097 |
sch.dispatchSurfaceRedrawNeededAsync(mSurfaceHolder, callbacks); |
| 1098 |
} |
| 1099 |
|
| 1100 |
|
| 1101 |
|
| 1102 |
|
| 1103 |
@Override |
| 1104 |
public void surfaceSyncStarted() { |
| 1105 |
ViewRootImpl viewRoot = getViewRootImpl(); |
| 1106 |
if (viewRoot != null) { |
| 1107 |
synchronized (mSyncIds) { |
| 1108 |
for (int syncId : mSyncIds) { |
| 1109 |
viewRoot.mergeSync(syncId, mSurfaceSyncer); |
| 1110 |
} |
| 1111 |
} |
| 1112 |
} |
| 1113 |
} |
| 1114 |
|
| 1115 |
private static class SyncBufferTransactionCallback { |
| 1116 |
private final CountDownLatch mCountDownLatch = new CountDownLatch(1); |
| 1117 |
private Transaction mTransaction; |
| 1118 |
|
| 1119 |
Transaction waitForTransaction() { |
| 1120 |
try { |
| 1121 |
mCountDownLatch.await(); |
| 1122 |
} catch (InterruptedException e) { |
| 1123 |
} |
| 1124 |
return mTransaction; |
| 1125 |
} |
| 1126 |
|
| 1127 |
void onTransactionReady(Transaction t) { |
| 1128 |
mTransaction = t; |
| 1129 |
mCountDownLatch.countDown(); |
| 1130 |
} |
| 1131 |
} |
| 1132 |
|
| 1133 |
|
| 1134 |
|
| 1135 |
|
| 1136 |
|
| 1137 |
|
| 1138 |
|
| 1139 |
|
| 1140 |
|
| 1141 |
private void copySurface(boolean surfaceControlCreated, boolean bufferSizeChanged) { |
| 1142 |
if (surfaceControlCreated) { |
| 1143 |
mSurface.copyFrom(mBlastBufferQueue); |
| 1144 |
} |
| 1145 |
|
| 1146 |
if (bufferSizeChanged && getContext().getApplicationInfo().targetSdkVersion |
| 1147 |
< Build.VERSION_CODES.O) { |
| 1148 |
|
| 1149 |
|
| 1150 |
|
| 1151 |
|
| 1152 |
|
| 1153 |
if (mBlastBufferQueue != null) { |
| 1154 |
mSurface.transferFrom(mBlastBufferQueue.createSurfaceWithHandle()); |
| 1155 |
} |
| 1156 |
} |
| 1157 |
} |
| 1158 |
|
| 1159 |
private void setBufferSize(Transaction transaction) { |
| 1160 |
mBlastSurfaceControl.setTransformHint(mTransformHint); |
| 1161 |
if (mBlastBufferQueue != null) { |
| 1162 |
mBlastBufferQueue.update(mBlastSurfaceControl, mSurfaceWidth, mSurfaceHeight, |
| 1163 |
mFormat); |
| 1164 |
} |
| 1165 |
} |
| 1166 |
|
| 1167 |
|
| 1168 |
|
| 1169 |
|
| 1170 |
|
| 1171 |
|
| 1172 |
|
| 1173 |
|
| 1174 |
|
| 1175 |
|
| 1176 |
|
| 1177 |
|
| 1178 |
|
| 1179 |
|
| 1180 |
|
| 1181 |
|
| 1182 |
|
| 1183 |
|
| 1184 |
|
| 1185 |
|
| 1186 |
|
| 1187 |
|
| 1188 |
|
| 1189 |
|
| 1190 |
|
| 1191 |
private void createBlastSurfaceControls(ViewRootImpl viewRoot, String name, |
| 1192 |
Transaction surfaceUpdateTransaction) { |
| 1193 |
if (mSurfaceControl == null) { |
| 1194 |
mSurfaceControl = new SurfaceControl.Builder(mSurfaceSession) |
| 1195 |
.setName(name) |
| 1196 |
.setLocalOwnerView(this) |
| 1197 |
.setParent(viewRoot.getBoundsLayer()) |
| 1198 |
.setCallsite("SurfaceView.updateSurface") |
| 1199 |
.setContainerLayer() |
| 1200 |
.build(); |
| 1201 |
} |
| 1202 |
|
| 1203 |
if (mBlastSurfaceControl == null) { |
| 1204 |
mBlastSurfaceControl = new SurfaceControl.Builder(mSurfaceSession) |
| 1205 |
.setName(name + "(BLAST)") |
| 1206 |
.setLocalOwnerView(this) |
| 1207 |
.setParent(mSurfaceControl) |
| 1208 |
.setFlags(mSurfaceFlags) |
| 1209 |
.setHidden(false) |
| 1210 |
.setBLASTLayer() |
| 1211 |
.setCallsite("SurfaceView.updateSurface") |
| 1212 |
.build(); |
| 1213 |
} else { |
| 1214 |
|
| 1215 |
surfaceUpdateTransaction |
| 1216 |
.setOpaque(mBlastSurfaceControl, (mSurfaceFlags & SurfaceControl.OPAQUE) != 0) |
| 1217 |
.setSecure(mBlastSurfaceControl, (mSurfaceFlags & SurfaceControl.SECURE) != 0) |
| 1218 |
.show(mBlastSurfaceControl); |
| 1219 |
} |
| 1220 |
|
| 1221 |
if (mBackgroundControl == null) { |
| 1222 |
mBackgroundControl = new SurfaceControl.Builder(mSurfaceSession) |
| 1223 |
.setName("Background for " + name) |
| 1224 |
.setLocalOwnerView(this) |
| 1225 |
.setOpaque(true) |
| 1226 |
.setColorLayer() |
| 1227 |
.setParent(mSurfaceControl) |
| 1228 |
.setCallsite("SurfaceView.updateSurface") |
| 1229 |
.build(); |
| 1230 |
} |
| 1231 |
|
| 1232 |
|
| 1233 |
|
| 1234 |
if (mBlastBufferQueue != null) { |
| 1235 |
mBlastBufferQueue.destroy(); |
| 1236 |
} |
| 1237 |
mTransformHint = viewRoot.getBufferTransformHint(); |
| 1238 |
mBlastSurfaceControl.setTransformHint(mTransformHint); |
| 1239 |
|
| 1240 |
mBlastBufferQueue = new BLASTBufferQueue(name, false ); |
| 1241 |
mBlastBufferQueue.update(mBlastSurfaceControl, mSurfaceWidth, mSurfaceHeight, mFormat); |
| 1242 |
mBlastBufferQueue.setTransactionHangCallback(ViewRootImpl.sTransactionHangCallback); |
| 1243 |
} |
| 1244 |
|
| 1245 |
private void onDrawFinished() { |
| 1246 |
if (DEBUG) { |
| 1247 |
Log.i(TAG, System.identityHashCode(this) + " " |
| 1248 |
+ "finishedDrawing"); |
| 1249 |
} |
| 1250 |
|
| 1251 |
runOnUiThread(this::performDrawFinished); |
| 1252 |
} |
| 1253 |
|
| 1254 |
|
| 1255 |
|
| 1256 |
|
| 1257 |
|
| 1258 |
|
| 1259 |
|
| 1260 |
|
| 1261 |
|
| 1262 |
|
| 1263 |
|
| 1264 |
|
| 1265 |
|
| 1266 |
|
| 1267 |
protected void onSetSurfacePositionAndScale(@NonNull Transaction transaction, |
| 1268 |
@NonNull SurfaceControl surface, int positionLeft, int positionTop, |
| 1269 |
float postScaleX, float postScaleY) { |
| 1270 |
transaction.setPosition(surface, positionLeft, positionTop); |
| 1271 |
transaction.setMatrix(surface, postScaleX , 0f , |
| 1272 |
0f , postScaleY ); |
| 1273 |
} |
| 1274 |
|
| 1275 |
|
| 1276 |
public void requestUpdateSurfacePositionAndScale() { |
| 1277 |
if (mSurfaceControl == null) { |
| 1278 |
return; |
| 1279 |
} |
| 1280 |
final Transaction transaction = new Transaction(); |
| 1281 |
onSetSurfacePositionAndScale(transaction, mSurfaceControl, |
| 1282 |
mScreenRect.left, |
| 1283 |
mScreenRect.top , |
| 1284 |
mScreenRect.width() / (float) mSurfaceWidth , |
| 1285 |
mScreenRect.height() / (float) mSurfaceHeight ); |
| 1286 |
applyTransactionOnVriDraw(transaction); |
| 1287 |
invalidate(); |
| 1288 |
} |
| 1289 |
|
| 1290 |
|
| 1291 |
|
| 1292 |
|
| 1293 |
|
| 1294 |
|
| 1295 |
public @NonNull Rect getSurfaceRenderPosition() { |
| 1296 |
return mRTLastReportedPosition; |
| 1297 |
} |
| 1298 |
|
| 1299 |
private void applyOrMergeTransaction(Transaction t, long frameNumber) { |
| 1300 |
final ViewRootImpl viewRoot = getViewRootImpl(); |
| 1301 |
if (viewRoot != null) { |
| 1302 |
|
| 1303 |
viewRoot.mergeWithNextTransaction(t, frameNumber); |
| 1304 |
} else { |
| 1305 |
t.apply(); |
| 1306 |
} |
| 1307 |
} |
| 1308 |
|
| 1309 |
private final Rect mRTLastReportedPosition = new Rect(); |
| 1310 |
private final Point mRTLastReportedSurfaceSize = new Point(); |
| 1311 |
|
| 1312 |
private class SurfaceViewPositionUpdateListener implements RenderNode.PositionUpdateListener { |
| 1313 |
private final int mRtSurfaceWidth; |
| 1314 |
private final int mRtSurfaceHeight; |
| 1315 |
private boolean mRtFirst = true; |
| 1316 |
private final SurfaceControl.Transaction mPositionChangedTransaction = |
| 1317 |
new SurfaceControl.Transaction(); |
| 1318 |
|
| 1319 |
SurfaceViewPositionUpdateListener(int surfaceWidth, int surfaceHeight) { |
| 1320 |
mRtSurfaceWidth = surfaceWidth; |
| 1321 |
mRtSurfaceHeight = surfaceHeight; |
| 1322 |
} |
| 1323 |
|
| 1324 |
@Override |
| 1325 |
public void positionChanged(long frameNumber, int left, int top, int right, int bottom) { |
| 1326 |
if (!mRtFirst && (mRTLastReportedPosition.left == left |
| 1327 |
&& mRTLastReportedPosition.top == top |
| 1328 |
&& mRTLastReportedPosition.right == right |
| 1329 |
&& mRTLastReportedPosition.bottom == bottom |
| 1330 |
&& mRTLastReportedSurfaceSize.x == mRtSurfaceWidth |
| 1331 |
&& mRTLastReportedSurfaceSize.y == mRtSurfaceHeight)) { |
| 1332 |
return; |
| 1333 |
} |
| 1334 |
mRtFirst = false; |
| 1335 |
try { |
| 1336 |
if (DEBUG_POSITION) { |
| 1337 |
Log.d(TAG, String.format( |
| 1338 |
"%d updateSurfacePosition RenderWorker, frameNr = %d, " |
| 1339 |
+ "position = [%d, %d, %d, %d] surfaceSize = %dx%d", |
| 1340 |
System.identityHashCode(SurfaceView.this), frameNumber, |
| 1341 |
left, top, right, bottom, mRtSurfaceWidth, mRtSurfaceHeight)); |
| 1342 |
} |
| 1343 |
synchronized (mSurfaceControlLock) { |
| 1344 |
if (mSurfaceControl == null) return; |
| 1345 |
mRTLastReportedPosition.set(left, top, right, bottom); |
| 1346 |
mRTLastReportedSurfaceSize.set(mRtSurfaceWidth, mRtSurfaceHeight); |
| 1347 |
onSetSurfacePositionAndScale(mPositionChangedTransaction, mSurfaceControl, |
| 1348 |
mRTLastReportedPosition.left , |
| 1349 |
mRTLastReportedPosition.top , |
| 1350 |
mRTLastReportedPosition.width() |
| 1351 |
/ (float) mRtSurfaceWidth , |
| 1352 |
mRTLastReportedPosition.height() |
| 1353 |
/ (float) mRtSurfaceHeight ); |
| 1354 |
if (mViewVisibility) { |
| 1355 |
|
| 1356 |
mPositionChangedTransaction.show(mSurfaceControl); |
| 1357 |
} |
| 1358 |
} |
| 1359 |
applyOrMergeTransaction(mPositionChangedTransaction, frameNumber); |
| 1360 |
} catch (Exception ex) { |
| 1361 |
Log.e(TAG, "Exception from repositionChild", ex); |
| 1362 |
} |
| 1363 |
} |
| 1364 |
|
| 1365 |
@Override |
| 1366 |
public void applyStretch(long frameNumber, float width, float height, |
| 1367 |
float vecX, float vecY, float maxStretchX, float maxStretchY, |
| 1368 |
float childRelativeLeft, float childRelativeTop, float childRelativeRight, |
| 1369 |
float childRelativeBottom) { |
| 1370 |
mRtTransaction.setStretchEffect(mSurfaceControl, width, height, vecX, vecY, |
| 1371 |
maxStretchX, maxStretchY, childRelativeLeft, childRelativeTop, |
| 1372 |
childRelativeRight, childRelativeBottom); |
| 1373 |
applyOrMergeTransaction(mRtTransaction, frameNumber); |
| 1374 |
} |
| 1375 |
|
| 1376 |
@Override |
| 1377 |
public void positionLost(long frameNumber) { |
| 1378 |
if (DEBUG_POSITION) { |
| 1379 |
Log.d(TAG, String.format("%d windowPositionLost, frameNr = %d", |
| 1380 |
System.identityHashCode(this), frameNumber)); |
| 1381 |
} |
| 1382 |
mRTLastReportedPosition.setEmpty(); |
| 1383 |
mRTLastReportedSurfaceSize.set(-1, -1); |
| 1384 |
|
| 1385 |
|
| 1386 |
synchronized (mSurfaceControlLock) { |
| 1387 |
if (mSurfaceControl == null) return; |
| 1388 |
|
| 1389 |
mRtTransaction.hide(mSurfaceControl); |
| 1390 |
applyOrMergeTransaction(mRtTransaction, frameNumber); |
| 1391 |
} |
| 1392 |
} |
| 1393 |
} |
| 1394 |
|
| 1395 |
private SurfaceViewPositionUpdateListener mPositionListener = null; |
| 1396 |
|
| 1397 |
private SurfaceHolder.Callback[] getSurfaceCallbacks() { |
| 1398 |
SurfaceHolder.Callback[] callbacks; |
| 1399 |
synchronized (mCallbacks) { |
| 1400 |
callbacks = new SurfaceHolder.Callback[mCallbacks.size()]; |
| 1401 |
mCallbacks.toArray(callbacks); |
| 1402 |
} |
| 1403 |
return callbacks; |
| 1404 |
} |
| 1405 |
|
| 1406 |
private void runOnUiThread(Runnable runnable) { |
| 1407 |
Handler handler = getHandler(); |
| 1408 |
if (handler != null && handler.getLooper() != Looper.myLooper()) { |
| 1409 |
handler.post(runnable); |
| 1410 |
} else { |
| 1411 |
runnable.run(); |
| 1412 |
} |
| 1413 |
} |
| 1414 |
|
| 1415 |
|
| 1416 |
|
| 1417 |
|
| 1418 |
|
| 1419 |
|
| 1420 |
|
| 1421 |
|
| 1422 |
@UnsupportedAppUsage |
| 1423 |
public boolean isFixedSize() { |
| 1424 |
return (mRequestedWidth != -1 || mRequestedHeight != -1); |
| 1425 |
} |
| 1426 |
|
| 1427 |
private boolean isAboveParent() { |
| 1428 |
return mSubLayer >= 0; |
| 1429 |
} |
| 1430 |
|
| 1431 |
|
| 1432 |
|
| 1433 |
|
| 1434 |
|
| 1435 |
|
| 1436 |
|
| 1437 |
|
| 1438 |
public void setResizeBackgroundColor(int bgColor) { |
| 1439 |
final SurfaceControl.Transaction transaction = new SurfaceControl.Transaction(); |
| 1440 |
setResizeBackgroundColor(transaction, bgColor); |
| 1441 |
applyTransactionOnVriDraw(transaction); |
| 1442 |
invalidate(); |
| 1443 |
} |
| 1444 |
|
| 1445 |
|
| 1446 |
|
| 1447 |
|
| 1448 |
|
| 1449 |
|
| 1450 |
public void setResizeBackgroundColor(@NonNull SurfaceControl.Transaction t, int bgColor) { |
| 1451 |
if (mBackgroundControl == null) { |
| 1452 |
return; |
| 1453 |
} |
| 1454 |
mBackgroundColor = bgColor; |
| 1455 |
updateBackgroundColor(t); |
| 1456 |
} |
| 1457 |
|
| 1458 |
@UnsupportedAppUsage |
| 1459 |
private final SurfaceHolder mSurfaceHolder = new SurfaceHolder() { |
| 1460 |
private static final String LOG_TAG = "SurfaceHolder"; |
| 1461 |
|
| 1462 |
@Override |
| 1463 |
public boolean isCreating() { |
| 1464 |
return mIsCreating; |
| 1465 |
} |
| 1466 |
|
| 1467 |
@Override |
| 1468 |
public void addCallback(Callback callback) { |
| 1469 |
synchronized (mCallbacks) { |
| 1470 |
|
| 1471 |
|
| 1472 |
if (!mCallbacks.contains(callback)) { |
| 1473 |
mCallbacks.add(callback); |
| 1474 |
} |
| 1475 |
} |
| 1476 |
} |
| 1477 |
|
| 1478 |
@Override |
| 1479 |
public void removeCallback(Callback callback) { |
| 1480 |
synchronized (mCallbacks) { |
| 1481 |
mCallbacks.remove(callback); |
| 1482 |
} |
| 1483 |
} |
| 1484 |
|
| 1485 |
@Override |
| 1486 |
public void setFixedSize(int width, int height) { |
| 1487 |
if (mRequestedWidth != width || mRequestedHeight != height) { |
| 1488 |
if (DEBUG_POSITION) { |
| 1489 |
Log.d(TAG, String.format("%d setFixedSize %dx%d -> %dx%d", |
| 1490 |
System.identityHashCode(this), mRequestedWidth, mRequestedHeight, width, |
| 1491 |
height)); |
| 1492 |
} |
| 1493 |
mRequestedWidth = width; |
| 1494 |
mRequestedHeight = height; |
| 1495 |
requestLayout(); |
| 1496 |
} |
| 1497 |
} |
| 1498 |
|
| 1499 |
@Override |
| 1500 |
public void setSizeFromLayout() { |
| 1501 |
if (mRequestedWidth != -1 || mRequestedHeight != -1) { |
| 1502 |
if (DEBUG_POSITION) { |
| 1503 |
Log.d(TAG, String.format("%d setSizeFromLayout was %dx%d", |
| 1504 |
System.identityHashCode(this), mRequestedWidth, mRequestedHeight)); |
| 1505 |
} |
| 1506 |
mRequestedWidth = mRequestedHeight = -1; |
| 1507 |
requestLayout(); |
| 1508 |
} |
| 1509 |
} |
| 1510 |
|
| 1511 |
@Override |
| 1512 |
public void setFormat(int format) { |
| 1513 |
|
| 1514 |
|
| 1515 |
if (format == PixelFormat.OPAQUE) |
| 1516 |
format = PixelFormat.RGB_565; |
| 1517 |
|
| 1518 |
mRequestedFormat = format; |
| 1519 |
if (mSurfaceControl != null) { |
| 1520 |
updateSurface(); |
| 1521 |
} |
| 1522 |
} |
| 1523 |
|
| 1524 |
|
| 1525 |
|
| 1526 |
|
| 1527 |
@Override |
| 1528 |
@Deprecated |
| 1529 |
public void setType(int type) { } |
| 1530 |
|
| 1531 |
@Override |
| 1532 |
public void setKeepScreenOn(boolean screenOn) { |
| 1533 |
runOnUiThread(() -> SurfaceView.this.setKeepScreenOn(screenOn)); |
| 1534 |
} |
| 1535 |
|
| 1536 |
|
| 1537 |
|
| 1538 |
|
| 1539 |
|
| 1540 |
|
| 1541 |
|
| 1542 |
|
| 1543 |
|
| 1544 |
|
| 1545 |
@Override |
| 1546 |
public Canvas lockCanvas() { |
| 1547 |
return internalLockCanvas(null, false); |
| 1548 |
} |
| 1549 |
|
| 1550 |
|
| 1551 |
|
| 1552 |
|
| 1553 |
|
| 1554 |
|
| 1555 |
|
| 1556 |
|
| 1557 |
|
| 1558 |
|
| 1559 |
|
| 1560 |
|
| 1561 |
|
| 1562 |
|
| 1563 |
|
| 1564 |
|
| 1565 |
@Override |
| 1566 |
public Canvas lockCanvas(Rect inOutDirty) { |
| 1567 |
return internalLockCanvas(inOutDirty, false); |
| 1568 |
} |
| 1569 |
|
| 1570 |
@Override |
| 1571 |
public Canvas lockHardwareCanvas() { |
| 1572 |
return internalLockCanvas(null, true); |
| 1573 |
} |
| 1574 |
|
| 1575 |
private Canvas internalLockCanvas(Rect dirty, boolean hardware) { |
| 1576 |
mSurfaceLock.lock(); |
| 1577 |
|
| 1578 |
if (DEBUG) Log.i(TAG, System.identityHashCode(this) + " " + "Locking canvas... stopped=" |
| 1579 |
+ mDrawingStopped + ", surfaceControl=" + mSurfaceControl); |
| 1580 |
|
| 1581 |
Canvas c = null; |
| 1582 |
if (!mDrawingStopped && mSurfaceControl != null) { |
| 1583 |
try { |
| 1584 |
if (hardware) { |
| 1585 |
c = mSurface.lockHardwareCanvas(); |
| 1586 |
} else { |
| 1587 |
c = mSurface.lockCanvas(dirty); |
| 1588 |
} |
| 1589 |
} catch (Exception e) { |
| 1590 |
Log.e(LOG_TAG, "Exception locking surface", e); |
| 1591 |
} |
| 1592 |
} |
| 1593 |
|
| 1594 |
if (DEBUG) Log.i(TAG, System.identityHashCode(this) + " " + "Returned canvas: " + c); |
| 1595 |
if (c != null) { |
| 1596 |
mLastLockTime = SystemClock.uptimeMillis(); |
| 1597 |
return c; |
| 1598 |
} |
| 1599 |
|
| 1600 |
|
| 1601 |
|
| 1602 |
|
| 1603 |
long now = SystemClock.uptimeMillis(); |
| 1604 |
long nextTime = mLastLockTime + 100; |
| 1605 |
if (nextTime > now) { |
| 1606 |
try { |
| 1607 |
Thread.sleep(nextTime-now); |
| 1608 |
} catch (InterruptedException e) { |
| 1609 |
} |
| 1610 |
now = SystemClock.uptimeMillis(); |
| 1611 |
} |
| 1612 |
mLastLockTime = now; |
| 1613 |
mSurfaceLock.unlock(); |
| 1614 |
|
| 1615 |
return null; |
| 1616 |
} |
| 1617 |
|
| 1618 |
|
| 1619 |
|
| 1620 |
|
| 1621 |
|
| 1622 |
|
| 1623 |
|
| 1624 |
@Override |
| 1625 |
public void unlockCanvasAndPost(Canvas canvas) { |
| 1626 |
try { |
| 1627 |
mSurface.unlockCanvasAndPost(canvas); |
| 1628 |
} finally { |
| 1629 |
mSurfaceLock.unlock(); |
| 1630 |
} |
| 1631 |
} |
| 1632 |
|
| 1633 |
@Override |
| 1634 |
public Surface getSurface() { |
| 1635 |
return mSurface; |
| 1636 |
} |
| 1637 |
|
| 1638 |
@Override |
| 1639 |
public Rect getSurfaceFrame() { |
| 1640 |
return mSurfaceFrame; |
| 1641 |
} |
| 1642 |
}; |
| 1643 |
|
| 1644 |
|
| 1645 |
|
| 1646 |
|
| 1647 |
|
| 1648 |
|
| 1649 |
|
| 1650 |
public SurfaceControl getSurfaceControl() { |
| 1651 |
return mSurfaceControl; |
| 1652 |
} |
| 1653 |
|
| 1654 |
|
| 1655 |
|
| 1656 |
|
| 1657 |
|
| 1658 |
|
| 1659 |
|
| 1660 |
public @Nullable IBinder getHostToken() { |
| 1661 |
final ViewRootImpl viewRoot = getViewRootImpl(); |
| 1662 |
if (viewRoot == null) { |
| 1663 |
return null; |
| 1664 |
} |
| 1665 |
return viewRoot.getInputToken(); |
| 1666 |
} |
| 1667 |
|
| 1668 |
|
| 1669 |
|
| 1670 |
|
| 1671 |
|
| 1672 |
|
| 1673 |
@Override |
| 1674 |
public void surfaceCreated(SurfaceControl.Transaction t) { |
| 1675 |
setWindowStopped(false); |
| 1676 |
} |
| 1677 |
|
| 1678 |
|
| 1679 |
|
| 1680 |
|
| 1681 |
|
| 1682 |
|
| 1683 |
@Override |
| 1684 |
public void surfaceDestroyed() { |
| 1685 |
setWindowStopped(true); |
| 1686 |
mRemoteAccessibilityController.disassosciateHierarchy(); |
| 1687 |
} |
| 1688 |
|
| 1689 |
|
| 1690 |
|
| 1691 |
|
| 1692 |
|
| 1693 |
|
| 1694 |
@Override |
| 1695 |
public void surfaceReplaced(Transaction t) { |
| 1696 |
if (mSurfaceControl != null && mBackgroundControl != null) { |
| 1697 |
updateRelativeZ(t); |
| 1698 |
} |
| 1699 |
} |
| 1700 |
|
| 1701 |
private void updateRelativeZ(Transaction t) { |
| 1702 |
final ViewRootImpl viewRoot = getViewRootImpl(); |
| 1703 |
if (viewRoot == null) { |
| 1704 |
|
| 1705 |
return; |
| 1706 |
} |
| 1707 |
final SurfaceControl viewRootControl = viewRoot.getSurfaceControl(); |
| 1708 |
t.setRelativeLayer(mBackgroundControl, viewRootControl, Integer.MIN_VALUE); |
| 1709 |
t.setRelativeLayer(mSurfaceControl, viewRootControl, mSubLayer); |
| 1710 |
} |
| 1711 |
|
| 1712 |
|
| 1713 |
|
| 1714 |
|
| 1715 |
|
| 1716 |
|
| 1717 |
|
| 1718 |
|
| 1719 |
|
| 1720 |
|
| 1721 |
|
| 1722 |
|
| 1723 |
|
| 1724 |
|
| 1725 |
|
| 1726 |
|
| 1727 |
|
| 1728 |
|
| 1729 |
|
| 1730 |
|
| 1731 |
|
| 1732 |
|
| 1733 |
|
| 1734 |
|
| 1735 |
public void setChildSurfacePackage(@NonNull SurfaceControlViewHost.SurfacePackage p) { |
| 1736 |
final SurfaceControl lastSc = mSurfacePackage != null ? |
| 1737 |
mSurfacePackage.getSurfaceControl() : null; |
| 1738 |
final SurfaceControl.Transaction transaction = new Transaction(); |
| 1739 |
if (mSurfaceControl != null) { |
| 1740 |
if (lastSc != null) { |
| 1741 |
transaction.reparent(lastSc, null); |
| 1742 |
mSurfacePackage.release(); |
| 1743 |
} |
| 1744 |
reparentSurfacePackage(transaction, p); |
| 1745 |
applyTransactionOnVriDraw(transaction); |
| 1746 |
} |
| 1747 |
mSurfacePackage = p; |
| 1748 |
invalidate(); |
| 1749 |
} |
| 1750 |
|
| 1751 |
private void reparentSurfacePackage(SurfaceControl.Transaction t, |
| 1752 |
SurfaceControlViewHost.SurfacePackage p) { |
| 1753 |
final SurfaceControl sc = p.getSurfaceControl(); |
| 1754 |
if (sc == null || !sc.isValid()) { |
| 1755 |
return; |
| 1756 |
} |
| 1757 |
initEmbeddedHierarchyForAccessibility(p); |
| 1758 |
t.reparent(sc, mBlastSurfaceControl).show(sc); |
| 1759 |
} |
| 1760 |
|
| 1761 |
|
| 1762 |
@Override |
| 1763 |
public void onInitializeAccessibilityNodeInfoInternal(AccessibilityNodeInfo info) { |
| 1764 |
super.onInitializeAccessibilityNodeInfoInternal(info); |
| 1765 |
if (!mRemoteAccessibilityController.connected()) { |
| 1766 |
return; |
| 1767 |
} |
| 1768 |
|
| 1769 |
|
| 1770 |
info.addChild(mRemoteAccessibilityController.getLeashToken()); |
| 1771 |
} |
| 1772 |
|
| 1773 |
@Override |
| 1774 |
public int getImportantForAccessibility() { |
| 1775 |
final int mode = super.getImportantForAccessibility(); |
| 1776 |
|
| 1777 |
|
| 1778 |
|
| 1779 |
if ((mRemoteAccessibilityController!= null && !mRemoteAccessibilityController.connected()) |
| 1780 |
|| mode != IMPORTANT_FOR_ACCESSIBILITY_AUTO) { |
| 1781 |
return mode; |
| 1782 |
} |
| 1783 |
return IMPORTANT_FOR_ACCESSIBILITY_YES; |
| 1784 |
} |
| 1785 |
|
| 1786 |
private void initEmbeddedHierarchyForAccessibility(SurfaceControlViewHost.SurfacePackage p) { |
| 1787 |
final IAccessibilityEmbeddedConnection connection = p.getAccessibilityEmbeddedConnection(); |
| 1788 |
if (mRemoteAccessibilityController.alreadyAssociated(connection)) { |
| 1789 |
return; |
| 1790 |
} |
| 1791 |
mRemoteAccessibilityController.assosciateHierarchy(connection, |
| 1792 |
getViewRootImpl().mLeashToken, getAccessibilityViewId()); |
| 1793 |
|
| 1794 |
updateEmbeddedAccessibilityMatrix(true); |
| 1795 |
} |
| 1796 |
|
| 1797 |
private void notifySurfaceDestroyed() { |
| 1798 |
if (mSurface.isValid()) { |
| 1799 |
if (DEBUG) Log.i(TAG, System.identityHashCode(this) + " " |
| 1800 |
+ "surfaceDestroyed"); |
| 1801 |
SurfaceHolder.Callback[] callbacks = getSurfaceCallbacks(); |
| 1802 |
for (SurfaceHolder.Callback c : callbacks) { |
| 1803 |
c.surfaceDestroyed(mSurfaceHolder); |
| 1804 |
} |
| 1805 |
|
| 1806 |
|
| 1807 |
|
| 1808 |
|
| 1809 |
|
| 1810 |
|
| 1811 |
|
| 1812 |
|
| 1813 |
|
| 1814 |
|
| 1815 |
|
| 1816 |
if (mSurface.isValid()) { |
| 1817 |
mSurface.forceScopedDisconnect(); |
| 1818 |
} |
| 1819 |
} |
| 1820 |
} |
| 1821 |
|
| 1822 |
void updateEmbeddedAccessibilityMatrix(boolean force) { |
| 1823 |
if (!mRemoteAccessibilityController.connected()) { |
| 1824 |
return; |
| 1825 |
} |
| 1826 |
getBoundsOnScreen(mTmpRect); |
| 1827 |
|
| 1828 |
|
| 1829 |
|
| 1830 |
|
| 1831 |
mTmpRect.offset(-mAttachInfo.mWindowLeft, -mAttachInfo.mWindowTop); |
| 1832 |
mTmpMatrix.reset(); |
| 1833 |
mTmpMatrix.setTranslate(mTmpRect.left, mTmpRect.top); |
| 1834 |
mTmpMatrix.postScale(mScreenRect.width() / (float) mSurfaceWidth, |
| 1835 |
mScreenRect.height() / (float) mSurfaceHeight); |
| 1836 |
mRemoteAccessibilityController.setWindowMatrix(mTmpMatrix, force); |
| 1837 |
} |
| 1838 |
|
| 1839 |
@Override |
| 1840 |
protected void onFocusChanged(boolean gainFocus, @FocusDirection int direction, |
| 1841 |
@Nullable Rect previouslyFocusedRect) { |
| 1842 |
super.onFocusChanged(gainFocus, direction, previouslyFocusedRect); |
| 1843 |
final ViewRootImpl viewRoot = getViewRootImpl(); |
| 1844 |
if (mSurfacePackage == null || viewRoot == null) { |
| 1845 |
return; |
| 1846 |
} |
| 1847 |
try { |
| 1848 |
viewRoot.mWindowSession.grantEmbeddedWindowFocus(viewRoot.mWindow, |
| 1849 |
mSurfacePackage.getInputToken(), gainFocus); |
| 1850 |
} catch (Exception e) { |
| 1851 |
Log.e(TAG, System.identityHashCode(this) |
| 1852 |
+ "Exception requesting focus on embedded window", e); |
| 1853 |
} |
| 1854 |
} |
| 1855 |
|
| 1856 |
private void applyTransactionOnVriDraw(Transaction t) { |
| 1857 |
final ViewRootImpl viewRoot = getViewRootImpl(); |
| 1858 |
if (viewRoot != null) { |
| 1859 |
|
| 1860 |
viewRoot.applyTransactionOnDraw(t); |
| 1861 |
} else { |
| 1862 |
t.apply(); |
| 1863 |
} |
| 1864 |
} |
| 1865 |
|
| 1866 |
|
| 1867 |
|
| 1868 |
|
| 1869 |
public void syncNextFrame(Consumer<Transaction> t) { |
| 1870 |
mBlastBufferQueue.syncNextTransaction(t); |
| 1871 |
} |
| 1872 |
} |