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