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