blob: 131f11c19fbdbfcc4ddf0f21fdab8d5b89c245cb [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
Dianne Hackbornde75cb42011-03-02 17:11:21 -080067 // Screenshot does NOT include rotation!
68 mSnapshotRotation = 0;
Mathias Agopian0ab84ef2011-10-13 16:02:48 -070069 if (originalRotation == Surface.ROTATION_90
70 || originalRotation == Surface.ROTATION_270) {
71 mWidth = originalHeight;
72 mHeight = originalWidth;
73 } else {
74 mWidth = originalWidth;
75 mHeight = originalHeight;
76 }
Dianne Hackbornde75cb42011-03-02 17:11:21 -080077
Jeff Brownbc68a592011-07-25 12:58:12 -070078 mOriginalRotation = originalRotation;
79 mOriginalWidth = originalWidth;
80 mOriginalHeight = originalHeight;
Dianne Hackbornf9d0be92010-11-24 12:35:25 -080081
Dianne Hackborn94cb2eb2011-01-13 21:09:44 -080082 if (!inTransaction) {
Dianne Hackborn36991742011-10-11 21:35:26 -070083 if (WindowManagerService.SHOW_LIGHT_TRANSACTIONS) Slog.i(WindowManagerService.TAG,
Dianne Hackborn94cb2eb2011-01-13 21:09:44 -080084 ">>> OPEN TRANSACTION ScreenRotationAnimation");
85 Surface.openTransaction();
86 }
Dianne Hackborn352cc982011-01-04 11:34:18 -080087
Dianne Hackborna1111872010-11-23 20:55:11 -080088 try {
Dianne Hackborn352cc982011-01-04 11:34:18 -080089 try {
Dianne Hackborn94cb2eb2011-01-13 21:09:44 -080090 mSurface = new Surface(session, 0, "FreezeSurface",
Mathias Agopian0ab84ef2011-10-13 16:02:48 -070091 -1, mWidth, mHeight, PixelFormat.OPAQUE, Surface.FX_SURFACE_SCREENSHOT);
92 if (mSurface == null || !mSurface.isValid()) {
93 // Screenshot failed, punt.
94 mSurface = null;
95 return;
96 }
Dianne Hackborn50660e22011-02-02 17:12:25 -080097 mSurface.setLayer(FREEZE_LAYER + 1);
Dianne Hackborn352cc982011-01-04 11:34:18 -080098 } catch (Surface.OutOfResourcesException e) {
Dianne Hackborn94cb2eb2011-01-13 21:09:44 -080099 Slog.w(TAG, "Unable to allocate freeze surface", e);
Dianne Hackborn352cc982011-01-04 11:34:18 -0800100 }
Dianne Hackborna1111872010-11-23 20:55:11 -0800101
Dianne Hackborn5fd21692011-06-07 14:09:47 -0700102 if (WindowManagerService.SHOW_TRANSACTIONS ||
103 WindowManagerService.SHOW_SURFACE_ALLOC) Slog.i(WindowManagerService.TAG,
104 " FREEZE " + mSurface + ": CREATE");
105
Jeff Brownbc68a592011-07-25 12:58:12 -0700106 setRotation(originalRotation);
Dianne Hackborn94cb2eb2011-01-13 21:09:44 -0800107 } finally {
108 if (!inTransaction) {
109 Surface.closeTransaction();
Dianne Hackborn36991742011-10-11 21:35:26 -0700110 if (WindowManagerService.SHOW_LIGHT_TRANSACTIONS) Slog.i(WindowManagerService.TAG,
Dianne Hackborn94cb2eb2011-01-13 21:09:44 -0800111 "<<< CLOSE TRANSACTION ScreenRotationAnimation");
Dianne Hackborn352cc982011-01-04 11:34:18 -0800112 }
Dianne Hackborn0f761d62010-11-30 22:06:10 -0800113 }
Dianne Hackborna1111872010-11-23 20:55:11 -0800114 }
115
Dianne Hackbornde75cb42011-03-02 17:11:21 -0800116 boolean hasScreenshot() {
117 return mSurface != null;
118 }
119
Dianne Hackbornf9d0be92010-11-24 12:35:25 -0800120 static int deltaRotation(int oldRotation, int newRotation) {
121 int delta = newRotation - oldRotation;
Dianne Hackborna1111872010-11-23 20:55:11 -0800122 if (delta < 0) delta += 4;
Dianne Hackbornf9d0be92010-11-24 12:35:25 -0800123 return delta;
124 }
Dianne Hackborna1111872010-11-23 20:55:11 -0800125
Dianne Hackbornf9d0be92010-11-24 12:35:25 -0800126 void setSnapshotTransform(Matrix matrix, float alpha) {
Dianne Hackborn352cc982011-01-04 11:34:18 -0800127 if (mSurface != null) {
128 matrix.getValues(mTmpFloats);
Dianne Hackbornd040edb2011-08-31 12:47:58 -0700129 mSurface.setPosition(mTmpFloats[Matrix.MTRANS_X],
130 mTmpFloats[Matrix.MTRANS_Y]);
Dianne Hackborn352cc982011-01-04 11:34:18 -0800131 mSurface.setMatrix(
132 mTmpFloats[Matrix.MSCALE_X], mTmpFloats[Matrix.MSKEW_Y],
133 mTmpFloats[Matrix.MSKEW_X], mTmpFloats[Matrix.MSCALE_Y]);
134 mSurface.setAlpha(alpha);
135 if (DEBUG) {
136 float[] srcPnts = new float[] { 0, 0, mWidth, mHeight };
137 float[] dstPnts = new float[4];
138 matrix.mapPoints(dstPnts, srcPnts);
139 Slog.i(TAG, "Original : (" + srcPnts[0] + "," + srcPnts[1]
140 + ")-(" + srcPnts[2] + "," + srcPnts[3] + ")");
141 Slog.i(TAG, "Transformed: (" + dstPnts[0] + "," + dstPnts[1]
142 + ")-(" + dstPnts[2] + "," + dstPnts[3] + ")");
143 }
Dianne Hackborna1111872010-11-23 20:55:11 -0800144 }
145 }
146
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800147 public static void createRotationMatrix(int rotation, int width, int height,
148 Matrix outMatrix) {
149 switch (rotation) {
150 case Surface.ROTATION_0:
151 outMatrix.reset();
152 break;
153 case Surface.ROTATION_90:
154 outMatrix.setRotate(90, 0, 0);
155 outMatrix.postTranslate(height, 0);
156 break;
157 case Surface.ROTATION_180:
158 outMatrix.setRotate(180, 0, 0);
159 outMatrix.postTranslate(width, height);
160 break;
161 case Surface.ROTATION_270:
162 outMatrix.setRotate(270, 0, 0);
163 outMatrix.postTranslate(0, width);
164 break;
165 }
166 }
167
Dianne Hackbornf9d0be92010-11-24 12:35:25 -0800168 // Must be called while in a transaction.
169 public void setRotation(int rotation) {
170 mCurRotation = rotation;
171
172 // Compute the transformation matrix that must be applied
173 // to the snapshot to make it stay in the same original position
174 // with the current screen rotation.
175 int delta = deltaRotation(rotation, mSnapshotRotation);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800176 createRotationMatrix(delta, mWidth, mHeight, mSnapshotInitialMatrix);
Dianne Hackbornf9d0be92010-11-24 12:35:25 -0800177
178 if (DEBUG) Slog.v(TAG, "**** ROTATION: " + delta);
179 setSnapshotTransform(mSnapshotInitialMatrix, 1.0f);
180 }
181
182 /**
183 * Returns true if animating.
184 */
Dianne Hackborn50660e22011-02-02 17:12:25 -0800185 public boolean dismiss(SurfaceSession session, long maxAnimationDuration,
Jeff Brownbc68a592011-07-25 12:58:12 -0700186 float animationScale, int finalWidth, int finalHeight) {
Dianne Hackbornde75cb42011-03-02 17:11:21 -0800187 if (mSurface == null) {
188 // Can't do animation.
Dianne Hackbornf9d0be92010-11-24 12:35:25 -0800189 return false;
190 }
191
Dianne Hackbornde75cb42011-03-02 17:11:21 -0800192 // Figure out how the screen has moved from the original rotation.
193 int delta = deltaRotation(mCurRotation, mOriginalRotation);
194
Dianne Hackbornf9d0be92010-11-24 12:35:25 -0800195 switch (delta) {
196 case Surface.ROTATION_0:
197 mExitAnimation = AnimationUtils.loadAnimation(mContext,
198 com.android.internal.R.anim.screen_rotate_0_exit);
199 mEnterAnimation = AnimationUtils.loadAnimation(mContext,
200 com.android.internal.R.anim.screen_rotate_0_enter);
201 break;
202 case Surface.ROTATION_90:
203 mExitAnimation = AnimationUtils.loadAnimation(mContext,
204 com.android.internal.R.anim.screen_rotate_plus_90_exit);
205 mEnterAnimation = AnimationUtils.loadAnimation(mContext,
206 com.android.internal.R.anim.screen_rotate_plus_90_enter);
207 break;
208 case Surface.ROTATION_180:
209 mExitAnimation = AnimationUtils.loadAnimation(mContext,
210 com.android.internal.R.anim.screen_rotate_180_exit);
211 mEnterAnimation = AnimationUtils.loadAnimation(mContext,
212 com.android.internal.R.anim.screen_rotate_180_enter);
213 break;
214 case Surface.ROTATION_270:
215 mExitAnimation = AnimationUtils.loadAnimation(mContext,
216 com.android.internal.R.anim.screen_rotate_minus_90_exit);
217 mEnterAnimation = AnimationUtils.loadAnimation(mContext,
218 com.android.internal.R.anim.screen_rotate_minus_90_enter);
219 break;
220 }
221
Dianne Hackbornf9d0be92010-11-24 12:35:25 -0800222 // Initialize the animations. This is a hack, redefining what "parent"
223 // means to allow supplying the last and next size. In this definition
224 // "%p" is the original (let's call it "previous") size, and "%" is the
225 // screen's current/new size.
Jeff Brownbc68a592011-07-25 12:58:12 -0700226 mEnterAnimation.initialize(finalWidth, finalHeight, mOriginalWidth, mOriginalHeight);
227 mExitAnimation.initialize(finalWidth, finalHeight, mOriginalWidth, mOriginalHeight);
Dianne Hackbornf9d0be92010-11-24 12:35:25 -0800228 mStarted = false;
229
230 mExitAnimation.restrictDuration(maxAnimationDuration);
231 mExitAnimation.scaleCurrentDuration(animationScale);
232 mEnterAnimation.restrictDuration(maxAnimationDuration);
233 mEnterAnimation.scaleCurrentDuration(animationScale);
234
Dianne Hackborn36991742011-10-11 21:35:26 -0700235 if (WindowManagerService.SHOW_LIGHT_TRANSACTIONS) Slog.i(WindowManagerService.TAG,
Dianne Hackborn50660e22011-02-02 17:12:25 -0800236 ">>> OPEN TRANSACTION ScreenRotationAnimation.dismiss");
237 Surface.openTransaction();
238
Dianne Hackborn50660e22011-02-02 17:12:25 -0800239 try {
Jeff Brownbc68a592011-07-25 12:58:12 -0700240 Rect outer = new Rect(-finalWidth, -finalHeight, finalWidth * 2, finalHeight * 2);
241 Rect inner = new Rect(0, 0, finalWidth, finalHeight);
Dianne Hackborn7916ac62011-05-16 20:45:48 -0700242 mBlackFrame = new BlackFrame(session, outer, inner, FREEZE_LAYER);
Dianne Hackborn50660e22011-02-02 17:12:25 -0800243 } catch (Surface.OutOfResourcesException e) {
244 Slog.w(TAG, "Unable to allocate black surface", e);
245 } finally {
246 Surface.closeTransaction();
Dianne Hackborn36991742011-10-11 21:35:26 -0700247 if (WindowManagerService.SHOW_LIGHT_TRANSACTIONS) Slog.i(WindowManagerService.TAG,
Dianne Hackborn50660e22011-02-02 17:12:25 -0800248 "<<< CLOSE TRANSACTION ScreenRotationAnimation.dismiss");
249 }
250
Dianne Hackbornf9d0be92010-11-24 12:35:25 -0800251 return true;
252 }
253
254 public void kill() {
255 if (mSurface != null) {
Dianne Hackborn5fd21692011-06-07 14:09:47 -0700256 if (WindowManagerService.SHOW_TRANSACTIONS ||
257 WindowManagerService.SHOW_SURFACE_ALLOC) Slog.i(WindowManagerService.TAG,
258 " FREEZE " + mSurface + ": DESTROY");
Dianne Hackbornf9d0be92010-11-24 12:35:25 -0800259 mSurface.destroy();
260 mSurface = null;
261 }
Dianne Hackborn7916ac62011-05-16 20:45:48 -0700262 if (mBlackFrame != null) {
263 mBlackFrame.kill();
Dianne Hackborn352cc982011-01-04 11:34:18 -0800264 }
Dianne Hackbornf9d0be92010-11-24 12:35:25 -0800265 if (mExitAnimation != null) {
266 mExitAnimation.cancel();
267 mExitAnimation = null;
268 }
269 if (mEnterAnimation != null) {
270 mEnterAnimation.cancel();
271 mEnterAnimation = null;
272 }
273 }
274
275 public boolean isAnimating() {
276 return mEnterAnimation != null || mExitAnimation != null;
277 }
278
279 public boolean stepAnimation(long now) {
280 if (mEnterAnimation == null && mExitAnimation == null) {
281 return false;
282 }
283
284 if (!mStarted) {
Dianne Hackborn89620282011-09-11 12:47:45 -0700285 if (mEnterAnimation != null) {
286 mEnterAnimation.setStartTime(now);
287 }
288 if (mExitAnimation != null) {
289 mExitAnimation.setStartTime(now);
290 }
Dianne Hackbornf9d0be92010-11-24 12:35:25 -0800291 mStarted = true;
292 }
293
294 mExitTransformation.clear();
295 boolean moreExit = false;
296 if (mExitAnimation != null) {
297 moreExit = mExitAnimation.getTransformation(now, mExitTransformation);
298 if (DEBUG) Slog.v(TAG, "Stepped exit: " + mExitTransformation);
299 if (!moreExit) {
300 if (DEBUG) Slog.v(TAG, "Exit animation done!");
301 mExitAnimation.cancel();
302 mExitAnimation = null;
303 mExitTransformation.clear();
304 if (mSurface != null) {
Dianne Hackborn50660e22011-02-02 17:12:25 -0800305 mSurface.hide();
Dianne Hackborn352cc982011-01-04 11:34:18 -0800306 }
Dianne Hackbornf9d0be92010-11-24 12:35:25 -0800307 }
308 }
309
310 mEnterTransformation.clear();
311 boolean moreEnter = false;
312 if (mEnterAnimation != null) {
313 moreEnter = mEnterAnimation.getTransformation(now, mEnterTransformation);
314 if (!moreEnter) {
315 mEnterAnimation.cancel();
316 mEnterAnimation = null;
317 mEnterTransformation.clear();
Dianne Hackborn7916ac62011-05-16 20:45:48 -0700318 if (mBlackFrame != null) {
319 mBlackFrame.hide();
Dianne Hackborn50660e22011-02-02 17:12:25 -0800320 }
321 } else {
Dianne Hackborn7916ac62011-05-16 20:45:48 -0700322 if (mBlackFrame != null) {
323 mBlackFrame.setMatrix(mEnterTransformation.getMatrix());
Dianne Hackborn50660e22011-02-02 17:12:25 -0800324 }
Dianne Hackbornf9d0be92010-11-24 12:35:25 -0800325 }
326 }
327
Dianne Hackborn352cc982011-01-04 11:34:18 -0800328 mSnapshotFinalMatrix.setConcat(mExitTransformation.getMatrix(), mSnapshotInitialMatrix);
329 setSnapshotTransform(mSnapshotFinalMatrix, mExitTransformation.getAlpha());
Dianne Hackbornf9d0be92010-11-24 12:35:25 -0800330
331 return moreEnter || moreExit;
332 }
333
334 public Transformation getEnterTransformation() {
335 return mEnterTransformation;
Dianne Hackborna1111872010-11-23 20:55:11 -0800336 }
337}