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