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