Skip to content

Commit 60ce6d4

Browse files
Craig MautnerAndroid (Google) Code Review
authored andcommitted
Merge "Call Surface.destroy when Display is removed." into jb-mr1-dev
2 parents 0e3984b + d5523dc commit 60ce6d4

File tree

5 files changed

+129
-66
lines changed

5 files changed

+129
-66
lines changed

services/java/com/android/server/display/OverlayDisplayAdapter.java

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,11 @@
1919
import com.android.internal.util.DumpUtils;
2020
import com.android.internal.util.IndentingPrintWriter;
2121

22-
import android.content.BroadcastReceiver;
2322
import android.content.Context;
24-
import android.content.Intent;
25-
import android.content.IntentFilter;
2623
import android.database.ContentObserver;
24+
import android.graphics.SurfaceTexture;
2725
import android.os.Handler;
2826
import android.os.IBinder;
29-
import android.os.UserHandle;
3027
import android.provider.Settings;
3128
import android.util.DisplayMetrics;
3229
import android.util.Slog;
@@ -192,28 +189,42 @@ private final class OverlayDisplayDevice extends DisplayDevice {
192189
private final int mDensityDpi;
193190

194191
private Surface mSurface;
192+
private SurfaceTexture mSurfaceTexture;
195193
private DisplayDeviceInfo mInfo;
196194

197195
public OverlayDisplayDevice(IBinder displayToken, String name,
198196
int width, int height, float refreshRate, int densityDpi,
199-
Surface surface) {
197+
SurfaceTexture surfaceTexture) {
200198
super(OverlayDisplayAdapter.this, displayToken);
201199
mName = name;
202200
mWidth = width;
203201
mHeight = height;
204202
mRefreshRate = refreshRate;
205203
mDensityDpi = densityDpi;
206-
mSurface = surface;
204+
mSurfaceTexture = surfaceTexture;
207205
}
208206

209-
public void clearSurfaceLocked() {
210-
mSurface = null;
207+
public void clearSurfaceTextureLocked() {
208+
if (mSurfaceTexture != null) {
209+
mSurfaceTexture = null;
210+
}
211211
sendTraversalRequestLocked();
212212
}
213213

214214
@Override
215215
public void performTraversalInTransactionLocked() {
216-
setSurfaceInTransactionLocked(mSurface);
216+
if (mSurfaceTexture != null) {
217+
if (mSurface == null) {
218+
mSurface = new Surface(mSurfaceTexture);
219+
}
220+
setSurfaceInTransactionLocked(mSurface);
221+
} else {
222+
setSurfaceInTransactionLocked(null);
223+
if (mSurface != null) {
224+
mSurface.destroy();
225+
mSurface = null;
226+
}
227+
}
217228
}
218229

219230
@Override
@@ -268,11 +279,11 @@ public void dismissLocked() {
268279

269280
// Called on the UI thread.
270281
@Override
271-
public void onWindowCreated(Surface surface, float refreshRate) {
282+
public void onWindowCreated(SurfaceTexture surfaceTexture, float refreshRate) {
272283
synchronized (getSyncRoot()) {
273284
IBinder displayToken = Surface.createDisplay(mName);
274285
mDevice = new OverlayDisplayDevice(displayToken, mName,
275-
mWidth, mHeight, refreshRate, mDensityDpi, surface);
286+
mWidth, mHeight, refreshRate, mDensityDpi, surfaceTexture);
276287

277288
sendDisplayDeviceEventLocked(mDevice, DISPLAY_DEVICE_EVENT_ADDED);
278289
}
@@ -283,7 +294,7 @@ public void onWindowCreated(Surface surface, float refreshRate) {
283294
public void onWindowDestroyed() {
284295
synchronized (getSyncRoot()) {
285296
if (mDevice != null) {
286-
mDevice.clearSurfaceLocked();
297+
mDevice.clearSurfaceTextureLocked();
287298
sendDisplayDeviceEventLocked(mDevice, DISPLAY_DEVICE_EVENT_REMOVED);
288299
}
289300
}

services/java/com/android/server/display/OverlayDisplayWindow.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
import android.view.LayoutInflater;
3030
import android.view.MotionEvent;
3131
import android.view.ScaleGestureDetector;
32-
import android.view.Surface;
3332
import android.view.TextureView;
3433
import android.view.View;
3534
import android.view.WindowManager;
@@ -146,6 +145,7 @@ public void relayout() {
146145
}
147146
}
148147

148+
@Override
149149
public void dump(PrintWriter pw) {
150150
pw.println("mWindowVisible=" + mWindowVisible);
151151
pw.println("mWindowX=" + mWindowX);
@@ -291,8 +291,7 @@ public void onDisplayRemoved(int displayId) {
291291
@Override
292292
public void onSurfaceTextureAvailable(SurfaceTexture surfaceTexture,
293293
int width, int height) {
294-
mListener.onWindowCreated(new Surface(surfaceTexture),
295-
mDefaultDisplayInfo.refreshRate);
294+
mListener.onWindowCreated(surfaceTexture, mDefaultDisplayInfo.refreshRate);
296295
}
297296

298297
@Override
@@ -361,7 +360,7 @@ public boolean onScale(ScaleGestureDetector detector) {
361360
* Watches for significant changes in the overlay display window lifecycle.
362361
*/
363362
public interface Listener {
364-
public void onWindowCreated(Surface surface, float refreshRate);
363+
public void onWindowCreated(SurfaceTexture surfaceTexture, float refreshRate);
365364
public void onWindowDestroyed();
366365
}
367366
}

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

Lines changed: 38 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -30,37 +30,37 @@
3030
* all state used for dim animation.
3131
*/
3232
class DimAnimator {
33+
static final String TAG = "DimAnimator";
34+
3335
Surface mDimSurface;
3436
boolean mDimShown = false;
3537
float mDimCurrentAlpha;
3638
float mDimTargetAlpha;
3739
float mDimDeltaPerMs;
3840
long mLastDimAnimTime;
39-
41+
4042
int mLastDimWidth, mLastDimHeight;
4143

4244
DimAnimator (SurfaceSession session, final int layerStack) {
43-
if (mDimSurface == null) {
44-
try {
45-
if (WindowManagerService.DEBUG_SURFACE_TRACE) {
46-
mDimSurface = new WindowStateAnimator.SurfaceTrace(session,
47-
"DimAnimator",
48-
16, 16, PixelFormat.OPAQUE,
49-
Surface.FX_SURFACE_DIM | Surface.HIDDEN);
50-
} else {
51-
mDimSurface = new Surface(session, "DimAnimator",
52-
16, 16, PixelFormat.OPAQUE,
53-
Surface.FX_SURFACE_DIM | Surface.HIDDEN);
54-
}
55-
if (WindowManagerService.SHOW_TRANSACTIONS ||
56-
WindowManagerService.SHOW_SURFACE_ALLOC) Slog.i(WindowManagerService.TAG,
57-
" DIM " + mDimSurface + ": CREATE");
58-
mDimSurface.setLayerStack(layerStack);
59-
mDimSurface.setAlpha(0.0f);
60-
mDimSurface.show();
61-
} catch (Exception e) {
62-
Slog.e(WindowManagerService.TAG, "Exception creating Dim surface", e);
45+
try {
46+
if (WindowManagerService.DEBUG_SURFACE_TRACE) {
47+
mDimSurface = new WindowStateAnimator.SurfaceTrace(session,
48+
"DimAnimator",
49+
16, 16, PixelFormat.OPAQUE,
50+
Surface.FX_SURFACE_DIM | Surface.HIDDEN);
51+
} else {
52+
mDimSurface = new Surface(session, "DimAnimator",
53+
16, 16, PixelFormat.OPAQUE,
54+
Surface.FX_SURFACE_DIM | Surface.HIDDEN);
6355
}
56+
if (WindowManagerService.SHOW_TRANSACTIONS ||
57+
WindowManagerService.SHOW_SURFACE_ALLOC) Slog.i(WindowManagerService.TAG,
58+
" DIM " + mDimSurface + ": CREATE");
59+
mDimSurface.setLayerStack(layerStack);
60+
mDimSurface.setAlpha(0.0f);
61+
mDimSurface.show();
62+
} catch (Exception e) {
63+
Slog.e(WindowManagerService.TAG, "Exception creating Dim surface", e);
6464
}
6565
}
6666

@@ -69,6 +69,11 @@ class DimAnimator {
6969
* {@link #updateSurface} after all windows are examined.
7070
*/
7171
void updateParameters(final Resources res, final Parameters params, final long currentTime) {
72+
if (mDimSurface == null) {
73+
Slog.e(TAG, "updateParameters: no Surface");
74+
return;
75+
}
76+
7277
// Multiply by 1.5 so that rotating a frozen surface that includes this does not expose a
7378
// corner.
7479
final int dw = (int) (params.mDimWidth * 1.5);
@@ -133,6 +138,11 @@ void updateParameters(final Resources res, final Parameters params, final long c
133138
* false when the animation is finished and the dim surface is hidden.
134139
*/
135140
boolean updateSurface(boolean dimming, long currentTime, boolean displayFrozen) {
141+
if (mDimSurface == null) {
142+
Slog.e(TAG, "updateSurface: no Surface");
143+
return false;
144+
}
145+
136146
if (!dimming) {
137147
if (mDimTargetAlpha != 0) {
138148
mLastDimAnimTime = currentTime;
@@ -187,6 +197,13 @@ boolean updateSurface(boolean dimming, long currentTime, boolean displayFrozen)
187197
return animating;
188198
}
189199

200+
public void kill() {
201+
if (mDimSurface != null) {
202+
mDimSurface.destroy();
203+
mDimSurface = null;
204+
}
205+
}
206+
190207
public void printTo(String prefix, PrintWriter pw) {
191208
pw.print(prefix);
192209
pw.print("mDimSurface="); pw.print(mDimSurface);

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

Lines changed: 37 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -24,41 +24,46 @@
2424
import java.io.PrintWriter;
2525

2626
class DimSurface {
27+
static final String TAG = "DimSurface";
28+
2729
Surface mDimSurface;
2830
boolean mDimShown = false;
2931
int mDimColor = 0;
3032
int mLayer = -1;
3133
int mLastDimWidth, mLastDimHeight;
3234

3335
DimSurface(SurfaceSession session, final int layerStack) {
34-
if (mDimSurface == null) {
35-
try {
36-
if (WindowManagerService.DEBUG_SURFACE_TRACE) {
37-
mDimSurface = new WindowStateAnimator.SurfaceTrace(session,
38-
"DimSurface",
39-
16, 16, PixelFormat.OPAQUE,
40-
Surface.FX_SURFACE_DIM | Surface.HIDDEN);
41-
} else {
42-
mDimSurface = new Surface(session, "DimSurface",
43-
16, 16, PixelFormat.OPAQUE,
44-
Surface.FX_SURFACE_DIM | Surface.HIDDEN);
45-
}
46-
if (WindowManagerService.SHOW_TRANSACTIONS ||
47-
WindowManagerService.SHOW_SURFACE_ALLOC) Slog.i(WindowManagerService.TAG,
48-
" DIM " + mDimSurface + ": CREATE");
49-
mDimSurface.setLayerStack(layerStack);
50-
mDimSurface.setAlpha(0.0f);
51-
mDimSurface.show();
52-
} catch (Exception e) {
53-
Slog.e(WindowManagerService.TAG, "Exception creating Dim surface", e);
36+
try {
37+
if (WindowManagerService.DEBUG_SURFACE_TRACE) {
38+
mDimSurface = new WindowStateAnimator.SurfaceTrace(session,
39+
"DimSurface",
40+
16, 16, PixelFormat.OPAQUE,
41+
Surface.FX_SURFACE_DIM | Surface.HIDDEN);
42+
} else {
43+
mDimSurface = new Surface(session, "DimSurface",
44+
16, 16, PixelFormat.OPAQUE,
45+
Surface.FX_SURFACE_DIM | Surface.HIDDEN);
5446
}
47+
if (WindowManagerService.SHOW_TRANSACTIONS ||
48+
WindowManagerService.SHOW_SURFACE_ALLOC) Slog.i(WindowManagerService.TAG,
49+
" DIM " + mDimSurface + ": CREATE");
50+
mDimSurface.setLayerStack(layerStack);
51+
mDimSurface.setAlpha(0.0f);
52+
mDimSurface.show();
53+
} catch (Exception e) {
54+
Slog.e(WindowManagerService.TAG, "Exception creating Dim surface", e);
5555
}
5656
}
5757

5858
/**
5959
* Show the dim surface.
6060
*/
6161
void show(int dw, int dh, int layer, int color) {
62+
if (mDimSurface == null) {
63+
Slog.e(TAG, "show: no Surface");
64+
return;
65+
}
66+
6267
if (!mDimShown) {
6368
if (WindowManagerService.SHOW_TRANSACTIONS) Slog.i(WindowManagerService.TAG, " DIM " + mDimSurface + ": SHOW pos=(0,0) (" +
6469
dw + "x" + dh + " layer=" + layer + ")");
@@ -88,6 +93,11 @@ void show(int dw, int dh, int layer, int color) {
8893
}
8994

9095
void hide() {
96+
if (mDimSurface == null) {
97+
Slog.e(TAG, "hide: no Surface");
98+
return;
99+
}
100+
91101
if (mDimShown) {
92102
mDimShown = false;
93103
try {
@@ -99,6 +109,13 @@ void hide() {
99109
}
100110
}
101111

112+
void kill() {
113+
if (mDimSurface != null) {
114+
mDimSurface.destroy();
115+
mDimSurface = null;
116+
}
117+
}
118+
102119
public void printTo(String prefix, PrintWriter pw) {
103120
pw.print(prefix); pw.print("mDimSurface="); pw.println(mDimSurface);
104121
pw.print(prefix); pw.print("mDimShown="); pw.print(mDimShown);

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

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,22 @@ void addDisplayLocked(final int displayId) {
147147
}
148148

149149
void removeDisplayLocked(final int displayId) {
150+
final DisplayContentsAnimator displayAnimator = mDisplayContentsAnimators.get(displayId);
151+
if (displayAnimator != null) {
152+
if (displayAnimator.mWindowAnimationBackgroundSurface != null) {
153+
displayAnimator.mWindowAnimationBackgroundSurface.kill();
154+
displayAnimator.mWindowAnimationBackgroundSurface = null;
155+
}
156+
if (displayAnimator.mScreenRotationAnimation != null) {
157+
displayAnimator.mScreenRotationAnimation.kill();
158+
displayAnimator.mScreenRotationAnimation = null;
159+
}
160+
if (displayAnimator.mDimAnimator != null) {
161+
displayAnimator.mDimAnimator.kill();
162+
displayAnimator.mDimAnimator = null;
163+
}
164+
}
165+
150166
mDisplayContentsAnimators.delete(displayId);
151167
}
152168

@@ -527,11 +543,15 @@ private void updateWallpaperLocked(int displayId) {
527543
}
528544
}
529545

530-
windowAnimationBackgroundSurface.show(mDw, mDh,
531-
animLayer - WindowManagerService.LAYER_OFFSET_DIM,
532-
windowAnimationBackgroundColor);
546+
if (windowAnimationBackgroundSurface != null) {
547+
windowAnimationBackgroundSurface.show(mDw, mDh,
548+
animLayer - WindowManagerService.LAYER_OFFSET_DIM,
549+
windowAnimationBackgroundColor);
550+
}
533551
} else {
534-
windowAnimationBackgroundSurface.hide();
552+
if (windowAnimationBackgroundSurface != null) {
553+
windowAnimationBackgroundSurface.hide();
554+
}
535555
}
536556
}
537557

@@ -643,9 +663,8 @@ private void animateLocked() {
643663

644664
final DimAnimator.Parameters dimParams = displayAnimator.mDimParams;
645665
final DimAnimator dimAnimator = displayAnimator.mDimAnimator;
646-
if (dimParams != null) {
647-
dimAnimator.updateParameters(
648-
mContext.getResources(), dimParams, mCurrentTime);
666+
if (dimAnimator != null && dimParams != null) {
667+
dimAnimator.updateParameters(mContext.getResources(), dimParams, mCurrentTime);
649668
}
650669
if (dimAnimator != null && dimAnimator.mDimShown) {
651670
mAnimating |= dimAnimator.updateSurface(isDimmingLocked(displayId),
@@ -801,9 +820,9 @@ ScreenRotationAnimation getScreenRotationAnimationLocked(int displayId) {
801820

802821
private class DisplayContentsAnimator {
803822
WinAnimatorList mWinAnimators = new WinAnimatorList();
804-
final DimAnimator mDimAnimator;
823+
DimAnimator mDimAnimator = null;
805824
DimAnimator.Parameters mDimParams = null;
806-
final DimSurface mWindowAnimationBackgroundSurface;
825+
DimSurface mWindowAnimationBackgroundSurface = null;
807826
ScreenRotationAnimation mScreenRotationAnimation = null;
808827

809828
public DisplayContentsAnimator(int displayId) {

0 commit comments

Comments
 (0)