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