blob: 2e1e3f763a158d03bc7d60912874fb6a7ae0db16 [file] [log] [blame]
Robert Carre6a83512015-11-03 16:09:21 -08001/*
2 * Copyright (C) 2015 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.wm;
18
Wale Ogunwale4958ad22017-06-22 09:08:14 -070019import static android.os.Trace.TRACE_TAG_WINDOW_MANAGER;
Steven Timotiusaf03df62017-07-18 16:56:43 -070020import static android.view.Surface.SCALING_MODE_SCALE_TO_WINDOW;
21
Wale Ogunwale722ff892016-02-18 13:37:55 -080022import static com.android.server.wm.WindowManagerDebugConfig.SHOW_SURFACE_ALLOC;
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -080023import static com.android.server.wm.WindowManagerDebugConfig.SHOW_TRANSACTIONS;
24import static com.android.server.wm.WindowManagerDebugConfig.SHOW_LIGHT_TRANSACTIONS;
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -080025import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_SURFACE_TRACE;
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -080026import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_VISIBILITY;
27import static com.android.server.wm.WindowManagerDebugConfig.TAG_WITH_CLASS_NAME;
28import static com.android.server.wm.WindowManagerDebugConfig.TAG_WM;
Steven Timotiusaf03df62017-07-18 16:56:43 -070029import static com.android.server.wm.proto.WindowSurfaceControllerProto.LAYER;
30import static com.android.server.wm.proto.WindowSurfaceControllerProto.SHOWN;
Robert Carre6a83512015-11-03 16:09:21 -080031
Robert Carre6a83512015-11-03 16:09:21 -080032import android.graphics.Point;
33import android.graphics.PointF;
34import android.graphics.Rect;
35import android.graphics.Region;
36import android.os.IBinder;
37import android.os.Debug;
Wale Ogunwale4958ad22017-06-22 09:08:14 -070038import android.os.Trace;
Steven Timotiusaf03df62017-07-18 16:56:43 -070039import android.util.proto.ProtoOutputStream;
Robert Carre6a83512015-11-03 16:09:21 -080040import android.view.Surface;
41import android.view.SurfaceControl;
42import android.view.SurfaceSession;
43import android.view.WindowContentFrameStats;
44import android.view.Surface.OutOfResourcesException;
45
46import android.util.Slog;
47
Robert Carr3b716242016-08-16 16:02:21 -070048import java.io.FileDescriptor;
Robert Carre6a83512015-11-03 16:09:21 -080049import java.io.PrintWriter;
50import java.util.ArrayList;
51
52class WindowSurfaceController {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -080053 static final String TAG = TAG_WITH_CLASS_NAME ? "WindowSurfaceController" : TAG_WM;
Robert Carre6a83512015-11-03 16:09:21 -080054
55 final WindowStateAnimator mAnimator;
56
Andrii Kulian283acd22017-08-03 04:03:51 -070057 private SurfaceControlWithBackground mSurfaceControl;
Robert Carre6a83512015-11-03 16:09:21 -080058
Wale Ogunwale943002b2017-02-15 19:34:01 -080059 // Should only be set from within setShown().
Robert Carre6a83512015-11-03 16:09:21 -080060 private boolean mSurfaceShown = false;
61 private float mSurfaceX = 0;
62 private float mSurfaceY = 0;
63 private float mSurfaceW = 0;
64 private float mSurfaceH = 0;
65
Matthew Bouyack8dd88f62016-10-24 14:01:26 -070066 // Initialize to the identity matrix.
67 private float mLastDsdx = 1;
68 private float mLastDtdx = 0;
69 private float mLastDsdy = 0;
70 private float mLastDtdy = 1;
71
Robert Carre6a83512015-11-03 16:09:21 -080072 private float mSurfaceAlpha = 0;
73
74 private int mSurfaceLayer = 0;
Robert Carre6a83512015-11-03 16:09:21 -080075
76 // Surface flinger doesn't support crop rectangles where width or height is non-positive.
77 // However, we need to somehow handle the situation where the cropping would completely hide
78 // the window. We achieve this by explicitly hiding the surface and not letting it be shown.
79 private boolean mHiddenForCrop = false;
Jorim Jaggi1eb39b02016-02-25 13:36:14 -050080
81 // Initially a surface is hidden after just being created.
82 private boolean mHiddenForOtherReasons = true;
Robert Carre6a83512015-11-03 16:09:21 -080083 private final String title;
84
Robert Carr68e5c9e2016-09-14 10:50:09 -070085 private final WindowManagerService mService;
86
Wale Ogunwale943002b2017-02-15 19:34:01 -080087 private final int mWindowType;
88 private final Session mWindowSession;
89
Albert Chaulk3bf2e572016-11-22 13:59:19 -050090 public WindowSurfaceController(SurfaceSession s, String name, int w, int h, int format,
91 int flags, WindowStateAnimator animator, int windowType, int ownerUid) {
Robert Carre6a83512015-11-03 16:09:21 -080092 mAnimator = animator;
93
94 mSurfaceW = w;
95 mSurfaceH = h;
Robert Carre6a83512015-11-03 16:09:21 -080096
97 title = name;
98
Robert Carr68e5c9e2016-09-14 10:50:09 -070099 mService = animator.mService;
Wale Ogunwale943002b2017-02-15 19:34:01 -0800100 final WindowState win = animator.mWin;
101 mWindowType = windowType;
102 mWindowSession = win.mSession;
Robert Carr68e5c9e2016-09-14 10:50:09 -0700103
Andrii Kulian283acd22017-08-03 04:03:51 -0700104 Trace.traceBegin(TRACE_TAG_WINDOW_MANAGER, "new SurfaceControl");
105 mSurfaceControl = new SurfaceControlWithBackground(
106 s, name, w, h, format, flags, windowType, ownerUid, this);
107 Trace.traceEnd(TRACE_TAG_WINDOW_MANAGER);
Robert Carr3b716242016-08-16 16:02:21 -0700108
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700109 if (mService.mRoot.mSurfaceTraceEnabled) {
110 mSurfaceControl = new RemoteSurfaceTrace(
Wale Ogunwale943002b2017-02-15 19:34:01 -0800111 mService.mRoot.mSurfaceTraceFd.getFileDescriptor(), mSurfaceControl, win);
Robert Carr3b716242016-08-16 16:02:21 -0700112 }
113 }
114
115 void installRemoteTrace(FileDescriptor fd) {
116 mSurfaceControl = new RemoteSurfaceTrace(fd, mSurfaceControl, mAnimator.mWin);
117 }
118
119 void removeRemoteTrace() {
Andrii Kulian283acd22017-08-03 04:03:51 -0700120 mSurfaceControl = new SurfaceControlWithBackground(mSurfaceControl);
Robert Carre6a83512015-11-03 16:09:21 -0800121 }
122
123
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700124 private void logSurface(String msg, RuntimeException where) {
Robert Carre6a83512015-11-03 16:09:21 -0800125 String str = " SURFACE " + msg + ": " + title;
126 if (where != null) {
127 Slog.i(TAG, str, where);
128 } else {
129 Slog.i(TAG, str);
130 }
131 }
132
Robert Carrd5c7dd62017-03-08 10:39:30 -0800133 void reparentChildrenInTransaction(WindowSurfaceController other) {
134 if (SHOW_TRANSACTIONS) Slog.i(TAG, "REPARENT from: " + this + " to: " + other);
135 if ((mSurfaceControl != null) && (other.mSurfaceControl != null)) {
136 mSurfaceControl.reparentChildren(other.getHandle());
137 }
138 }
139
140 void detachChildren() {
141 if (SHOW_TRANSACTIONS) Slog.i(TAG, "SEVER CHILDREN");
142 if (mSurfaceControl != null) {
143 mSurfaceControl.detachChildren();
144 }
145 }
146
Filip Gruszczynski63a35e22015-11-05 15:38:59 -0800147 void hideInTransaction(String reason) {
148 if (SHOW_TRANSACTIONS) logSurface("HIDE ( " + reason + " )", null);
Jorim Jaggi5e6968d2016-02-19 18:02:13 -0800149 mHiddenForOtherReasons = true;
Chong Zhang3cc58dd2016-04-20 17:45:24 -0700150
151 mAnimator.destroyPreservedSurfaceLocked();
Jorim Jaggi5e6968d2016-02-19 18:02:13 -0800152 updateVisibility();
153 }
154
155 private void hideSurface() {
Wale Ogunwale943002b2017-02-15 19:34:01 -0800156 if (mSurfaceControl == null) {
157 return;
158 }
159 setShown(false);
160 try {
161 mSurfaceControl.hide();
162 } catch (RuntimeException e) {
163 Slog.w(TAG, "Exception hiding surface in " + this);
Robert Carre6a83512015-11-03 16:09:21 -0800164 }
165 }
166
Robert Carre6a83512015-11-03 16:09:21 -0800167 void destroyInTransaction() {
Chong Zhangad23b3f2016-08-31 16:05:27 -0700168 if (SHOW_TRANSACTIONS || SHOW_SURFACE_ALLOC) {
169 Slog.i(TAG, "Destroying surface " + this + " called by " + Debug.getCallers(8));
170 }
Robert Carre6a83512015-11-03 16:09:21 -0800171 try {
Wale Ogunwale722ff892016-02-18 13:37:55 -0800172 if (mSurfaceControl != null) {
173 mSurfaceControl.destroy();
174 }
Robert Carre6a83512015-11-03 16:09:21 -0800175 } catch (RuntimeException e) {
176 Slog.w(TAG, "Error destroying surface in: " + this, e);
Wale Ogunwale722ff892016-02-18 13:37:55 -0800177 } finally {
Wale Ogunwale943002b2017-02-15 19:34:01 -0800178 setShown(false);
Wale Ogunwale722ff892016-02-18 13:37:55 -0800179 mSurfaceControl = null;
Robert Carre6a83512015-11-03 16:09:21 -0800180 }
181 }
182
Chong Zhang47e36a32016-02-29 16:44:33 -0800183 void disconnectInTransaction() {
184 if (SHOW_TRANSACTIONS || SHOW_SURFACE_ALLOC) {
185 Slog.i(TAG, "Disconnecting client: " + this);
186 }
187
188 try {
189 if (mSurfaceControl != null) {
190 mSurfaceControl.disconnect();
191 }
192 } catch (RuntimeException e) {
193 Slog.w(TAG, "Error disconnecting surface in: " + this, e);
194 }
195 }
196
Robert Carre6a83512015-11-03 16:09:21 -0800197 void setCropInTransaction(Rect clipRect, boolean recoveringMemory) {
198 if (SHOW_TRANSACTIONS) logSurface(
199 "CROP " + clipRect.toShortString(), null);
200 try {
Robert Carr4320d332016-06-10 15:13:32 -0700201 if (clipRect.width() > 0 && clipRect.height() > 0) {
Robert Carre6a83512015-11-03 16:09:21 -0800202 mSurfaceControl.setWindowCrop(clipRect);
203 mHiddenForCrop = false;
Jorim Jaggi5e6968d2016-02-19 18:02:13 -0800204 updateVisibility();
Robert Carre6a83512015-11-03 16:09:21 -0800205 } else {
Robert Carre6a83512015-11-03 16:09:21 -0800206 mHiddenForCrop = true;
Chong Zhang3cc58dd2016-04-20 17:45:24 -0700207 mAnimator.destroyPreservedSurfaceLocked();
Jorim Jaggi5e6968d2016-02-19 18:02:13 -0800208 updateVisibility();
Robert Carre6a83512015-11-03 16:09:21 -0800209 }
210 } catch (RuntimeException e) {
211 Slog.w(TAG, "Error setting crop surface of " + this
212 + " crop=" + clipRect.toShortString(), e);
213 if (!recoveringMemory) {
214 mAnimator.reclaimSomeSurfaceMemory("crop", true);
215 }
216 }
217 }
218
Robert Carr4320d332016-06-10 15:13:32 -0700219 void clearCropInTransaction(boolean recoveringMemory) {
220 if (SHOW_TRANSACTIONS) logSurface(
221 "CLEAR CROP", null);
222 try {
223 Rect clipRect = new Rect(0, 0, -1, -1);
224 mSurfaceControl.setWindowCrop(clipRect);
225 } catch (RuntimeException e) {
226 Slog.w(TAG, "Error setting clearing crop of " + this, e);
227 if (!recoveringMemory) {
228 mAnimator.reclaimSomeSurfaceMemory("crop", true);
229 }
230 }
231 }
232
Jorim Jaggi6a7c90a2016-03-11 15:04:59 +0100233 void setFinalCropInTransaction(Rect clipRect) {
234 if (SHOW_TRANSACTIONS) logSurface(
235 "FINAL CROP " + clipRect.toShortString(), null);
236 try {
237 mSurfaceControl.setFinalCrop(clipRect);
238 } catch (RuntimeException e) {
239 Slog.w(TAG, "Error disconnecting surface in: " + this, e);
240 }
241 }
242
Robert Carre6a83512015-11-03 16:09:21 -0800243 void setLayer(int layer) {
244 if (mSurfaceControl != null) {
Robert Carr68e5c9e2016-09-14 10:50:09 -0700245 mService.openSurfaceTransaction();
Robert Carre6a83512015-11-03 16:09:21 -0800246 try {
Robert Carraf422a82017-04-10 18:34:33 -0700247 if (mAnimator.mWin.usesRelativeZOrdering()) {
248 mSurfaceControl.setRelativeLayer(
249 mAnimator.mWin.getParentWindow()
250 .mWinAnimator.mSurfaceController.mSurfaceControl.getHandle(),
251 -1);
252 } else {
253 mSurfaceLayer = layer;
254 mSurfaceControl.setLayer(layer);
255 }
Robert Carre6a83512015-11-03 16:09:21 -0800256 } finally {
Robert Carr68e5c9e2016-09-14 10:50:09 -0700257 mService.closeSurfaceTransaction();
Robert Carre6a83512015-11-03 16:09:21 -0800258 }
259 }
260 }
261
Robert Carrda61ba92017-03-29 15:52:23 -0700262 void setLayerStackInTransaction(int layerStack) {
263 if (mSurfaceControl != null) {
264 mSurfaceControl.setLayerStack(layerStack);
265 }
266 }
267
Robert Carre6a83512015-11-03 16:09:21 -0800268 void setPositionInTransaction(float left, float top, boolean recoveringMemory) {
269 final boolean surfaceMoved = mSurfaceX != left || mSurfaceY != top;
270 if (surfaceMoved) {
271 mSurfaceX = left;
272 mSurfaceY = top;
273
274 try {
Chong Zhangae35fef2016-03-16 15:56:55 -0700275 if (SHOW_TRANSACTIONS) logSurface(
276 "POS (setPositionInTransaction) @ (" + left + "," + top + ")", null);
277
Robert Carre6a83512015-11-03 16:09:21 -0800278 mSurfaceControl.setPosition(left, top);
279 } catch (RuntimeException e) {
280 Slog.w(TAG, "Error positioning surface of " + this
281 + " pos=(" + left + "," + top + ")", e);
282 if (!recoveringMemory) {
283 mAnimator.reclaimSomeSurfaceMemory("position", true);
284 }
285 }
286 }
287 }
288
Robert Carr6da3cc02016-06-16 15:17:07 -0700289 void setGeometryAppliesWithResizeInTransaction(boolean recoveringMemory) {
290 mSurfaceControl.setGeometryAppliesWithResize();
Robert Carra9408d42016-06-03 13:28:48 -0700291 }
292
Andrii Kulian283acd22017-08-03 04:03:51 -0700293 void setMatrixInTransaction(float dsdx, float dtdx, float dtdy, float dsdy,
Robert Carre6a83512015-11-03 16:09:21 -0800294 boolean recoveringMemory) {
Matthew Bouyack8dd88f62016-10-24 14:01:26 -0700295 final boolean matrixChanged = mLastDsdx != dsdx || mLastDtdx != dtdx ||
Andrii Kulian283acd22017-08-03 04:03:51 -0700296 mLastDtdy != dtdy || mLastDsdy != dsdy;
Matthew Bouyack8dd88f62016-10-24 14:01:26 -0700297 if (!matrixChanged) {
298 return;
299 }
300
301 mLastDsdx = dsdx;
302 mLastDtdx = dtdx;
Matthew Bouyack8dd88f62016-10-24 14:01:26 -0700303 mLastDtdy = dtdy;
Andrii Kulian283acd22017-08-03 04:03:51 -0700304 mLastDsdy = dsdy;
Matthew Bouyack8dd88f62016-10-24 14:01:26 -0700305
Robert Carre1034cc32016-02-01 13:08:15 -0800306 try {
307 if (SHOW_TRANSACTIONS) logSurface(
Andrii Kulian283acd22017-08-03 04:03:51 -0700308 "MATRIX [" + dsdx + "," + dtdx + "," + dtdy + "," + dsdy + "]", null);
Robert Carre1034cc32016-02-01 13:08:15 -0800309 mSurfaceControl.setMatrix(
Andrii Kulian283acd22017-08-03 04:03:51 -0700310 dsdx, dtdx, dtdy, dsdy);
Robert Carre1034cc32016-02-01 13:08:15 -0800311 } catch (RuntimeException e) {
312 // If something goes wrong with the surface (such
313 // as running out of memory), don't take down the
314 // entire system.
315 Slog.e(TAG, "Error setting matrix on surface surface" + title
Andrii Kulian283acd22017-08-03 04:03:51 -0700316 + " MATRIX [" + dsdx + "," + dtdx + "," + dtdy + "," + dsdy + "]", null);
Robert Carre1034cc32016-02-01 13:08:15 -0800317 if (!recoveringMemory) {
318 mAnimator.reclaimSomeSurfaceMemory("matrix", true);
319 }
320 }
Robert Carre1034cc32016-02-01 13:08:15 -0800321 }
322
323 boolean setSizeInTransaction(int width, int height, boolean recoveringMemory) {
Robert Carre6a83512015-11-03 16:09:21 -0800324 final boolean surfaceResized = mSurfaceW != width || mSurfaceH != height;
325 if (surfaceResized) {
326 mSurfaceW = width;
327 mSurfaceH = height;
328
329 try {
330 if (SHOW_TRANSACTIONS) logSurface(
331 "SIZE " + width + "x" + height, null);
332 mSurfaceControl.setSize(width, height);
Robert Carre6a83512015-11-03 16:09:21 -0800333 } catch (RuntimeException e) {
334 // If something goes wrong with the surface (such
335 // as running out of memory), don't take down the
336 // entire system.
337 Slog.e(TAG, "Error resizing surface of " + title
338 + " size=(" + width + "x" + height + ")", e);
339 if (!recoveringMemory) {
340 mAnimator.reclaimSomeSurfaceMemory("size", true);
341 }
342 return false;
343 }
344 return true;
345 }
346 return false;
347 }
348
Robert Carraf422a82017-04-10 18:34:33 -0700349 boolean prepareToShowInTransaction(float alpha,
Robert Carrda61ba92017-03-29 15:52:23 -0700350 float dsdx, float dtdx, float dsdy,
Robert Carre6a83512015-11-03 16:09:21 -0800351 float dtdy, boolean recoveringMemory) {
352 if (mSurfaceControl != null) {
353 try {
354 mSurfaceAlpha = alpha;
355 mSurfaceControl.setAlpha(alpha);
Matthew Bouyack8dd88f62016-10-24 14:01:26 -0700356 mLastDsdx = dsdx;
357 mLastDtdx = dtdx;
358 mLastDsdy = dsdy;
359 mLastDtdy = dtdy;
Robert Carre6a83512015-11-03 16:09:21 -0800360 mSurfaceControl.setMatrix(
361 dsdx, dtdx, dsdy, dtdy);
Robert Carre6a83512015-11-03 16:09:21 -0800362 } catch (RuntimeException e) {
363 Slog.w(TAG, "Error updating surface in " + title, e);
364 if (!recoveringMemory) {
365 mAnimator.reclaimSomeSurfaceMemory("update", true);
366 }
367 return false;
368 }
369 }
370 return true;
371 }
372
373 void setTransparentRegionHint(final Region region) {
374 if (mSurfaceControl == null) {
375 Slog.w(TAG, "setTransparentRegionHint: null mSurface after mHasSurface true");
376 return;
377 }
378 if (SHOW_LIGHT_TRANSACTIONS) Slog.i(TAG, ">>> OPEN TRANSACTION setTransparentRegion");
Robert Carr68e5c9e2016-09-14 10:50:09 -0700379 mService.openSurfaceTransaction();
Robert Carre6a83512015-11-03 16:09:21 -0800380 try {
381 mSurfaceControl.setTransparentRegionHint(region);
382 } finally {
Robert Carr68e5c9e2016-09-14 10:50:09 -0700383 mService.closeSurfaceTransaction();
Robert Carre6a83512015-11-03 16:09:21 -0800384 if (SHOW_LIGHT_TRANSACTIONS) Slog.i(TAG,
385 "<<< CLOSE TRANSACTION setTransparentRegion");
386 }
387 }
388
389 void setOpaque(boolean isOpaque) {
390 if (SHOW_TRANSACTIONS) logSurface("isOpaque=" + isOpaque,
391 null);
392
393 if (mSurfaceControl == null) {
394 return;
395 }
396 if (SHOW_LIGHT_TRANSACTIONS) Slog.i(TAG, ">>> OPEN TRANSACTION setOpaqueLocked");
Robert Carr68e5c9e2016-09-14 10:50:09 -0700397 mService.openSurfaceTransaction();
Robert Carre6a83512015-11-03 16:09:21 -0800398 try {
399 mSurfaceControl.setOpaque(isOpaque);
400 } finally {
Robert Carr68e5c9e2016-09-14 10:50:09 -0700401 mService.closeSurfaceTransaction();
Robert Carre6a83512015-11-03 16:09:21 -0800402 if (SHOW_LIGHT_TRANSACTIONS) Slog.i(TAG, "<<< CLOSE TRANSACTION setOpaqueLocked");
403 }
404 }
405
406 void setSecure(boolean isSecure) {
407 if (SHOW_TRANSACTIONS) logSurface("isSecure=" + isSecure,
408 null);
409
410 if (mSurfaceControl == null) {
411 return;
412 }
413 if (SHOW_LIGHT_TRANSACTIONS) Slog.i(TAG, ">>> OPEN TRANSACTION setSecureLocked");
Robert Carr68e5c9e2016-09-14 10:50:09 -0700414 mService.openSurfaceTransaction();
Robert Carre6a83512015-11-03 16:09:21 -0800415 try {
416 mSurfaceControl.setSecure(isSecure);
417 } finally {
Robert Carr68e5c9e2016-09-14 10:50:09 -0700418 mService.closeSurfaceTransaction();
Robert Carre6a83512015-11-03 16:09:21 -0800419 if (SHOW_LIGHT_TRANSACTIONS) Slog.i(TAG, "<<< CLOSE TRANSACTION setSecureLocked");
420 }
421 }
422
Andrii Kulian283acd22017-08-03 04:03:51 -0700423 void getContainerRect(Rect rect) {
424 mAnimator.getContainerRect(rect);
425 }
426
Robert Carre6a83512015-11-03 16:09:21 -0800427 boolean showRobustlyInTransaction() {
Filip Gruszczynski19723a42015-11-25 15:01:48 -0800428 if (SHOW_TRANSACTIONS) logSurface(
429 "SHOW (performLayout)", null);
430 if (DEBUG_VISIBILITY) Slog.v(TAG, "Showing " + this
431 + " during relayout");
Jorim Jaggi5e6968d2016-02-19 18:02:13 -0800432 mHiddenForOtherReasons = false;
433 return updateVisibility();
434 }
Robert Carre6a83512015-11-03 16:09:21 -0800435
Jorim Jaggi5e6968d2016-02-19 18:02:13 -0800436 private boolean updateVisibility() {
437 if (mHiddenForCrop || mHiddenForOtherReasons) {
438 if (mSurfaceShown) {
439 hideSurface();
440 }
Robert Carre6a83512015-11-03 16:09:21 -0800441 return false;
Jorim Jaggi5e6968d2016-02-19 18:02:13 -0800442 } else {
443 if (!mSurfaceShown) {
444 return showSurface();
445 } else {
446 return true;
447 }
Robert Carre6a83512015-11-03 16:09:21 -0800448 }
Jorim Jaggi5e6968d2016-02-19 18:02:13 -0800449 }
Robert Carre6a83512015-11-03 16:09:21 -0800450
Jorim Jaggi5e6968d2016-02-19 18:02:13 -0800451 private boolean showSurface() {
Robert Carre6a83512015-11-03 16:09:21 -0800452 try {
Wale Ogunwale943002b2017-02-15 19:34:01 -0800453 setShown(true);
Robert Carre6a83512015-11-03 16:09:21 -0800454 mSurfaceControl.show();
455 return true;
456 } catch (RuntimeException e) {
457 Slog.w(TAG, "Failure showing surface " + mSurfaceControl + " in " + this, e);
458 }
459
460 mAnimator.reclaimSomeSurfaceMemory("show", true);
461
462 return false;
463 }
464
465 void deferTransactionUntil(IBinder handle, long frame) {
466 // TODO: Logging
467 mSurfaceControl.deferTransactionUntil(handle, frame);
468 }
469
Robert Carr1ca6a332016-04-11 18:00:43 -0700470 void forceScaleableInTransaction(boolean force) {
471 // -1 means we don't override the default or client specified
472 // scaling mode.
473 int scalingMode = force ? SCALING_MODE_SCALE_TO_WINDOW : -1;
474 mSurfaceControl.setOverrideScalingMode(scalingMode);
475 }
476
Robert Carre6a83512015-11-03 16:09:21 -0800477 boolean clearWindowContentFrameStats() {
478 if (mSurfaceControl == null) {
479 return false;
480 }
481 return mSurfaceControl.clearContentFrameStats();
482 }
483
484 boolean getWindowContentFrameStats(WindowContentFrameStats outStats) {
485 if (mSurfaceControl == null) {
486 return false;
487 }
488 return mSurfaceControl.getContentFrameStats(outStats);
489 }
490
491
492 boolean hasSurface() {
493 return mSurfaceControl != null;
494 }
495
496 IBinder getHandle() {
497 if (mSurfaceControl == null) {
498 return null;
499 }
500 return mSurfaceControl.getHandle();
501 }
502
503 void getSurface(Surface outSurface) {
504 outSurface.copyFrom(mSurfaceControl);
505 }
506
507 int getLayer() {
508 return mSurfaceLayer;
509 }
510
511 boolean getShown() {
512 return mSurfaceShown;
513 }
514
Filip Gruszczynski10a80e02015-11-06 09:21:17 -0800515 void setShown(boolean surfaceShown) {
516 mSurfaceShown = surfaceShown;
Wale Ogunwale943002b2017-02-15 19:34:01 -0800517
Wale Ogunwale01ad4342017-06-30 07:07:01 -0700518 mService.updateNonSystemOverlayWindowsVisibilityIfNeeded(mAnimator.mWin, surfaceShown);
519
Wale Ogunwale943002b2017-02-15 19:34:01 -0800520 if (mWindowSession != null) {
521 mWindowSession.onWindowSurfaceVisibilityChanged(this, mSurfaceShown, mWindowType);
522 }
Filip Gruszczynski10a80e02015-11-06 09:21:17 -0800523 }
524
Robert Carre6a83512015-11-03 16:09:21 -0800525 float getX() {
526 return mSurfaceX;
527 }
528
529 float getY() {
530 return mSurfaceY;
531 }
532
Robert Carrfed10072016-05-26 11:48:49 -0700533 float getWidth() {
534 return mSurfaceW;
535 }
536
537 float getHeight() {
538 return mSurfaceH;
539 }
540
Steven Timotiusaf03df62017-07-18 16:56:43 -0700541 void writeToProto(ProtoOutputStream proto, long fieldId) {
542 final long token = proto.start(fieldId);
543 proto.write(SHOWN, mSurfaceShown);
544 proto.write(LAYER, mSurfaceLayer);
545 proto.end(token);
546 }
Robert Carrfed10072016-05-26 11:48:49 -0700547
Robert Carre6a83512015-11-03 16:09:21 -0800548 public void dump(PrintWriter pw, String prefix, boolean dumpAll) {
549 if (dumpAll) {
550 pw.print(prefix); pw.print("mSurface="); pw.println(mSurfaceControl);
551 }
552 pw.print(prefix); pw.print("Surface: shown="); pw.print(mSurfaceShown);
553 pw.print(" layer="); pw.print(mSurfaceLayer);
554 pw.print(" alpha="); pw.print(mSurfaceAlpha);
555 pw.print(" rect=("); pw.print(mSurfaceX);
556 pw.print(","); pw.print(mSurfaceY);
557 pw.print(") "); pw.print(mSurfaceW);
Jorim Jaggid878e4c2017-03-03 17:17:06 +0100558 pw.print(" x "); pw.print(mSurfaceH);
559 pw.print(" transform=("); pw.print(mLastDsdx); pw.print(", ");
560 pw.print(mLastDtdx); pw.print(", "); pw.print(mLastDsdy);
561 pw.print(", "); pw.print(mLastDtdy); pw.println(")");
Robert Carre6a83512015-11-03 16:09:21 -0800562 }
563
Filip Gruszczynski78a08ee2015-11-08 18:04:32 -0800564 @Override
565 public String toString() {
566 return mSurfaceControl.toString();
567 }
568
Robert Carre6a83512015-11-03 16:09:21 -0800569 static class SurfaceTrace extends SurfaceControl {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800570 private final static String SURFACE_TAG = TAG_WITH_CLASS_NAME ? "SurfaceTrace" : TAG_WM;
Robert Carre6a83512015-11-03 16:09:21 -0800571 private final static boolean LOG_SURFACE_TRACE = DEBUG_SURFACE_TRACE;
572 final static ArrayList<SurfaceTrace> sSurfaces = new ArrayList<SurfaceTrace>();
573
574 private float mSurfaceTraceAlpha = 0;
575 private int mLayer;
576 private final PointF mPosition = new PointF();
577 private final Point mSize = new Point();
578 private final Rect mWindowCrop = new Rect();
Jorim Jaggi6a7c90a2016-03-11 15:04:59 +0100579 private final Rect mFinalCrop = new Rect();
Robert Carre6a83512015-11-03 16:09:21 -0800580 private boolean mShown = false;
581 private int mLayerStack;
582 private boolean mIsOpaque;
583 private float mDsdx, mDtdx, mDsdy, mDtdy;
584 private final String mName;
585
Albert Chaulk3bf2e572016-11-22 13:59:19 -0500586 public SurfaceTrace(SurfaceSession s, String name, int w, int h, int format, int flags,
587 int windowType, int ownerUid)
588 throws OutOfResourcesException {
589 super(s, name, w, h, format, flags, windowType, ownerUid);
590 mName = name != null ? name : "Not named";
591 mSize.set(w, h);
592 if (LOG_SURFACE_TRACE) Slog.v(SURFACE_TAG, "ctor: " + this + ". Called by "
593 + Debug.getCallers(3));
594 synchronized (sSurfaces) {
595 sSurfaces.add(0, this);
596 }
597 }
598
Robert Carre6a83512015-11-03 16:09:21 -0800599 public SurfaceTrace(SurfaceSession s,
Albert Chaulk3bf2e572016-11-22 13:59:19 -0500600 String name, int w, int h, int format, int flags) {
Robert Carre6a83512015-11-03 16:09:21 -0800601 super(s, name, w, h, format, flags);
602 mName = name != null ? name : "Not named";
603 mSize.set(w, h);
604 if (LOG_SURFACE_TRACE) Slog.v(SURFACE_TAG, "ctor: " + this + ". Called by "
605 + Debug.getCallers(3));
606 synchronized (sSurfaces) {
607 sSurfaces.add(0, this);
608 }
609 }
610
611 @Override
612 public void setAlpha(float alpha) {
613 if (mSurfaceTraceAlpha != alpha) {
614 if (LOG_SURFACE_TRACE) Slog.v(SURFACE_TAG, "setAlpha(" + alpha + "): OLD:" + this +
615 ". Called by " + Debug.getCallers(3));
616 mSurfaceTraceAlpha = alpha;
617 }
618 super.setAlpha(alpha);
619 }
620
621 @Override
622 public void setLayer(int zorder) {
623 if (zorder != mLayer) {
624 if (LOG_SURFACE_TRACE) Slog.v(SURFACE_TAG, "setLayer(" + zorder + "): OLD:" + this
625 + ". Called by " + Debug.getCallers(3));
626 mLayer = zorder;
627 }
628 super.setLayer(zorder);
629
630 synchronized (sSurfaces) {
631 sSurfaces.remove(this);
632 int i;
633 for (i = sSurfaces.size() - 1; i >= 0; i--) {
634 SurfaceTrace s = sSurfaces.get(i);
635 if (s.mLayer < zorder) {
636 break;
637 }
638 }
639 sSurfaces.add(i + 1, this);
640 }
641 }
642
643 @Override
644 public void setPosition(float x, float y) {
645 if (x != mPosition.x || y != mPosition.y) {
646 if (LOG_SURFACE_TRACE) Slog.v(SURFACE_TAG, "setPosition(" + x + "," + y + "): OLD:"
647 + this + ". Called by " + Debug.getCallers(3));
648 mPosition.set(x, y);
649 }
650 super.setPosition(x, y);
651 }
652
653 @Override
Robert Carr6da3cc02016-06-16 15:17:07 -0700654 public void setGeometryAppliesWithResize() {
655 if (LOG_SURFACE_TRACE) Slog.v(SURFACE_TAG, "setGeometryAppliesWithResize(): OLD: "
656 + this + ". Called by" + Debug.getCallers(3));
657 super.setGeometryAppliesWithResize();
Robert Carra9408d42016-06-03 13:28:48 -0700658 }
659
660 @Override
Robert Carre6a83512015-11-03 16:09:21 -0800661 public void setSize(int w, int h) {
662 if (w != mSize.x || h != mSize.y) {
663 if (LOG_SURFACE_TRACE) Slog.v(SURFACE_TAG, "setSize(" + w + "," + h + "): OLD:"
664 + this + ". Called by " + Debug.getCallers(3));
665 mSize.set(w, h);
666 }
667 super.setSize(w, h);
668 }
669
670 @Override
671 public void setWindowCrop(Rect crop) {
672 if (crop != null) {
673 if (!crop.equals(mWindowCrop)) {
674 if (LOG_SURFACE_TRACE) Slog.v(SURFACE_TAG, "setWindowCrop("
675 + crop.toShortString() + "): OLD:" + this + ". Called by "
676 + Debug.getCallers(3));
677 mWindowCrop.set(crop);
678 }
679 }
680 super.setWindowCrop(crop);
681 }
682
683 @Override
Jorim Jaggi6a7c90a2016-03-11 15:04:59 +0100684 public void setFinalCrop(Rect crop) {
685 if (crop != null) {
686 if (!crop.equals(mFinalCrop)) {
687 if (LOG_SURFACE_TRACE) Slog.v(SURFACE_TAG, "setFinalCrop("
688 + crop.toShortString() + "): OLD:" + this + ". Called by "
689 + Debug.getCallers(3));
690 mFinalCrop.set(crop);
691 }
692 }
693 super.setFinalCrop(crop);
694 }
695
696 @Override
Robert Carre6a83512015-11-03 16:09:21 -0800697 public void setLayerStack(int layerStack) {
698 if (layerStack != mLayerStack) {
699 if (LOG_SURFACE_TRACE) Slog.v(SURFACE_TAG, "setLayerStack(" + layerStack + "): OLD:"
700 + this + ". Called by " + Debug.getCallers(3));
701 mLayerStack = layerStack;
702 }
703 super.setLayerStack(layerStack);
704 }
705
706 @Override
707 public void setOpaque(boolean isOpaque) {
708 if (isOpaque != mIsOpaque) {
709 if (LOG_SURFACE_TRACE) Slog.v(SURFACE_TAG, "setOpaque(" + isOpaque + "): OLD:"
710 + this + ". Called by " + Debug.getCallers(3));
711 mIsOpaque = isOpaque;
712 }
713 super.setOpaque(isOpaque);
714 }
715
716 @Override
717 public void setSecure(boolean isSecure) {
718 super.setSecure(isSecure);
719 }
720
721 @Override
722 public void setMatrix(float dsdx, float dtdx, float dsdy, float dtdy) {
723 if (dsdx != mDsdx || dtdx != mDtdx || dsdy != mDsdy || dtdy != mDtdy) {
724 if (LOG_SURFACE_TRACE) Slog.v(SURFACE_TAG, "setMatrix(" + dsdx + "," + dtdx + ","
725 + dsdy + "," + dtdy + "): OLD:" + this + ". Called by "
726 + Debug.getCallers(3));
727 mDsdx = dsdx;
728 mDtdx = dtdx;
729 mDsdy = dsdy;
730 mDtdy = dtdy;
731 }
732 super.setMatrix(dsdx, dtdx, dsdy, dtdy);
733 }
734
735 @Override
736 public void hide() {
737 if (mShown) {
738 if (LOG_SURFACE_TRACE) Slog.v(SURFACE_TAG, "hide: OLD:" + this + ". Called by "
739 + Debug.getCallers(3));
740 mShown = false;
741 }
742 super.hide();
743 }
744
745 @Override
746 public void show() {
747 if (!mShown) {
748 if (LOG_SURFACE_TRACE) Slog.v(SURFACE_TAG, "show: OLD:" + this + ". Called by "
749 + Debug.getCallers(3));
750 mShown = true;
751 }
752 super.show();
753 }
754
755 @Override
756 public void destroy() {
757 super.destroy();
758 if (LOG_SURFACE_TRACE) Slog.v(SURFACE_TAG, "destroy: " + this + ". Called by "
759 + Debug.getCallers(3));
760 synchronized (sSurfaces) {
761 sSurfaces.remove(this);
762 }
763 }
764
765 @Override
766 public void release() {
767 super.release();
768 if (LOG_SURFACE_TRACE) Slog.v(SURFACE_TAG, "release: " + this + ". Called by "
769 + Debug.getCallers(3));
770 synchronized (sSurfaces) {
771 sSurfaces.remove(this);
772 }
773 }
774
Robert Carr6a19b4f2016-03-14 19:17:20 -0700775 @Override
776 public void setTransparentRegionHint(Region region) {
777 if (LOG_SURFACE_TRACE) Slog.v(SURFACE_TAG, "setTransparentRegionHint(" + region
778 + "): OLD: " + this + " . Called by " + Debug.getCallers(3));
779 super.setTransparentRegionHint(region);
780 }
781
Robert Carre6a83512015-11-03 16:09:21 -0800782 static void dumpAllSurfaces(PrintWriter pw, String header) {
783 synchronized (sSurfaces) {
784 final int N = sSurfaces.size();
785 if (N <= 0) {
786 return;
787 }
788 if (header != null) {
789 pw.println(header);
790 }
791 pw.println("WINDOW MANAGER SURFACES (dumpsys window surfaces)");
792 for (int i = 0; i < N; i++) {
793 SurfaceTrace s = sSurfaces.get(i);
794 pw.print(" Surface #"); pw.print(i); pw.print(": #");
795 pw.print(Integer.toHexString(System.identityHashCode(s)));
796 pw.print(" "); pw.println(s.mName);
797 pw.print(" mLayerStack="); pw.print(s.mLayerStack);
798 pw.print(" mLayer="); pw.println(s.mLayer);
799 pw.print(" mShown="); pw.print(s.mShown); pw.print(" mAlpha=");
800 pw.print(s.mSurfaceTraceAlpha); pw.print(" mIsOpaque=");
801 pw.println(s.mIsOpaque);
802 pw.print(" mPosition="); pw.print(s.mPosition.x); pw.print(",");
803 pw.print(s.mPosition.y);
804 pw.print(" mSize="); pw.print(s.mSize.x); pw.print("x");
805 pw.println(s.mSize.y);
806 pw.print(" mCrop="); s.mWindowCrop.printShortString(pw); pw.println();
Jorim Jaggi6a7c90a2016-03-11 15:04:59 +0100807 pw.print(" mFinalCrop="); s.mFinalCrop.printShortString(pw); pw.println();
Robert Carre6a83512015-11-03 16:09:21 -0800808 pw.print(" Transform: ("); pw.print(s.mDsdx); pw.print(", ");
809 pw.print(s.mDtdx); pw.print(", "); pw.print(s.mDsdy);
810 pw.print(", "); pw.print(s.mDtdy); pw.println(")");
811 }
812 }
813 }
814
815 @Override
816 public String toString() {
817 return "Surface " + Integer.toHexString(System.identityHashCode(this)) + " "
818 + mName + " (" + mLayerStack + "): shown=" + mShown + " layer=" + mLayer
819 + " alpha=" + mSurfaceTraceAlpha + " " + mPosition.x + "," + mPosition.y
820 + " " + mSize.x + "x" + mSize.y
821 + " crop=" + mWindowCrop.toShortString()
822 + " opaque=" + mIsOpaque
823 + " (" + mDsdx + "," + mDtdx + "," + mDsdy + "," + mDtdy + ")";
824 }
825 }
Robert Carre6a83512015-11-03 16:09:21 -0800826}