blob: 19cc203d0eeb238d3340e01f2f142e599894f0ac [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;
Dianne Hackborn352cc982011-01-04 11:34:18 -080043 Surface mBlackSurface;
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
58 final DisplayMetrics mDisplayMetrics = new DisplayMetrics();
59 final Matrix mSnapshotInitialMatrix = new Matrix();
60 final Matrix mSnapshotFinalMatrix = new Matrix();
Dianne Hackborna1111872010-11-23 20:55:11 -080061 final float[] mTmpFloats = new float[9];
62
Dianne Hackbornf9d0be92010-11-24 12:35:25 -080063 public ScreenRotationAnimation(Context context, Display display, SurfaceSession session) {
64 mContext = context;
65 mDisplay = display;
66
67 display.getMetrics(mDisplayMetrics);
Dianne Hackborna1111872010-11-23 20:55:11 -080068
69 Bitmap screenshot = Surface.screenshot(0, 0);
70
71 if (screenshot != null) {
72 // Screenshot does NOT include rotation!
Dianne Hackbornf9d0be92010-11-24 12:35:25 -080073 mSnapshotRotation = 0;
Dianne Hackborna1111872010-11-23 20:55:11 -080074 mWidth = screenshot.getWidth();
75 mHeight = screenshot.getHeight();
76 } else {
77 // Just in case.
Dianne Hackbornf9d0be92010-11-24 12:35:25 -080078 mSnapshotRotation = display.getRotation();
79 mWidth = mDisplayMetrics.widthPixels;
80 mHeight = mDisplayMetrics.heightPixels;
Dianne Hackborna1111872010-11-23 20:55:11 -080081 }
82
Dianne Hackbornf9d0be92010-11-24 12:35:25 -080083 mOriginalRotation = display.getRotation();
84 mOriginalWidth = mDisplayMetrics.widthPixels;
85 mOriginalHeight = mDisplayMetrics.heightPixels;
86
Dianne Hackborna1111872010-11-23 20:55:11 -080087 Surface.openTransaction();
Dianne Hackborn352cc982011-01-04 11:34:18 -080088
Dianne Hackborna1111872010-11-23 20:55:11 -080089 try {
90 mSurface = new Surface(session, 0, "FreezeSurface",
91 -1, mWidth, mHeight, PixelFormat.OPAQUE, 0);
Dianne Hackborn352cc982011-01-04 11:34:18 -080092 mSurface.setLayer(WindowManagerService.TYPE_LAYER_MULTIPLIER * 200);
Dianne Hackborna1111872010-11-23 20:55:11 -080093 } catch (Surface.OutOfResourcesException e) {
94 Slog.w(TAG, "Unable to allocate freeze surface", e);
95 }
Dianne Hackborn352cc982011-01-04 11:34:18 -080096
97 if (false) {
98 try {
99 int size = mOriginalWidth > mOriginalHeight ? mOriginalWidth : mOriginalHeight;
100 mBlackSurface = new Surface(session, 0, "BlackSurface",
101 -1, size, size, PixelFormat.OPAQUE, Surface.FX_SURFACE_DIM);
102 mBlackSurface.setAlpha(1.0f);
103 mBlackSurface.setLayer(0);
104 } catch (Surface.OutOfResourcesException e) {
105 Slog.w(TAG, "Unable to allocate black surface", e);
106 }
107 }
108
Dianne Hackborna1111872010-11-23 20:55:11 -0800109 setRotation(display.getRotation());
110
Dianne Hackborn352cc982011-01-04 11:34:18 -0800111 if (mSurface != null) {
112 Rect dirty = new Rect(0, 0, mWidth, mHeight);
113 Canvas c = null;
114 try {
115 c = mSurface.lockCanvas(dirty);
116 } catch (IllegalArgumentException e) {
117 Slog.w(TAG, "Unable to lock surface", e);
118 return;
119 } catch (Surface.OutOfResourcesException e) {
120 Slog.w(TAG, "Unable to lock surface", e);
121 return;
122 }
123 if (c == null) {
124 Slog.w(TAG, "Null surface");
125 return;
126 }
127
128 if (screenshot != null) {
129 c.drawBitmap(screenshot, 0, 0, new Paint(0));
130 } else {
131 c.drawColor(Color.GREEN);
132 }
133
134 mSurface.unlockCanvasAndPost(c);
Dianne Hackborna1111872010-11-23 20:55:11 -0800135 }
Dianne Hackborn352cc982011-01-04 11:34:18 -0800136
Dianne Hackborna1111872010-11-23 20:55:11 -0800137 Surface.closeTransaction();
138
Dianne Hackborn0f761d62010-11-30 22:06:10 -0800139 if (screenshot != null) {
140 screenshot.recycle();
141 }
Dianne Hackborna1111872010-11-23 20:55:11 -0800142 }
143
Dianne Hackbornf9d0be92010-11-24 12:35:25 -0800144 static int deltaRotation(int oldRotation, int newRotation) {
145 int delta = newRotation - oldRotation;
Dianne Hackborna1111872010-11-23 20:55:11 -0800146 if (delta < 0) delta += 4;
Dianne Hackbornf9d0be92010-11-24 12:35:25 -0800147 return delta;
148 }
Dianne Hackborna1111872010-11-23 20:55:11 -0800149
Dianne Hackbornf9d0be92010-11-24 12:35:25 -0800150 void setSnapshotTransform(Matrix matrix, float alpha) {
Dianne Hackborn352cc982011-01-04 11:34:18 -0800151 if (mSurface != null) {
152 matrix.getValues(mTmpFloats);
153 mSurface.setPosition((int)mTmpFloats[Matrix.MTRANS_X],
154 (int)mTmpFloats[Matrix.MTRANS_Y]);
155 mSurface.setMatrix(
156 mTmpFloats[Matrix.MSCALE_X], mTmpFloats[Matrix.MSKEW_Y],
157 mTmpFloats[Matrix.MSKEW_X], mTmpFloats[Matrix.MSCALE_Y]);
158 mSurface.setAlpha(alpha);
159 if (DEBUG) {
160 float[] srcPnts = new float[] { 0, 0, mWidth, mHeight };
161 float[] dstPnts = new float[4];
162 matrix.mapPoints(dstPnts, srcPnts);
163 Slog.i(TAG, "Original : (" + srcPnts[0] + "," + srcPnts[1]
164 + ")-(" + srcPnts[2] + "," + srcPnts[3] + ")");
165 Slog.i(TAG, "Transformed: (" + dstPnts[0] + "," + dstPnts[1]
166 + ")-(" + dstPnts[2] + "," + dstPnts[3] + ")");
167 }
Dianne Hackborna1111872010-11-23 20:55:11 -0800168 }
169 }
170
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800171 public static void createRotationMatrix(int rotation, int width, int height,
172 Matrix outMatrix) {
173 switch (rotation) {
174 case Surface.ROTATION_0:
175 outMatrix.reset();
176 break;
177 case Surface.ROTATION_90:
178 outMatrix.setRotate(90, 0, 0);
179 outMatrix.postTranslate(height, 0);
180 break;
181 case Surface.ROTATION_180:
182 outMatrix.setRotate(180, 0, 0);
183 outMatrix.postTranslate(width, height);
184 break;
185 case Surface.ROTATION_270:
186 outMatrix.setRotate(270, 0, 0);
187 outMatrix.postTranslate(0, width);
188 break;
189 }
190 }
191
Dianne Hackbornf9d0be92010-11-24 12:35:25 -0800192 // Must be called while in a transaction.
193 public void setRotation(int rotation) {
194 mCurRotation = rotation;
195
196 // Compute the transformation matrix that must be applied
197 // to the snapshot to make it stay in the same original position
198 // with the current screen rotation.
199 int delta = deltaRotation(rotation, mSnapshotRotation);
Dianne Hackborn0aae2d42010-12-07 23:51:29 -0800200 createRotationMatrix(delta, mWidth, mHeight, mSnapshotInitialMatrix);
Dianne Hackbornf9d0be92010-11-24 12:35:25 -0800201
202 if (DEBUG) Slog.v(TAG, "**** ROTATION: " + delta);
203 setSnapshotTransform(mSnapshotInitialMatrix, 1.0f);
204 }
205
206 /**
207 * Returns true if animating.
208 */
209 public boolean dismiss(long maxAnimationDuration, float animationScale) {
210 // Figure out how the screen has moved from the original rotation.
211 int delta = deltaRotation(mCurRotation, mOriginalRotation);
212 if (false && delta == 0) {
213 // Nothing changed, just remove the snapshot.
214 if (mSurface != null) {
215 mSurface.destroy();
216 mSurface = null;
217 }
218 return false;
219 }
220
221 switch (delta) {
222 case Surface.ROTATION_0:
223 mExitAnimation = AnimationUtils.loadAnimation(mContext,
224 com.android.internal.R.anim.screen_rotate_0_exit);
225 mEnterAnimation = AnimationUtils.loadAnimation(mContext,
226 com.android.internal.R.anim.screen_rotate_0_enter);
227 break;
228 case Surface.ROTATION_90:
229 mExitAnimation = AnimationUtils.loadAnimation(mContext,
230 com.android.internal.R.anim.screen_rotate_plus_90_exit);
231 mEnterAnimation = AnimationUtils.loadAnimation(mContext,
232 com.android.internal.R.anim.screen_rotate_plus_90_enter);
233 break;
234 case Surface.ROTATION_180:
235 mExitAnimation = AnimationUtils.loadAnimation(mContext,
236 com.android.internal.R.anim.screen_rotate_180_exit);
237 mEnterAnimation = AnimationUtils.loadAnimation(mContext,
238 com.android.internal.R.anim.screen_rotate_180_enter);
239 break;
240 case Surface.ROTATION_270:
241 mExitAnimation = AnimationUtils.loadAnimation(mContext,
242 com.android.internal.R.anim.screen_rotate_minus_90_exit);
243 mEnterAnimation = AnimationUtils.loadAnimation(mContext,
244 com.android.internal.R.anim.screen_rotate_minus_90_enter);
245 break;
246 }
247
248 mDisplay.getMetrics(mDisplayMetrics);
249
250 // Initialize the animations. This is a hack, redefining what "parent"
251 // means to allow supplying the last and next size. In this definition
252 // "%p" is the original (let's call it "previous") size, and "%" is the
253 // screen's current/new size.
254 mEnterAnimation.initialize(mDisplayMetrics.widthPixels, mDisplayMetrics.heightPixels,
255 mOriginalWidth, mOriginalHeight);
256 mExitAnimation.initialize(mDisplayMetrics.widthPixels, mDisplayMetrics.heightPixels,
257 mOriginalWidth, mOriginalHeight);
258 mStarted = false;
259
260 mExitAnimation.restrictDuration(maxAnimationDuration);
261 mExitAnimation.scaleCurrentDuration(animationScale);
262 mEnterAnimation.restrictDuration(maxAnimationDuration);
263 mEnterAnimation.scaleCurrentDuration(animationScale);
264
265 return true;
266 }
267
268 public void kill() {
269 if (mSurface != null) {
270 mSurface.destroy();
271 mSurface = null;
272 }
Dianne Hackborn352cc982011-01-04 11:34:18 -0800273 if (mBlackSurface != null) {
274 mBlackSurface.destroy();
275 mBlackSurface = null;
276 }
Dianne Hackbornf9d0be92010-11-24 12:35:25 -0800277 if (mExitAnimation != null) {
278 mExitAnimation.cancel();
279 mExitAnimation = null;
280 }
281 if (mEnterAnimation != null) {
282 mEnterAnimation.cancel();
283 mEnterAnimation = null;
284 }
285 }
286
287 public boolean isAnimating() {
288 return mEnterAnimation != null || mExitAnimation != null;
289 }
290
291 public boolean stepAnimation(long now) {
292 if (mEnterAnimation == null && mExitAnimation == null) {
293 return false;
294 }
295
296 if (!mStarted) {
297 mEnterAnimation.setStartTime(now);
298 mExitAnimation.setStartTime(now);
299 mStarted = true;
300 }
301
302 mExitTransformation.clear();
303 boolean moreExit = false;
304 if (mExitAnimation != null) {
305 moreExit = mExitAnimation.getTransformation(now, mExitTransformation);
306 if (DEBUG) Slog.v(TAG, "Stepped exit: " + mExitTransformation);
307 if (!moreExit) {
308 if (DEBUG) Slog.v(TAG, "Exit animation done!");
309 mExitAnimation.cancel();
310 mExitAnimation = null;
311 mExitTransformation.clear();
312 if (mSurface != null) {
313 mSurface.destroy();
314 mSurface = null;
315 }
Dianne Hackborn352cc982011-01-04 11:34:18 -0800316 if (mBlackSurface != null) {
317 mBlackSurface.destroy();
318 mBlackSurface = null;
319 }
Dianne Hackbornf9d0be92010-11-24 12:35:25 -0800320 }
321 }
322
323 mEnterTransformation.clear();
324 boolean moreEnter = false;
325 if (mEnterAnimation != null) {
326 moreEnter = mEnterAnimation.getTransformation(now, mEnterTransformation);
327 if (!moreEnter) {
328 mEnterAnimation.cancel();
329 mEnterAnimation = null;
330 mEnterTransformation.clear();
331 }
332 }
333
Dianne Hackborn352cc982011-01-04 11:34:18 -0800334 mSnapshotFinalMatrix.setConcat(mExitTransformation.getMatrix(), mSnapshotInitialMatrix);
335 setSnapshotTransform(mSnapshotFinalMatrix, mExitTransformation.getAlpha());
Dianne Hackbornf9d0be92010-11-24 12:35:25 -0800336
337 return moreEnter || moreExit;
338 }
339
340 public Transformation getEnterTransformation() {
341 return mEnterTransformation;
Dianne Hackborna1111872010-11-23 20:55:11 -0800342 }
343}