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