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