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