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 | |
Robert Carr | 30ad618 | 2019-12-18 01:46:04 -0800 | [diff] [blame] | 23 | import android.annotation.NonNull; |
Vishnu Nair | 5cf25319 | 2019-11-07 15:33:20 -0800 | [diff] [blame] | 24 | import android.annotation.Nullable; |
Artur Satayev | ad9254c | 2019-12-10 17:47:54 +0000 | [diff] [blame] | 25 | import android.compat.annotation.UnsupportedAppUsage; |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 26 | import android.content.Context; |
Mitsuru Oshima | 64f5934 | 2009-06-21 00:03:11 -0700 | [diff] [blame] | 27 | import android.content.res.CompatibilityInfo.Translator; |
Mark Renouf | 34d04f3 | 2019-05-13 15:53:18 -0400 | [diff] [blame] | 28 | import android.graphics.BlendMode; |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 29 | import android.graphics.Canvas; |
Andrii Kulian | cf8f683 | 2018-01-23 19:43:30 -0800 | [diff] [blame] | 30 | import android.graphics.Color; |
Jackal Guo | ac234d6 | 2020-02-03 15:05:43 +0800 | [diff] [blame] | 31 | import android.graphics.Matrix; |
Mark Renouf | 34d04f3 | 2019-05-13 15:53:18 -0400 | [diff] [blame] | 32 | import android.graphics.Paint; |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 33 | import android.graphics.PixelFormat; |
| 34 | import android.graphics.PorterDuff; |
| 35 | import android.graphics.Rect; |
| 36 | import android.graphics.Region; |
John Reck | 32f140aa6 | 2018-10-04 15:08:24 -0700 | [diff] [blame] | 37 | import android.graphics.RenderNode; |
Yohei Yukawa | 3b5011a | 2017-03-16 15:34:12 -0700 | [diff] [blame] | 38 | import android.os.Build; |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 39 | import android.os.Handler; |
Vishnu Nair | 5cf25319 | 2019-11-07 15:33:20 -0800 | [diff] [blame] | 40 | import android.os.IBinder; |
John Reck | 7992500 | 2017-05-26 13:57:14 -0700 | [diff] [blame] | 41 | import android.os.Looper; |
Jackal Guo | ac234d6 | 2020-02-03 15:05:43 +0800 | [diff] [blame] | 42 | import android.os.RemoteException; |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 43 | import android.os.SystemClock; |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 44 | import android.util.AttributeSet; |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 45 | import android.util.Log; |
Vishnu Nair | 8cb00ae | 2019-08-02 15:20:29 -0700 | [diff] [blame] | 46 | import android.view.SurfaceControl.Transaction; |
Valerie Hau | 779af29 | 2020-02-07 10:53:32 -0800 | [diff] [blame] | 47 | import android.view.accessibility.AccessibilityNodeInfo; |
Jackal Guo | ac234d6 | 2020-02-03 15:05:43 +0800 | [diff] [blame] | 48 | import android.view.accessibility.IAccessibilityEmbeddedConnection; |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 49 | |
Robert Carr | 25cfa13 | 2016-11-16 13:24:09 -0800 | [diff] [blame] | 50 | import com.android.internal.view.SurfaceCallbackHelper; |
Aurimas Liutikas | 67e2ae8 | 2016-10-11 18:17:42 -0700 | [diff] [blame] | 51 | |
Jon Larimer | 9bdf576 | 2009-01-02 18:55:15 -0500 | [diff] [blame] | 52 | import java.util.ArrayList; |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 53 | import java.util.concurrent.locks.ReentrantLock; |
| 54 | |
| 55 | /** |
| 56 | * Provides a dedicated drawing surface embedded inside of a view hierarchy. |
| 57 | * You can control the format of this surface and, if you like, its size; the |
| 58 | * SurfaceView takes care of placing the surface at the correct location on the |
| 59 | * screen |
Igor Murashkin | a86ab640 | 2013-08-30 12:58:36 -0700 | [diff] [blame] | 60 | * |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 61 | * <p>The surface is Z ordered so that it is behind the window holding its |
| 62 | * SurfaceView; the SurfaceView punches a hole in its window to allow its |
Jesse Hall | c9f345f | 2012-09-26 11:55:16 -0700 | [diff] [blame] | 63 | * 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] | 64 | * compositing with the Surface any siblings of the SurfaceView that would |
Jesse Hall | c9f345f | 2012-09-26 11:55:16 -0700 | [diff] [blame] | 65 | * 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] | 66 | * buttons on top of the Surface, though note however that it can have an |
| 67 | * impact on performance since a full alpha-blended composite will be performed |
| 68 | * each time the Surface changes. |
Igor Murashkin | a86ab640 | 2013-08-30 12:58:36 -0700 | [diff] [blame] | 69 | * |
Jesse Hall | c9f345f | 2012-09-26 11:55:16 -0700 | [diff] [blame] | 70 | * <p> The transparent region that makes the surface visible is based on the |
| 71 | * layout positions in the view hierarchy. If the post-layout transform |
| 72 | * properties are used to draw a sibling view on top of the SurfaceView, the |
| 73 | * view may not be properly composited with the surface. |
| 74 | * |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 75 | * <p>Access to the underlying surface is provided via the SurfaceHolder interface, |
| 76 | * which can be retrieved by calling {@link #getHolder}. |
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 | * <p>The Surface will be created for you while the SurfaceView's window is |
| 79 | * visible; you should implement {@link SurfaceHolder.Callback#surfaceCreated} |
| 80 | * and {@link SurfaceHolder.Callback#surfaceDestroyed} to discover when the |
| 81 | * Surface is created and destroyed as the window is shown and hidden. |
Igor Murashkin | a86ab640 | 2013-08-30 12:58:36 -0700 | [diff] [blame] | 82 | * |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 83 | * <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] | 84 | * 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] | 85 | * this way, you need to be aware of some threading semantics: |
Igor Murashkin | a86ab640 | 2013-08-30 12:58:36 -0700 | [diff] [blame] | 86 | * |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 87 | * <ul> |
| 88 | * <li> All SurfaceView and |
| 89 | * {@link SurfaceHolder.Callback SurfaceHolder.Callback} methods will be called |
| 90 | * from the thread running the SurfaceView's window (typically the main thread |
Jesse Hall | c9f345f | 2012-09-26 11:55:16 -0700 | [diff] [blame] | 91 | * 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] | 92 | * state that is also touched by the drawing thread. |
| 93 | * <li> You must ensure that the drawing thread only touches the underlying |
| 94 | * Surface while it is valid -- between |
| 95 | * {@link SurfaceHolder.Callback#surfaceCreated SurfaceHolder.Callback.surfaceCreated()} |
| 96 | * and |
| 97 | * {@link SurfaceHolder.Callback#surfaceDestroyed SurfaceHolder.Callback.surfaceDestroyed()}. |
| 98 | * </ul> |
Chris Craik | 6ee192f | 2016-05-17 14:29:10 -0700 | [diff] [blame] | 99 | * |
| 100 | * <p class="note"><strong>Note:</strong> Starting in platform version |
| 101 | * {@link android.os.Build.VERSION_CODES#N}, SurfaceView's window position is |
| 102 | * updated synchronously with other View rendering. This means that translating |
| 103 | * and scaling a SurfaceView on screen will not cause rendering artifacts. Such |
| 104 | * artifacts may occur on previous versions of the platform when its window is |
| 105 | * positioned asynchronously.</p> |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 106 | */ |
Vishnu Nair | 3b037f7 | 2019-07-22 12:55:58 -0700 | [diff] [blame] | 107 | public class SurfaceView extends View implements ViewRootImpl.SurfaceChangedCallback { |
Robert Carr | d5c7dd6 | 2017-03-08 10:39:30 -0800 | [diff] [blame] | 108 | private static final String TAG = "SurfaceView"; |
| 109 | private static final boolean DEBUG = false; |
Issei Suzuki | 006b71f | 2019-06-17 15:56:57 +0200 | [diff] [blame] | 110 | private static final boolean DEBUG_POSITION = false; |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 111 | |
Mathew Inwood | a570dee | 2018-08-17 14:56:00 +0100 | [diff] [blame] | 112 | @UnsupportedAppUsage |
Issei Suzuki | f041259 | 2019-07-02 12:31:34 +0200 | [diff] [blame] | 113 | final ArrayList<SurfaceHolder.Callback> mCallbacks = new ArrayList<>(); |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 114 | |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 115 | final int[] mLocation = new int[2]; |
Igor Murashkin | a86ab640 | 2013-08-30 12:58:36 -0700 | [diff] [blame] | 116 | |
Mathew Inwood | a570dee | 2018-08-17 14:56:00 +0100 | [diff] [blame] | 117 | @UnsupportedAppUsage |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 118 | final ReentrantLock mSurfaceLock = new ReentrantLock(); |
Mathew Inwood | a570dee | 2018-08-17 14:56:00 +0100 | [diff] [blame] | 119 | @UnsupportedAppUsage |
Dianne Hackborn | 61566cc | 2011-12-02 23:31:52 -0800 | [diff] [blame] | 120 | final Surface mSurface = new Surface(); // Current surface in use |
Mathew Inwood | 31755f9 | 2018-12-20 13:53:36 +0000 | [diff] [blame] | 121 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023) |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 122 | boolean mDrawingStopped = true; |
Robert Carr | d5c7dd6 | 2017-03-08 10:39:30 -0800 | [diff] [blame] | 123 | // We use this to track if the application has produced a frame |
| 124 | // in to the Surface. Up until that point, we should be careful not to punch |
| 125 | // holes. |
| 126 | boolean mDrawFinished = false; |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 127 | |
Robert Carr | d5c7dd6 | 2017-03-08 10:39:30 -0800 | [diff] [blame] | 128 | final Rect mScreenRect = new Rect(); |
Vishnu Nair | 8cb00ae | 2019-08-02 15:20:29 -0700 | [diff] [blame] | 129 | private final SurfaceSession mSurfaceSession = new SurfaceSession(); |
Robert Carr | d5c7dd6 | 2017-03-08 10:39:30 -0800 | [diff] [blame] | 130 | |
Robert Carr | b923f54 | 2019-05-13 12:27:51 -0700 | [diff] [blame] | 131 | SurfaceControl mSurfaceControl; |
Robert Carr | 3bc95b5 | 2017-03-20 21:57:23 -0700 | [diff] [blame] | 132 | // In the case of format changes we switch out the surface in-place |
| 133 | // we need to preserve the old one until the new one has drawn. |
Robert Carr | b923f54 | 2019-05-13 12:27:51 -0700 | [diff] [blame] | 134 | SurfaceControl mDeferredDestroySurfaceControl; |
| 135 | SurfaceControl mBackgroundControl; |
shawnlin | ba532af | 2020-06-02 17:48:37 +0800 | [diff] [blame] | 136 | private boolean mDisableBackgroundLayer = false; |
Robert Carr | ebaaca1 | 2020-05-13 11:23:43 -0700 | [diff] [blame] | 137 | |
| 138 | /** |
| 139 | * We use this lock in SOME cases when reading or writing SurfaceControl, |
| 140 | * but use the following model so that the RenderThread can run locklessly |
| 141 | * in the position up-date case. |
| 142 | * |
| 143 | * 1. UI Thread can read from mSurfaceControl (use in Transactions) without |
| 144 | * holding the lock. |
| 145 | * 2. UI Thread will hold the lock when writing to mSurfaceControl (calling release |
| 146 | * or remove). |
| 147 | * 3. Render thread will also hold the lock when writing to mSurfaceControl (e.g. |
| 148 | * calling release from positionLost). |
| 149 | * 3. RenderNode.PositionUpdateListener::positionChanged will only be called |
| 150 | * when the UI thread is paused (blocked on the Render thread). |
| 151 | * 4. positionChanged thus will not be required to hold the lock as the |
| 152 | * UI thread is blocked, and the other writer is the RT itself. |
| 153 | */ |
Issei Suzuki | 006b71f | 2019-06-17 15:56:57 +0200 | [diff] [blame] | 154 | final Object mSurfaceControlLock = new Object(); |
Robert Carr | 3387913 | 2016-09-06 14:41:40 -0700 | [diff] [blame] | 155 | final Rect mTmpRect = new Rect(); |
Igor Murashkin | a86ab640 | 2013-08-30 12:58:36 -0700 | [diff] [blame] | 156 | |
Mark Renouf | 34d04f3 | 2019-05-13 15:53:18 -0400 | [diff] [blame] | 157 | Paint mRoundedViewportPaint; |
| 158 | |
Robert Carr | d5c7dd6 | 2017-03-08 10:39:30 -0800 | [diff] [blame] | 159 | int mSubLayer = APPLICATION_MEDIA_SUBLAYER; |
Igor Murashkin | a86ab640 | 2013-08-30 12:58:36 -0700 | [diff] [blame] | 160 | |
Mathew Inwood | 31755f9 | 2018-12-20 13:53:36 +0000 | [diff] [blame] | 161 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023) |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 162 | boolean mIsCreating = false; |
John Reck | f23a1b8 | 2016-06-22 14:23:31 -0700 | [diff] [blame] | 163 | private volatile boolean mRtHandlingPositionUpdates = false; |
Robert Carr | 005c63e | 2019-09-20 14:31:24 -0700 | [diff] [blame] | 164 | private volatile boolean mRtReleaseSurfaces = false; |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 165 | |
Issei Suzuki | f041259 | 2019-07-02 12:31:34 +0200 | [diff] [blame] | 166 | private final ViewTreeObserver.OnScrollChangedListener mScrollChangedListener = |
| 167 | this::updateSurface; |
Igor Murashkin | a86ab640 | 2013-08-30 12:58:36 -0700 | [diff] [blame] | 168 | |
Mathew Inwood | a570dee | 2018-08-17 14:56:00 +0100 | [diff] [blame] | 169 | @UnsupportedAppUsage |
Issei Suzuki | f041259 | 2019-07-02 12:31:34 +0200 | [diff] [blame] | 170 | private final ViewTreeObserver.OnPreDrawListener mDrawListener = () -> { |
| 171 | // reposition ourselves where the surface is |
| 172 | mHaveFrame = getWidth() > 0 && getHeight() > 0; |
| 173 | updateSurface(); |
| 174 | return true; |
| 175 | }; |
John Reck | f648108 | 2016-02-02 15:18:23 -0800 | [diff] [blame] | 176 | |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 177 | boolean mRequestedVisible = false; |
Mathias Agopian | 6b7f1a6 | 2009-09-09 18:32:34 -0700 | [diff] [blame] | 178 | boolean mWindowVisibility = false; |
Robert Carr | d5c7dd6 | 2017-03-08 10:39:30 -0800 | [diff] [blame] | 179 | boolean mLastWindowVisibility = false; |
Mathias Agopian | 6b7f1a6 | 2009-09-09 18:32:34 -0700 | [diff] [blame] | 180 | boolean mViewVisibility = false; |
Robert Carr | 414ebc6 | 2017-04-12 12:01:00 -0700 | [diff] [blame] | 181 | boolean mWindowStopped = false; |
| 182 | |
Mathew Inwood | 31755f9 | 2018-12-20 13:53:36 +0000 | [diff] [blame] | 183 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023) |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 184 | int mRequestedWidth = -1; |
Mathew Inwood | 31755f9 | 2018-12-20 13:53:36 +0000 | [diff] [blame] | 185 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023) |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 186 | int mRequestedHeight = -1; |
Mathias Agopian | d6ddcb7 | 2010-05-24 19:00:08 -0700 | [diff] [blame] | 187 | /* Set SurfaceView's format to 565 by default to maintain backward |
| 188 | * compatibility with applications assuming this format. |
| 189 | */ |
Mathew Inwood | a570dee | 2018-08-17 14:56:00 +0100 | [diff] [blame] | 190 | @UnsupportedAppUsage |
Mathias Agopian | d6ddcb7 | 2010-05-24 19:00:08 -0700 | [diff] [blame] | 191 | int mRequestedFormat = PixelFormat.RGB_565; |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 192 | |
Issei Suzuki | 006b71f | 2019-06-17 15:56:57 +0200 | [diff] [blame] | 193 | boolean mUseAlpha = false; |
| 194 | float mSurfaceAlpha = 1f; |
Mark Renouf | e574ad0 | 2019-08-12 16:22:15 -0400 | [diff] [blame] | 195 | boolean mClipSurfaceToBounds; |
Winson Chung | d4a9abd | 2020-03-21 23:01:16 -0700 | [diff] [blame] | 196 | int mBackgroundColor = Color.BLACK; |
Issei Suzuki | 006b71f | 2019-06-17 15:56:57 +0200 | [diff] [blame] | 197 | |
Mathew Inwood | a570dee | 2018-08-17 14:56:00 +0100 | [diff] [blame] | 198 | @UnsupportedAppUsage |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 199 | boolean mHaveFrame = false; |
Dianne Hackborn | 6d05fd3 | 2011-11-19 14:36:15 -0800 | [diff] [blame] | 200 | boolean mSurfaceCreated = false; |
Mathew Inwood | 31755f9 | 2018-12-20 13:53:36 +0000 | [diff] [blame] | 201 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023) |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 202 | long mLastLockTime = 0; |
Igor Murashkin | a86ab640 | 2013-08-30 12:58:36 -0700 | [diff] [blame] | 203 | |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 204 | boolean mVisible = false; |
Robert Carr | 64aadd0 | 2015-11-06 13:54:20 -0800 | [diff] [blame] | 205 | int mWindowSpaceLeft = -1; |
| 206 | int mWindowSpaceTop = -1; |
Robert Carr | d5c7dd6 | 2017-03-08 10:39:30 -0800 | [diff] [blame] | 207 | int mSurfaceWidth = -1; |
| 208 | int mSurfaceHeight = -1; |
Mark Renouf | 34d04f3 | 2019-05-13 15:53:18 -0400 | [diff] [blame] | 209 | float mCornerRadius; |
Mathew Inwood | a570dee | 2018-08-17 14:56:00 +0100 | [diff] [blame] | 210 | @UnsupportedAppUsage |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 211 | int mFormat = -1; |
Mathew Inwood | 31755f9 | 2018-12-20 13:53:36 +0000 | [diff] [blame] | 212 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023) |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 213 | final Rect mSurfaceFrame = new Rect(); |
Dianne Hackborn | 726426e | 2010-03-31 22:04:36 -0700 | [diff] [blame] | 214 | int mLastSurfaceWidth = -1, mLastSurfaceHeight = -1; |
Romain Guy | f2499fa | 2011-01-26 18:31:23 -0800 | [diff] [blame] | 215 | |
Romain Guy | 01d5edc | 2011-01-28 11:28:53 -0800 | [diff] [blame] | 216 | private boolean mGlobalListenersAdded; |
Robert Carr | 8508bb2 | 2017-03-27 15:46:27 -0700 | [diff] [blame] | 217 | private boolean mAttachedToWindow; |
Romain Guy | f2499fa | 2011-01-26 18:31:23 -0800 | [diff] [blame] | 218 | |
Robert Carr | d5c7dd6 | 2017-03-08 10:39:30 -0800 | [diff] [blame] | 219 | private int mSurfaceFlags = SurfaceControl.HIDDEN; |
| 220 | |
Robert Carr | 8508bb2 | 2017-03-27 15:46:27 -0700 | [diff] [blame] | 221 | private int mPendingReportDraws; |
| 222 | |
Robert Carr | 27a800a | 2018-03-16 13:33:45 -0700 | [diff] [blame] | 223 | private SurfaceControl.Transaction mRtTransaction = new SurfaceControl.Transaction(); |
chaviw | 9f6171e | 2019-06-07 16:33:50 -0700 | [diff] [blame] | 224 | private SurfaceControl.Transaction mTmpTransaction = new SurfaceControl.Transaction(); |
Vishnu Nair | 8cb00ae | 2019-08-02 15:20:29 -0700 | [diff] [blame] | 225 | private int mParentSurfaceGenerationId; |
Robert Carr | 27a800a | 2018-03-16 13:33:45 -0700 | [diff] [blame] | 226 | |
Jackal Guo | ac234d6 | 2020-02-03 15:05:43 +0800 | [diff] [blame] | 227 | private RemoteAccessibilityEmbeddedConnection mRemoteAccessibilityEmbeddedConnection; |
| 228 | |
| 229 | private final Matrix mScreenMatrixForEmbeddedHierarchy = new Matrix(); |
| 230 | private final Matrix mTmpMatrix = new Matrix(); |
| 231 | private final float[] mMatrixValues = new float[9]; |
| 232 | |
Robert Carr | 30ad618 | 2019-12-18 01:46:04 -0800 | [diff] [blame] | 233 | SurfaceControlViewHost.SurfacePackage mSurfacePackage; |
Jackal Guo | 79b182e | 2019-11-18 11:44:52 +0800 | [diff] [blame] | 234 | |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 235 | public SurfaceView(Context context) { |
Alan Viverette | 768ca7d | 2016-08-04 09:54:14 -0400 | [diff] [blame] | 236 | this(context, null); |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 237 | } |
Igor Murashkin | a86ab640 | 2013-08-30 12:58:36 -0700 | [diff] [blame] | 238 | |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 239 | public SurfaceView(Context context, AttributeSet attrs) { |
Alan Viverette | 768ca7d | 2016-08-04 09:54:14 -0400 | [diff] [blame] | 240 | this(context, attrs, 0); |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 241 | } |
| 242 | |
Alan Viverette | 617feb9 | 2013-09-09 18:09:13 -0700 | [diff] [blame] | 243 | public SurfaceView(Context context, AttributeSet attrs, int defStyleAttr) { |
Alan Viverette | 768ca7d | 2016-08-04 09:54:14 -0400 | [diff] [blame] | 244 | this(context, attrs, defStyleAttr, 0); |
Alan Viverette | 617feb9 | 2013-09-09 18:09:13 -0700 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | public SurfaceView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { |
shawnlin | ba532af | 2020-06-02 17:48:37 +0800 | [diff] [blame] | 248 | this(context, attrs, defStyleAttr, defStyleRes, false); |
| 249 | } |
| 250 | |
| 251 | /** @hide */ |
| 252 | public SurfaceView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr, |
| 253 | int defStyleRes, boolean disableBackgroundLayer) { |
Alan Viverette | 617feb9 | 2013-09-09 18:09:13 -0700 | [diff] [blame] | 254 | super(context, attrs, defStyleAttr, defStyleRes); |
Adam Powell | 769b863 | 2019-02-04 11:28:22 -0800 | [diff] [blame] | 255 | mRenderNode.addPositionUpdateListener(mPositionListener); |
Mathias Agopian | d6ddcb7 | 2010-05-24 19:00:08 -0700 | [diff] [blame] | 256 | |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 257 | setWillNotDraw(true); |
shawnlin | ba532af | 2020-06-02 17:48:37 +0800 | [diff] [blame] | 258 | mDisableBackgroundLayer = disableBackgroundLayer; |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 259 | } |
Igor Murashkin | a86ab640 | 2013-08-30 12:58:36 -0700 | [diff] [blame] | 260 | |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 261 | /** |
| 262 | * Return the SurfaceHolder providing access and control over this |
| 263 | * SurfaceView's underlying surface. |
Igor Murashkin | a86ab640 | 2013-08-30 12:58:36 -0700 | [diff] [blame] | 264 | * |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 265 | * @return SurfaceHolder The holder of the surface. |
| 266 | */ |
| 267 | public SurfaceHolder getHolder() { |
| 268 | return mSurfaceHolder; |
| 269 | } |
| 270 | |
Robert Carr | 414ebc6 | 2017-04-12 12:01:00 -0700 | [diff] [blame] | 271 | private void updateRequestedVisibility() { |
| 272 | mRequestedVisible = mViewVisibility && mWindowVisibility && !mWindowStopped; |
| 273 | } |
| 274 | |
Vishnu Nair | 3b037f7 | 2019-07-22 12:55:58 -0700 | [diff] [blame] | 275 | private void setWindowStopped(boolean stopped) { |
Robert Carr | 414ebc6 | 2017-04-12 12:01:00 -0700 | [diff] [blame] | 276 | mWindowStopped = stopped; |
| 277 | updateRequestedVisibility(); |
| 278 | updateSurface(); |
| 279 | } |
| 280 | |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 281 | @Override |
| 282 | protected void onAttachedToWindow() { |
| 283 | super.onAttachedToWindow(); |
Robert Carr | 414ebc6 | 2017-04-12 12:01:00 -0700 | [diff] [blame] | 284 | |
Vishnu Nair | 3b037f7 | 2019-07-22 12:55:58 -0700 | [diff] [blame] | 285 | getViewRootImpl().addSurfaceChangedCallback(this); |
Robert Carr | 00177cc | 2017-05-15 15:49:17 -0700 | [diff] [blame] | 286 | mWindowStopped = false; |
Robert Carr | 414ebc6 | 2017-04-12 12:01:00 -0700 | [diff] [blame] | 287 | |
Mathias Agopian | 6b7f1a6 | 2009-09-09 18:32:34 -0700 | [diff] [blame] | 288 | mViewVisibility = getVisibility() == VISIBLE; |
Robert Carr | 414ebc6 | 2017-04-12 12:01:00 -0700 | [diff] [blame] | 289 | updateRequestedVisibility(); |
Romain Guy | 01d5edc | 2011-01-28 11:28:53 -0800 | [diff] [blame] | 290 | |
Robert Carr | 8508bb2 | 2017-03-27 15:46:27 -0700 | [diff] [blame] | 291 | mAttachedToWindow = true; |
Karthik Ravi Shankar | a59c3a5 | 2017-09-11 17:36:25 -0700 | [diff] [blame] | 292 | mParent.requestTransparentRegion(SurfaceView.this); |
Romain Guy | 01d5edc | 2011-01-28 11:28:53 -0800 | [diff] [blame] | 293 | if (!mGlobalListenersAdded) { |
| 294 | ViewTreeObserver observer = getViewTreeObserver(); |
| 295 | observer.addOnScrollChangedListener(mScrollChangedListener); |
| 296 | observer.addOnPreDrawListener(mDrawListener); |
| 297 | mGlobalListenersAdded = true; |
| 298 | } |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | @Override |
| 302 | protected void onWindowVisibilityChanged(int visibility) { |
| 303 | super.onWindowVisibilityChanged(visibility); |
Mathias Agopian | 6b7f1a6 | 2009-09-09 18:32:34 -0700 | [diff] [blame] | 304 | mWindowVisibility = visibility == VISIBLE; |
Robert Carr | 414ebc6 | 2017-04-12 12:01:00 -0700 | [diff] [blame] | 305 | updateRequestedVisibility(); |
Robert Carr | d5c7dd6 | 2017-03-08 10:39:30 -0800 | [diff] [blame] | 306 | updateSurface(); |
Mathias Agopian | 6b7f1a6 | 2009-09-09 18:32:34 -0700 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | @Override |
| 310 | public void setVisibility(int visibility) { |
| 311 | super.setVisibility(visibility); |
| 312 | mViewVisibility = visibility == VISIBLE; |
Robert Carr | 414ebc6 | 2017-04-12 12:01:00 -0700 | [diff] [blame] | 313 | boolean newRequestedVisible = mWindowVisibility && mViewVisibility && !mWindowStopped; |
Mathias Agopian | cbeb332 | 2012-05-12 19:57:07 -0700 | [diff] [blame] | 314 | if (newRequestedVisible != mRequestedVisible) { |
| 315 | // our base class (View) invalidates the layout only when |
| 316 | // we go from/to the GONE state. However, SurfaceView needs |
| 317 | // to request a re-layout when the visibility changes at all. |
| 318 | // This is needed because the transparent region is computed |
| 319 | // as part of the layout phase, and it changes (obviously) when |
| 320 | // the visibility changes. |
| 321 | requestLayout(); |
| 322 | } |
| 323 | mRequestedVisible = newRequestedVisible; |
Robert Carr | d5c7dd6 | 2017-03-08 10:39:30 -0800 | [diff] [blame] | 324 | updateSurface(); |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 325 | } |
Romain Guy | afc3e11 | 2010-06-07 17:04:33 -0700 | [diff] [blame] | 326 | |
Issei Suzuki | 006b71f | 2019-06-17 15:56:57 +0200 | [diff] [blame] | 327 | /** |
| 328 | * Make alpha value of this view reflect onto the surface. This can only be called from at most |
| 329 | * one SurfaceView within a view tree. |
| 330 | * |
| 331 | * <p class="note"><strong>Note:</strong> Alpha value of the view is ignored and the underlying |
| 332 | * surface is rendered opaque by default.</p> |
| 333 | * |
| 334 | * @hide |
| 335 | */ |
| 336 | public void setUseAlpha() { |
| 337 | if (!mUseAlpha) { |
| 338 | mUseAlpha = true; |
| 339 | updateSurfaceAlpha(); |
| 340 | } |
| 341 | } |
| 342 | |
| 343 | @Override |
| 344 | public void setAlpha(float alpha) { |
| 345 | // Sets the opacity of the view to a value, where 0 means the view is completely transparent |
| 346 | // and 1 means the view is completely opaque. |
| 347 | // |
| 348 | // Note: Alpha value of this view is ignored by default. To enable alpha blending, you need |
| 349 | // to call setUseAlpha() as well. |
| 350 | // This view doesn't support translucent opacity if the view is located z-below, since the |
| 351 | // logic to punch a hole in the view hierarchy cannot handle such case. See also |
| 352 | // #clearSurfaceViewPort(Canvas) |
| 353 | if (DEBUG) { |
| 354 | Log.d(TAG, System.identityHashCode(this) |
| 355 | + " setAlpha: mUseAlpha = " + mUseAlpha + " alpha=" + alpha); |
| 356 | } |
| 357 | super.setAlpha(alpha); |
| 358 | updateSurfaceAlpha(); |
| 359 | } |
| 360 | |
| 361 | private float getFixedAlpha() { |
| 362 | // Compute alpha value to be set on the underlying surface. |
| 363 | final float alpha = getAlpha(); |
| 364 | return mUseAlpha && (mSubLayer > 0 || alpha == 0f) ? alpha : 1f; |
| 365 | } |
| 366 | |
| 367 | private void updateSurfaceAlpha() { |
| 368 | if (!mUseAlpha) { |
| 369 | if (DEBUG) { |
| 370 | Log.d(TAG, System.identityHashCode(this) |
| 371 | + " updateSurfaceAlpha: setUseAlpha() is not called, ignored."); |
| 372 | } |
| 373 | return; |
| 374 | } |
| 375 | final float viewAlpha = getAlpha(); |
| 376 | if (mSubLayer < 0 && 0f < viewAlpha && viewAlpha < 1f) { |
| 377 | Log.w(TAG, System.identityHashCode(this) |
| 378 | + " updateSurfaceAlpha:" |
| 379 | + " translucent color is not supported for a surface placed z-below."); |
| 380 | } |
| 381 | if (!mHaveFrame) { |
| 382 | if (DEBUG) { |
| 383 | Log.d(TAG, System.identityHashCode(this) |
| 384 | + " updateSurfaceAlpha: has no surface."); |
| 385 | } |
| 386 | return; |
| 387 | } |
| 388 | final ViewRootImpl viewRoot = getViewRootImpl(); |
| 389 | if (viewRoot == null) { |
| 390 | if (DEBUG) { |
| 391 | Log.d(TAG, System.identityHashCode(this) |
| 392 | + " updateSurfaceAlpha: ViewRootImpl not available."); |
| 393 | } |
| 394 | return; |
| 395 | } |
| 396 | if (mSurfaceControl == null) { |
| 397 | if (DEBUG) { |
| 398 | Log.d(TAG, System.identityHashCode(this) |
| 399 | + "updateSurfaceAlpha:" |
| 400 | + " surface is not yet created, or already released."); |
| 401 | } |
| 402 | return; |
| 403 | } |
| 404 | final Surface parent = viewRoot.mSurface; |
| 405 | if (parent == null || !parent.isValid()) { |
| 406 | if (DEBUG) { |
| 407 | Log.d(TAG, System.identityHashCode(this) |
| 408 | + " updateSurfaceAlpha: ViewRootImpl has no valid surface"); |
| 409 | } |
| 410 | return; |
| 411 | } |
| 412 | final float alpha = getFixedAlpha(); |
| 413 | if (alpha != mSurfaceAlpha) { |
| 414 | if (isHardwareAccelerated()) { |
| 415 | /* |
| 416 | * Schedule a callback that reflects an alpha value onto the underlying surfaces. |
| 417 | * This gets called on a RenderThread worker thread, so members accessed here must |
| 418 | * be protected by a lock. |
| 419 | */ |
Robert Carr | 6d16c8c | 2020-02-21 12:04:31 -0800 | [diff] [blame] | 420 | final boolean useBLAST = viewRoot.useBLAST(); |
Issei Suzuki | 006b71f | 2019-06-17 15:56:57 +0200 | [diff] [blame] | 421 | viewRoot.registerRtFrameCallback(frame -> { |
| 422 | try { |
Robert Carr | 85bb940 | 2019-12-19 01:33:24 -0800 | [diff] [blame] | 423 | final SurfaceControl.Transaction t = useBLAST ? |
| 424 | viewRoot.getBLASTSyncTransaction() : new SurfaceControl.Transaction(); |
Issei Suzuki | 006b71f | 2019-06-17 15:56:57 +0200 | [diff] [blame] | 425 | synchronized (mSurfaceControlLock) { |
| 426 | if (!parent.isValid()) { |
| 427 | if (DEBUG) { |
| 428 | Log.d(TAG, System.identityHashCode(this) |
| 429 | + " updateSurfaceAlpha RT:" |
| 430 | + " ViewRootImpl has no valid surface"); |
| 431 | } |
| 432 | return; |
| 433 | } |
| 434 | if (mSurfaceControl == null) { |
| 435 | if (DEBUG) { |
| 436 | Log.d(TAG, System.identityHashCode(this) |
| 437 | + "updateSurfaceAlpha RT:" |
| 438 | + " mSurfaceControl has already released"); |
| 439 | } |
| 440 | return; |
| 441 | } |
| 442 | if (DEBUG) { |
| 443 | Log.d(TAG, System.identityHashCode(this) |
| 444 | + " updateSurfaceAlpha RT: set alpha=" + alpha); |
| 445 | } |
| 446 | t.setAlpha(mSurfaceControl, alpha); |
Robert Carr | 85bb940 | 2019-12-19 01:33:24 -0800 | [diff] [blame] | 447 | if (!useBLAST) { |
Robert Carr | cef9a28 | 2020-01-14 09:08:05 -0800 | [diff] [blame] | 448 | t.deferTransactionUntil(mSurfaceControl, |
| 449 | viewRoot.getRenderSurfaceControl(), frame); |
Robert Carr | 85bb940 | 2019-12-19 01:33:24 -0800 | [diff] [blame] | 450 | } |
Issei Suzuki | 006b71f | 2019-06-17 15:56:57 +0200 | [diff] [blame] | 451 | } |
| 452 | // It's possible that mSurfaceControl is released in the UI thread before |
| 453 | // the transaction completes. If that happens, an exception is thrown, which |
| 454 | // must be caught immediately. |
| 455 | t.apply(); |
| 456 | } catch (Exception e) { |
| 457 | Log.e(TAG, System.identityHashCode(this) |
| 458 | + "updateSurfaceAlpha RT: Exception during surface transaction", e); |
| 459 | } |
| 460 | }); |
| 461 | damageInParent(); |
| 462 | } else { |
| 463 | if (DEBUG) { |
| 464 | Log.d(TAG, System.identityHashCode(this) |
| 465 | + " updateSurfaceAlpha: set alpha=" + alpha); |
| 466 | } |
chaviw | 619da69 | 2019-06-10 15:39:40 -0700 | [diff] [blame] | 467 | mTmpTransaction.setAlpha(mSurfaceControl, alpha).apply(); |
Issei Suzuki | 006b71f | 2019-06-17 15:56:57 +0200 | [diff] [blame] | 468 | } |
| 469 | mSurfaceAlpha = alpha; |
| 470 | } |
| 471 | } |
| 472 | |
Robert Carr | b53670a | 2017-05-25 18:20:49 -0700 | [diff] [blame] | 473 | private void performDrawFinished() { |
| 474 | if (mPendingReportDraws > 0) { |
| 475 | mDrawFinished = true; |
| 476 | if (mAttachedToWindow) { |
Pengfei Zhao | 1561720 | 2019-02-28 17:07:41 +0800 | [diff] [blame] | 477 | mParent.requestTransparentRegion(SurfaceView.this); |
Robert Carr | b53670a | 2017-05-25 18:20:49 -0700 | [diff] [blame] | 478 | notifyDrawFinished(); |
| 479 | invalidate(); |
| 480 | } |
| 481 | } else { |
| 482 | Log.e(TAG, System.identityHashCode(this) + "finished drawing" |
| 483 | + " but no pending report draw (extra call" |
| 484 | + " to draw completion runnable?)"); |
| 485 | } |
| 486 | } |
| 487 | |
Robert Carr | 8508bb2 | 2017-03-27 15:46:27 -0700 | [diff] [blame] | 488 | void notifyDrawFinished() { |
| 489 | ViewRootImpl viewRoot = getViewRootImpl(); |
| 490 | if (viewRoot != null) { |
| 491 | viewRoot.pendingDrawFinished(); |
| 492 | } |
| 493 | mPendingReportDraws--; |
| 494 | } |
| 495 | |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 496 | @Override |
John Reck | 77e4a52 | 2014-10-01 10:38:07 -0700 | [diff] [blame] | 497 | protected void onDetachedFromWindow() { |
Lucas Dupin | 0c20734 | 2017-04-14 19:47:23 -0700 | [diff] [blame] | 498 | ViewRootImpl viewRoot = getViewRootImpl(); |
| 499 | // It's possible to create a SurfaceView using the default constructor and never |
| 500 | // attach it to a view hierarchy, this is a common use case when dealing with |
| 501 | // OpenGL. A developer will probably create a new GLSurfaceView, and let it manage |
| 502 | // the lifecycle. Instead of attaching it to a view, he/she can just pass |
| 503 | // the SurfaceHolder forward, most live wallpapers do it. |
| 504 | if (viewRoot != null) { |
Vishnu Nair | 3b037f7 | 2019-07-22 12:55:58 -0700 | [diff] [blame] | 505 | viewRoot.removeSurfaceChangedCallback(this); |
Lucas Dupin | 0c20734 | 2017-04-14 19:47:23 -0700 | [diff] [blame] | 506 | } |
Robert Carr | 414ebc6 | 2017-04-12 12:01:00 -0700 | [diff] [blame] | 507 | |
Robert Carr | 8508bb2 | 2017-03-27 15:46:27 -0700 | [diff] [blame] | 508 | mAttachedToWindow = false; |
Romain Guy | 01d5edc | 2011-01-28 11:28:53 -0800 | [diff] [blame] | 509 | if (mGlobalListenersAdded) { |
| 510 | ViewTreeObserver observer = getViewTreeObserver(); |
| 511 | observer.removeOnScrollChangedListener(mScrollChangedListener); |
| 512 | observer.removeOnPreDrawListener(mDrawListener); |
| 513 | mGlobalListenersAdded = false; |
| 514 | } |
| 515 | |
Robert Carr | 8508bb2 | 2017-03-27 15:46:27 -0700 | [diff] [blame] | 516 | while (mPendingReportDraws > 0) { |
| 517 | notifyDrawFinished(); |
| 518 | } |
| 519 | |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 520 | mRequestedVisible = false; |
Wonsik Kim | 5aec7b9 | 2017-03-07 17:15:50 -0800 | [diff] [blame] | 521 | |
Robert Carr | d5c7dd6 | 2017-03-08 10:39:30 -0800 | [diff] [blame] | 522 | updateSurface(); |
Issei Suzuki | 006b71f | 2019-06-17 15:56:57 +0200 | [diff] [blame] | 523 | releaseSurfaces(); |
Robert Carr | 414ebc6 | 2017-04-12 12:01:00 -0700 | [diff] [blame] | 524 | |
Robert Carr | 5af7d62 | 2020-03-17 12:04:20 -0700 | [diff] [blame] | 525 | // We don't release this as part of releaseSurfaces as |
| 526 | // that is also called on transient visibility changes. We can't |
| 527 | // recreate this Surface, so only release it when we are fully |
| 528 | // detached. |
| 529 | if (mSurfacePackage != null) { |
| 530 | mSurfacePackage.release(); |
| 531 | mSurfacePackage = null; |
| 532 | } |
| 533 | |
| 534 | mHaveFrame = false; |
John Reck | 77e4a52 | 2014-10-01 10:38:07 -0700 | [diff] [blame] | 535 | super.onDetachedFromWindow(); |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 536 | } |
| 537 | |
| 538 | @Override |
| 539 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
Dianne Hackborn | 189ee18 | 2010-12-02 21:48:53 -0800 | [diff] [blame] | 540 | int width = mRequestedWidth >= 0 |
| 541 | ? resolveSizeAndState(mRequestedWidth, widthMeasureSpec, 0) |
| 542 | : getDefaultSize(0, widthMeasureSpec); |
| 543 | int height = mRequestedHeight >= 0 |
| 544 | ? resolveSizeAndState(mRequestedHeight, heightMeasureSpec, 0) |
| 545 | : getDefaultSize(0, heightMeasureSpec); |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 546 | setMeasuredDimension(width, height); |
| 547 | } |
Igor Murashkin | a86ab640 | 2013-08-30 12:58:36 -0700 | [diff] [blame] | 548 | |
Mathias Agopian | ef11530 | 2010-10-04 20:15:08 -0700 | [diff] [blame] | 549 | /** @hide */ |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 550 | @Override |
Mathew Inwood | a570dee | 2018-08-17 14:56:00 +0100 | [diff] [blame] | 551 | @UnsupportedAppUsage |
Mathias Agopian | 995bb9d | 2010-10-04 17:13:15 -0700 | [diff] [blame] | 552 | protected boolean setFrame(int left, int top, int right, int bottom) { |
| 553 | boolean result = super.setFrame(left, top, right, bottom); |
Robert Carr | d5c7dd6 | 2017-03-08 10:39:30 -0800 | [diff] [blame] | 554 | updateSurface(); |
Mathias Agopian | 995bb9d | 2010-10-04 17:13:15 -0700 | [diff] [blame] | 555 | return result; |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 556 | } |
| 557 | |
| 558 | @Override |
| 559 | public boolean gatherTransparentRegion(Region region) { |
Robert Carr | 3ca12be7 | 2017-05-22 14:57:48 -0700 | [diff] [blame] | 560 | if (isAboveParent() || !mDrawFinished) { |
Dianne Hackborn | 1cd403e | 2009-09-14 22:29:14 -0700 | [diff] [blame] | 561 | return super.gatherTransparentRegion(region); |
| 562 | } |
Igor Murashkin | a86ab640 | 2013-08-30 12:58:36 -0700 | [diff] [blame] | 563 | |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 564 | boolean opaque = true; |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 565 | if ((mPrivateFlags & PFLAG_SKIP_DRAW) == 0) { |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 566 | // this view draws, remove it from the transparent region |
| 567 | opaque = super.gatherTransparentRegion(region); |
| 568 | } else if (region != null) { |
| 569 | int w = getWidth(); |
| 570 | int h = getHeight(); |
| 571 | if (w>0 && h>0) { |
| 572 | getLocationInWindow(mLocation); |
| 573 | // otherwise, punch a hole in the whole hierarchy |
| 574 | int l = mLocation[0]; |
| 575 | int t = mLocation[1]; |
| 576 | region.op(l, t, l+w, t+h, Region.Op.UNION); |
| 577 | } |
| 578 | } |
| 579 | if (PixelFormat.formatHasAlpha(mRequestedFormat)) { |
| 580 | opaque = false; |
| 581 | } |
| 582 | return opaque; |
| 583 | } |
| 584 | |
| 585 | @Override |
| 586 | public void draw(Canvas canvas) { |
Robert Carr | d5c7dd6 | 2017-03-08 10:39:30 -0800 | [diff] [blame] | 587 | if (mDrawFinished && !isAboveParent()) { |
Dianne Hackborn | 1cd403e | 2009-09-14 22:29:14 -0700 | [diff] [blame] | 588 | // draw() is not called when SKIP_DRAW is set |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 589 | if ((mPrivateFlags & PFLAG_SKIP_DRAW) == 0) { |
Dianne Hackborn | 1cd403e | 2009-09-14 22:29:14 -0700 | [diff] [blame] | 590 | // punch a whole in the view-hierarchy below us |
Mark Renouf | 34d04f3 | 2019-05-13 15:53:18 -0400 | [diff] [blame] | 591 | clearSurfaceViewPort(canvas); |
Dianne Hackborn | 1cd403e | 2009-09-14 22:29:14 -0700 | [diff] [blame] | 592 | } |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 593 | } |
| 594 | super.draw(canvas); |
| 595 | } |
| 596 | |
| 597 | @Override |
| 598 | protected void dispatchDraw(Canvas canvas) { |
Robert Carr | d5c7dd6 | 2017-03-08 10:39:30 -0800 | [diff] [blame] | 599 | if (mDrawFinished && !isAboveParent()) { |
| 600 | // draw() is not called when SKIP_DRAW is set |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 601 | if ((mPrivateFlags & PFLAG_SKIP_DRAW) == PFLAG_SKIP_DRAW) { |
Dianne Hackborn | 1cd403e | 2009-09-14 22:29:14 -0700 | [diff] [blame] | 602 | // punch a whole in the view-hierarchy below us |
Mark Renouf | 34d04f3 | 2019-05-13 15:53:18 -0400 | [diff] [blame] | 603 | clearSurfaceViewPort(canvas); |
Dianne Hackborn | 1cd403e | 2009-09-14 22:29:14 -0700 | [diff] [blame] | 604 | } |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 605 | } |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 606 | super.dispatchDraw(canvas); |
| 607 | } |
| 608 | |
Mark Renouf | e574ad0 | 2019-08-12 16:22:15 -0400 | [diff] [blame] | 609 | /** |
| 610 | * Control whether the surface is clipped to the same bounds as the View. If true, then |
| 611 | * the bounds set by {@link #setClipBounds(Rect)} are applied to the surface as window-crop. |
| 612 | * |
| 613 | * @param enabled whether to enable surface clipping |
| 614 | * @hide |
| 615 | */ |
| 616 | public void setEnableSurfaceClipping(boolean enabled) { |
| 617 | mClipSurfaceToBounds = enabled; |
| 618 | invalidate(); |
| 619 | } |
| 620 | |
| 621 | @Override |
| 622 | public void setClipBounds(Rect clipBounds) { |
| 623 | super.setClipBounds(clipBounds); |
| 624 | |
| 625 | if (!mClipSurfaceToBounds) { |
| 626 | return; |
| 627 | } |
| 628 | |
| 629 | // When cornerRadius is non-zero, a draw() is required to update |
| 630 | // the viewport (rounding the corners of the clipBounds). |
| 631 | if (mCornerRadius > 0f && !isAboveParent()) { |
| 632 | invalidate(); |
| 633 | } |
| 634 | |
| 635 | if (mSurfaceControl != null) { |
| 636 | if (mClipBounds != null) { |
| 637 | mTmpRect.set(mClipBounds); |
| 638 | } else { |
| 639 | mTmpRect.set(0, 0, mSurfaceWidth, mSurfaceHeight); |
| 640 | } |
| 641 | SyncRtSurfaceTransactionApplier applier = new SyncRtSurfaceTransactionApplier(this); |
Vishnu Nair | 2ed39d8 | 2020-06-17 15:43:13 -0700 | [diff] [blame] | 642 | applier.scheduleApply( |
Mark Renouf | e574ad0 | 2019-08-12 16:22:15 -0400 | [diff] [blame] | 643 | new SyncRtSurfaceTransactionApplier.SurfaceParams.Builder(mSurfaceControl) |
| 644 | .withWindowCrop(mTmpRect) |
| 645 | .build()); |
| 646 | } |
| 647 | } |
| 648 | |
Mark Renouf | 34d04f3 | 2019-05-13 15:53:18 -0400 | [diff] [blame] | 649 | private void clearSurfaceViewPort(Canvas canvas) { |
| 650 | if (mCornerRadius > 0f) { |
| 651 | canvas.getClipBounds(mTmpRect); |
Mark Renouf | e574ad0 | 2019-08-12 16:22:15 -0400 | [diff] [blame] | 652 | if (mClipSurfaceToBounds && mClipBounds != null) { |
| 653 | mTmpRect.intersect(mClipBounds); |
| 654 | } |
Mark Renouf | 34d04f3 | 2019-05-13 15:53:18 -0400 | [diff] [blame] | 655 | canvas.drawRoundRect(mTmpRect.left, mTmpRect.top, mTmpRect.right, mTmpRect.bottom, |
| 656 | mCornerRadius, mCornerRadius, mRoundedViewportPaint); |
| 657 | } else { |
| 658 | canvas.drawColor(0, PorterDuff.Mode.CLEAR); |
| 659 | } |
| 660 | } |
| 661 | |
| 662 | /** |
| 663 | * Sets the corner radius for the SurfaceView. This will round both the corners of the |
| 664 | * underlying surface, as well as the corners of the hole created to expose the surface. |
| 665 | * |
| 666 | * @param cornerRadius the new radius of the corners in pixels |
| 667 | * @hide |
| 668 | */ |
| 669 | public void setCornerRadius(float cornerRadius) { |
| 670 | mCornerRadius = cornerRadius; |
| 671 | if (mCornerRadius > 0f && mRoundedViewportPaint == null) { |
| 672 | mRoundedViewportPaint = new Paint(Paint.ANTI_ALIAS_FLAG); |
| 673 | mRoundedViewportPaint.setBlendMode(BlendMode.CLEAR); |
| 674 | mRoundedViewportPaint.setColor(0); |
| 675 | } |
| 676 | invalidate(); |
| 677 | } |
| 678 | |
Dianne Hackborn | c4d5d02 | 2009-05-21 17:32:42 -0700 | [diff] [blame] | 679 | /** |
Mark Renouf | e574ad0 | 2019-08-12 16:22:15 -0400 | [diff] [blame] | 680 | * Returns the corner radius for the SurfaceView. |
| 681 | |
| 682 | * @return the radius of the corners in pixels |
| 683 | * @hide |
| 684 | */ |
| 685 | public float getCornerRadius() { |
| 686 | return mCornerRadius; |
| 687 | } |
| 688 | |
| 689 | /** |
Dianne Hackborn | 29e4a3c | 2009-09-30 22:35:40 -0700 | [diff] [blame] | 690 | * Control whether the surface view's surface is placed on top of another |
| 691 | * regular surface view in the window (but still behind the window itself). |
| 692 | * This is typically used to place overlays on top of an underlying media |
| 693 | * surface view. |
Igor Murashkin | a86ab640 | 2013-08-30 12:58:36 -0700 | [diff] [blame] | 694 | * |
Dianne Hackborn | 29e4a3c | 2009-09-30 22:35:40 -0700 | [diff] [blame] | 695 | * <p>Note that this must be set before the surface view's containing |
| 696 | * window is attached to the window manager. |
Igor Murashkin | a86ab640 | 2013-08-30 12:58:36 -0700 | [diff] [blame] | 697 | * |
Dianne Hackborn | 29e4a3c | 2009-09-30 22:35:40 -0700 | [diff] [blame] | 698 | * <p>Calling this overrides any previous call to {@link #setZOrderOnTop}. |
| 699 | */ |
| 700 | public void setZOrderMediaOverlay(boolean isMediaOverlay) { |
Robert Carr | d5c7dd6 | 2017-03-08 10:39:30 -0800 | [diff] [blame] | 701 | mSubLayer = isMediaOverlay |
| 702 | ? APPLICATION_MEDIA_OVERLAY_SUBLAYER : APPLICATION_MEDIA_SUBLAYER; |
Dianne Hackborn | 29e4a3c | 2009-09-30 22:35:40 -0700 | [diff] [blame] | 703 | } |
Igor Murashkin | a86ab640 | 2013-08-30 12:58:36 -0700 | [diff] [blame] | 704 | |
Dianne Hackborn | 29e4a3c | 2009-09-30 22:35:40 -0700 | [diff] [blame] | 705 | /** |
Dianne Hackborn | 1cd403e | 2009-09-14 22:29:14 -0700 | [diff] [blame] | 706 | * Control whether the surface view's surface is placed on top of its |
| 707 | * window. Normally it is placed behind the window, to allow it to |
| 708 | * (for the most part) appear to composite with the views in the |
| 709 | * hierarchy. By setting this, you cause it to be placed above the |
| 710 | * window. This means that none of the contents of the window this |
| 711 | * SurfaceView is in will be visible on top of its surface. |
Igor Murashkin | a86ab640 | 2013-08-30 12:58:36 -0700 | [diff] [blame] | 712 | * |
Dianne Hackborn | 1cd403e | 2009-09-14 22:29:14 -0700 | [diff] [blame] | 713 | * <p>Note that this must be set before the surface view's containing |
Svet Ganov | a57dadd | 2020-03-18 23:30:16 -0700 | [diff] [blame] | 714 | * window is attached to the window manager. If you target {@link Build.VERSION_CODES#R} |
| 715 | * the Z ordering can be changed dynamically if the backing surface is |
| 716 | * created, otherwise it would be applied at surface construction time. |
Igor Murashkin | a86ab640 | 2013-08-30 12:58:36 -0700 | [diff] [blame] | 717 | * |
Dianne Hackborn | 29e4a3c | 2009-09-30 22:35:40 -0700 | [diff] [blame] | 718 | * <p>Calling this overrides any previous call to {@link #setZOrderMediaOverlay}. |
Svet Ganov | a57dadd | 2020-03-18 23:30:16 -0700 | [diff] [blame] | 719 | * |
| 720 | * @param onTop Whether to show the surface on top of this view's window. |
Dianne Hackborn | 1cd403e | 2009-09-14 22:29:14 -0700 | [diff] [blame] | 721 | */ |
Dianne Hackborn | 29e4a3c | 2009-09-30 22:35:40 -0700 | [diff] [blame] | 722 | public void setZOrderOnTop(boolean onTop) { |
Svet Ganov | a57dadd | 2020-03-18 23:30:16 -0700 | [diff] [blame] | 723 | // In R and above we allow dynamic layer changes. |
| 724 | final boolean allowDynamicChange = getContext().getApplicationInfo().targetSdkVersion |
| 725 | > Build.VERSION_CODES.Q; |
| 726 | setZOrderedOnTop(onTop, allowDynamicChange); |
| 727 | } |
| 728 | |
| 729 | /** |
| 730 | * @return Whether the surface backing this view appears on top of its parent. |
| 731 | * |
| 732 | * @hide |
| 733 | */ |
| 734 | public boolean isZOrderedOnTop() { |
| 735 | return mSubLayer > 0; |
| 736 | } |
| 737 | |
| 738 | /** |
| 739 | * Controls whether the surface view's surface is placed on top of its |
| 740 | * window. Normally it is placed behind the window, to allow it to |
| 741 | * (for the most part) appear to composite with the views in the |
| 742 | * hierarchy. By setting this, you cause it to be placed above the |
| 743 | * window. This means that none of the contents of the window this |
| 744 | * SurfaceView is in will be visible on top of its surface. |
| 745 | * |
| 746 | * <p>Calling this overrides any previous call to {@link #setZOrderMediaOverlay}. |
| 747 | * |
| 748 | * @param onTop Whether to show the surface on top of this view's window. |
| 749 | * @param allowDynamicChange Whether this can happen after the surface is created. |
| 750 | * @return Whether the Z ordering changed. |
| 751 | * |
| 752 | * @hide |
| 753 | */ |
| 754 | public boolean setZOrderedOnTop(boolean onTop, boolean allowDynamicChange) { |
| 755 | final int subLayer; |
Derek Sollenberger | ecde72f | 2010-03-01 13:44:42 -0500 | [diff] [blame] | 756 | if (onTop) { |
Svet Ganov | a57dadd | 2020-03-18 23:30:16 -0700 | [diff] [blame] | 757 | subLayer = APPLICATION_PANEL_SUBLAYER; |
Derek Sollenberger | ecde72f | 2010-03-01 13:44:42 -0500 | [diff] [blame] | 758 | } else { |
Svet Ganov | a57dadd | 2020-03-18 23:30:16 -0700 | [diff] [blame] | 759 | subLayer = APPLICATION_MEDIA_SUBLAYER; |
Derek Sollenberger | ecde72f | 2010-03-01 13:44:42 -0500 | [diff] [blame] | 760 | } |
Svet Ganov | a57dadd | 2020-03-18 23:30:16 -0700 | [diff] [blame] | 761 | if (mSubLayer == subLayer) { |
| 762 | return false; |
| 763 | } |
| 764 | mSubLayer = subLayer; |
| 765 | |
| 766 | if (!allowDynamicChange) { |
| 767 | return false; |
| 768 | } |
| 769 | if (mSurfaceControl == null) { |
| 770 | return true; |
| 771 | } |
| 772 | final ViewRootImpl viewRoot = getViewRootImpl(); |
| 773 | if (viewRoot == null) { |
| 774 | return true; |
| 775 | } |
| 776 | final Surface parent = viewRoot.mSurface; |
| 777 | if (parent == null || !parent.isValid()) { |
| 778 | return true; |
| 779 | } |
| 780 | |
| 781 | /* |
| 782 | * Schedule a callback that reflects an alpha value onto the underlying surfaces. |
| 783 | * This gets called on a RenderThread worker thread, so members accessed here must |
| 784 | * be protected by a lock. |
| 785 | */ |
| 786 | final boolean useBLAST = viewRoot.useBLAST(); |
| 787 | viewRoot.registerRtFrameCallback(frame -> { |
| 788 | try { |
| 789 | final SurfaceControl.Transaction t = useBLAST |
| 790 | ? viewRoot.getBLASTSyncTransaction() |
| 791 | : new SurfaceControl.Transaction(); |
| 792 | synchronized (mSurfaceControlLock) { |
| 793 | if (!parent.isValid() || mSurfaceControl == null) { |
| 794 | return; |
| 795 | } |
| 796 | updateRelativeZ(t); |
| 797 | if (!useBLAST) { |
| 798 | t.deferTransactionUntil(mSurfaceControl, |
| 799 | viewRoot.getRenderSurfaceControl(), frame); |
| 800 | } |
| 801 | } |
| 802 | // It's possible that mSurfaceControl is released in the UI thread before |
| 803 | // the transaction completes. If that happens, an exception is thrown, which |
| 804 | // must be caught immediately. |
| 805 | t.apply(); |
| 806 | } catch (Exception e) { |
| 807 | Log.e(TAG, System.identityHashCode(this) |
| 808 | + "setZOrderOnTop RT: Exception during surface transaction", e); |
| 809 | } |
| 810 | }); |
| 811 | |
| 812 | invalidate(); |
| 813 | |
| 814 | return true; |
Dianne Hackborn | 1cd403e | 2009-09-14 22:29:14 -0700 | [diff] [blame] | 815 | } |
Jeff Brown | f0681b3 | 2012-10-23 17:35:57 -0700 | [diff] [blame] | 816 | |
| 817 | /** |
| 818 | * Control whether the surface view's content should be treated as secure, |
| 819 | * preventing it from appearing in screenshots or from being viewed on |
| 820 | * non-secure displays. |
| 821 | * |
| 822 | * <p>Note that this must be set before the surface view's containing |
| 823 | * window is attached to the window manager. |
| 824 | * |
| 825 | * <p>See {@link android.view.Display#FLAG_SECURE} for details. |
| 826 | * |
| 827 | * @param isSecure True if the surface view is secure. |
| 828 | */ |
| 829 | public void setSecure(boolean isSecure) { |
| 830 | if (isSecure) { |
Robert Carr | d5c7dd6 | 2017-03-08 10:39:30 -0800 | [diff] [blame] | 831 | mSurfaceFlags |= SurfaceControl.SECURE; |
Jeff Brown | f0681b3 | 2012-10-23 17:35:57 -0700 | [diff] [blame] | 832 | } else { |
Robert Carr | d5c7dd6 | 2017-03-08 10:39:30 -0800 | [diff] [blame] | 833 | mSurfaceFlags &= ~SurfaceControl.SECURE; |
Jeff Brown | f0681b3 | 2012-10-23 17:35:57 -0700 | [diff] [blame] | 834 | } |
| 835 | } |
| 836 | |
Robert Carr | 5523555 | 2017-06-02 14:21:03 -0700 | [diff] [blame] | 837 | private void updateOpaqueFlag() { |
Robert Carr | 851e7e4 | 2017-06-06 14:04:50 -0700 | [diff] [blame] | 838 | if (!PixelFormat.formatHasAlpha(mRequestedFormat)) { |
Robert Carr | 5523555 | 2017-06-02 14:21:03 -0700 | [diff] [blame] | 839 | mSurfaceFlags |= SurfaceControl.OPAQUE; |
| 840 | } else { |
| 841 | mSurfaceFlags &= ~SurfaceControl.OPAQUE; |
| 842 | } |
| 843 | } |
| 844 | |
chaviw | 619da69 | 2019-06-10 15:39:40 -0700 | [diff] [blame] | 845 | private void updateBackgroundVisibility(Transaction t) { |
Robert Carr | b923f54 | 2019-05-13 12:27:51 -0700 | [diff] [blame] | 846 | if (mBackgroundControl == null) { |
| 847 | return; |
| 848 | } |
shawnlin | ba532af | 2020-06-02 17:48:37 +0800 | [diff] [blame] | 849 | if ((mSubLayer < 0) && ((mSurfaceFlags & SurfaceControl.OPAQUE) != 0) |
| 850 | && !mDisableBackgroundLayer) { |
chaviw | 619da69 | 2019-06-10 15:39:40 -0700 | [diff] [blame] | 851 | t.show(mBackgroundControl); |
Robert Carr | b923f54 | 2019-05-13 12:27:51 -0700 | [diff] [blame] | 852 | } else { |
chaviw | 619da69 | 2019-06-10 15:39:40 -0700 | [diff] [blame] | 853 | t.hide(mBackgroundControl); |
Robert Carr | b923f54 | 2019-05-13 12:27:51 -0700 | [diff] [blame] | 854 | } |
| 855 | } |
| 856 | |
Winson Chung | d4a9abd | 2020-03-21 23:01:16 -0700 | [diff] [blame] | 857 | private Transaction updateBackgroundColor(Transaction t) { |
| 858 | final float[] colorComponents = new float[] { Color.red(mBackgroundColor) / 255.f, |
| 859 | Color.green(mBackgroundColor) / 255.f, Color.blue(mBackgroundColor) / 255.f }; |
| 860 | t.setColor(mBackgroundControl, colorComponents); |
| 861 | return t; |
| 862 | } |
chaviw | 619da69 | 2019-06-10 15:39:40 -0700 | [diff] [blame] | 863 | |
Robert Carr | b923f54 | 2019-05-13 12:27:51 -0700 | [diff] [blame] | 864 | private void releaseSurfaces() { |
Robert Carr | 005c63e | 2019-09-20 14:31:24 -0700 | [diff] [blame] | 865 | mSurfaceAlpha = 1f; |
| 866 | |
Issei Suzuki | 006b71f | 2019-06-17 15:56:57 +0200 | [diff] [blame] | 867 | synchronized (mSurfaceControlLock) { |
Robert Carr | de63cc6 | 2019-10-25 13:18:29 -0700 | [diff] [blame] | 868 | mSurface.release(); |
| 869 | |
Robert Carr | 005c63e | 2019-09-20 14:31:24 -0700 | [diff] [blame] | 870 | if (mRtHandlingPositionUpdates) { |
| 871 | mRtReleaseSurfaces = true; |
| 872 | return; |
| 873 | } |
| 874 | |
Issei Suzuki | 006b71f | 2019-06-17 15:56:57 +0200 | [diff] [blame] | 875 | if (mSurfaceControl != null) { |
| 876 | mTmpTransaction.remove(mSurfaceControl); |
| 877 | mSurfaceControl = null; |
| 878 | } |
| 879 | if (mBackgroundControl != null) { |
| 880 | mTmpTransaction.remove(mBackgroundControl); |
| 881 | mBackgroundControl = null; |
| 882 | } |
| 883 | mTmpTransaction.apply(); |
Robert Carr | b923f54 | 2019-05-13 12:27:51 -0700 | [diff] [blame] | 884 | } |
Robert Carr | b923f54 | 2019-05-13 12:27:51 -0700 | [diff] [blame] | 885 | } |
| 886 | |
Youngsang Cho | 9a22f0f | 2014-04-09 22:51:54 +0900 | [diff] [blame] | 887 | /** @hide */ |
Robert Carr | d5c7dd6 | 2017-03-08 10:39:30 -0800 | [diff] [blame] | 888 | protected void updateSurface() { |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 889 | if (!mHaveFrame) { |
Issei Suzuki | 006b71f | 2019-06-17 15:56:57 +0200 | [diff] [blame] | 890 | if (DEBUG) { |
| 891 | Log.d(TAG, System.identityHashCode(this) + " updateSurface: has no frame"); |
| 892 | } |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 893 | return; |
| 894 | } |
Vishnu Nair | 34486ad | 2020-06-10 18:14:20 -0700 | [diff] [blame] | 895 | final ViewRootImpl viewRoot = getViewRootImpl(); |
| 896 | |
| 897 | if (viewRoot == null) { |
| 898 | return; |
| 899 | } |
| 900 | |
| 901 | if (viewRoot.mSurface == null || !viewRoot.mSurface.isValid()) { |
| 902 | notifySurfaceDestroyed(); |
| 903 | releaseSurfaces(); |
Robert Carr | d5c7dd6 | 2017-03-08 10:39:30 -0800 | [diff] [blame] | 904 | return; |
Joe Onorato | 168173a | 2009-08-12 21:40:29 -0700 | [diff] [blame] | 905 | } |
Mitsuru Oshima | 64f5934 | 2009-06-21 00:03:11 -0700 | [diff] [blame] | 906 | |
Issei Suzuki | f041259 | 2019-07-02 12:31:34 +0200 | [diff] [blame] | 907 | final Translator translator = viewRoot.mTranslator; |
| 908 | if (translator != null) { |
| 909 | mSurface.setCompatibilityTranslator(translator); |
Mitsuru Oshima | 38ed7d77 | 2009-07-21 14:39:34 -0700 | [diff] [blame] | 910 | } |
Igor Murashkin | a86ab640 | 2013-08-30 12:58:36 -0700 | [diff] [blame] | 911 | |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 912 | int myWidth = mRequestedWidth; |
| 913 | if (myWidth <= 0) myWidth = getWidth(); |
| 914 | int myHeight = mRequestedHeight; |
| 915 | if (myHeight <= 0) myHeight = getHeight(); |
Mitsuru Oshima | 001a6e52 | 2009-05-11 21:14:03 -0700 | [diff] [blame] | 916 | |
Issei Suzuki | 006b71f | 2019-06-17 15:56:57 +0200 | [diff] [blame] | 917 | final float alpha = getFixedAlpha(); |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 918 | final boolean formatChanged = mFormat != mRequestedFormat; |
Robert Carr | 7c67b7d | 2017-07-06 15:28:34 -0700 | [diff] [blame] | 919 | final boolean visibleChanged = mVisible != mRequestedVisible; |
Issei Suzuki | 006b71f | 2019-06-17 15:56:57 +0200 | [diff] [blame] | 920 | final boolean alphaChanged = mSurfaceAlpha != alpha; |
Robert Carr | 7c67b7d | 2017-07-06 15:28:34 -0700 | [diff] [blame] | 921 | final boolean creating = (mSurfaceControl == null || formatChanged || visibleChanged) |
Robert Carr | d5c7dd6 | 2017-03-08 10:39:30 -0800 | [diff] [blame] | 922 | && mRequestedVisible; |
| 923 | final boolean sizeChanged = mSurfaceWidth != myWidth || mSurfaceHeight != myHeight; |
Robert Carr | d5c7dd6 | 2017-03-08 10:39:30 -0800 | [diff] [blame] | 924 | final boolean windowVisibleChanged = mWindowVisibility != mLastWindowVisibility; |
Robert Carr | 49b593e | 2016-07-11 12:21:18 -0700 | [diff] [blame] | 925 | boolean redrawNeeded = false; |
Robert Carr | bf01a77 | 2020-02-18 08:42:53 -0800 | [diff] [blame] | 926 | getLocationInSurface(mLocation); |
| 927 | final boolean positionChanged = mWindowSpaceLeft != mLocation[0] |
| 928 | || mWindowSpaceTop != mLocation[1]; |
| 929 | final boolean layoutSizeChanged = getWidth() != mScreenRect.width() |
| 930 | || getHeight() != mScreenRect.height(); |
Robert Carr | 49b593e | 2016-07-11 12:21:18 -0700 | [diff] [blame] | 931 | |
Robert Carr | bf01a77 | 2020-02-18 08:42:53 -0800 | [diff] [blame] | 932 | |
| 933 | if (creating || formatChanged || sizeChanged || visibleChanged || |
| 934 | (mUseAlpha && alphaChanged) || windowVisibleChanged || |
| 935 | positionChanged || layoutSizeChanged) { |
John Reck | f648108 | 2016-02-02 15:18:23 -0800 | [diff] [blame] | 936 | getLocationInWindow(mLocation); |
| 937 | |
John Reck | aa6e84f | 2016-06-16 15:36:13 -0700 | [diff] [blame] | 938 | if (DEBUG) Log.i(TAG, System.identityHashCode(this) + " " |
| 939 | + "Changes: creating=" + creating |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 940 | + " format=" + formatChanged + " size=" + sizeChanged |
Issei Suzuki | 006b71f | 2019-06-17 15:56:57 +0200 | [diff] [blame] | 941 | + " visible=" + visibleChanged + " alpha=" + alphaChanged |
| 942 | + " mUseAlpha=" + mUseAlpha |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 943 | + " visible=" + visibleChanged |
Robert Carr | 64aadd0 | 2015-11-06 13:54:20 -0800 | [diff] [blame] | 944 | + " left=" + (mWindowSpaceLeft != mLocation[0]) |
| 945 | + " top=" + (mWindowSpaceTop != mLocation[1])); |
Mitsuru Oshima | 64f5934 | 2009-06-21 00:03:11 -0700 | [diff] [blame] | 946 | |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 947 | try { |
| 948 | final boolean visible = mVisible = mRequestedVisible; |
Robert Carr | 64aadd0 | 2015-11-06 13:54:20 -0800 | [diff] [blame] | 949 | mWindowSpaceLeft = mLocation[0]; |
| 950 | mWindowSpaceTop = mLocation[1]; |
Robert Carr | d5c7dd6 | 2017-03-08 10:39:30 -0800 | [diff] [blame] | 951 | mSurfaceWidth = myWidth; |
| 952 | mSurfaceHeight = myHeight; |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 953 | mFormat = mRequestedFormat; |
Robert Carr | d5c7dd6 | 2017-03-08 10:39:30 -0800 | [diff] [blame] | 954 | mLastWindowVisibility = mWindowVisibility; |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 955 | |
Robert Carr | d5c7dd6 | 2017-03-08 10:39:30 -0800 | [diff] [blame] | 956 | mScreenRect.left = mWindowSpaceLeft; |
| 957 | mScreenRect.top = mWindowSpaceTop; |
| 958 | mScreenRect.right = mWindowSpaceLeft + getWidth(); |
| 959 | mScreenRect.bottom = mWindowSpaceTop + getHeight(); |
Issei Suzuki | f041259 | 2019-07-02 12:31:34 +0200 | [diff] [blame] | 960 | if (translator != null) { |
| 961 | translator.translateRectInAppWindowToScreen(mScreenRect); |
Mitsuru Oshima | 64f5934 | 2009-06-21 00:03:11 -0700 | [diff] [blame] | 962 | } |
Igor Murashkin | a86ab640 | 2013-08-30 12:58:36 -0700 | [diff] [blame] | 963 | |
Issei Suzuki | 006b71f | 2019-06-17 15:56:57 +0200 | [diff] [blame] | 964 | final Rect surfaceInsets = viewRoot.mWindowAttributes.surfaceInsets; |
Robert Carr | d5c7dd6 | 2017-03-08 10:39:30 -0800 | [diff] [blame] | 965 | mScreenRect.offset(surfaceInsets.left, surfaceInsets.top); |
| 966 | |
| 967 | if (creating) { |
Robert Carr | 3bc95b5 | 2017-03-20 21:57:23 -0700 | [diff] [blame] | 968 | mDeferredDestroySurfaceControl = mSurfaceControl; |
Robert Carr | 5523555 | 2017-06-02 14:21:03 -0700 | [diff] [blame] | 969 | |
| 970 | updateOpaqueFlag(); |
Vishnu Nair | 8cb00ae | 2019-08-02 15:20:29 -0700 | [diff] [blame] | 971 | // SurfaceView hierarchy |
| 972 | // ViewRootImpl surface |
| 973 | // - bounds layer (crops all child surfaces to parent surface insets) |
| 974 | // - SurfaceView surface (drawn relative to ViewRootImpl surface) |
| 975 | // - Background color layer (drawn behind all SurfaceView surfaces) |
| 976 | // |
| 977 | // The bounds layer is used to crop the surface view so it does not draw into |
| 978 | // the parent surface inset region. Since there can be multiple surface views |
| 979 | // below or above the parent surface, one option is to create multiple bounds |
| 980 | // layer for each z order. The other option, the one implement is to create |
| 981 | // a single bounds layer and set z order for each child surface relative to the |
| 982 | // parent surface. |
| 983 | // When creating the surface view, we parent it to the bounds layer and then |
| 984 | // set the relative z order. When the parent surface changes, we have to |
| 985 | // make sure to update the relative z via ViewRootImpl.SurfaceChangedCallback. |
Robert Carr | e625fcf | 2017-09-01 12:36:28 -0700 | [diff] [blame] | 986 | final String name = "SurfaceView - " + viewRoot.getTitle().toString(); |
Vishnu Nair | eaab0e5 | 2019-06-24 08:12:28 -0700 | [diff] [blame] | 987 | |
| 988 | mSurfaceControl = new SurfaceControl.Builder(mSurfaceSession) |
| 989 | .setName(name) |
Svet Ganov | a5b4990 | 2020-06-18 00:47:22 -0700 | [diff] [blame] | 990 | .setLocalOwnerView(this) |
Vishnu Nair | eaab0e5 | 2019-06-24 08:12:28 -0700 | [diff] [blame] | 991 | .setOpaque((mSurfaceFlags & SurfaceControl.OPAQUE) != 0) |
| 992 | .setBufferSize(mSurfaceWidth, mSurfaceHeight) |
| 993 | .setFormat(mFormat) |
Vishnu Nair | 8cb00ae | 2019-08-02 15:20:29 -0700 | [diff] [blame] | 994 | .setParent(viewRoot.getBoundsLayer()) |
Vishnu Nair | eaab0e5 | 2019-06-24 08:12:28 -0700 | [diff] [blame] | 995 | .setFlags(mSurfaceFlags) |
| 996 | .build(); |
Robert Carr | b923f54 | 2019-05-13 12:27:51 -0700 | [diff] [blame] | 997 | mBackgroundControl = new SurfaceControl.Builder(mSurfaceSession) |
| 998 | .setName("Background for -" + name) |
Svet Ganov | a5b4990 | 2020-06-18 00:47:22 -0700 | [diff] [blame] | 999 | .setLocalOwnerView(this) |
Robert Carr | b923f54 | 2019-05-13 12:27:51 -0700 | [diff] [blame] | 1000 | .setOpaque(true) |
| 1001 | .setColorLayer() |
| 1002 | .setParent(mSurfaceControl) |
| 1003 | .build(); |
| 1004 | |
Robert Carr | 44ab575 | 2017-03-20 21:47:11 -0700 | [diff] [blame] | 1005 | } else if (mSurfaceControl == null) { |
| 1006 | return; |
Robert Carr | 64aadd0 | 2015-11-06 13:54:20 -0800 | [diff] [blame] | 1007 | } |
| 1008 | |
Robert Carr | d5c7dd6 | 2017-03-08 10:39:30 -0800 | [diff] [blame] | 1009 | boolean realSizeChanged = false; |
Dianne Hackborn | 6d05fd3 | 2011-11-19 14:36:15 -0800 | [diff] [blame] | 1010 | |
Dianne Hackborn | 726426e | 2010-03-31 22:04:36 -0700 | [diff] [blame] | 1011 | mSurfaceLock.lock(); |
| 1012 | try { |
Dianne Hackborn | 726426e | 2010-03-31 22:04:36 -0700 | [diff] [blame] | 1013 | mDrawingStopped = !visible; |
Igor Murashkin | a86ab640 | 2013-08-30 12:58:36 -0700 | [diff] [blame] | 1014 | |
John Reck | aa6e84f | 2016-06-16 15:36:13 -0700 | [diff] [blame] | 1015 | if (DEBUG) Log.i(TAG, System.identityHashCode(this) + " " |
| 1016 | + "Cur surface: " + mSurface); |
Dianne Hackborn | 6d05fd3 | 2011-11-19 14:36:15 -0800 | [diff] [blame] | 1017 | |
chaviw | 619da69 | 2019-06-10 15:39:40 -0700 | [diff] [blame] | 1018 | // If we are creating the surface control or the parent surface has not |
| 1019 | // changed, then set relative z. Otherwise allow the parent |
| 1020 | // SurfaceChangedCallback to update the relative z. This is needed so that |
| 1021 | // we do not change the relative z before the server is ready to swap the |
| 1022 | // parent surface. |
| 1023 | if (creating || (mParentSurfaceGenerationId |
| 1024 | == viewRoot.mSurface.getGenerationId())) { |
| 1025 | updateRelativeZ(mTmpTransaction); |
Dianne Hackborn | 726426e | 2010-03-31 22:04:36 -0700 | [diff] [blame] | 1026 | } |
chaviw | 619da69 | 2019-06-10 15:39:40 -0700 | [diff] [blame] | 1027 | mParentSurfaceGenerationId = viewRoot.mSurface.getGenerationId(); |
| 1028 | |
| 1029 | if (mViewVisibility) { |
| 1030 | mTmpTransaction.show(mSurfaceControl); |
| 1031 | } else { |
| 1032 | mTmpTransaction.hide(mSurfaceControl); |
| 1033 | } |
Robert Carr | 30ad618 | 2019-12-18 01:46:04 -0800 | [diff] [blame] | 1034 | |
| 1035 | if (mSurfacePackage != null) { |
| 1036 | reparentSurfacePackage(mTmpTransaction, mSurfacePackage); |
| 1037 | } |
| 1038 | |
chaviw | 619da69 | 2019-06-10 15:39:40 -0700 | [diff] [blame] | 1039 | updateBackgroundVisibility(mTmpTransaction); |
Winson Chung | d4a9abd | 2020-03-21 23:01:16 -0700 | [diff] [blame] | 1040 | updateBackgroundColor(mTmpTransaction); |
chaviw | 619da69 | 2019-06-10 15:39:40 -0700 | [diff] [blame] | 1041 | if (mUseAlpha) { |
| 1042 | mTmpTransaction.setAlpha(mSurfaceControl, alpha); |
| 1043 | mSurfaceAlpha = alpha; |
| 1044 | } |
| 1045 | |
| 1046 | // While creating the surface, we will set it's initial |
| 1047 | // geometry. Outside of that though, we should generally |
| 1048 | // leave it to the RenderThread. |
| 1049 | // |
| 1050 | // There is one more case when the buffer size changes we aren't yet |
| 1051 | // prepared to sync (as even following the transaction applying |
| 1052 | // we still need to latch a buffer). |
| 1053 | // b/28866173 |
| 1054 | if (sizeChanged || creating || !mRtHandlingPositionUpdates) { |
Svet Ganov | a5b4990 | 2020-06-18 00:47:22 -0700 | [diff] [blame] | 1055 | onSetSurfacePositionAndScaleRT(mTmpTransaction, mSurfaceControl, |
| 1056 | mScreenRect.left, /*positionLeft*/ |
| 1057 | mScreenRect.top /*positionTop*/ , |
| 1058 | mScreenRect.width() / (float) mSurfaceWidth /*postScaleX*/, |
| 1059 | mScreenRect.height() / (float) mSurfaceHeight /*postScaleY*/); |
| 1060 | |
chaviw | 619da69 | 2019-06-10 15:39:40 -0700 | [diff] [blame] | 1061 | // Set a window crop when creating the surface or changing its size to |
| 1062 | // crop the buffer to the surface size since the buffer producer may |
| 1063 | // use SCALING_MODE_SCALE and submit a larger size than the surface |
| 1064 | // size. |
| 1065 | if (mClipSurfaceToBounds && mClipBounds != null) { |
| 1066 | mTmpTransaction.setWindowCrop(mSurfaceControl, mClipBounds); |
| 1067 | } else { |
| 1068 | mTmpTransaction.setWindowCrop(mSurfaceControl, mSurfaceWidth, |
| 1069 | mSurfaceHeight); |
| 1070 | } |
Robert Carr | 59773cb | 2020-04-01 14:35:07 -0700 | [diff] [blame] | 1071 | } else if ((layoutSizeChanged || positionChanged || visibleChanged) && |
Robert Carr | 6d16c8c | 2020-02-21 12:04:31 -0800 | [diff] [blame] | 1072 | viewRoot.useBLAST()) { |
Robert Carr | bf01a77 | 2020-02-18 08:42:53 -0800 | [diff] [blame] | 1073 | viewRoot.setUseBLASTSyncTransaction(); |
chaviw | 619da69 | 2019-06-10 15:39:40 -0700 | [diff] [blame] | 1074 | } |
| 1075 | mTmpTransaction.setCornerRadius(mSurfaceControl, mCornerRadius); |
| 1076 | if (sizeChanged && !creating) { |
| 1077 | mTmpTransaction.setBufferSize(mSurfaceControl, mSurfaceWidth, |
| 1078 | mSurfaceHeight); |
| 1079 | } |
| 1080 | |
| 1081 | mTmpTransaction.apply(); |
Jackal Guo | ac234d6 | 2020-02-03 15:05:43 +0800 | [diff] [blame] | 1082 | updateScreenMatrixForEmbeddedHierarchy(); |
Dianne Hackborn | 6d05fd3 | 2011-11-19 14:36:15 -0800 | [diff] [blame] | 1083 | |
Robert Carr | d5c7dd6 | 2017-03-08 10:39:30 -0800 | [diff] [blame] | 1084 | if (sizeChanged || creating) { |
| 1085 | redrawNeeded = true; |
| 1086 | } |
Dianne Hackborn | 6d05fd3 | 2011-11-19 14:36:15 -0800 | [diff] [blame] | 1087 | |
Dianne Hackborn | 726426e | 2010-03-31 22:04:36 -0700 | [diff] [blame] | 1088 | mSurfaceFrame.left = 0; |
| 1089 | mSurfaceFrame.top = 0; |
Issei Suzuki | f041259 | 2019-07-02 12:31:34 +0200 | [diff] [blame] | 1090 | if (translator == null) { |
Robert Carr | d5c7dd6 | 2017-03-08 10:39:30 -0800 | [diff] [blame] | 1091 | mSurfaceFrame.right = mSurfaceWidth; |
| 1092 | mSurfaceFrame.bottom = mSurfaceHeight; |
Dianne Hackborn | 726426e | 2010-03-31 22:04:36 -0700 | [diff] [blame] | 1093 | } else { |
Issei Suzuki | f041259 | 2019-07-02 12:31:34 +0200 | [diff] [blame] | 1094 | float appInvertedScale = translator.applicationInvertedScale; |
Robert Carr | d5c7dd6 | 2017-03-08 10:39:30 -0800 | [diff] [blame] | 1095 | mSurfaceFrame.right = (int) (mSurfaceWidth * appInvertedScale + 0.5f); |
| 1096 | mSurfaceFrame.bottom = (int) (mSurfaceHeight * appInvertedScale + 0.5f); |
Dianne Hackborn | 726426e | 2010-03-31 22:04:36 -0700 | [diff] [blame] | 1097 | } |
Igor Murashkin | a86ab640 | 2013-08-30 12:58:36 -0700 | [diff] [blame] | 1098 | |
Dianne Hackborn | 726426e | 2010-03-31 22:04:36 -0700 | [diff] [blame] | 1099 | final int surfaceWidth = mSurfaceFrame.right; |
| 1100 | final int surfaceHeight = mSurfaceFrame.bottom; |
| 1101 | realSizeChanged = mLastSurfaceWidth != surfaceWidth |
| 1102 | || mLastSurfaceHeight != surfaceHeight; |
| 1103 | mLastSurfaceWidth = surfaceWidth; |
| 1104 | mLastSurfaceHeight = surfaceHeight; |
| 1105 | } finally { |
| 1106 | mSurfaceLock.unlock(); |
Mitsuru Oshima | 589cebe | 2009-07-22 20:38:58 -0700 | [diff] [blame] | 1107 | } |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1108 | |
| 1109 | try { |
Robert Carr | d5c7dd6 | 2017-03-08 10:39:30 -0800 | [diff] [blame] | 1110 | redrawNeeded |= visible && !mDrawFinished; |
Dianne Hackborn | d76b67c | 2010-07-13 17:48:30 -0700 | [diff] [blame] | 1111 | |
Issei Suzuki | 60557b5 | 2019-07-03 20:04:48 +0200 | [diff] [blame] | 1112 | SurfaceHolder.Callback[] callbacks = null; |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1113 | |
Robert Carr | d5c7dd6 | 2017-03-08 10:39:30 -0800 | [diff] [blame] | 1114 | final boolean surfaceChanged = creating; |
Dianne Hackborn | 6d05fd3 | 2011-11-19 14:36:15 -0800 | [diff] [blame] | 1115 | if (mSurfaceCreated && (surfaceChanged || (!visible && visibleChanged))) { |
| 1116 | mSurfaceCreated = false; |
Vishnu Nair | 34486ad | 2020-06-10 18:14:20 -0700 | [diff] [blame] | 1117 | notifySurfaceDestroyed(); |
Dianne Hackborn | 6d05fd3 | 2011-11-19 14:36:15 -0800 | [diff] [blame] | 1118 | } |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1119 | |
Robert Carr | d5c7dd6 | 2017-03-08 10:39:30 -0800 | [diff] [blame] | 1120 | if (creating) { |
Robert Carr | b923f54 | 2019-05-13 12:27:51 -0700 | [diff] [blame] | 1121 | mSurface.copyFrom(mSurfaceControl); |
Robert Carr | d5c7dd6 | 2017-03-08 10:39:30 -0800 | [diff] [blame] | 1122 | } |
| 1123 | |
Bryce Lee | 453fc36 | 2017-06-20 10:47:55 -0700 | [diff] [blame] | 1124 | if (sizeChanged && getContext().getApplicationInfo().targetSdkVersion |
Bryce Lee | 02949f1 | 2017-06-16 07:20:34 -0700 | [diff] [blame] | 1125 | < Build.VERSION_CODES.O) { |
| 1126 | // Some legacy applications use the underlying native {@link Surface} object |
| 1127 | // as a key to whether anything has changed. In these cases, updates to the |
| 1128 | // existing {@link Surface} will be ignored when the size changes. |
| 1129 | // Therefore, we must explicitly recreate the {@link Surface} in these |
| 1130 | // cases. |
Robert Carr | b923f54 | 2019-05-13 12:27:51 -0700 | [diff] [blame] | 1131 | mSurface.createFrom(mSurfaceControl); |
Bryce Lee | 02949f1 | 2017-06-16 07:20:34 -0700 | [diff] [blame] | 1132 | } |
| 1133 | |
Andreas Röhl | f750b8c | 2012-07-02 13:06:26 +0200 | [diff] [blame] | 1134 | if (visible && mSurface.isValid()) { |
Dianne Hackborn | 6d05fd3 | 2011-11-19 14:36:15 -0800 | [diff] [blame] | 1135 | if (!mSurfaceCreated && (surfaceChanged || visibleChanged)) { |
| 1136 | mSurfaceCreated = true; |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1137 | mIsCreating = true; |
John Reck | aa6e84f | 2016-06-16 15:36:13 -0700 | [diff] [blame] | 1138 | if (DEBUG) Log.i(TAG, System.identityHashCode(this) + " " |
| 1139 | + "visibleChanged -- surfaceCreated"); |
Dianne Hackborn | 6d05fd3 | 2011-11-19 14:36:15 -0800 | [diff] [blame] | 1140 | if (callbacks == null) { |
| 1141 | callbacks = getSurfaceCallbacks(); |
| 1142 | } |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1143 | for (SurfaceHolder.Callback c : callbacks) { |
| 1144 | c.surfaceCreated(mSurfaceHolder); |
| 1145 | } |
| 1146 | } |
| 1147 | if (creating || formatChanged || sizeChanged |
Dianne Hackborn | 726426e | 2010-03-31 22:04:36 -0700 | [diff] [blame] | 1148 | || visibleChanged || realSizeChanged) { |
John Reck | aa6e84f | 2016-06-16 15:36:13 -0700 | [diff] [blame] | 1149 | if (DEBUG) Log.i(TAG, System.identityHashCode(this) + " " |
| 1150 | + "surfaceChanged -- format=" + mFormat |
Dianne Hackborn | 6d05fd3 | 2011-11-19 14:36:15 -0800 | [diff] [blame] | 1151 | + " w=" + myWidth + " h=" + myHeight); |
| 1152 | if (callbacks == null) { |
| 1153 | callbacks = getSurfaceCallbacks(); |
| 1154 | } |
Dianne Hackborn | 251fd43 | 2010-07-14 16:56:31 -0700 | [diff] [blame] | 1155 | for (SurfaceHolder.Callback c : callbacks) { |
| 1156 | c.surfaceChanged(mSurfaceHolder, mFormat, myWidth, myHeight); |
| 1157 | } |
Dianne Hackborn | d76b67c | 2010-07-13 17:48:30 -0700 | [diff] [blame] | 1158 | } |
| 1159 | if (redrawNeeded) { |
John Reck | aa6e84f | 2016-06-16 15:36:13 -0700 | [diff] [blame] | 1160 | if (DEBUG) Log.i(TAG, System.identityHashCode(this) + " " |
| 1161 | + "surfaceRedrawNeeded"); |
Dianne Hackborn | 6d05fd3 | 2011-11-19 14:36:15 -0800 | [diff] [blame] | 1162 | if (callbacks == null) { |
| 1163 | callbacks = getSurfaceCallbacks(); |
| 1164 | } |
Robert Carr | 8508bb2 | 2017-03-27 15:46:27 -0700 | [diff] [blame] | 1165 | |
| 1166 | mPendingReportDraws++; |
| 1167 | viewRoot.drawPending(); |
Robert Carr | 25cfa13 | 2016-11-16 13:24:09 -0800 | [diff] [blame] | 1168 | SurfaceCallbackHelper sch = |
Robert Carr | d5c7dd6 | 2017-03-08 10:39:30 -0800 | [diff] [blame] | 1169 | new SurfaceCallbackHelper(this::onDrawFinished); |
Robert Carr | 25cfa13 | 2016-11-16 13:24:09 -0800 | [diff] [blame] | 1170 | sch.dispatchSurfaceRedrawNeededAsync(mSurfaceHolder, callbacks); |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1171 | } |
| 1172 | } |
| 1173 | } finally { |
| 1174 | mIsCreating = false; |
Robert Carr | d5c7dd6 | 2017-03-08 10:39:30 -0800 | [diff] [blame] | 1175 | if (mSurfaceControl != null && !mSurfaceCreated) { |
Robert Carr | b923f54 | 2019-05-13 12:27:51 -0700 | [diff] [blame] | 1176 | releaseSurfaces(); |
Robert Carr | d5c7dd6 | 2017-03-08 10:39:30 -0800 | [diff] [blame] | 1177 | } |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1178 | } |
Robert Carr | d5c7dd6 | 2017-03-08 10:39:30 -0800 | [diff] [blame] | 1179 | } catch (Exception ex) { |
Robert Carr | 44ab575 | 2017-03-20 21:47:11 -0700 | [diff] [blame] | 1180 | Log.e(TAG, "Exception configuring surface", ex); |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1181 | } |
Dianne Hackborn | 6d05fd3 | 2011-11-19 14:36:15 -0800 | [diff] [blame] | 1182 | if (DEBUG) Log.v( |
Robert Carr | d5c7dd6 | 2017-03-08 10:39:30 -0800 | [diff] [blame] | 1183 | TAG, "Layout: x=" + mScreenRect.left + " y=" + mScreenRect.top |
| 1184 | + " w=" + mScreenRect.width() + " h=" + mScreenRect.height() |
| 1185 | + ", frame=" + mSurfaceFrame); |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1186 | } |
| 1187 | } |
| 1188 | |
Robert Carr | d5c7dd6 | 2017-03-08 10:39:30 -0800 | [diff] [blame] | 1189 | private void onDrawFinished() { |
| 1190 | if (DEBUG) { |
| 1191 | Log.i(TAG, System.identityHashCode(this) + " " |
| 1192 | + "finishedDrawing"); |
| 1193 | } |
Robert Carr | 3bc95b5 | 2017-03-20 21:57:23 -0700 | [diff] [blame] | 1194 | |
| 1195 | if (mDeferredDestroySurfaceControl != null) { |
Robert Carr | 6ce9ffb | 2020-06-01 11:02:19 -0700 | [diff] [blame] | 1196 | synchronized (mSurfaceControlLock) { |
| 1197 | mTmpTransaction.remove(mDeferredDestroySurfaceControl).apply(); |
| 1198 | mDeferredDestroySurfaceControl = null; |
| 1199 | } |
Robert Carr | 3bc95b5 | 2017-03-20 21:57:23 -0700 | [diff] [blame] | 1200 | } |
| 1201 | |
Issei Suzuki | 60557b5 | 2019-07-03 20:04:48 +0200 | [diff] [blame] | 1202 | runOnUiThread(this::performDrawFinished); |
Robert Carr | d5c7dd6 | 2017-03-08 10:39:30 -0800 | [diff] [blame] | 1203 | } |
| 1204 | |
Robert Carr | 27a800a | 2018-03-16 13:33:45 -0700 | [diff] [blame] | 1205 | /** |
| 1206 | * A place to over-ride for applying child-surface transactions. |
| 1207 | * These can be synchronized with the viewroot surface using deferTransaction. |
| 1208 | * |
| 1209 | * Called from RenderWorker while UI thread is paused. |
| 1210 | * @hide |
| 1211 | */ |
| 1212 | protected void applyChildSurfaceTransaction_renderWorker(SurfaceControl.Transaction t, |
| 1213 | Surface viewRootSurface, long nextViewRootFrameNumber) { |
| 1214 | } |
| 1215 | |
Svet Ganov | a5b4990 | 2020-06-18 00:47:22 -0700 | [diff] [blame] | 1216 | /** |
| 1217 | * Sets the surface position and scale. Can be called on |
| 1218 | * the UI thread as well as on the renderer thread. |
| 1219 | * |
| 1220 | * @param transaction Transaction in which to execute. |
| 1221 | * @param surface Surface whose location to set. |
| 1222 | * @param positionLeft The left position to set. |
| 1223 | * @param positionTop The top position to set. |
| 1224 | * @param postScaleX The X axis post scale |
| 1225 | * @param postScaleY The Y axis post scale |
| 1226 | * |
| 1227 | * @hide |
| 1228 | */ |
| 1229 | protected void onSetSurfacePositionAndScaleRT(@NonNull Transaction transaction, |
| 1230 | @NonNull SurfaceControl surface, int positionLeft, int positionTop, |
| 1231 | float postScaleX, float postScaleY) { |
| 1232 | transaction.setPosition(surface, positionLeft, positionTop); |
| 1233 | transaction.setMatrix(surface, postScaleX /*dsdx*/, 0f /*dtdx*/, |
Svet Ganov | 4846803 | 2020-06-23 23:38:07 -0700 | [diff] [blame] | 1234 | 0f /*dtdy*/, postScaleY /*dsdy*/); |
Svet Ganov | a5b4990 | 2020-06-18 00:47:22 -0700 | [diff] [blame] | 1235 | } |
| 1236 | |
| 1237 | /** @hide */ |
| 1238 | public void requestUpdateSurfacePositionAndScale() { |
| 1239 | if (mSurfaceControl == null) { |
| 1240 | return; |
| 1241 | } |
| 1242 | onSetSurfacePositionAndScaleRT(mTmpTransaction, mSurfaceControl, |
| 1243 | mScreenRect.left, /*positionLeft*/ |
| 1244 | mScreenRect.top/*positionTop*/ , |
| 1245 | mScreenRect.width() / (float) mSurfaceWidth /*postScaleX*/, |
| 1246 | mScreenRect.height() / (float) mSurfaceHeight /*postScaleY*/); |
| 1247 | mTmpTransaction.apply(); |
| 1248 | } |
| 1249 | |
Robert Carr | 85bb940 | 2019-12-19 01:33:24 -0800 | [diff] [blame] | 1250 | private void applySurfaceTransforms(SurfaceControl surface, SurfaceControl.Transaction t, |
| 1251 | Rect position, long frameNumber) { |
Robert Carr | 6d16c8c | 2020-02-21 12:04:31 -0800 | [diff] [blame] | 1252 | final ViewRootImpl viewRoot = getViewRootImpl(); |
Robert Carr | 92e08bf | 2020-04-01 14:29:13 -0700 | [diff] [blame] | 1253 | if (frameNumber > 0 && viewRoot != null && !viewRoot.isDrawingToBLASTTransaction()) { |
Robert Carr | cef9a28 | 2020-01-14 09:08:05 -0800 | [diff] [blame] | 1254 | t.deferTransactionUntil(surface, viewRoot.getRenderSurfaceControl(), |
Robert Carr | 27a800a | 2018-03-16 13:33:45 -0700 | [diff] [blame] | 1255 | frameNumber); |
Robert Carr | d5c7dd6 | 2017-03-08 10:39:30 -0800 | [diff] [blame] | 1256 | } |
Robert Carr | 386fd70 | 2018-03-23 13:46:39 -0700 | [diff] [blame] | 1257 | |
Svet Ganov | a5b4990 | 2020-06-18 00:47:22 -0700 | [diff] [blame] | 1258 | onSetSurfacePositionAndScaleRT(t, surface, |
| 1259 | position.left /*positionLeft*/, |
| 1260 | position.top /*positionTop*/, |
| 1261 | position.width() / (float) mSurfaceWidth /*postScaleX*/, |
| 1262 | position.height() / (float) mSurfaceHeight /*postScaleY*/); |
| 1263 | |
John Reck | d0abd66 | 2019-05-01 12:52:04 -0700 | [diff] [blame] | 1264 | if (mViewVisibility) { |
Robert Carr | 85bb940 | 2019-12-19 01:33:24 -0800 | [diff] [blame] | 1265 | t.show(surface); |
John Reck | d0abd66 | 2019-05-01 12:52:04 -0700 | [diff] [blame] | 1266 | } |
Robert Carr | 386fd70 | 2018-03-23 13:46:39 -0700 | [diff] [blame] | 1267 | } |
| 1268 | |
Svet Ganov | a5b4990 | 2020-06-18 00:47:22 -0700 | [diff] [blame] | 1269 | /** |
| 1270 | * @return The last render position of the backing surface or an empty rect. |
| 1271 | * |
| 1272 | * @hide |
| 1273 | */ |
| 1274 | public @NonNull Rect getSurfaceRenderPosition() { |
| 1275 | return mRTLastReportedPosition; |
| 1276 | } |
| 1277 | |
Robert Carr | 386fd70 | 2018-03-23 13:46:39 -0700 | [diff] [blame] | 1278 | private void setParentSpaceRectangle(Rect position, long frameNumber) { |
| 1279 | final ViewRootImpl viewRoot = getViewRootImpl(); |
Robert Carr | 92e08bf | 2020-04-01 14:29:13 -0700 | [diff] [blame] | 1280 | final boolean useBLAST = viewRoot.isDrawingToBLASTTransaction(); |
Robert Carr | 85bb940 | 2019-12-19 01:33:24 -0800 | [diff] [blame] | 1281 | final SurfaceControl.Transaction t = useBLAST ? viewRoot.getBLASTSyncTransaction() : |
| 1282 | mRtTransaction; |
Robert Carr | 386fd70 | 2018-03-23 13:46:39 -0700 | [diff] [blame] | 1283 | |
Robert Carr | 85bb940 | 2019-12-19 01:33:24 -0800 | [diff] [blame] | 1284 | applySurfaceTransforms(mSurfaceControl, t, position, frameNumber); |
Robert Carr | 27a800a | 2018-03-16 13:33:45 -0700 | [diff] [blame] | 1285 | |
Robert Carr | 85bb940 | 2019-12-19 01:33:24 -0800 | [diff] [blame] | 1286 | applyChildSurfaceTransaction_renderWorker(t, viewRoot.mSurface, |
Robert Carr | 27a800a | 2018-03-16 13:33:45 -0700 | [diff] [blame] | 1287 | frameNumber); |
| 1288 | |
Robert Carr | 85bb940 | 2019-12-19 01:33:24 -0800 | [diff] [blame] | 1289 | if (!useBLAST) { |
| 1290 | t.apply(); |
| 1291 | } |
Robert Carr | d5c7dd6 | 2017-03-08 10:39:30 -0800 | [diff] [blame] | 1292 | } |
| 1293 | |
John Reck | f648108 | 2016-02-02 15:18:23 -0800 | [diff] [blame] | 1294 | private Rect mRTLastReportedPosition = new Rect(); |
| 1295 | |
John Reck | 6b16440 | 2018-09-24 15:25:42 -0700 | [diff] [blame] | 1296 | private RenderNode.PositionUpdateListener mPositionListener = |
| 1297 | new RenderNode.PositionUpdateListener() { |
Robert Carr | d5c7dd6 | 2017-03-08 10:39:30 -0800 | [diff] [blame] | 1298 | |
John Reck | 6b16440 | 2018-09-24 15:25:42 -0700 | [diff] [blame] | 1299 | @Override |
| 1300 | public void positionChanged(long frameNumber, int left, int top, int right, int bottom) { |
| 1301 | if (mSurfaceControl == null) { |
| 1302 | return; |
John Reck | f648108 | 2016-02-02 15:18:23 -0800 | [diff] [blame] | 1303 | } |
John Reck | f648108 | 2016-02-02 15:18:23 -0800 | [diff] [blame] | 1304 | |
John Reck | 6b16440 | 2018-09-24 15:25:42 -0700 | [diff] [blame] | 1305 | // TODO: This is teensy bit racey in that a brand new SurfaceView moving on |
| 1306 | // its 2nd frame if RenderThread is running slowly could potentially see |
| 1307 | // this as false, enter the branch, get pre-empted, then this comes along |
| 1308 | // and reports a new position, then the UI thread resumes and reports |
| 1309 | // its position. This could therefore be de-sync'd in that interval, but |
| 1310 | // the synchronization would violate the rule that RT must never block |
| 1311 | // on the UI thread which would open up potential deadlocks. The risk of |
| 1312 | // a single-frame desync is therefore preferable for now. |
Robert Carr | 005c63e | 2019-09-20 14:31:24 -0700 | [diff] [blame] | 1313 | synchronized(mSurfaceControlLock) { |
| 1314 | mRtHandlingPositionUpdates = true; |
| 1315 | } |
John Reck | 6b16440 | 2018-09-24 15:25:42 -0700 | [diff] [blame] | 1316 | if (mRTLastReportedPosition.left == left |
| 1317 | && mRTLastReportedPosition.top == top |
| 1318 | && mRTLastReportedPosition.right == right |
| 1319 | && mRTLastReportedPosition.bottom == bottom) { |
| 1320 | return; |
| 1321 | } |
| 1322 | try { |
Issei Suzuki | 006b71f | 2019-06-17 15:56:57 +0200 | [diff] [blame] | 1323 | if (DEBUG_POSITION) { |
John Reck | 6b16440 | 2018-09-24 15:25:42 -0700 | [diff] [blame] | 1324 | Log.d(TAG, String.format( |
| 1325 | "%d updateSurfacePosition RenderWorker, frameNr = %d, " |
Issei Suzuki | 60557b5 | 2019-07-03 20:04:48 +0200 | [diff] [blame] | 1326 | + "position = [%d, %d, %d, %d]", |
John Reck | 6b16440 | 2018-09-24 15:25:42 -0700 | [diff] [blame] | 1327 | System.identityHashCode(this), frameNumber, |
| 1328 | left, top, right, bottom)); |
| 1329 | } |
| 1330 | mRTLastReportedPosition.set(left, top, right, bottom); |
| 1331 | setParentSpaceRectangle(mRTLastReportedPosition, frameNumber); |
| 1332 | // Now overwrite mRTLastReportedPosition with our values |
| 1333 | } catch (Exception ex) { |
| 1334 | Log.e(TAG, "Exception from repositionChild", ex); |
| 1335 | } |
John Reck | aa6e84f | 2016-06-16 15:36:13 -0700 | [diff] [blame] | 1336 | } |
Robert Carr | ad3a493 | 2017-06-20 14:55:21 -0700 | [diff] [blame] | 1337 | |
John Reck | 6b16440 | 2018-09-24 15:25:42 -0700 | [diff] [blame] | 1338 | @Override |
| 1339 | public void positionLost(long frameNumber) { |
Robert Carr | 6d16c8c | 2020-02-21 12:04:31 -0800 | [diff] [blame] | 1340 | final ViewRootImpl viewRoot = getViewRootImpl(); |
Robert Carr | 92e08bf | 2020-04-01 14:29:13 -0700 | [diff] [blame] | 1341 | boolean useBLAST = viewRoot != null && viewRoot.isDrawingToBLASTTransaction(); |
John Reck | 6b16440 | 2018-09-24 15:25:42 -0700 | [diff] [blame] | 1342 | if (DEBUG) { |
| 1343 | Log.d(TAG, String.format("%d windowPositionLost, frameNr = %d", |
| 1344 | System.identityHashCode(this), frameNumber)); |
| 1345 | } |
| 1346 | mRTLastReportedPosition.setEmpty(); |
| 1347 | |
| 1348 | if (mSurfaceControl == null) { |
| 1349 | return; |
| 1350 | } |
John Reck | d0abd66 | 2019-05-01 12:52:04 -0700 | [diff] [blame] | 1351 | |
Robert Carr | 85bb940 | 2019-12-19 01:33:24 -0800 | [diff] [blame] | 1352 | final SurfaceControl.Transaction t = useBLAST ? |
| 1353 | (viewRoot != null ? viewRoot.getBLASTSyncTransaction() : mRtTransaction) : |
| 1354 | mRtTransaction; |
| 1355 | |
Robert Carr | ebaaca1 | 2020-05-13 11:23:43 -0700 | [diff] [blame] | 1356 | /** |
| 1357 | * positionLost can be called while UI thread is un-paused so we |
| 1358 | * need to hold the lock here. |
| 1359 | */ |
Robert Carr | 005c63e | 2019-09-20 14:31:24 -0700 | [diff] [blame] | 1360 | synchronized (mSurfaceControlLock) { |
Robert Carr | ebaaca1 | 2020-05-13 11:23:43 -0700 | [diff] [blame] | 1361 | if (frameNumber > 0 && viewRoot != null && !useBLAST) { |
| 1362 | if (viewRoot.mSurface.isValid()) { |
| 1363 | mRtTransaction.deferTransactionUntil(mSurfaceControl, |
| 1364 | viewRoot.getRenderSurfaceControl(), frameNumber); |
| 1365 | } |
| 1366 | } |
| 1367 | t.hide(mSurfaceControl); |
| 1368 | |
Robert Carr | 005c63e | 2019-09-20 14:31:24 -0700 | [diff] [blame] | 1369 | if (mRtReleaseSurfaces) { |
| 1370 | mRtReleaseSurfaces = false; |
| 1371 | mRtTransaction.remove(mSurfaceControl); |
| 1372 | mRtTransaction.remove(mBackgroundControl); |
| 1373 | mSurfaceControl = null; |
| 1374 | mBackgroundControl = null; |
| 1375 | } |
| 1376 | mRtHandlingPositionUpdates = false; |
| 1377 | } |
| 1378 | |
Robert Carr | 85bb940 | 2019-12-19 01:33:24 -0800 | [diff] [blame] | 1379 | // If we aren't using BLAST, we apply the transaction locally, otherise we let the ViewRoot apply it for us. |
| 1380 | // If the ViewRoot is null, we behave as if we aren't using BLAST so we need to apply the transaction. |
| 1381 | if (!useBLAST || viewRoot == null) { |
| 1382 | mRtTransaction.apply(); |
| 1383 | } |
John Reck | f23a1b8 | 2016-06-22 14:23:31 -0700 | [diff] [blame] | 1384 | } |
John Reck | 6b16440 | 2018-09-24 15:25:42 -0700 | [diff] [blame] | 1385 | }; |
John Reck | aa6e84f | 2016-06-16 15:36:13 -0700 | [diff] [blame] | 1386 | |
Dianne Hackborn | 6d05fd3 | 2011-11-19 14:36:15 -0800 | [diff] [blame] | 1387 | private SurfaceHolder.Callback[] getSurfaceCallbacks() { |
Issei Suzuki | 60557b5 | 2019-07-03 20:04:48 +0200 | [diff] [blame] | 1388 | SurfaceHolder.Callback[] callbacks; |
Dianne Hackborn | 6d05fd3 | 2011-11-19 14:36:15 -0800 | [diff] [blame] | 1389 | synchronized (mCallbacks) { |
| 1390 | callbacks = new SurfaceHolder.Callback[mCallbacks.size()]; |
| 1391 | mCallbacks.toArray(callbacks); |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1392 | } |
Dianne Hackborn | 6d05fd3 | 2011-11-19 14:36:15 -0800 | [diff] [blame] | 1393 | return callbacks; |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1394 | } |
| 1395 | |
John Reck | 7992500 | 2017-05-26 13:57:14 -0700 | [diff] [blame] | 1396 | private void runOnUiThread(Runnable runnable) { |
| 1397 | Handler handler = getHandler(); |
| 1398 | if (handler != null && handler.getLooper() != Looper.myLooper()) { |
| 1399 | handler.post(runnable); |
| 1400 | } else { |
| 1401 | runnable.run(); |
| 1402 | } |
| 1403 | } |
| 1404 | |
Yohei Yukawa | 3b5011a | 2017-03-16 15:34:12 -0700 | [diff] [blame] | 1405 | /** |
Derek Sollenberger | 7179b81 | 2010-03-22 13:41:20 -0400 | [diff] [blame] | 1406 | * Check to see if the surface has fixed size dimensions or if the surface's |
| 1407 | * dimensions are dimensions are dependent on its current layout. |
| 1408 | * |
| 1409 | * @return true if the surface has dimensions that are fixed in size |
| 1410 | * @hide |
| 1411 | */ |
Mathew Inwood | a570dee | 2018-08-17 14:56:00 +0100 | [diff] [blame] | 1412 | @UnsupportedAppUsage |
Derek Sollenberger | 7179b81 | 2010-03-22 13:41:20 -0400 | [diff] [blame] | 1413 | public boolean isFixedSize() { |
| 1414 | return (mRequestedWidth != -1 || mRequestedHeight != -1); |
| 1415 | } |
| 1416 | |
Robert Carr | d5c7dd6 | 2017-03-08 10:39:30 -0800 | [diff] [blame] | 1417 | private boolean isAboveParent() { |
| 1418 | return mSubLayer >= 0; |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1419 | } |
| 1420 | |
Andrii Kulian | cf8f683 | 2018-01-23 19:43:30 -0800 | [diff] [blame] | 1421 | /** |
| 1422 | * Set an opaque background color to use with this {@link SurfaceView} when it's being resized |
| 1423 | * and size of the content hasn't updated yet. This color will fill the expanded area when the |
| 1424 | * view becomes larger. |
| 1425 | * @param bgColor An opaque color to fill the background. Alpha component will be ignored. |
| 1426 | * @hide |
| 1427 | */ |
| 1428 | public void setResizeBackgroundColor(int bgColor) { |
Robert Carr | b923f54 | 2019-05-13 12:27:51 -0700 | [diff] [blame] | 1429 | if (mBackgroundControl == null) { |
| 1430 | return; |
| 1431 | } |
| 1432 | |
Winson Chung | d4a9abd | 2020-03-21 23:01:16 -0700 | [diff] [blame] | 1433 | mBackgroundColor = bgColor; |
| 1434 | updateBackgroundColor(mTmpTransaction).apply(); |
Andrii Kulian | cf8f683 | 2018-01-23 19:43:30 -0800 | [diff] [blame] | 1435 | } |
| 1436 | |
Mathew Inwood | a570dee | 2018-08-17 14:56:00 +0100 | [diff] [blame] | 1437 | @UnsupportedAppUsage |
Igor Murashkin | a86ab640 | 2013-08-30 12:58:36 -0700 | [diff] [blame] | 1438 | private final SurfaceHolder mSurfaceHolder = new SurfaceHolder() { |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1439 | private static final String LOG_TAG = "SurfaceHolder"; |
Igor Murashkin | a86ab640 | 2013-08-30 12:58:36 -0700 | [diff] [blame] | 1440 | |
| 1441 | @Override |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1442 | public boolean isCreating() { |
| 1443 | return mIsCreating; |
| 1444 | } |
| 1445 | |
Igor Murashkin | a86ab640 | 2013-08-30 12:58:36 -0700 | [diff] [blame] | 1446 | @Override |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1447 | public void addCallback(Callback callback) { |
| 1448 | synchronized (mCallbacks) { |
Igor Murashkin | a86ab640 | 2013-08-30 12:58:36 -0700 | [diff] [blame] | 1449 | // 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] | 1450 | // have only a couple callbacks, so it doesn't matter. |
Issei Suzuki | f041259 | 2019-07-02 12:31:34 +0200 | [diff] [blame] | 1451 | if (!mCallbacks.contains(callback)) { |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1452 | mCallbacks.add(callback); |
| 1453 | } |
| 1454 | } |
| 1455 | } |
| 1456 | |
Igor Murashkin | a86ab640 | 2013-08-30 12:58:36 -0700 | [diff] [blame] | 1457 | @Override |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1458 | public void removeCallback(Callback callback) { |
| 1459 | synchronized (mCallbacks) { |
| 1460 | mCallbacks.remove(callback); |
| 1461 | } |
| 1462 | } |
Igor Murashkin | a86ab640 | 2013-08-30 12:58:36 -0700 | [diff] [blame] | 1463 | |
| 1464 | @Override |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1465 | public void setFixedSize(int width, int height) { |
| 1466 | if (mRequestedWidth != width || mRequestedHeight != height) { |
| 1467 | mRequestedWidth = width; |
| 1468 | mRequestedHeight = height; |
| 1469 | requestLayout(); |
| 1470 | } |
| 1471 | } |
| 1472 | |
Igor Murashkin | a86ab640 | 2013-08-30 12:58:36 -0700 | [diff] [blame] | 1473 | @Override |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1474 | public void setSizeFromLayout() { |
| 1475 | if (mRequestedWidth != -1 || mRequestedHeight != -1) { |
| 1476 | mRequestedWidth = mRequestedHeight = -1; |
| 1477 | requestLayout(); |
| 1478 | } |
| 1479 | } |
| 1480 | |
Igor Murashkin | a86ab640 | 2013-08-30 12:58:36 -0700 | [diff] [blame] | 1481 | @Override |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1482 | public void setFormat(int format) { |
Mathias Agopian | 2d468c5 | 2010-06-14 21:50:48 -0700 | [diff] [blame] | 1483 | // for backward compatibility reason, OPAQUE always |
| 1484 | // means 565 for SurfaceView |
| 1485 | if (format == PixelFormat.OPAQUE) |
| 1486 | format = PixelFormat.RGB_565; |
| 1487 | |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1488 | mRequestedFormat = format; |
Robert Carr | d5c7dd6 | 2017-03-08 10:39:30 -0800 | [diff] [blame] | 1489 | if (mSurfaceControl != null) { |
| 1490 | updateSurface(); |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1491 | } |
| 1492 | } |
| 1493 | |
Mathias Agopian | d211230 | 2010-12-07 19:38:17 -0800 | [diff] [blame] | 1494 | /** |
| 1495 | * @deprecated setType is now ignored. |
| 1496 | */ |
Igor Murashkin | a86ab640 | 2013-08-30 12:58:36 -0700 | [diff] [blame] | 1497 | @Override |
Mathias Agopian | d211230 | 2010-12-07 19:38:17 -0800 | [diff] [blame] | 1498 | @Deprecated |
| 1499 | public void setType(int type) { } |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1500 | |
Igor Murashkin | a86ab640 | 2013-08-30 12:58:36 -0700 | [diff] [blame] | 1501 | @Override |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1502 | public void setKeepScreenOn(boolean screenOn) { |
John Reck | 7992500 | 2017-05-26 13:57:14 -0700 | [diff] [blame] | 1503 | runOnUiThread(() -> SurfaceView.this.setKeepScreenOn(screenOn)); |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1504 | } |
Igor Murashkin | a86ab640 | 2013-08-30 12:58:36 -0700 | [diff] [blame] | 1505 | |
Mathias Agopian | 9ddf32a | 2013-04-17 15:04:47 -0700 | [diff] [blame] | 1506 | /** |
| 1507 | * Gets a {@link Canvas} for drawing into the SurfaceView's Surface |
| 1508 | * |
| 1509 | * After drawing into the provided {@link Canvas}, the caller must |
| 1510 | * invoke {@link #unlockCanvasAndPost} to post the new contents to the surface. |
| 1511 | * |
| 1512 | * The caller must redraw the entire surface. |
| 1513 | * @return A canvas for drawing into the surface. |
| 1514 | */ |
Igor Murashkin | a86ab640 | 2013-08-30 12:58:36 -0700 | [diff] [blame] | 1515 | @Override |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1516 | public Canvas lockCanvas() { |
John Reck | 6bc7014 | 2016-10-26 16:49:17 -0700 | [diff] [blame] | 1517 | return internalLockCanvas(null, false); |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1518 | } |
| 1519 | |
Mathias Agopian | 9ddf32a | 2013-04-17 15:04:47 -0700 | [diff] [blame] | 1520 | /** |
| 1521 | * Gets a {@link Canvas} for drawing into the SurfaceView's Surface |
| 1522 | * |
| 1523 | * After drawing into the provided {@link Canvas}, the caller must |
| 1524 | * invoke {@link #unlockCanvasAndPost} to post the new contents to the surface. |
| 1525 | * |
| 1526 | * @param inOutDirty A rectangle that represents the dirty region that the caller wants |
| 1527 | * to redraw. This function may choose to expand the dirty rectangle if for example |
| 1528 | * the surface has been resized or if the previous contents of the surface were |
| 1529 | * not available. The caller must redraw the entire dirty region as represented |
| 1530 | * by the contents of the inOutDirty rectangle upon return from this function. |
| 1531 | * The caller may also pass <code>null</code> instead, in the case where the |
| 1532 | * entire surface should be redrawn. |
| 1533 | * @return A canvas for drawing into the surface. |
| 1534 | */ |
Igor Murashkin | a86ab640 | 2013-08-30 12:58:36 -0700 | [diff] [blame] | 1535 | @Override |
Mathias Agopian | 9ddf32a | 2013-04-17 15:04:47 -0700 | [diff] [blame] | 1536 | public Canvas lockCanvas(Rect inOutDirty) { |
John Reck | 6bc7014 | 2016-10-26 16:49:17 -0700 | [diff] [blame] | 1537 | return internalLockCanvas(inOutDirty, false); |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1538 | } |
| 1539 | |
John Reck | 6bc7014 | 2016-10-26 16:49:17 -0700 | [diff] [blame] | 1540 | @Override |
| 1541 | public Canvas lockHardwareCanvas() { |
| 1542 | return internalLockCanvas(null, true); |
| 1543 | } |
| 1544 | |
| 1545 | private Canvas internalLockCanvas(Rect dirty, boolean hardware) { |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1546 | mSurfaceLock.lock(); |
| 1547 | |
John Reck | aa6e84f | 2016-06-16 15:36:13 -0700 | [diff] [blame] | 1548 | if (DEBUG) Log.i(TAG, System.identityHashCode(this) + " " + "Locking canvas... stopped=" |
Robert Carr | d5c7dd6 | 2017-03-08 10:39:30 -0800 | [diff] [blame] | 1549 | + mDrawingStopped + ", surfaceControl=" + mSurfaceControl); |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1550 | |
| 1551 | Canvas c = null; |
Robert Carr | d5c7dd6 | 2017-03-08 10:39:30 -0800 | [diff] [blame] | 1552 | if (!mDrawingStopped && mSurfaceControl != null) { |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1553 | try { |
John Reck | 6bc7014 | 2016-10-26 16:49:17 -0700 | [diff] [blame] | 1554 | if (hardware) { |
| 1555 | c = mSurface.lockHardwareCanvas(); |
| 1556 | } else { |
| 1557 | c = mSurface.lockCanvas(dirty); |
| 1558 | } |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1559 | } catch (Exception e) { |
| 1560 | Log.e(LOG_TAG, "Exception locking surface", e); |
| 1561 | } |
| 1562 | } |
| 1563 | |
John Reck | aa6e84f | 2016-06-16 15:36:13 -0700 | [diff] [blame] | 1564 | 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] | 1565 | if (c != null) { |
| 1566 | mLastLockTime = SystemClock.uptimeMillis(); |
| 1567 | return c; |
| 1568 | } |
Igor Murashkin | a86ab640 | 2013-08-30 12:58:36 -0700 | [diff] [blame] | 1569 | |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1570 | // If the Surface is not ready to be drawn, then return null, |
| 1571 | // but throttle calls to this function so it isn't called more |
| 1572 | // than every 100ms. |
| 1573 | long now = SystemClock.uptimeMillis(); |
| 1574 | long nextTime = mLastLockTime + 100; |
| 1575 | if (nextTime > now) { |
| 1576 | try { |
| 1577 | Thread.sleep(nextTime-now); |
| 1578 | } catch (InterruptedException e) { |
| 1579 | } |
| 1580 | now = SystemClock.uptimeMillis(); |
| 1581 | } |
| 1582 | mLastLockTime = now; |
| 1583 | mSurfaceLock.unlock(); |
Igor Murashkin | a86ab640 | 2013-08-30 12:58:36 -0700 | [diff] [blame] | 1584 | |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1585 | return null; |
| 1586 | } |
| 1587 | |
Mathias Agopian | 9ddf32a | 2013-04-17 15:04:47 -0700 | [diff] [blame] | 1588 | /** |
| 1589 | * Posts the new contents of the {@link Canvas} to the surface and |
| 1590 | * releases the {@link Canvas}. |
| 1591 | * |
| 1592 | * @param canvas The canvas previously obtained from {@link #lockCanvas}. |
| 1593 | */ |
Igor Murashkin | a86ab640 | 2013-08-30 12:58:36 -0700 | [diff] [blame] | 1594 | @Override |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1595 | public void unlockCanvasAndPost(Canvas canvas) { |
| 1596 | mSurface.unlockCanvasAndPost(canvas); |
| 1597 | mSurfaceLock.unlock(); |
| 1598 | } |
| 1599 | |
Igor Murashkin | a86ab640 | 2013-08-30 12:58:36 -0700 | [diff] [blame] | 1600 | @Override |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1601 | public Surface getSurface() { |
| 1602 | return mSurface; |
| 1603 | } |
| 1604 | |
Igor Murashkin | a86ab640 | 2013-08-30 12:58:36 -0700 | [diff] [blame] | 1605 | @Override |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1606 | public Rect getSurfaceFrame() { |
| 1607 | return mSurfaceFrame; |
| 1608 | } |
| 1609 | }; |
Robert Carr | 5523555 | 2017-06-02 14:21:03 -0700 | [diff] [blame] | 1610 | |
chaviw | ff2e7d8 | 2018-11-02 11:11:27 -0700 | [diff] [blame] | 1611 | /** |
Robert Carr | 76907ee | 2019-01-11 13:38:19 -0800 | [diff] [blame] | 1612 | * Return a SurfaceControl which can be used for parenting Surfaces to |
| 1613 | * this SurfaceView. |
| 1614 | * |
| 1615 | * @return The SurfaceControl for this SurfaceView. |
chaviw | ff2e7d8 | 2018-11-02 11:11:27 -0700 | [diff] [blame] | 1616 | */ |
| 1617 | public SurfaceControl getSurfaceControl() { |
Robert Carr | b923f54 | 2019-05-13 12:27:51 -0700 | [diff] [blame] | 1618 | return mSurfaceControl; |
chaviw | ff2e7d8 | 2018-11-02 11:11:27 -0700 | [diff] [blame] | 1619 | } |
Vishnu Nair | 3b037f7 | 2019-07-22 12:55:58 -0700 | [diff] [blame] | 1620 | |
| 1621 | /** |
Robert Carr | 87f5d2c | 2020-01-19 17:27:00 -0800 | [diff] [blame] | 1622 | * A token used for constructing {@link SurfaceControlViewHost}. This token should |
| 1623 | * be passed from the host process to the client process. |
| 1624 | * |
| 1625 | * @return The token |
Vishnu Nair | 5cf25319 | 2019-11-07 15:33:20 -0800 | [diff] [blame] | 1626 | */ |
Robert Carr | 87f5d2c | 2020-01-19 17:27:00 -0800 | [diff] [blame] | 1627 | public @Nullable IBinder getHostToken() { |
Vishnu Nair | 5cf25319 | 2019-11-07 15:33:20 -0800 | [diff] [blame] | 1628 | final ViewRootImpl viewRoot = getViewRootImpl(); |
| 1629 | if (viewRoot == null) { |
| 1630 | return null; |
| 1631 | } |
| 1632 | return viewRoot.getInputToken(); |
| 1633 | } |
| 1634 | |
| 1635 | /** |
Vishnu Nair | 3b037f7 | 2019-07-22 12:55:58 -0700 | [diff] [blame] | 1636 | * Set window stopped to false and update surface visibility when ViewRootImpl surface is |
| 1637 | * created. |
| 1638 | * @hide |
| 1639 | */ |
| 1640 | @Override |
| 1641 | public void surfaceCreated(SurfaceControl.Transaction t) { |
| 1642 | setWindowStopped(false); |
| 1643 | } |
| 1644 | |
| 1645 | /** |
| 1646 | * Set window stopped to true and update surface visibility when ViewRootImpl surface is |
| 1647 | * destroyed. |
| 1648 | * @hide |
| 1649 | */ |
| 1650 | @Override |
| 1651 | public void surfaceDestroyed() { |
| 1652 | setWindowStopped(true); |
Jackal Guo | ac234d6 | 2020-02-03 15:05:43 +0800 | [diff] [blame] | 1653 | setRemoteAccessibilityEmbeddedConnection(null, null); |
Vishnu Nair | 3b037f7 | 2019-07-22 12:55:58 -0700 | [diff] [blame] | 1654 | } |
| 1655 | |
| 1656 | /** |
Vishnu Nair | 8cb00ae | 2019-08-02 15:20:29 -0700 | [diff] [blame] | 1657 | * Called when a valid ViewRootImpl surface is replaced by another valid surface. In this |
| 1658 | * case update relative z to the new parent surface. |
Vishnu Nair | 3b037f7 | 2019-07-22 12:55:58 -0700 | [diff] [blame] | 1659 | * @hide |
| 1660 | */ |
| 1661 | @Override |
Vishnu Nair | 8cb00ae | 2019-08-02 15:20:29 -0700 | [diff] [blame] | 1662 | public void surfaceReplaced(Transaction t) { |
| 1663 | if (mSurfaceControl != null && mBackgroundControl != null) { |
chaviw | 619da69 | 2019-06-10 15:39:40 -0700 | [diff] [blame] | 1664 | updateRelativeZ(t); |
Vishnu Nair | 8cb00ae | 2019-08-02 15:20:29 -0700 | [diff] [blame] | 1665 | } |
| 1666 | } |
Vishnu Nair | 3b037f7 | 2019-07-22 12:55:58 -0700 | [diff] [blame] | 1667 | |
chaviw | 619da69 | 2019-06-10 15:39:40 -0700 | [diff] [blame] | 1668 | private void updateRelativeZ(Transaction t) { |
Robert Carr | c34aea8 | 2020-06-16 11:47:07 -0700 | [diff] [blame] | 1669 | final ViewRootImpl viewRoot = getViewRootImpl(); |
| 1670 | if (viewRoot == null) { |
| 1671 | // We were just detached. |
| 1672 | return; |
| 1673 | } |
| 1674 | final SurfaceControl viewRootControl = viewRoot.getSurfaceControl(); |
| 1675 | t.setRelativeLayer(mBackgroundControl, viewRootControl, Integer.MIN_VALUE); |
| 1676 | t.setRelativeLayer(mSurfaceControl, viewRootControl, mSubLayer); |
Vishnu Nair | 3b037f7 | 2019-07-22 12:55:58 -0700 | [diff] [blame] | 1677 | } |
Robert Carr | 85bb940 | 2019-12-19 01:33:24 -0800 | [diff] [blame] | 1678 | |
| 1679 | /** |
Robert Carr | 87f5d2c | 2020-01-19 17:27:00 -0800 | [diff] [blame] | 1680 | * Display the view-hierarchy embedded within a {@link SurfaceControlViewHost.SurfacePackage} |
Robert Carr | 20c2396 | 2020-04-10 15:23:42 -0700 | [diff] [blame] | 1681 | * within this SurfaceView. |
| 1682 | * |
| 1683 | * This can be called independently of the SurfaceView lifetime callbacks. SurfaceView |
| 1684 | * will internally manage reparenting the package to our Surface as it is created |
| 1685 | * and destroyed. |
| 1686 | * |
| 1687 | * If this SurfaceView is above its host Surface (see |
Robert Carr | 87f5d2c | 2020-01-19 17:27:00 -0800 | [diff] [blame] | 1688 | * {@link #setZOrderOnTop} then the embedded Surface hierarchy will be able to receive |
Robert Carr | 20c2396 | 2020-04-10 15:23:42 -0700 | [diff] [blame] | 1689 | * input. |
| 1690 | * |
| 1691 | * This will take ownership of the SurfaceControl contained inside the SurfacePackage |
Robert Carr | 5af7d62 | 2020-03-17 12:04:20 -0700 | [diff] [blame] | 1692 | * and free the caller of the obligation to call |
Robert Carr | 20c2396 | 2020-04-10 15:23:42 -0700 | [diff] [blame] | 1693 | * {@link SurfaceControlViewHost.SurfacePackage#release}. However, note that |
| 1694 | * {@link SurfaceControlViewHost.SurfacePackage#release} and |
| 1695 | * {@link SurfaceControlViewHost#release} are not the same. While the ownership |
| 1696 | * of this particular {@link SurfaceControlViewHost.SurfacePackage} will be taken by the |
| 1697 | * SurfaceView the underlying {@link SurfaceControlViewHost} remains managed by it's original |
| 1698 | * remote-owner. |
Robert Carr | 87f5d2c | 2020-01-19 17:27:00 -0800 | [diff] [blame] | 1699 | * |
| 1700 | * @param p The SurfacePackage to embed. |
Robert Carr | 30ad618 | 2019-12-18 01:46:04 -0800 | [diff] [blame] | 1701 | */ |
Robert Carr | 30ad618 | 2019-12-18 01:46:04 -0800 | [diff] [blame] | 1702 | public void setChildSurfacePackage(@NonNull SurfaceControlViewHost.SurfacePackage p) { |
| 1703 | final SurfaceControl sc = p != null ? p.getSurfaceControl() : null; |
| 1704 | final SurfaceControl lastSc = mSurfacePackage != null ? |
| 1705 | mSurfacePackage.getSurfaceControl() : null; |
| 1706 | if (mSurfaceControl != null && lastSc != null) { |
| 1707 | mTmpTransaction.reparent(lastSc, null).apply(); |
Robert Carr | 5af7d62 | 2020-03-17 12:04:20 -0700 | [diff] [blame] | 1708 | mSurfacePackage.release(); |
Robert Carr | 30ad618 | 2019-12-18 01:46:04 -0800 | [diff] [blame] | 1709 | } else if (mSurfaceControl != null) { |
| 1710 | reparentSurfacePackage(mTmpTransaction, p); |
| 1711 | mTmpTransaction.apply(); |
| 1712 | } |
| 1713 | mSurfacePackage = p; |
| 1714 | } |
| 1715 | |
| 1716 | private void reparentSurfacePackage(SurfaceControl.Transaction t, |
| 1717 | SurfaceControlViewHost.SurfacePackage p) { |
Jackal Guo | ac234d6 | 2020-02-03 15:05:43 +0800 | [diff] [blame] | 1718 | initEmbeddedHierarchyForAccessibility(p); |
Robert Carr | 20c62d2 | 2020-02-05 15:25:58 -0800 | [diff] [blame] | 1719 | final SurfaceControl sc = p.getSurfaceControl(); |
| 1720 | t.reparent(sc, mSurfaceControl).show(sc); |
Robert Carr | 30ad618 | 2019-12-18 01:46:04 -0800 | [diff] [blame] | 1721 | } |
| 1722 | |
Jackal Guo | 79b182e | 2019-11-18 11:44:52 +0800 | [diff] [blame] | 1723 | /** @hide */ |
| 1724 | @Override |
| 1725 | public void onInitializeAccessibilityNodeInfoInternal(AccessibilityNodeInfo info) { |
| 1726 | super.onInitializeAccessibilityNodeInfoInternal(info); |
Jackal Guo | ac234d6 | 2020-02-03 15:05:43 +0800 | [diff] [blame] | 1727 | final RemoteAccessibilityEmbeddedConnection wrapper = |
| 1728 | getRemoteAccessibilityEmbeddedConnection(); |
| 1729 | if (wrapper == null) { |
Jackal Guo | 79b182e | 2019-11-18 11:44:52 +0800 | [diff] [blame] | 1730 | return; |
| 1731 | } |
| 1732 | // Add a leashed child when this SurfaceView embeds another view hierarchy. Getting this |
| 1733 | // leashed child would return the root node in the embedded hierarchy |
Jackal Guo | ac234d6 | 2020-02-03 15:05:43 +0800 | [diff] [blame] | 1734 | info.addChild(wrapper.getLeashToken()); |
| 1735 | } |
| 1736 | |
Jackal Guo | a88ee6f | 2020-02-11 14:20:16 +0800 | [diff] [blame] | 1737 | @Override |
| 1738 | public int getImportantForAccessibility() { |
| 1739 | final int mode = super.getImportantForAccessibility(); |
| 1740 | // If developers explicitly set the important mode for it, don't change the mode. |
| 1741 | // Only change the mode to important when this SurfaceView isn't explicitly set and has |
| 1742 | // an embedded hierarchy. |
| 1743 | if (mRemoteAccessibilityEmbeddedConnection == null |
| 1744 | || mode != IMPORTANT_FOR_ACCESSIBILITY_AUTO) { |
| 1745 | return mode; |
| 1746 | } |
| 1747 | return IMPORTANT_FOR_ACCESSIBILITY_YES; |
| 1748 | } |
| 1749 | |
Jackal Guo | ac234d6 | 2020-02-03 15:05:43 +0800 | [diff] [blame] | 1750 | private void initEmbeddedHierarchyForAccessibility(SurfaceControlViewHost.SurfacePackage p) { |
| 1751 | final IAccessibilityEmbeddedConnection connection = p.getAccessibilityEmbeddedConnection(); |
| 1752 | final RemoteAccessibilityEmbeddedConnection wrapper = |
| 1753 | getRemoteAccessibilityEmbeddedConnection(); |
| 1754 | |
| 1755 | // Do nothing if package is embedding the same view hierarchy. |
| 1756 | if (wrapper != null && wrapper.getConnection().equals(connection)) { |
| 1757 | return; |
| 1758 | } |
| 1759 | |
| 1760 | // If this SurfaceView embeds a different view hierarchy, unlink the previous one first. |
| 1761 | setRemoteAccessibilityEmbeddedConnection(null, null); |
| 1762 | |
| 1763 | try { |
| 1764 | final IBinder leashToken = connection.associateEmbeddedHierarchy( |
| 1765 | getViewRootImpl().mLeashToken, getAccessibilityViewId()); |
| 1766 | setRemoteAccessibilityEmbeddedConnection(connection, leashToken); |
| 1767 | } catch (RemoteException e) { |
| 1768 | Log.d(TAG, "Error while associateEmbeddedHierarchy " + e); |
| 1769 | } |
| 1770 | updateScreenMatrixForEmbeddedHierarchy(); |
| 1771 | } |
| 1772 | |
| 1773 | private void setRemoteAccessibilityEmbeddedConnection( |
| 1774 | IAccessibilityEmbeddedConnection connection, IBinder leashToken) { |
| 1775 | try { |
| 1776 | if (mRemoteAccessibilityEmbeddedConnection != null) { |
| 1777 | mRemoteAccessibilityEmbeddedConnection.getConnection() |
| 1778 | .disassociateEmbeddedHierarchy(); |
| 1779 | mRemoteAccessibilityEmbeddedConnection.unlinkToDeath(); |
| 1780 | mRemoteAccessibilityEmbeddedConnection = null; |
| 1781 | } |
| 1782 | if (connection != null && leashToken != null) { |
| 1783 | mRemoteAccessibilityEmbeddedConnection = |
| 1784 | new RemoteAccessibilityEmbeddedConnection(connection, leashToken); |
| 1785 | mRemoteAccessibilityEmbeddedConnection.linkToDeath(); |
| 1786 | } |
| 1787 | } catch (RemoteException e) { |
| 1788 | Log.d(TAG, "Error while setRemoteEmbeddedConnection " + e); |
| 1789 | } |
| 1790 | } |
| 1791 | |
| 1792 | private RemoteAccessibilityEmbeddedConnection getRemoteAccessibilityEmbeddedConnection() { |
| 1793 | return mRemoteAccessibilityEmbeddedConnection; |
| 1794 | } |
| 1795 | |
| 1796 | private void updateScreenMatrixForEmbeddedHierarchy() { |
Jackal Guo | a67e14a | 2020-02-20 14:01:11 +0800 | [diff] [blame] | 1797 | getBoundsOnScreen(mTmpRect); |
Jackal Guo | ac234d6 | 2020-02-03 15:05:43 +0800 | [diff] [blame] | 1798 | mTmpMatrix.reset(); |
Jackal Guo | ef364c5 | 2020-02-14 15:38:39 +0800 | [diff] [blame] | 1799 | mTmpMatrix.setTranslate(mTmpRect.left, mTmpRect.top); |
Jackal Guo | ac234d6 | 2020-02-03 15:05:43 +0800 | [diff] [blame] | 1800 | mTmpMatrix.postScale(mScreenRect.width() / (float) mSurfaceWidth, |
| 1801 | mScreenRect.height() / (float) mSurfaceHeight); |
| 1802 | |
| 1803 | // If the screen matrix is identity or doesn't change, do nothing. |
| 1804 | if (mTmpMatrix.isIdentity() || mTmpMatrix.equals(mScreenMatrixForEmbeddedHierarchy)) { |
| 1805 | return; |
| 1806 | } |
| 1807 | |
| 1808 | try { |
| 1809 | final RemoteAccessibilityEmbeddedConnection wrapper = |
| 1810 | getRemoteAccessibilityEmbeddedConnection(); |
| 1811 | if (wrapper == null) { |
| 1812 | return; |
| 1813 | } |
| 1814 | mTmpMatrix.getValues(mMatrixValues); |
| 1815 | wrapper.getConnection().setScreenMatrix(mMatrixValues); |
| 1816 | mScreenMatrixForEmbeddedHierarchy.set(mTmpMatrix); |
| 1817 | } catch (RemoteException e) { |
| 1818 | Log.d(TAG, "Error while setScreenMatrix " + e); |
| 1819 | } |
| 1820 | } |
| 1821 | |
Vishnu Nair | 34486ad | 2020-06-10 18:14:20 -0700 | [diff] [blame] | 1822 | private void notifySurfaceDestroyed() { |
| 1823 | if (mSurface.isValid()) { |
| 1824 | if (DEBUG) Log.i(TAG, System.identityHashCode(this) + " " |
| 1825 | + "surfaceDestroyed"); |
| 1826 | SurfaceHolder.Callback[] callbacks = getSurfaceCallbacks(); |
| 1827 | for (SurfaceHolder.Callback c : callbacks) { |
| 1828 | c.surfaceDestroyed(mSurfaceHolder); |
| 1829 | } |
| 1830 | // Since Android N the same surface may be reused and given to us |
| 1831 | // again by the system server at a later point. However |
| 1832 | // as we didn't do this in previous releases, clients weren't |
| 1833 | // necessarily required to clean up properly in |
| 1834 | // surfaceDestroyed. This leads to problems for example when |
| 1835 | // clients don't destroy their EGL context, and try |
| 1836 | // and create a new one on the same surface following reuse. |
| 1837 | // Since there is no valid use of the surface in-between |
| 1838 | // surfaceDestroyed and surfaceCreated, we force a disconnect, |
| 1839 | // so the next connect will always work if we end up reusing |
| 1840 | // the surface. |
| 1841 | if (mSurface.isValid()) { |
| 1842 | mSurface.forceScopedDisconnect(); |
| 1843 | } |
| 1844 | } |
| 1845 | } |
| 1846 | |
Jackal Guo | ac234d6 | 2020-02-03 15:05:43 +0800 | [diff] [blame] | 1847 | /** |
| 1848 | * Wrapper of accessibility embedded connection for embedded view hierarchy. |
| 1849 | */ |
| 1850 | private final class RemoteAccessibilityEmbeddedConnection implements IBinder.DeathRecipient { |
| 1851 | private final IAccessibilityEmbeddedConnection mConnection; |
| 1852 | private final IBinder mLeashToken; |
| 1853 | |
| 1854 | RemoteAccessibilityEmbeddedConnection(IAccessibilityEmbeddedConnection connection, |
| 1855 | IBinder leashToken) { |
| 1856 | mConnection = connection; |
| 1857 | mLeashToken = leashToken; |
| 1858 | } |
| 1859 | |
| 1860 | IAccessibilityEmbeddedConnection getConnection() { |
| 1861 | return mConnection; |
| 1862 | } |
| 1863 | |
| 1864 | IBinder getLeashToken() { |
| 1865 | return mLeashToken; |
| 1866 | } |
| 1867 | |
| 1868 | void linkToDeath() throws RemoteException { |
| 1869 | mConnection.asBinder().linkToDeath(this, 0); |
| 1870 | } |
| 1871 | |
| 1872 | void unlinkToDeath() { |
| 1873 | mConnection.asBinder().unlinkToDeath(this, 0); |
| 1874 | } |
| 1875 | |
| 1876 | @Override |
| 1877 | public void binderDied() { |
| 1878 | unlinkToDeath(); |
| 1879 | runOnUiThread(() -> { |
| 1880 | if (mRemoteAccessibilityEmbeddedConnection == this) { |
| 1881 | mRemoteAccessibilityEmbeddedConnection = null; |
| 1882 | } |
| 1883 | }); |
| 1884 | } |
Jackal Guo | 79b182e | 2019-11-18 11:44:52 +0800 | [diff] [blame] | 1885 | } |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1886 | } |