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