Dianne Hackborn | a111187 | 2010-11-23 20:55:11 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 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 com.android.server; // TODO: use com.android.server.wm, once things move there |
| 18 | |
Dianne Hackborn | f9d0be9 | 2010-11-24 12:35:25 -0800 | [diff] [blame] | 19 | import android.content.Context; |
Dianne Hackborn | a111187 | 2010-11-23 20:55:11 -0800 | [diff] [blame] | 20 | import android.graphics.Bitmap; |
| 21 | import android.graphics.Canvas; |
| 22 | import android.graphics.Color; |
| 23 | import android.graphics.Matrix; |
| 24 | import android.graphics.Paint; |
| 25 | import android.graphics.PixelFormat; |
| 26 | import android.graphics.Rect; |
| 27 | import android.util.DisplayMetrics; |
| 28 | import android.util.Slog; |
| 29 | import android.view.Display; |
| 30 | import android.view.Surface; |
| 31 | import android.view.SurfaceSession; |
Dianne Hackborn | f9d0be9 | 2010-11-24 12:35:25 -0800 | [diff] [blame] | 32 | import android.view.animation.Animation; |
| 33 | import android.view.animation.AnimationUtils; |
| 34 | import android.view.animation.Transformation; |
Dianne Hackborn | a111187 | 2010-11-23 20:55:11 -0800 | [diff] [blame] | 35 | |
| 36 | class ScreenRotationAnimation { |
Dianne Hackborn | f9d0be9 | 2010-11-24 12:35:25 -0800 | [diff] [blame] | 37 | static final String TAG = "ScreenRotationAnimation"; |
| 38 | static final boolean DEBUG = false; |
Dianne Hackborn | a111187 | 2010-11-23 20:55:11 -0800 | [diff] [blame] | 39 | |
Dianne Hackborn | f9d0be9 | 2010-11-24 12:35:25 -0800 | [diff] [blame] | 40 | final Context mContext; |
| 41 | final Display mDisplay; |
Dianne Hackborn | a111187 | 2010-11-23 20:55:11 -0800 | [diff] [blame] | 42 | Surface mSurface; |
Dianne Hackborn | 352cc98 | 2011-01-04 11:34:18 -0800 | [diff] [blame^] | 43 | Surface mBlackSurface; |
Dianne Hackborn | a111187 | 2010-11-23 20:55:11 -0800 | [diff] [blame] | 44 | int mWidth, mHeight; |
| 45 | |
Dianne Hackborn | f9d0be9 | 2010-11-24 12:35:25 -0800 | [diff] [blame] | 46 | int mSnapshotRotation; |
| 47 | int mSnapshotDeltaRotation; |
| 48 | int mOriginalRotation; |
| 49 | int mOriginalWidth, mOriginalHeight; |
Dianne Hackborn | a111187 | 2010-11-23 20:55:11 -0800 | [diff] [blame] | 50 | int mCurRotation; |
Dianne Hackborn | a111187 | 2010-11-23 20:55:11 -0800 | [diff] [blame] | 51 | |
Dianne Hackborn | f9d0be9 | 2010-11-24 12:35:25 -0800 | [diff] [blame] | 52 | Animation mExitAnimation; |
| 53 | final Transformation mExitTransformation = new Transformation(); |
| 54 | Animation mEnterAnimation; |
| 55 | final Transformation mEnterTransformation = new Transformation(); |
| 56 | boolean mStarted; |
| 57 | |
| 58 | final DisplayMetrics mDisplayMetrics = new DisplayMetrics(); |
| 59 | final Matrix mSnapshotInitialMatrix = new Matrix(); |
| 60 | final Matrix mSnapshotFinalMatrix = new Matrix(); |
Dianne Hackborn | a111187 | 2010-11-23 20:55:11 -0800 | [diff] [blame] | 61 | final float[] mTmpFloats = new float[9]; |
| 62 | |
Dianne Hackborn | f9d0be9 | 2010-11-24 12:35:25 -0800 | [diff] [blame] | 63 | public ScreenRotationAnimation(Context context, Display display, SurfaceSession session) { |
| 64 | mContext = context; |
| 65 | mDisplay = display; |
| 66 | |
| 67 | display.getMetrics(mDisplayMetrics); |
Dianne Hackborn | a111187 | 2010-11-23 20:55:11 -0800 | [diff] [blame] | 68 | |
| 69 | Bitmap screenshot = Surface.screenshot(0, 0); |
| 70 | |
| 71 | if (screenshot != null) { |
| 72 | // Screenshot does NOT include rotation! |
Dianne Hackborn | f9d0be9 | 2010-11-24 12:35:25 -0800 | [diff] [blame] | 73 | mSnapshotRotation = 0; |
Dianne Hackborn | a111187 | 2010-11-23 20:55:11 -0800 | [diff] [blame] | 74 | mWidth = screenshot.getWidth(); |
| 75 | mHeight = screenshot.getHeight(); |
| 76 | } else { |
| 77 | // Just in case. |
Dianne Hackborn | f9d0be9 | 2010-11-24 12:35:25 -0800 | [diff] [blame] | 78 | mSnapshotRotation = display.getRotation(); |
| 79 | mWidth = mDisplayMetrics.widthPixels; |
| 80 | mHeight = mDisplayMetrics.heightPixels; |
Dianne Hackborn | a111187 | 2010-11-23 20:55:11 -0800 | [diff] [blame] | 81 | } |
| 82 | |
Dianne Hackborn | f9d0be9 | 2010-11-24 12:35:25 -0800 | [diff] [blame] | 83 | mOriginalRotation = display.getRotation(); |
| 84 | mOriginalWidth = mDisplayMetrics.widthPixels; |
| 85 | mOriginalHeight = mDisplayMetrics.heightPixels; |
| 86 | |
Dianne Hackborn | a111187 | 2010-11-23 20:55:11 -0800 | [diff] [blame] | 87 | Surface.openTransaction(); |
Dianne Hackborn | 352cc98 | 2011-01-04 11:34:18 -0800 | [diff] [blame^] | 88 | |
Dianne Hackborn | a111187 | 2010-11-23 20:55:11 -0800 | [diff] [blame] | 89 | try { |
| 90 | mSurface = new Surface(session, 0, "FreezeSurface", |
| 91 | -1, mWidth, mHeight, PixelFormat.OPAQUE, 0); |
Dianne Hackborn | 352cc98 | 2011-01-04 11:34:18 -0800 | [diff] [blame^] | 92 | mSurface.setLayer(WindowManagerService.TYPE_LAYER_MULTIPLIER * 200); |
Dianne Hackborn | a111187 | 2010-11-23 20:55:11 -0800 | [diff] [blame] | 93 | } catch (Surface.OutOfResourcesException e) { |
| 94 | Slog.w(TAG, "Unable to allocate freeze surface", e); |
| 95 | } |
Dianne Hackborn | 352cc98 | 2011-01-04 11:34:18 -0800 | [diff] [blame^] | 96 | |
| 97 | if (false) { |
| 98 | try { |
| 99 | int size = mOriginalWidth > mOriginalHeight ? mOriginalWidth : mOriginalHeight; |
| 100 | mBlackSurface = new Surface(session, 0, "BlackSurface", |
| 101 | -1, size, size, PixelFormat.OPAQUE, Surface.FX_SURFACE_DIM); |
| 102 | mBlackSurface.setAlpha(1.0f); |
| 103 | mBlackSurface.setLayer(0); |
| 104 | } catch (Surface.OutOfResourcesException e) { |
| 105 | Slog.w(TAG, "Unable to allocate black surface", e); |
| 106 | } |
| 107 | } |
| 108 | |
Dianne Hackborn | a111187 | 2010-11-23 20:55:11 -0800 | [diff] [blame] | 109 | setRotation(display.getRotation()); |
| 110 | |
Dianne Hackborn | 352cc98 | 2011-01-04 11:34:18 -0800 | [diff] [blame^] | 111 | if (mSurface != null) { |
| 112 | Rect dirty = new Rect(0, 0, mWidth, mHeight); |
| 113 | Canvas c = null; |
| 114 | try { |
| 115 | c = mSurface.lockCanvas(dirty); |
| 116 | } catch (IllegalArgumentException e) { |
| 117 | Slog.w(TAG, "Unable to lock surface", e); |
| 118 | return; |
| 119 | } catch (Surface.OutOfResourcesException e) { |
| 120 | Slog.w(TAG, "Unable to lock surface", e); |
| 121 | return; |
| 122 | } |
| 123 | if (c == null) { |
| 124 | Slog.w(TAG, "Null surface"); |
| 125 | return; |
| 126 | } |
| 127 | |
| 128 | if (screenshot != null) { |
| 129 | c.drawBitmap(screenshot, 0, 0, new Paint(0)); |
| 130 | } else { |
| 131 | c.drawColor(Color.GREEN); |
| 132 | } |
| 133 | |
| 134 | mSurface.unlockCanvasAndPost(c); |
Dianne Hackborn | a111187 | 2010-11-23 20:55:11 -0800 | [diff] [blame] | 135 | } |
Dianne Hackborn | 352cc98 | 2011-01-04 11:34:18 -0800 | [diff] [blame^] | 136 | |
Dianne Hackborn | a111187 | 2010-11-23 20:55:11 -0800 | [diff] [blame] | 137 | Surface.closeTransaction(); |
| 138 | |
Dianne Hackborn | 0f761d6 | 2010-11-30 22:06:10 -0800 | [diff] [blame] | 139 | if (screenshot != null) { |
| 140 | screenshot.recycle(); |
| 141 | } |
Dianne Hackborn | a111187 | 2010-11-23 20:55:11 -0800 | [diff] [blame] | 142 | } |
| 143 | |
Dianne Hackborn | f9d0be9 | 2010-11-24 12:35:25 -0800 | [diff] [blame] | 144 | static int deltaRotation(int oldRotation, int newRotation) { |
| 145 | int delta = newRotation - oldRotation; |
Dianne Hackborn | a111187 | 2010-11-23 20:55:11 -0800 | [diff] [blame] | 146 | if (delta < 0) delta += 4; |
Dianne Hackborn | f9d0be9 | 2010-11-24 12:35:25 -0800 | [diff] [blame] | 147 | return delta; |
| 148 | } |
Dianne Hackborn | a111187 | 2010-11-23 20:55:11 -0800 | [diff] [blame] | 149 | |
Dianne Hackborn | f9d0be9 | 2010-11-24 12:35:25 -0800 | [diff] [blame] | 150 | void setSnapshotTransform(Matrix matrix, float alpha) { |
Dianne Hackborn | 352cc98 | 2011-01-04 11:34:18 -0800 | [diff] [blame^] | 151 | if (mSurface != null) { |
| 152 | matrix.getValues(mTmpFloats); |
| 153 | mSurface.setPosition((int)mTmpFloats[Matrix.MTRANS_X], |
| 154 | (int)mTmpFloats[Matrix.MTRANS_Y]); |
| 155 | mSurface.setMatrix( |
| 156 | mTmpFloats[Matrix.MSCALE_X], mTmpFloats[Matrix.MSKEW_Y], |
| 157 | mTmpFloats[Matrix.MSKEW_X], mTmpFloats[Matrix.MSCALE_Y]); |
| 158 | mSurface.setAlpha(alpha); |
| 159 | if (DEBUG) { |
| 160 | float[] srcPnts = new float[] { 0, 0, mWidth, mHeight }; |
| 161 | float[] dstPnts = new float[4]; |
| 162 | matrix.mapPoints(dstPnts, srcPnts); |
| 163 | Slog.i(TAG, "Original : (" + srcPnts[0] + "," + srcPnts[1] |
| 164 | + ")-(" + srcPnts[2] + "," + srcPnts[3] + ")"); |
| 165 | Slog.i(TAG, "Transformed: (" + dstPnts[0] + "," + dstPnts[1] |
| 166 | + ")-(" + dstPnts[2] + "," + dstPnts[3] + ")"); |
| 167 | } |
Dianne Hackborn | a111187 | 2010-11-23 20:55:11 -0800 | [diff] [blame] | 168 | } |
| 169 | } |
| 170 | |
Dianne Hackborn | 0aae2d4 | 2010-12-07 23:51:29 -0800 | [diff] [blame] | 171 | public static void createRotationMatrix(int rotation, int width, int height, |
| 172 | Matrix outMatrix) { |
| 173 | switch (rotation) { |
| 174 | case Surface.ROTATION_0: |
| 175 | outMatrix.reset(); |
| 176 | break; |
| 177 | case Surface.ROTATION_90: |
| 178 | outMatrix.setRotate(90, 0, 0); |
| 179 | outMatrix.postTranslate(height, 0); |
| 180 | break; |
| 181 | case Surface.ROTATION_180: |
| 182 | outMatrix.setRotate(180, 0, 0); |
| 183 | outMatrix.postTranslate(width, height); |
| 184 | break; |
| 185 | case Surface.ROTATION_270: |
| 186 | outMatrix.setRotate(270, 0, 0); |
| 187 | outMatrix.postTranslate(0, width); |
| 188 | break; |
| 189 | } |
| 190 | } |
| 191 | |
Dianne Hackborn | f9d0be9 | 2010-11-24 12:35:25 -0800 | [diff] [blame] | 192 | // Must be called while in a transaction. |
| 193 | public void setRotation(int rotation) { |
| 194 | mCurRotation = rotation; |
| 195 | |
| 196 | // Compute the transformation matrix that must be applied |
| 197 | // to the snapshot to make it stay in the same original position |
| 198 | // with the current screen rotation. |
| 199 | int delta = deltaRotation(rotation, mSnapshotRotation); |
Dianne Hackborn | 0aae2d4 | 2010-12-07 23:51:29 -0800 | [diff] [blame] | 200 | createRotationMatrix(delta, mWidth, mHeight, mSnapshotInitialMatrix); |
Dianne Hackborn | f9d0be9 | 2010-11-24 12:35:25 -0800 | [diff] [blame] | 201 | |
| 202 | if (DEBUG) Slog.v(TAG, "**** ROTATION: " + delta); |
| 203 | setSnapshotTransform(mSnapshotInitialMatrix, 1.0f); |
| 204 | } |
| 205 | |
| 206 | /** |
| 207 | * Returns true if animating. |
| 208 | */ |
| 209 | public boolean dismiss(long maxAnimationDuration, float animationScale) { |
| 210 | // Figure out how the screen has moved from the original rotation. |
| 211 | int delta = deltaRotation(mCurRotation, mOriginalRotation); |
| 212 | if (false && delta == 0) { |
| 213 | // Nothing changed, just remove the snapshot. |
| 214 | if (mSurface != null) { |
| 215 | mSurface.destroy(); |
| 216 | mSurface = null; |
| 217 | } |
| 218 | return false; |
| 219 | } |
| 220 | |
| 221 | switch (delta) { |
| 222 | case Surface.ROTATION_0: |
| 223 | mExitAnimation = AnimationUtils.loadAnimation(mContext, |
| 224 | com.android.internal.R.anim.screen_rotate_0_exit); |
| 225 | mEnterAnimation = AnimationUtils.loadAnimation(mContext, |
| 226 | com.android.internal.R.anim.screen_rotate_0_enter); |
| 227 | break; |
| 228 | case Surface.ROTATION_90: |
| 229 | mExitAnimation = AnimationUtils.loadAnimation(mContext, |
| 230 | com.android.internal.R.anim.screen_rotate_plus_90_exit); |
| 231 | mEnterAnimation = AnimationUtils.loadAnimation(mContext, |
| 232 | com.android.internal.R.anim.screen_rotate_plus_90_enter); |
| 233 | break; |
| 234 | case Surface.ROTATION_180: |
| 235 | mExitAnimation = AnimationUtils.loadAnimation(mContext, |
| 236 | com.android.internal.R.anim.screen_rotate_180_exit); |
| 237 | mEnterAnimation = AnimationUtils.loadAnimation(mContext, |
| 238 | com.android.internal.R.anim.screen_rotate_180_enter); |
| 239 | break; |
| 240 | case Surface.ROTATION_270: |
| 241 | mExitAnimation = AnimationUtils.loadAnimation(mContext, |
| 242 | com.android.internal.R.anim.screen_rotate_minus_90_exit); |
| 243 | mEnterAnimation = AnimationUtils.loadAnimation(mContext, |
| 244 | com.android.internal.R.anim.screen_rotate_minus_90_enter); |
| 245 | break; |
| 246 | } |
| 247 | |
| 248 | mDisplay.getMetrics(mDisplayMetrics); |
| 249 | |
| 250 | // Initialize the animations. This is a hack, redefining what "parent" |
| 251 | // means to allow supplying the last and next size. In this definition |
| 252 | // "%p" is the original (let's call it "previous") size, and "%" is the |
| 253 | // screen's current/new size. |
| 254 | mEnterAnimation.initialize(mDisplayMetrics.widthPixels, mDisplayMetrics.heightPixels, |
| 255 | mOriginalWidth, mOriginalHeight); |
| 256 | mExitAnimation.initialize(mDisplayMetrics.widthPixels, mDisplayMetrics.heightPixels, |
| 257 | mOriginalWidth, mOriginalHeight); |
| 258 | mStarted = false; |
| 259 | |
| 260 | mExitAnimation.restrictDuration(maxAnimationDuration); |
| 261 | mExitAnimation.scaleCurrentDuration(animationScale); |
| 262 | mEnterAnimation.restrictDuration(maxAnimationDuration); |
| 263 | mEnterAnimation.scaleCurrentDuration(animationScale); |
| 264 | |
| 265 | return true; |
| 266 | } |
| 267 | |
| 268 | public void kill() { |
| 269 | if (mSurface != null) { |
| 270 | mSurface.destroy(); |
| 271 | mSurface = null; |
| 272 | } |
Dianne Hackborn | 352cc98 | 2011-01-04 11:34:18 -0800 | [diff] [blame^] | 273 | if (mBlackSurface != null) { |
| 274 | mBlackSurface.destroy(); |
| 275 | mBlackSurface = null; |
| 276 | } |
Dianne Hackborn | f9d0be9 | 2010-11-24 12:35:25 -0800 | [diff] [blame] | 277 | if (mExitAnimation != null) { |
| 278 | mExitAnimation.cancel(); |
| 279 | mExitAnimation = null; |
| 280 | } |
| 281 | if (mEnterAnimation != null) { |
| 282 | mEnterAnimation.cancel(); |
| 283 | mEnterAnimation = null; |
| 284 | } |
| 285 | } |
| 286 | |
| 287 | public boolean isAnimating() { |
| 288 | return mEnterAnimation != null || mExitAnimation != null; |
| 289 | } |
| 290 | |
| 291 | public boolean stepAnimation(long now) { |
| 292 | if (mEnterAnimation == null && mExitAnimation == null) { |
| 293 | return false; |
| 294 | } |
| 295 | |
| 296 | if (!mStarted) { |
| 297 | mEnterAnimation.setStartTime(now); |
| 298 | mExitAnimation.setStartTime(now); |
| 299 | mStarted = true; |
| 300 | } |
| 301 | |
| 302 | mExitTransformation.clear(); |
| 303 | boolean moreExit = false; |
| 304 | if (mExitAnimation != null) { |
| 305 | moreExit = mExitAnimation.getTransformation(now, mExitTransformation); |
| 306 | if (DEBUG) Slog.v(TAG, "Stepped exit: " + mExitTransformation); |
| 307 | if (!moreExit) { |
| 308 | if (DEBUG) Slog.v(TAG, "Exit animation done!"); |
| 309 | mExitAnimation.cancel(); |
| 310 | mExitAnimation = null; |
| 311 | mExitTransformation.clear(); |
| 312 | if (mSurface != null) { |
| 313 | mSurface.destroy(); |
| 314 | mSurface = null; |
| 315 | } |
Dianne Hackborn | 352cc98 | 2011-01-04 11:34:18 -0800 | [diff] [blame^] | 316 | if (mBlackSurface != null) { |
| 317 | mBlackSurface.destroy(); |
| 318 | mBlackSurface = null; |
| 319 | } |
Dianne Hackborn | f9d0be9 | 2010-11-24 12:35:25 -0800 | [diff] [blame] | 320 | } |
| 321 | } |
| 322 | |
| 323 | mEnterTransformation.clear(); |
| 324 | boolean moreEnter = false; |
| 325 | if (mEnterAnimation != null) { |
| 326 | moreEnter = mEnterAnimation.getTransformation(now, mEnterTransformation); |
| 327 | if (!moreEnter) { |
| 328 | mEnterAnimation.cancel(); |
| 329 | mEnterAnimation = null; |
| 330 | mEnterTransformation.clear(); |
| 331 | } |
| 332 | } |
| 333 | |
Dianne Hackborn | 352cc98 | 2011-01-04 11:34:18 -0800 | [diff] [blame^] | 334 | mSnapshotFinalMatrix.setConcat(mExitTransformation.getMatrix(), mSnapshotInitialMatrix); |
| 335 | setSnapshotTransform(mSnapshotFinalMatrix, mExitTransformation.getAlpha()); |
Dianne Hackborn | f9d0be9 | 2010-11-24 12:35:25 -0800 | [diff] [blame] | 336 | |
| 337 | return moreEnter || moreExit; |
| 338 | } |
| 339 | |
| 340 | public Transformation getEnterTransformation() { |
| 341 | return mEnterTransformation; |
Dianne Hackborn | a111187 | 2010-11-23 20:55:11 -0800 | [diff] [blame] | 342 | } |
| 343 | } |