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