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 | |
Dianne Hackborn | 72c82ab | 2009-08-11 21:13:54 -0700 | [diff] [blame] | 19 | import com.android.internal.view.BaseIWindow; |
| 20 | |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 21 | import android.content.Context; |
Dianne Hackborn | e36d6e2 | 2010-02-17 19:46:25 -0800 | [diff] [blame] | 22 | import android.content.res.Configuration; |
Mitsuru Oshima | 64f5934 | 2009-06-21 00:03:11 -0700 | [diff] [blame] | 23 | import android.content.res.CompatibilityInfo.Translator; |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 24 | import android.graphics.Canvas; |
| 25 | import android.graphics.PixelFormat; |
| 26 | import android.graphics.PorterDuff; |
| 27 | import android.graphics.Rect; |
| 28 | import android.graphics.Region; |
| 29 | import android.os.Handler; |
| 30 | import android.os.Message; |
| 31 | import android.os.RemoteException; |
| 32 | import android.os.SystemClock; |
| 33 | import android.os.ParcelFileDescriptor; |
| 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 | |
Jon Larimer | 9bdf576 | 2009-01-02 18:55:15 -0500 | [diff] [blame] | 37 | import java.lang.ref.WeakReference; |
| 38 | import java.util.ArrayList; |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 39 | import java.util.concurrent.locks.ReentrantLock; |
| 40 | |
| 41 | /** |
| 42 | * Provides a dedicated drawing surface embedded inside of a view hierarchy. |
| 43 | * You can control the format of this surface and, if you like, its size; the |
| 44 | * SurfaceView takes care of placing the surface at the correct location on the |
| 45 | * screen |
Igor Murashkin | a86ab640 | 2013-08-30 12:58:36 -0700 | [diff] [blame] | 46 | * |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 47 | * <p>The surface is Z ordered so that it is behind the window holding its |
| 48 | * SurfaceView; the SurfaceView punches a hole in its window to allow its |
Jesse Hall | c9f345f | 2012-09-26 11:55:16 -0700 | [diff] [blame] | 49 | * 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] | 50 | * compositing with the Surface any siblings of the SurfaceView that would |
Jesse Hall | c9f345f | 2012-09-26 11:55:16 -0700 | [diff] [blame] | 51 | * 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] | 52 | * buttons on top of the Surface, though note however that it can have an |
| 53 | * impact on performance since a full alpha-blended composite will be performed |
| 54 | * each time the Surface changes. |
Igor Murashkin | a86ab640 | 2013-08-30 12:58:36 -0700 | [diff] [blame] | 55 | * |
Jesse Hall | c9f345f | 2012-09-26 11:55:16 -0700 | [diff] [blame] | 56 | * <p> The transparent region that makes the surface visible is based on the |
| 57 | * layout positions in the view hierarchy. If the post-layout transform |
| 58 | * properties are used to draw a sibling view on top of the SurfaceView, the |
| 59 | * view may not be properly composited with the surface. |
| 60 | * |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 61 | * <p>Access to the underlying surface is provided via the SurfaceHolder interface, |
| 62 | * which can be retrieved by calling {@link #getHolder}. |
Igor Murashkin | a86ab640 | 2013-08-30 12:58:36 -0700 | [diff] [blame] | 63 | * |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 64 | * <p>The Surface will be created for you while the SurfaceView's window is |
| 65 | * visible; you should implement {@link SurfaceHolder.Callback#surfaceCreated} |
| 66 | * and {@link SurfaceHolder.Callback#surfaceDestroyed} to discover when the |
| 67 | * Surface is created and destroyed as the window is shown and hidden. |
Igor Murashkin | a86ab640 | 2013-08-30 12:58:36 -0700 | [diff] [blame] | 68 | * |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 69 | * <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] | 70 | * 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] | 71 | * this way, you need to be aware of some threading semantics: |
Igor Murashkin | a86ab640 | 2013-08-30 12:58:36 -0700 | [diff] [blame] | 72 | * |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 73 | * <ul> |
| 74 | * <li> All SurfaceView and |
| 75 | * {@link SurfaceHolder.Callback SurfaceHolder.Callback} methods will be called |
| 76 | * from the thread running the SurfaceView's window (typically the main thread |
Jesse Hall | c9f345f | 2012-09-26 11:55:16 -0700 | [diff] [blame] | 77 | * 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] | 78 | * state that is also touched by the drawing thread. |
| 79 | * <li> You must ensure that the drawing thread only touches the underlying |
| 80 | * Surface while it is valid -- between |
| 81 | * {@link SurfaceHolder.Callback#surfaceCreated SurfaceHolder.Callback.surfaceCreated()} |
| 82 | * and |
| 83 | * {@link SurfaceHolder.Callback#surfaceDestroyed SurfaceHolder.Callback.surfaceDestroyed()}. |
| 84 | * </ul> |
| 85 | */ |
| 86 | public class SurfaceView extends View { |
| 87 | static private final String TAG = "SurfaceView"; |
| 88 | static private final boolean DEBUG = false; |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 89 | |
| 90 | final ArrayList<SurfaceHolder.Callback> mCallbacks |
| 91 | = new ArrayList<SurfaceHolder.Callback>(); |
| 92 | |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 93 | final int[] mLocation = new int[2]; |
Igor Murashkin | a86ab640 | 2013-08-30 12:58:36 -0700 | [diff] [blame] | 94 | |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 95 | final ReentrantLock mSurfaceLock = new ReentrantLock(); |
Dianne Hackborn | 61566cc | 2011-12-02 23:31:52 -0800 | [diff] [blame] | 96 | final Surface mSurface = new Surface(); // Current surface in use |
| 97 | final Surface mNewSurface = new Surface(); // New surface we are switching to |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 98 | boolean mDrawingStopped = true; |
| 99 | |
| 100 | final WindowManager.LayoutParams mLayout |
| 101 | = new WindowManager.LayoutParams(); |
| 102 | IWindowSession mSession; |
| 103 | MyWindow mWindow; |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 104 | final Rect mVisibleInsets = new Rect(); |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 105 | final Rect mWinFrame = new Rect(); |
Dianne Hackborn | c4aad01 | 2013-02-22 15:05:25 -0800 | [diff] [blame] | 106 | final Rect mOverscanInsets = new Rect(); |
The Android Open Source Project | f013e1a | 2008-12-17 18:05:43 -0800 | [diff] [blame] | 107 | final Rect mContentInsets = new Rect(); |
Dianne Hackborn | 694f79b | 2010-03-17 19:44:59 -0700 | [diff] [blame] | 108 | final Configuration mConfiguration = new Configuration(); |
Igor Murashkin | a86ab640 | 2013-08-30 12:58:36 -0700 | [diff] [blame] | 109 | |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 110 | static final int KEEP_SCREEN_ON_MSG = 1; |
| 111 | static final int GET_NEW_SURFACE_MSG = 2; |
Dianne Hackborn | 726426e | 2010-03-31 22:04:36 -0700 | [diff] [blame] | 112 | static final int UPDATE_WINDOW_MSG = 3; |
Igor Murashkin | a86ab640 | 2013-08-30 12:58:36 -0700 | [diff] [blame] | 113 | |
Dianne Hackborn | c4d5d02 | 2009-05-21 17:32:42 -0700 | [diff] [blame] | 114 | int mWindowType = WindowManager.LayoutParams.TYPE_APPLICATION_MEDIA; |
Igor Murashkin | a86ab640 | 2013-08-30 12:58:36 -0700 | [diff] [blame] | 115 | |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 116 | boolean mIsCreating = false; |
| 117 | |
| 118 | final Handler mHandler = new Handler() { |
| 119 | @Override |
| 120 | public void handleMessage(Message msg) { |
| 121 | switch (msg.what) { |
| 122 | case KEEP_SCREEN_ON_MSG: { |
| 123 | setKeepScreenOn(msg.arg1 != 0); |
| 124 | } break; |
| 125 | case GET_NEW_SURFACE_MSG: { |
| 126 | handleGetNewSurface(); |
| 127 | } break; |
Dianne Hackborn | 726426e | 2010-03-31 22:04:36 -0700 | [diff] [blame] | 128 | case UPDATE_WINDOW_MSG: { |
Dianne Hackborn | d76b67c | 2010-07-13 17:48:30 -0700 | [diff] [blame] | 129 | updateWindow(false, false); |
Dianne Hackborn | 726426e | 2010-03-31 22:04:36 -0700 | [diff] [blame] | 130 | } break; |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 131 | } |
| 132 | } |
| 133 | }; |
Igor Murashkin | a86ab640 | 2013-08-30 12:58:36 -0700 | [diff] [blame] | 134 | |
Dianne Hackborn | e2af5c8 | 2010-03-18 15:44:34 -0700 | [diff] [blame] | 135 | final ViewTreeObserver.OnScrollChangedListener mScrollChangedListener |
| 136 | = new ViewTreeObserver.OnScrollChangedListener() { |
Igor Murashkin | a86ab640 | 2013-08-30 12:58:36 -0700 | [diff] [blame] | 137 | @Override |
Dianne Hackborn | e2af5c8 | 2010-03-18 15:44:34 -0700 | [diff] [blame] | 138 | public void onScrollChanged() { |
Dianne Hackborn | d76b67c | 2010-07-13 17:48:30 -0700 | [diff] [blame] | 139 | updateWindow(false, false); |
Dianne Hackborn | e2af5c8 | 2010-03-18 15:44:34 -0700 | [diff] [blame] | 140 | } |
| 141 | }; |
Igor Murashkin | a86ab640 | 2013-08-30 12:58:36 -0700 | [diff] [blame] | 142 | |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 143 | boolean mRequestedVisible = false; |
Mathias Agopian | 6b7f1a6 | 2009-09-09 18:32:34 -0700 | [diff] [blame] | 144 | boolean mWindowVisibility = false; |
| 145 | boolean mViewVisibility = false; |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 146 | int mRequestedWidth = -1; |
| 147 | int mRequestedHeight = -1; |
Mathias Agopian | d6ddcb7 | 2010-05-24 19:00:08 -0700 | [diff] [blame] | 148 | /* Set SurfaceView's format to 565 by default to maintain backward |
| 149 | * compatibility with applications assuming this format. |
| 150 | */ |
| 151 | int mRequestedFormat = PixelFormat.RGB_565; |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 152 | |
| 153 | boolean mHaveFrame = false; |
Dianne Hackborn | 6d05fd3 | 2011-11-19 14:36:15 -0800 | [diff] [blame] | 154 | boolean mSurfaceCreated = false; |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 155 | long mLastLockTime = 0; |
Igor Murashkin | a86ab640 | 2013-08-30 12:58:36 -0700 | [diff] [blame] | 156 | |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 157 | boolean mVisible = false; |
| 158 | int mLeft = -1; |
| 159 | int mTop = -1; |
| 160 | int mWidth = -1; |
| 161 | int mHeight = -1; |
| 162 | int mFormat = -1; |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 163 | final Rect mSurfaceFrame = new Rect(); |
Dianne Hackborn | 726426e | 2010-03-31 22:04:36 -0700 | [diff] [blame] | 164 | int mLastSurfaceWidth = -1, mLastSurfaceHeight = -1; |
| 165 | boolean mUpdateWindowNeeded; |
| 166 | boolean mReportDrawNeeded; |
Mitsuru Oshima | 64f5934 | 2009-06-21 00:03:11 -0700 | [diff] [blame] | 167 | private Translator mTranslator; |
Romain Guy | f2499fa | 2011-01-26 18:31:23 -0800 | [diff] [blame] | 168 | |
| 169 | private final ViewTreeObserver.OnPreDrawListener mDrawListener = |
| 170 | new ViewTreeObserver.OnPreDrawListener() { |
| 171 | @Override |
| 172 | public boolean onPreDraw() { |
Igor Murashkin | a86ab640 | 2013-08-30 12:58:36 -0700 | [diff] [blame] | 173 | // reposition ourselves where the surface is |
Romain Guy | 0c75622 | 2011-01-26 18:45:28 -0800 | [diff] [blame] | 174 | mHaveFrame = getWidth() > 0 && getHeight() > 0; |
Romain Guy | f2499fa | 2011-01-26 18:31:23 -0800 | [diff] [blame] | 175 | updateWindow(false, false); |
| 176 | return true; |
| 177 | } |
| 178 | }; |
Romain Guy | 01d5edc | 2011-01-28 11:28:53 -0800 | [diff] [blame] | 179 | private boolean mGlobalListenersAdded; |
Romain Guy | f2499fa | 2011-01-26 18:31:23 -0800 | [diff] [blame] | 180 | |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 181 | public SurfaceView(Context context) { |
| 182 | super(context); |
Mathias Agopian | d6ddcb7 | 2010-05-24 19:00:08 -0700 | [diff] [blame] | 183 | init(); |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 184 | } |
Igor Murashkin | a86ab640 | 2013-08-30 12:58:36 -0700 | [diff] [blame] | 185 | |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 186 | public SurfaceView(Context context, AttributeSet attrs) { |
| 187 | super(context, attrs); |
Mathias Agopian | d6ddcb7 | 2010-05-24 19:00:08 -0700 | [diff] [blame] | 188 | init(); |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | public SurfaceView(Context context, AttributeSet attrs, int defStyle) { |
| 192 | super(context, attrs, defStyle); |
Mathias Agopian | d6ddcb7 | 2010-05-24 19:00:08 -0700 | [diff] [blame] | 193 | init(); |
| 194 | } |
| 195 | |
| 196 | private void init() { |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 197 | setWillNotDraw(true); |
| 198 | } |
Igor Murashkin | a86ab640 | 2013-08-30 12:58:36 -0700 | [diff] [blame] | 199 | |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 200 | /** |
| 201 | * Return the SurfaceHolder providing access and control over this |
| 202 | * SurfaceView's underlying surface. |
Igor Murashkin | a86ab640 | 2013-08-30 12:58:36 -0700 | [diff] [blame] | 203 | * |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 204 | * @return SurfaceHolder The holder of the surface. |
| 205 | */ |
| 206 | public SurfaceHolder getHolder() { |
| 207 | return mSurfaceHolder; |
| 208 | } |
| 209 | |
| 210 | @Override |
| 211 | protected void onAttachedToWindow() { |
| 212 | super.onAttachedToWindow(); |
| 213 | mParent.requestTransparentRegion(this); |
| 214 | mSession = getWindowSession(); |
| 215 | mLayout.token = getWindowToken(); |
| 216 | mLayout.setTitle("SurfaceView"); |
Mathias Agopian | 6b7f1a6 | 2009-09-09 18:32:34 -0700 | [diff] [blame] | 217 | mViewVisibility = getVisibility() == VISIBLE; |
Romain Guy | 01d5edc | 2011-01-28 11:28:53 -0800 | [diff] [blame] | 218 | |
| 219 | if (!mGlobalListenersAdded) { |
| 220 | ViewTreeObserver observer = getViewTreeObserver(); |
| 221 | observer.addOnScrollChangedListener(mScrollChangedListener); |
| 222 | observer.addOnPreDrawListener(mDrawListener); |
| 223 | mGlobalListenersAdded = true; |
| 224 | } |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | @Override |
| 228 | protected void onWindowVisibilityChanged(int visibility) { |
| 229 | super.onWindowVisibilityChanged(visibility); |
Mathias Agopian | 6b7f1a6 | 2009-09-09 18:32:34 -0700 | [diff] [blame] | 230 | mWindowVisibility = visibility == VISIBLE; |
| 231 | mRequestedVisible = mWindowVisibility && mViewVisibility; |
Dianne Hackborn | d76b67c | 2010-07-13 17:48:30 -0700 | [diff] [blame] | 232 | updateWindow(false, false); |
Mathias Agopian | 6b7f1a6 | 2009-09-09 18:32:34 -0700 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | @Override |
| 236 | public void setVisibility(int visibility) { |
| 237 | super.setVisibility(visibility); |
| 238 | mViewVisibility = visibility == VISIBLE; |
Mathias Agopian | cbeb332 | 2012-05-12 19:57:07 -0700 | [diff] [blame] | 239 | boolean newRequestedVisible = mWindowVisibility && mViewVisibility; |
| 240 | if (newRequestedVisible != mRequestedVisible) { |
| 241 | // our base class (View) invalidates the layout only when |
| 242 | // we go from/to the GONE state. However, SurfaceView needs |
| 243 | // to request a re-layout when the visibility changes at all. |
| 244 | // This is needed because the transparent region is computed |
| 245 | // as part of the layout phase, and it changes (obviously) when |
| 246 | // the visibility changes. |
| 247 | requestLayout(); |
| 248 | } |
| 249 | mRequestedVisible = newRequestedVisible; |
Dianne Hackborn | d76b67c | 2010-07-13 17:48:30 -0700 | [diff] [blame] | 250 | updateWindow(false, false); |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 251 | } |
Romain Guy | afc3e11 | 2010-06-07 17:04:33 -0700 | [diff] [blame] | 252 | |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 253 | @Override |
| 254 | protected void onDetachedFromWindow() { |
Romain Guy | 01d5edc | 2011-01-28 11:28:53 -0800 | [diff] [blame] | 255 | if (mGlobalListenersAdded) { |
| 256 | ViewTreeObserver observer = getViewTreeObserver(); |
| 257 | observer.removeOnScrollChangedListener(mScrollChangedListener); |
| 258 | observer.removeOnPreDrawListener(mDrawListener); |
| 259 | mGlobalListenersAdded = false; |
| 260 | } |
| 261 | |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 262 | mRequestedVisible = false; |
Dianne Hackborn | d76b67c | 2010-07-13 17:48:30 -0700 | [diff] [blame] | 263 | updateWindow(false, false); |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 264 | mHaveFrame = false; |
| 265 | if (mWindow != null) { |
| 266 | try { |
| 267 | mSession.remove(mWindow); |
| 268 | } catch (RemoteException ex) { |
Romain Guy | 01d5edc | 2011-01-28 11:28:53 -0800 | [diff] [blame] | 269 | // Not much we can do here... |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 270 | } |
| 271 | mWindow = null; |
| 272 | } |
| 273 | mSession = null; |
| 274 | mLayout.token = null; |
| 275 | |
| 276 | super.onDetachedFromWindow(); |
| 277 | } |
| 278 | |
| 279 | @Override |
| 280 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
Dianne Hackborn | 189ee18 | 2010-12-02 21:48:53 -0800 | [diff] [blame] | 281 | int width = mRequestedWidth >= 0 |
| 282 | ? resolveSizeAndState(mRequestedWidth, widthMeasureSpec, 0) |
| 283 | : getDefaultSize(0, widthMeasureSpec); |
| 284 | int height = mRequestedHeight >= 0 |
| 285 | ? resolveSizeAndState(mRequestedHeight, heightMeasureSpec, 0) |
| 286 | : getDefaultSize(0, heightMeasureSpec); |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 287 | setMeasuredDimension(width, height); |
| 288 | } |
Igor Murashkin | a86ab640 | 2013-08-30 12:58:36 -0700 | [diff] [blame] | 289 | |
Mathias Agopian | ef11530 | 2010-10-04 20:15:08 -0700 | [diff] [blame] | 290 | /** @hide */ |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 291 | @Override |
Mathias Agopian | 995bb9d | 2010-10-04 17:13:15 -0700 | [diff] [blame] | 292 | protected boolean setFrame(int left, int top, int right, int bottom) { |
| 293 | boolean result = super.setFrame(left, top, right, bottom); |
Dianne Hackborn | d76b67c | 2010-07-13 17:48:30 -0700 | [diff] [blame] | 294 | updateWindow(false, false); |
Mathias Agopian | 995bb9d | 2010-10-04 17:13:15 -0700 | [diff] [blame] | 295 | return result; |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | @Override |
| 299 | public boolean gatherTransparentRegion(Region region) { |
Dianne Hackborn | 1cd403e | 2009-09-14 22:29:14 -0700 | [diff] [blame] | 300 | if (mWindowType == WindowManager.LayoutParams.TYPE_APPLICATION_PANEL) { |
| 301 | return super.gatherTransparentRegion(region); |
| 302 | } |
Igor Murashkin | a86ab640 | 2013-08-30 12:58:36 -0700 | [diff] [blame] | 303 | |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 304 | boolean opaque = true; |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 305 | if ((mPrivateFlags & PFLAG_SKIP_DRAW) == 0) { |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 306 | // this view draws, remove it from the transparent region |
| 307 | opaque = super.gatherTransparentRegion(region); |
| 308 | } else if (region != null) { |
| 309 | int w = getWidth(); |
| 310 | int h = getHeight(); |
| 311 | if (w>0 && h>0) { |
| 312 | getLocationInWindow(mLocation); |
| 313 | // otherwise, punch a hole in the whole hierarchy |
| 314 | int l = mLocation[0]; |
| 315 | int t = mLocation[1]; |
| 316 | region.op(l, t, l+w, t+h, Region.Op.UNION); |
| 317 | } |
| 318 | } |
| 319 | if (PixelFormat.formatHasAlpha(mRequestedFormat)) { |
| 320 | opaque = false; |
| 321 | } |
| 322 | return opaque; |
| 323 | } |
| 324 | |
| 325 | @Override |
| 326 | public void draw(Canvas canvas) { |
Dianne Hackborn | 1cd403e | 2009-09-14 22:29:14 -0700 | [diff] [blame] | 327 | if (mWindowType != WindowManager.LayoutParams.TYPE_APPLICATION_PANEL) { |
| 328 | // draw() is not called when SKIP_DRAW is set |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 329 | if ((mPrivateFlags & PFLAG_SKIP_DRAW) == 0) { |
Dianne Hackborn | 1cd403e | 2009-09-14 22:29:14 -0700 | [diff] [blame] | 330 | // punch a whole in the view-hierarchy below us |
| 331 | canvas.drawColor(0, PorterDuff.Mode.CLEAR); |
| 332 | } |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 333 | } |
| 334 | super.draw(canvas); |
| 335 | } |
| 336 | |
| 337 | @Override |
| 338 | protected void dispatchDraw(Canvas canvas) { |
Dianne Hackborn | 1cd403e | 2009-09-14 22:29:14 -0700 | [diff] [blame] | 339 | if (mWindowType != WindowManager.LayoutParams.TYPE_APPLICATION_PANEL) { |
| 340 | // if SKIP_DRAW is cleared, draw() has already punched a hole |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 341 | if ((mPrivateFlags & PFLAG_SKIP_DRAW) == PFLAG_SKIP_DRAW) { |
Dianne Hackborn | 1cd403e | 2009-09-14 22:29:14 -0700 | [diff] [blame] | 342 | // punch a whole in the view-hierarchy below us |
| 343 | canvas.drawColor(0, PorterDuff.Mode.CLEAR); |
| 344 | } |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 345 | } |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 346 | super.dispatchDraw(canvas); |
| 347 | } |
| 348 | |
Dianne Hackborn | c4d5d02 | 2009-05-21 17:32:42 -0700 | [diff] [blame] | 349 | /** |
Dianne Hackborn | 29e4a3c | 2009-09-30 22:35:40 -0700 | [diff] [blame] | 350 | * Control whether the surface view's surface is placed on top of another |
| 351 | * regular surface view in the window (but still behind the window itself). |
| 352 | * This is typically used to place overlays on top of an underlying media |
| 353 | * surface view. |
Igor Murashkin | a86ab640 | 2013-08-30 12:58:36 -0700 | [diff] [blame] | 354 | * |
Dianne Hackborn | 29e4a3c | 2009-09-30 22:35:40 -0700 | [diff] [blame] | 355 | * <p>Note that this must be set before the surface view's containing |
| 356 | * window is attached to the window manager. |
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>Calling this overrides any previous call to {@link #setZOrderOnTop}. |
| 359 | */ |
| 360 | public void setZOrderMediaOverlay(boolean isMediaOverlay) { |
| 361 | mWindowType = isMediaOverlay |
| 362 | ? WindowManager.LayoutParams.TYPE_APPLICATION_MEDIA_OVERLAY |
| 363 | : WindowManager.LayoutParams.TYPE_APPLICATION_MEDIA; |
| 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) { |
| 381 | mWindowType = WindowManager.LayoutParams.TYPE_APPLICATION_PANEL; |
| 382 | // ensures the surface is placed below the IME |
| 383 | mLayout.flags |= WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM; |
| 384 | } else { |
| 385 | mWindowType = WindowManager.LayoutParams.TYPE_APPLICATION_MEDIA; |
| 386 | mLayout.flags &= ~WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM; |
| 387 | } |
Dianne Hackborn | 1cd403e | 2009-09-14 22:29:14 -0700 | [diff] [blame] | 388 | } |
Jeff Brown | f0681b3 | 2012-10-23 17:35:57 -0700 | [diff] [blame] | 389 | |
| 390 | /** |
| 391 | * Control whether the surface view's content should be treated as secure, |
| 392 | * preventing it from appearing in screenshots or from being viewed on |
| 393 | * non-secure displays. |
| 394 | * |
| 395 | * <p>Note that this must be set before the surface view's containing |
| 396 | * window is attached to the window manager. |
| 397 | * |
| 398 | * <p>See {@link android.view.Display#FLAG_SECURE} for details. |
| 399 | * |
| 400 | * @param isSecure True if the surface view is secure. |
| 401 | */ |
| 402 | public void setSecure(boolean isSecure) { |
| 403 | if (isSecure) { |
| 404 | mLayout.flags |= WindowManager.LayoutParams.FLAG_SECURE; |
| 405 | } else { |
| 406 | mLayout.flags &= ~WindowManager.LayoutParams.FLAG_SECURE; |
| 407 | } |
| 408 | } |
| 409 | |
Dianne Hackborn | 1cd403e | 2009-09-14 22:29:14 -0700 | [diff] [blame] | 410 | /** |
Dianne Hackborn | c4d5d02 | 2009-05-21 17:32:42 -0700 | [diff] [blame] | 411 | * Hack to allow special layering of windows. The type is one of the |
| 412 | * types in WindowManager.LayoutParams. This is a hack so: |
| 413 | * @hide |
| 414 | */ |
| 415 | public void setWindowType(int type) { |
| 416 | mWindowType = type; |
| 417 | } |
Mitsuru Oshima | 34bf2ee | 2009-07-17 09:57:28 -0700 | [diff] [blame] | 418 | |
Dianne Hackborn | d76b67c | 2010-07-13 17:48:30 -0700 | [diff] [blame] | 419 | private void updateWindow(boolean force, boolean redrawNeeded) { |
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(); |
Joe Onorato | 168173a | 2009-08-12 21:40:29 -0700 | [diff] [blame] | 424 | if (viewRoot != null) { |
| 425 | mTranslator = viewRoot.mTranslator; |
| 426 | } |
Mitsuru Oshima | 64f5934 | 2009-06-21 00:03:11 -0700 | [diff] [blame] | 427 | |
Dianne Hackborn | 5be8de3 | 2011-05-24 18:11:57 -0700 | [diff] [blame] | 428 | if (mTranslator != null) { |
| 429 | mSurface.setCompatibilityTranslator(mTranslator); |
Mitsuru Oshima | 38ed7d77 | 2009-07-21 14:39:34 -0700 | [diff] [blame] | 430 | } |
Igor Murashkin | a86ab640 | 2013-08-30 12:58:36 -0700 | [diff] [blame] | 431 | |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 432 | int myWidth = mRequestedWidth; |
| 433 | if (myWidth <= 0) myWidth = getWidth(); |
| 434 | int myHeight = mRequestedHeight; |
| 435 | if (myHeight <= 0) myHeight = getHeight(); |
Mitsuru Oshima | 001a6e52 | 2009-05-11 21:14:03 -0700 | [diff] [blame] | 436 | |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 437 | getLocationInWindow(mLocation); |
| 438 | final boolean creating = mWindow == null; |
| 439 | final boolean formatChanged = mFormat != mRequestedFormat; |
| 440 | final boolean sizeChanged = mWidth != myWidth || mHeight != myHeight; |
Dianne Hackborn | 6d05fd3 | 2011-11-19 14:36:15 -0800 | [diff] [blame] | 441 | final boolean visibleChanged = mVisible != mRequestedVisible; |
Mathias Agopian | d211230 | 2010-12-07 19:38:17 -0800 | [diff] [blame] | 442 | |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 443 | if (force || creating || formatChanged || sizeChanged || visibleChanged |
Mathias Agopian | d211230 | 2010-12-07 19:38:17 -0800 | [diff] [blame] | 444 | || mLeft != mLocation[0] || mTop != mLocation[1] |
Dianne Hackborn | d76b67c | 2010-07-13 17:48:30 -0700 | [diff] [blame] | 445 | || mUpdateWindowNeeded || mReportDrawNeeded || redrawNeeded) { |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 446 | |
Dianne Hackborn | 6d05fd3 | 2011-11-19 14:36:15 -0800 | [diff] [blame] | 447 | if (DEBUG) Log.i(TAG, "Changes: creating=" + creating |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 448 | + " format=" + formatChanged + " size=" + sizeChanged |
| 449 | + " visible=" + visibleChanged |
| 450 | + " left=" + (mLeft != mLocation[0]) |
| 451 | + " top=" + (mTop != mLocation[1])); |
Mitsuru Oshima | 64f5934 | 2009-06-21 00:03:11 -0700 | [diff] [blame] | 452 | |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 453 | try { |
| 454 | final boolean visible = mVisible = mRequestedVisible; |
| 455 | mLeft = mLocation[0]; |
| 456 | mTop = mLocation[1]; |
| 457 | mWidth = myWidth; |
| 458 | mHeight = myHeight; |
| 459 | mFormat = mRequestedFormat; |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 460 | |
Mitsuru Oshima | 64f5934 | 2009-06-21 00:03:11 -0700 | [diff] [blame] | 461 | // Scaling/Translate window's layout here because mLayout is not used elsewhere. |
Igor Murashkin | a86ab640 | 2013-08-30 12:58:36 -0700 | [diff] [blame] | 462 | |
Mitsuru Oshima | 64f5934 | 2009-06-21 00:03:11 -0700 | [diff] [blame] | 463 | // Places the window relative |
| 464 | mLayout.x = mLeft; |
| 465 | mLayout.y = mTop; |
| 466 | mLayout.width = getWidth(); |
| 467 | mLayout.height = getHeight(); |
| 468 | if (mTranslator != null) { |
| 469 | mTranslator.translateLayoutParamsInAppWindowToScreen(mLayout); |
| 470 | } |
Igor Murashkin | a86ab640 | 2013-08-30 12:58:36 -0700 | [diff] [blame] | 471 | |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 472 | mLayout.format = mRequestedFormat; |
Dianne Hackborn | 1cd403e | 2009-09-14 22:29:14 -0700 | [diff] [blame] | 473 | mLayout.flags |=WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE |
| 474 | | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE |
| 475 | | WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 476 | | WindowManager.LayoutParams.FLAG_SCALED |
| 477 | | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE |
| 478 | | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE |
| 479 | ; |
Mitsuru Oshima | 841f13c | 2009-07-17 17:23:31 -0700 | [diff] [blame] | 480 | if (!getContext().getResources().getCompatibilityInfo().supportsScreen()) { |
Adam Lesinski | 95c4297 | 2013-10-02 10:13:27 -0700 | [diff] [blame] | 481 | mLayout.privateFlags |= |
| 482 | WindowManager.LayoutParams.PRIVATE_FLAG_COMPATIBLE_WINDOW; |
Mitsuru Oshima | 841f13c | 2009-07-17 17:23:31 -0700 | [diff] [blame] | 483 | } |
Dianne Hackborn | 1c5383c | 2013-04-15 15:07:21 -0700 | [diff] [blame] | 484 | mLayout.privateFlags |= WindowManager.LayoutParams.PRIVATE_FLAG_NO_MOVE_ANIMATION; |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 485 | |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 486 | if (mWindow == null) { |
Jeff Brown | 8d0243a | 2012-09-24 15:01:47 -0700 | [diff] [blame] | 487 | Display display = getDisplay(); |
Jon Larimer | 9bdf576 | 2009-01-02 18:55:15 -0500 | [diff] [blame] | 488 | mWindow = new MyWindow(this); |
Dianne Hackborn | c4d5d02 | 2009-05-21 17:32:42 -0700 | [diff] [blame] | 489 | mLayout.type = mWindowType; |
Fabrice Di Meglio | aac0d4e | 2012-07-19 19:21:26 -0700 | [diff] [blame] | 490 | mLayout.gravity = Gravity.START|Gravity.TOP; |
Craig Mautner | 6881a10 | 2012-07-27 13:04:51 -0700 | [diff] [blame] | 491 | mSession.addToDisplayWithoutInputChannel(mWindow, mWindow.mSeq, mLayout, |
Jeff Brown | 8d0243a | 2012-09-24 15:01:47 -0700 | [diff] [blame] | 492 | mVisible ? VISIBLE : GONE, display.getDisplayId(), mContentInsets); |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 493 | } |
Igor Murashkin | a86ab640 | 2013-08-30 12:58:36 -0700 | [diff] [blame] | 494 | |
Dianne Hackborn | 726426e | 2010-03-31 22:04:36 -0700 | [diff] [blame] | 495 | boolean realSizeChanged; |
| 496 | boolean reportDrawNeeded; |
Dianne Hackborn | 6d05fd3 | 2011-11-19 14:36:15 -0800 | [diff] [blame] | 497 | |
| 498 | int relayoutResult; |
| 499 | |
Dianne Hackborn | 726426e | 2010-03-31 22:04:36 -0700 | [diff] [blame] | 500 | mSurfaceLock.lock(); |
| 501 | try { |
| 502 | mUpdateWindowNeeded = false; |
| 503 | reportDrawNeeded = mReportDrawNeeded; |
| 504 | mReportDrawNeeded = false; |
| 505 | mDrawingStopped = !visible; |
Igor Murashkin | a86ab640 | 2013-08-30 12:58:36 -0700 | [diff] [blame] | 506 | |
Dianne Hackborn | 6d05fd3 | 2011-11-19 14:36:15 -0800 | [diff] [blame] | 507 | if (DEBUG) Log.i(TAG, "Cur surface: " + mSurface); |
| 508 | |
| 509 | relayoutResult = mSession.relayout( |
Dianne Hackborn | 9a230e0 | 2011-10-06 11:51:27 -0700 | [diff] [blame] | 510 | mWindow, mWindow.mSeq, mLayout, mWidth, mHeight, |
Dianne Hackborn | 6d05fd3 | 2011-11-19 14:36:15 -0800 | [diff] [blame] | 511 | visible ? VISIBLE : GONE, |
Jeff Brown | 98365d7 | 2012-08-19 20:30:52 -0700 | [diff] [blame] | 512 | WindowManagerGlobal.RELAYOUT_DEFER_SURFACE_DESTROY, |
Dianne Hackborn | c4aad01 | 2013-02-22 15:05:25 -0800 | [diff] [blame] | 513 | mWinFrame, mOverscanInsets, mContentInsets, |
Dianne Hackborn | 6d05fd3 | 2011-11-19 14:36:15 -0800 | [diff] [blame] | 514 | mVisibleInsets, mConfiguration, mNewSurface); |
Jeff Brown | 98365d7 | 2012-08-19 20:30:52 -0700 | [diff] [blame] | 515 | if ((relayoutResult & WindowManagerGlobal.RELAYOUT_RES_FIRST_TIME) != 0) { |
Dianne Hackborn | 726426e | 2010-03-31 22:04:36 -0700 | [diff] [blame] | 516 | mReportDrawNeeded = true; |
| 517 | } |
Dianne Hackborn | 6d05fd3 | 2011-11-19 14:36:15 -0800 | [diff] [blame] | 518 | |
| 519 | if (DEBUG) Log.i(TAG, "New surface: " + mNewSurface |
Dianne Hackborn | 726426e | 2010-03-31 22:04:36 -0700 | [diff] [blame] | 520 | + ", vis=" + visible + ", frame=" + mWinFrame); |
Dianne Hackborn | 6d05fd3 | 2011-11-19 14:36:15 -0800 | [diff] [blame] | 521 | |
Dianne Hackborn | 726426e | 2010-03-31 22:04:36 -0700 | [diff] [blame] | 522 | mSurfaceFrame.left = 0; |
| 523 | mSurfaceFrame.top = 0; |
| 524 | if (mTranslator == null) { |
| 525 | mSurfaceFrame.right = mWinFrame.width(); |
| 526 | mSurfaceFrame.bottom = mWinFrame.height(); |
| 527 | } else { |
| 528 | float appInvertedScale = mTranslator.applicationInvertedScale; |
| 529 | mSurfaceFrame.right = (int) (mWinFrame.width() * appInvertedScale + 0.5f); |
| 530 | mSurfaceFrame.bottom = (int) (mWinFrame.height() * appInvertedScale + 0.5f); |
| 531 | } |
Igor Murashkin | a86ab640 | 2013-08-30 12:58:36 -0700 | [diff] [blame] | 532 | |
Dianne Hackborn | 726426e | 2010-03-31 22:04:36 -0700 | [diff] [blame] | 533 | final int surfaceWidth = mSurfaceFrame.right; |
| 534 | final int surfaceHeight = mSurfaceFrame.bottom; |
| 535 | realSizeChanged = mLastSurfaceWidth != surfaceWidth |
| 536 | || mLastSurfaceHeight != surfaceHeight; |
| 537 | mLastSurfaceWidth = surfaceWidth; |
| 538 | mLastSurfaceHeight = surfaceHeight; |
| 539 | } finally { |
| 540 | mSurfaceLock.unlock(); |
Mitsuru Oshima | 589cebe | 2009-07-22 20:38:58 -0700 | [diff] [blame] | 541 | } |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 542 | |
| 543 | try { |
Dianne Hackborn | d76b67c | 2010-07-13 17:48:30 -0700 | [diff] [blame] | 544 | redrawNeeded |= creating | reportDrawNeeded; |
| 545 | |
Dianne Hackborn | 6d05fd3 | 2011-11-19 14:36:15 -0800 | [diff] [blame] | 546 | SurfaceHolder.Callback callbacks[] = null; |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 547 | |
Jeff Brown | 98365d7 | 2012-08-19 20:30:52 -0700 | [diff] [blame] | 548 | final boolean surfaceChanged = (relayoutResult |
| 549 | & WindowManagerGlobal.RELAYOUT_RES_SURFACE_CHANGED) != 0; |
Dianne Hackborn | 6d05fd3 | 2011-11-19 14:36:15 -0800 | [diff] [blame] | 550 | if (mSurfaceCreated && (surfaceChanged || (!visible && visibleChanged))) { |
| 551 | mSurfaceCreated = false; |
| 552 | if (mSurface.isValid()) { |
| 553 | if (DEBUG) Log.i(TAG, "visibleChanged -- surfaceDestroyed"); |
| 554 | callbacks = getSurfaceCallbacks(); |
| 555 | for (SurfaceHolder.Callback c : callbacks) { |
| 556 | c.surfaceDestroyed(mSurfaceHolder); |
| 557 | } |
Mitsuru Oshima | 3d91492 | 2009-05-13 22:29:15 -0700 | [diff] [blame] | 558 | } |
Dianne Hackborn | 6d05fd3 | 2011-11-19 14:36:15 -0800 | [diff] [blame] | 559 | } |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 560 | |
Dianne Hackborn | 61566cc | 2011-12-02 23:31:52 -0800 | [diff] [blame] | 561 | mSurface.transferFrom(mNewSurface); |
Dianne Hackborn | 6d05fd3 | 2011-11-19 14:36:15 -0800 | [diff] [blame] | 562 | |
Andreas Röhl | f750b8c | 2012-07-02 13:06:26 +0200 | [diff] [blame] | 563 | if (visible && mSurface.isValid()) { |
Dianne Hackborn | 6d05fd3 | 2011-11-19 14:36:15 -0800 | [diff] [blame] | 564 | if (!mSurfaceCreated && (surfaceChanged || visibleChanged)) { |
| 565 | mSurfaceCreated = true; |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 566 | mIsCreating = true; |
Dianne Hackborn | 6d05fd3 | 2011-11-19 14:36:15 -0800 | [diff] [blame] | 567 | if (DEBUG) Log.i(TAG, "visibleChanged -- surfaceCreated"); |
| 568 | if (callbacks == null) { |
| 569 | callbacks = getSurfaceCallbacks(); |
| 570 | } |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 571 | for (SurfaceHolder.Callback c : callbacks) { |
| 572 | c.surfaceCreated(mSurfaceHolder); |
| 573 | } |
| 574 | } |
| 575 | if (creating || formatChanged || sizeChanged |
Dianne Hackborn | 726426e | 2010-03-31 22:04:36 -0700 | [diff] [blame] | 576 | || visibleChanged || realSizeChanged) { |
Dianne Hackborn | 6d05fd3 | 2011-11-19 14:36:15 -0800 | [diff] [blame] | 577 | if (DEBUG) Log.i(TAG, "surfaceChanged -- format=" + mFormat |
| 578 | + " w=" + myWidth + " h=" + myHeight); |
| 579 | if (callbacks == null) { |
| 580 | callbacks = getSurfaceCallbacks(); |
| 581 | } |
Dianne Hackborn | 251fd43 | 2010-07-14 16:56:31 -0700 | [diff] [blame] | 582 | for (SurfaceHolder.Callback c : callbacks) { |
| 583 | c.surfaceChanged(mSurfaceHolder, mFormat, myWidth, myHeight); |
| 584 | } |
Dianne Hackborn | d76b67c | 2010-07-13 17:48:30 -0700 | [diff] [blame] | 585 | } |
| 586 | if (redrawNeeded) { |
Dianne Hackborn | 6d05fd3 | 2011-11-19 14:36:15 -0800 | [diff] [blame] | 587 | if (DEBUG) Log.i(TAG, "surfaceRedrawNeeded"); |
| 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) { |
Dianne Hackborn | d76b67c | 2010-07-13 17:48:30 -0700 | [diff] [blame] | 592 | if (c instanceof SurfaceHolder.Callback2) { |
| 593 | ((SurfaceHolder.Callback2)c).surfaceRedrawNeeded( |
| 594 | mSurfaceHolder); |
| 595 | } |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 596 | } |
| 597 | } |
| 598 | } |
| 599 | } finally { |
| 600 | mIsCreating = false; |
Dianne Hackborn | d76b67c | 2010-07-13 17:48:30 -0700 | [diff] [blame] | 601 | if (redrawNeeded) { |
Dianne Hackborn | 6d05fd3 | 2011-11-19 14:36:15 -0800 | [diff] [blame] | 602 | if (DEBUG) Log.i(TAG, "finishedDrawing"); |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 603 | mSession.finishDrawing(mWindow); |
| 604 | } |
Dianne Hackborn | 6d05fd3 | 2011-11-19 14:36:15 -0800 | [diff] [blame] | 605 | mSession.performDeferredDestroy(mWindow); |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 606 | } |
| 607 | } catch (RemoteException ex) { |
| 608 | } |
Dianne Hackborn | 6d05fd3 | 2011-11-19 14:36:15 -0800 | [diff] [blame] | 609 | if (DEBUG) Log.v( |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 610 | TAG, "Layout: x=" + mLayout.x + " y=" + mLayout.y + |
| 611 | " w=" + mLayout.width + " h=" + mLayout.height + |
| 612 | ", frame=" + mSurfaceFrame); |
| 613 | } |
| 614 | } |
| 615 | |
Dianne Hackborn | 6d05fd3 | 2011-11-19 14:36:15 -0800 | [diff] [blame] | 616 | private SurfaceHolder.Callback[] getSurfaceCallbacks() { |
| 617 | SurfaceHolder.Callback callbacks[]; |
| 618 | synchronized (mCallbacks) { |
| 619 | callbacks = new SurfaceHolder.Callback[mCallbacks.size()]; |
| 620 | mCallbacks.toArray(callbacks); |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 621 | } |
Dianne Hackborn | 6d05fd3 | 2011-11-19 14:36:15 -0800 | [diff] [blame] | 622 | return callbacks; |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 623 | } |
| 624 | |
| 625 | void handleGetNewSurface() { |
Dianne Hackborn | d76b67c | 2010-07-13 17:48:30 -0700 | [diff] [blame] | 626 | updateWindow(false, false); |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 627 | } |
The Android Open Source Project | b798689 | 2009-01-09 17:51:23 -0800 | [diff] [blame] | 628 | |
Derek Sollenberger | 7179b81 | 2010-03-22 13:41:20 -0400 | [diff] [blame] | 629 | /** |
| 630 | * Check to see if the surface has fixed size dimensions or if the surface's |
| 631 | * dimensions are dimensions are dependent on its current layout. |
| 632 | * |
| 633 | * @return true if the surface has dimensions that are fixed in size |
| 634 | * @hide |
| 635 | */ |
| 636 | public boolean isFixedSize() { |
| 637 | return (mRequestedWidth != -1 || mRequestedHeight != -1); |
| 638 | } |
| 639 | |
Dianne Hackborn | 72c82ab | 2009-08-11 21:13:54 -0700 | [diff] [blame] | 640 | private static class MyWindow extends BaseIWindow { |
Mitsuru Oshima | 8169dae | 2009-04-28 18:12:09 -0700 | [diff] [blame] | 641 | private final WeakReference<SurfaceView> mSurfaceView; |
Jon Larimer | 9bdf576 | 2009-01-02 18:55:15 -0500 | [diff] [blame] | 642 | |
| 643 | public MyWindow(SurfaceView surfaceView) { |
| 644 | mSurfaceView = new WeakReference<SurfaceView>(surfaceView); |
| 645 | } |
| 646 | |
Craig Mautner | 656e3af | 2012-09-06 15:04:22 -0700 | [diff] [blame] | 647 | @Override |
Dianne Hackborn | c4aad01 | 2013-02-22 15:05:25 -0800 | [diff] [blame] | 648 | public void resized(Rect frame, Rect overscanInsets, Rect contentInsets, |
Dianne Hackborn | e36d6e2 | 2010-02-17 19:46:25 -0800 | [diff] [blame] | 649 | Rect visibleInsets, boolean reportDraw, Configuration newConfig) { |
Jon Larimer | 9bdf576 | 2009-01-02 18:55:15 -0500 | [diff] [blame] | 650 | SurfaceView surfaceView = mSurfaceView.get(); |
| 651 | if (surfaceView != null) { |
Dianne Hackborn | 6d05fd3 | 2011-11-19 14:36:15 -0800 | [diff] [blame] | 652 | if (DEBUG) Log.v( |
Craig Mautner | 656e3af | 2012-09-06 15:04:22 -0700 | [diff] [blame] | 653 | "SurfaceView", surfaceView + " got resized: w=" + frame.width() |
| 654 | + " h=" + frame.height() + ", cur w=" + mCurWidth + " h=" + mCurHeight); |
Dianne Hackborn | 726426e | 2010-03-31 22:04:36 -0700 | [diff] [blame] | 655 | surfaceView.mSurfaceLock.lock(); |
| 656 | try { |
Jon Larimer | 9bdf576 | 2009-01-02 18:55:15 -0500 | [diff] [blame] | 657 | if (reportDraw) { |
Dianne Hackborn | 726426e | 2010-03-31 22:04:36 -0700 | [diff] [blame] | 658 | surfaceView.mUpdateWindowNeeded = true; |
| 659 | surfaceView.mReportDrawNeeded = true; |
| 660 | surfaceView.mHandler.sendEmptyMessage(UPDATE_WINDOW_MSG); |
Craig Mautner | 656e3af | 2012-09-06 15:04:22 -0700 | [diff] [blame] | 661 | } else if (surfaceView.mWinFrame.width() != frame.width() |
| 662 | || surfaceView.mWinFrame.height() != frame.height()) { |
Dianne Hackborn | 726426e | 2010-03-31 22:04:36 -0700 | [diff] [blame] | 663 | surfaceView.mUpdateWindowNeeded = true; |
| 664 | surfaceView.mHandler.sendEmptyMessage(UPDATE_WINDOW_MSG); |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 665 | } |
Dianne Hackborn | 726426e | 2010-03-31 22:04:36 -0700 | [diff] [blame] | 666 | } finally { |
| 667 | surfaceView.mSurfaceLock.unlock(); |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 668 | } |
| 669 | } |
| 670 | } |
| 671 | |
Igor Murashkin | a86ab640 | 2013-08-30 12:58:36 -0700 | [diff] [blame] | 672 | @Override |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 673 | public void dispatchAppVisibility(boolean visible) { |
| 674 | // The point of SurfaceView is to let the app control the surface. |
| 675 | } |
The Android Open Source Project | b798689 | 2009-01-09 17:51:23 -0800 | [diff] [blame] | 676 | |
Igor Murashkin | a86ab640 | 2013-08-30 12:58:36 -0700 | [diff] [blame] | 677 | @Override |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 678 | public void dispatchGetNewSurface() { |
Jon Larimer | 9bdf576 | 2009-01-02 18:55:15 -0500 | [diff] [blame] | 679 | SurfaceView surfaceView = mSurfaceView.get(); |
| 680 | if (surfaceView != null) { |
The Android Open Source Project | b798689 | 2009-01-09 17:51:23 -0800 | [diff] [blame] | 681 | Message msg = surfaceView.mHandler.obtainMessage(GET_NEW_SURFACE_MSG); |
| 682 | surfaceView.mHandler.sendMessage(msg); |
Jon Larimer | 9bdf576 | 2009-01-02 18:55:15 -0500 | [diff] [blame] | 683 | } |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 684 | } |
| 685 | |
Igor Murashkin | a86ab640 | 2013-08-30 12:58:36 -0700 | [diff] [blame] | 686 | @Override |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 687 | public void windowFocusChanged(boolean hasFocus, boolean touchEnabled) { |
| 688 | Log.w("SurfaceView", "Unexpected focus in surface: focus=" + hasFocus + ", touchEnabled=" + touchEnabled); |
| 689 | } |
| 690 | |
Igor Murashkin | a86ab640 | 2013-08-30 12:58:36 -0700 | [diff] [blame] | 691 | @Override |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 692 | public void executeCommand(String command, String parameters, ParcelFileDescriptor out) { |
| 693 | } |
| 694 | |
| 695 | int mCurWidth = -1; |
| 696 | int mCurHeight = -1; |
| 697 | } |
| 698 | |
Igor Murashkin | a86ab640 | 2013-08-30 12:58:36 -0700 | [diff] [blame] | 699 | private final SurfaceHolder mSurfaceHolder = new SurfaceHolder() { |
| 700 | |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 701 | private static final String LOG_TAG = "SurfaceHolder"; |
Igor Murashkin | a86ab640 | 2013-08-30 12:58:36 -0700 | [diff] [blame] | 702 | |
| 703 | @Override |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 704 | public boolean isCreating() { |
| 705 | return mIsCreating; |
| 706 | } |
| 707 | |
Igor Murashkin | a86ab640 | 2013-08-30 12:58:36 -0700 | [diff] [blame] | 708 | @Override |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 709 | public void addCallback(Callback callback) { |
| 710 | synchronized (mCallbacks) { |
Igor Murashkin | a86ab640 | 2013-08-30 12:58:36 -0700 | [diff] [blame] | 711 | // 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] | 712 | // have only a couple callbacks, so it doesn't matter. |
Igor Murashkin | a86ab640 | 2013-08-30 12:58:36 -0700 | [diff] [blame] | 713 | if (mCallbacks.contains(callback) == false) { |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 714 | mCallbacks.add(callback); |
| 715 | } |
| 716 | } |
| 717 | } |
| 718 | |
Igor Murashkin | a86ab640 | 2013-08-30 12:58:36 -0700 | [diff] [blame] | 719 | @Override |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 720 | public void removeCallback(Callback callback) { |
| 721 | synchronized (mCallbacks) { |
| 722 | mCallbacks.remove(callback); |
| 723 | } |
| 724 | } |
Igor Murashkin | a86ab640 | 2013-08-30 12:58:36 -0700 | [diff] [blame] | 725 | |
| 726 | @Override |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 727 | public void setFixedSize(int width, int height) { |
| 728 | if (mRequestedWidth != width || mRequestedHeight != height) { |
| 729 | mRequestedWidth = width; |
| 730 | mRequestedHeight = height; |
| 731 | requestLayout(); |
| 732 | } |
| 733 | } |
| 734 | |
Igor Murashkin | a86ab640 | 2013-08-30 12:58:36 -0700 | [diff] [blame] | 735 | @Override |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 736 | public void setSizeFromLayout() { |
| 737 | if (mRequestedWidth != -1 || mRequestedHeight != -1) { |
| 738 | mRequestedWidth = mRequestedHeight = -1; |
| 739 | requestLayout(); |
| 740 | } |
| 741 | } |
| 742 | |
Igor Murashkin | a86ab640 | 2013-08-30 12:58:36 -0700 | [diff] [blame] | 743 | @Override |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 744 | public void setFormat(int format) { |
Mathias Agopian | 2d468c5 | 2010-06-14 21:50:48 -0700 | [diff] [blame] | 745 | |
| 746 | // for backward compatibility reason, OPAQUE always |
| 747 | // means 565 for SurfaceView |
| 748 | if (format == PixelFormat.OPAQUE) |
| 749 | format = PixelFormat.RGB_565; |
| 750 | |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 751 | mRequestedFormat = format; |
| 752 | if (mWindow != null) { |
Dianne Hackborn | d76b67c | 2010-07-13 17:48:30 -0700 | [diff] [blame] | 753 | updateWindow(false, false); |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 754 | } |
| 755 | } |
| 756 | |
Mathias Agopian | d211230 | 2010-12-07 19:38:17 -0800 | [diff] [blame] | 757 | /** |
| 758 | * @deprecated setType is now ignored. |
| 759 | */ |
Igor Murashkin | a86ab640 | 2013-08-30 12:58:36 -0700 | [diff] [blame] | 760 | @Override |
Mathias Agopian | d211230 | 2010-12-07 19:38:17 -0800 | [diff] [blame] | 761 | @Deprecated |
| 762 | public void setType(int type) { } |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 763 | |
Igor Murashkin | a86ab640 | 2013-08-30 12:58:36 -0700 | [diff] [blame] | 764 | @Override |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 765 | public void setKeepScreenOn(boolean screenOn) { |
| 766 | Message msg = mHandler.obtainMessage(KEEP_SCREEN_ON_MSG); |
| 767 | msg.arg1 = screenOn ? 1 : 0; |
| 768 | mHandler.sendMessage(msg); |
| 769 | } |
Igor Murashkin | a86ab640 | 2013-08-30 12:58:36 -0700 | [diff] [blame] | 770 | |
Mathias Agopian | 9ddf32a | 2013-04-17 15:04:47 -0700 | [diff] [blame] | 771 | /** |
| 772 | * Gets a {@link Canvas} for drawing into the SurfaceView's Surface |
| 773 | * |
| 774 | * After drawing into the provided {@link Canvas}, the caller must |
| 775 | * invoke {@link #unlockCanvasAndPost} to post the new contents to the surface. |
| 776 | * |
| 777 | * The caller must redraw the entire surface. |
| 778 | * @return A canvas for drawing into the surface. |
| 779 | */ |
Igor Murashkin | a86ab640 | 2013-08-30 12:58:36 -0700 | [diff] [blame] | 780 | @Override |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 781 | public Canvas lockCanvas() { |
| 782 | return internalLockCanvas(null); |
| 783 | } |
| 784 | |
Mathias Agopian | 9ddf32a | 2013-04-17 15:04:47 -0700 | [diff] [blame] | 785 | /** |
| 786 | * Gets a {@link Canvas} for drawing into the SurfaceView's Surface |
| 787 | * |
| 788 | * After drawing into the provided {@link Canvas}, the caller must |
| 789 | * invoke {@link #unlockCanvasAndPost} to post the new contents to the surface. |
| 790 | * |
| 791 | * @param inOutDirty A rectangle that represents the dirty region that the caller wants |
| 792 | * to redraw. This function may choose to expand the dirty rectangle if for example |
| 793 | * the surface has been resized or if the previous contents of the surface were |
| 794 | * not available. The caller must redraw the entire dirty region as represented |
| 795 | * by the contents of the inOutDirty rectangle upon return from this function. |
| 796 | * The caller may also pass <code>null</code> instead, in the case where the |
| 797 | * entire surface should be redrawn. |
| 798 | * @return A canvas for drawing into the surface. |
| 799 | */ |
Igor Murashkin | a86ab640 | 2013-08-30 12:58:36 -0700 | [diff] [blame] | 800 | @Override |
Mathias Agopian | 9ddf32a | 2013-04-17 15:04:47 -0700 | [diff] [blame] | 801 | public Canvas lockCanvas(Rect inOutDirty) { |
| 802 | return internalLockCanvas(inOutDirty); |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 803 | } |
| 804 | |
| 805 | private final Canvas internalLockCanvas(Rect dirty) { |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 806 | mSurfaceLock.lock(); |
| 807 | |
Dianne Hackborn | 6d05fd3 | 2011-11-19 14:36:15 -0800 | [diff] [blame] | 808 | if (DEBUG) Log.i(TAG, "Locking canvas... stopped=" |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 809 | + mDrawingStopped + ", win=" + mWindow); |
| 810 | |
| 811 | Canvas c = null; |
| 812 | if (!mDrawingStopped && mWindow != null) { |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 813 | try { |
Jeff Brown | 30bc34f | 2011-01-25 12:56:56 -0800 | [diff] [blame] | 814 | c = mSurface.lockCanvas(dirty); |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 815 | } catch (Exception e) { |
| 816 | Log.e(LOG_TAG, "Exception locking surface", e); |
| 817 | } |
| 818 | } |
| 819 | |
Dianne Hackborn | 6d05fd3 | 2011-11-19 14:36:15 -0800 | [diff] [blame] | 820 | if (DEBUG) Log.i(TAG, "Returned canvas: " + c); |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 821 | if (c != null) { |
| 822 | mLastLockTime = SystemClock.uptimeMillis(); |
| 823 | return c; |
| 824 | } |
Igor Murashkin | a86ab640 | 2013-08-30 12:58:36 -0700 | [diff] [blame] | 825 | |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 826 | // If the Surface is not ready to be drawn, then return null, |
| 827 | // but throttle calls to this function so it isn't called more |
| 828 | // than every 100ms. |
| 829 | long now = SystemClock.uptimeMillis(); |
| 830 | long nextTime = mLastLockTime + 100; |
| 831 | if (nextTime > now) { |
| 832 | try { |
| 833 | Thread.sleep(nextTime-now); |
| 834 | } catch (InterruptedException e) { |
| 835 | } |
| 836 | now = SystemClock.uptimeMillis(); |
| 837 | } |
| 838 | mLastLockTime = now; |
| 839 | mSurfaceLock.unlock(); |
Igor Murashkin | a86ab640 | 2013-08-30 12:58:36 -0700 | [diff] [blame] | 840 | |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 841 | return null; |
| 842 | } |
| 843 | |
Mathias Agopian | 9ddf32a | 2013-04-17 15:04:47 -0700 | [diff] [blame] | 844 | /** |
| 845 | * Posts the new contents of the {@link Canvas} to the surface and |
| 846 | * releases the {@link Canvas}. |
| 847 | * |
| 848 | * @param canvas The canvas previously obtained from {@link #lockCanvas}. |
| 849 | */ |
Igor Murashkin | a86ab640 | 2013-08-30 12:58:36 -0700 | [diff] [blame] | 850 | @Override |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 851 | public void unlockCanvasAndPost(Canvas canvas) { |
| 852 | mSurface.unlockCanvasAndPost(canvas); |
| 853 | mSurfaceLock.unlock(); |
| 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 Surface getSurface() { |
| 858 | return mSurface; |
| 859 | } |
| 860 | |
Igor Murashkin | a86ab640 | 2013-08-30 12:58:36 -0700 | [diff] [blame] | 861 | @Override |
The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 862 | public Rect getSurfaceFrame() { |
| 863 | return mSurfaceFrame; |
| 864 | } |
| 865 | }; |
| 866 | } |