1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
|
// Copyright 2012 Google Inc. All Rights Reserved.
package com.android.server.wm;
import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION_STARTING;
import static com.android.server.wm.WindowManagerService.LayoutFields.SET_ORIENTATION_CHANGE_COMPLETE;
import static com.android.server.wm.WindowManagerService.LayoutFields.SET_TURN_ON_SCREEN;
import android.content.Context;
import android.graphics.Matrix;
import android.graphics.PixelFormat;
import android.graphics.Point;
import android.graphics.PointF;
import android.graphics.Rect;
import android.graphics.Region;
import android.os.Debug;
import android.util.Slog;
import android.view.DisplayInfo;
import android.view.Surface;
import android.view.SurfaceSession;
import android.view.WindowManager;
import android.view.WindowManagerPolicy;
import android.view.WindowManager.LayoutParams;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.Transformation;
import com.android.server.wm.WindowManagerService.H;
import java.io.PrintWriter;
import java.util.ArrayList;
class WinAnimatorList extends ArrayList<WindowStateAnimator> {
public WinAnimatorList() {
super();
}
public WinAnimatorList(WinAnimatorList other) {
super(other);
}
}
/**
* Keep track of animations and surface operations for a single WindowState.
**/
class WindowStateAnimator {
static final boolean DEBUG_VISIBILITY = WindowManagerService.DEBUG_VISIBILITY;
static final boolean DEBUG_ANIM = WindowManagerService.DEBUG_ANIM;
static final boolean DEBUG_LAYERS = WindowManagerService.DEBUG_LAYERS;
static final boolean DEBUG_STARTING_WINDOW = WindowManagerService.DEBUG_STARTING_WINDOW;
static final boolean SHOW_TRANSACTIONS = WindowManagerService.SHOW_TRANSACTIONS;
static final boolean SHOW_LIGHT_TRANSACTIONS = WindowManagerService.SHOW_LIGHT_TRANSACTIONS;
static final boolean SHOW_SURFACE_ALLOC = WindowManagerService.SHOW_SURFACE_ALLOC;
static final boolean localLOGV = WindowManagerService.localLOGV;
static final boolean DEBUG_ORIENTATION = WindowManagerService.DEBUG_ORIENTATION;
static final boolean DEBUG_SURFACE_TRACE = WindowManagerService.DEBUG_SURFACE_TRACE;
static final String TAG = "WindowStateAnimator";
// Unchanging local convenience fields.
final WindowManagerService mService;
final WindowState mWin;
final WindowStateAnimator mAttachedWinAnimator;
final WindowAnimator mAnimator;
AppWindowAnimator mAppAnimator;
final Session mSession;
final WindowManagerPolicy mPolicy;
final Context mContext;
final boolean mIsWallpaper;
// If this is a universe background window, this is the transformation
// it is applying to the rest of the universe.
final Transformation mUniverseTransform = new Transformation();
// Currently running animation.
boolean mAnimating;
boolean mLocalAnimating;
Animation mAnimation;
boolean mAnimationIsEntrance;
boolean mHasTransformation;
boolean mHasLocalTransformation;
final Transformation mTransformation = new Transformation();
boolean mWasAnimating; // Were we animating going into the most recent animation step?
int mAnimLayer;
int mLastLayer;
Surface mSurface;
Surface mPendingDestroySurface;
/**
* Set when we have changed the size of the surface, to know that
* we must tell them application to resize (and thus redraw itself).
*/
boolean mSurfaceResized;
/**
* Set if the client has asked that the destroy of its surface be delayed
* until it explicitly says it is okay.
*/
boolean mSurfaceDestroyDeferred;
float mShownAlpha = 0;
float mAlpha = 0;
float mLastAlpha = 0;
// Used to save animation distances between the time they are calculated and when they are
// used.
int mAnimDw;
int mAnimDh;
float mDsDx=1, mDtDx=0, mDsDy=0, mDtDy=1;
float mLastDsDx=1, mLastDtDx=0, mLastDsDy=0, mLastDtDy=1;
boolean mHaveMatrix;
// For debugging, this is the last information given to the surface flinger.
boolean mSurfaceShown;
float mSurfaceX, mSurfaceY, mSurfaceW, mSurfaceH;
int mSurfaceLayer;
float mSurfaceAlpha;
// Set to true if, when the window gets displayed, it should perform
// an enter animation.
boolean mEnterAnimationPending;
/** This is set when there is no Surface */
static final int NO_SURFACE = 0;
/** This is set after the Surface has been created but before the window has been drawn. During
* this time the surface is hidden. */
static final int DRAW_PENDING = 1;
/** This is set after the window has finished drawing for the first time but before its surface
* is shown. The surface will be displayed when the next layout is run. */
static final int COMMIT_DRAW_PENDING = 2;
/** This is set during the time after the window's drawing has been committed, and before its
* surface is actually shown. It is used to delay showing the surface until all windows in a
* token are ready to be shown. */
static final int READY_TO_SHOW = 3;
/** Set when the window has been shown in the screen the first time. */
static final int HAS_DRAWN = 4;
static String drawStateToString(int state) {
switch (state) {
case NO_SURFACE: return "NO_SURFACE";
case DRAW_PENDING: return "DRAW_PENDING";
case COMMIT_DRAW_PENDING: return "COMMIT_DRAW_PENDING";
case READY_TO_SHOW: return "READY_TO_SHOW";
case HAS_DRAWN: return "HAS_DRAWN";
default: return Integer.toString(state);
}
}
int mDrawState;
/** Was this window last hidden? */
boolean mLastHidden;
int mAttrFlags;
int mAttrType;
final int mLayerStack;
public WindowStateAnimator(final WindowState win) {
final WindowManagerService service = win.mService;
mService = service;
mAnimator = service.mAnimator;
mPolicy = service.mPolicy;
mContext = service.mContext;
final DisplayInfo displayInfo = win.mDisplayContent.getDisplayInfo();
mAnimDw = displayInfo.appWidth;
mAnimDh = displayInfo.appHeight;
mWin = win;
mAttachedWinAnimator = win.mAttachedWindow == null
? null : win.mAttachedWindow.mWinAnimator;
mAppAnimator = win.mAppToken == null ? null : win.mAppToken.mAppAnimator;
mSession = win.mSession;
mAttrFlags = win.mAttrs.flags;
mAttrType = win.mAttrs.type;
mIsWallpaper = win.mIsWallpaper;
mLayerStack = win.mDisplayContent.getDisplay().getLayerStack();
}
public void setAnimation(Animation anim) {
if (localLOGV) Slog.v(TAG, "Setting animation in " + this + ": " + anim);
mAnimating = false;
mLocalAnimating = false;
mAnimation = anim;
mAnimation.restrictDuration(WindowManagerService.MAX_ANIMATION_DURATION);
mAnimation.scaleCurrentDuration(mService.mWindowAnimationScale);
// Start out animation gone if window is gone, or visible if window is visible.
mTransformation.clear();
mTransformation.setAlpha(mLastHidden ? 0 : 1);
mHasLocalTransformation = true;
}
public void clearAnimation() {
if (mAnimation != null) {
mAnimating = true;
mLocalAnimating = false;
mAnimation.cancel();
mAnimation = null;
}
}
/** Is the window or its container currently animating? */
boolean isAnimating() {
return mAnimation != null
|| (mAttachedWinAnimator != null && mAttachedWinAnimator.mAnimation != null)
|| (mAppAnimator != null &&
(mAppAnimator.animation != null
|| mAppAnimator.mAppToken.inPendingTransaction));
}
/** Is the window animating the DummyAnimation? */
boolean isDummyAnimation() {
return mAppAnimator != null
&& mAppAnimator.animation == AppWindowAnimator.sDummyAnimation;
}
/** Is this window currently animating? */
boolean isWindowAnimating() {
return mAnimation != null;
}
void cancelExitAnimationForNextAnimationLocked() {
if (mAnimation != null) {
mAnimation.cancel();
mAnimation = null;
mLocalAnimating = false;
destroySurfaceLocked();
}
}
private boolean stepAnimation(long currentTime) {
if ((mAnimation == null) || !mLocalAnimating) {
return false;
}
mTransformation.clear();
final boolean more = mAnimation.getTransformation(currentTime, mTransformation);
if (false && DEBUG_ANIM) Slog.v(
TAG, "Stepped animation in " + this +
": more=" + more + ", xform=" + mTransformation);
return more;
}
// This must be called while inside a transaction. Returns true if
// there is more animation to run.
boolean stepAnimationLocked(long currentTime) {
// Save the animation state as it was before this step so WindowManagerService can tell if
// we just started or just stopped animating by comparing mWasAnimating with isAnimating().
mWasAnimating = mAnimating;
if (mService.okToDisplay()) {
// We will run animations as long as the display isn't frozen.
if (mWin.isDrawnLw() && mAnimation != null) {
mHasTransformation = true;
mHasLocalTransformation = true;
if (!mLocalAnimating) {
if (DEBUG_ANIM) Slog.v(
TAG, "Starting animation in " + this +
" @ " + currentTime + ": ww=" + mWin.mFrame.width() +
" wh=" + mWin.mFrame.height() +
" dw=" + mAnimDw + " dh=" + mAnimDh +
" scale=" + mService.mWindowAnimationScale);
mAnimation.initialize(mWin.mFrame.width(), mWin.mFrame.height(),
mAnimDw, mAnimDh);
final DisplayInfo displayInfo = mWin.mDisplayContent.getDisplayInfo();
mAnimDw = displayInfo.appWidth;
mAnimDh = displayInfo.appHeight;
mAnimation.setStartTime(currentTime);
mLocalAnimating = true;
mAnimating = true;
}
if ((mAnimation != null) && mLocalAnimating) {
if (stepAnimation(currentTime)) {
return true;
}
}
if (DEBUG_ANIM) Slog.v(
TAG, "Finished animation in " + this +
" @ " + currentTime);
//WindowManagerService.this.dump();
}
mHasLocalTransformation = false;
if ((!mLocalAnimating || mAnimationIsEntrance) && mAppAnimator != null
&& mAppAnimator.animation != null) {
// When our app token is animating, we kind-of pretend like
// we are as well. Note the mLocalAnimating mAnimationIsEntrance
// part of this check means that we will only do this if
// our window is not currently exiting, or it is not
// locally animating itself. The idea being that one that
// is exiting and doing a local animation should be removed
// once that animation is done.
mAnimating = true;
mHasTransformation = true;
mTransformation.clear();
return false;
} else if (mHasTransformation) {
// Little trick to get through the path below to act like
// we have finished an animation.
mAnimating = true;
} else if (isAnimating()) {
mAnimating = true;
}
} else if (mAnimation != null) {
// If the display is frozen, and there is a pending animation,
// clear it and make sure we run the cleanup code.
mAnimating = true;
}
if (!mAnimating && !mLocalAnimating) {
return false;
}
// Done animating, clean up.
if (DEBUG_ANIM) Slog.v(
TAG, "Animation done in " + this + ": exiting=" + mWin.mExiting
+ ", reportedVisible="
+ (mWin.mAppToken != null ? mWin.mAppToken.reportedVisible : false));
mAnimating = false;
mLocalAnimating = false;
if (mAnimation != null) {
mAnimation.cancel();
mAnimation = null;
}
if (mAnimator.mWindowDetachedWallpaper == mWin) {
mAnimator.mWindowDetachedWallpaper = null;
}
mAnimLayer = mWin.mLayer;
if (mWin.mIsImWindow) {
mAnimLayer += mService.mInputMethodAnimLayerAdjustment;
} else if (mIsWallpaper) {
mAnimLayer += mService.mWallpaperAnimLayerAdjustment;
}
if (DEBUG_LAYERS) Slog.v(TAG, "Stepping win " + this
+ " anim layer: " + mAnimLayer);
mHasTransformation = false;
mHasLocalTransformation = false;
if (mWin.mPolicyVisibility != mWin.mPolicyVisibilityAfterAnim) {
if (WindowState.DEBUG_VISIBILITY) {
Slog.v(TAG, "Policy visibility changing after anim in " + this + ": "
+ mWin.mPolicyVisibilityAfterAnim);
}
mWin.mPolicyVisibility = mWin.mPolicyVisibilityAfterAnim;
mWin.mDisplayContent.layoutNeeded = true;
if (!mWin.mPolicyVisibility) {
if (mService.mCurrentFocus == mWin) {
mService.mFocusMayChange = true;
}
// Window is no longer visible -- make sure if we were waiting
// for it to be displayed before enabling the display, that
// we allow the display to be enabled now.
mService.enableScreenIfNeededLocked();
}
}
mTransformation.clear();
if (mDrawState == HAS_DRAWN
&& mWin.mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_STARTING
&& mWin.mAppToken != null
&& mWin.mAppToken.firstWindowDrawn
&& mWin.mAppToken.startingData != null) {
if (DEBUG_STARTING_WINDOW) Slog.v(TAG, "Finish starting "
+ mWin.mToken + ": first real window done animating");
mService.mFinishedStarting.add(mWin.mAppToken);
mService.mH.sendEmptyMessage(H.FINISHED_STARTING);
}
finishExit();
final int displayId = mWin.mDisplayContent.getDisplayId();
mAnimator.setPendingLayoutChanges(displayId, WindowManagerPolicy.FINISH_LAYOUT_REDO_ANIM);
if (WindowManagerService.DEBUG_LAYOUT_REPEATS) mService.debugLayoutRepeats(
"WindowStateAnimator", mAnimator.mPendingLayoutChanges.get(displayId));
if (mWin.mAppToken != null) {
mWin.mAppToken.updateReportedVisibilityLocked();
}
return false;
}
void finishExit() {
if (WindowManagerService.DEBUG_ANIM) Slog.v(
TAG, "finishExit in " + this
+ ": exiting=" + mWin.mExiting
+ " remove=" + mWin.mRemoveOnExit
+ " windowAnimating=" + isWindowAnimating());
final int N = mWin.mChildWindows.size();
for (int i=0; i<N; i++) {
mWin.mChildWindows.get(i).mWinAnimator.finishExit();
}
if (!mWin.mExiting) {
return;
}
if (isWindowAnimating()) {
return;
}
if (WindowManagerService.localLOGV) Slog.v(
TAG, "Exit animation finished in " + this
+ ": remove=" + mWin.mRemoveOnExit);
if (mSurface != null) {
mService.mDestroySurface.add(mWin);
mWin.mDestroying = true;
if (WindowState.SHOW_TRANSACTIONS) WindowManagerService.logSurface(
mWin, "HIDE (finishExit)", null);
hide();
}
mWin.mExiting = false;
if (mWin.mRemoveOnExit) {
mService.mPendingRemove.add(mWin);
mWin.mRemoveOnExit = false;
}
mAnimator.hideWallpapersLocked(mWin);
}
void hide() {
if (!mLastHidden) {
//dump();
mLastHidden = true;
if (WindowManagerService.SHOW_TRANSACTIONS) WindowManagerService.logSurface(mWin,
"HIDE (performLayout)", null);
if (mSurface != null) {
mSurfaceShown = false;
try {
mSurface.hide();
} catch (RuntimeException e) {
Slog.w(TAG, "Exception hiding surface in " + mWin);
}
}
}
}
boolean finishDrawingLocked() {
if (DEBUG_STARTING_WINDOW &&
mWin.mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_STARTING) {
Slog.v(TAG, "Finishing drawing window " + mWin + ": mDrawState="
+ drawStateToString(mDrawState));
}
if (mDrawState == DRAW_PENDING) {
if (DEBUG_SURFACE_TRACE || DEBUG_ANIM || SHOW_TRANSACTIONS || DEBUG_ORIENTATION)
Slog.v(TAG, "finishDrawingLocked: mDrawState=COMMIT_DRAW_PENDING " + this + " in "
+ mSurface);
if (DEBUG_STARTING_WINDOW &&
mWin.mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_STARTING) {
Slog.v(TAG, "Draw state now committed in " + mWin);
}
mDrawState = COMMIT_DRAW_PENDING;
return true;
}
return false;
}
// This must be called while inside a transaction.
boolean commitFinishDrawingLocked(long currentTime) {
if (DEBUG_STARTING_WINDOW &&
mWin.mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_STARTING) {
Slog.i(TAG, "commitFinishDrawingLocked: " + mWin + " cur mDrawState="
+ drawStateToString(mDrawState));
}
if (mDrawState != COMMIT_DRAW_PENDING) {
return false;
}
if (DEBUG_SURFACE_TRACE || DEBUG_ANIM) {
Slog.i(TAG, "commitFinishDrawingLocked: mDrawState=READY_TO_SHOW " + mSurface);
}
mDrawState = READY_TO_SHOW;
final boolean starting = mWin.mAttrs.type == TYPE_APPLICATION_STARTING;
final AppWindowToken atoken = mWin.mAppToken;
if (atoken == null || atoken.allDrawn || starting) {
performShowLocked();
}
return true;
}
static class SurfaceTrace extends Surface {
private final static String SURFACE_TAG = "SurfaceTrace";
final static ArrayList<SurfaceTrace> sSurfaces = new ArrayList<SurfaceTrace>();
private float mSurfaceTraceAlpha = 0;
private int mLayer;
private final PointF mPosition = new PointF();
private final Point mSize = new Point();
private final Rect mWindowCrop = new Rect();
private boolean mShown = false;
private int mLayerStack;
private String mName;
public SurfaceTrace(SurfaceSession s,
String name, int w, int h, int format, int flags)
throws OutOfResourcesException {
super(s, name, w, h, format, flags);
mName = name != null ? name : "Not named";
mSize.set(w, h);
Slog.v(SURFACE_TAG, "ctor: " + this + ". Called by "
+ Debug.getCallers(3));
}
@Override
public void setAlpha(float alpha) {
super.setAlpha(alpha);
mSurfaceTraceAlpha = alpha;
Slog.v(SURFACE_TAG, "setAlpha: " + this + ". Called by "
+ Debug.getCallers(3));
}
@Override
public void setLayer(int zorder) {
super.setLayer(zorder);
mLayer = zorder;
Slog.v(SURFACE_TAG, "setLayer: " + this + ". Called by "
+ Debug.getCallers(3));
sSurfaces.remove(this);
int i;
for (i = sSurfaces.size() - 1; i >= 0; i--) {
SurfaceTrace s = sSurfaces.get(i);
if (s.mLayer < zorder) {
break;
}
}
sSurfaces.add(i + 1, this);
}
@Override
public void setPosition(float x, float y) {
super.setPosition(x, y);
mPosition.set(x, y);
Slog.v(SURFACE_TAG, "setPosition: " + this + ". Called by "
+ Debug.getCallers(3));
}
@Override
public void setSize(int w, int h) {
super.setSize(w, h);
mSize.set(w, h);
Slog.v(SURFACE_TAG, "setSize: " + this + ". Called by "
+ Debug.getCallers(3));
}
@Override
public void setWindowCrop(Rect crop) {
super.setWindowCrop(crop);
if (crop != null) {
mWindowCrop.set(crop);
}
Slog.v(SURFACE_TAG, "setWindowCrop: " + this + ". Called by "
+ Debug.getCallers(3));
}
@Override
public void setLayerStack(int layerStack) {
super.setLayerStack(layerStack);
mLayerStack = layerStack;
Slog.v(SURFACE_TAG, "setLayerStack: " + this + ". Called by " + Debug.getCallers(3));
}
@Override
public void hide() {
super.hide();
mShown = false;
Slog.v(SURFACE_TAG, "hide: " + this + ". Called by "
+ Debug.getCallers(3));
}
@Override
public void show() {
super.show();
mShown = true;
Slog.v(SURFACE_TAG, "show: " + this + ". Called by "
+ Debug.getCallers(3));
}
@Override
public void destroy() {
super.destroy();
Slog.v(SURFACE_TAG, "destroy: " + this + ". Called by "
+ Debug.getCallers(3));
sSurfaces.remove(this);
}
@Override
public void release() {
super.release();
Slog.v(SURFACE_TAG, "release: " + this + ". Called by "
+ Debug.getCallers(3));
sSurfaces.remove(this);
}
static void dumpAllSurfaces() {
final int N = sSurfaces.size();
for (int i = 0; i < N; i++) {
Slog.i(TAG, "SurfaceDump: " + sSurfaces.get(i));
}
}
@Override
public String toString() {
return "Surface " + Integer.toHexString(System.identityHashCode(this)) + " "
+ mName + " (" + mLayerStack + "): shown=" + mShown + " layer=" + mLayer
+ " alpha=" + mSurfaceTraceAlpha + " " + mPosition.x + "," + mPosition.y
+ " " + mSize.x + "x" + mSize.y
+ " crop=" + mWindowCrop.toShortString();
}
}
Surface createSurfaceLocked() {
if (mSurface == null) {
if (DEBUG_ANIM || DEBUG_ORIENTATION) Slog.i(TAG,
"createSurface " + this + ": mDrawState=DRAW_PENDING");
mDrawState = DRAW_PENDING;
if (mWin.mAppToken != null) {
mWin.mAppToken.allDrawn = false;
}
mService.makeWindowFreezingScreenIfNeededLocked(mWin);
int flags = Surface.HIDDEN;
final WindowManager.LayoutParams attrs = mWin.mAttrs;
if ((attrs.flags&WindowManager.LayoutParams.FLAG_SECURE) != 0) {
flags |= Surface.SECURE;
}
if (WindowState.DEBUG_VISIBILITY) Slog.v(
TAG, "Creating surface in session "
+ mSession.mSurfaceSession + " window " + this
+ " w=" + mWin.mCompatFrame.width()
+ " h=" + mWin.mCompatFrame.height() + " format="
+ attrs.format + " flags=" + flags);
int w = mWin.mCompatFrame.width();
int h = mWin.mCompatFrame.height();
if ((attrs.flags & LayoutParams.FLAG_SCALED) != 0) {
// for a scaled surface, we always want the requested
// size.
w = mWin.mRequestedWidth;
h = mWin.mRequestedHeight;
}
// Something is wrong and SurfaceFlinger will not like this,
// try to revert to sane values
if (w <= 0) w = 1;
if (h <= 0) h = 1;
mSurfaceShown = false;
mSurfaceLayer = 0;
mSurfaceAlpha = 0;
mSurfaceX = 0;
mSurfaceY = 0;
mSurfaceW = w;
mSurfaceH = h;
mWin.mLastSystemDecorRect.set(0, 0, 0, 0);
try {
final boolean isHwAccelerated = (attrs.flags &
WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED) != 0;
final int format = isHwAccelerated ? PixelFormat.TRANSLUCENT : attrs.format;
if (!PixelFormat.formatHasAlpha(attrs.format)) {
flags |= Surface.OPAQUE;
}
if (DEBUG_SURFACE_TRACE) {
mSurface = new SurfaceTrace(
mSession.mSurfaceSession,
attrs.getTitle().toString(),
w, h, format, flags);
} else {
mSurface = new Surface(
mSession.mSurfaceSession,
attrs.getTitle().toString(),
w, h, format, flags);
}
mWin.mHasSurface = true;
if (SHOW_TRANSACTIONS || SHOW_SURFACE_ALLOC) Slog.i(TAG,
" CREATE SURFACE "
+ mSurface + " IN SESSION "
+ mSession.mSurfaceSession
+ ": pid=" + mSession.mPid + " format="
+ attrs.format + " flags=0x"
+ Integer.toHexString(flags)
+ " / " + this);
} catch (Surface.OutOfResourcesException e) {
mWin.mHasSurface = false;
Slog.w(TAG, "OutOfResourcesException creating surface");
mService.reclaimSomeSurfaceMemoryLocked(this, "create", true);
mDrawState = NO_SURFACE;
return null;
} catch (Exception e) {
mWin.mHasSurface = false;
Slog.e(TAG, "Exception creating surface", e);
mDrawState = NO_SURFACE;
return null;
}
if (WindowManagerService.localLOGV) Slog.v(
TAG, "Got surface: " + mSurface
+ ", set left=" + mWin.mFrame.left + " top=" + mWin.mFrame.top
+ ", animLayer=" + mAnimLayer);
if (SHOW_LIGHT_TRANSACTIONS) {
Slog.i(TAG, ">>> OPEN TRANSACTION createSurfaceLocked");
WindowManagerService.logSurface(mWin, "CREATE pos=("
+ mWin.mFrame.left + "," + mWin.mFrame.top + ") ("
+ mWin.mCompatFrame.width() + "x" + mWin.mCompatFrame.height()
+ "), layer=" + mAnimLayer + " HIDE", null);
}
Surface.openTransaction();
try {
try {
mSurfaceX = mWin.mFrame.left + mWin.mXOffset;
mSurfaceY = mWin.mFrame.top + mWin.mYOffset;
mSurface.setPosition(mSurfaceX, mSurfaceY);
mSurfaceLayer = mAnimLayer;
mSurface.setLayerStack(mLayerStack);
mSurface.setLayer(mAnimLayer);
mSurface.setAlpha(0);
mSurfaceShown = false;
} catch (RuntimeException e) {
Slog.w(TAG, "Error creating surface in " + w, e);
mService.reclaimSomeSurfaceMemoryLocked(this, "create-init", true);
}
mLastHidden = true;
} finally {
Surface.closeTransaction();
if (SHOW_LIGHT_TRANSACTIONS) Slog.i(TAG,
"<<< CLOSE TRANSACTION createSurfaceLocked");
}
if (WindowManagerService.localLOGV) Slog.v(
TAG, "Created surface " + this);
}
return mSurface;
}
void destroySurfaceLocked() {
if (mWin.mAppToken != null && mWin == mWin.mAppToken.startingWindow) {
mWin.mAppToken.startingDisplayed = false;
}
if (mSurface != null) {
int i = mWin.mChildWindows.size();
while (i > 0) {
i--;
WindowState c = mWin.mChildWindows.get(i);
c.mAttachedHidden = true;
}
try {
if (DEBUG_VISIBILITY) {
RuntimeException e = null;
if (!WindowManagerService.HIDE_STACK_CRAWLS) {
e = new RuntimeException();
e.fillInStackTrace();
}
Slog.w(TAG, "Window " + this + " destroying surface "
+ mSurface + ", session " + mSession, e);
}
if (mSurfaceDestroyDeferred) {
if (mSurface != null && mPendingDestroySurface != mSurface) {
if (mPendingDestroySurface != null) {
if (SHOW_TRANSACTIONS || SHOW_SURFACE_ALLOC) {
RuntimeException e = null;
if (!WindowManagerService.HIDE_STACK_CRAWLS) {
e = new RuntimeException();
e.fillInStackTrace();
}
WindowManagerService.logSurface(mWin, "DESTROY PENDING", e);
}
mPendingDestroySurface.destroy();
}
mPendingDestroySurface = mSurface;
}
} else {
if (SHOW_TRANSACTIONS || SHOW_SURFACE_ALLOC) {
RuntimeException e = null;
if (!WindowManagerService.HIDE_STACK_CRAWLS) {
e = new RuntimeException();
e.fillInStackTrace();
}
WindowManagerService.logSurface(mWin, "DESTROY", e);
}
mSurface.destroy();
}
mAnimator.hideWallpapersLocked(mWin);
} catch (RuntimeException e) {
Slog.w(TAG, "Exception thrown when destroying Window " + this
+ " surface " + mSurface + " session " + mSession
+ ": " + e.toString());
}
mSurfaceShown = false;
mSurface = null;
mWin.mHasSurface =false;
mDrawState = NO_SURFACE;
}
}
void destroyDeferredSurfaceLocked() {
try {
if (mPendingDestroySurface != null) {
if (SHOW_TRANSACTIONS || SHOW_SURFACE_ALLOC) {
RuntimeException e = null;
if (!WindowManagerService.HIDE_STACK_CRAWLS) {
e = new RuntimeException();
e.fillInStackTrace();
}
WindowManagerService.logSurface(mWin, "DESTROY PENDING", e);
}
mPendingDestroySurface.destroy();
mAnimator.hideWallpapersLocked(mWin);
}
} catch (RuntimeException e) {
Slog.w(TAG, "Exception thrown when destroying Window "
+ this + " surface " + mPendingDestroySurface
+ " session " + mSession + ": " + e.toString());
}
mSurfaceDestroyDeferred = false;
mPendingDestroySurface = null;
}
void computeShownFrameLocked() {
final boolean selfTransformation = mHasLocalTransformation;
Transformation attachedTransformation =
(mAttachedWinAnimator != null && mAttachedWinAnimator.mHasLocalTransformation)
? mAttachedWinAnimator.mTransformation : null;
Transformation appTransformation = (mAppAnimator != null && mAppAnimator.hasTransformation)
? mAppAnimator.transformation : null;
// Wallpapers are animated based on the "real" window they
// are currently targeting.
if (mIsWallpaper && mAnimator.mLowerWallpaperTarget == null
&& mAnimator.mWallpaperTarget != null) {
final WindowStateAnimator wallpaperAnimator = mAnimator.mWallpaperTarget.mWinAnimator;
if (wallpaperAnimator.mHasLocalTransformation &&
wallpaperAnimator.mAnimation != null &&
!wallpaperAnimator.mAnimation.getDetachWallpaper()) {
attachedTransformation = wallpaperAnimator.mTransformation;
if (WindowManagerService.DEBUG_WALLPAPER && attachedTransformation != null) {
Slog.v(TAG, "WP target attached xform: " + attachedTransformation);
}
}
final AppWindowAnimator wpAppAnimator = mAnimator.mWpAppAnimator;
if (wpAppAnimator != null && wpAppAnimator.hasTransformation
&& wpAppAnimator.animation != null
&& !wpAppAnimator.animation.getDetachWallpaper()) {
appTransformation = wpAppAnimator.transformation;
if (WindowManagerService.DEBUG_WALLPAPER && appTransformation != null) {
Slog.v(TAG, "WP target app xform: " + appTransformation);
}
}
}
final int displayId = mWin.getDisplayId();
final ScreenRotationAnimation screenRotationAnimation =
mAnimator.getScreenRotationAnimationLocked(displayId);
final boolean screenAnimation =
screenRotationAnimation != null && screenRotationAnimation.isAnimating();
if (selfTransformation || attachedTransformation != null
|| appTransformation != null || screenAnimation) {
// cache often used attributes locally
final Rect frame = mWin.mFrame;
final float tmpFloats[] = mService.mTmpFloats;
final Matrix tmpMatrix = mWin.mTmpMatrix;
// Compute the desired transformation.
if (screenAnimation) {
// If we are doing a screen animation, the global rotation
// applied to windows can result in windows that are carefully
// aligned with each other to slightly separate, allowing you
// to see what is behind them. An unsightly mess. This...
// thing... magically makes it call good: scale each window
// slightly (two pixels larger in each dimension, from the
// window's center).
final float w = frame.width();
final float h = frame.height();
if (w>=1 && h>=1) {
tmpMatrix.setScale(1 + 2/w, 1 + 2/h, w/2, h/2);
} else {
tmpMatrix.reset();
}
} else {
tmpMatrix.reset();
}
tmpMatrix.postScale(mWin.mGlobalScale, mWin.mGlobalScale);
if (selfTransformation) {
tmpMatrix.postConcat(mTransformation.getMatrix());
}
tmpMatrix.postTranslate(frame.left + mWin.mXOffset, frame.top + mWin.mYOffset);
if (attachedTransformation != null) {
tmpMatrix.postConcat(attachedTransformation.getMatrix());
}
if (appTransformation != null) {
tmpMatrix.postConcat(appTransformation.getMatrix());
}
if (mAnimator.mUniverseBackground != null) {
tmpMatrix.postConcat(mAnimator.mUniverseBackground.mUniverseTransform.getMatrix());
}
if (screenAnimation) {
tmpMatrix.postConcat(screenRotationAnimation.getEnterTransformation().getMatrix());
}
MagnificationSpec spec = mWin.getWindowMagnificationSpecLocked();
if (spec != null && !spec.isNop()) {
tmpMatrix.postScale(spec.mScale, spec.mScale);
tmpMatrix.postTranslate(spec.mOffsetX, spec.mOffsetY);
}
// "convert" it into SurfaceFlinger's format
// (a 2x2 matrix + an offset)
// Here we must not transform the position of the surface
// since it is already included in the transformation.
//Slog.i(TAG, "Transform: " + matrix);
mHaveMatrix = true;
tmpMatrix.getValues(tmpFloats);
mDsDx = tmpFloats[Matrix.MSCALE_X];
mDtDx = tmpFloats[Matrix.MSKEW_Y];
mDsDy = tmpFloats[Matrix.MSKEW_X];
mDtDy = tmpFloats[Matrix.MSCALE_Y];
float x = tmpFloats[Matrix.MTRANS_X];
float y = tmpFloats[Matrix.MTRANS_Y];
int w = frame.width();
int h = frame.height();
mWin.mShownFrame.set(x, y, x+w, y+h);
// Now set the alpha... but because our current hardware
// can't do alpha transformation on a non-opaque surface,
// turn it off if we are running an animation that is also
// transforming since it is more important to have that
// animation be smooth.
mShownAlpha = mAlpha;
if (!mService.mLimitedAlphaCompositing
|| (!PixelFormat.formatHasAlpha(mWin.mAttrs.format)
|| (mWin.isIdentityMatrix(mDsDx, mDtDx, mDsDy, mDtDy)
&& x == frame.left && y == frame.top))) {
//Slog.i(TAG, "Applying alpha transform");
if (selfTransformation) {
mShownAlpha *= mTransformation.getAlpha();
}
if (attachedTransformation != null) {
mShownAlpha *= attachedTransformation.getAlpha();
}
if (appTransformation != null) {
mShownAlpha *= appTransformation.getAlpha();
}
if (mAnimator.mUniverseBackground != null) {
mShownAlpha *= mAnimator.mUniverseBackground.mUniverseTransform.getAlpha();
}
if (screenAnimation) {
mShownAlpha *= screenRotationAnimation.getEnterTransformation().getAlpha();
}
} else {
//Slog.i(TAG, "Not applying alpha transform");
}
if ((DEBUG_SURFACE_TRACE || WindowManagerService.localLOGV)
&& (mShownAlpha == 1.0 || mShownAlpha == 0.0)) Slog.v(
TAG, "computeShownFrameLocked: Animating " + this + " mAlpha=" + mAlpha
+ " self=" + (selfTransformation ? mTransformation.getAlpha() : "null")
+ " attached=" + (attachedTransformation == null ?
"null" : attachedTransformation.getAlpha())
+ " app=" + (appTransformation == null ? "null" : appTransformation.getAlpha())
+ " screen=" + (screenAnimation ?
screenRotationAnimation.getEnterTransformation().getAlpha() : "null"));
return;
} else if (mIsWallpaper &&
(mAnimator.mPendingActions & WindowAnimator.WALLPAPER_ACTION_PENDING) != 0) {
return;
}
if (WindowManagerService.localLOGV) Slog.v(
TAG, "computeShownFrameLocked: " + this +
" not attached, mAlpha=" + mAlpha);
final boolean applyUniverseTransformation = (mAnimator.mUniverseBackground != null
&& mWin.mAttrs.type != WindowManager.LayoutParams.TYPE_UNIVERSE_BACKGROUND
&& mWin.mBaseLayer < mAnimator.mAboveUniverseLayer);
MagnificationSpec spec = mWin.getWindowMagnificationSpecLocked();
if (applyUniverseTransformation || spec != null) {
final Rect frame = mWin.mFrame;
final float tmpFloats[] = mService.mTmpFloats;
final Matrix tmpMatrix = mWin.mTmpMatrix;
tmpMatrix.setScale(mWin.mGlobalScale, mWin.mGlobalScale);
tmpMatrix.postTranslate(frame.left + mWin.mXOffset, frame.top + mWin.mYOffset);
if (applyUniverseTransformation) {
tmpMatrix.postConcat(mAnimator.mUniverseBackground.mUniverseTransform.getMatrix());
}
if (spec != null && !spec.isNop()) {
tmpMatrix.postScale(spec.mScale, spec.mScale);
tmpMatrix.postTranslate(spec.mOffsetX, spec.mOffsetY);
}
tmpMatrix.getValues(tmpFloats);
mHaveMatrix = true;
mDsDx = tmpFloats[Matrix.MSCALE_X];
mDtDx = tmpFloats[Matrix.MSKEW_Y];
mDsDy = tmpFloats[Matrix.MSKEW_X];
mDtDy = tmpFloats[Matrix.MSCALE_Y];
float x = tmpFloats[Matrix.MTRANS_X];
float y = tmpFloats[Matrix.MTRANS_Y];
int w = frame.width();
int h = frame.height();
mWin.mShownFrame.set(x, y, x + w, y + h);
mShownAlpha = mAlpha;
if (applyUniverseTransformation) {
mShownAlpha *= mAnimator.mUniverseBackground.mUniverseTransform.getAlpha();
}
} else {
mWin.mShownFrame.set(mWin.mFrame);
if (mWin.mXOffset != 0 || mWin.mYOffset != 0) {
mWin.mShownFrame.offset(mWin.mXOffset, mWin.mYOffset);
}
mShownAlpha = mAlpha;
mHaveMatrix = false;
mDsDx = mWin.mGlobalScale;
mDtDx = 0;
mDsDy = 0;
mDtDy = mWin.mGlobalScale;
}
}
void applyDecorRect(final Rect decorRect) {
final WindowState w = mWin;
// Compute the offset of the window in relation to the decor rect.
final int offX = w.mXOffset + w.mFrame.left;
final int offY = w.mYOffset + w.mFrame.top;
// Initialize the decor rect to the entire frame.
w.mSystemDecorRect.set(0, 0, w.mFrame.width(), w.mFrame.height());
// Intersect with the decor rect, offsetted by window position.
w.mSystemDecorRect.intersect(decorRect.left-offX, decorRect.top-offY,
decorRect.right-offX, decorRect.bottom-offY);
// If size compatibility is being applied to the window, the
// surface is scaled relative to the screen. Also apply this
// scaling to the crop rect. We aren't using the standard rect
// scale function because we want to round things to make the crop
// always round to a larger rect to ensure we don't crop too
// much and hide part of the window that should be seen.
if (w.mEnforceSizeCompat && w.mInvGlobalScale != 1.0f) {
final float scale = w.mInvGlobalScale;
w.mSystemDecorRect.left = (int) (w.mSystemDecorRect.left * scale - 0.5f);
w.mSystemDecorRect.top = (int) (w.mSystemDecorRect.top * scale - 0.5f);
w.mSystemDecorRect.right = (int) ((w.mSystemDecorRect.right+1) * scale - 0.5f);
w.mSystemDecorRect.bottom = (int) ((w.mSystemDecorRect.bottom+1) * scale - 0.5f);
}
}
void updateSurfaceWindowCrop(final boolean recoveringMemory) {
final WindowState w = mWin;
DisplayInfo displayInfo = w.mDisplayContent.getDisplayInfo();
// Need to recompute a new system decor rect each time.
if ((w.mAttrs.flags & LayoutParams.FLAG_SCALED) != 0) {
// Currently can't do this cropping for scaled windows. We'll
// just keep the crop rect the same as the source surface.
w.mSystemDecorRect.set(0, 0, w.mRequestedWidth, w.mRequestedHeight);
} else if (!w.isDefaultDisplay()) {
// On a different display there is no system decor. Crop the window
// by the screen boundaries.
w.mSystemDecorRect.set(0, 0, w.mCompatFrame.width(), w.mCompatFrame.height());
w.mSystemDecorRect.intersect(-w.mCompatFrame.left, -w.mCompatFrame.top,
displayInfo.logicalWidth - w.mCompatFrame.left,
displayInfo.logicalHeight - w.mCompatFrame.top);
} else if (w.mLayer >= mService.mSystemDecorLayer) {
// Above the decor layer is easy, just use the entire window.
// Unless we have a universe background... in which case all the
// windows need to be cropped by the screen, so they don't cover
// the universe background.
if (mAnimator.mUniverseBackground == null) {
w.mSystemDecorRect.set(0, 0, w.mCompatFrame.width(),
w.mCompatFrame.height());
} else {
applyDecorRect(mService.mScreenRect);
}
} else if (w.mAttrs.type == WindowManager.LayoutParams.TYPE_UNIVERSE_BACKGROUND) {
// The universe background isn't cropped.
w.mSystemDecorRect.set(0, 0, w.mCompatFrame.width(),
w.mCompatFrame.height());
} else {
applyDecorRect(mService.mSystemDecorRect);
}
if (!w.mSystemDecorRect.equals(w.mLastSystemDecorRect)) {
w.mLastSystemDecorRect.set(w.mSystemDecorRect);
try {
if (WindowManagerService.SHOW_TRANSACTIONS) WindowManagerService.logSurface(w,
"CROP " + w.mSystemDecorRect.toShortString(), null);
mSurface.setWindowCrop(w.mSystemDecorRect);
} catch (RuntimeException e) {
Slog.w(TAG, "Error setting crop surface of " + w
+ " crop=" + w.mSystemDecorRect.toShortString(), e);
if (!recoveringMemory) {
mService.reclaimSomeSurfaceMemoryLocked(this, "crop", true);
}
}
}
}
void setSurfaceBoundariesLocked(final boolean recoveringMemory) {
final WindowState w = mWin;
int width, height;
if ((w.mAttrs.flags & LayoutParams.FLAG_SCALED) != 0) {
// for a scaled surface, we just want to use
// the requested size.
width = w.mRequestedWidth;
height = w.mRequestedHeight;
} else {
width = w.mCompatFrame.width();
height = w.mCompatFrame.height();
}
if (width < 1) {
width = 1;
}
if (height < 1) {
height = 1;
}
final boolean surfaceResized = mSurfaceW != width || mSurfaceH != height;
if (surfaceResized) {
mSurfaceW = width;
mSurfaceH = height;
}
final float left = w.mShownFrame.left;
final float top = w.mShownFrame.top;
if (mSurfaceX != left || mSurfaceY != top) {
try {
if (WindowManagerService.SHOW_TRANSACTIONS) WindowManagerService.logSurface(w,
"POS " + left + ", " + top, null);
mSurfaceX = left;
mSurfaceY = top;
mSurface.setPosition(left, top);
} catch (RuntimeException e) {
Slog.w(TAG, "Error positioning surface of " + w
+ " pos=(" + left
+ "," + top + ")", e);
if (!recoveringMemory) {
mService.reclaimSomeSurfaceMemoryLocked(this, "position", true);
}
}
}
if (surfaceResized) {
try {
if (WindowManagerService.SHOW_TRANSACTIONS) WindowManagerService.logSurface(w,
"SIZE " + width + "x" + height, null);
mSurfaceResized = true;
mSurface.setSize(width, height);
final int displayId = w.mDisplayContent.getDisplayId();
mAnimator.setPendingLayoutChanges(displayId,
WindowManagerPolicy.FINISH_LAYOUT_REDO_WALLPAPER);
if ((w.mAttrs.flags & LayoutParams.FLAG_DIM_BEHIND) != 0) {
final DisplayInfo displayInfo = mWin.mDisplayContent.getDisplayInfo();
mService.startDimmingLocked(this, w.mExiting ? 0 : w.mAttrs.dimAmount,
displayInfo.appWidth, displayInfo.appHeight);
}
} catch (RuntimeException e) {
// If something goes wrong with the surface (such
// as running out of memory), don't take down the
// entire system.
Slog.e(TAG, "Error resizing surface of " + w
+ " size=(" + width + "x" + height + ")", e);
if (!recoveringMemory) {
mService.reclaimSomeSurfaceMemoryLocked(this, "size", true);
}
}
}
updateSurfaceWindowCrop(recoveringMemory);
}
public void prepareSurfaceLocked(final boolean recoveringMemory) {
final WindowState w = mWin;
if (mSurface == null) {
if (w.mOrientationChanging) {
if (DEBUG_ORIENTATION) {
Slog.v(TAG, "Orientation change skips hidden " + w);
}
w.mOrientationChanging = false;
}
return;
}
boolean displayed = false;
computeShownFrameLocked();
setSurfaceBoundariesLocked(recoveringMemory);
if (mIsWallpaper && !mWin.mWallpaperVisible) {
// Wallpaper is no longer visible and there is no wp target => hide it.
hide();
} else if (w.mAttachedHidden || !w.isReadyForDisplay()) {
hide();
mAnimator.hideWallpapersLocked(w);
// If we are waiting for this window to handle an
// orientation change, well, it is hidden, so
// doesn't really matter. Note that this does
// introduce a potential glitch if the window
// becomes unhidden before it has drawn for the
// new orientation.
if (w.mOrientationChanging) {
w.mOrientationChanging = false;
if (DEBUG_ORIENTATION) Slog.v(TAG,
"Orientation change skips hidden " + w);
}
} else if (mLastLayer != mAnimLayer
|| mLastAlpha != mShownAlpha
|| mLastDsDx != mDsDx
|| mLastDtDx != mDtDx
|| mLastDsDy != mDsDy
|| mLastDtDy != mDtDy
|| w.mLastHScale != w.mHScale
|| w.mLastVScale != w.mVScale
|| mLastHidden) {
displayed = true;
mLastAlpha = mShownAlpha;
mLastLayer = mAnimLayer;
mLastDsDx = mDsDx;
mLastDtDx = mDtDx;
mLastDsDy = mDsDy;
mLastDtDy = mDtDy;
w.mLastHScale = w.mHScale;
w.mLastVScale = w.mVScale;
if (WindowManagerService.SHOW_TRANSACTIONS) WindowManagerService.logSurface(w,
"alpha=" + mShownAlpha + " layer=" + mAnimLayer
+ " matrix=[" + (mDsDx*w.mHScale)
+ "," + (mDtDx*w.mVScale)
+ "][" + (mDsDy*w.mHScale)
+ "," + (mDtDy*w.mVScale) + "]", null);
if (mSurface != null) {
try {
mSurfaceAlpha = mShownAlpha;
mSurface.setAlpha(mShownAlpha);
mSurfaceLayer = mAnimLayer;
mSurface.setLayer(mAnimLayer);
mSurface.setMatrix(
mDsDx*w.mHScale, mDtDx*w.mVScale,
mDsDy*w.mHScale, mDtDy*w.mVScale);
if (mLastHidden && mDrawState == HAS_DRAWN) {
if (WindowManagerService.SHOW_TRANSACTIONS) WindowManagerService.logSurface(w,
"SHOW (performLayout)", null);
if (WindowManagerService.DEBUG_VISIBILITY) Slog.v(TAG, "Showing " + w
+ " during relayout");
if (showSurfaceRobustlyLocked()) {
mLastHidden = false;
if (mIsWallpaper) {
mService.dispatchWallpaperVisibility(w, true);
}
} else {
w.mOrientationChanging = false;
}
}
if (mSurface != null) {
w.mToken.hasVisible = true;
}
} catch (RuntimeException e) {
Slog.w(TAG, "Error updating surface in " + w, e);
if (!recoveringMemory) {
mService.reclaimSomeSurfaceMemoryLocked(this, "update", true);
}
}
}
} else {
if (DEBUG_ANIM && isAnimating()) {
Slog.v(TAG, "prepareSurface: No changes in animation for " + this);
}
displayed = true;
}
if (displayed) {
if (w.mOrientationChanging) {
if (!w.isDrawnLw()) {
mAnimator.mBulkUpdateParams &= ~SET_ORIENTATION_CHANGE_COMPLETE;
if (DEBUG_ORIENTATION) Slog.v(TAG,
"Orientation continue waiting for draw in " + w);
} else {
w.mOrientationChanging = false;
if (DEBUG_ORIENTATION) Slog.v(TAG, "Orientation change complete in " + w);
}
}
w.mToken.hasVisible = true;
}
}
void setTransparentRegionHint(final Region region) {
if (mSurface == null) {
Slog.w(TAG, "setTransparentRegionHint: null mSurface after mHasSurface true");
return;
}
if (SHOW_LIGHT_TRANSACTIONS) Slog.i(TAG,
">>> OPEN TRANSACTION setTransparentRegion");
Surface.openTransaction();
try {
if (SHOW_TRANSACTIONS) WindowManagerService.logSurface(mWin,
"transparentRegionHint=" + region, null);
mSurface.setTransparentRegionHint(region);
} finally {
Surface.closeTransaction();
if (SHOW_LIGHT_TRANSACTIONS) Slog.i(TAG,
"<<< CLOSE TRANSACTION setTransparentRegion");
}
}
void setWallpaperOffset(int left, int top) {
mSurfaceX = left;
mSurfaceY = top;
if (mAnimating) {
// If this window (or its app token) is animating, then the position
// of the surface will be re-computed on the next animation frame.
// We can't poke it directly here because it depends on whatever
// transformation is being applied by the animation.
return;
}
if (SHOW_LIGHT_TRANSACTIONS) Slog.i(TAG,
">>> OPEN TRANSACTION setWallpaperOffset");
Surface.openTransaction();
try {
if (WindowManagerService.SHOW_TRANSACTIONS) WindowManagerService.logSurface(mWin,
"POS " + left + ", " + top, null);
mSurface.setPosition(mWin.mFrame.left + left, mWin.mFrame.top + top);
updateSurfaceWindowCrop(false);
} catch (RuntimeException e) {
Slog.w(TAG, "Error positioning surface of " + mWin
+ " pos=(" + left + "," + top + ")", e);
} finally {
Surface.closeTransaction();
if (SHOW_LIGHT_TRANSACTIONS) Slog.i(TAG,
"<<< CLOSE TRANSACTION setWallpaperOffset");
}
}
// This must be called while inside a transaction.
boolean performShowLocked() {
if (mWin.isOtherUsersAppWindow()) {
Slog.w(TAG, "current user violation " + mService.mCurrentUserId + " trying to display "
+ this + ", type " + mWin.mAttrs.type + ", belonging to " + mWin.mOwnerUid);
return false;
}
if (DEBUG_VISIBILITY || (DEBUG_STARTING_WINDOW &&
mWin.mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_STARTING)) {
RuntimeException e = null;
if (!WindowManagerService.HIDE_STACK_CRAWLS) {
e = new RuntimeException();
e.fillInStackTrace();
}
Slog.v(TAG, "performShow on " + this
+ ": mDrawState=" + mDrawState + " readyForDisplay="
+ mWin.isReadyForDisplayIgnoringKeyguard()
+ " starting=" + (mWin.mAttrs.type == TYPE_APPLICATION_STARTING)
+ " during animation: policyVis=" + mWin.mPolicyVisibility
+ " attHidden=" + mWin.mAttachedHidden
+ " tok.hiddenRequested="
+ (mWin.mAppToken != null ? mWin.mAppToken.hiddenRequested : false)
+ " tok.hidden="
+ (mWin.mAppToken != null ? mWin.mAppToken.hidden : false)
+ " animating=" + mAnimating
+ " tok animating="
+ (mAppAnimator != null ? mAppAnimator.animating : false), e);
}
if (mDrawState == READY_TO_SHOW && mWin.isReadyForDisplayIgnoringKeyguard()) {
if (SHOW_TRANSACTIONS || DEBUG_ORIENTATION)
WindowManagerService.logSurface(mWin, "SHOW (performShowLocked)", null);
if (DEBUG_VISIBILITY || (DEBUG_STARTING_WINDOW &&
mWin.mAttrs.type == WindowManager.LayoutParams.TYPE_APPLICATION_STARTING)) {
Slog.v(TAG, "Showing " + this
+ " during animation: policyVis=" + mWin.mPolicyVisibility
+ " attHidden=" + mWin.mAttachedHidden
+ " tok.hiddenRequested="
+ (mWin.mAppToken != null ? mWin.mAppToken.hiddenRequested : false)
+ " tok.hidden="
+ (mWin.mAppToken != null ? mWin.mAppToken.hidden : false)
+ " animating=" + mAnimating
+ " tok animating="
+ (mAppAnimator != null ? mAppAnimator.animating : false));
}
mService.enableScreenIfNeededLocked();
applyEnterAnimationLocked();
// Force the show in the next prepareSurfaceLocked() call.
mLastAlpha = -1;
if (DEBUG_SURFACE_TRACE || DEBUG_ANIM)
Slog.v(TAG, "performShowLocked: mDrawState=HAS_DRAWN in " + this);
mDrawState = HAS_DRAWN;
mService.updateLayoutToAnimationLocked();
int i = mWin.mChildWindows.size();
while (i > 0) {
i--;
WindowState c = mWin.mChildWindows.get(i);
if (c.mAttachedHidden) {
c.mAttachedHidden = false;
if (c.mWinAnimator.mSurface != null) {
c.mWinAnimator.performShowLocked();
// It hadn't been shown, which means layout not
// performed on it, so now we want to make sure to
// do a layout. If called from within the transaction
// loop, this will cause it to restart with a new
// layout.
c.mDisplayContent.layoutNeeded = true;
}
}
}
if (mWin.mAttrs.type != TYPE_APPLICATION_STARTING
&& mWin.mAppToken != null) {
mWin.mAppToken.firstWindowDrawn = true;
if (mWin.mAppToken.startingData != null) {
if (WindowManagerService.DEBUG_STARTING_WINDOW ||
WindowManagerService.DEBUG_ANIM) Slog.v(TAG,
"Finish starting " + mWin.mToken
+ ": first real window is shown, no animation");
// If this initial window is animating, stop it -- we
// will do an animation to reveal it from behind the
// starting window, so there is no need for it to also
// be doing its own stuff.
clearAnimation();
mService.mFinishedStarting.add(mWin.mAppToken);
mService.mH.sendEmptyMessage(H.FINISHED_STARTING);
}
mWin.mAppToken.updateReportedVisibilityLocked();
}
return true;
}
return false;
}
/**
* Have the surface flinger show a surface, robustly dealing with
* error conditions. In particular, if there is not enough memory
* to show the surface, then we will try to get rid of other surfaces
* in order to succeed.
*
* @return Returns true if the surface was successfully shown.
*/
boolean showSurfaceRobustlyLocked() {
try {
if (mSurface != null) {
mSurfaceShown = true;
mSurface.show();
if (mWin.mTurnOnScreen) {
if (DEBUG_VISIBILITY) Slog.v(TAG,
"Show surface turning screen on: " + mWin);
mWin.mTurnOnScreen = false;
mAnimator.mBulkUpdateParams |= SET_TURN_ON_SCREEN;
}
}
return true;
} catch (RuntimeException e) {
Slog.w(TAG, "Failure showing surface " + mSurface + " in " + mWin, e);
}
mService.reclaimSomeSurfaceMemoryLocked(this, "show", true);
return false;
}
void applyEnterAnimationLocked() {
final int transit;
if (mEnterAnimationPending) {
mEnterAnimationPending = false;
transit = WindowManagerPolicy.TRANSIT_ENTER;
} else {
transit = WindowManagerPolicy.TRANSIT_SHOW;
}
applyAnimationLocked(transit, true);
mService.scheduleNotifyWindowTranstionIfNeededLocked(mWin, transit);
}
// TODO(cmautner): Move back to WindowState?
/**
* Choose the correct animation and set it to the passed WindowState.
* @param transit If WindowManagerPolicy.TRANSIT_PREVIEW_DONE and the app window has been drawn
* then the animation will be app_starting_exit. Any other value loads the animation from
* the switch statement below.
* @param isEntrance The animation type the last time this was called. Used to keep from
* loading the same animation twice.
* @return true if an animation has been loaded.
*/
boolean applyAnimationLocked(int transit, boolean isEntrance) {
if (mLocalAnimating && mAnimationIsEntrance == isEntrance) {
// If we are trying to apply an animation, but already running
// an animation of the same type, then just leave that one alone.
return true;
}
// Only apply an animation if the display isn't frozen. If it is
// frozen, there is no reason to animate and it can cause strange
// artifacts when we unfreeze the display if some different animation
// is running.
if (mService.okToDisplay()) {
int anim = mPolicy.selectAnimationLw(mWin, transit);
int attr = -1;
Animation a = null;
if (anim != 0) {
a = AnimationUtils.loadAnimation(mContext, anim);
} else {
switch (transit) {
case WindowManagerPolicy.TRANSIT_ENTER:
attr = com.android.internal.R.styleable.WindowAnimation_windowEnterAnimation;
break;
case WindowManagerPolicy.TRANSIT_EXIT:
attr = com.android.internal.R.styleable.WindowAnimation_windowExitAnimation;
break;
case WindowManagerPolicy.TRANSIT_SHOW:
attr = com.android.internal.R.styleable.WindowAnimation_windowShowAnimation;
break;
case WindowManagerPolicy.TRANSIT_HIDE:
attr = com.android.internal.R.styleable.WindowAnimation_windowHideAnimation;
break;
}
if (attr >= 0) {
a = mService.loadAnimation(mWin.mAttrs, attr);
}
}
if (WindowManagerService.DEBUG_ANIM) Slog.v(TAG,
"applyAnimation: win=" + this
+ " anim=" + anim + " attr=0x" + Integer.toHexString(attr)
+ " a=" + a
+ " transit=" + transit
+ " isEntrance=" + isEntrance + " Callers " + Debug.getCallers(3));
if (a != null) {
if (WindowManagerService.DEBUG_ANIM) {
RuntimeException e = null;
if (!WindowManagerService.HIDE_STACK_CRAWLS) {
e = new RuntimeException();
e.fillInStackTrace();
}
Slog.v(TAG, "Loaded animation " + a + " for " + this, e);
}
setAnimation(a);
mAnimationIsEntrance = isEntrance;
}
} else {
clearAnimation();
}
return mAnimation != null;
}
public void dump(PrintWriter pw, String prefix, boolean dumpAll) {
if (mAnimating || mLocalAnimating || mAnimationIsEntrance
|| mAnimation != null) {
pw.print(prefix); pw.print("mAnimating="); pw.print(mAnimating);
pw.print(" mLocalAnimating="); pw.print(mLocalAnimating);
pw.print(" mAnimationIsEntrance="); pw.print(mAnimationIsEntrance);
pw.print(" mAnimation="); pw.println(mAnimation);
}
if (mHasTransformation || mHasLocalTransformation) {
pw.print(prefix); pw.print("XForm: has=");
pw.print(mHasTransformation);
pw.print(" hasLocal="); pw.print(mHasLocalTransformation);
pw.print(" "); mTransformation.printShortString(pw);
pw.println();
}
if (mSurface != null) {
if (dumpAll) {
pw.print(prefix); pw.print("mSurface="); pw.println(mSurface);
pw.print(prefix); pw.print("mDrawState=");
pw.print(drawStateToString(mDrawState));
pw.print(" mLastHidden="); pw.println(mLastHidden);
}
pw.print(prefix); pw.print("Surface: shown="); pw.print(mSurfaceShown);
pw.print(" layer="); pw.print(mSurfaceLayer);
pw.print(" alpha="); pw.print(mSurfaceAlpha);
pw.print(" rect=("); pw.print(mSurfaceX);
pw.print(","); pw.print(mSurfaceY);
pw.print(") "); pw.print(mSurfaceW);
pw.print(" x "); pw.println(mSurfaceH);
}
if (mPendingDestroySurface != null) {
pw.print(prefix); pw.print("mPendingDestroySurface=");
pw.println(mPendingDestroySurface);
}
if (mSurfaceResized || mSurfaceDestroyDeferred) {
pw.print(prefix); pw.print("mSurfaceResized="); pw.print(mSurfaceResized);
pw.print(" mSurfaceDestroyDeferred="); pw.println(mSurfaceDestroyDeferred);
}
if (mWin.mAttrs.type == WindowManager.LayoutParams.TYPE_UNIVERSE_BACKGROUND) {
pw.print(prefix); pw.print("mUniverseTransform=");
mUniverseTransform.printShortString(pw);
pw.println();
}
if (mShownAlpha != 1 || mAlpha != 1 || mLastAlpha != 1) {
pw.print(prefix); pw.print("mShownAlpha="); pw.print(mShownAlpha);
pw.print(" mAlpha="); pw.print(mAlpha);
pw.print(" mLastAlpha="); pw.println(mLastAlpha);
}
if (mHaveMatrix || mWin.mGlobalScale != 1) {
pw.print(prefix); pw.print("mGlobalScale="); pw.print(mWin.mGlobalScale);
pw.print(" mDsDx="); pw.print(mDsDx);
pw.print(" mDtDx="); pw.print(mDtDx);
pw.print(" mDsDy="); pw.print(mDsDy);
pw.print(" mDtDy="); pw.println(mDtDy);
}
}
@Override
public String toString() {
StringBuffer sb = new StringBuffer("WindowStateAnimator (");
sb.append(mWin.mLastTitle + "): ");
sb.append("mSurface " + mSurface);
sb.append(", mAnimation " + mAnimation);
return sb.toString();
}
}
|