blob: 7e5464761e38c03eaf2ba37fa28b915578241d20 [file] [log] [blame]
Justin Klaassen10d07c82017-09-15 17:58:39 -04001/*
2 * Copyright (C) 2006 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
17package android.view;
18
Justin Klaassenb8042fc2018-04-15 00:41:15 -040019import static android.view.WindowManagerPolicyConstants.APPLICATION_MEDIA_OVERLAY_SUBLAYER;
20import static android.view.WindowManagerPolicyConstants.APPLICATION_MEDIA_SUBLAYER;
21import static android.view.WindowManagerPolicyConstants.APPLICATION_PANEL_SUBLAYER;
Justin Klaassen10d07c82017-09-15 17:58:39 -040022
23import android.content.Context;
Justin Klaassenb8042fc2018-04-15 00:41:15 -040024import android.content.res.CompatibilityInfo.Translator;
25import android.content.res.Configuration;
Justin Klaassen10d07c82017-09-15 17:58:39 -040026import android.graphics.Canvas;
Justin Klaassenb8042fc2018-04-15 00:41:15 -040027import android.graphics.Color;
28import android.graphics.PixelFormat;
29import android.graphics.PorterDuff;
Justin Klaassen10d07c82017-09-15 17:58:39 -040030import android.graphics.Rect;
31import android.graphics.Region;
Justin Klaassenb8042fc2018-04-15 00:41:15 -040032import android.os.Build;
33import android.os.Handler;
34import android.os.IBinder;
35import android.os.Looper;
36import android.os.SystemClock;
Justin Klaassen10d07c82017-09-15 17:58:39 -040037import android.util.AttributeSet;
Justin Klaassenb8042fc2018-04-15 00:41:15 -040038import android.util.Log;
39
40import com.android.internal.view.SurfaceCallbackHelper;
41
42import java.util.ArrayList;
43import java.util.concurrent.locks.ReentrantLock;
Justin Klaassen10d07c82017-09-15 17:58:39 -040044
45/**
Justin Klaassenb8042fc2018-04-15 00:41:15 -040046 * Provides a dedicated drawing surface embedded inside of a view hierarchy.
47 * You can control the format of this surface and, if you like, its size; the
48 * SurfaceView takes care of placing the surface at the correct location on the
49 * screen
Justin Klaassen10d07c82017-09-15 17:58:39 -040050 *
Justin Klaassenb8042fc2018-04-15 00:41:15 -040051 * <p>The surface is Z ordered so that it is behind the window holding its
52 * SurfaceView; the SurfaceView punches a hole in its window to allow its
53 * surface to be displayed. The view hierarchy will take care of correctly
54 * compositing with the Surface any siblings of the SurfaceView that would
55 * normally appear on top of it. This can be used to place overlays such as
56 * buttons on top of the Surface, though note however that it can have an
57 * impact on performance since a full alpha-blended composite will be performed
58 * each time the Surface changes.
Justin Klaassen10d07c82017-09-15 17:58:39 -040059 *
Justin Klaassenb8042fc2018-04-15 00:41:15 -040060 * <p> The transparent region that makes the surface visible is based on the
61 * layout positions in the view hierarchy. If the post-layout transform
62 * properties are used to draw a sibling view on top of the SurfaceView, the
63 * view may not be properly composited with the surface.
64 *
65 * <p>Access to the underlying surface is provided via the SurfaceHolder interface,
66 * which can be retrieved by calling {@link #getHolder}.
67 *
68 * <p>The Surface will be created for you while the SurfaceView's window is
69 * visible; you should implement {@link SurfaceHolder.Callback#surfaceCreated}
70 * and {@link SurfaceHolder.Callback#surfaceDestroyed} to discover when the
71 * Surface is created and destroyed as the window is shown and hidden.
72 *
73 * <p>One of the purposes of this class is to provide a surface in which a
74 * secondary thread can render into the screen. If you are going to use it
75 * this way, you need to be aware of some threading semantics:
76 *
77 * <ul>
78 * <li> All SurfaceView and
79 * {@link SurfaceHolder.Callback SurfaceHolder.Callback} methods will be called
80 * from the thread running the SurfaceView's window (typically the main thread
81 * of the application). They thus need to correctly synchronize with any
82 * state that is also touched by the drawing thread.
83 * <li> You must ensure that the drawing thread only touches the underlying
84 * Surface while it is valid -- between
85 * {@link SurfaceHolder.Callback#surfaceCreated SurfaceHolder.Callback.surfaceCreated()}
86 * and
87 * {@link SurfaceHolder.Callback#surfaceDestroyed SurfaceHolder.Callback.surfaceDestroyed()}.
88 * </ul>
89 *
90 * <p class="note"><strong>Note:</strong> Starting in platform version
91 * {@link android.os.Build.VERSION_CODES#N}, SurfaceView's window position is
92 * updated synchronously with other View rendering. This means that translating
93 * and scaling a SurfaceView on screen will not cause rendering artifacts. Such
94 * artifacts may occur on previous versions of the platform when its window is
95 * positioned asynchronously.</p>
Justin Klaassen10d07c82017-09-15 17:58:39 -040096 */
Justin Klaassenb8042fc2018-04-15 00:41:15 -040097public class SurfaceView extends View implements ViewRootImpl.WindowStoppedCallback {
98 private static final String TAG = "SurfaceView";
99 private static final boolean DEBUG = false;
100
101 final ArrayList<SurfaceHolder.Callback> mCallbacks
102 = new ArrayList<SurfaceHolder.Callback>();
103
104 final int[] mLocation = new int[2];
105
106 final ReentrantLock mSurfaceLock = new ReentrantLock();
107 final Surface mSurface = new Surface(); // Current surface in use
108 boolean mDrawingStopped = true;
109 // We use this to track if the application has produced a frame
110 // in to the Surface. Up until that point, we should be careful not to punch
111 // holes.
112 boolean mDrawFinished = false;
113
114 final Rect mScreenRect = new Rect();
115 SurfaceSession mSurfaceSession;
116
117 SurfaceControlWithBackground mSurfaceControl;
118 // In the case of format changes we switch out the surface in-place
119 // we need to preserve the old one until the new one has drawn.
120 SurfaceControl mDeferredDestroySurfaceControl;
121 final Rect mTmpRect = new Rect();
122 final Configuration mConfiguration = new Configuration();
123
124 int mSubLayer = APPLICATION_MEDIA_SUBLAYER;
125
126 boolean mIsCreating = false;
127 private volatile boolean mRtHandlingPositionUpdates = false;
128
129 private final ViewTreeObserver.OnScrollChangedListener mScrollChangedListener
130 = new ViewTreeObserver.OnScrollChangedListener() {
131 @Override
132 public void onScrollChanged() {
133 updateSurface();
134 }
135 };
136
137 private final ViewTreeObserver.OnPreDrawListener mDrawListener =
138 new ViewTreeObserver.OnPreDrawListener() {
139 @Override
140 public boolean onPreDraw() {
141 // reposition ourselves where the surface is
142 mHaveFrame = getWidth() > 0 && getHeight() > 0;
143 updateSurface();
144 return true;
145 }
146 };
147
148 boolean mRequestedVisible = false;
149 boolean mWindowVisibility = false;
150 boolean mLastWindowVisibility = false;
151 boolean mViewVisibility = false;
152 boolean mWindowStopped = false;
153
154 int mRequestedWidth = -1;
155 int mRequestedHeight = -1;
156 /* Set SurfaceView's format to 565 by default to maintain backward
157 * compatibility with applications assuming this format.
158 */
159 int mRequestedFormat = PixelFormat.RGB_565;
160
161 boolean mHaveFrame = false;
162 boolean mSurfaceCreated = false;
163 long mLastLockTime = 0;
164
165 boolean mVisible = false;
166 int mWindowSpaceLeft = -1;
167 int mWindowSpaceTop = -1;
168 int mSurfaceWidth = -1;
169 int mSurfaceHeight = -1;
170 int mFormat = -1;
171 final Rect mSurfaceFrame = new Rect();
172 int mLastSurfaceWidth = -1, mLastSurfaceHeight = -1;
173 private Translator mTranslator;
174
175 private boolean mGlobalListenersAdded;
176 private boolean mAttachedToWindow;
177
178 private int mSurfaceFlags = SurfaceControl.HIDDEN;
179
180 private int mPendingReportDraws;
181
182 private SurfaceControl.Transaction mRtTransaction = new SurfaceControl.Transaction();
Justin Klaassen10d07c82017-09-15 17:58:39 -0400183
184 public SurfaceView(Context context) {
185 this(context, null);
186 }
187
188 public SurfaceView(Context context, AttributeSet attrs) {
Justin Klaassenb8042fc2018-04-15 00:41:15 -0400189 this(context, attrs, 0);
Justin Klaassen10d07c82017-09-15 17:58:39 -0400190 }
191
Justin Klaassenb8042fc2018-04-15 00:41:15 -0400192 public SurfaceView(Context context, AttributeSet attrs, int defStyleAttr) {
193 this(context, attrs, defStyleAttr, 0);
Justin Klaassen10d07c82017-09-15 17:58:39 -0400194 }
195
196 public SurfaceView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
197 super(context, attrs, defStyleAttr, defStyleRes);
Justin Klaassenb8042fc2018-04-15 00:41:15 -0400198 mRenderNode.requestPositionUpdates(this);
199
200 setWillNotDraw(true);
Justin Klaassen10d07c82017-09-15 17:58:39 -0400201 }
202
Justin Klaassenb8042fc2018-04-15 00:41:15 -0400203 /**
204 * Return the SurfaceHolder providing access and control over this
205 * SurfaceView's underlying surface.
206 *
207 * @return SurfaceHolder The holder of the surface.
208 */
Justin Klaassen10d07c82017-09-15 17:58:39 -0400209 public SurfaceHolder getHolder() {
210 return mSurfaceHolder;
211 }
212
Justin Klaassenb8042fc2018-04-15 00:41:15 -0400213 private void updateRequestedVisibility() {
214 mRequestedVisible = mViewVisibility && mWindowVisibility && !mWindowStopped;
215 }
216
217 /** @hide */
218 @Override
219 public void windowStopped(boolean stopped) {
220 mWindowStopped = stopped;
221 updateRequestedVisibility();
222 updateSurface();
223 }
224
225 @Override
226 protected void onAttachedToWindow() {
227 super.onAttachedToWindow();
228
229 getViewRootImpl().addWindowStoppedCallback(this);
230 mWindowStopped = false;
231
232 mViewVisibility = getVisibility() == VISIBLE;
233 updateRequestedVisibility();
234
235 mAttachedToWindow = true;
236 mParent.requestTransparentRegion(SurfaceView.this);
237 if (!mGlobalListenersAdded) {
238 ViewTreeObserver observer = getViewTreeObserver();
239 observer.addOnScrollChangedListener(mScrollChangedListener);
240 observer.addOnPreDrawListener(mDrawListener);
241 mGlobalListenersAdded = true;
242 }
243 }
244
245 @Override
246 protected void onWindowVisibilityChanged(int visibility) {
247 super.onWindowVisibilityChanged(visibility);
248 mWindowVisibility = visibility == VISIBLE;
249 updateRequestedVisibility();
250 updateSurface();
251 }
252
253 @Override
254 public void setVisibility(int visibility) {
255 super.setVisibility(visibility);
256 mViewVisibility = visibility == VISIBLE;
257 boolean newRequestedVisible = mWindowVisibility && mViewVisibility && !mWindowStopped;
258 if (newRequestedVisible != mRequestedVisible) {
259 // our base class (View) invalidates the layout only when
260 // we go from/to the GONE state. However, SurfaceView needs
261 // to request a re-layout when the visibility changes at all.
262 // This is needed because the transparent region is computed
263 // as part of the layout phase, and it changes (obviously) when
264 // the visibility changes.
265 requestLayout();
266 }
267 mRequestedVisible = newRequestedVisible;
268 updateSurface();
269 }
270
271 private void performDrawFinished() {
272 if (mPendingReportDraws > 0) {
273 mDrawFinished = true;
274 if (mAttachedToWindow) {
275 notifyDrawFinished();
276 invalidate();
277 }
278 } else {
279 Log.e(TAG, System.identityHashCode(this) + "finished drawing"
280 + " but no pending report draw (extra call"
281 + " to draw completion runnable?)");
282 }
283 }
284
285 void notifyDrawFinished() {
286 ViewRootImpl viewRoot = getViewRootImpl();
287 if (viewRoot != null) {
288 viewRoot.pendingDrawFinished();
289 }
290 mPendingReportDraws--;
291 }
292
293 @Override
294 protected void onDetachedFromWindow() {
295 ViewRootImpl viewRoot = getViewRootImpl();
296 // It's possible to create a SurfaceView using the default constructor and never
297 // attach it to a view hierarchy, this is a common use case when dealing with
298 // OpenGL. A developer will probably create a new GLSurfaceView, and let it manage
299 // the lifecycle. Instead of attaching it to a view, he/she can just pass
300 // the SurfaceHolder forward, most live wallpapers do it.
301 if (viewRoot != null) {
302 viewRoot.removeWindowStoppedCallback(this);
303 }
304
305 mAttachedToWindow = false;
306 if (mGlobalListenersAdded) {
307 ViewTreeObserver observer = getViewTreeObserver();
308 observer.removeOnScrollChangedListener(mScrollChangedListener);
309 observer.removeOnPreDrawListener(mDrawListener);
310 mGlobalListenersAdded = false;
311 }
312
313 while (mPendingReportDraws > 0) {
314 notifyDrawFinished();
315 }
316
317 mRequestedVisible = false;
318
319 updateSurface();
320 if (mSurfaceControl != null) {
321 mSurfaceControl.destroy();
322 }
323 mSurfaceControl = null;
324
325 mHaveFrame = false;
326
327 super.onDetachedFromWindow();
328 }
329
330 @Override
331 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
332 int width = mRequestedWidth >= 0
333 ? resolveSizeAndState(mRequestedWidth, widthMeasureSpec, 0)
334 : getDefaultSize(0, widthMeasureSpec);
335 int height = mRequestedHeight >= 0
336 ? resolveSizeAndState(mRequestedHeight, heightMeasureSpec, 0)
337 : getDefaultSize(0, heightMeasureSpec);
338 setMeasuredDimension(width, height);
339 }
340
341 /** @hide */
342 @Override
343 protected boolean setFrame(int left, int top, int right, int bottom) {
344 boolean result = super.setFrame(left, top, right, bottom);
345 updateSurface();
346 return result;
347 }
348
349 @Override
350 public boolean gatherTransparentRegion(Region region) {
351 if (isAboveParent() || !mDrawFinished) {
352 return super.gatherTransparentRegion(region);
353 }
354
355 boolean opaque = true;
356 if ((mPrivateFlags & PFLAG_SKIP_DRAW) == 0) {
357 // this view draws, remove it from the transparent region
358 opaque = super.gatherTransparentRegion(region);
359 } else if (region != null) {
360 int w = getWidth();
361 int h = getHeight();
362 if (w>0 && h>0) {
363 getLocationInWindow(mLocation);
364 // otherwise, punch a hole in the whole hierarchy
365 int l = mLocation[0];
366 int t = mLocation[1];
367 region.op(l, t, l+w, t+h, Region.Op.UNION);
368 }
369 }
370 if (PixelFormat.formatHasAlpha(mRequestedFormat)) {
371 opaque = false;
372 }
373 return opaque;
374 }
375
376 @Override
377 public void draw(Canvas canvas) {
378 if (mDrawFinished && !isAboveParent()) {
379 // draw() is not called when SKIP_DRAW is set
380 if ((mPrivateFlags & PFLAG_SKIP_DRAW) == 0) {
381 // punch a whole in the view-hierarchy below us
382 canvas.drawColor(0, PorterDuff.Mode.CLEAR);
383 }
384 }
385 super.draw(canvas);
386 }
387
388 @Override
389 protected void dispatchDraw(Canvas canvas) {
390 if (mDrawFinished && !isAboveParent()) {
391 // draw() is not called when SKIP_DRAW is set
392 if ((mPrivateFlags & PFLAG_SKIP_DRAW) == PFLAG_SKIP_DRAW) {
393 // punch a whole in the view-hierarchy below us
394 canvas.drawColor(0, PorterDuff.Mode.CLEAR);
395 }
396 }
397 super.dispatchDraw(canvas);
398 }
399
400 /**
401 * Control whether the surface view's surface is placed on top of another
402 * regular surface view in the window (but still behind the window itself).
403 * This is typically used to place overlays on top of an underlying media
404 * surface view.
405 *
406 * <p>Note that this must be set before the surface view's containing
407 * window is attached to the window manager.
408 *
409 * <p>Calling this overrides any previous call to {@link #setZOrderOnTop}.
410 */
411 public void setZOrderMediaOverlay(boolean isMediaOverlay) {
412 mSubLayer = isMediaOverlay
413 ? APPLICATION_MEDIA_OVERLAY_SUBLAYER : APPLICATION_MEDIA_SUBLAYER;
414 }
415
416 /**
417 * Control whether the surface view's surface is placed on top of its
418 * window. Normally it is placed behind the window, to allow it to
419 * (for the most part) appear to composite with the views in the
420 * hierarchy. By setting this, you cause it to be placed above the
421 * window. This means that none of the contents of the window this
422 * SurfaceView is in will be visible on top of its surface.
423 *
424 * <p>Note that this must be set before the surface view's containing
425 * window is attached to the window manager.
426 *
427 * <p>Calling this overrides any previous call to {@link #setZOrderMediaOverlay}.
428 */
429 public void setZOrderOnTop(boolean onTop) {
430 if (onTop) {
431 mSubLayer = APPLICATION_PANEL_SUBLAYER;
432 } else {
433 mSubLayer = APPLICATION_MEDIA_SUBLAYER;
434 }
435 }
436
437 /**
438 * Control whether the surface view's content should be treated as secure,
439 * preventing it from appearing in screenshots or from being viewed on
440 * non-secure displays.
441 *
442 * <p>Note that this must be set before the surface view's containing
443 * window is attached to the window manager.
444 *
445 * <p>See {@link android.view.Display#FLAG_SECURE} for details.
446 *
447 * @param isSecure True if the surface view is secure.
448 */
449 public void setSecure(boolean isSecure) {
450 if (isSecure) {
451 mSurfaceFlags |= SurfaceControl.SECURE;
452 } else {
453 mSurfaceFlags &= ~SurfaceControl.SECURE;
454 }
455 }
456
457 private void updateOpaqueFlag() {
458 if (!PixelFormat.formatHasAlpha(mRequestedFormat)) {
459 mSurfaceFlags |= SurfaceControl.OPAQUE;
460 } else {
461 mSurfaceFlags &= ~SurfaceControl.OPAQUE;
462 }
463 }
464
465 private Rect getParentSurfaceInsets() {
466 final ViewRootImpl root = getViewRootImpl();
467 if (root == null) {
468 return null;
469 } else {
470 return root.mWindowAttributes.surfaceInsets;
471 }
472 }
473
474 /** @hide */
475 protected void updateSurface() {
476 if (!mHaveFrame) {
477 return;
478 }
479 ViewRootImpl viewRoot = getViewRootImpl();
480 if (viewRoot == null || viewRoot.mSurface == null || !viewRoot.mSurface.isValid()) {
481 return;
482 }
483
484 mTranslator = viewRoot.mTranslator;
485 if (mTranslator != null) {
486 mSurface.setCompatibilityTranslator(mTranslator);
487 }
488
489 int myWidth = mRequestedWidth;
490 if (myWidth <= 0) myWidth = getWidth();
491 int myHeight = mRequestedHeight;
492 if (myHeight <= 0) myHeight = getHeight();
493
494 final boolean formatChanged = mFormat != mRequestedFormat;
495 final boolean visibleChanged = mVisible != mRequestedVisible;
496 final boolean creating = (mSurfaceControl == null || formatChanged || visibleChanged)
497 && mRequestedVisible;
498 final boolean sizeChanged = mSurfaceWidth != myWidth || mSurfaceHeight != myHeight;
499 final boolean windowVisibleChanged = mWindowVisibility != mLastWindowVisibility;
500 boolean redrawNeeded = false;
501
502 if (creating || formatChanged || sizeChanged || visibleChanged || windowVisibleChanged) {
503 getLocationInWindow(mLocation);
504
505 if (DEBUG) Log.i(TAG, System.identityHashCode(this) + " "
506 + "Changes: creating=" + creating
507 + " format=" + formatChanged + " size=" + sizeChanged
508 + " visible=" + visibleChanged
509 + " left=" + (mWindowSpaceLeft != mLocation[0])
510 + " top=" + (mWindowSpaceTop != mLocation[1]));
511
512 try {
513 final boolean visible = mVisible = mRequestedVisible;
514 mWindowSpaceLeft = mLocation[0];
515 mWindowSpaceTop = mLocation[1];
516 mSurfaceWidth = myWidth;
517 mSurfaceHeight = myHeight;
518 mFormat = mRequestedFormat;
519 mLastWindowVisibility = mWindowVisibility;
520
521 mScreenRect.left = mWindowSpaceLeft;
522 mScreenRect.top = mWindowSpaceTop;
523 mScreenRect.right = mWindowSpaceLeft + getWidth();
524 mScreenRect.bottom = mWindowSpaceTop + getHeight();
525 if (mTranslator != null) {
526 mTranslator.translateRectInAppWindowToScreen(mScreenRect);
527 }
528
529 final Rect surfaceInsets = getParentSurfaceInsets();
530 mScreenRect.offset(surfaceInsets.left, surfaceInsets.top);
531
532 if (creating) {
533 mSurfaceSession = new SurfaceSession(viewRoot.mSurface);
534 mDeferredDestroySurfaceControl = mSurfaceControl;
535
536 updateOpaqueFlag();
537 final String name = "SurfaceView - " + viewRoot.getTitle().toString();
538
539 mSurfaceControl = new SurfaceControlWithBackground(
540 name,
541 (mSurfaceFlags & SurfaceControl.OPAQUE) != 0,
542 new SurfaceControl.Builder(mSurfaceSession)
543 .setSize(mSurfaceWidth, mSurfaceHeight)
544 .setFormat(mFormat)
545 .setFlags(mSurfaceFlags));
546 } else if (mSurfaceControl == null) {
547 return;
548 }
549
550 boolean realSizeChanged = false;
551
552 mSurfaceLock.lock();
553 try {
554 mDrawingStopped = !visible;
555
556 if (DEBUG) Log.i(TAG, System.identityHashCode(this) + " "
557 + "Cur surface: " + mSurface);
558
559 SurfaceControl.openTransaction();
560 try {
561 mSurfaceControl.setLayer(mSubLayer);
562 if (mViewVisibility) {
563 mSurfaceControl.show();
564 } else {
565 mSurfaceControl.hide();
566 }
567
568 // While creating the surface, we will set it's initial
569 // geometry. Outside of that though, we should generally
570 // leave it to the RenderThread.
571 //
572 // There is one more case when the buffer size changes we aren't yet
573 // prepared to sync (as even following the transaction applying
574 // we still need to latch a buffer).
575 // b/28866173
576 if (sizeChanged || creating || !mRtHandlingPositionUpdates) {
577 mSurfaceControl.setPosition(mScreenRect.left, mScreenRect.top);
578 mSurfaceControl.setMatrix(mScreenRect.width() / (float) mSurfaceWidth,
579 0.0f, 0.0f,
580 mScreenRect.height() / (float) mSurfaceHeight);
581 }
582 if (sizeChanged) {
583 mSurfaceControl.setSize(mSurfaceWidth, mSurfaceHeight);
584 }
585 } finally {
586 SurfaceControl.closeTransaction();
587 }
588
589 if (sizeChanged || creating) {
590 redrawNeeded = true;
591 }
592
593 mSurfaceFrame.left = 0;
594 mSurfaceFrame.top = 0;
595 if (mTranslator == null) {
596 mSurfaceFrame.right = mSurfaceWidth;
597 mSurfaceFrame.bottom = mSurfaceHeight;
598 } else {
599 float appInvertedScale = mTranslator.applicationInvertedScale;
600 mSurfaceFrame.right = (int) (mSurfaceWidth * appInvertedScale + 0.5f);
601 mSurfaceFrame.bottom = (int) (mSurfaceHeight * appInvertedScale + 0.5f);
602 }
603
604 final int surfaceWidth = mSurfaceFrame.right;
605 final int surfaceHeight = mSurfaceFrame.bottom;
606 realSizeChanged = mLastSurfaceWidth != surfaceWidth
607 || mLastSurfaceHeight != surfaceHeight;
608 mLastSurfaceWidth = surfaceWidth;
609 mLastSurfaceHeight = surfaceHeight;
610 } finally {
611 mSurfaceLock.unlock();
612 }
613
614 try {
615 redrawNeeded |= visible && !mDrawFinished;
616
617 SurfaceHolder.Callback callbacks[] = null;
618
619 final boolean surfaceChanged = creating;
620 if (mSurfaceCreated && (surfaceChanged || (!visible && visibleChanged))) {
621 mSurfaceCreated = false;
622 if (mSurface.isValid()) {
623 if (DEBUG) Log.i(TAG, System.identityHashCode(this) + " "
624 + "visibleChanged -- surfaceDestroyed");
625 callbacks = getSurfaceCallbacks();
626 for (SurfaceHolder.Callback c : callbacks) {
627 c.surfaceDestroyed(mSurfaceHolder);
628 }
629 // Since Android N the same surface may be reused and given to us
630 // again by the system server at a later point. However
631 // as we didn't do this in previous releases, clients weren't
632 // necessarily required to clean up properly in
633 // surfaceDestroyed. This leads to problems for example when
634 // clients don't destroy their EGL context, and try
635 // and create a new one on the same surface following reuse.
636 // Since there is no valid use of the surface in-between
637 // surfaceDestroyed and surfaceCreated, we force a disconnect,
638 // so the next connect will always work if we end up reusing
639 // the surface.
640 if (mSurface.isValid()) {
641 mSurface.forceScopedDisconnect();
642 }
643 }
644 }
645
646 if (creating) {
647 mSurface.copyFrom(mSurfaceControl);
648 }
649
650 if (sizeChanged && getContext().getApplicationInfo().targetSdkVersion
651 < Build.VERSION_CODES.O) {
652 // Some legacy applications use the underlying native {@link Surface} object
653 // as a key to whether anything has changed. In these cases, updates to the
654 // existing {@link Surface} will be ignored when the size changes.
655 // Therefore, we must explicitly recreate the {@link Surface} in these
656 // cases.
657 mSurface.createFrom(mSurfaceControl);
658 }
659
660 if (visible && mSurface.isValid()) {
661 if (!mSurfaceCreated && (surfaceChanged || visibleChanged)) {
662 mSurfaceCreated = true;
663 mIsCreating = true;
664 if (DEBUG) Log.i(TAG, System.identityHashCode(this) + " "
665 + "visibleChanged -- surfaceCreated");
666 if (callbacks == null) {
667 callbacks = getSurfaceCallbacks();
668 }
669 for (SurfaceHolder.Callback c : callbacks) {
670 c.surfaceCreated(mSurfaceHolder);
671 }
672 }
673 if (creating || formatChanged || sizeChanged
674 || visibleChanged || realSizeChanged) {
675 if (DEBUG) Log.i(TAG, System.identityHashCode(this) + " "
676 + "surfaceChanged -- format=" + mFormat
677 + " w=" + myWidth + " h=" + myHeight);
678 if (callbacks == null) {
679 callbacks = getSurfaceCallbacks();
680 }
681 for (SurfaceHolder.Callback c : callbacks) {
682 c.surfaceChanged(mSurfaceHolder, mFormat, myWidth, myHeight);
683 }
684 }
685 if (redrawNeeded) {
686 if (DEBUG) Log.i(TAG, System.identityHashCode(this) + " "
687 + "surfaceRedrawNeeded");
688 if (callbacks == null) {
689 callbacks = getSurfaceCallbacks();
690 }
691
692 mPendingReportDraws++;
693 viewRoot.drawPending();
694 SurfaceCallbackHelper sch =
695 new SurfaceCallbackHelper(this::onDrawFinished);
696 sch.dispatchSurfaceRedrawNeededAsync(mSurfaceHolder, callbacks);
697 }
698 }
699 } finally {
700 mIsCreating = false;
701 if (mSurfaceControl != null && !mSurfaceCreated) {
702 mSurface.release();
703 // If we are not in the stopped state, then the destruction of the Surface
704 // represents a visual change we need to display, and we should go ahead
705 // and destroy the SurfaceControl. However if we are in the stopped state,
706 // we can just leave the Surface around so it can be a part of animations,
707 // and we let the life-time be tied to the parent surface.
708 if (!mWindowStopped) {
709 mSurfaceControl.destroy();
710 mSurfaceControl = null;
711 }
712 }
713 }
714 } catch (Exception ex) {
715 Log.e(TAG, "Exception configuring surface", ex);
716 }
717 if (DEBUG) Log.v(
718 TAG, "Layout: x=" + mScreenRect.left + " y=" + mScreenRect.top
719 + " w=" + mScreenRect.width() + " h=" + mScreenRect.height()
720 + ", frame=" + mSurfaceFrame);
721 } else {
722 // Calculate the window position in case RT loses the window
723 // and we need to fallback to a UI-thread driven position update
724 getLocationInSurface(mLocation);
725 final boolean positionChanged = mWindowSpaceLeft != mLocation[0]
726 || mWindowSpaceTop != mLocation[1];
727 final boolean layoutSizeChanged = getWidth() != mScreenRect.width()
728 || getHeight() != mScreenRect.height();
729 if (positionChanged || layoutSizeChanged) { // Only the position has changed
730 mWindowSpaceLeft = mLocation[0];
731 mWindowSpaceTop = mLocation[1];
732 // For our size changed check, we keep mScreenRect.width() and mScreenRect.height()
733 // in view local space.
734 mLocation[0] = getWidth();
735 mLocation[1] = getHeight();
736
737 mScreenRect.set(mWindowSpaceLeft, mWindowSpaceTop,
738 mWindowSpaceLeft + mLocation[0], mWindowSpaceTop + mLocation[1]);
739
740 if (mTranslator != null) {
741 mTranslator.translateRectInAppWindowToScreen(mScreenRect);
742 }
743
744 if (mSurfaceControl == null) {
745 return;
746 }
747
748 if (!isHardwareAccelerated() || !mRtHandlingPositionUpdates) {
749 try {
750 if (DEBUG) Log.d(TAG, String.format("%d updateSurfacePosition UI, " +
751 "postion = [%d, %d, %d, %d]", System.identityHashCode(this),
752 mScreenRect.left, mScreenRect.top,
753 mScreenRect.right, mScreenRect.bottom));
754 setParentSpaceRectangle(mScreenRect, -1);
755 } catch (Exception ex) {
756 Log.e(TAG, "Exception configuring surface", ex);
757 }
758 }
759 }
760 }
761 }
762
763 private void onDrawFinished() {
764 if (DEBUG) {
765 Log.i(TAG, System.identityHashCode(this) + " "
766 + "finishedDrawing");
767 }
768
769 if (mDeferredDestroySurfaceControl != null) {
770 mDeferredDestroySurfaceControl.destroy();
771 mDeferredDestroySurfaceControl = null;
772 }
773
774 runOnUiThread(() -> {
775 performDrawFinished();
776 });
777 }
778
779 /**
780 * A place to over-ride for applying child-surface transactions.
781 * These can be synchronized with the viewroot surface using deferTransaction.
782 *
783 * Called from RenderWorker while UI thread is paused.
784 * @hide
785 */
786 protected void applyChildSurfaceTransaction_renderWorker(SurfaceControl.Transaction t,
787 Surface viewRootSurface, long nextViewRootFrameNumber) {
788 }
789
790 private void applySurfaceTransforms(SurfaceControl surface, Rect position, long frameNumber) {
791 if (frameNumber > 0) {
792 final ViewRootImpl viewRoot = getViewRootImpl();
793
794 mRtTransaction.deferTransactionUntilSurface(surface, viewRoot.mSurface,
795 frameNumber);
796 }
797
798 mRtTransaction.setPosition(surface, position.left, position.top);
799 mRtTransaction.setMatrix(surface,
800 position.width() / (float) mSurfaceWidth,
801 0.0f, 0.0f,
802 position.height() / (float) mSurfaceHeight);
803 }
804
805 private void setParentSpaceRectangle(Rect position, long frameNumber) {
806 final ViewRootImpl viewRoot = getViewRootImpl();
807
808 applySurfaceTransforms(mSurfaceControl, position, frameNumber);
809 applySurfaceTransforms(mSurfaceControl.mBackgroundControl, position, frameNumber);
810
811 applyChildSurfaceTransaction_renderWorker(mRtTransaction, viewRoot.mSurface,
812 frameNumber);
813
814 mRtTransaction.apply();
815 }
816
817 private Rect mRTLastReportedPosition = new Rect();
818
819 /**
820 * Called by native by a Rendering Worker thread to update the window position
821 * @hide
822 */
823 public final void updateSurfacePosition_renderWorker(long frameNumber,
824 int left, int top, int right, int bottom) {
825 if (mSurfaceControl == null) {
826 return;
827 }
828
829 // TODO: This is teensy bit racey in that a brand new SurfaceView moving on
830 // its 2nd frame if RenderThread is running slowly could potentially see
831 // this as false, enter the branch, get pre-empted, then this comes along
832 // and reports a new position, then the UI thread resumes and reports
833 // its position. This could therefore be de-sync'd in that interval, but
834 // the synchronization would violate the rule that RT must never block
835 // on the UI thread which would open up potential deadlocks. The risk of
836 // a single-frame desync is therefore preferable for now.
837 mRtHandlingPositionUpdates = true;
838 if (mRTLastReportedPosition.left == left
839 && mRTLastReportedPosition.top == top
840 && mRTLastReportedPosition.right == right
841 && mRTLastReportedPosition.bottom == bottom) {
842 return;
843 }
844 try {
845 if (DEBUG) {
846 Log.d(TAG, String.format("%d updateSurfacePosition RenderWorker, frameNr = %d, " +
847 "postion = [%d, %d, %d, %d]", System.identityHashCode(this),
848 frameNumber, left, top, right, bottom));
849 }
850 mRTLastReportedPosition.set(left, top, right, bottom);
851 setParentSpaceRectangle(mRTLastReportedPosition, frameNumber);
852 // Now overwrite mRTLastReportedPosition with our values
853 } catch (Exception ex) {
854 Log.e(TAG, "Exception from repositionChild", ex);
855 }
856 }
857
858 /**
859 * Called by native on RenderThread to notify that the view is no longer in the
860 * draw tree. UI thread is blocked at this point.
861 * @hide
862 */
863 public final void surfacePositionLost_uiRtSync(long frameNumber) {
864 if (DEBUG) {
865 Log.d(TAG, String.format("%d windowPositionLost, frameNr = %d",
866 System.identityHashCode(this), frameNumber));
867 }
868 mRTLastReportedPosition.setEmpty();
869
870 if (mSurfaceControl == null) {
871 return;
872 }
873 if (mRtHandlingPositionUpdates) {
874 mRtHandlingPositionUpdates = false;
875 // This callback will happen while the UI thread is blocked, so we can
876 // safely access other member variables at this time.
877 // So do what the UI thread would have done if RT wasn't handling position
878 // updates.
879 if (!mScreenRect.isEmpty() && !mScreenRect.equals(mRTLastReportedPosition)) {
880 try {
881 if (DEBUG) Log.d(TAG, String.format("%d updateSurfacePosition, " +
882 "postion = [%d, %d, %d, %d]", System.identityHashCode(this),
883 mScreenRect.left, mScreenRect.top,
884 mScreenRect.right, mScreenRect.bottom));
885 setParentSpaceRectangle(mScreenRect, frameNumber);
886 } catch (Exception ex) {
887 Log.e(TAG, "Exception configuring surface", ex);
888 }
889 }
890 }
891 }
892
893 private SurfaceHolder.Callback[] getSurfaceCallbacks() {
894 SurfaceHolder.Callback callbacks[];
895 synchronized (mCallbacks) {
896 callbacks = new SurfaceHolder.Callback[mCallbacks.size()];
897 mCallbacks.toArray(callbacks);
898 }
899 return callbacks;
900 }
901
902 private void runOnUiThread(Runnable runnable) {
903 Handler handler = getHandler();
904 if (handler != null && handler.getLooper() != Looper.myLooper()) {
905 handler.post(runnable);
906 } else {
907 runnable.run();
908 }
909 }
910
911 /**
912 * Check to see if the surface has fixed size dimensions or if the surface's
913 * dimensions are dimensions are dependent on its current layout.
914 *
915 * @return true if the surface has dimensions that are fixed in size
916 * @hide
917 */
918 public boolean isFixedSize() {
919 return (mRequestedWidth != -1 || mRequestedHeight != -1);
920 }
921
922 private boolean isAboveParent() {
923 return mSubLayer >= 0;
924 }
925
926 /**
927 * Set an opaque background color to use with this {@link SurfaceView} when it's being resized
928 * and size of the content hasn't updated yet. This color will fill the expanded area when the
929 * view becomes larger.
930 * @param bgColor An opaque color to fill the background. Alpha component will be ignored.
931 * @hide
932 */
933 public void setResizeBackgroundColor(int bgColor) {
934 mSurfaceControl.setBackgroundColor(bgColor);
935 }
936
937 private final SurfaceHolder mSurfaceHolder = new SurfaceHolder() {
938 private static final String LOG_TAG = "SurfaceHolder";
Justin Klaassen10d07c82017-09-15 17:58:39 -0400939
940 @Override
941 public boolean isCreating() {
Justin Klaassenb8042fc2018-04-15 00:41:15 -0400942 return mIsCreating;
Justin Klaassen10d07c82017-09-15 17:58:39 -0400943 }
944
945 @Override
946 public void addCallback(Callback callback) {
Justin Klaassenb8042fc2018-04-15 00:41:15 -0400947 synchronized (mCallbacks) {
948 // This is a linear search, but in practice we'll
949 // have only a couple callbacks, so it doesn't matter.
950 if (mCallbacks.contains(callback) == false) {
951 mCallbacks.add(callback);
952 }
953 }
Justin Klaassen10d07c82017-09-15 17:58:39 -0400954 }
955
956 @Override
957 public void removeCallback(Callback callback) {
Justin Klaassenb8042fc2018-04-15 00:41:15 -0400958 synchronized (mCallbacks) {
959 mCallbacks.remove(callback);
960 }
Justin Klaassen10d07c82017-09-15 17:58:39 -0400961 }
962
963 @Override
964 public void setFixedSize(int width, int height) {
Justin Klaassenb8042fc2018-04-15 00:41:15 -0400965 if (mRequestedWidth != width || mRequestedHeight != height) {
966 mRequestedWidth = width;
967 mRequestedHeight = height;
968 requestLayout();
969 }
Justin Klaassen10d07c82017-09-15 17:58:39 -0400970 }
971
972 @Override
973 public void setSizeFromLayout() {
Justin Klaassenb8042fc2018-04-15 00:41:15 -0400974 if (mRequestedWidth != -1 || mRequestedHeight != -1) {
975 mRequestedWidth = mRequestedHeight = -1;
976 requestLayout();
977 }
Justin Klaassen10d07c82017-09-15 17:58:39 -0400978 }
979
980 @Override
981 public void setFormat(int format) {
Justin Klaassenb8042fc2018-04-15 00:41:15 -0400982 // for backward compatibility reason, OPAQUE always
983 // means 565 for SurfaceView
984 if (format == PixelFormat.OPAQUE)
985 format = PixelFormat.RGB_565;
986
987 mRequestedFormat = format;
988 if (mSurfaceControl != null) {
989 updateSurface();
990 }
Justin Klaassen10d07c82017-09-15 17:58:39 -0400991 }
992
Justin Klaassenb8042fc2018-04-15 00:41:15 -0400993 /**
994 * @deprecated setType is now ignored.
995 */
Justin Klaassen10d07c82017-09-15 17:58:39 -0400996 @Override
Justin Klaassenb8042fc2018-04-15 00:41:15 -0400997 @Deprecated
998 public void setType(int type) { }
Justin Klaassen10d07c82017-09-15 17:58:39 -0400999
1000 @Override
1001 public void setKeepScreenOn(boolean screenOn) {
Justin Klaassenb8042fc2018-04-15 00:41:15 -04001002 runOnUiThread(() -> SurfaceView.this.setKeepScreenOn(screenOn));
Justin Klaassen10d07c82017-09-15 17:58:39 -04001003 }
1004
Justin Klaassenb8042fc2018-04-15 00:41:15 -04001005 /**
1006 * Gets a {@link Canvas} for drawing into the SurfaceView's Surface
1007 *
1008 * After drawing into the provided {@link Canvas}, the caller must
1009 * invoke {@link #unlockCanvasAndPost} to post the new contents to the surface.
1010 *
1011 * The caller must redraw the entire surface.
1012 * @return A canvas for drawing into the surface.
1013 */
Justin Klaassen10d07c82017-09-15 17:58:39 -04001014 @Override
1015 public Canvas lockCanvas() {
Justin Klaassenb8042fc2018-04-15 00:41:15 -04001016 return internalLockCanvas(null, false);
1017 }
1018
1019 /**
1020 * Gets a {@link Canvas} for drawing into the SurfaceView's Surface
1021 *
1022 * After drawing into the provided {@link Canvas}, the caller must
1023 * invoke {@link #unlockCanvasAndPost} to post the new contents to the surface.
1024 *
1025 * @param inOutDirty A rectangle that represents the dirty region that the caller wants
1026 * to redraw. This function may choose to expand the dirty rectangle if for example
1027 * the surface has been resized or if the previous contents of the surface were
1028 * not available. The caller must redraw the entire dirty region as represented
1029 * by the contents of the inOutDirty rectangle upon return from this function.
1030 * The caller may also pass <code>null</code> instead, in the case where the
1031 * entire surface should be redrawn.
1032 * @return A canvas for drawing into the surface.
1033 */
1034 @Override
1035 public Canvas lockCanvas(Rect inOutDirty) {
1036 return internalLockCanvas(inOutDirty, false);
Justin Klaassen47ed54e2017-10-24 19:50:40 -04001037 }
1038
Justin Klaassen98fe7812018-01-03 13:39:41 -05001039 @Override
Justin Klaassenb8042fc2018-04-15 00:41:15 -04001040 public Canvas lockHardwareCanvas() {
1041 return internalLockCanvas(null, true);
1042 }
1043
1044 private Canvas internalLockCanvas(Rect dirty, boolean hardware) {
1045 mSurfaceLock.lock();
1046
1047 if (DEBUG) Log.i(TAG, System.identityHashCode(this) + " " + "Locking canvas... stopped="
1048 + mDrawingStopped + ", surfaceControl=" + mSurfaceControl);
1049
1050 Canvas c = null;
1051 if (!mDrawingStopped && mSurfaceControl != null) {
1052 try {
1053 if (hardware) {
1054 c = mSurface.lockHardwareCanvas();
1055 } else {
1056 c = mSurface.lockCanvas(dirty);
1057 }
1058 } catch (Exception e) {
1059 Log.e(LOG_TAG, "Exception locking surface", e);
1060 }
1061 }
1062
1063 if (DEBUG) Log.i(TAG, System.identityHashCode(this) + " " + "Returned canvas: " + c);
1064 if (c != null) {
1065 mLastLockTime = SystemClock.uptimeMillis();
1066 return c;
1067 }
1068
1069 // If the Surface is not ready to be drawn, then return null,
1070 // but throttle calls to this function so it isn't called more
1071 // than every 100ms.
1072 long now = SystemClock.uptimeMillis();
1073 long nextTime = mLastLockTime + 100;
1074 if (nextTime > now) {
1075 try {
1076 Thread.sleep(nextTime-now);
1077 } catch (InterruptedException e) {
1078 }
1079 now = SystemClock.uptimeMillis();
1080 }
1081 mLastLockTime = now;
1082 mSurfaceLock.unlock();
1083
Justin Klaassen98fe7812018-01-03 13:39:41 -05001084 return null;
1085 }
1086
Justin Klaassenb8042fc2018-04-15 00:41:15 -04001087 /**
1088 * Posts the new contents of the {@link Canvas} to the surface and
1089 * releases the {@link Canvas}.
1090 *
1091 * @param canvas The canvas previously obtained from {@link #lockCanvas}.
1092 */
Justin Klaassen10d07c82017-09-15 17:58:39 -04001093 @Override
1094 public void unlockCanvasAndPost(Canvas canvas) {
Justin Klaassenb8042fc2018-04-15 00:41:15 -04001095 mSurface.unlockCanvasAndPost(canvas);
1096 mSurfaceLock.unlock();
Justin Klaassen10d07c82017-09-15 17:58:39 -04001097 }
1098
1099 @Override
1100 public Surface getSurface() {
Justin Klaassenb8042fc2018-04-15 00:41:15 -04001101 return mSurface;
Justin Klaassen10d07c82017-09-15 17:58:39 -04001102 }
1103
1104 @Override
1105 public Rect getSurfaceFrame() {
Justin Klaassenb8042fc2018-04-15 00:41:15 -04001106 return mSurfaceFrame;
Justin Klaassen10d07c82017-09-15 17:58:39 -04001107 }
1108 };
Justin Klaassen98fe7812018-01-03 13:39:41 -05001109
Justin Klaassenb8042fc2018-04-15 00:41:15 -04001110 class SurfaceControlWithBackground extends SurfaceControl {
1111 SurfaceControl mBackgroundControl;
1112 private boolean mOpaque = true;
1113 public boolean mVisible = false;
1114
1115 public SurfaceControlWithBackground(String name, boolean opaque, SurfaceControl.Builder b)
1116 throws Exception {
1117 super(b.setName(name).build());
1118
1119 mBackgroundControl = b.setName("Background for -" + name)
1120 .setFormat(OPAQUE)
1121 .setColorLayer(true)
1122 .build();
1123 mOpaque = opaque;
1124 }
1125
1126 @Override
1127 public void setAlpha(float alpha) {
1128 super.setAlpha(alpha);
1129 mBackgroundControl.setAlpha(alpha);
1130 }
1131
1132 @Override
1133 public void setLayer(int zorder) {
1134 super.setLayer(zorder);
1135 // -3 is below all other child layers as SurfaceView never goes below -2
1136 mBackgroundControl.setLayer(-3);
1137 }
1138
1139 @Override
1140 public void setPosition(float x, float y) {
1141 super.setPosition(x, y);
1142 mBackgroundControl.setPosition(x, y);
1143 }
1144
1145 @Override
1146 public void setSize(int w, int h) {
1147 super.setSize(w, h);
1148 mBackgroundControl.setSize(w, h);
1149 }
1150
1151 @Override
1152 public void setWindowCrop(Rect crop) {
1153 super.setWindowCrop(crop);
1154 mBackgroundControl.setWindowCrop(crop);
1155 }
1156
1157 @Override
1158 public void setFinalCrop(Rect crop) {
1159 super.setFinalCrop(crop);
1160 mBackgroundControl.setFinalCrop(crop);
1161 }
1162
1163 @Override
1164 public void setLayerStack(int layerStack) {
1165 super.setLayerStack(layerStack);
1166 mBackgroundControl.setLayerStack(layerStack);
1167 }
1168
1169 @Override
1170 public void setOpaque(boolean isOpaque) {
1171 super.setOpaque(isOpaque);
1172 mOpaque = isOpaque;
1173 updateBackgroundVisibility();
1174 }
1175
1176 @Override
1177 public void setSecure(boolean isSecure) {
1178 super.setSecure(isSecure);
1179 }
1180
1181 @Override
1182 public void setMatrix(float dsdx, float dtdx, float dsdy, float dtdy) {
1183 super.setMatrix(dsdx, dtdx, dsdy, dtdy);
1184 mBackgroundControl.setMatrix(dsdx, dtdx, dsdy, dtdy);
1185 }
1186
1187 @Override
1188 public void hide() {
1189 super.hide();
1190 mVisible = false;
1191 updateBackgroundVisibility();
1192 }
1193
1194 @Override
1195 public void show() {
1196 super.show();
1197 mVisible = true;
1198 updateBackgroundVisibility();
1199 }
1200
1201 @Override
1202 public void destroy() {
1203 super.destroy();
1204 mBackgroundControl.destroy();
1205 }
1206
1207 @Override
1208 public void release() {
1209 super.release();
1210 mBackgroundControl.release();
1211 }
1212
1213 @Override
1214 public void setTransparentRegionHint(Region region) {
1215 super.setTransparentRegionHint(region);
1216 mBackgroundControl.setTransparentRegionHint(region);
1217 }
1218
1219 @Override
1220 public void deferTransactionUntil(IBinder handle, long frame) {
1221 super.deferTransactionUntil(handle, frame);
1222 mBackgroundControl.deferTransactionUntil(handle, frame);
1223 }
1224
1225 @Override
1226 public void deferTransactionUntil(Surface barrier, long frame) {
1227 super.deferTransactionUntil(barrier, frame);
1228 mBackgroundControl.deferTransactionUntil(barrier, frame);
1229 }
1230
1231 /** Set the color to fill the background with. */
1232 private void setBackgroundColor(int bgColor) {
1233 final float[] colorComponents = new float[] { Color.red(bgColor) / 255.f,
1234 Color.green(bgColor) / 255.f, Color.blue(bgColor) / 255.f };
1235
1236 SurfaceControl.openTransaction();
1237 try {
1238 mBackgroundControl.setColor(colorComponents);
1239 } finally {
1240 SurfaceControl.closeTransaction();
1241 }
1242 }
1243
1244 void updateBackgroundVisibility() {
1245 if (mOpaque && mVisible) {
1246 mBackgroundControl.show();
1247 } else {
1248 mBackgroundControl.hide();
1249 }
1250 }
1251 }
1252}