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