blob: e25638f248886e4dab3ce062abad02e3fc9ad01e [file] [log] [blame]
Dianne Hackborna1111872010-11-23 20:55:11 -08001/*
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
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080017package com.android.server.wm;
Dianne Hackborna1111872010-11-23 20:55:11 -080018
Dianne Hackbornf9d0be92010-11-24 12:35:25 -080019import android.content.Context;
Dianne Hackborna1111872010-11-23 20:55:11 -080020import android.graphics.Bitmap;
21import android.graphics.Canvas;
Dianne Hackborna1111872010-11-23 20:55:11 -080022import android.graphics.Matrix;
23import android.graphics.Paint;
24import android.graphics.PixelFormat;
Jeff Brown8db9ac42011-01-21 14:39:52 -080025import android.graphics.PorterDuff;
26import android.graphics.PorterDuffXfermode;
Dianne Hackborna1111872010-11-23 20:55:11 -080027import android.graphics.Rect;
Dianne Hackborna1111872010-11-23 20:55:11 -080028import android.util.Slog;
Dianne Hackborna1111872010-11-23 20:55:11 -080029import android.view.Surface;
30import android.view.SurfaceSession;
Dianne Hackbornf9d0be92010-11-24 12:35:25 -080031import android.view.animation.Animation;
32import android.view.animation.AnimationUtils;
33import android.view.animation.Transformation;
Dianne Hackborna1111872010-11-23 20:55:11 -080034
35class ScreenRotationAnimation {
Dianne Hackbornf9d0be92010-11-24 12:35:25 -080036 static final String TAG = "ScreenRotationAnimation";
37 static final boolean DEBUG = false;
Dianne Hackborna1111872010-11-23 20:55:11 -080038
Dianne Hackborn50660e22011-02-02 17:12:25 -080039 static final int FREEZE_LAYER = WindowManagerService.TYPE_LAYER_MULTIPLIER * 200;
40
Dianne Hackbornf9d0be92010-11-24 12:35:25 -080041 final Context mContext;
Dianne Hackborna1111872010-11-23 20:55:11 -080042 Surface mSurface;
Dianne Hackborn7916ac62011-05-16 20:45:48 -070043 BlackFrame mBlackFrame;
Dianne Hackborna1111872010-11-23 20:55:11 -080044 int mWidth, mHeight;
45
Dianne Hackbornf9d0be92010-11-24 12:35:25 -080046 int mSnapshotRotation;
47 int mSnapshotDeltaRotation;
48 int mOriginalRotation;
49 int mOriginalWidth, mOriginalHeight;
Dianne Hackborna1111872010-11-23 20:55:11 -080050 int mCurRotation;
Dianne Hackborna1111872010-11-23 20:55:11 -080051
Dianne Hackbornf9d0be92010-11-24 12:35:25 -080052 Animation mExitAnimation;
53 final Transformation mExitTransformation = new Transformation();
54 Animation mEnterAnimation;
55 final Transformation mEnterTransformation = new Transformation();
56 boolean mStarted;
57
Dianne Hackbornf9d0be92010-11-24 12:35:25 -080058 final Matrix mSnapshotInitialMatrix = new Matrix();
59 final Matrix mSnapshotFinalMatrix = new Matrix();
Dianne Hackborn50660e22011-02-02 17:12:25 -080060 final Matrix mTmpMatrix = new Matrix();
Dianne Hackborna1111872010-11-23 20:55:11 -080061 final float[] mTmpFloats = new float[9];
62
Jeff Brownbc68a592011-07-25 12:58:12 -070063 public ScreenRotationAnimation(Context context, SurfaceSession session,
64 boolean inTransaction, int originalWidth, int originalHeight, int originalRotation) {
Dianne Hackbornf9d0be92010-11-24 12:35:25 -080065 mContext = context;
Dianne Hackborna1111872010-11-23 20:55:11 -080066
67 Bitmap screenshot = Surface.screenshot(0, 0);
68
Dianne Hackbornde75cb42011-03-02 17:11:21 -080069 if (screenshot == null) {
70 // Device is not capable of screenshots... we can't do an animation.
71 return;
Dianne Hackborna1111872010-11-23 20:55:11 -080072 }
73
Dianne Hackbornde75cb42011-03-02 17:11:21 -080074 // Screenshot does NOT include rotation!
75 mSnapshotRotation = 0;
76 mWidth = screenshot.getWidth();
77 mHeight = screenshot.getHeight();
78
Jeff Brownbc68a592011-07-25 12:58:12 -070079 mOriginalRotation = originalRotation;
80 mOriginalWidth = originalWidth;
81 mOriginalHeight = originalHeight;
Dianne Hackbornf9d0be92010-11-24 12:35:25 -080082
Dianne Hackborn94cb2eb2011-01-13 21:09:44 -080083 if (!inTransaction) {
84 if (WindowManagerService.SHOW_TRANSACTIONS) Slog.i(WindowManagerService.TAG,
85 ">>> OPEN TRANSACTION ScreenRotationAnimation");
86 Surface.openTransaction();
87 }
Dianne Hackborn352cc982011-01-04 11:34:18 -080088
Dianne Hackborna1111872010-11-23 20:55:11 -080089 try {
Dianne Hackborn352cc982011-01-04 11:34:18 -080090 try {
Dianne Hackborn94cb2eb2011-01-13 21:09:44 -080091 mSurface = new Surface(session, 0, "FreezeSurface",
92 -1, mWidth, mHeight, PixelFormat.OPAQUE, 0);
Dianne Hackborn50660e22011-02-02 17:12:25 -080093 mSurface.setLayer(FREEZE_LAYER + 1);
Dianne Hackborn352cc982011-01-04 11:34:18 -080094 } catch (Surface.OutOfResourcesException e) {
Dianne Hackborn94cb2eb2011-01-13 21:09:44 -080095 Slog.w(TAG, "Unable to allocate freeze surface", e);
Dianne Hackborn352cc982011-01-04 11:34:18 -080096 }
Dianne Hackborna1111872010-11-23 20:55:11 -080097
Dianne Hackborn5fd21692011-06-07 14:09:47 -070098 if (WindowManagerService.SHOW_TRANSACTIONS ||
99 WindowManagerService.SHOW_SURFACE_ALLOC) Slog.i(WindowManagerService.TAG,
100 " FREEZE " + mSurface + ": CREATE");
101
Jeff Brownbc68a592011-07-25 12:58:12 -0700102 setRotation(originalRotation);
Dianne Hackborn94cb2eb2011-01-13 21:09:44 -0800103
104 if (mSurface != null) {
105 Rect dirty = new Rect(0, 0, mWidth, mHeight);
106 Canvas c = null;
107 try {
108 c = mSurface.lockCanvas(dirty);
109 } catch (IllegalArgumentException e) {
110 Slog.w(TAG, "Unable to lock surface", e);
Dianne Hackborn94cb2eb2011-01-13 21:09:44 -0800111 } catch (Surface.OutOfResourcesException e) {
112 Slog.w(TAG, "Unable to lock surface", e);
Dianne Hackborn94cb2eb2011-01-13 21:09:44 -0800113 }
114 if (c == null) {
Dianne Hackbornde75cb42011-03-02 17:11:21 -0800115 Slog.w(TAG, "Null surface canvas");
116 mSurface.destroy();
117 mSurface = null;
Dianne Hackborn94cb2eb2011-01-13 21:09:44 -0800118 return;
119 }
120
Dianne Hackbornde75cb42011-03-02 17:11:21 -0800121 Paint paint = new Paint(0);
122 paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC));
123 c.drawBitmap(screenshot, 0, 0, paint);
Dianne Hackborn94cb2eb2011-01-13 21:09:44 -0800124
125 mSurface.unlockCanvasAndPost(c);
126 }
127 } finally {
128 if (!inTransaction) {
129 Surface.closeTransaction();
130 if (WindowManagerService.SHOW_TRANSACTIONS) Slog.i(WindowManagerService.TAG,
131 "<<< CLOSE TRANSACTION ScreenRotationAnimation");
Dianne Hackborn352cc982011-01-04 11:34:18 -0800132 }
133
Dianne Hackbornde75cb42011-03-02 17:11:21 -0800134 screenshot.recycle();
Dianne Hackborn0f761d62010-11-30 22:06:10 -0800135 }
Dianne Hackborna1111872010-11-23 20:55:11 -0800136 }
137
Dianne Hackbornde75cb42011-03-02 17:11:21 -0800138 boolean hasScreenshot() {
139 return mSurface != null;
140 }
141
Dianne Hackbornf9d0be92010-11-24 12:35:25 -0800142 static int deltaRotation(int oldRotation, int newRotation) {
143 int delta = newRotation - oldRotation;
Dianne Hackborna1111872010-11-23 20:55:11 -0800144 if (delta < 0) delta += 4;
Dianne Hackbornf9d0be92010-11-24 12:35:25 -0800145 return delta;
146 }
Dianne Hackborna1111872010-11-23 20:55:11 -0800147
Dianne Hackbornf9d0be92010-11-24 12:35:25 -0800148 void setSnapshotTransform(Matrix matrix, float alpha) {
Dianne Hackborn352cc982011-01-04 11:34:18 -0800149 if (mSurface != null) {
150 matrix.getValues(mTmpFloats);
Dianne Hackbornd040edb2011-08-31 12:47:58 -0700151 mSurface.setPosition(mTmpFloats[Matrix.MTRANS_X],
152 mTmpFloats[Matrix.MTRANS_Y]);
Dianne Hackborn352cc982011-01-04 11:34:18 -0800153 mSurface.setMatrix(
154 mTmpFloats[Matrix.MSCALE_X], mTmpFloats[Matrix.MSKEW_Y],
155 mTmpFloats[Matrix.MSKEW_X], mTmpFloats[Matrix.MSCALE_Y]);
156 mSurface.setAlpha(alpha);
157 if (DEBUG) {
158 float[] srcPnts = new float[] { 0, 0, mWidth, mHeight };
159 float[] dstPnts = new float[4];
160 matrix.mapPoints(dstPnts, srcPnts);
161 Slog.i(TAG, "Original : (" + srcPnts[0] + "," + srcPnts[1]
162 + ")-(" + srcPnts[2] + "," + srcPnts[3] + ")");
163 Slog.i(TAG, "Transformed: (" + dstPnts[0] + "," + dstPnts[1]
164 + ")-(" + dstPnts[2] + "," + dstPnts[3] + ")");
165 }
Dianne Hackborna1111872010-11-23 20:55:11 -0800166 }
167 }
168
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800169 public static void createRotationMatrix(int rotation, int width, int height,
170 Matrix outMatrix) {
171 switch (rotation) {
172 case Surface.ROTATION_0:
173 outMatrix.reset();
174 break;
175 case Surface.ROTATION_90:
176 outMatrix.setRotate(90, 0, 0);
177 outMatrix.postTranslate(height, 0);
178 break;
179 case Surface.ROTATION_180:
180 outMatrix.setRotate(180, 0, 0);
181 outMatrix.postTranslate(width, height);
182 break;
183 case Surface.ROTATION_270:
184 outMatrix.setRotate(270, 0, 0);
185 outMatrix.postTranslate(0, width);
186 break;
187 }
188 }
189
Dianne Hackbornf9d0be92010-11-24 12:35:25 -0800190 // Must be called while in a transaction.
191 public void setRotation(int rotation) {
192 mCurRotation = rotation;
193
194 // Compute the transformation matrix that must be applied
195 // to the snapshot to make it stay in the same original position
196 // with the current screen rotation.
197 int delta = deltaRotation(rotation, mSnapshotRotation);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800198 createRotationMatrix(delta, mWidth, mHeight, mSnapshotInitialMatrix);
Dianne Hackbornf9d0be92010-11-24 12:35:25 -0800199
200 if (DEBUG) Slog.v(TAG, "**** ROTATION: " + delta);
201 setSnapshotTransform(mSnapshotInitialMatrix, 1.0f);
202 }
203
204 /**
205 * Returns true if animating.
206 */
Dianne Hackborn50660e22011-02-02 17:12:25 -0800207 public boolean dismiss(SurfaceSession session, long maxAnimationDuration,
Jeff Brownbc68a592011-07-25 12:58:12 -0700208 float animationScale, int finalWidth, int finalHeight) {
Dianne Hackbornde75cb42011-03-02 17:11:21 -0800209 if (mSurface == null) {
210 // Can't do animation.
Dianne Hackbornf9d0be92010-11-24 12:35:25 -0800211 return false;
212 }
213
Dianne Hackbornde75cb42011-03-02 17:11:21 -0800214 // Figure out how the screen has moved from the original rotation.
215 int delta = deltaRotation(mCurRotation, mOriginalRotation);
216
Dianne Hackbornf9d0be92010-11-24 12:35:25 -0800217 switch (delta) {
218 case Surface.ROTATION_0:
219 mExitAnimation = AnimationUtils.loadAnimation(mContext,
220 com.android.internal.R.anim.screen_rotate_0_exit);
221 mEnterAnimation = AnimationUtils.loadAnimation(mContext,
222 com.android.internal.R.anim.screen_rotate_0_enter);
223 break;
224 case Surface.ROTATION_90:
225 mExitAnimation = AnimationUtils.loadAnimation(mContext,
226 com.android.internal.R.anim.screen_rotate_plus_90_exit);
227 mEnterAnimation = AnimationUtils.loadAnimation(mContext,
228 com.android.internal.R.anim.screen_rotate_plus_90_enter);
229 break;
230 case Surface.ROTATION_180:
231 mExitAnimation = AnimationUtils.loadAnimation(mContext,
232 com.android.internal.R.anim.screen_rotate_180_exit);
233 mEnterAnimation = AnimationUtils.loadAnimation(mContext,
234 com.android.internal.R.anim.screen_rotate_180_enter);
235 break;
236 case Surface.ROTATION_270:
237 mExitAnimation = AnimationUtils.loadAnimation(mContext,
238 com.android.internal.R.anim.screen_rotate_minus_90_exit);
239 mEnterAnimation = AnimationUtils.loadAnimation(mContext,
240 com.android.internal.R.anim.screen_rotate_minus_90_enter);
241 break;
242 }
243
Dianne Hackbornf9d0be92010-11-24 12:35:25 -0800244 // Initialize the animations. This is a hack, redefining what "parent"
245 // means to allow supplying the last and next size. In this definition
246 // "%p" is the original (let's call it "previous") size, and "%" is the
247 // screen's current/new size.
Jeff Brownbc68a592011-07-25 12:58:12 -0700248 mEnterAnimation.initialize(finalWidth, finalHeight, mOriginalWidth, mOriginalHeight);
249 mExitAnimation.initialize(finalWidth, finalHeight, mOriginalWidth, mOriginalHeight);
Dianne Hackbornf9d0be92010-11-24 12:35:25 -0800250 mStarted = false;
251
252 mExitAnimation.restrictDuration(maxAnimationDuration);
253 mExitAnimation.scaleCurrentDuration(animationScale);
254 mEnterAnimation.restrictDuration(maxAnimationDuration);
255 mEnterAnimation.scaleCurrentDuration(animationScale);
256
Dianne Hackborn50660e22011-02-02 17:12:25 -0800257 if (WindowManagerService.SHOW_TRANSACTIONS) Slog.i(WindowManagerService.TAG,
258 ">>> OPEN TRANSACTION ScreenRotationAnimation.dismiss");
259 Surface.openTransaction();
260
Dianne Hackborn50660e22011-02-02 17:12:25 -0800261 try {
Jeff Brownbc68a592011-07-25 12:58:12 -0700262 Rect outer = new Rect(-finalWidth, -finalHeight, finalWidth * 2, finalHeight * 2);
263 Rect inner = new Rect(0, 0, finalWidth, finalHeight);
Dianne Hackborn7916ac62011-05-16 20:45:48 -0700264 mBlackFrame = new BlackFrame(session, outer, inner, FREEZE_LAYER);
Dianne Hackborn50660e22011-02-02 17:12:25 -0800265 } catch (Surface.OutOfResourcesException e) {
266 Slog.w(TAG, "Unable to allocate black surface", e);
267 } finally {
268 Surface.closeTransaction();
269 if (WindowManagerService.SHOW_TRANSACTIONS) Slog.i(WindowManagerService.TAG,
270 "<<< CLOSE TRANSACTION ScreenRotationAnimation.dismiss");
271 }
272
Dianne Hackbornf9d0be92010-11-24 12:35:25 -0800273 return true;
274 }
275
276 public void kill() {
277 if (mSurface != null) {
Dianne Hackborn5fd21692011-06-07 14:09:47 -0700278 if (WindowManagerService.SHOW_TRANSACTIONS ||
279 WindowManagerService.SHOW_SURFACE_ALLOC) Slog.i(WindowManagerService.TAG,
280 " FREEZE " + mSurface + ": DESTROY");
Dianne Hackbornf9d0be92010-11-24 12:35:25 -0800281 mSurface.destroy();
282 mSurface = null;
283 }
Dianne Hackborn7916ac62011-05-16 20:45:48 -0700284 if (mBlackFrame != null) {
285 mBlackFrame.kill();
Dianne Hackborn352cc982011-01-04 11:34:18 -0800286 }
Dianne Hackbornf9d0be92010-11-24 12:35:25 -0800287 if (mExitAnimation != null) {
288 mExitAnimation.cancel();
289 mExitAnimation = null;
290 }
291 if (mEnterAnimation != null) {
292 mEnterAnimation.cancel();
293 mEnterAnimation = null;
294 }
295 }
296
297 public boolean isAnimating() {
298 return mEnterAnimation != null || mExitAnimation != null;
299 }
300
301 public boolean stepAnimation(long now) {
302 if (mEnterAnimation == null && mExitAnimation == null) {
303 return false;
304 }
305
306 if (!mStarted) {
Dianne Hackborn89620282011-09-11 12:47:45 -0700307 if (mEnterAnimation != null) {
308 mEnterAnimation.setStartTime(now);
309 }
310 if (mExitAnimation != null) {
311 mExitAnimation.setStartTime(now);
312 }
Dianne Hackbornf9d0be92010-11-24 12:35:25 -0800313 mStarted = true;
314 }
315
316 mExitTransformation.clear();
317 boolean moreExit = false;
318 if (mExitAnimation != null) {
319 moreExit = mExitAnimation.getTransformation(now, mExitTransformation);
320 if (DEBUG) Slog.v(TAG, "Stepped exit: " + mExitTransformation);
321 if (!moreExit) {
322 if (DEBUG) Slog.v(TAG, "Exit animation done!");
323 mExitAnimation.cancel();
324 mExitAnimation = null;
325 mExitTransformation.clear();
326 if (mSurface != null) {
Dianne Hackborn50660e22011-02-02 17:12:25 -0800327 mSurface.hide();
Dianne Hackborn352cc982011-01-04 11:34:18 -0800328 }
Dianne Hackbornf9d0be92010-11-24 12:35:25 -0800329 }
330 }
331
332 mEnterTransformation.clear();
333 boolean moreEnter = false;
334 if (mEnterAnimation != null) {
335 moreEnter = mEnterAnimation.getTransformation(now, mEnterTransformation);
336 if (!moreEnter) {
337 mEnterAnimation.cancel();
338 mEnterAnimation = null;
339 mEnterTransformation.clear();
Dianne Hackborn7916ac62011-05-16 20:45:48 -0700340 if (mBlackFrame != null) {
341 mBlackFrame.hide();
Dianne Hackborn50660e22011-02-02 17:12:25 -0800342 }
343 } else {
Dianne Hackborn7916ac62011-05-16 20:45:48 -0700344 if (mBlackFrame != null) {
345 mBlackFrame.setMatrix(mEnterTransformation.getMatrix());
Dianne Hackborn50660e22011-02-02 17:12:25 -0800346 }
Dianne Hackbornf9d0be92010-11-24 12:35:25 -0800347 }
348 }
349
Dianne Hackborn352cc982011-01-04 11:34:18 -0800350 mSnapshotFinalMatrix.setConcat(mExitTransformation.getMatrix(), mSnapshotInitialMatrix);
351 setSnapshotTransform(mSnapshotFinalMatrix, mExitTransformation.getAlpha());
Dianne Hackbornf9d0be92010-11-24 12:35:25 -0800352
353 return moreEnter || moreExit;
354 }
355
356 public Transformation getEnterTransformation() {
357 return mEnterTransformation;
Dianne Hackborna1111872010-11-23 20:55:11 -0800358 }
359}