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