Skip to content

Commit 7358fbf

Browse files
author
Craig Mautner
committed
Minor cleanups.
- Replace HashSet with ArrayList. - Check for Watermark and SurfaceSession initialization once, not every time through layout. - Move watermark rendering into animation. - Add surface operation debugging. Change-Id: I4b7e7c0b8d89d43c67a42753832f90b8632d4f5d
1 parent f804ba1 commit 7358fbf

File tree

3 files changed

+146
-45
lines changed

3 files changed

+146
-45
lines changed

services/java/com/android/server/wm/WindowAnimator.java

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323

2424
import java.io.PrintWriter;
2525
import java.util.ArrayList;
26-
import java.util.HashSet;
2726

2827
/**
2928
* Singleton class that carries out the animations and Surface operations in a separate task
@@ -36,8 +35,7 @@ public class WindowAnimator {
3635
final Context mContext;
3736
final WindowManagerPolicy mPolicy;
3837

39-
HashSet<WindowStateAnimator> mWinAnimators = new HashSet<WindowStateAnimator>();
40-
HashSet<WindowStateAnimator> mFinished = new HashSet<WindowStateAnimator>();
38+
ArrayList<WindowStateAnimator> mWinAnimators = new ArrayList<WindowStateAnimator>();
4139

4240
boolean mAnimating;
4341
boolean mTokenMayBeDrawn;
@@ -449,16 +447,9 @@ void animate() {
449447
mScreenRotationAnimation.updateSurfaces();
450448
}
451449

452-
mFinished.clear();
453-
for (final WindowStateAnimator winAnimator : mWinAnimators) {
454-
if (winAnimator.mSurface == null) {
455-
mFinished.add(winAnimator);
456-
} else {
457-
winAnimator.prepareSurfaceLocked(true);
458-
}
459-
}
460-
for (final WindowStateAnimator winAnimator : mFinished) {
461-
mWinAnimators.remove(winAnimator);
450+
final int N = mWinAnimators.size();
451+
for (int i = 0; i < N; i++) {
452+
mWinAnimators.get(i).prepareSurfaceLocked(true);
462453
}
463454

464455
if (mDimParams != null) {
@@ -477,6 +468,10 @@ void animate() {
477468
mService.mBlackFrame.clearMatrix();
478469
}
479470
}
471+
472+
if (mService.mWatermark != null) {
473+
mService.mWatermark.drawIfNeeded();
474+
}
480475
} catch (RuntimeException e) {
481476
Log.wtf(TAG, "Unhandled exception in Window Manager", e);
482477
} finally {

services/java/com/android/server/wm/WindowManagerService.java

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@ public class WindowManagerService extends IWindowManager.Stub
175175
static final boolean DEBUG_SCREENSHOT = false;
176176
static final boolean DEBUG_BOOT = false;
177177
static final boolean DEBUG_LAYOUT_REPEATS = true;
178+
static final boolean DEBUG_SURFACE_TRACE = false;
178179
static final boolean SHOW_SURFACE_ALLOC = false;
179180
static final boolean SHOW_TRANSACTIONS = false;
180181
static final boolean SHOW_LIGHT_TRANSACTIONS = false || SHOW_TRANSACTIONS;
@@ -425,7 +426,7 @@ public void onReceive(Context context, Intent intent) {
425426

426427
IInputMethodManager mInputMethodManager;
427428

428-
SurfaceSession mFxSession;
429+
final SurfaceSession mFxSession;
429430
Watermark mWatermark;
430431
StrictModeFlash mStrictModeFlash;
431432

@@ -862,6 +863,11 @@ private WindowManagerService(Context context, PowerManagerService pm,
862863

863864
// Add ourself to the Watchdog monitors.
864865
Watchdog.getInstance().addMonitor(this);
866+
mFxSession = new SurfaceSession();
867+
868+
Surface.openTransaction();
869+
createWatermark();
870+
Surface.closeTransaction();
865871
}
866872

867873
public InputManagerService getInputManagerService() {
@@ -7592,7 +7598,8 @@ public int handleAppTransitionReadyLocked() {
75927598
if (DEBUG_APP_TRANSITIONS) Slog.v(TAG,
75937599
"Check opening app" + wtoken + ": allDrawn="
75947600
+ wtoken.allDrawn + " startingDisplayed="
7595-
+ wtoken.startingDisplayed);
7601+
+ wtoken.startingDisplayed + " startingMoved="
7602+
+ wtoken.startingMoved);
75967603
if (!wtoken.allDrawn && !wtoken.startingDisplayed
75977604
&& !wtoken.startingMoved) {
75987605
goodToGo = false;
@@ -8058,23 +8065,13 @@ private final void performLayoutAndPlaceSurfacesLockedInner(
80588065
mInnerFields.mHoldScreen = null;
80598066
mInnerFields.mScreenBrightness = -1;
80608067
mInnerFields.mButtonBrightness = -1;
8061-
boolean focusDisplayed = false;
80628068
mAnimator.mAnimating = false;
8063-
boolean createWatermark = false;
8064-
8065-
if (mFxSession == null) {
8066-
mFxSession = new SurfaceSession();
8067-
createWatermark = true;
8068-
}
80698069

80708070
if (SHOW_LIGHT_TRANSACTIONS) Slog.i(TAG,
80718071
">>> OPEN TRANSACTION performLayoutAndPlaceSurfaces");
80728072

80738073
Surface.openTransaction();
80748074

8075-
if (createWatermark) {
8076-
createWatermark();
8077-
}
80788075
if (mWatermark != null) {
80798076
mWatermark.positionSurface(dw, dh);
80808077
}
@@ -8145,6 +8142,7 @@ private final void performLayoutAndPlaceSurfacesLockedInner(
81458142
mInnerFields.mDimming = false;
81468143
mInnerFields.mSyswin = false;
81478144

8145+
boolean focusDisplayed = false;
81488146
final int N = mWindows.size();
81498147
for (i=N-1; i>=0; i--) {
81508148
WindowState w = mWindows.get(i);
@@ -8168,6 +8166,10 @@ private final void performLayoutAndPlaceSurfacesLockedInner(
81688166
updateWallpaperVisibilityLocked();
81698167
}
81708168
}
8169+
if (focusDisplayed) {
8170+
mH.sendEmptyMessage(H.REPORT_LOSING_FOCUS);
8171+
}
8172+
81718173
if (!mInnerFields.mDimming && mAnimator.isDimming()) {
81728174
mAnimator.stopDimming();
81738175
}
@@ -8294,15 +8296,18 @@ private final void performLayoutAndPlaceSurfacesLockedInner(
82948296

82958297
// Update animations of all applications, including those
82968298
// associated with exiting/removed apps
8297-
mAnimator.animate();
8298-
mPendingLayoutChanges |= mAnimator.mPendingLayoutChanges;
8299-
if (DEBUG_LAYOUT_REPEATS) debugLayoutRepeats("after animate()", mPendingLayoutChanges);
8300-
8301-
if (SHOW_LIGHT_TRANSACTIONS) Slog.i(TAG,
8302-
"<<< CLOSE TRANSACTION performLayoutAndPlaceSurfaces");
8303-
8304-
if (mWatermark != null) {
8305-
mWatermark.drawIfNeeded();
8299+
synchronized (mAnimator) {
8300+
final ArrayList<WindowStateAnimator> winAnimators = mAnimator.mWinAnimators;
8301+
winAnimators.clear();
8302+
for (i = 0; i < N; i++) {
8303+
final WindowStateAnimator winAnimator = mWindows.get(i).mWinAnimator;
8304+
if (winAnimator.mSurface != null) {
8305+
winAnimators.add(winAnimator);
8306+
}
8307+
}
8308+
mAnimator.animate();
8309+
mPendingLayoutChanges |= mAnimator.mPendingLayoutChanges;
8310+
if (DEBUG_LAYOUT_REPEATS) debugLayoutRepeats("after animate()", mPendingLayoutChanges);
83068311
}
83078312

83088313
if (DEBUG_ORIENTATION && mDisplayFrozen) Slog.v(TAG,
@@ -8415,9 +8420,6 @@ private final void performLayoutAndPlaceSurfacesLockedInner(
84158420
mToBottomApps.clear();
84168421
}
84178422

8418-
if (focusDisplayed) {
8419-
mH.sendEmptyMessage(H.REPORT_LOSING_FOCUS);
8420-
}
84218423
if (wallpaperDestroyed) {
84228424
mLayoutNeeded |= adjustWallpaperWindowsLocked() != 0;
84238425
}
@@ -8588,7 +8590,6 @@ boolean reclaimSomeSurfaceMemoryLocked(WindowStateAnimator winAnimator, String o
85888590
wsa.mSurfaceShown = false;
85898591
wsa.mSurface = null;
85908592
ws.mHasSurface = false;
8591-
mAnimator.mWinAnimators.remove(wsa);
85928593
mForceRemoves.add(ws);
85938594
i--;
85948595
N--;
@@ -8602,7 +8603,6 @@ boolean reclaimSomeSurfaceMemoryLocked(WindowStateAnimator winAnimator, String o
86028603
wsa.mSurfaceShown = false;
86038604
wsa.mSurface = null;
86048605
ws.mHasSurface = false;
8605-
mAnimator.mWinAnimators.remove(wsa);
86068606
leakedSurface = true;
86078607
}
86088608
}
@@ -8642,7 +8642,6 @@ boolean reclaimSomeSurfaceMemoryLocked(WindowStateAnimator winAnimator, String o
86428642
winAnimator.mSurfaceShown = false;
86438643
winAnimator.mSurface = null;
86448644
winAnimator.mWin.mHasSurface = false;
8645-
mAnimator.mWinAnimators.remove(winAnimator);
86468645
}
86478646

86488647
try {

services/java/com/android/server/wm/WindowStateAnimator.java

Lines changed: 112 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,14 @@
1010
import android.content.Context;
1111
import android.graphics.Matrix;
1212
import android.graphics.PixelFormat;
13+
import android.graphics.Point;
14+
import android.graphics.PointF;
1315
import android.graphics.Rect;
1416
import android.graphics.Region;
1517
import android.os.RemoteException;
1618
import android.util.Slog;
1719
import android.view.Surface;
20+
import android.view.SurfaceSession;
1821
import android.view.WindowManager;
1922
import android.view.WindowManagerPolicy;
2023
import android.view.WindowManager.LayoutParams;
@@ -25,6 +28,7 @@
2528
import com.android.server.wm.WindowManagerService.H;
2629

2730
import java.io.PrintWriter;
31+
import java.util.ArrayList;
2832

2933
/**
3034
* Keep track of animations and surface operations for a single WindowState.
@@ -39,6 +43,7 @@ class WindowStateAnimator {
3943
static final boolean SHOW_SURFACE_ALLOC = WindowManagerService.SHOW_SURFACE_ALLOC;
4044
static final boolean localLOGV = WindowManagerService.localLOGV;
4145
static final boolean DEBUG_ORIENTATION = WindowManagerService.DEBUG_ORIENTATION;
46+
static final boolean DEBUG_SURFACE_TRACE = WindowManagerService.DEBUG_SURFACE_TRACE;
4247

4348
static final String TAG = "WindowStateAnimator";
4449

@@ -398,6 +403,105 @@ boolean commitFinishDrawingLocked(long currentTime) {
398403
return true;
399404
}
400405

406+
static class MySurface extends Surface {
407+
final static ArrayList<MySurface> sSurfaces = new ArrayList<MySurface>();
408+
409+
private float mMySurfaceAlpha = 0xff;
410+
private int mLayer;
411+
private PointF mPosition = new PointF();
412+
private Point mSize = new Point();
413+
private boolean mShown = false;
414+
private String mName = "Not named";
415+
416+
public MySurface(SurfaceSession s,
417+
int pid, int display, int w, int h, int format, int flags) throws
418+
OutOfResourcesException {
419+
super(s, pid, display, w, h, format, flags);
420+
mSize = new Point(w, h);
421+
Slog.v("SurfaceTrace", "ctor: " + this);
422+
}
423+
424+
public MySurface(SurfaceSession s,
425+
int pid, String name, int display, int w, int h, int format, int flags)
426+
throws OutOfResourcesException {
427+
super(s, pid, name, display, w, h, format, flags);
428+
mName = name;
429+
mSize = new Point(w, h);
430+
Slog.v("SurfaceTrace", "ctor: " + this);
431+
}
432+
433+
@Override
434+
public void setAlpha(float alpha) {
435+
super.setAlpha(alpha);
436+
mMySurfaceAlpha = alpha;
437+
Slog.v("SurfaceTrace", "setAlpha: " + this);
438+
}
439+
440+
@Override
441+
public void setLayer(int zorder) {
442+
super.setLayer(zorder);
443+
mLayer = zorder;
444+
Slog.v("SurfaceTrace", "setLayer: " + this);
445+
446+
sSurfaces.remove(this);
447+
int i;
448+
for (i = sSurfaces.size() - 1; i >= 0; i--) {
449+
MySurface s = sSurfaces.get(i);
450+
if (s.mLayer < zorder) {
451+
break;
452+
}
453+
}
454+
sSurfaces.add(i + 1, this);
455+
}
456+
457+
@Override
458+
public void setPosition(float x, float y) {
459+
super.setPosition(x, y);
460+
mPosition = new PointF(x, y);
461+
}
462+
463+
@Override
464+
public void setSize(int w, int h) {
465+
super.setSize(w, h);
466+
mSize = new Point(w, h);
467+
}
468+
469+
@Override
470+
public void hide() {
471+
super.hide();
472+
mShown = false;
473+
Slog.v("SurfaceTrace", "hide: " + this);
474+
}
475+
@Override
476+
public void show() {
477+
super.show();
478+
mShown = true;
479+
Slog.v("SurfaceTrace", "show: " + this);
480+
}
481+
482+
@Override
483+
public void destroy() {
484+
super.destroy();
485+
Slog.v("SurfaceTrace", "destroy: " + this + ". Called by "
486+
+ WindowManagerService.getCaller());
487+
sSurfaces.remove(this);
488+
}
489+
490+
static void dumpAllSurfaces() {
491+
final int N = sSurfaces.size();
492+
for (int i = 0; i < N; i++) {
493+
Slog.i(TAG, "SurfaceDump: " + sSurfaces.get(i));
494+
}
495+
}
496+
497+
@Override
498+
public String toString() {
499+
return "Surface " + mName + ": shown=" + mShown + " layer=" + mLayer
500+
+ " alpha=" + mMySurfaceAlpha + " " + mPosition.x + "," + mPosition.y
501+
+ " " + mSize.x + "x" + mSize.y;
502+
}
503+
}
504+
401505
Surface createSurfaceLocked() {
402506
if (mSurface == null) {
403507
mReportDestroySurface = false;
@@ -452,12 +556,18 @@ Surface createSurfaceLocked() {
452556
if (!PixelFormat.formatHasAlpha(attrs.format)) {
453557
flags |= Surface.OPAQUE;
454558
}
455-
mSurface = new Surface(
559+
if (DEBUG_SURFACE_TRACE) {
560+
mSurface = new MySurface(
561+
mSession.mSurfaceSession, mSession.mPid,
562+
attrs.getTitle().toString(),
563+
0, w, h, format, flags);
564+
} else {
565+
mSurface = new Surface(
456566
mSession.mSurfaceSession, mSession.mPid,
457567
attrs.getTitle().toString(),
458568
0, w, h, format, flags);
569+
}
459570
mWin.mHasSurface = true;
460-
mAnimator.mWinAnimators.add(this);
461571
if (SHOW_TRANSACTIONS || SHOW_SURFACE_ALLOC) Slog.i(TAG,
462572
" CREATE SURFACE "
463573
+ mSurface + " IN SESSION "
@@ -468,14 +578,12 @@ Surface createSurfaceLocked() {
468578
+ " / " + this);
469579
} catch (Surface.OutOfResourcesException e) {
470580
mWin.mHasSurface = false;
471-
mAnimator.mWinAnimators.remove(this);
472581
Slog.w(TAG, "OutOfResourcesException creating surface");
473582
mService.reclaimSomeSurfaceMemoryLocked(this, "create", true);
474583
mDrawState = NO_SURFACE;
475584
return null;
476585
} catch (Exception e) {
477586
mWin.mHasSurface = false;
478-
mAnimator.mWinAnimators.remove(this);
479587
Slog.e(TAG, "Exception creating surface", e);
480588
mDrawState = NO_SURFACE;
481589
return null;
@@ -593,7 +701,6 @@ void destroySurfaceLocked() {
593701
mSurfaceShown = false;
594702
mSurface = null;
595703
mWin.mHasSurface =false;
596-
mAnimator.mWinAnimators.remove(this);
597704
}
598705
}
599706

0 commit comments

Comments
 (0)