blob: a95a6c715774f1b26bcd2fe2cd2c6340904c6e35 [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 Hackbornf9d0be92010-11-24 12:35:25 -0800155 // Must be called while in a transaction.
156 public void setRotation(int rotation) {
157 mCurRotation = rotation;
158
159 // Compute the transformation matrix that must be applied
160 // to the snapshot to make it stay in the same original position
161 // with the current screen rotation.
162 int delta = deltaRotation(rotation, mSnapshotRotation);
163 switch (delta) {
164 case Surface.ROTATION_0:
165 mSnapshotInitialMatrix.reset();
166 break;
167 case Surface.ROTATION_90:
168 mSnapshotInitialMatrix.setRotate(90, 0, 0);
169 mSnapshotInitialMatrix.postTranslate(mHeight, 0);
170 break;
171 case Surface.ROTATION_180:
172 mSnapshotInitialMatrix.setRotate(180, 0, 0);
173 mSnapshotInitialMatrix.postTranslate(mWidth, mHeight);
174 break;
175 case Surface.ROTATION_270:
176 mSnapshotInitialMatrix.setRotate(270, 0, 0);
177 mSnapshotInitialMatrix.postTranslate(0, mWidth);
178 break;
179 }
180
181 if (DEBUG) Slog.v(TAG, "**** ROTATION: " + delta);
182 setSnapshotTransform(mSnapshotInitialMatrix, 1.0f);
183 }
184
185 /**
186 * Returns true if animating.
187 */
188 public boolean dismiss(long maxAnimationDuration, float animationScale) {
189 // Figure out how the screen has moved from the original rotation.
190 int delta = deltaRotation(mCurRotation, mOriginalRotation);
191 if (false && delta == 0) {
192 // Nothing changed, just remove the snapshot.
193 if (mSurface != null) {
194 mSurface.destroy();
195 mSurface = null;
196 }
197 return false;
198 }
199
200 switch (delta) {
201 case Surface.ROTATION_0:
202 mExitAnimation = AnimationUtils.loadAnimation(mContext,
203 com.android.internal.R.anim.screen_rotate_0_exit);
204 mEnterAnimation = AnimationUtils.loadAnimation(mContext,
205 com.android.internal.R.anim.screen_rotate_0_enter);
206 break;
207 case Surface.ROTATION_90:
208 mExitAnimation = AnimationUtils.loadAnimation(mContext,
209 com.android.internal.R.anim.screen_rotate_plus_90_exit);
210 mEnterAnimation = AnimationUtils.loadAnimation(mContext,
211 com.android.internal.R.anim.screen_rotate_plus_90_enter);
212 break;
213 case Surface.ROTATION_180:
214 mExitAnimation = AnimationUtils.loadAnimation(mContext,
215 com.android.internal.R.anim.screen_rotate_180_exit);
216 mEnterAnimation = AnimationUtils.loadAnimation(mContext,
217 com.android.internal.R.anim.screen_rotate_180_enter);
218 break;
219 case Surface.ROTATION_270:
220 mExitAnimation = AnimationUtils.loadAnimation(mContext,
221 com.android.internal.R.anim.screen_rotate_minus_90_exit);
222 mEnterAnimation = AnimationUtils.loadAnimation(mContext,
223 com.android.internal.R.anim.screen_rotate_minus_90_enter);
224 break;
225 }
226
227 mDisplay.getMetrics(mDisplayMetrics);
228
229 // Initialize the animations. This is a hack, redefining what "parent"
230 // means to allow supplying the last and next size. In this definition
231 // "%p" is the original (let's call it "previous") size, and "%" is the
232 // screen's current/new size.
233 mEnterAnimation.initialize(mDisplayMetrics.widthPixels, mDisplayMetrics.heightPixels,
234 mOriginalWidth, mOriginalHeight);
235 mExitAnimation.initialize(mDisplayMetrics.widthPixels, mDisplayMetrics.heightPixels,
236 mOriginalWidth, mOriginalHeight);
237 mStarted = false;
238
239 mExitAnimation.restrictDuration(maxAnimationDuration);
240 mExitAnimation.scaleCurrentDuration(animationScale);
241 mEnterAnimation.restrictDuration(maxAnimationDuration);
242 mEnterAnimation.scaleCurrentDuration(animationScale);
243
244 return true;
245 }
246
247 public void kill() {
248 if (mSurface != null) {
249 mSurface.destroy();
250 mSurface = null;
251 }
252 if (mExitAnimation != null) {
253 mExitAnimation.cancel();
254 mExitAnimation = null;
255 }
256 if (mEnterAnimation != null) {
257 mEnterAnimation.cancel();
258 mEnterAnimation = null;
259 }
260 }
261
262 public boolean isAnimating() {
263 return mEnterAnimation != null || mExitAnimation != null;
264 }
265
266 public boolean stepAnimation(long now) {
267 if (mEnterAnimation == null && mExitAnimation == null) {
268 return false;
269 }
270
271 if (!mStarted) {
272 mEnterAnimation.setStartTime(now);
273 mExitAnimation.setStartTime(now);
274 mStarted = true;
275 }
276
277 mExitTransformation.clear();
278 boolean moreExit = false;
279 if (mExitAnimation != null) {
280 moreExit = mExitAnimation.getTransformation(now, mExitTransformation);
281 if (DEBUG) Slog.v(TAG, "Stepped exit: " + mExitTransformation);
282 if (!moreExit) {
283 if (DEBUG) Slog.v(TAG, "Exit animation done!");
284 mExitAnimation.cancel();
285 mExitAnimation = null;
286 mExitTransformation.clear();
287 if (mSurface != null) {
288 mSurface.destroy();
289 mSurface = null;
290 }
291 }
292 }
293
294 mEnterTransformation.clear();
295 boolean moreEnter = false;
296 if (mEnterAnimation != null) {
297 moreEnter = mEnterAnimation.getTransformation(now, mEnterTransformation);
298 if (!moreEnter) {
299 mEnterAnimation.cancel();
300 mEnterAnimation = null;
301 mEnterTransformation.clear();
302 }
303 }
304
305 if (mSurface != null) {
306 mSnapshotFinalMatrix.setConcat(mExitTransformation.getMatrix(), mSnapshotInitialMatrix);
307 setSnapshotTransform(mSnapshotFinalMatrix, mExitTransformation.getAlpha());
308 }
309
310 return moreEnter || moreExit;
311 }
312
313 public Transformation getEnterTransformation() {
314 return mEnterTransformation;
Dianne Hackborna1111872010-11-23 20:55:11 -0800315 }
316}