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