blob: 7e5464761e38c03eaf2ba37fa28b915578241d20 [file] [log] [blame]
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07001/*
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
Adrian Roose99bc052017-11-20 17:55:31 +010019import static android.view.WindowManagerPolicyConstants.APPLICATION_MEDIA_OVERLAY_SUBLAYER;
20import static android.view.WindowManagerPolicyConstants.APPLICATION_MEDIA_SUBLAYER;
21import static android.view.WindowManagerPolicyConstants.APPLICATION_PANEL_SUBLAYER;
Robert Carrd5c7dd62017-03-08 10:39:30 -080022
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070023import android.content.Context;
Mitsuru Oshima64f59342009-06-21 00:03:11 -070024import android.content.res.CompatibilityInfo.Translator;
Aurimas Liutikas67e2ae82016-10-11 18:17:42 -070025import android.content.res.Configuration;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070026import android.graphics.Canvas;
Andrii Kuliancf8f6832018-01-23 19:43:30 -080027import android.graphics.Color;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070028import android.graphics.PixelFormat;
29import android.graphics.PorterDuff;
30import android.graphics.Rect;
31import android.graphics.Region;
Yohei Yukawa3b5011a2017-03-16 15:34:12 -070032import android.os.Build;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070033import android.os.Handler;
Robert Carr55235552017-06-02 14:21:03 -070034import android.os.IBinder;
John Reck79925002017-05-26 13:57:14 -070035import android.os.Looper;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070036import android.os.SystemClock;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070037import android.util.AttributeSet;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070038import android.util.Log;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070039
Robert Carr25cfa132016-11-16 13:24:09 -080040import com.android.internal.view.SurfaceCallbackHelper;
Aurimas Liutikas67e2ae82016-10-11 18:17:42 -070041
Jon Larimer9bdf5762009-01-02 18:55:15 -050042import java.util.ArrayList;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070043import java.util.concurrent.locks.ReentrantLock;
44
45/**
46 * 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
Igor Murashkina86ab6402013-08-30 12:58:36 -070050 *
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070051 * <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
Jesse Hallc9f345f2012-09-26 11:55:16 -070053 * surface to be displayed. The view hierarchy will take care of correctly
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070054 * compositing with the Surface any siblings of the SurfaceView that would
Jesse Hallc9f345f2012-09-26 11:55:16 -070055 * normally appear on top of it. This can be used to place overlays such as
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070056 * 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.
Igor Murashkina86ab6402013-08-30 12:58:36 -070059 *
Jesse Hallc9f345f2012-09-26 11:55:16 -070060 * <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 *
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070065 * <p>Access to the underlying surface is provided via the SurfaceHolder interface,
66 * which can be retrieved by calling {@link #getHolder}.
Igor Murashkina86ab6402013-08-30 12:58:36 -070067 *
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070068 * <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.
Igor Murashkina86ab6402013-08-30 12:58:36 -070072 *
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070073 * <p>One of the purposes of this class is to provide a surface in which a
Jesse Hallc9f345f2012-09-26 11:55:16 -070074 * secondary thread can render into the screen. If you are going to use it
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070075 * this way, you need to be aware of some threading semantics:
Igor Murashkina86ab6402013-08-30 12:58:36 -070076 *
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070077 * <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
Jesse Hallc9f345f2012-09-26 11:55:16 -070081 * of the application). They thus need to correctly synchronize with any
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070082 * 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>
Chris Craik6ee192f2016-05-17 14:29:10 -070089 *
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>
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070096 */
Robert Carr414ebc62017-04-12 12:01:00 -070097public class SurfaceView extends View implements ViewRootImpl.WindowStoppedCallback {
Robert Carrd5c7dd62017-03-08 10:39:30 -080098 private static final String TAG = "SurfaceView";
99 private static final boolean DEBUG = false;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700100
101 final ArrayList<SurfaceHolder.Callback> mCallbacks
102 = new ArrayList<SurfaceHolder.Callback>();
103
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800104 final int[] mLocation = new int[2];
Igor Murashkina86ab6402013-08-30 12:58:36 -0700105
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700106 final ReentrantLock mSurfaceLock = new ReentrantLock();
Dianne Hackborn61566cc2011-12-02 23:31:52 -0800107 final Surface mSurface = new Surface(); // Current surface in use
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700108 boolean mDrawingStopped = true;
Robert Carrd5c7dd62017-03-08 10:39:30 -0800109 // 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;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700113
Robert Carrd5c7dd62017-03-08 10:39:30 -0800114 final Rect mScreenRect = new Rect();
115 SurfaceSession mSurfaceSession;
116
Andrii Kuliancf8f6832018-01-23 19:43:30 -0800117 SurfaceControlWithBackground mSurfaceControl;
Robert Carr3bc95b52017-03-20 21:57:23 -0700118 // 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;
Robert Carr33879132016-09-06 14:41:40 -0700121 final Rect mTmpRect = new Rect();
Dianne Hackborn694f79b2010-03-17 19:44:59 -0700122 final Configuration mConfiguration = new Configuration();
Igor Murashkina86ab6402013-08-30 12:58:36 -0700123
Robert Carrd5c7dd62017-03-08 10:39:30 -0800124 int mSubLayer = APPLICATION_MEDIA_SUBLAYER;
Igor Murashkina86ab6402013-08-30 12:58:36 -0700125
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700126 boolean mIsCreating = false;
John Reckf23a1b82016-06-22 14:23:31 -0700127 private volatile boolean mRtHandlingPositionUpdates = false;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700128
John Reckf6481082016-02-02 15:18:23 -0800129 private final ViewTreeObserver.OnScrollChangedListener mScrollChangedListener
Dianne Hackborne2af5c82010-03-18 15:44:34 -0700130 = new ViewTreeObserver.OnScrollChangedListener() {
Igor Murashkina86ab6402013-08-30 12:58:36 -0700131 @Override
Dianne Hackborne2af5c82010-03-18 15:44:34 -0700132 public void onScrollChanged() {
Robert Carrd5c7dd62017-03-08 10:39:30 -0800133 updateSurface();
Dianne Hackborne2af5c82010-03-18 15:44:34 -0700134 }
135 };
Igor Murashkina86ab6402013-08-30 12:58:36 -0700136
John Reckf6481082016-02-02 15:18:23 -0800137 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;
Robert Carrd5c7dd62017-03-08 10:39:30 -0800143 updateSurface();
John Reckf6481082016-02-02 15:18:23 -0800144 return true;
145 }
146 };
147
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700148 boolean mRequestedVisible = false;
Mathias Agopian6b7f1a62009-09-09 18:32:34 -0700149 boolean mWindowVisibility = false;
Robert Carrd5c7dd62017-03-08 10:39:30 -0800150 boolean mLastWindowVisibility = false;
Mathias Agopian6b7f1a62009-09-09 18:32:34 -0700151 boolean mViewVisibility = false;
Robert Carr414ebc62017-04-12 12:01:00 -0700152 boolean mWindowStopped = false;
153
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700154 int mRequestedWidth = -1;
155 int mRequestedHeight = -1;
Mathias Agopiand6ddcb72010-05-24 19:00:08 -0700156 /* 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;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700160
161 boolean mHaveFrame = false;
Dianne Hackborn6d05fd32011-11-19 14:36:15 -0800162 boolean mSurfaceCreated = false;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700163 long mLastLockTime = 0;
Igor Murashkina86ab6402013-08-30 12:58:36 -0700164
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700165 boolean mVisible = false;
Robert Carr64aadd02015-11-06 13:54:20 -0800166 int mWindowSpaceLeft = -1;
167 int mWindowSpaceTop = -1;
Robert Carrd5c7dd62017-03-08 10:39:30 -0800168 int mSurfaceWidth = -1;
169 int mSurfaceHeight = -1;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700170 int mFormat = -1;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700171 final Rect mSurfaceFrame = new Rect();
Dianne Hackborn726426e2010-03-31 22:04:36 -0700172 int mLastSurfaceWidth = -1, mLastSurfaceHeight = -1;
Mitsuru Oshima64f59342009-06-21 00:03:11 -0700173 private Translator mTranslator;
Romain Guyf2499fa2011-01-26 18:31:23 -0800174
Romain Guy01d5edc2011-01-28 11:28:53 -0800175 private boolean mGlobalListenersAdded;
Robert Carr8508bb22017-03-27 15:46:27 -0700176 private boolean mAttachedToWindow;
Romain Guyf2499fa2011-01-26 18:31:23 -0800177
Robert Carrd5c7dd62017-03-08 10:39:30 -0800178 private int mSurfaceFlags = SurfaceControl.HIDDEN;
179
Robert Carr8508bb22017-03-27 15:46:27 -0700180 private int mPendingReportDraws;
181
Robert Carr27a800a2018-03-16 13:33:45 -0700182 private SurfaceControl.Transaction mRtTransaction = new SurfaceControl.Transaction();
183
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700184 public SurfaceView(Context context) {
Alan Viverette768ca7d2016-08-04 09:54:14 -0400185 this(context, null);
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700186 }
Igor Murashkina86ab6402013-08-30 12:58:36 -0700187
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700188 public SurfaceView(Context context, AttributeSet attrs) {
Alan Viverette768ca7d2016-08-04 09:54:14 -0400189 this(context, attrs, 0);
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700190 }
191
Alan Viverette617feb92013-09-09 18:09:13 -0700192 public SurfaceView(Context context, AttributeSet attrs, int defStyleAttr) {
Alan Viverette768ca7d2016-08-04 09:54:14 -0400193 this(context, attrs, defStyleAttr, 0);
Alan Viverette617feb92013-09-09 18:09:13 -0700194 }
195
196 public SurfaceView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
197 super(context, attrs, defStyleAttr, defStyleRes);
John Reck3acf03822016-11-02 11:14:47 -0700198 mRenderNode.requestPositionUpdates(this);
Mathias Agopiand6ddcb72010-05-24 19:00:08 -0700199
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700200 setWillNotDraw(true);
201 }
Igor Murashkina86ab6402013-08-30 12:58:36 -0700202
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700203 /**
204 * Return the SurfaceHolder providing access and control over this
205 * SurfaceView's underlying surface.
Igor Murashkina86ab6402013-08-30 12:58:36 -0700206 *
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700207 * @return SurfaceHolder The holder of the surface.
208 */
209 public SurfaceHolder getHolder() {
210 return mSurfaceHolder;
211 }
212
Robert Carr414ebc62017-04-12 12:01:00 -0700213 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
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700225 @Override
226 protected void onAttachedToWindow() {
227 super.onAttachedToWindow();
Robert Carr414ebc62017-04-12 12:01:00 -0700228
229 getViewRootImpl().addWindowStoppedCallback(this);
Robert Carr00177cc2017-05-15 15:49:17 -0700230 mWindowStopped = false;
Robert Carr414ebc62017-04-12 12:01:00 -0700231
Mathias Agopian6b7f1a62009-09-09 18:32:34 -0700232 mViewVisibility = getVisibility() == VISIBLE;
Robert Carr414ebc62017-04-12 12:01:00 -0700233 updateRequestedVisibility();
Romain Guy01d5edc2011-01-28 11:28:53 -0800234
Robert Carr8508bb22017-03-27 15:46:27 -0700235 mAttachedToWindow = true;
Karthik Ravi Shankara59c3a52017-09-11 17:36:25 -0700236 mParent.requestTransparentRegion(SurfaceView.this);
Romain Guy01d5edc2011-01-28 11:28:53 -0800237 if (!mGlobalListenersAdded) {
238 ViewTreeObserver observer = getViewTreeObserver();
239 observer.addOnScrollChangedListener(mScrollChangedListener);
240 observer.addOnPreDrawListener(mDrawListener);
241 mGlobalListenersAdded = true;
242 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700243 }
244
245 @Override
246 protected void onWindowVisibilityChanged(int visibility) {
247 super.onWindowVisibilityChanged(visibility);
Mathias Agopian6b7f1a62009-09-09 18:32:34 -0700248 mWindowVisibility = visibility == VISIBLE;
Robert Carr414ebc62017-04-12 12:01:00 -0700249 updateRequestedVisibility();
Robert Carrd5c7dd62017-03-08 10:39:30 -0800250 updateSurface();
Mathias Agopian6b7f1a62009-09-09 18:32:34 -0700251 }
252
253 @Override
254 public void setVisibility(int visibility) {
255 super.setVisibility(visibility);
256 mViewVisibility = visibility == VISIBLE;
Robert Carr414ebc62017-04-12 12:01:00 -0700257 boolean newRequestedVisible = mWindowVisibility && mViewVisibility && !mWindowStopped;
Mathias Agopiancbeb3322012-05-12 19:57:07 -0700258 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;
Robert Carrd5c7dd62017-03-08 10:39:30 -0800268 updateSurface();
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700269 }
Romain Guyafc3e112010-06-07 17:04:33 -0700270
Robert Carrb53670a2017-05-25 18:20:49 -0700271 private void performDrawFinished() {
272 if (mPendingReportDraws > 0) {
273 mDrawFinished = true;
274 if (mAttachedToWindow) {
Robert Carrb53670a2017-05-25 18:20:49 -0700275 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
Robert Carr8508bb22017-03-27 15:46:27 -0700285 void notifyDrawFinished() {
286 ViewRootImpl viewRoot = getViewRootImpl();
287 if (viewRoot != null) {
288 viewRoot.pendingDrawFinished();
289 }
290 mPendingReportDraws--;
291 }
292
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700293 @Override
John Reck77e4a522014-10-01 10:38:07 -0700294 protected void onDetachedFromWindow() {
Lucas Dupin0c207342017-04-14 19:47:23 -0700295 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 }
Robert Carr414ebc62017-04-12 12:01:00 -0700304
Robert Carr8508bb22017-03-27 15:46:27 -0700305 mAttachedToWindow = false;
Romain Guy01d5edc2011-01-28 11:28:53 -0800306 if (mGlobalListenersAdded) {
307 ViewTreeObserver observer = getViewTreeObserver();
308 observer.removeOnScrollChangedListener(mScrollChangedListener);
309 observer.removeOnPreDrawListener(mDrawListener);
310 mGlobalListenersAdded = false;
311 }
312
Robert Carr8508bb22017-03-27 15:46:27 -0700313 while (mPendingReportDraws > 0) {
314 notifyDrawFinished();
315 }
316
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700317 mRequestedVisible = false;
Wonsik Kim5aec7b92017-03-07 17:15:50 -0800318
Robert Carrd5c7dd62017-03-08 10:39:30 -0800319 updateSurface();
320 if (mSurfaceControl != null) {
321 mSurfaceControl.destroy();
322 }
323 mSurfaceControl = null;
324
325 mHaveFrame = false;
Robert Carr414ebc62017-04-12 12:01:00 -0700326
John Reck77e4a522014-10-01 10:38:07 -0700327 super.onDetachedFromWindow();
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700328 }
329
330 @Override
331 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Dianne Hackborn189ee182010-12-02 21:48:53 -0800332 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);
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700338 setMeasuredDimension(width, height);
339 }
Igor Murashkina86ab6402013-08-30 12:58:36 -0700340
Mathias Agopianef115302010-10-04 20:15:08 -0700341 /** @hide */
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700342 @Override
Mathias Agopian995bb9d2010-10-04 17:13:15 -0700343 protected boolean setFrame(int left, int top, int right, int bottom) {
344 boolean result = super.setFrame(left, top, right, bottom);
Robert Carrd5c7dd62017-03-08 10:39:30 -0800345 updateSurface();
Mathias Agopian995bb9d2010-10-04 17:13:15 -0700346 return result;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700347 }
348
349 @Override
350 public boolean gatherTransparentRegion(Region region) {
Robert Carr3ca12be72017-05-22 14:57:48 -0700351 if (isAboveParent() || !mDrawFinished) {
Dianne Hackborn1cd403e2009-09-14 22:29:14 -0700352 return super.gatherTransparentRegion(region);
353 }
Igor Murashkina86ab6402013-08-30 12:58:36 -0700354
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700355 boolean opaque = true;
Dianne Hackborn4702a852012-08-17 15:18:29 -0700356 if ((mPrivateFlags & PFLAG_SKIP_DRAW) == 0) {
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700357 // 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) {
Robert Carrd5c7dd62017-03-08 10:39:30 -0800378 if (mDrawFinished && !isAboveParent()) {
Dianne Hackborn1cd403e2009-09-14 22:29:14 -0700379 // draw() is not called when SKIP_DRAW is set
Dianne Hackborn4702a852012-08-17 15:18:29 -0700380 if ((mPrivateFlags & PFLAG_SKIP_DRAW) == 0) {
Dianne Hackborn1cd403e2009-09-14 22:29:14 -0700381 // punch a whole in the view-hierarchy below us
382 canvas.drawColor(0, PorterDuff.Mode.CLEAR);
383 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700384 }
385 super.draw(canvas);
386 }
387
388 @Override
389 protected void dispatchDraw(Canvas canvas) {
Robert Carrd5c7dd62017-03-08 10:39:30 -0800390 if (mDrawFinished && !isAboveParent()) {
391 // draw() is not called when SKIP_DRAW is set
Dianne Hackborn4702a852012-08-17 15:18:29 -0700392 if ((mPrivateFlags & PFLAG_SKIP_DRAW) == PFLAG_SKIP_DRAW) {
Dianne Hackborn1cd403e2009-09-14 22:29:14 -0700393 // punch a whole in the view-hierarchy below us
394 canvas.drawColor(0, PorterDuff.Mode.CLEAR);
395 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700396 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700397 super.dispatchDraw(canvas);
398 }
399
Dianne Hackbornc4d5d022009-05-21 17:32:42 -0700400 /**
Dianne Hackborn29e4a3c2009-09-30 22:35:40 -0700401 * 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.
Igor Murashkina86ab6402013-08-30 12:58:36 -0700405 *
Dianne Hackborn29e4a3c2009-09-30 22:35:40 -0700406 * <p>Note that this must be set before the surface view's containing
407 * window is attached to the window manager.
Igor Murashkina86ab6402013-08-30 12:58:36 -0700408 *
Dianne Hackborn29e4a3c2009-09-30 22:35:40 -0700409 * <p>Calling this overrides any previous call to {@link #setZOrderOnTop}.
410 */
411 public void setZOrderMediaOverlay(boolean isMediaOverlay) {
Robert Carrd5c7dd62017-03-08 10:39:30 -0800412 mSubLayer = isMediaOverlay
413 ? APPLICATION_MEDIA_OVERLAY_SUBLAYER : APPLICATION_MEDIA_SUBLAYER;
Dianne Hackborn29e4a3c2009-09-30 22:35:40 -0700414 }
Igor Murashkina86ab6402013-08-30 12:58:36 -0700415
Dianne Hackborn29e4a3c2009-09-30 22:35:40 -0700416 /**
Dianne Hackborn1cd403e2009-09-14 22:29:14 -0700417 * 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.
Igor Murashkina86ab6402013-08-30 12:58:36 -0700423 *
Dianne Hackborn1cd403e2009-09-14 22:29:14 -0700424 * <p>Note that this must be set before the surface view's containing
425 * window is attached to the window manager.
Igor Murashkina86ab6402013-08-30 12:58:36 -0700426 *
Dianne Hackborn29e4a3c2009-09-30 22:35:40 -0700427 * <p>Calling this overrides any previous call to {@link #setZOrderMediaOverlay}.
Dianne Hackborn1cd403e2009-09-14 22:29:14 -0700428 */
Dianne Hackborn29e4a3c2009-09-30 22:35:40 -0700429 public void setZOrderOnTop(boolean onTop) {
Derek Sollenbergerecde72f2010-03-01 13:44:42 -0500430 if (onTop) {
Robert Carrd5c7dd62017-03-08 10:39:30 -0800431 mSubLayer = APPLICATION_PANEL_SUBLAYER;
Derek Sollenbergerecde72f2010-03-01 13:44:42 -0500432 } else {
Robert Carrd5c7dd62017-03-08 10:39:30 -0800433 mSubLayer = APPLICATION_MEDIA_SUBLAYER;
Derek Sollenbergerecde72f2010-03-01 13:44:42 -0500434 }
Dianne Hackborn1cd403e2009-09-14 22:29:14 -0700435 }
Jeff Brownf0681b32012-10-23 17:35:57 -0700436
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) {
Robert Carrd5c7dd62017-03-08 10:39:30 -0800451 mSurfaceFlags |= SurfaceControl.SECURE;
Jeff Brownf0681b32012-10-23 17:35:57 -0700452 } else {
Robert Carrd5c7dd62017-03-08 10:39:30 -0800453 mSurfaceFlags &= ~SurfaceControl.SECURE;
Jeff Brownf0681b32012-10-23 17:35:57 -0700454 }
455 }
456
Robert Carr55235552017-06-02 14:21:03 -0700457 private void updateOpaqueFlag() {
Robert Carr851e7e42017-06-06 14:04:50 -0700458 if (!PixelFormat.formatHasAlpha(mRequestedFormat)) {
Robert Carr55235552017-06-02 14:21:03 -0700459 mSurfaceFlags |= SurfaceControl.OPAQUE;
460 } else {
461 mSurfaceFlags &= ~SurfaceControl.OPAQUE;
462 }
463 }
464
Robert Carrd5c7dd62017-03-08 10:39:30 -0800465 private Rect getParentSurfaceInsets() {
466 final ViewRootImpl root = getViewRootImpl();
467 if (root == null) {
468 return null;
469 } else {
470 return root.mWindowAttributes.surfaceInsets;
471 }
Jeff Tinker3896db12017-03-03 00:20:22 +0000472 }
473
Youngsang Cho9a22f0f2014-04-09 22:51:54 +0900474 /** @hide */
Robert Carrd5c7dd62017-03-08 10:39:30 -0800475 protected void updateSurface() {
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700476 if (!mHaveFrame) {
477 return;
478 }
Jeff Browna175a5b2012-02-15 19:18:31 -0800479 ViewRootImpl viewRoot = getViewRootImpl();
Robert Carrd5c7dd62017-03-08 10:39:30 -0800480 if (viewRoot == null || viewRoot.mSurface == null || !viewRoot.mSurface.isValid()) {
481 return;
Joe Onorato168173a2009-08-12 21:40:29 -0700482 }
Mitsuru Oshima64f59342009-06-21 00:03:11 -0700483
Robert Carrd5c7dd62017-03-08 10:39:30 -0800484 mTranslator = viewRoot.mTranslator;
Dianne Hackborn5be8de32011-05-24 18:11:57 -0700485 if (mTranslator != null) {
486 mSurface.setCompatibilityTranslator(mTranslator);
Mitsuru Oshima38ed7d772009-07-21 14:39:34 -0700487 }
Igor Murashkina86ab6402013-08-30 12:58:36 -0700488
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700489 int myWidth = mRequestedWidth;
490 if (myWidth <= 0) myWidth = getWidth();
491 int myHeight = mRequestedHeight;
492 if (myHeight <= 0) myHeight = getHeight();
Mitsuru Oshima001a6e522009-05-11 21:14:03 -0700493
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700494 final boolean formatChanged = mFormat != mRequestedFormat;
Robert Carr7c67b7d2017-07-06 15:28:34 -0700495 final boolean visibleChanged = mVisible != mRequestedVisible;
496 final boolean creating = (mSurfaceControl == null || formatChanged || visibleChanged)
Robert Carrd5c7dd62017-03-08 10:39:30 -0800497 && mRequestedVisible;
498 final boolean sizeChanged = mSurfaceWidth != myWidth || mSurfaceHeight != myHeight;
Robert Carrd5c7dd62017-03-08 10:39:30 -0800499 final boolean windowVisibleChanged = mWindowVisibility != mLastWindowVisibility;
Robert Carr49b593e2016-07-11 12:21:18 -0700500 boolean redrawNeeded = false;
501
Robert Carrd5c7dd62017-03-08 10:39:30 -0800502 if (creating || formatChanged || sizeChanged || visibleChanged || windowVisibleChanged) {
John Reckf6481082016-02-02 15:18:23 -0800503 getLocationInWindow(mLocation);
504
John Reckaa6e84f2016-06-16 15:36:13 -0700505 if (DEBUG) Log.i(TAG, System.identityHashCode(this) + " "
506 + "Changes: creating=" + creating
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700507 + " format=" + formatChanged + " size=" + sizeChanged
508 + " visible=" + visibleChanged
Robert Carr64aadd02015-11-06 13:54:20 -0800509 + " left=" + (mWindowSpaceLeft != mLocation[0])
510 + " top=" + (mWindowSpaceTop != mLocation[1]));
Mitsuru Oshima64f59342009-06-21 00:03:11 -0700511
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700512 try {
513 final boolean visible = mVisible = mRequestedVisible;
Robert Carr64aadd02015-11-06 13:54:20 -0800514 mWindowSpaceLeft = mLocation[0];
515 mWindowSpaceTop = mLocation[1];
Robert Carrd5c7dd62017-03-08 10:39:30 -0800516 mSurfaceWidth = myWidth;
517 mSurfaceHeight = myHeight;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700518 mFormat = mRequestedFormat;
Robert Carrd5c7dd62017-03-08 10:39:30 -0800519 mLastWindowVisibility = mWindowVisibility;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700520
Robert Carrd5c7dd62017-03-08 10:39:30 -0800521 mScreenRect.left = mWindowSpaceLeft;
522 mScreenRect.top = mWindowSpaceTop;
523 mScreenRect.right = mWindowSpaceLeft + getWidth();
524 mScreenRect.bottom = mWindowSpaceTop + getHeight();
Mitsuru Oshima64f59342009-06-21 00:03:11 -0700525 if (mTranslator != null) {
Robert Carrd5c7dd62017-03-08 10:39:30 -0800526 mTranslator.translateRectInAppWindowToScreen(mScreenRect);
Mitsuru Oshima64f59342009-06-21 00:03:11 -0700527 }
Igor Murashkina86ab6402013-08-30 12:58:36 -0700528
Robert Carrd5c7dd62017-03-08 10:39:30 -0800529 final Rect surfaceInsets = getParentSurfaceInsets();
530 mScreenRect.offset(surfaceInsets.left, surfaceInsets.top);
531
532 if (creating) {
533 mSurfaceSession = new SurfaceSession(viewRoot.mSurface);
Robert Carr3bc95b52017-03-20 21:57:23 -0700534 mDeferredDestroySurfaceControl = mSurfaceControl;
Robert Carr55235552017-06-02 14:21:03 -0700535
536 updateOpaqueFlag();
Robert Carre625fcf2017-09-01 12:36:28 -0700537 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));
Robert Carr44ab5752017-03-20 21:47:11 -0700546 } else if (mSurfaceControl == null) {
547 return;
Robert Carr64aadd02015-11-06 13:54:20 -0800548 }
549
Robert Carrd5c7dd62017-03-08 10:39:30 -0800550 boolean realSizeChanged = false;
Dianne Hackborn6d05fd32011-11-19 14:36:15 -0800551
Dianne Hackborn726426e2010-03-31 22:04:36 -0700552 mSurfaceLock.lock();
553 try {
Dianne Hackborn726426e2010-03-31 22:04:36 -0700554 mDrawingStopped = !visible;
Igor Murashkina86ab6402013-08-30 12:58:36 -0700555
John Reckaa6e84f2016-06-16 15:36:13 -0700556 if (DEBUG) Log.i(TAG, System.identityHashCode(this) + " "
557 + "Cur surface: " + mSurface);
Dianne Hackborn6d05fd32011-11-19 14:36:15 -0800558
Robert Carrd5c7dd62017-03-08 10:39:30 -0800559 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.
Robert Carr511719f2017-03-13 15:27:15 -0700571 //
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) {
Robert Carrd5c7dd62017-03-08 10:39:30 -0800577 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();
Dianne Hackborn726426e2010-03-31 22:04:36 -0700587 }
Dianne Hackborn6d05fd32011-11-19 14:36:15 -0800588
Robert Carrd5c7dd62017-03-08 10:39:30 -0800589 if (sizeChanged || creating) {
590 redrawNeeded = true;
591 }
Dianne Hackborn6d05fd32011-11-19 14:36:15 -0800592
Dianne Hackborn726426e2010-03-31 22:04:36 -0700593 mSurfaceFrame.left = 0;
594 mSurfaceFrame.top = 0;
595 if (mTranslator == null) {
Robert Carrd5c7dd62017-03-08 10:39:30 -0800596 mSurfaceFrame.right = mSurfaceWidth;
597 mSurfaceFrame.bottom = mSurfaceHeight;
Dianne Hackborn726426e2010-03-31 22:04:36 -0700598 } else {
599 float appInvertedScale = mTranslator.applicationInvertedScale;
Robert Carrd5c7dd62017-03-08 10:39:30 -0800600 mSurfaceFrame.right = (int) (mSurfaceWidth * appInvertedScale + 0.5f);
601 mSurfaceFrame.bottom = (int) (mSurfaceHeight * appInvertedScale + 0.5f);
Dianne Hackborn726426e2010-03-31 22:04:36 -0700602 }
Igor Murashkina86ab6402013-08-30 12:58:36 -0700603
Dianne Hackborn726426e2010-03-31 22:04:36 -0700604 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();
Mitsuru Oshima589cebe2009-07-22 20:38:58 -0700612 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700613
614 try {
Robert Carrd5c7dd62017-03-08 10:39:30 -0800615 redrawNeeded |= visible && !mDrawFinished;
Dianne Hackbornd76b67c2010-07-13 17:48:30 -0700616
Dianne Hackborn6d05fd32011-11-19 14:36:15 -0800617 SurfaceHolder.Callback callbacks[] = null;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700618
Robert Carrd5c7dd62017-03-08 10:39:30 -0800619 final boolean surfaceChanged = creating;
Dianne Hackborn6d05fd32011-11-19 14:36:15 -0800620 if (mSurfaceCreated && (surfaceChanged || (!visible && visibleChanged))) {
621 mSurfaceCreated = false;
622 if (mSurface.isValid()) {
John Reckaa6e84f2016-06-16 15:36:13 -0700623 if (DEBUG) Log.i(TAG, System.identityHashCode(this) + " "
624 + "visibleChanged -- surfaceDestroyed");
Dianne Hackborn6d05fd32011-11-19 14:36:15 -0800625 callbacks = getSurfaceCallbacks();
626 for (SurfaceHolder.Callback c : callbacks) {
627 c.surfaceDestroyed(mSurfaceHolder);
628 }
Robert Carr387838b2016-09-07 14:12:44 -0700629 // 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.
John Reck6ba466f2016-09-30 14:30:04 -0700640 if (mSurface.isValid()) {
641 mSurface.forceScopedDisconnect();
642 }
Mitsuru Oshima3d914922009-05-13 22:29:15 -0700643 }
Dianne Hackborn6d05fd32011-11-19 14:36:15 -0800644 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700645
Robert Carrd5c7dd62017-03-08 10:39:30 -0800646 if (creating) {
647 mSurface.copyFrom(mSurfaceControl);
648 }
649
Bryce Lee453fc362017-06-20 10:47:55 -0700650 if (sizeChanged && getContext().getApplicationInfo().targetSdkVersion
Bryce Lee02949f12017-06-16 07:20:34 -0700651 < 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
Andreas Röhlf750b8c2012-07-02 13:06:26 +0200660 if (visible && mSurface.isValid()) {
Dianne Hackborn6d05fd32011-11-19 14:36:15 -0800661 if (!mSurfaceCreated && (surfaceChanged || visibleChanged)) {
662 mSurfaceCreated = true;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700663 mIsCreating = true;
John Reckaa6e84f2016-06-16 15:36:13 -0700664 if (DEBUG) Log.i(TAG, System.identityHashCode(this) + " "
665 + "visibleChanged -- surfaceCreated");
Dianne Hackborn6d05fd32011-11-19 14:36:15 -0800666 if (callbacks == null) {
667 callbacks = getSurfaceCallbacks();
668 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700669 for (SurfaceHolder.Callback c : callbacks) {
670 c.surfaceCreated(mSurfaceHolder);
671 }
672 }
673 if (creating || formatChanged || sizeChanged
Dianne Hackborn726426e2010-03-31 22:04:36 -0700674 || visibleChanged || realSizeChanged) {
John Reckaa6e84f2016-06-16 15:36:13 -0700675 if (DEBUG) Log.i(TAG, System.identityHashCode(this) + " "
676 + "surfaceChanged -- format=" + mFormat
Dianne Hackborn6d05fd32011-11-19 14:36:15 -0800677 + " w=" + myWidth + " h=" + myHeight);
678 if (callbacks == null) {
679 callbacks = getSurfaceCallbacks();
680 }
Dianne Hackborn251fd432010-07-14 16:56:31 -0700681 for (SurfaceHolder.Callback c : callbacks) {
682 c.surfaceChanged(mSurfaceHolder, mFormat, myWidth, myHeight);
683 }
Dianne Hackbornd76b67c2010-07-13 17:48:30 -0700684 }
685 if (redrawNeeded) {
John Reckaa6e84f2016-06-16 15:36:13 -0700686 if (DEBUG) Log.i(TAG, System.identityHashCode(this) + " "
687 + "surfaceRedrawNeeded");
Dianne Hackborn6d05fd32011-11-19 14:36:15 -0800688 if (callbacks == null) {
689 callbacks = getSurfaceCallbacks();
690 }
Robert Carr8508bb22017-03-27 15:46:27 -0700691
692 mPendingReportDraws++;
693 viewRoot.drawPending();
Robert Carr25cfa132016-11-16 13:24:09 -0800694 SurfaceCallbackHelper sch =
Robert Carrd5c7dd62017-03-08 10:39:30 -0800695 new SurfaceCallbackHelper(this::onDrawFinished);
Robert Carr25cfa132016-11-16 13:24:09 -0800696 sch.dispatchSurfaceRedrawNeededAsync(mSurfaceHolder, callbacks);
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700697 }
698 }
699 } finally {
700 mIsCreating = false;
Robert Carrd5c7dd62017-03-08 10:39:30 -0800701 if (mSurfaceControl != null && !mSurfaceCreated) {
Robert Carrde844432017-05-04 13:45:45 -0700702 mSurface.release();
Robert Carr73678092017-05-19 15:06:50 -0700703 // 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 }
Robert Carrd5c7dd62017-03-08 10:39:30 -0800712 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700713 }
Robert Carrd5c7dd62017-03-08 10:39:30 -0800714 } catch (Exception ex) {
Robert Carr44ab5752017-03-20 21:47:11 -0700715 Log.e(TAG, "Exception configuring surface", ex);
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700716 }
Dianne Hackborn6d05fd32011-11-19 14:36:15 -0800717 if (DEBUG) Log.v(
Robert Carrd5c7dd62017-03-08 10:39:30 -0800718 TAG, "Layout: x=" + mScreenRect.left + " y=" + mScreenRect.top
719 + " w=" + mScreenRect.width() + " h=" + mScreenRect.height()
720 + ", frame=" + mSurfaceFrame);
John Reckf23a1b82016-06-22 14:23:31 -0700721 } 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
Robert Carrd5c7dd62017-03-08 10:39:30 -0800724 getLocationInSurface(mLocation);
John Reckf6481082016-02-02 15:18:23 -0800725 final boolean positionChanged = mWindowSpaceLeft != mLocation[0]
726 || mWindowSpaceTop != mLocation[1];
Robert Carrd5c7dd62017-03-08 10:39:30 -0800727 final boolean layoutSizeChanged = getWidth() != mScreenRect.width()
728 || getHeight() != mScreenRect.height();
John Reckf6481082016-02-02 15:18:23 -0800729 if (positionChanged || layoutSizeChanged) { // Only the position has changed
730 mWindowSpaceLeft = mLocation[0];
731 mWindowSpaceTop = mLocation[1];
Robert Carrd5c7dd62017-03-08 10:39:30 -0800732 // For our size changed check, we keep mScreenRect.width() and mScreenRect.height()
John Reckf6481082016-02-02 15:18:23 -0800733 // in view local space.
Robert Carrd5c7dd62017-03-08 10:39:30 -0800734 mLocation[0] = getWidth();
735 mLocation[1] = getHeight();
Robert Carr64aadd02015-11-06 13:54:20 -0800736
Robert Carrd5c7dd62017-03-08 10:39:30 -0800737 mScreenRect.set(mWindowSpaceLeft, mWindowSpaceTop,
Robert Carrc90e5f82017-07-18 13:10:02 -0700738 mWindowSpaceLeft + mLocation[0], mWindowSpaceTop + mLocation[1]);
John Reckf057fb52016-04-15 13:46:29 -0700739
740 if (mTranslator != null) {
Robert Carrd5c7dd62017-03-08 10:39:30 -0800741 mTranslator.translateRectInAppWindowToScreen(mScreenRect);
John Reckf057fb52016-04-15 13:46:29 -0700742 }
743
Robert Carr3651ab82017-04-25 12:05:34 -0700744 if (mSurfaceControl == null) {
745 return;
746 }
747
Bryce Lee16e50892017-04-11 01:59:37 +0000748 if (!isHardwareAccelerated() || !mRtHandlingPositionUpdates) {
John Reckf23a1b82016-06-22 14:23:31 -0700749 try {
Robert Carrd5c7dd62017-03-08 10:39:30 -0800750 if (DEBUG) Log.d(TAG, String.format("%d updateSurfacePosition UI, " +
John Reckf23a1b82016-06-22 14:23:31 -0700751 "postion = [%d, %d, %d, %d]", System.identityHashCode(this),
Robert Carrd5c7dd62017-03-08 10:39:30 -0800752 mScreenRect.left, mScreenRect.top,
753 mScreenRect.right, mScreenRect.bottom));
754 setParentSpaceRectangle(mScreenRect, -1);
755 } catch (Exception ex) {
Robert Carr44ab5752017-03-20 21:47:11 -0700756 Log.e(TAG, "Exception configuring surface", ex);
John Reckf23a1b82016-06-22 14:23:31 -0700757 }
John Reckf6481082016-02-02 15:18:23 -0800758 }
Rob Carr64e516f2015-10-29 00:20:45 +0000759 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700760 }
761 }
762
Robert Carrd5c7dd62017-03-08 10:39:30 -0800763 private void onDrawFinished() {
764 if (DEBUG) {
765 Log.i(TAG, System.identityHashCode(this) + " "
766 + "finishedDrawing");
767 }
Robert Carr3bc95b52017-03-20 21:57:23 -0700768
769 if (mDeferredDestroySurfaceControl != null) {
770 mDeferredDestroySurfaceControl.destroy();
771 mDeferredDestroySurfaceControl = null;
772 }
773
John Reck79925002017-05-26 13:57:14 -0700774 runOnUiThread(() -> {
Robert Carrb53670a2017-05-25 18:20:49 -0700775 performDrawFinished();
John Reck79925002017-05-26 13:57:14 -0700776 });
Robert Carrd5c7dd62017-03-08 10:39:30 -0800777 }
778
Robert Carr27a800a2018-03-16 13:33:45 -0700779 /**
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
Robert Carr386fd702018-03-23 13:46:39 -0700790 private void applySurfaceTransforms(SurfaceControl surface, Rect position, long frameNumber) {
Robert Carr27a800a2018-03-16 13:33:45 -0700791 if (frameNumber > 0) {
Robert Carr386fd702018-03-23 13:46:39 -0700792 final ViewRootImpl viewRoot = getViewRootImpl();
793
794 mRtTransaction.deferTransactionUntilSurface(surface, viewRoot.mSurface,
Robert Carr27a800a2018-03-16 13:33:45 -0700795 frameNumber);
Robert Carrd5c7dd62017-03-08 10:39:30 -0800796 }
Robert Carr386fd702018-03-23 13:46:39 -0700797
798 mRtTransaction.setPosition(surface, position.left, position.top);
799 mRtTransaction.setMatrix(surface,
Robert Carr27a800a2018-03-16 13:33:45 -0700800 position.width() / (float) mSurfaceWidth,
801 0.0f, 0.0f,
802 position.height() / (float) mSurfaceHeight);
Robert Carr386fd702018-03-23 13:46:39 -0700803 }
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);
Robert Carr27a800a2018-03-16 13:33:45 -0700810
811 applyChildSurfaceTransaction_renderWorker(mRtTransaction, viewRoot.mSurface,
812 frameNumber);
813
814 mRtTransaction.apply();
Robert Carrd5c7dd62017-03-08 10:39:30 -0800815 }
816
John Reckf6481082016-02-02 15:18:23 -0800817 private Rect mRTLastReportedPosition = new Rect();
818
819 /**
Robert Carr33879132016-09-06 14:41:40 -0700820 * Called by native by a Rendering Worker thread to update the window position
John Reckf6481082016-02-02 15:18:23 -0800821 * @hide
822 */
Robert Carrd5c7dd62017-03-08 10:39:30 -0800823 public final void updateSurfacePosition_renderWorker(long frameNumber,
John Reckf6481082016-02-02 15:18:23 -0800824 int left, int top, int right, int bottom) {
Robert Carrd5c7dd62017-03-08 10:39:30 -0800825 if (mSurfaceControl == null) {
John Reckf6481082016-02-02 15:18:23 -0800826 return;
827 }
Robert Carrd5c7dd62017-03-08 10:39:30 -0800828
John Reckf23a1b82016-06-22 14:23:31 -0700829 // 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;
John Reckf6481082016-02-02 15:18:23 -0800838 if (mRTLastReportedPosition.left == left
839 && mRTLastReportedPosition.top == top
840 && mRTLastReportedPosition.right == right
841 && mRTLastReportedPosition.bottom == bottom) {
842 return;
843 }
844 try {
845 if (DEBUG) {
Robert Carrd5c7dd62017-03-08 10:39:30 -0800846 Log.d(TAG, String.format("%d updateSurfacePosition RenderWorker, frameNr = %d, " +
John Reckaa6e84f2016-06-16 15:36:13 -0700847 "postion = [%d, %d, %d, %d]", System.identityHashCode(this),
848 frameNumber, left, top, right, bottom));
John Reckf6481082016-02-02 15:18:23 -0800849 }
Wonsik Kim5aec7b92017-03-07 17:15:50 -0800850 mRTLastReportedPosition.set(left, top, right, bottom);
Robert Carrd5c7dd62017-03-08 10:39:30 -0800851 setParentSpaceRectangle(mRTLastReportedPosition, frameNumber);
852 // Now overwrite mRTLastReportedPosition with our values
853 } catch (Exception ex) {
John Reckf6481082016-02-02 15:18:23 -0800854 Log.e(TAG, "Exception from repositionChild", ex);
855 }
856 }
857
John Reckaa6e84f2016-06-16 15:36:13 -0700858 /**
Robert Carrd5c7dd62017-03-08 10:39:30 -0800859 * Called by native on RenderThread to notify that the view is no longer in the
Robert Carr33879132016-09-06 14:41:40 -0700860 * draw tree. UI thread is blocked at this point.
John Reckaa6e84f2016-06-16 15:36:13 -0700861 * @hide
862 */
Robert Carrd5c7dd62017-03-08 10:39:30 -0800863 public final void surfacePositionLost_uiRtSync(long frameNumber) {
John Reckaa6e84f2016-06-16 15:36:13 -0700864 if (DEBUG) {
Robert Carr33879132016-09-06 14:41:40 -0700865 Log.d(TAG, String.format("%d windowPositionLost, frameNr = %d",
John Reckaa6e84f2016-06-16 15:36:13 -0700866 System.identityHashCode(this), frameNumber));
867 }
Robert Carrad3a4932017-06-20 14:55:21 -0700868 mRTLastReportedPosition.setEmpty();
869
Robert Carrd5c7dd62017-03-08 10:39:30 -0800870 if (mSurfaceControl == null) {
John Reck474659c2016-06-27 07:56:37 -0700871 return;
872 }
John Reckf23a1b82016-06-22 14:23:31 -0700873 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.
Robert Carrd5c7dd62017-03-08 10:39:30 -0800879 if (!mScreenRect.isEmpty() && !mScreenRect.equals(mRTLastReportedPosition)) {
John Reckf23a1b82016-06-22 14:23:31 -0700880 try {
Robert Carrd5c7dd62017-03-08 10:39:30 -0800881 if (DEBUG) Log.d(TAG, String.format("%d updateSurfacePosition, " +
John Reckf23a1b82016-06-22 14:23:31 -0700882 "postion = [%d, %d, %d, %d]", System.identityHashCode(this),
Robert Carrd5c7dd62017-03-08 10:39:30 -0800883 mScreenRect.left, mScreenRect.top,
884 mScreenRect.right, mScreenRect.bottom));
885 setParentSpaceRectangle(mScreenRect, frameNumber);
886 } catch (Exception ex) {
Robert Carr44ab5752017-03-20 21:47:11 -0700887 Log.e(TAG, "Exception configuring surface", ex);
John Reckf23a1b82016-06-22 14:23:31 -0700888 }
889 }
John Reckf23a1b82016-06-22 14:23:31 -0700890 }
John Reckaa6e84f2016-06-16 15:36:13 -0700891 }
892
Dianne Hackborn6d05fd32011-11-19 14:36:15 -0800893 private SurfaceHolder.Callback[] getSurfaceCallbacks() {
894 SurfaceHolder.Callback callbacks[];
895 synchronized (mCallbacks) {
896 callbacks = new SurfaceHolder.Callback[mCallbacks.size()];
897 mCallbacks.toArray(callbacks);
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700898 }
Dianne Hackborn6d05fd32011-11-19 14:36:15 -0800899 return callbacks;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700900 }
901
John Reck79925002017-05-26 13:57:14 -0700902 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
Yohei Yukawa3b5011a2017-03-16 15:34:12 -0700911 /**
Derek Sollenberger7179b812010-03-22 13:41:20 -0400912 * 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
Robert Carrd5c7dd62017-03-08 10:39:30 -0800922 private boolean isAboveParent() {
923 return mSubLayer >= 0;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700924 }
925
Andrii Kuliancf8f6832018-01-23 19:43:30 -0800926 /**
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
Igor Murashkina86ab6402013-08-30 12:58:36 -0700937 private final SurfaceHolder mSurfaceHolder = new SurfaceHolder() {
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700938 private static final String LOG_TAG = "SurfaceHolder";
Igor Murashkina86ab6402013-08-30 12:58:36 -0700939
940 @Override
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700941 public boolean isCreating() {
942 return mIsCreating;
943 }
944
Igor Murashkina86ab6402013-08-30 12:58:36 -0700945 @Override
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700946 public void addCallback(Callback callback) {
947 synchronized (mCallbacks) {
Igor Murashkina86ab6402013-08-30 12:58:36 -0700948 // This is a linear search, but in practice we'll
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700949 // have only a couple callbacks, so it doesn't matter.
Igor Murashkina86ab6402013-08-30 12:58:36 -0700950 if (mCallbacks.contains(callback) == false) {
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700951 mCallbacks.add(callback);
952 }
953 }
954 }
955
Igor Murashkina86ab6402013-08-30 12:58:36 -0700956 @Override
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700957 public void removeCallback(Callback callback) {
958 synchronized (mCallbacks) {
959 mCallbacks.remove(callback);
960 }
961 }
Igor Murashkina86ab6402013-08-30 12:58:36 -0700962
963 @Override
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700964 public void setFixedSize(int width, int height) {
965 if (mRequestedWidth != width || mRequestedHeight != height) {
966 mRequestedWidth = width;
967 mRequestedHeight = height;
968 requestLayout();
969 }
970 }
971
Igor Murashkina86ab6402013-08-30 12:58:36 -0700972 @Override
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700973 public void setSizeFromLayout() {
974 if (mRequestedWidth != -1 || mRequestedHeight != -1) {
975 mRequestedWidth = mRequestedHeight = -1;
976 requestLayout();
977 }
978 }
979
Igor Murashkina86ab6402013-08-30 12:58:36 -0700980 @Override
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700981 public void setFormat(int format) {
Mathias Agopian2d468c52010-06-14 21:50:48 -0700982 // for backward compatibility reason, OPAQUE always
983 // means 565 for SurfaceView
984 if (format == PixelFormat.OPAQUE)
985 format = PixelFormat.RGB_565;
986
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700987 mRequestedFormat = format;
Robert Carrd5c7dd62017-03-08 10:39:30 -0800988 if (mSurfaceControl != null) {
989 updateSurface();
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700990 }
991 }
992
Mathias Agopiand2112302010-12-07 19:38:17 -0800993 /**
994 * @deprecated setType is now ignored.
995 */
Igor Murashkina86ab6402013-08-30 12:58:36 -0700996 @Override
Mathias Agopiand2112302010-12-07 19:38:17 -0800997 @Deprecated
998 public void setType(int type) { }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700999
Igor Murashkina86ab6402013-08-30 12:58:36 -07001000 @Override
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07001001 public void setKeepScreenOn(boolean screenOn) {
John Reck79925002017-05-26 13:57:14 -07001002 runOnUiThread(() -> SurfaceView.this.setKeepScreenOn(screenOn));
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07001003 }
Igor Murashkina86ab6402013-08-30 12:58:36 -07001004
Mathias Agopian9ddf32a2013-04-17 15:04:47 -07001005 /**
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 */
Igor Murashkina86ab6402013-08-30 12:58:36 -07001014 @Override
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07001015 public Canvas lockCanvas() {
John Reck6bc70142016-10-26 16:49:17 -07001016 return internalLockCanvas(null, false);
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07001017 }
1018
Mathias Agopian9ddf32a2013-04-17 15:04:47 -07001019 /**
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 */
Igor Murashkina86ab6402013-08-30 12:58:36 -07001034 @Override
Mathias Agopian9ddf32a2013-04-17 15:04:47 -07001035 public Canvas lockCanvas(Rect inOutDirty) {
John Reck6bc70142016-10-26 16:49:17 -07001036 return internalLockCanvas(inOutDirty, false);
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07001037 }
1038
John Reck6bc70142016-10-26 16:49:17 -07001039 @Override
1040 public Canvas lockHardwareCanvas() {
1041 return internalLockCanvas(null, true);
1042 }
1043
1044 private Canvas internalLockCanvas(Rect dirty, boolean hardware) {
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07001045 mSurfaceLock.lock();
1046
John Reckaa6e84f2016-06-16 15:36:13 -07001047 if (DEBUG) Log.i(TAG, System.identityHashCode(this) + " " + "Locking canvas... stopped="
Robert Carrd5c7dd62017-03-08 10:39:30 -08001048 + mDrawingStopped + ", surfaceControl=" + mSurfaceControl);
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07001049
1050 Canvas c = null;
Robert Carrd5c7dd62017-03-08 10:39:30 -08001051 if (!mDrawingStopped && mSurfaceControl != null) {
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07001052 try {
John Reck6bc70142016-10-26 16:49:17 -07001053 if (hardware) {
1054 c = mSurface.lockHardwareCanvas();
1055 } else {
1056 c = mSurface.lockCanvas(dirty);
1057 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07001058 } catch (Exception e) {
1059 Log.e(LOG_TAG, "Exception locking surface", e);
1060 }
1061 }
1062
John Reckaa6e84f2016-06-16 15:36:13 -07001063 if (DEBUG) Log.i(TAG, System.identityHashCode(this) + " " + "Returned canvas: " + c);
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07001064 if (c != null) {
1065 mLastLockTime = SystemClock.uptimeMillis();
1066 return c;
1067 }
Igor Murashkina86ab6402013-08-30 12:58:36 -07001068
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07001069 // 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();
Igor Murashkina86ab6402013-08-30 12:58:36 -07001083
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07001084 return null;
1085 }
1086
Mathias Agopian9ddf32a2013-04-17 15:04:47 -07001087 /**
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 */
Igor Murashkina86ab6402013-08-30 12:58:36 -07001093 @Override
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07001094 public void unlockCanvasAndPost(Canvas canvas) {
1095 mSurface.unlockCanvasAndPost(canvas);
1096 mSurfaceLock.unlock();
1097 }
1098
Igor Murashkina86ab6402013-08-30 12:58:36 -07001099 @Override
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07001100 public Surface getSurface() {
1101 return mSurface;
1102 }
1103
Igor Murashkina86ab6402013-08-30 12:58:36 -07001104 @Override
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07001105 public Rect getSurfaceFrame() {
1106 return mSurfaceFrame;
1107 }
1108 };
Robert Carr55235552017-06-02 14:21:03 -07001109
1110 class SurfaceControlWithBackground extends SurfaceControl {
Robert Carr386fd702018-03-23 13:46:39 -07001111 SurfaceControl mBackgroundControl;
Robert Carr55235552017-06-02 14:21:03 -07001112 private boolean mOpaque = true;
1113 public boolean mVisible = false;
1114
Robert Carre625fcf2017-09-01 12:36:28 -07001115 public SurfaceControlWithBackground(String name, boolean opaque, SurfaceControl.Builder b)
Robert Carr55235552017-06-02 14:21:03 -07001116 throws Exception {
Robert Carre625fcf2017-09-01 12:36:28 -07001117 super(b.setName(name).build());
1118
1119 mBackgroundControl = b.setName("Background for -" + name)
1120 .setFormat(OPAQUE)
1121 .setColorLayer(true)
1122 .build();
1123 mOpaque = opaque;
Robert Carr55235552017-06-02 14:21:03 -07001124 }
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
Robert Carr552da0e2017-06-12 11:43:51 -07001225 @Override
1226 public void deferTransactionUntil(Surface barrier, long frame) {
1227 super.deferTransactionUntil(barrier, frame);
1228 mBackgroundControl.deferTransactionUntil(barrier, frame);
1229 }
1230
Andrii Kuliancf8f6832018-01-23 19:43:30 -08001231 /** 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
Robert Carr55235552017-06-02 14:21:03 -07001244 void updateBackgroundVisibility() {
1245 if (mOpaque && mVisible) {
1246 mBackgroundControl.show();
1247 } else {
1248 mBackgroundControl.hide();
1249 }
1250 }
1251 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07001252}