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