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