blob: 4a9da4a6672f0fb4d85ca9c412d5e844184841c4 [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
Yohei Yukawa9309c192017-09-09 17:45:27 -070019import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION_PANEL;
Adrian Roose99bc052017-11-20 17:55:31 +010020import static android.view.WindowManagerPolicyConstants.APPLICATION_MEDIA_OVERLAY_SUBLAYER;
21import static android.view.WindowManagerPolicyConstants.APPLICATION_MEDIA_SUBLAYER;
22import static android.view.WindowManagerPolicyConstants.APPLICATION_PANEL_SUBLAYER;
Robert Carrd5c7dd62017-03-08 10:39:30 -080023
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070024import android.content.Context;
Mitsuru Oshima64f59342009-06-21 00:03:11 -070025import android.content.res.CompatibilityInfo.Translator;
Aurimas Liutikas67e2ae82016-10-11 18:17:42 -070026import android.content.res.Configuration;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070027import android.graphics.Canvas;
Andrii Kuliancf8f6832018-01-23 19:43:30 -080028import android.graphics.Color;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070029import android.graphics.PixelFormat;
30import android.graphics.PorterDuff;
31import android.graphics.Rect;
32import android.graphics.Region;
Yohei Yukawa3b5011a2017-03-16 15:34:12 -070033import android.os.Build;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070034import android.os.Handler;
Robert Carr55235552017-06-02 14:21:03 -070035import android.os.IBinder;
John Reck79925002017-05-26 13:57:14 -070036import android.os.Looper;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070037import android.os.SystemClock;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070038import android.util.AttributeSet;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070039import android.util.Log;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070040
Robert Carr25cfa132016-11-16 13:24:09 -080041import com.android.internal.view.SurfaceCallbackHelper;
Aurimas Liutikas67e2ae82016-10-11 18:17:42 -070042
Jon Larimer9bdf5762009-01-02 18:55:15 -050043import java.util.ArrayList;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070044import java.util.concurrent.locks.ReentrantLock;
45
46/**
47 * Provides a dedicated drawing surface embedded inside of a view hierarchy.
48 * You can control the format of this surface and, if you like, its size; the
49 * SurfaceView takes care of placing the surface at the correct location on the
50 * screen
Igor Murashkina86ab6402013-08-30 12:58:36 -070051 *
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070052 * <p>The surface is Z ordered so that it is behind the window holding its
53 * SurfaceView; the SurfaceView punches a hole in its window to allow its
Jesse Hallc9f345f2012-09-26 11:55:16 -070054 * surface to be displayed. The view hierarchy will take care of correctly
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070055 * compositing with the Surface any siblings of the SurfaceView that would
Jesse Hallc9f345f2012-09-26 11:55:16 -070056 * 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 -070057 * buttons on top of the Surface, though note however that it can have an
58 * impact on performance since a full alpha-blended composite will be performed
59 * each time the Surface changes.
Igor Murashkina86ab6402013-08-30 12:58:36 -070060 *
Jesse Hallc9f345f2012-09-26 11:55:16 -070061 * <p> The transparent region that makes the surface visible is based on the
62 * layout positions in the view hierarchy. If the post-layout transform
63 * properties are used to draw a sibling view on top of the SurfaceView, the
64 * view may not be properly composited with the surface.
65 *
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070066 * <p>Access to the underlying surface is provided via the SurfaceHolder interface,
67 * which can be retrieved by calling {@link #getHolder}.
Igor Murashkina86ab6402013-08-30 12:58:36 -070068 *
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070069 * <p>The Surface will be created for you while the SurfaceView's window is
70 * visible; you should implement {@link SurfaceHolder.Callback#surfaceCreated}
71 * and {@link SurfaceHolder.Callback#surfaceDestroyed} to discover when the
72 * Surface is created and destroyed as the window is shown and hidden.
Igor Murashkina86ab6402013-08-30 12:58:36 -070073 *
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070074 * <p>One of the purposes of this class is to provide a surface in which a
Jesse Hallc9f345f2012-09-26 11:55:16 -070075 * secondary thread can render into the screen. If you are going to use it
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070076 * this way, you need to be aware of some threading semantics:
Igor Murashkina86ab6402013-08-30 12:58:36 -070077 *
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070078 * <ul>
79 * <li> All SurfaceView and
80 * {@link SurfaceHolder.Callback SurfaceHolder.Callback} methods will be called
81 * from the thread running the SurfaceView's window (typically the main thread
Jesse Hallc9f345f2012-09-26 11:55:16 -070082 * of the application). They thus need to correctly synchronize with any
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070083 * state that is also touched by the drawing thread.
84 * <li> You must ensure that the drawing thread only touches the underlying
85 * Surface while it is valid -- between
86 * {@link SurfaceHolder.Callback#surfaceCreated SurfaceHolder.Callback.surfaceCreated()}
87 * and
88 * {@link SurfaceHolder.Callback#surfaceDestroyed SurfaceHolder.Callback.surfaceDestroyed()}.
89 * </ul>
Chris Craik6ee192f2016-05-17 14:29:10 -070090 *
91 * <p class="note"><strong>Note:</strong> Starting in platform version
92 * {@link android.os.Build.VERSION_CODES#N}, SurfaceView's window position is
93 * updated synchronously with other View rendering. This means that translating
94 * and scaling a SurfaceView on screen will not cause rendering artifacts. Such
95 * artifacts may occur on previous versions of the platform when its window is
96 * positioned asynchronously.</p>
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070097 */
Robert Carr414ebc62017-04-12 12:01:00 -070098public class SurfaceView extends View implements ViewRootImpl.WindowStoppedCallback {
Robert Carrd5c7dd62017-03-08 10:39:30 -080099 private static final String TAG = "SurfaceView";
100 private static final boolean DEBUG = false;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700101
102 final ArrayList<SurfaceHolder.Callback> mCallbacks
103 = new ArrayList<SurfaceHolder.Callback>();
104
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800105 final int[] mLocation = new int[2];
Igor Murashkina86ab6402013-08-30 12:58:36 -0700106
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700107 final ReentrantLock mSurfaceLock = new ReentrantLock();
Dianne Hackborn61566cc2011-12-02 23:31:52 -0800108 final Surface mSurface = new Surface(); // Current surface in use
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700109 boolean mDrawingStopped = true;
Robert Carrd5c7dd62017-03-08 10:39:30 -0800110 // We use this to track if the application has produced a frame
111 // in to the Surface. Up until that point, we should be careful not to punch
112 // holes.
113 boolean mDrawFinished = false;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700114
Robert Carrd5c7dd62017-03-08 10:39:30 -0800115 final Rect mScreenRect = new Rect();
116 SurfaceSession mSurfaceSession;
117
Andrii Kuliancf8f6832018-01-23 19:43:30 -0800118 SurfaceControlWithBackground mSurfaceControl;
Robert Carr3bc95b52017-03-20 21:57:23 -0700119 // In the case of format changes we switch out the surface in-place
120 // we need to preserve the old one until the new one has drawn.
121 SurfaceControl mDeferredDestroySurfaceControl;
Robert Carr33879132016-09-06 14:41:40 -0700122 final Rect mTmpRect = new Rect();
Dianne Hackborn694f79b2010-03-17 19:44:59 -0700123 final Configuration mConfiguration = new Configuration();
Igor Murashkina86ab6402013-08-30 12:58:36 -0700124
Robert Carrd5c7dd62017-03-08 10:39:30 -0800125 int mSubLayer = APPLICATION_MEDIA_SUBLAYER;
Igor Murashkina86ab6402013-08-30 12:58:36 -0700126
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700127 boolean mIsCreating = false;
John Reckf23a1b82016-06-22 14:23:31 -0700128 private volatile boolean mRtHandlingPositionUpdates = false;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700129
John Reckf6481082016-02-02 15:18:23 -0800130 private final ViewTreeObserver.OnScrollChangedListener mScrollChangedListener
Dianne Hackborne2af5c82010-03-18 15:44:34 -0700131 = new ViewTreeObserver.OnScrollChangedListener() {
Igor Murashkina86ab6402013-08-30 12:58:36 -0700132 @Override
Dianne Hackborne2af5c82010-03-18 15:44:34 -0700133 public void onScrollChanged() {
Robert Carrd5c7dd62017-03-08 10:39:30 -0800134 updateSurface();
Dianne Hackborne2af5c82010-03-18 15:44:34 -0700135 }
136 };
Igor Murashkina86ab6402013-08-30 12:58:36 -0700137
John Reckf6481082016-02-02 15:18:23 -0800138 private final ViewTreeObserver.OnPreDrawListener mDrawListener =
139 new ViewTreeObserver.OnPreDrawListener() {
140 @Override
141 public boolean onPreDraw() {
142 // reposition ourselves where the surface is
143 mHaveFrame = getWidth() > 0 && getHeight() > 0;
Robert Carrd5c7dd62017-03-08 10:39:30 -0800144 updateSurface();
John Reckf6481082016-02-02 15:18:23 -0800145 return true;
146 }
147 };
148
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700149 boolean mRequestedVisible = false;
Mathias Agopian6b7f1a62009-09-09 18:32:34 -0700150 boolean mWindowVisibility = false;
Robert Carrd5c7dd62017-03-08 10:39:30 -0800151 boolean mLastWindowVisibility = false;
Mathias Agopian6b7f1a62009-09-09 18:32:34 -0700152 boolean mViewVisibility = false;
Robert Carr414ebc62017-04-12 12:01:00 -0700153 boolean mWindowStopped = false;
154
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700155 int mRequestedWidth = -1;
156 int mRequestedHeight = -1;
Mathias Agopiand6ddcb72010-05-24 19:00:08 -0700157 /* Set SurfaceView's format to 565 by default to maintain backward
158 * compatibility with applications assuming this format.
159 */
160 int mRequestedFormat = PixelFormat.RGB_565;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700161
162 boolean mHaveFrame = false;
Dianne Hackborn6d05fd32011-11-19 14:36:15 -0800163 boolean mSurfaceCreated = false;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700164 long mLastLockTime = 0;
Igor Murashkina86ab6402013-08-30 12:58:36 -0700165
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700166 boolean mVisible = false;
Robert Carr64aadd02015-11-06 13:54:20 -0800167 int mWindowSpaceLeft = -1;
168 int mWindowSpaceTop = -1;
Robert Carrd5c7dd62017-03-08 10:39:30 -0800169 int mSurfaceWidth = -1;
170 int mSurfaceHeight = -1;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700171 int mFormat = -1;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700172 final Rect mSurfaceFrame = new Rect();
Dianne Hackborn726426e2010-03-31 22:04:36 -0700173 int mLastSurfaceWidth = -1, mLastSurfaceHeight = -1;
Mitsuru Oshima64f59342009-06-21 00:03:11 -0700174 private Translator mTranslator;
Romain Guyf2499fa2011-01-26 18:31:23 -0800175
Romain Guy01d5edc2011-01-28 11:28:53 -0800176 private boolean mGlobalListenersAdded;
Robert Carr8508bb22017-03-27 15:46:27 -0700177 private boolean mAttachedToWindow;
Romain Guyf2499fa2011-01-26 18:31:23 -0800178
Robert Carrd5c7dd62017-03-08 10:39:30 -0800179 private int mSurfaceFlags = SurfaceControl.HIDDEN;
180
Robert Carr8508bb22017-03-27 15:46:27 -0700181 private int mPendingReportDraws;
182
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700183 public SurfaceView(Context context) {
Alan Viverette768ca7d2016-08-04 09:54:14 -0400184 this(context, null);
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700185 }
Igor Murashkina86ab6402013-08-30 12:58:36 -0700186
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700187 public SurfaceView(Context context, AttributeSet attrs) {
Alan Viverette768ca7d2016-08-04 09:54:14 -0400188 this(context, attrs, 0);
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700189 }
190
Alan Viverette617feb92013-09-09 18:09:13 -0700191 public SurfaceView(Context context, AttributeSet attrs, int defStyleAttr) {
Alan Viverette768ca7d2016-08-04 09:54:14 -0400192 this(context, attrs, defStyleAttr, 0);
Alan Viverette617feb92013-09-09 18:09:13 -0700193 }
194
195 public SurfaceView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
196 super(context, attrs, defStyleAttr, defStyleRes);
John Reck3acf03822016-11-02 11:14:47 -0700197 mRenderNode.requestPositionUpdates(this);
Mathias Agopiand6ddcb72010-05-24 19:00:08 -0700198
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700199 setWillNotDraw(true);
200 }
Igor Murashkina86ab6402013-08-30 12:58:36 -0700201
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700202 /**
203 * Return the SurfaceHolder providing access and control over this
204 * SurfaceView's underlying surface.
Igor Murashkina86ab6402013-08-30 12:58:36 -0700205 *
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700206 * @return SurfaceHolder The holder of the surface.
207 */
208 public SurfaceHolder getHolder() {
209 return mSurfaceHolder;
210 }
211
Robert Carr414ebc62017-04-12 12:01:00 -0700212 private void updateRequestedVisibility() {
213 mRequestedVisible = mViewVisibility && mWindowVisibility && !mWindowStopped;
214 }
215
216 /** @hide */
217 @Override
218 public void windowStopped(boolean stopped) {
219 mWindowStopped = stopped;
220 updateRequestedVisibility();
221 updateSurface();
222 }
223
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700224 @Override
225 protected void onAttachedToWindow() {
226 super.onAttachedToWindow();
Robert Carr414ebc62017-04-12 12:01:00 -0700227
228 getViewRootImpl().addWindowStoppedCallback(this);
Robert Carr00177cc2017-05-15 15:49:17 -0700229 mWindowStopped = false;
Robert Carr414ebc62017-04-12 12:01:00 -0700230
Mathias Agopian6b7f1a62009-09-09 18:32:34 -0700231 mViewVisibility = getVisibility() == VISIBLE;
Robert Carr414ebc62017-04-12 12:01:00 -0700232 updateRequestedVisibility();
Romain Guy01d5edc2011-01-28 11:28:53 -0800233
Robert Carr8508bb22017-03-27 15:46:27 -0700234 mAttachedToWindow = true;
Karthik Ravi Shankara59c3a52017-09-11 17:36:25 -0700235 mParent.requestTransparentRegion(SurfaceView.this);
Romain Guy01d5edc2011-01-28 11:28:53 -0800236 if (!mGlobalListenersAdded) {
237 ViewTreeObserver observer = getViewTreeObserver();
238 observer.addOnScrollChangedListener(mScrollChangedListener);
239 observer.addOnPreDrawListener(mDrawListener);
240 mGlobalListenersAdded = true;
241 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700242 }
243
244 @Override
245 protected void onWindowVisibilityChanged(int visibility) {
246 super.onWindowVisibilityChanged(visibility);
Mathias Agopian6b7f1a62009-09-09 18:32:34 -0700247 mWindowVisibility = visibility == VISIBLE;
Robert Carr414ebc62017-04-12 12:01:00 -0700248 updateRequestedVisibility();
Robert Carrd5c7dd62017-03-08 10:39:30 -0800249 updateSurface();
Mathias Agopian6b7f1a62009-09-09 18:32:34 -0700250 }
251
252 @Override
253 public void setVisibility(int visibility) {
254 super.setVisibility(visibility);
255 mViewVisibility = visibility == VISIBLE;
Robert Carr414ebc62017-04-12 12:01:00 -0700256 boolean newRequestedVisible = mWindowVisibility && mViewVisibility && !mWindowStopped;
Mathias Agopiancbeb3322012-05-12 19:57:07 -0700257 if (newRequestedVisible != mRequestedVisible) {
258 // our base class (View) invalidates the layout only when
259 // we go from/to the GONE state. However, SurfaceView needs
260 // to request a re-layout when the visibility changes at all.
261 // This is needed because the transparent region is computed
262 // as part of the layout phase, and it changes (obviously) when
263 // the visibility changes.
264 requestLayout();
265 }
266 mRequestedVisible = newRequestedVisible;
Robert Carrd5c7dd62017-03-08 10:39:30 -0800267 updateSurface();
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700268 }
Romain Guyafc3e112010-06-07 17:04:33 -0700269
Robert Carrb53670a2017-05-25 18:20:49 -0700270 private void performDrawFinished() {
271 if (mPendingReportDraws > 0) {
272 mDrawFinished = true;
273 if (mAttachedToWindow) {
Robert Carrb53670a2017-05-25 18:20:49 -0700274 notifyDrawFinished();
275 invalidate();
276 }
277 } else {
278 Log.e(TAG, System.identityHashCode(this) + "finished drawing"
279 + " but no pending report draw (extra call"
280 + " to draw completion runnable?)");
281 }
282 }
283
Robert Carr8508bb22017-03-27 15:46:27 -0700284 void notifyDrawFinished() {
285 ViewRootImpl viewRoot = getViewRootImpl();
286 if (viewRoot != null) {
287 viewRoot.pendingDrawFinished();
288 }
289 mPendingReportDraws--;
290 }
291
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700292 @Override
John Reck77e4a522014-10-01 10:38:07 -0700293 protected void onDetachedFromWindow() {
Lucas Dupin0c207342017-04-14 19:47:23 -0700294 ViewRootImpl viewRoot = getViewRootImpl();
295 // It's possible to create a SurfaceView using the default constructor and never
296 // attach it to a view hierarchy, this is a common use case when dealing with
297 // OpenGL. A developer will probably create a new GLSurfaceView, and let it manage
298 // the lifecycle. Instead of attaching it to a view, he/she can just pass
299 // the SurfaceHolder forward, most live wallpapers do it.
300 if (viewRoot != null) {
301 viewRoot.removeWindowStoppedCallback(this);
302 }
Robert Carr414ebc62017-04-12 12:01:00 -0700303
Robert Carr8508bb22017-03-27 15:46:27 -0700304 mAttachedToWindow = false;
Romain Guy01d5edc2011-01-28 11:28:53 -0800305 if (mGlobalListenersAdded) {
306 ViewTreeObserver observer = getViewTreeObserver();
307 observer.removeOnScrollChangedListener(mScrollChangedListener);
308 observer.removeOnPreDrawListener(mDrawListener);
309 mGlobalListenersAdded = false;
310 }
311
Robert Carr8508bb22017-03-27 15:46:27 -0700312 while (mPendingReportDraws > 0) {
313 notifyDrawFinished();
314 }
315
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700316 mRequestedVisible = false;
Wonsik Kim5aec7b92017-03-07 17:15:50 -0800317
Robert Carrd5c7dd62017-03-08 10:39:30 -0800318 updateSurface();
319 if (mSurfaceControl != null) {
320 mSurfaceControl.destroy();
321 }
322 mSurfaceControl = null;
323
324 mHaveFrame = false;
Robert Carr414ebc62017-04-12 12:01:00 -0700325
John Reck77e4a522014-10-01 10:38:07 -0700326 super.onDetachedFromWindow();
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700327 }
328
329 @Override
330 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Dianne Hackborn189ee182010-12-02 21:48:53 -0800331 int width = mRequestedWidth >= 0
332 ? resolveSizeAndState(mRequestedWidth, widthMeasureSpec, 0)
333 : getDefaultSize(0, widthMeasureSpec);
334 int height = mRequestedHeight >= 0
335 ? resolveSizeAndState(mRequestedHeight, heightMeasureSpec, 0)
336 : getDefaultSize(0, heightMeasureSpec);
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700337 setMeasuredDimension(width, height);
338 }
Igor Murashkina86ab6402013-08-30 12:58:36 -0700339
Mathias Agopianef115302010-10-04 20:15:08 -0700340 /** @hide */
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700341 @Override
Mathias Agopian995bb9d2010-10-04 17:13:15 -0700342 protected boolean setFrame(int left, int top, int right, int bottom) {
343 boolean result = super.setFrame(left, top, right, bottom);
Robert Carrd5c7dd62017-03-08 10:39:30 -0800344 updateSurface();
Mathias Agopian995bb9d2010-10-04 17:13:15 -0700345 return result;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700346 }
347
348 @Override
349 public boolean gatherTransparentRegion(Region region) {
Robert Carr3ca12be72017-05-22 14:57:48 -0700350 if (isAboveParent() || !mDrawFinished) {
Dianne Hackborn1cd403e2009-09-14 22:29:14 -0700351 return super.gatherTransparentRegion(region);
352 }
Igor Murashkina86ab6402013-08-30 12:58:36 -0700353
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700354 boolean opaque = true;
Dianne Hackborn4702a852012-08-17 15:18:29 -0700355 if ((mPrivateFlags & PFLAG_SKIP_DRAW) == 0) {
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700356 // this view draws, remove it from the transparent region
357 opaque = super.gatherTransparentRegion(region);
358 } else if (region != null) {
359 int w = getWidth();
360 int h = getHeight();
361 if (w>0 && h>0) {
362 getLocationInWindow(mLocation);
363 // otherwise, punch a hole in the whole hierarchy
364 int l = mLocation[0];
365 int t = mLocation[1];
366 region.op(l, t, l+w, t+h, Region.Op.UNION);
367 }
368 }
369 if (PixelFormat.formatHasAlpha(mRequestedFormat)) {
370 opaque = false;
371 }
372 return opaque;
373 }
374
375 @Override
376 public void draw(Canvas canvas) {
Robert Carrd5c7dd62017-03-08 10:39:30 -0800377 if (mDrawFinished && !isAboveParent()) {
Dianne Hackborn1cd403e2009-09-14 22:29:14 -0700378 // draw() is not called when SKIP_DRAW is set
Dianne Hackborn4702a852012-08-17 15:18:29 -0700379 if ((mPrivateFlags & PFLAG_SKIP_DRAW) == 0) {
Dianne Hackborn1cd403e2009-09-14 22:29:14 -0700380 // punch a whole in the view-hierarchy below us
381 canvas.drawColor(0, PorterDuff.Mode.CLEAR);
382 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700383 }
384 super.draw(canvas);
385 }
386
387 @Override
388 protected void dispatchDraw(Canvas canvas) {
Robert Carrd5c7dd62017-03-08 10:39:30 -0800389 if (mDrawFinished && !isAboveParent()) {
390 // draw() is not called when SKIP_DRAW is set
Dianne Hackborn4702a852012-08-17 15:18:29 -0700391 if ((mPrivateFlags & PFLAG_SKIP_DRAW) == PFLAG_SKIP_DRAW) {
Dianne Hackborn1cd403e2009-09-14 22:29:14 -0700392 // punch a whole in the view-hierarchy below us
393 canvas.drawColor(0, PorterDuff.Mode.CLEAR);
394 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700395 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700396 super.dispatchDraw(canvas);
397 }
398
Dianne Hackbornc4d5d022009-05-21 17:32:42 -0700399 /**
Dianne Hackborn29e4a3c2009-09-30 22:35:40 -0700400 * Control whether the surface view's surface is placed on top of another
401 * regular surface view in the window (but still behind the window itself).
402 * This is typically used to place overlays on top of an underlying media
403 * surface view.
Igor Murashkina86ab6402013-08-30 12:58:36 -0700404 *
Dianne Hackborn29e4a3c2009-09-30 22:35:40 -0700405 * <p>Note that this must be set before the surface view's containing
406 * window is attached to the window manager.
Igor Murashkina86ab6402013-08-30 12:58:36 -0700407 *
Dianne Hackborn29e4a3c2009-09-30 22:35:40 -0700408 * <p>Calling this overrides any previous call to {@link #setZOrderOnTop}.
409 */
410 public void setZOrderMediaOverlay(boolean isMediaOverlay) {
Robert Carrd5c7dd62017-03-08 10:39:30 -0800411 mSubLayer = isMediaOverlay
412 ? APPLICATION_MEDIA_OVERLAY_SUBLAYER : APPLICATION_MEDIA_SUBLAYER;
Dianne Hackborn29e4a3c2009-09-30 22:35:40 -0700413 }
Igor Murashkina86ab6402013-08-30 12:58:36 -0700414
Dianne Hackborn29e4a3c2009-09-30 22:35:40 -0700415 /**
Dianne Hackborn1cd403e2009-09-14 22:29:14 -0700416 * Control whether the surface view's surface is placed on top of its
417 * window. Normally it is placed behind the window, to allow it to
418 * (for the most part) appear to composite with the views in the
419 * hierarchy. By setting this, you cause it to be placed above the
420 * window. This means that none of the contents of the window this
421 * SurfaceView is in will be visible on top of its surface.
Igor Murashkina86ab6402013-08-30 12:58:36 -0700422 *
Dianne Hackborn1cd403e2009-09-14 22:29:14 -0700423 * <p>Note that this must be set before the surface view's containing
424 * window is attached to the window manager.
Igor Murashkina86ab6402013-08-30 12:58:36 -0700425 *
Dianne Hackborn29e4a3c2009-09-30 22:35:40 -0700426 * <p>Calling this overrides any previous call to {@link #setZOrderMediaOverlay}.
Dianne Hackborn1cd403e2009-09-14 22:29:14 -0700427 */
Dianne Hackborn29e4a3c2009-09-30 22:35:40 -0700428 public void setZOrderOnTop(boolean onTop) {
Derek Sollenbergerecde72f2010-03-01 13:44:42 -0500429 if (onTop) {
Robert Carrd5c7dd62017-03-08 10:39:30 -0800430 mSubLayer = APPLICATION_PANEL_SUBLAYER;
Derek Sollenbergerecde72f2010-03-01 13:44:42 -0500431 } else {
Robert Carrd5c7dd62017-03-08 10:39:30 -0800432 mSubLayer = APPLICATION_MEDIA_SUBLAYER;
Derek Sollenbergerecde72f2010-03-01 13:44:42 -0500433 }
Dianne Hackborn1cd403e2009-09-14 22:29:14 -0700434 }
Jeff Brownf0681b32012-10-23 17:35:57 -0700435
436 /**
437 * Control whether the surface view's content should be treated as secure,
438 * preventing it from appearing in screenshots or from being viewed on
439 * non-secure displays.
440 *
441 * <p>Note that this must be set before the surface view's containing
442 * window is attached to the window manager.
443 *
444 * <p>See {@link android.view.Display#FLAG_SECURE} for details.
445 *
446 * @param isSecure True if the surface view is secure.
447 */
448 public void setSecure(boolean isSecure) {
449 if (isSecure) {
Robert Carrd5c7dd62017-03-08 10:39:30 -0800450 mSurfaceFlags |= SurfaceControl.SECURE;
Jeff Brownf0681b32012-10-23 17:35:57 -0700451 } else {
Robert Carrd5c7dd62017-03-08 10:39:30 -0800452 mSurfaceFlags &= ~SurfaceControl.SECURE;
Jeff Brownf0681b32012-10-23 17:35:57 -0700453 }
454 }
455
Robert Carr55235552017-06-02 14:21:03 -0700456 private void updateOpaqueFlag() {
Robert Carr851e7e42017-06-06 14:04:50 -0700457 if (!PixelFormat.formatHasAlpha(mRequestedFormat)) {
Robert Carr55235552017-06-02 14:21:03 -0700458 mSurfaceFlags |= SurfaceControl.OPAQUE;
459 } else {
460 mSurfaceFlags &= ~SurfaceControl.OPAQUE;
461 }
462 }
463
Robert Carrd5c7dd62017-03-08 10:39:30 -0800464 private Rect getParentSurfaceInsets() {
465 final ViewRootImpl root = getViewRootImpl();
466 if (root == null) {
467 return null;
468 } else {
469 return root.mWindowAttributes.surfaceInsets;
470 }
Jeff Tinker3896db12017-03-03 00:20:22 +0000471 }
472
Youngsang Cho9a22f0f2014-04-09 22:51:54 +0900473 /** @hide */
Robert Carrd5c7dd62017-03-08 10:39:30 -0800474 protected void updateSurface() {
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700475 if (!mHaveFrame) {
476 return;
477 }
Jeff Browna175a5b2012-02-15 19:18:31 -0800478 ViewRootImpl viewRoot = getViewRootImpl();
Robert Carrd5c7dd62017-03-08 10:39:30 -0800479 if (viewRoot == null || viewRoot.mSurface == null || !viewRoot.mSurface.isValid()) {
480 return;
Joe Onorato168173a2009-08-12 21:40:29 -0700481 }
Mitsuru Oshima64f59342009-06-21 00:03:11 -0700482
Robert Carrd5c7dd62017-03-08 10:39:30 -0800483 mTranslator = viewRoot.mTranslator;
Dianne Hackborn5be8de32011-05-24 18:11:57 -0700484 if (mTranslator != null) {
485 mSurface.setCompatibilityTranslator(mTranslator);
Mitsuru Oshima38ed7d772009-07-21 14:39:34 -0700486 }
Igor Murashkina86ab6402013-08-30 12:58:36 -0700487
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700488 int myWidth = mRequestedWidth;
489 if (myWidth <= 0) myWidth = getWidth();
490 int myHeight = mRequestedHeight;
491 if (myHeight <= 0) myHeight = getHeight();
Mitsuru Oshima001a6e522009-05-11 21:14:03 -0700492
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700493 final boolean formatChanged = mFormat != mRequestedFormat;
Robert Carr7c67b7d2017-07-06 15:28:34 -0700494 final boolean visibleChanged = mVisible != mRequestedVisible;
495 final boolean creating = (mSurfaceControl == null || formatChanged || visibleChanged)
Robert Carrd5c7dd62017-03-08 10:39:30 -0800496 && mRequestedVisible;
497 final boolean sizeChanged = mSurfaceWidth != myWidth || mSurfaceHeight != myHeight;
Robert Carrd5c7dd62017-03-08 10:39:30 -0800498 final boolean windowVisibleChanged = mWindowVisibility != mLastWindowVisibility;
Robert Carr49b593e2016-07-11 12:21:18 -0700499 boolean redrawNeeded = false;
500
Robert Carrd5c7dd62017-03-08 10:39:30 -0800501 if (creating || formatChanged || sizeChanged || visibleChanged || windowVisibleChanged) {
John Reckf6481082016-02-02 15:18:23 -0800502 getLocationInWindow(mLocation);
503
John Reckaa6e84f2016-06-16 15:36:13 -0700504 if (DEBUG) Log.i(TAG, System.identityHashCode(this) + " "
505 + "Changes: creating=" + creating
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700506 + " format=" + formatChanged + " size=" + sizeChanged
507 + " visible=" + visibleChanged
Robert Carr64aadd02015-11-06 13:54:20 -0800508 + " left=" + (mWindowSpaceLeft != mLocation[0])
509 + " top=" + (mWindowSpaceTop != mLocation[1]));
Mitsuru Oshima64f59342009-06-21 00:03:11 -0700510
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700511 try {
512 final boolean visible = mVisible = mRequestedVisible;
Robert Carr64aadd02015-11-06 13:54:20 -0800513 mWindowSpaceLeft = mLocation[0];
514 mWindowSpaceTop = mLocation[1];
Robert Carrd5c7dd62017-03-08 10:39:30 -0800515 mSurfaceWidth = myWidth;
516 mSurfaceHeight = myHeight;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700517 mFormat = mRequestedFormat;
Robert Carrd5c7dd62017-03-08 10:39:30 -0800518 mLastWindowVisibility = mWindowVisibility;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700519
Robert Carrd5c7dd62017-03-08 10:39:30 -0800520 mScreenRect.left = mWindowSpaceLeft;
521 mScreenRect.top = mWindowSpaceTop;
522 mScreenRect.right = mWindowSpaceLeft + getWidth();
523 mScreenRect.bottom = mWindowSpaceTop + getHeight();
Mitsuru Oshima64f59342009-06-21 00:03:11 -0700524 if (mTranslator != null) {
Robert Carrd5c7dd62017-03-08 10:39:30 -0800525 mTranslator.translateRectInAppWindowToScreen(mScreenRect);
Mitsuru Oshima64f59342009-06-21 00:03:11 -0700526 }
Igor Murashkina86ab6402013-08-30 12:58:36 -0700527
Robert Carrd5c7dd62017-03-08 10:39:30 -0800528 final Rect surfaceInsets = getParentSurfaceInsets();
529 mScreenRect.offset(surfaceInsets.left, surfaceInsets.top);
530
531 if (creating) {
532 mSurfaceSession = new SurfaceSession(viewRoot.mSurface);
Robert Carr3bc95b52017-03-20 21:57:23 -0700533 mDeferredDestroySurfaceControl = mSurfaceControl;
Robert Carr55235552017-06-02 14:21:03 -0700534
535 updateOpaqueFlag();
Robert Carre625fcf2017-09-01 12:36:28 -0700536 final String name = "SurfaceView - " + viewRoot.getTitle().toString();
537
538 mSurfaceControl = new SurfaceControlWithBackground(
539 name,
540 (mSurfaceFlags & SurfaceControl.OPAQUE) != 0,
541 new SurfaceControl.Builder(mSurfaceSession)
542 .setSize(mSurfaceWidth, mSurfaceHeight)
543 .setFormat(mFormat)
544 .setFlags(mSurfaceFlags));
Robert Carr44ab5752017-03-20 21:47:11 -0700545 } else if (mSurfaceControl == null) {
546 return;
Robert Carr64aadd02015-11-06 13:54:20 -0800547 }
548
Robert Carrd5c7dd62017-03-08 10:39:30 -0800549 boolean realSizeChanged = false;
Dianne Hackborn6d05fd32011-11-19 14:36:15 -0800550
Dianne Hackborn726426e2010-03-31 22:04:36 -0700551 mSurfaceLock.lock();
552 try {
Dianne Hackborn726426e2010-03-31 22:04:36 -0700553 mDrawingStopped = !visible;
Igor Murashkina86ab6402013-08-30 12:58:36 -0700554
John Reckaa6e84f2016-06-16 15:36:13 -0700555 if (DEBUG) Log.i(TAG, System.identityHashCode(this) + " "
556 + "Cur surface: " + mSurface);
Dianne Hackborn6d05fd32011-11-19 14:36:15 -0800557
Robert Carrd5c7dd62017-03-08 10:39:30 -0800558 SurfaceControl.openTransaction();
559 try {
560 mSurfaceControl.setLayer(mSubLayer);
561 if (mViewVisibility) {
562 mSurfaceControl.show();
563 } else {
564 mSurfaceControl.hide();
565 }
566
567 // While creating the surface, we will set it's initial
568 // geometry. Outside of that though, we should generally
569 // leave it to the RenderThread.
Robert Carr511719f2017-03-13 15:27:15 -0700570 //
571 // There is one more case when the buffer size changes we aren't yet
572 // prepared to sync (as even following the transaction applying
573 // we still need to latch a buffer).
574 // b/28866173
575 if (sizeChanged || creating || !mRtHandlingPositionUpdates) {
Robert Carrd5c7dd62017-03-08 10:39:30 -0800576 mSurfaceControl.setPosition(mScreenRect.left, mScreenRect.top);
577 mSurfaceControl.setMatrix(mScreenRect.width() / (float) mSurfaceWidth,
578 0.0f, 0.0f,
579 mScreenRect.height() / (float) mSurfaceHeight);
580 }
581 if (sizeChanged) {
582 mSurfaceControl.setSize(mSurfaceWidth, mSurfaceHeight);
583 }
584 } finally {
585 SurfaceControl.closeTransaction();
Dianne Hackborn726426e2010-03-31 22:04:36 -0700586 }
Dianne Hackborn6d05fd32011-11-19 14:36:15 -0800587
Robert Carrd5c7dd62017-03-08 10:39:30 -0800588 if (sizeChanged || creating) {
589 redrawNeeded = true;
590 }
Dianne Hackborn6d05fd32011-11-19 14:36:15 -0800591
Dianne Hackborn726426e2010-03-31 22:04:36 -0700592 mSurfaceFrame.left = 0;
593 mSurfaceFrame.top = 0;
594 if (mTranslator == null) {
Robert Carrd5c7dd62017-03-08 10:39:30 -0800595 mSurfaceFrame.right = mSurfaceWidth;
596 mSurfaceFrame.bottom = mSurfaceHeight;
Dianne Hackborn726426e2010-03-31 22:04:36 -0700597 } else {
598 float appInvertedScale = mTranslator.applicationInvertedScale;
Robert Carrd5c7dd62017-03-08 10:39:30 -0800599 mSurfaceFrame.right = (int) (mSurfaceWidth * appInvertedScale + 0.5f);
600 mSurfaceFrame.bottom = (int) (mSurfaceHeight * appInvertedScale + 0.5f);
Dianne Hackborn726426e2010-03-31 22:04:36 -0700601 }
Igor Murashkina86ab6402013-08-30 12:58:36 -0700602
Dianne Hackborn726426e2010-03-31 22:04:36 -0700603 final int surfaceWidth = mSurfaceFrame.right;
604 final int surfaceHeight = mSurfaceFrame.bottom;
605 realSizeChanged = mLastSurfaceWidth != surfaceWidth
606 || mLastSurfaceHeight != surfaceHeight;
607 mLastSurfaceWidth = surfaceWidth;
608 mLastSurfaceHeight = surfaceHeight;
609 } finally {
610 mSurfaceLock.unlock();
Mitsuru Oshima589cebe2009-07-22 20:38:58 -0700611 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700612
613 try {
Robert Carrd5c7dd62017-03-08 10:39:30 -0800614 redrawNeeded |= visible && !mDrawFinished;
Dianne Hackbornd76b67c2010-07-13 17:48:30 -0700615
Dianne Hackborn6d05fd32011-11-19 14:36:15 -0800616 SurfaceHolder.Callback callbacks[] = null;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700617
Robert Carrd5c7dd62017-03-08 10:39:30 -0800618 final boolean surfaceChanged = creating;
Dianne Hackborn6d05fd32011-11-19 14:36:15 -0800619 if (mSurfaceCreated && (surfaceChanged || (!visible && visibleChanged))) {
620 mSurfaceCreated = false;
621 if (mSurface.isValid()) {
John Reckaa6e84f2016-06-16 15:36:13 -0700622 if (DEBUG) Log.i(TAG, System.identityHashCode(this) + " "
623 + "visibleChanged -- surfaceDestroyed");
Dianne Hackborn6d05fd32011-11-19 14:36:15 -0800624 callbacks = getSurfaceCallbacks();
625 for (SurfaceHolder.Callback c : callbacks) {
626 c.surfaceDestroyed(mSurfaceHolder);
627 }
Robert Carr387838b2016-09-07 14:12:44 -0700628 // Since Android N the same surface may be reused and given to us
629 // again by the system server at a later point. However
630 // as we didn't do this in previous releases, clients weren't
631 // necessarily required to clean up properly in
632 // surfaceDestroyed. This leads to problems for example when
633 // clients don't destroy their EGL context, and try
634 // and create a new one on the same surface following reuse.
635 // Since there is no valid use of the surface in-between
636 // surfaceDestroyed and surfaceCreated, we force a disconnect,
637 // so the next connect will always work if we end up reusing
638 // the surface.
John Reck6ba466f2016-09-30 14:30:04 -0700639 if (mSurface.isValid()) {
640 mSurface.forceScopedDisconnect();
641 }
Mitsuru Oshima3d914922009-05-13 22:29:15 -0700642 }
Dianne Hackborn6d05fd32011-11-19 14:36:15 -0800643 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700644
Robert Carrd5c7dd62017-03-08 10:39:30 -0800645 if (creating) {
646 mSurface.copyFrom(mSurfaceControl);
647 }
648
Bryce Lee453fc362017-06-20 10:47:55 -0700649 if (sizeChanged && getContext().getApplicationInfo().targetSdkVersion
Bryce Lee02949f12017-06-16 07:20:34 -0700650 < Build.VERSION_CODES.O) {
651 // Some legacy applications use the underlying native {@link Surface} object
652 // as a key to whether anything has changed. In these cases, updates to the
653 // existing {@link Surface} will be ignored when the size changes.
654 // Therefore, we must explicitly recreate the {@link Surface} in these
655 // cases.
656 mSurface.createFrom(mSurfaceControl);
657 }
658
Andreas Röhlf750b8c2012-07-02 13:06:26 +0200659 if (visible && mSurface.isValid()) {
Dianne Hackborn6d05fd32011-11-19 14:36:15 -0800660 if (!mSurfaceCreated && (surfaceChanged || visibleChanged)) {
661 mSurfaceCreated = true;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700662 mIsCreating = true;
John Reckaa6e84f2016-06-16 15:36:13 -0700663 if (DEBUG) Log.i(TAG, System.identityHashCode(this) + " "
664 + "visibleChanged -- surfaceCreated");
Dianne Hackborn6d05fd32011-11-19 14:36:15 -0800665 if (callbacks == null) {
666 callbacks = getSurfaceCallbacks();
667 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700668 for (SurfaceHolder.Callback c : callbacks) {
669 c.surfaceCreated(mSurfaceHolder);
670 }
671 }
672 if (creating || formatChanged || sizeChanged
Dianne Hackborn726426e2010-03-31 22:04:36 -0700673 || visibleChanged || realSizeChanged) {
John Reckaa6e84f2016-06-16 15:36:13 -0700674 if (DEBUG) Log.i(TAG, System.identityHashCode(this) + " "
675 + "surfaceChanged -- format=" + mFormat
Dianne Hackborn6d05fd32011-11-19 14:36:15 -0800676 + " w=" + myWidth + " h=" + myHeight);
677 if (callbacks == null) {
678 callbacks = getSurfaceCallbacks();
679 }
Dianne Hackborn251fd432010-07-14 16:56:31 -0700680 for (SurfaceHolder.Callback c : callbacks) {
681 c.surfaceChanged(mSurfaceHolder, mFormat, myWidth, myHeight);
682 }
Dianne Hackbornd76b67c2010-07-13 17:48:30 -0700683 }
684 if (redrawNeeded) {
John Reckaa6e84f2016-06-16 15:36:13 -0700685 if (DEBUG) Log.i(TAG, System.identityHashCode(this) + " "
686 + "surfaceRedrawNeeded");
Dianne Hackborn6d05fd32011-11-19 14:36:15 -0800687 if (callbacks == null) {
688 callbacks = getSurfaceCallbacks();
689 }
Robert Carr8508bb22017-03-27 15:46:27 -0700690
691 mPendingReportDraws++;
692 viewRoot.drawPending();
Robert Carr25cfa132016-11-16 13:24:09 -0800693 SurfaceCallbackHelper sch =
Robert Carrd5c7dd62017-03-08 10:39:30 -0800694 new SurfaceCallbackHelper(this::onDrawFinished);
Robert Carr25cfa132016-11-16 13:24:09 -0800695 sch.dispatchSurfaceRedrawNeededAsync(mSurfaceHolder, callbacks);
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700696 }
697 }
698 } finally {
699 mIsCreating = false;
Robert Carrd5c7dd62017-03-08 10:39:30 -0800700 if (mSurfaceControl != null && !mSurfaceCreated) {
Robert Carrde844432017-05-04 13:45:45 -0700701 mSurface.release();
Robert Carr73678092017-05-19 15:06:50 -0700702 // If we are not in the stopped state, then the destruction of the Surface
703 // represents a visual change we need to display, and we should go ahead
704 // and destroy the SurfaceControl. However if we are in the stopped state,
705 // we can just leave the Surface around so it can be a part of animations,
706 // and we let the life-time be tied to the parent surface.
707 if (!mWindowStopped) {
708 mSurfaceControl.destroy();
709 mSurfaceControl = null;
710 }
Robert Carrd5c7dd62017-03-08 10:39:30 -0800711 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700712 }
Robert Carrd5c7dd62017-03-08 10:39:30 -0800713 } catch (Exception ex) {
Robert Carr44ab5752017-03-20 21:47:11 -0700714 Log.e(TAG, "Exception configuring surface", ex);
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700715 }
Dianne Hackborn6d05fd32011-11-19 14:36:15 -0800716 if (DEBUG) Log.v(
Robert Carrd5c7dd62017-03-08 10:39:30 -0800717 TAG, "Layout: x=" + mScreenRect.left + " y=" + mScreenRect.top
718 + " w=" + mScreenRect.width() + " h=" + mScreenRect.height()
719 + ", frame=" + mSurfaceFrame);
John Reckf23a1b82016-06-22 14:23:31 -0700720 } else {
721 // Calculate the window position in case RT loses the window
722 // and we need to fallback to a UI-thread driven position update
Robert Carrd5c7dd62017-03-08 10:39:30 -0800723 getLocationInSurface(mLocation);
John Reckf6481082016-02-02 15:18:23 -0800724 final boolean positionChanged = mWindowSpaceLeft != mLocation[0]
725 || mWindowSpaceTop != mLocation[1];
Robert Carrd5c7dd62017-03-08 10:39:30 -0800726 final boolean layoutSizeChanged = getWidth() != mScreenRect.width()
727 || getHeight() != mScreenRect.height();
John Reckf6481082016-02-02 15:18:23 -0800728 if (positionChanged || layoutSizeChanged) { // Only the position has changed
729 mWindowSpaceLeft = mLocation[0];
730 mWindowSpaceTop = mLocation[1];
Robert Carrd5c7dd62017-03-08 10:39:30 -0800731 // For our size changed check, we keep mScreenRect.width() and mScreenRect.height()
John Reckf6481082016-02-02 15:18:23 -0800732 // in view local space.
Robert Carrd5c7dd62017-03-08 10:39:30 -0800733 mLocation[0] = getWidth();
734 mLocation[1] = getHeight();
Robert Carr64aadd02015-11-06 13:54:20 -0800735
Robert Carrd5c7dd62017-03-08 10:39:30 -0800736 mScreenRect.set(mWindowSpaceLeft, mWindowSpaceTop,
Robert Carrc90e5f82017-07-18 13:10:02 -0700737 mWindowSpaceLeft + mLocation[0], mWindowSpaceTop + mLocation[1]);
John Reckf057fb52016-04-15 13:46:29 -0700738
739 if (mTranslator != null) {
Robert Carrd5c7dd62017-03-08 10:39:30 -0800740 mTranslator.translateRectInAppWindowToScreen(mScreenRect);
John Reckf057fb52016-04-15 13:46:29 -0700741 }
742
Robert Carr3651ab82017-04-25 12:05:34 -0700743 if (mSurfaceControl == null) {
744 return;
745 }
746
Bryce Lee16e50892017-04-11 01:59:37 +0000747 if (!isHardwareAccelerated() || !mRtHandlingPositionUpdates) {
John Reckf23a1b82016-06-22 14:23:31 -0700748 try {
Robert Carrd5c7dd62017-03-08 10:39:30 -0800749 if (DEBUG) Log.d(TAG, String.format("%d updateSurfacePosition UI, " +
John Reckf23a1b82016-06-22 14:23:31 -0700750 "postion = [%d, %d, %d, %d]", System.identityHashCode(this),
Robert Carrd5c7dd62017-03-08 10:39:30 -0800751 mScreenRect.left, mScreenRect.top,
752 mScreenRect.right, mScreenRect.bottom));
753 setParentSpaceRectangle(mScreenRect, -1);
754 } catch (Exception ex) {
Robert Carr44ab5752017-03-20 21:47:11 -0700755 Log.e(TAG, "Exception configuring surface", ex);
John Reckf23a1b82016-06-22 14:23:31 -0700756 }
John Reckf6481082016-02-02 15:18:23 -0800757 }
Rob Carr64e516f2015-10-29 00:20:45 +0000758 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700759 }
760 }
761
Robert Carrd5c7dd62017-03-08 10:39:30 -0800762 private void onDrawFinished() {
763 if (DEBUG) {
764 Log.i(TAG, System.identityHashCode(this) + " "
765 + "finishedDrawing");
766 }
Robert Carr3bc95b52017-03-20 21:57:23 -0700767
768 if (mDeferredDestroySurfaceControl != null) {
769 mDeferredDestroySurfaceControl.destroy();
770 mDeferredDestroySurfaceControl = null;
771 }
772
John Reck79925002017-05-26 13:57:14 -0700773 runOnUiThread(() -> {
Robert Carrb53670a2017-05-25 18:20:49 -0700774 performDrawFinished();
John Reck79925002017-05-26 13:57:14 -0700775 });
Robert Carrd5c7dd62017-03-08 10:39:30 -0800776 }
777
778 private void setParentSpaceRectangle(Rect position, long frameNumber) {
779 ViewRootImpl viewRoot = getViewRootImpl();
780
781 SurfaceControl.openTransaction();
782 try {
783 if (frameNumber > 0) {
784 mSurfaceControl.deferTransactionUntil(viewRoot.mSurface, frameNumber);
785 }
786 mSurfaceControl.setPosition(position.left, position.top);
787 mSurfaceControl.setMatrix(position.width() / (float) mSurfaceWidth,
788 0.0f, 0.0f,
789 position.height() / (float) mSurfaceHeight);
790 } finally {
791 SurfaceControl.closeTransaction();
792 }
793 }
794
John Reckf6481082016-02-02 15:18:23 -0800795 private Rect mRTLastReportedPosition = new Rect();
796
797 /**
Robert Carr33879132016-09-06 14:41:40 -0700798 * Called by native by a Rendering Worker thread to update the window position
John Reckf6481082016-02-02 15:18:23 -0800799 * @hide
800 */
Robert Carrd5c7dd62017-03-08 10:39:30 -0800801 public final void updateSurfacePosition_renderWorker(long frameNumber,
John Reckf6481082016-02-02 15:18:23 -0800802 int left, int top, int right, int bottom) {
Robert Carrd5c7dd62017-03-08 10:39:30 -0800803 if (mSurfaceControl == null) {
John Reckf6481082016-02-02 15:18:23 -0800804 return;
805 }
Robert Carrd5c7dd62017-03-08 10:39:30 -0800806
John Reckf23a1b82016-06-22 14:23:31 -0700807 // TODO: This is teensy bit racey in that a brand new SurfaceView moving on
808 // its 2nd frame if RenderThread is running slowly could potentially see
809 // this as false, enter the branch, get pre-empted, then this comes along
810 // and reports a new position, then the UI thread resumes and reports
811 // its position. This could therefore be de-sync'd in that interval, but
812 // the synchronization would violate the rule that RT must never block
813 // on the UI thread which would open up potential deadlocks. The risk of
814 // a single-frame desync is therefore preferable for now.
815 mRtHandlingPositionUpdates = true;
John Reckf6481082016-02-02 15:18:23 -0800816 if (mRTLastReportedPosition.left == left
817 && mRTLastReportedPosition.top == top
818 && mRTLastReportedPosition.right == right
819 && mRTLastReportedPosition.bottom == bottom) {
820 return;
821 }
822 try {
823 if (DEBUG) {
Robert Carrd5c7dd62017-03-08 10:39:30 -0800824 Log.d(TAG, String.format("%d updateSurfacePosition RenderWorker, frameNr = %d, " +
John Reckaa6e84f2016-06-16 15:36:13 -0700825 "postion = [%d, %d, %d, %d]", System.identityHashCode(this),
826 frameNumber, left, top, right, bottom));
John Reckf6481082016-02-02 15:18:23 -0800827 }
Wonsik Kim5aec7b92017-03-07 17:15:50 -0800828 mRTLastReportedPosition.set(left, top, right, bottom);
Robert Carrd5c7dd62017-03-08 10:39:30 -0800829 setParentSpaceRectangle(mRTLastReportedPosition, frameNumber);
830 // Now overwrite mRTLastReportedPosition with our values
831 } catch (Exception ex) {
John Reckf6481082016-02-02 15:18:23 -0800832 Log.e(TAG, "Exception from repositionChild", ex);
833 }
834 }
835
John Reckaa6e84f2016-06-16 15:36:13 -0700836 /**
Robert Carrd5c7dd62017-03-08 10:39:30 -0800837 * Called by native on RenderThread to notify that the view is no longer in the
Robert Carr33879132016-09-06 14:41:40 -0700838 * draw tree. UI thread is blocked at this point.
John Reckaa6e84f2016-06-16 15:36:13 -0700839 * @hide
840 */
Robert Carrd5c7dd62017-03-08 10:39:30 -0800841 public final void surfacePositionLost_uiRtSync(long frameNumber) {
John Reckaa6e84f2016-06-16 15:36:13 -0700842 if (DEBUG) {
Robert Carr33879132016-09-06 14:41:40 -0700843 Log.d(TAG, String.format("%d windowPositionLost, frameNr = %d",
John Reckaa6e84f2016-06-16 15:36:13 -0700844 System.identityHashCode(this), frameNumber));
845 }
Robert Carrad3a4932017-06-20 14:55:21 -0700846 mRTLastReportedPosition.setEmpty();
847
Robert Carrd5c7dd62017-03-08 10:39:30 -0800848 if (mSurfaceControl == null) {
John Reck474659c2016-06-27 07:56:37 -0700849 return;
850 }
John Reckf23a1b82016-06-22 14:23:31 -0700851 if (mRtHandlingPositionUpdates) {
852 mRtHandlingPositionUpdates = false;
853 // This callback will happen while the UI thread is blocked, so we can
854 // safely access other member variables at this time.
855 // So do what the UI thread would have done if RT wasn't handling position
856 // updates.
Robert Carrd5c7dd62017-03-08 10:39:30 -0800857 if (!mScreenRect.isEmpty() && !mScreenRect.equals(mRTLastReportedPosition)) {
John Reckf23a1b82016-06-22 14:23:31 -0700858 try {
Robert Carrd5c7dd62017-03-08 10:39:30 -0800859 if (DEBUG) Log.d(TAG, String.format("%d updateSurfacePosition, " +
John Reckf23a1b82016-06-22 14:23:31 -0700860 "postion = [%d, %d, %d, %d]", System.identityHashCode(this),
Robert Carrd5c7dd62017-03-08 10:39:30 -0800861 mScreenRect.left, mScreenRect.top,
862 mScreenRect.right, mScreenRect.bottom));
863 setParentSpaceRectangle(mScreenRect, frameNumber);
864 } catch (Exception ex) {
Robert Carr44ab5752017-03-20 21:47:11 -0700865 Log.e(TAG, "Exception configuring surface", ex);
John Reckf23a1b82016-06-22 14:23:31 -0700866 }
867 }
John Reckf23a1b82016-06-22 14:23:31 -0700868 }
John Reckaa6e84f2016-06-16 15:36:13 -0700869 }
870
Dianne Hackborn6d05fd32011-11-19 14:36:15 -0800871 private SurfaceHolder.Callback[] getSurfaceCallbacks() {
872 SurfaceHolder.Callback callbacks[];
873 synchronized (mCallbacks) {
874 callbacks = new SurfaceHolder.Callback[mCallbacks.size()];
875 mCallbacks.toArray(callbacks);
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700876 }
Dianne Hackborn6d05fd32011-11-19 14:36:15 -0800877 return callbacks;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700878 }
879
Yohei Yukawa9309c192017-09-09 17:45:27 -0700880 /**
881 * This method still exists only for compatibility reasons because some applications have relied
882 * on this method via reflection. See Issue 36345857 for details.
883 *
884 * @deprecated No platform code is using this method anymore.
885 * @hide
886 */
887 @Deprecated
888 public void setWindowType(int type) {
889 if (getContext().getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.O) {
890 throw new UnsupportedOperationException(
891 "SurfaceView#setWindowType() has never been a public API.");
892 }
893
894 if (type == TYPE_APPLICATION_PANEL) {
895 Log.e(TAG, "If you are calling SurfaceView#setWindowType(TYPE_APPLICATION_PANEL) "
896 + "just to make the SurfaceView to be placed on top of its window, you must "
897 + "call setZOrderOnTop(true) instead.", new Throwable());
898 setZOrderOnTop(true);
899 return;
900 }
901 Log.e(TAG, "SurfaceView#setWindowType(int) is deprecated and now does nothing. "
902 + "type=" + type, new Throwable());
903 }
904
John Reck79925002017-05-26 13:57:14 -0700905 private void runOnUiThread(Runnable runnable) {
906 Handler handler = getHandler();
907 if (handler != null && handler.getLooper() != Looper.myLooper()) {
908 handler.post(runnable);
909 } else {
910 runnable.run();
911 }
912 }
913
Yohei Yukawa3b5011a2017-03-16 15:34:12 -0700914 /**
Derek Sollenberger7179b812010-03-22 13:41:20 -0400915 * Check to see if the surface has fixed size dimensions or if the surface's
916 * dimensions are dimensions are dependent on its current layout.
917 *
918 * @return true if the surface has dimensions that are fixed in size
919 * @hide
920 */
921 public boolean isFixedSize() {
922 return (mRequestedWidth != -1 || mRequestedHeight != -1);
923 }
924
Robert Carrd5c7dd62017-03-08 10:39:30 -0800925 private boolean isAboveParent() {
926 return mSubLayer >= 0;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700927 }
928
Andrii Kuliancf8f6832018-01-23 19:43:30 -0800929 /**
930 * Set an opaque background color to use with this {@link SurfaceView} when it's being resized
931 * and size of the content hasn't updated yet. This color will fill the expanded area when the
932 * view becomes larger.
933 * @param bgColor An opaque color to fill the background. Alpha component will be ignored.
934 * @hide
935 */
936 public void setResizeBackgroundColor(int bgColor) {
937 mSurfaceControl.setBackgroundColor(bgColor);
938 }
939
Igor Murashkina86ab6402013-08-30 12:58:36 -0700940 private final SurfaceHolder mSurfaceHolder = new SurfaceHolder() {
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700941 private static final String LOG_TAG = "SurfaceHolder";
Igor Murashkina86ab6402013-08-30 12:58:36 -0700942
943 @Override
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700944 public boolean isCreating() {
945 return mIsCreating;
946 }
947
Igor Murashkina86ab6402013-08-30 12:58:36 -0700948 @Override
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700949 public void addCallback(Callback callback) {
950 synchronized (mCallbacks) {
Igor Murashkina86ab6402013-08-30 12:58:36 -0700951 // This is a linear search, but in practice we'll
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700952 // have only a couple callbacks, so it doesn't matter.
Igor Murashkina86ab6402013-08-30 12:58:36 -0700953 if (mCallbacks.contains(callback) == false) {
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700954 mCallbacks.add(callback);
955 }
956 }
957 }
958
Igor Murashkina86ab6402013-08-30 12:58:36 -0700959 @Override
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700960 public void removeCallback(Callback callback) {
961 synchronized (mCallbacks) {
962 mCallbacks.remove(callback);
963 }
964 }
Igor Murashkina86ab6402013-08-30 12:58:36 -0700965
966 @Override
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700967 public void setFixedSize(int width, int height) {
968 if (mRequestedWidth != width || mRequestedHeight != height) {
969 mRequestedWidth = width;
970 mRequestedHeight = height;
971 requestLayout();
972 }
973 }
974
Igor Murashkina86ab6402013-08-30 12:58:36 -0700975 @Override
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700976 public void setSizeFromLayout() {
977 if (mRequestedWidth != -1 || mRequestedHeight != -1) {
978 mRequestedWidth = mRequestedHeight = -1;
979 requestLayout();
980 }
981 }
982
Igor Murashkina86ab6402013-08-30 12:58:36 -0700983 @Override
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700984 public void setFormat(int format) {
Mathias Agopian2d468c52010-06-14 21:50:48 -0700985 // for backward compatibility reason, OPAQUE always
986 // means 565 for SurfaceView
987 if (format == PixelFormat.OPAQUE)
988 format = PixelFormat.RGB_565;
989
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700990 mRequestedFormat = format;
Robert Carrd5c7dd62017-03-08 10:39:30 -0800991 if (mSurfaceControl != null) {
992 updateSurface();
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700993 }
994 }
995
Mathias Agopiand2112302010-12-07 19:38:17 -0800996 /**
997 * @deprecated setType is now ignored.
998 */
Igor Murashkina86ab6402013-08-30 12:58:36 -0700999 @Override
Mathias Agopiand2112302010-12-07 19:38:17 -08001000 @Deprecated
1001 public void setType(int type) { }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07001002
Igor Murashkina86ab6402013-08-30 12:58:36 -07001003 @Override
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07001004 public void setKeepScreenOn(boolean screenOn) {
John Reck79925002017-05-26 13:57:14 -07001005 runOnUiThread(() -> SurfaceView.this.setKeepScreenOn(screenOn));
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07001006 }
Igor Murashkina86ab6402013-08-30 12:58:36 -07001007
Mathias Agopian9ddf32a2013-04-17 15:04:47 -07001008 /**
1009 * Gets a {@link Canvas} for drawing into the SurfaceView's Surface
1010 *
1011 * After drawing into the provided {@link Canvas}, the caller must
1012 * invoke {@link #unlockCanvasAndPost} to post the new contents to the surface.
1013 *
1014 * The caller must redraw the entire surface.
1015 * @return A canvas for drawing into the surface.
1016 */
Igor Murashkina86ab6402013-08-30 12:58:36 -07001017 @Override
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07001018 public Canvas lockCanvas() {
John Reck6bc70142016-10-26 16:49:17 -07001019 return internalLockCanvas(null, false);
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07001020 }
1021
Mathias Agopian9ddf32a2013-04-17 15:04:47 -07001022 /**
1023 * Gets a {@link Canvas} for drawing into the SurfaceView's Surface
1024 *
1025 * After drawing into the provided {@link Canvas}, the caller must
1026 * invoke {@link #unlockCanvasAndPost} to post the new contents to the surface.
1027 *
1028 * @param inOutDirty A rectangle that represents the dirty region that the caller wants
1029 * to redraw. This function may choose to expand the dirty rectangle if for example
1030 * the surface has been resized or if the previous contents of the surface were
1031 * not available. The caller must redraw the entire dirty region as represented
1032 * by the contents of the inOutDirty rectangle upon return from this function.
1033 * The caller may also pass <code>null</code> instead, in the case where the
1034 * entire surface should be redrawn.
1035 * @return A canvas for drawing into the surface.
1036 */
Igor Murashkina86ab6402013-08-30 12:58:36 -07001037 @Override
Mathias Agopian9ddf32a2013-04-17 15:04:47 -07001038 public Canvas lockCanvas(Rect inOutDirty) {
John Reck6bc70142016-10-26 16:49:17 -07001039 return internalLockCanvas(inOutDirty, false);
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07001040 }
1041
John Reck6bc70142016-10-26 16:49:17 -07001042 @Override
1043 public Canvas lockHardwareCanvas() {
1044 return internalLockCanvas(null, true);
1045 }
1046
1047 private Canvas internalLockCanvas(Rect dirty, boolean hardware) {
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07001048 mSurfaceLock.lock();
1049
John Reckaa6e84f2016-06-16 15:36:13 -07001050 if (DEBUG) Log.i(TAG, System.identityHashCode(this) + " " + "Locking canvas... stopped="
Robert Carrd5c7dd62017-03-08 10:39:30 -08001051 + mDrawingStopped + ", surfaceControl=" + mSurfaceControl);
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07001052
1053 Canvas c = null;
Robert Carrd5c7dd62017-03-08 10:39:30 -08001054 if (!mDrawingStopped && mSurfaceControl != null) {
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07001055 try {
John Reck6bc70142016-10-26 16:49:17 -07001056 if (hardware) {
1057 c = mSurface.lockHardwareCanvas();
1058 } else {
1059 c = mSurface.lockCanvas(dirty);
1060 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07001061 } catch (Exception e) {
1062 Log.e(LOG_TAG, "Exception locking surface", e);
1063 }
1064 }
1065
John Reckaa6e84f2016-06-16 15:36:13 -07001066 if (DEBUG) Log.i(TAG, System.identityHashCode(this) + " " + "Returned canvas: " + c);
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07001067 if (c != null) {
1068 mLastLockTime = SystemClock.uptimeMillis();
1069 return c;
1070 }
Igor Murashkina86ab6402013-08-30 12:58:36 -07001071
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07001072 // If the Surface is not ready to be drawn, then return null,
1073 // but throttle calls to this function so it isn't called more
1074 // than every 100ms.
1075 long now = SystemClock.uptimeMillis();
1076 long nextTime = mLastLockTime + 100;
1077 if (nextTime > now) {
1078 try {
1079 Thread.sleep(nextTime-now);
1080 } catch (InterruptedException e) {
1081 }
1082 now = SystemClock.uptimeMillis();
1083 }
1084 mLastLockTime = now;
1085 mSurfaceLock.unlock();
Igor Murashkina86ab6402013-08-30 12:58:36 -07001086
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07001087 return null;
1088 }
1089
Mathias Agopian9ddf32a2013-04-17 15:04:47 -07001090 /**
1091 * Posts the new contents of the {@link Canvas} to the surface and
1092 * releases the {@link Canvas}.
1093 *
1094 * @param canvas The canvas previously obtained from {@link #lockCanvas}.
1095 */
Igor Murashkina86ab6402013-08-30 12:58:36 -07001096 @Override
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07001097 public void unlockCanvasAndPost(Canvas canvas) {
1098 mSurface.unlockCanvasAndPost(canvas);
1099 mSurfaceLock.unlock();
1100 }
1101
Igor Murashkina86ab6402013-08-30 12:58:36 -07001102 @Override
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07001103 public Surface getSurface() {
1104 return mSurface;
1105 }
1106
Igor Murashkina86ab6402013-08-30 12:58:36 -07001107 @Override
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07001108 public Rect getSurfaceFrame() {
1109 return mSurfaceFrame;
1110 }
1111 };
Robert Carr55235552017-06-02 14:21:03 -07001112
1113 class SurfaceControlWithBackground extends SurfaceControl {
1114 private SurfaceControl mBackgroundControl;
1115 private boolean mOpaque = true;
1116 public boolean mVisible = false;
1117
Robert Carre625fcf2017-09-01 12:36:28 -07001118 public SurfaceControlWithBackground(String name, boolean opaque, SurfaceControl.Builder b)
Robert Carr55235552017-06-02 14:21:03 -07001119 throws Exception {
Robert Carre625fcf2017-09-01 12:36:28 -07001120 super(b.setName(name).build());
1121
1122 mBackgroundControl = b.setName("Background for -" + name)
1123 .setFormat(OPAQUE)
1124 .setColorLayer(true)
1125 .build();
1126 mOpaque = opaque;
Robert Carr55235552017-06-02 14:21:03 -07001127 }
1128
1129 @Override
1130 public void setAlpha(float alpha) {
1131 super.setAlpha(alpha);
1132 mBackgroundControl.setAlpha(alpha);
1133 }
1134
1135 @Override
1136 public void setLayer(int zorder) {
1137 super.setLayer(zorder);
1138 // -3 is below all other child layers as SurfaceView never goes below -2
1139 mBackgroundControl.setLayer(-3);
1140 }
1141
1142 @Override
1143 public void setPosition(float x, float y) {
1144 super.setPosition(x, y);
1145 mBackgroundControl.setPosition(x, y);
1146 }
1147
1148 @Override
1149 public void setSize(int w, int h) {
1150 super.setSize(w, h);
1151 mBackgroundControl.setSize(w, h);
1152 }
1153
1154 @Override
1155 public void setWindowCrop(Rect crop) {
1156 super.setWindowCrop(crop);
1157 mBackgroundControl.setWindowCrop(crop);
1158 }
1159
1160 @Override
1161 public void setFinalCrop(Rect crop) {
1162 super.setFinalCrop(crop);
1163 mBackgroundControl.setFinalCrop(crop);
1164 }
1165
1166 @Override
1167 public void setLayerStack(int layerStack) {
1168 super.setLayerStack(layerStack);
1169 mBackgroundControl.setLayerStack(layerStack);
1170 }
1171
1172 @Override
1173 public void setOpaque(boolean isOpaque) {
1174 super.setOpaque(isOpaque);
1175 mOpaque = isOpaque;
1176 updateBackgroundVisibility();
1177 }
1178
1179 @Override
1180 public void setSecure(boolean isSecure) {
1181 super.setSecure(isSecure);
1182 }
1183
1184 @Override
1185 public void setMatrix(float dsdx, float dtdx, float dsdy, float dtdy) {
1186 super.setMatrix(dsdx, dtdx, dsdy, dtdy);
1187 mBackgroundControl.setMatrix(dsdx, dtdx, dsdy, dtdy);
1188 }
1189
1190 @Override
1191 public void hide() {
1192 super.hide();
1193 mVisible = false;
1194 updateBackgroundVisibility();
1195 }
1196
1197 @Override
1198 public void show() {
1199 super.show();
1200 mVisible = true;
1201 updateBackgroundVisibility();
1202 }
1203
1204 @Override
1205 public void destroy() {
1206 super.destroy();
1207 mBackgroundControl.destroy();
1208 }
1209
1210 @Override
1211 public void release() {
1212 super.release();
1213 mBackgroundControl.release();
1214 }
1215
1216 @Override
1217 public void setTransparentRegionHint(Region region) {
1218 super.setTransparentRegionHint(region);
1219 mBackgroundControl.setTransparentRegionHint(region);
1220 }
1221
1222 @Override
1223 public void deferTransactionUntil(IBinder handle, long frame) {
1224 super.deferTransactionUntil(handle, frame);
1225 mBackgroundControl.deferTransactionUntil(handle, frame);
1226 }
1227
Robert Carr552da0e2017-06-12 11:43:51 -07001228 @Override
1229 public void deferTransactionUntil(Surface barrier, long frame) {
1230 super.deferTransactionUntil(barrier, frame);
1231 mBackgroundControl.deferTransactionUntil(barrier, frame);
1232 }
1233
Andrii Kuliancf8f6832018-01-23 19:43:30 -08001234 /** Set the color to fill the background with. */
1235 private void setBackgroundColor(int bgColor) {
1236 final float[] colorComponents = new float[] { Color.red(bgColor) / 255.f,
1237 Color.green(bgColor) / 255.f, Color.blue(bgColor) / 255.f };
1238
1239 SurfaceControl.openTransaction();
1240 try {
1241 mBackgroundControl.setColor(colorComponents);
1242 } finally {
1243 SurfaceControl.closeTransaction();
1244 }
1245 }
1246
Robert Carr55235552017-06-02 14:21:03 -07001247 void updateBackgroundVisibility() {
1248 if (mOpaque && mVisible) {
1249 mBackgroundControl.show();
1250 } else {
1251 mBackgroundControl.hide();
1252 }
1253 }
1254 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07001255}