blob: db34856e0ad6cde868d152b74e7363d1535bed07 [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 Carr29daa922018-04-27 11:56:48 -0700703
704 mSurfaceControl.destroy();
705 mSurfaceControl = null;
Robert Carrd5c7dd62017-03-08 10:39:30 -0800706 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700707 }
Robert Carrd5c7dd62017-03-08 10:39:30 -0800708 } catch (Exception ex) {
Robert Carr44ab5752017-03-20 21:47:11 -0700709 Log.e(TAG, "Exception configuring surface", ex);
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700710 }
Dianne Hackborn6d05fd32011-11-19 14:36:15 -0800711 if (DEBUG) Log.v(
Robert Carrd5c7dd62017-03-08 10:39:30 -0800712 TAG, "Layout: x=" + mScreenRect.left + " y=" + mScreenRect.top
713 + " w=" + mScreenRect.width() + " h=" + mScreenRect.height()
714 + ", frame=" + mSurfaceFrame);
John Reckf23a1b82016-06-22 14:23:31 -0700715 } else {
716 // Calculate the window position in case RT loses the window
717 // and we need to fallback to a UI-thread driven position update
Robert Carrd5c7dd62017-03-08 10:39:30 -0800718 getLocationInSurface(mLocation);
John Reckf6481082016-02-02 15:18:23 -0800719 final boolean positionChanged = mWindowSpaceLeft != mLocation[0]
720 || mWindowSpaceTop != mLocation[1];
Robert Carrd5c7dd62017-03-08 10:39:30 -0800721 final boolean layoutSizeChanged = getWidth() != mScreenRect.width()
722 || getHeight() != mScreenRect.height();
John Reckf6481082016-02-02 15:18:23 -0800723 if (positionChanged || layoutSizeChanged) { // Only the position has changed
724 mWindowSpaceLeft = mLocation[0];
725 mWindowSpaceTop = mLocation[1];
Robert Carrd5c7dd62017-03-08 10:39:30 -0800726 // For our size changed check, we keep mScreenRect.width() and mScreenRect.height()
John Reckf6481082016-02-02 15:18:23 -0800727 // in view local space.
Robert Carrd5c7dd62017-03-08 10:39:30 -0800728 mLocation[0] = getWidth();
729 mLocation[1] = getHeight();
Robert Carr64aadd02015-11-06 13:54:20 -0800730
Robert Carrd5c7dd62017-03-08 10:39:30 -0800731 mScreenRect.set(mWindowSpaceLeft, mWindowSpaceTop,
Robert Carrc90e5f82017-07-18 13:10:02 -0700732 mWindowSpaceLeft + mLocation[0], mWindowSpaceTop + mLocation[1]);
John Reckf057fb52016-04-15 13:46:29 -0700733
734 if (mTranslator != null) {
Robert Carrd5c7dd62017-03-08 10:39:30 -0800735 mTranslator.translateRectInAppWindowToScreen(mScreenRect);
John Reckf057fb52016-04-15 13:46:29 -0700736 }
737
Robert Carr3651ab82017-04-25 12:05:34 -0700738 if (mSurfaceControl == null) {
739 return;
740 }
741
Bryce Lee16e50892017-04-11 01:59:37 +0000742 if (!isHardwareAccelerated() || !mRtHandlingPositionUpdates) {
John Reckf23a1b82016-06-22 14:23:31 -0700743 try {
Robert Carrd5c7dd62017-03-08 10:39:30 -0800744 if (DEBUG) Log.d(TAG, String.format("%d updateSurfacePosition UI, " +
John Reckf23a1b82016-06-22 14:23:31 -0700745 "postion = [%d, %d, %d, %d]", System.identityHashCode(this),
Robert Carrd5c7dd62017-03-08 10:39:30 -0800746 mScreenRect.left, mScreenRect.top,
747 mScreenRect.right, mScreenRect.bottom));
748 setParentSpaceRectangle(mScreenRect, -1);
749 } catch (Exception ex) {
Robert Carr44ab5752017-03-20 21:47:11 -0700750 Log.e(TAG, "Exception configuring surface", ex);
John Reckf23a1b82016-06-22 14:23:31 -0700751 }
John Reckf6481082016-02-02 15:18:23 -0800752 }
Rob Carr64e516f2015-10-29 00:20:45 +0000753 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700754 }
755 }
756
Robert Carrd5c7dd62017-03-08 10:39:30 -0800757 private void onDrawFinished() {
758 if (DEBUG) {
759 Log.i(TAG, System.identityHashCode(this) + " "
760 + "finishedDrawing");
761 }
Robert Carr3bc95b52017-03-20 21:57:23 -0700762
763 if (mDeferredDestroySurfaceControl != null) {
764 mDeferredDestroySurfaceControl.destroy();
765 mDeferredDestroySurfaceControl = null;
766 }
767
John Reck79925002017-05-26 13:57:14 -0700768 runOnUiThread(() -> {
Robert Carrb53670a2017-05-25 18:20:49 -0700769 performDrawFinished();
John Reck79925002017-05-26 13:57:14 -0700770 });
Robert Carrd5c7dd62017-03-08 10:39:30 -0800771 }
772
Robert Carr27a800a2018-03-16 13:33:45 -0700773 /**
774 * A place to over-ride for applying child-surface transactions.
775 * These can be synchronized with the viewroot surface using deferTransaction.
776 *
777 * Called from RenderWorker while UI thread is paused.
778 * @hide
779 */
780 protected void applyChildSurfaceTransaction_renderWorker(SurfaceControl.Transaction t,
781 Surface viewRootSurface, long nextViewRootFrameNumber) {
782 }
783
Robert Carr386fd702018-03-23 13:46:39 -0700784 private void applySurfaceTransforms(SurfaceControl surface, Rect position, long frameNumber) {
Robert Carr27a800a2018-03-16 13:33:45 -0700785 if (frameNumber > 0) {
Robert Carr386fd702018-03-23 13:46:39 -0700786 final ViewRootImpl viewRoot = getViewRootImpl();
787
788 mRtTransaction.deferTransactionUntilSurface(surface, viewRoot.mSurface,
Robert Carr27a800a2018-03-16 13:33:45 -0700789 frameNumber);
Robert Carrd5c7dd62017-03-08 10:39:30 -0800790 }
Robert Carr386fd702018-03-23 13:46:39 -0700791
792 mRtTransaction.setPosition(surface, position.left, position.top);
793 mRtTransaction.setMatrix(surface,
Robert Carr27a800a2018-03-16 13:33:45 -0700794 position.width() / (float) mSurfaceWidth,
795 0.0f, 0.0f,
796 position.height() / (float) mSurfaceHeight);
Robert Carr386fd702018-03-23 13:46:39 -0700797 }
798
799 private void setParentSpaceRectangle(Rect position, long frameNumber) {
800 final ViewRootImpl viewRoot = getViewRootImpl();
801
802 applySurfaceTransforms(mSurfaceControl, position, frameNumber);
803 applySurfaceTransforms(mSurfaceControl.mBackgroundControl, position, frameNumber);
Robert Carr27a800a2018-03-16 13:33:45 -0700804
805 applyChildSurfaceTransaction_renderWorker(mRtTransaction, viewRoot.mSurface,
806 frameNumber);
807
808 mRtTransaction.apply();
Robert Carrd5c7dd62017-03-08 10:39:30 -0800809 }
810
John Reckf6481082016-02-02 15:18:23 -0800811 private Rect mRTLastReportedPosition = new Rect();
812
813 /**
Robert Carr33879132016-09-06 14:41:40 -0700814 * Called by native by a Rendering Worker thread to update the window position
John Reckf6481082016-02-02 15:18:23 -0800815 * @hide
816 */
Robert Carrd5c7dd62017-03-08 10:39:30 -0800817 public final void updateSurfacePosition_renderWorker(long frameNumber,
John Reckf6481082016-02-02 15:18:23 -0800818 int left, int top, int right, int bottom) {
Robert Carrd5c7dd62017-03-08 10:39:30 -0800819 if (mSurfaceControl == null) {
John Reckf6481082016-02-02 15:18:23 -0800820 return;
821 }
Robert Carrd5c7dd62017-03-08 10:39:30 -0800822
John Reckf23a1b82016-06-22 14:23:31 -0700823 // TODO: This is teensy bit racey in that a brand new SurfaceView moving on
824 // its 2nd frame if RenderThread is running slowly could potentially see
825 // this as false, enter the branch, get pre-empted, then this comes along
826 // and reports a new position, then the UI thread resumes and reports
827 // its position. This could therefore be de-sync'd in that interval, but
828 // the synchronization would violate the rule that RT must never block
829 // on the UI thread which would open up potential deadlocks. The risk of
830 // a single-frame desync is therefore preferable for now.
831 mRtHandlingPositionUpdates = true;
John Reckf6481082016-02-02 15:18:23 -0800832 if (mRTLastReportedPosition.left == left
833 && mRTLastReportedPosition.top == top
834 && mRTLastReportedPosition.right == right
835 && mRTLastReportedPosition.bottom == bottom) {
836 return;
837 }
838 try {
839 if (DEBUG) {
Robert Carrd5c7dd62017-03-08 10:39:30 -0800840 Log.d(TAG, String.format("%d updateSurfacePosition RenderWorker, frameNr = %d, " +
John Reckaa6e84f2016-06-16 15:36:13 -0700841 "postion = [%d, %d, %d, %d]", System.identityHashCode(this),
842 frameNumber, left, top, right, bottom));
John Reckf6481082016-02-02 15:18:23 -0800843 }
Wonsik Kim5aec7b92017-03-07 17:15:50 -0800844 mRTLastReportedPosition.set(left, top, right, bottom);
Robert Carrd5c7dd62017-03-08 10:39:30 -0800845 setParentSpaceRectangle(mRTLastReportedPosition, frameNumber);
846 // Now overwrite mRTLastReportedPosition with our values
847 } catch (Exception ex) {
John Reckf6481082016-02-02 15:18:23 -0800848 Log.e(TAG, "Exception from repositionChild", ex);
849 }
850 }
851
John Reckaa6e84f2016-06-16 15:36:13 -0700852 /**
Robert Carrd5c7dd62017-03-08 10:39:30 -0800853 * Called by native on RenderThread to notify that the view is no longer in the
Robert Carr33879132016-09-06 14:41:40 -0700854 * draw tree. UI thread is blocked at this point.
John Reckaa6e84f2016-06-16 15:36:13 -0700855 * @hide
856 */
Robert Carrd5c7dd62017-03-08 10:39:30 -0800857 public final void surfacePositionLost_uiRtSync(long frameNumber) {
John Reckaa6e84f2016-06-16 15:36:13 -0700858 if (DEBUG) {
Robert Carr33879132016-09-06 14:41:40 -0700859 Log.d(TAG, String.format("%d windowPositionLost, frameNr = %d",
John Reckaa6e84f2016-06-16 15:36:13 -0700860 System.identityHashCode(this), frameNumber));
861 }
Robert Carrad3a4932017-06-20 14:55:21 -0700862 mRTLastReportedPosition.setEmpty();
863
Robert Carrd5c7dd62017-03-08 10:39:30 -0800864 if (mSurfaceControl == null) {
John Reck474659c2016-06-27 07:56:37 -0700865 return;
866 }
John Reckf23a1b82016-06-22 14:23:31 -0700867 if (mRtHandlingPositionUpdates) {
868 mRtHandlingPositionUpdates = false;
869 // This callback will happen while the UI thread is blocked, so we can
870 // safely access other member variables at this time.
871 // So do what the UI thread would have done if RT wasn't handling position
872 // updates.
Robert Carrd5c7dd62017-03-08 10:39:30 -0800873 if (!mScreenRect.isEmpty() && !mScreenRect.equals(mRTLastReportedPosition)) {
John Reckf23a1b82016-06-22 14:23:31 -0700874 try {
Robert Carrd5c7dd62017-03-08 10:39:30 -0800875 if (DEBUG) Log.d(TAG, String.format("%d updateSurfacePosition, " +
John Reckf23a1b82016-06-22 14:23:31 -0700876 "postion = [%d, %d, %d, %d]", System.identityHashCode(this),
Robert Carrd5c7dd62017-03-08 10:39:30 -0800877 mScreenRect.left, mScreenRect.top,
878 mScreenRect.right, mScreenRect.bottom));
879 setParentSpaceRectangle(mScreenRect, frameNumber);
880 } catch (Exception ex) {
Robert Carr44ab5752017-03-20 21:47:11 -0700881 Log.e(TAG, "Exception configuring surface", ex);
John Reckf23a1b82016-06-22 14:23:31 -0700882 }
883 }
John Reckf23a1b82016-06-22 14:23:31 -0700884 }
John Reckaa6e84f2016-06-16 15:36:13 -0700885 }
886
Dianne Hackborn6d05fd32011-11-19 14:36:15 -0800887 private SurfaceHolder.Callback[] getSurfaceCallbacks() {
888 SurfaceHolder.Callback callbacks[];
889 synchronized (mCallbacks) {
890 callbacks = new SurfaceHolder.Callback[mCallbacks.size()];
891 mCallbacks.toArray(callbacks);
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700892 }
Dianne Hackborn6d05fd32011-11-19 14:36:15 -0800893 return callbacks;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700894 }
895
John Reck79925002017-05-26 13:57:14 -0700896 private void runOnUiThread(Runnable runnable) {
897 Handler handler = getHandler();
898 if (handler != null && handler.getLooper() != Looper.myLooper()) {
899 handler.post(runnable);
900 } else {
901 runnable.run();
902 }
903 }
904
Yohei Yukawa3b5011a2017-03-16 15:34:12 -0700905 /**
Derek Sollenberger7179b812010-03-22 13:41:20 -0400906 * Check to see if the surface has fixed size dimensions or if the surface's
907 * dimensions are dimensions are dependent on its current layout.
908 *
909 * @return true if the surface has dimensions that are fixed in size
910 * @hide
911 */
912 public boolean isFixedSize() {
913 return (mRequestedWidth != -1 || mRequestedHeight != -1);
914 }
915
Robert Carrd5c7dd62017-03-08 10:39:30 -0800916 private boolean isAboveParent() {
917 return mSubLayer >= 0;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700918 }
919
Andrii Kuliancf8f6832018-01-23 19:43:30 -0800920 /**
921 * Set an opaque background color to use with this {@link SurfaceView} when it's being resized
922 * and size of the content hasn't updated yet. This color will fill the expanded area when the
923 * view becomes larger.
924 * @param bgColor An opaque color to fill the background. Alpha component will be ignored.
925 * @hide
926 */
927 public void setResizeBackgroundColor(int bgColor) {
928 mSurfaceControl.setBackgroundColor(bgColor);
929 }
930
Igor Murashkina86ab6402013-08-30 12:58:36 -0700931 private final SurfaceHolder mSurfaceHolder = new SurfaceHolder() {
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700932 private static final String LOG_TAG = "SurfaceHolder";
Igor Murashkina86ab6402013-08-30 12:58:36 -0700933
934 @Override
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700935 public boolean isCreating() {
936 return mIsCreating;
937 }
938
Igor Murashkina86ab6402013-08-30 12:58:36 -0700939 @Override
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700940 public void addCallback(Callback callback) {
941 synchronized (mCallbacks) {
Igor Murashkina86ab6402013-08-30 12:58:36 -0700942 // This is a linear search, but in practice we'll
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700943 // have only a couple callbacks, so it doesn't matter.
Igor Murashkina86ab6402013-08-30 12:58:36 -0700944 if (mCallbacks.contains(callback) == false) {
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700945 mCallbacks.add(callback);
946 }
947 }
948 }
949
Igor Murashkina86ab6402013-08-30 12:58:36 -0700950 @Override
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700951 public void removeCallback(Callback callback) {
952 synchronized (mCallbacks) {
953 mCallbacks.remove(callback);
954 }
955 }
Igor Murashkina86ab6402013-08-30 12:58:36 -0700956
957 @Override
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700958 public void setFixedSize(int width, int height) {
959 if (mRequestedWidth != width || mRequestedHeight != height) {
960 mRequestedWidth = width;
961 mRequestedHeight = height;
962 requestLayout();
963 }
964 }
965
Igor Murashkina86ab6402013-08-30 12:58:36 -0700966 @Override
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700967 public void setSizeFromLayout() {
968 if (mRequestedWidth != -1 || mRequestedHeight != -1) {
969 mRequestedWidth = mRequestedHeight = -1;
970 requestLayout();
971 }
972 }
973
Igor Murashkina86ab6402013-08-30 12:58:36 -0700974 @Override
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700975 public void setFormat(int format) {
Mathias Agopian2d468c52010-06-14 21:50:48 -0700976 // for backward compatibility reason, OPAQUE always
977 // means 565 for SurfaceView
978 if (format == PixelFormat.OPAQUE)
979 format = PixelFormat.RGB_565;
980
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700981 mRequestedFormat = format;
Robert Carrd5c7dd62017-03-08 10:39:30 -0800982 if (mSurfaceControl != null) {
983 updateSurface();
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700984 }
985 }
986
Mathias Agopiand2112302010-12-07 19:38:17 -0800987 /**
988 * @deprecated setType is now ignored.
989 */
Igor Murashkina86ab6402013-08-30 12:58:36 -0700990 @Override
Mathias Agopiand2112302010-12-07 19:38:17 -0800991 @Deprecated
992 public void setType(int type) { }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700993
Igor Murashkina86ab6402013-08-30 12:58:36 -0700994 @Override
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700995 public void setKeepScreenOn(boolean screenOn) {
John Reck79925002017-05-26 13:57:14 -0700996 runOnUiThread(() -> SurfaceView.this.setKeepScreenOn(screenOn));
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700997 }
Igor Murashkina86ab6402013-08-30 12:58:36 -0700998
Mathias Agopian9ddf32a2013-04-17 15:04:47 -0700999 /**
1000 * Gets a {@link Canvas} for drawing into the SurfaceView's Surface
1001 *
1002 * After drawing into the provided {@link Canvas}, the caller must
1003 * invoke {@link #unlockCanvasAndPost} to post the new contents to the surface.
1004 *
1005 * The caller must redraw the entire surface.
1006 * @return A canvas for drawing into the surface.
1007 */
Igor Murashkina86ab6402013-08-30 12:58:36 -07001008 @Override
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07001009 public Canvas lockCanvas() {
John Reck6bc70142016-10-26 16:49:17 -07001010 return internalLockCanvas(null, false);
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07001011 }
1012
Mathias Agopian9ddf32a2013-04-17 15:04:47 -07001013 /**
1014 * Gets a {@link Canvas} for drawing into the SurfaceView's Surface
1015 *
1016 * After drawing into the provided {@link Canvas}, the caller must
1017 * invoke {@link #unlockCanvasAndPost} to post the new contents to the surface.
1018 *
1019 * @param inOutDirty A rectangle that represents the dirty region that the caller wants
1020 * to redraw. This function may choose to expand the dirty rectangle if for example
1021 * the surface has been resized or if the previous contents of the surface were
1022 * not available. The caller must redraw the entire dirty region as represented
1023 * by the contents of the inOutDirty rectangle upon return from this function.
1024 * The caller may also pass <code>null</code> instead, in the case where the
1025 * entire surface should be redrawn.
1026 * @return A canvas for drawing into the surface.
1027 */
Igor Murashkina86ab6402013-08-30 12:58:36 -07001028 @Override
Mathias Agopian9ddf32a2013-04-17 15:04:47 -07001029 public Canvas lockCanvas(Rect inOutDirty) {
John Reck6bc70142016-10-26 16:49:17 -07001030 return internalLockCanvas(inOutDirty, false);
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07001031 }
1032
John Reck6bc70142016-10-26 16:49:17 -07001033 @Override
1034 public Canvas lockHardwareCanvas() {
1035 return internalLockCanvas(null, true);
1036 }
1037
1038 private Canvas internalLockCanvas(Rect dirty, boolean hardware) {
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07001039 mSurfaceLock.lock();
1040
John Reckaa6e84f2016-06-16 15:36:13 -07001041 if (DEBUG) Log.i(TAG, System.identityHashCode(this) + " " + "Locking canvas... stopped="
Robert Carrd5c7dd62017-03-08 10:39:30 -08001042 + mDrawingStopped + ", surfaceControl=" + mSurfaceControl);
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07001043
1044 Canvas c = null;
Robert Carrd5c7dd62017-03-08 10:39:30 -08001045 if (!mDrawingStopped && mSurfaceControl != null) {
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07001046 try {
John Reck6bc70142016-10-26 16:49:17 -07001047 if (hardware) {
1048 c = mSurface.lockHardwareCanvas();
1049 } else {
1050 c = mSurface.lockCanvas(dirty);
1051 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07001052 } catch (Exception e) {
1053 Log.e(LOG_TAG, "Exception locking surface", e);
1054 }
1055 }
1056
John Reckaa6e84f2016-06-16 15:36:13 -07001057 if (DEBUG) Log.i(TAG, System.identityHashCode(this) + " " + "Returned canvas: " + c);
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07001058 if (c != null) {
1059 mLastLockTime = SystemClock.uptimeMillis();
1060 return c;
1061 }
Igor Murashkina86ab6402013-08-30 12:58:36 -07001062
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07001063 // If the Surface is not ready to be drawn, then return null,
1064 // but throttle calls to this function so it isn't called more
1065 // than every 100ms.
1066 long now = SystemClock.uptimeMillis();
1067 long nextTime = mLastLockTime + 100;
1068 if (nextTime > now) {
1069 try {
1070 Thread.sleep(nextTime-now);
1071 } catch (InterruptedException e) {
1072 }
1073 now = SystemClock.uptimeMillis();
1074 }
1075 mLastLockTime = now;
1076 mSurfaceLock.unlock();
Igor Murashkina86ab6402013-08-30 12:58:36 -07001077
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07001078 return null;
1079 }
1080
Mathias Agopian9ddf32a2013-04-17 15:04:47 -07001081 /**
1082 * Posts the new contents of the {@link Canvas} to the surface and
1083 * releases the {@link Canvas}.
1084 *
1085 * @param canvas The canvas previously obtained from {@link #lockCanvas}.
1086 */
Igor Murashkina86ab6402013-08-30 12:58:36 -07001087 @Override
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07001088 public void unlockCanvasAndPost(Canvas canvas) {
1089 mSurface.unlockCanvasAndPost(canvas);
1090 mSurfaceLock.unlock();
1091 }
1092
Igor Murashkina86ab6402013-08-30 12:58:36 -07001093 @Override
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07001094 public Surface getSurface() {
1095 return mSurface;
1096 }
1097
Igor Murashkina86ab6402013-08-30 12:58:36 -07001098 @Override
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07001099 public Rect getSurfaceFrame() {
1100 return mSurfaceFrame;
1101 }
1102 };
Robert Carr55235552017-06-02 14:21:03 -07001103
1104 class SurfaceControlWithBackground extends SurfaceControl {
Robert Carr386fd702018-03-23 13:46:39 -07001105 SurfaceControl mBackgroundControl;
Robert Carr55235552017-06-02 14:21:03 -07001106 private boolean mOpaque = true;
1107 public boolean mVisible = false;
1108
Robert Carre625fcf2017-09-01 12:36:28 -07001109 public SurfaceControlWithBackground(String name, boolean opaque, SurfaceControl.Builder b)
Robert Carr55235552017-06-02 14:21:03 -07001110 throws Exception {
Robert Carre625fcf2017-09-01 12:36:28 -07001111 super(b.setName(name).build());
1112
1113 mBackgroundControl = b.setName("Background for -" + name)
1114 .setFormat(OPAQUE)
1115 .setColorLayer(true)
1116 .build();
1117 mOpaque = opaque;
Robert Carr55235552017-06-02 14:21:03 -07001118 }
1119
1120 @Override
1121 public void setAlpha(float alpha) {
1122 super.setAlpha(alpha);
1123 mBackgroundControl.setAlpha(alpha);
1124 }
1125
1126 @Override
1127 public void setLayer(int zorder) {
1128 super.setLayer(zorder);
1129 // -3 is below all other child layers as SurfaceView never goes below -2
1130 mBackgroundControl.setLayer(-3);
1131 }
1132
1133 @Override
1134 public void setPosition(float x, float y) {
1135 super.setPosition(x, y);
1136 mBackgroundControl.setPosition(x, y);
1137 }
1138
1139 @Override
1140 public void setSize(int w, int h) {
1141 super.setSize(w, h);
1142 mBackgroundControl.setSize(w, h);
1143 }
1144
1145 @Override
1146 public void setWindowCrop(Rect crop) {
1147 super.setWindowCrop(crop);
1148 mBackgroundControl.setWindowCrop(crop);
1149 }
1150
1151 @Override
1152 public void setFinalCrop(Rect crop) {
1153 super.setFinalCrop(crop);
1154 mBackgroundControl.setFinalCrop(crop);
1155 }
1156
1157 @Override
1158 public void setLayerStack(int layerStack) {
1159 super.setLayerStack(layerStack);
1160 mBackgroundControl.setLayerStack(layerStack);
1161 }
1162
1163 @Override
1164 public void setOpaque(boolean isOpaque) {
1165 super.setOpaque(isOpaque);
1166 mOpaque = isOpaque;
1167 updateBackgroundVisibility();
1168 }
1169
1170 @Override
1171 public void setSecure(boolean isSecure) {
1172 super.setSecure(isSecure);
1173 }
1174
1175 @Override
1176 public void setMatrix(float dsdx, float dtdx, float dsdy, float dtdy) {
1177 super.setMatrix(dsdx, dtdx, dsdy, dtdy);
1178 mBackgroundControl.setMatrix(dsdx, dtdx, dsdy, dtdy);
1179 }
1180
1181 @Override
1182 public void hide() {
1183 super.hide();
1184 mVisible = false;
1185 updateBackgroundVisibility();
1186 }
1187
1188 @Override
1189 public void show() {
1190 super.show();
1191 mVisible = true;
1192 updateBackgroundVisibility();
1193 }
1194
1195 @Override
1196 public void destroy() {
1197 super.destroy();
1198 mBackgroundControl.destroy();
1199 }
1200
1201 @Override
1202 public void release() {
1203 super.release();
1204 mBackgroundControl.release();
1205 }
1206
1207 @Override
1208 public void setTransparentRegionHint(Region region) {
1209 super.setTransparentRegionHint(region);
1210 mBackgroundControl.setTransparentRegionHint(region);
1211 }
1212
1213 @Override
1214 public void deferTransactionUntil(IBinder handle, long frame) {
1215 super.deferTransactionUntil(handle, frame);
1216 mBackgroundControl.deferTransactionUntil(handle, frame);
1217 }
1218
Robert Carr552da0e2017-06-12 11:43:51 -07001219 @Override
1220 public void deferTransactionUntil(Surface barrier, long frame) {
1221 super.deferTransactionUntil(barrier, frame);
1222 mBackgroundControl.deferTransactionUntil(barrier, frame);
1223 }
1224
Andrii Kuliancf8f6832018-01-23 19:43:30 -08001225 /** Set the color to fill the background with. */
1226 private void setBackgroundColor(int bgColor) {
1227 final float[] colorComponents = new float[] { Color.red(bgColor) / 255.f,
1228 Color.green(bgColor) / 255.f, Color.blue(bgColor) / 255.f };
1229
1230 SurfaceControl.openTransaction();
1231 try {
1232 mBackgroundControl.setColor(colorComponents);
1233 } finally {
1234 SurfaceControl.closeTransaction();
1235 }
1236 }
1237
Robert Carr55235552017-06-02 14:21:03 -07001238 void updateBackgroundVisibility() {
1239 if (mOpaque && mVisible) {
1240 mBackgroundControl.show();
1241 } else {
1242 mBackgroundControl.hide();
1243 }
1244 }
1245 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07001246}