blob: ced7c7b3d46026338548dbb37cc864648d818b3a [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
17package com.android.server; // TODO: use com.android.server.wm, once things move there
18
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;
22import android.graphics.Color;
23import android.graphics.Matrix;
24import android.graphics.Paint;
25import android.graphics.PixelFormat;
26import android.graphics.Rect;
27import android.util.DisplayMetrics;
28import android.util.Slog;
29import android.view.Display;
30import android.view.Surface;
31import android.view.SurfaceSession;
Dianne Hackbornf9d0be92010-11-24 12:35:25 -080032import android.view.animation.Animation;
33import android.view.animation.AnimationUtils;
34import android.view.animation.Transformation;
Dianne Hackborna1111872010-11-23 20:55:11 -080035
36class ScreenRotationAnimation {
Dianne Hackbornf9d0be92010-11-24 12:35:25 -080037 static final String TAG = "ScreenRotationAnimation";
38 static final boolean DEBUG = false;
Dianne Hackborna1111872010-11-23 20:55:11 -080039
Dianne Hackbornf9d0be92010-11-24 12:35:25 -080040 final Context mContext;
41 final Display mDisplay;
Dianne Hackborna1111872010-11-23 20:55:11 -080042 Surface mSurface;
43 int mWidth, mHeight;
44
Dianne Hackbornf9d0be92010-11-24 12:35:25 -080045 int mSnapshotRotation;
46 int mSnapshotDeltaRotation;
47 int mOriginalRotation;
48 int mOriginalWidth, mOriginalHeight;
Dianne Hackborna1111872010-11-23 20:55:11 -080049 int mCurRotation;
Dianne Hackborna1111872010-11-23 20:55:11 -080050
Dianne Hackbornf9d0be92010-11-24 12:35:25 -080051 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 Hackborna1111872010-11-23 20:55:11 -080060 final float[] mTmpFloats = new float[9];
61
Dianne Hackbornf9d0be92010-11-24 12:35:25 -080062 public ScreenRotationAnimation(Context context, Display display, SurfaceSession session) {
63 mContext = context;
64 mDisplay = display;
65
66 display.getMetrics(mDisplayMetrics);
Dianne Hackborna1111872010-11-23 20:55:11 -080067
68 Bitmap screenshot = Surface.screenshot(0, 0);
69
70 if (screenshot != null) {
71 // Screenshot does NOT include rotation!
Dianne Hackbornf9d0be92010-11-24 12:35:25 -080072 mSnapshotRotation = 0;
Dianne Hackborna1111872010-11-23 20:55:11 -080073 mWidth = screenshot.getWidth();
74 mHeight = screenshot.getHeight();
75 } else {
76 // Just in case.
Dianne Hackbornf9d0be92010-11-24 12:35:25 -080077 mSnapshotRotation = display.getRotation();
78 mWidth = mDisplayMetrics.widthPixels;
79 mHeight = mDisplayMetrics.heightPixels;
Dianne Hackborna1111872010-11-23 20:55:11 -080080 }
81
Dianne Hackbornf9d0be92010-11-24 12:35:25 -080082 mOriginalRotation = display.getRotation();
83 mOriginalWidth = mDisplayMetrics.widthPixels;
84 mOriginalHeight = mDisplayMetrics.heightPixels;
85
Dianne Hackborna1111872010-11-23 20:55:11 -080086 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 Hackborn0f761d62010-11-30 22:06:10 -0800125 if (screenshot != null) {
126 screenshot.recycle();
127 }
Dianne Hackborna1111872010-11-23 20:55:11 -0800128 }
129
Dianne Hackbornf9d0be92010-11-24 12:35:25 -0800130 static int deltaRotation(int oldRotation, int newRotation) {
131 int delta = newRotation - oldRotation;
Dianne Hackborna1111872010-11-23 20:55:11 -0800132 if (delta < 0) delta += 4;
Dianne Hackbornf9d0be92010-11-24 12:35:25 -0800133 return delta;
134 }
Dianne Hackborna1111872010-11-23 20:55:11 -0800135
Dianne Hackbornf9d0be92010-11-24 12:35:25 -0800136 void setSnapshotTransform(Matrix matrix, float alpha) {
137 matrix.getValues(mTmpFloats);
Dianne Hackborna1111872010-11-23 20:55:11 -0800138 mSurface.setPosition((int)mTmpFloats[Matrix.MTRANS_X],
139 (int)mTmpFloats[Matrix.MTRANS_Y]);
140 mSurface.setMatrix(
Dianne Hackbornf9d0be92010-11-24 12:35:25 -0800141 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 Hackborna1111872010-11-23 20:55:11 -0800145 float[] srcPnts = new float[] { 0, 0, mWidth, mHeight };
Dianne Hackbornf9d0be92010-11-24 12:35:25 -0800146 float[] dstPnts = new float[4];
147 matrix.mapPoints(dstPnts, srcPnts);
Dianne Hackborna1111872010-11-23 20:55:11 -0800148 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 Hackborn0aae2d42010-12-07 23:51:29 -0800155 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 Hackbornf9d0be92010-11-24 12:35:25 -0800176 // 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 Hackborn0aae2d42010-12-07 23:51:29 -0800184 createRotationMatrix(delta, mWidth, mHeight, mSnapshotInitialMatrix);
Dianne Hackbornf9d0be92010-11-24 12:35:25 -0800185
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 Hackborna1111872010-11-23 20:55:11 -0800320 }
321}