blob: 110d5cb90f459451dd0f891834968fcbc9d385c0 [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;
Wale Ogunwale722ff892016-02-18 13:37:55 -080020import static com.android.server.wm.WindowManagerDebugConfig.SHOW_SURFACE_ALLOC;
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -080021import static com.android.server.wm.WindowManagerDebugConfig.SHOW_TRANSACTIONS;
22import static com.android.server.wm.WindowManagerDebugConfig.SHOW_LIGHT_TRANSACTIONS;
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -080023import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_SURFACE_TRACE;
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -080024import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_VISIBILITY;
25import static com.android.server.wm.WindowManagerDebugConfig.TAG_WITH_CLASS_NAME;
26import static com.android.server.wm.WindowManagerDebugConfig.TAG_WM;
Robert Carr1ca6a332016-04-11 18:00:43 -070027import static android.view.Surface.SCALING_MODE_SCALE_TO_WINDOW;
Robert Carre6a83512015-11-03 16:09:21 -080028
Robert Carre6a83512015-11-03 16:09:21 -080029import android.graphics.Point;
30import android.graphics.PointF;
31import android.graphics.Rect;
32import android.graphics.Region;
33import android.os.IBinder;
34import android.os.Debug;
Wale Ogunwale4958ad22017-06-22 09:08:14 -070035import android.os.Trace;
Robert Carre6a83512015-11-03 16:09:21 -080036import android.view.Surface;
37import android.view.SurfaceControl;
38import android.view.SurfaceSession;
39import android.view.WindowContentFrameStats;
40import android.view.Surface.OutOfResourcesException;
41
42import android.util.Slog;
43
Robert Carr3b716242016-08-16 16:02:21 -070044import java.io.FileDescriptor;
Robert Carre6a83512015-11-03 16:09:21 -080045import java.io.PrintWriter;
46import java.util.ArrayList;
47
48class WindowSurfaceController {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -080049 static final String TAG = TAG_WITH_CLASS_NAME ? "WindowSurfaceController" : TAG_WM;
Robert Carre6a83512015-11-03 16:09:21 -080050
51 final WindowStateAnimator mAnimator;
52
Andrii Kulian283acd22017-08-03 04:03:51 -070053 private SurfaceControlWithBackground mSurfaceControl;
Robert Carre6a83512015-11-03 16:09:21 -080054
Wale Ogunwale943002b2017-02-15 19:34:01 -080055 // Should only be set from within setShown().
Robert Carre6a83512015-11-03 16:09:21 -080056 private boolean mSurfaceShown = false;
57 private float mSurfaceX = 0;
58 private float mSurfaceY = 0;
59 private float mSurfaceW = 0;
60 private float mSurfaceH = 0;
61
Matthew Bouyack8dd88f62016-10-24 14:01:26 -070062 // Initialize to the identity matrix.
63 private float mLastDsdx = 1;
64 private float mLastDtdx = 0;
65 private float mLastDsdy = 0;
66 private float mLastDtdy = 1;
67
Robert Carre6a83512015-11-03 16:09:21 -080068 private float mSurfaceAlpha = 0;
69
70 private int mSurfaceLayer = 0;
Robert Carre6a83512015-11-03 16:09:21 -080071
72 // Surface flinger doesn't support crop rectangles where width or height is non-positive.
73 // However, we need to somehow handle the situation where the cropping would completely hide
74 // the window. We achieve this by explicitly hiding the surface and not letting it be shown.
75 private boolean mHiddenForCrop = false;
Jorim Jaggi1eb39b02016-02-25 13:36:14 -050076
77 // Initially a surface is hidden after just being created.
78 private boolean mHiddenForOtherReasons = true;
Robert Carre6a83512015-11-03 16:09:21 -080079 private final String title;
80
Robert Carr68e5c9e2016-09-14 10:50:09 -070081 private final WindowManagerService mService;
82
Wale Ogunwale943002b2017-02-15 19:34:01 -080083 private final int mWindowType;
84 private final Session mWindowSession;
85
Albert Chaulk3bf2e572016-11-22 13:59:19 -050086 public WindowSurfaceController(SurfaceSession s, String name, int w, int h, int format,
87 int flags, WindowStateAnimator animator, int windowType, int ownerUid) {
Robert Carre6a83512015-11-03 16:09:21 -080088 mAnimator = animator;
89
90 mSurfaceW = w;
91 mSurfaceH = h;
Robert Carre6a83512015-11-03 16:09:21 -080092
93 title = name;
94
Robert Carr68e5c9e2016-09-14 10:50:09 -070095 mService = animator.mService;
Wale Ogunwale943002b2017-02-15 19:34:01 -080096 final WindowState win = animator.mWin;
97 mWindowType = windowType;
98 mWindowSession = win.mSession;
Robert Carr68e5c9e2016-09-14 10:50:09 -070099
Andrii Kulian283acd22017-08-03 04:03:51 -0700100 Trace.traceBegin(TRACE_TAG_WINDOW_MANAGER, "new SurfaceControl");
101 mSurfaceControl = new SurfaceControlWithBackground(
102 s, name, w, h, format, flags, windowType, ownerUid, this);
103 Trace.traceEnd(TRACE_TAG_WINDOW_MANAGER);
Robert Carr3b716242016-08-16 16:02:21 -0700104
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700105 if (mService.mRoot.mSurfaceTraceEnabled) {
106 mSurfaceControl = new RemoteSurfaceTrace(
Wale Ogunwale943002b2017-02-15 19:34:01 -0800107 mService.mRoot.mSurfaceTraceFd.getFileDescriptor(), mSurfaceControl, win);
Robert Carr3b716242016-08-16 16:02:21 -0700108 }
109 }
110
111 void installRemoteTrace(FileDescriptor fd) {
112 mSurfaceControl = new RemoteSurfaceTrace(fd, mSurfaceControl, mAnimator.mWin);
113 }
114
115 void removeRemoteTrace() {
Andrii Kulian283acd22017-08-03 04:03:51 -0700116 mSurfaceControl = new SurfaceControlWithBackground(mSurfaceControl);
Robert Carre6a83512015-11-03 16:09:21 -0800117 }
118
119
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700120 private void logSurface(String msg, RuntimeException where) {
Robert Carre6a83512015-11-03 16:09:21 -0800121 String str = " SURFACE " + msg + ": " + title;
122 if (where != null) {
123 Slog.i(TAG, str, where);
124 } else {
125 Slog.i(TAG, str);
126 }
127 }
128
Robert Carrd5c7dd62017-03-08 10:39:30 -0800129 void reparentChildrenInTransaction(WindowSurfaceController other) {
130 if (SHOW_TRANSACTIONS) Slog.i(TAG, "REPARENT from: " + this + " to: " + other);
131 if ((mSurfaceControl != null) && (other.mSurfaceControl != null)) {
132 mSurfaceControl.reparentChildren(other.getHandle());
133 }
134 }
135
136 void detachChildren() {
137 if (SHOW_TRANSACTIONS) Slog.i(TAG, "SEVER CHILDREN");
138 if (mSurfaceControl != null) {
139 mSurfaceControl.detachChildren();
140 }
141 }
142
Filip Gruszczynski63a35e22015-11-05 15:38:59 -0800143 void hideInTransaction(String reason) {
144 if (SHOW_TRANSACTIONS) logSurface("HIDE ( " + reason + " )", null);
Jorim Jaggi5e6968d2016-02-19 18:02:13 -0800145 mHiddenForOtherReasons = true;
Chong Zhang3cc58dd2016-04-20 17:45:24 -0700146
147 mAnimator.destroyPreservedSurfaceLocked();
Jorim Jaggi5e6968d2016-02-19 18:02:13 -0800148 updateVisibility();
149 }
150
151 private void hideSurface() {
Wale Ogunwale943002b2017-02-15 19:34:01 -0800152 if (mSurfaceControl == null) {
153 return;
154 }
155 setShown(false);
156 try {
157 mSurfaceControl.hide();
158 } catch (RuntimeException e) {
159 Slog.w(TAG, "Exception hiding surface in " + this);
Robert Carre6a83512015-11-03 16:09:21 -0800160 }
161 }
162
Robert Carre6a83512015-11-03 16:09:21 -0800163 void destroyInTransaction() {
Chong Zhangad23b3f2016-08-31 16:05:27 -0700164 if (SHOW_TRANSACTIONS || SHOW_SURFACE_ALLOC) {
165 Slog.i(TAG, "Destroying surface " + this + " called by " + Debug.getCallers(8));
166 }
Robert Carre6a83512015-11-03 16:09:21 -0800167 try {
Wale Ogunwale722ff892016-02-18 13:37:55 -0800168 if (mSurfaceControl != null) {
169 mSurfaceControl.destroy();
170 }
Robert Carre6a83512015-11-03 16:09:21 -0800171 } catch (RuntimeException e) {
172 Slog.w(TAG, "Error destroying surface in: " + this, e);
Wale Ogunwale722ff892016-02-18 13:37:55 -0800173 } finally {
Wale Ogunwale943002b2017-02-15 19:34:01 -0800174 setShown(false);
Wale Ogunwale722ff892016-02-18 13:37:55 -0800175 mSurfaceControl = null;
Robert Carre6a83512015-11-03 16:09:21 -0800176 }
177 }
178
Chong Zhang47e36a32016-02-29 16:44:33 -0800179 void disconnectInTransaction() {
180 if (SHOW_TRANSACTIONS || SHOW_SURFACE_ALLOC) {
181 Slog.i(TAG, "Disconnecting client: " + this);
182 }
183
184 try {
185 if (mSurfaceControl != null) {
186 mSurfaceControl.disconnect();
187 }
188 } catch (RuntimeException e) {
189 Slog.w(TAG, "Error disconnecting surface in: " + this, e);
190 }
191 }
192
Robert Carre6a83512015-11-03 16:09:21 -0800193 void setCropInTransaction(Rect clipRect, boolean recoveringMemory) {
194 if (SHOW_TRANSACTIONS) logSurface(
195 "CROP " + clipRect.toShortString(), null);
196 try {
Robert Carr4320d332016-06-10 15:13:32 -0700197 if (clipRect.width() > 0 && clipRect.height() > 0) {
Robert Carre6a83512015-11-03 16:09:21 -0800198 mSurfaceControl.setWindowCrop(clipRect);
199 mHiddenForCrop = false;
Jorim Jaggi5e6968d2016-02-19 18:02:13 -0800200 updateVisibility();
Robert Carre6a83512015-11-03 16:09:21 -0800201 } else {
Robert Carre6a83512015-11-03 16:09:21 -0800202 mHiddenForCrop = true;
Chong Zhang3cc58dd2016-04-20 17:45:24 -0700203 mAnimator.destroyPreservedSurfaceLocked();
Jorim Jaggi5e6968d2016-02-19 18:02:13 -0800204 updateVisibility();
Robert Carre6a83512015-11-03 16:09:21 -0800205 }
206 } catch (RuntimeException e) {
207 Slog.w(TAG, "Error setting crop surface of " + this
208 + " crop=" + clipRect.toShortString(), e);
209 if (!recoveringMemory) {
210 mAnimator.reclaimSomeSurfaceMemory("crop", true);
211 }
212 }
213 }
214
Robert Carr4320d332016-06-10 15:13:32 -0700215 void clearCropInTransaction(boolean recoveringMemory) {
216 if (SHOW_TRANSACTIONS) logSurface(
217 "CLEAR CROP", null);
218 try {
219 Rect clipRect = new Rect(0, 0, -1, -1);
220 mSurfaceControl.setWindowCrop(clipRect);
221 } catch (RuntimeException e) {
222 Slog.w(TAG, "Error setting clearing crop of " + this, e);
223 if (!recoveringMemory) {
224 mAnimator.reclaimSomeSurfaceMemory("crop", true);
225 }
226 }
227 }
228
Jorim Jaggi6a7c90a2016-03-11 15:04:59 +0100229 void setFinalCropInTransaction(Rect clipRect) {
230 if (SHOW_TRANSACTIONS) logSurface(
231 "FINAL CROP " + clipRect.toShortString(), null);
232 try {
233 mSurfaceControl.setFinalCrop(clipRect);
234 } catch (RuntimeException e) {
235 Slog.w(TAG, "Error disconnecting surface in: " + this, e);
236 }
237 }
238
Robert Carre6a83512015-11-03 16:09:21 -0800239 void setLayer(int layer) {
240 if (mSurfaceControl != null) {
Robert Carr68e5c9e2016-09-14 10:50:09 -0700241 mService.openSurfaceTransaction();
Robert Carre6a83512015-11-03 16:09:21 -0800242 try {
Robert Carraf422a82017-04-10 18:34:33 -0700243 if (mAnimator.mWin.usesRelativeZOrdering()) {
244 mSurfaceControl.setRelativeLayer(
245 mAnimator.mWin.getParentWindow()
246 .mWinAnimator.mSurfaceController.mSurfaceControl.getHandle(),
247 -1);
248 } else {
249 mSurfaceLayer = layer;
250 mSurfaceControl.setLayer(layer);
251 }
Robert Carre6a83512015-11-03 16:09:21 -0800252 } finally {
Robert Carr68e5c9e2016-09-14 10:50:09 -0700253 mService.closeSurfaceTransaction();
Robert Carre6a83512015-11-03 16:09:21 -0800254 }
255 }
256 }
257
Robert Carrda61ba92017-03-29 15:52:23 -0700258 void setLayerStackInTransaction(int layerStack) {
259 if (mSurfaceControl != null) {
260 mSurfaceControl.setLayerStack(layerStack);
261 }
262 }
263
Robert Carre6a83512015-11-03 16:09:21 -0800264 void setPositionInTransaction(float left, float top, boolean recoveringMemory) {
265 final boolean surfaceMoved = mSurfaceX != left || mSurfaceY != top;
266 if (surfaceMoved) {
267 mSurfaceX = left;
268 mSurfaceY = top;
269
270 try {
Chong Zhangae35fef2016-03-16 15:56:55 -0700271 if (SHOW_TRANSACTIONS) logSurface(
272 "POS (setPositionInTransaction) @ (" + left + "," + top + ")", null);
273
Robert Carre6a83512015-11-03 16:09:21 -0800274 mSurfaceControl.setPosition(left, top);
275 } catch (RuntimeException e) {
276 Slog.w(TAG, "Error positioning surface of " + this
277 + " pos=(" + left + "," + top + ")", e);
278 if (!recoveringMemory) {
279 mAnimator.reclaimSomeSurfaceMemory("position", true);
280 }
281 }
282 }
283 }
284
Robert Carr6da3cc02016-06-16 15:17:07 -0700285 void setGeometryAppliesWithResizeInTransaction(boolean recoveringMemory) {
286 mSurfaceControl.setGeometryAppliesWithResize();
Robert Carra9408d42016-06-03 13:28:48 -0700287 }
288
Andrii Kulian283acd22017-08-03 04:03:51 -0700289 void setMatrixInTransaction(float dsdx, float dtdx, float dtdy, float dsdy,
Robert Carre6a83512015-11-03 16:09:21 -0800290 boolean recoveringMemory) {
Matthew Bouyack8dd88f62016-10-24 14:01:26 -0700291 final boolean matrixChanged = mLastDsdx != dsdx || mLastDtdx != dtdx ||
Andrii Kulian283acd22017-08-03 04:03:51 -0700292 mLastDtdy != dtdy || mLastDsdy != dsdy;
Matthew Bouyack8dd88f62016-10-24 14:01:26 -0700293 if (!matrixChanged) {
294 return;
295 }
296
297 mLastDsdx = dsdx;
298 mLastDtdx = dtdx;
Matthew Bouyack8dd88f62016-10-24 14:01:26 -0700299 mLastDtdy = dtdy;
Andrii Kulian283acd22017-08-03 04:03:51 -0700300 mLastDsdy = dsdy;
Matthew Bouyack8dd88f62016-10-24 14:01:26 -0700301
Robert Carre1034cc32016-02-01 13:08:15 -0800302 try {
303 if (SHOW_TRANSACTIONS) logSurface(
Andrii Kulian283acd22017-08-03 04:03:51 -0700304 "MATRIX [" + dsdx + "," + dtdx + "," + dtdy + "," + dsdy + "]", null);
Robert Carre1034cc32016-02-01 13:08:15 -0800305 mSurfaceControl.setMatrix(
Andrii Kulian283acd22017-08-03 04:03:51 -0700306 dsdx, dtdx, dtdy, dsdy);
Robert Carre1034cc32016-02-01 13:08:15 -0800307 } catch (RuntimeException e) {
308 // If something goes wrong with the surface (such
309 // as running out of memory), don't take down the
310 // entire system.
311 Slog.e(TAG, "Error setting matrix on surface surface" + title
Andrii Kulian283acd22017-08-03 04:03:51 -0700312 + " MATRIX [" + dsdx + "," + dtdx + "," + dtdy + "," + dsdy + "]", null);
Robert Carre1034cc32016-02-01 13:08:15 -0800313 if (!recoveringMemory) {
314 mAnimator.reclaimSomeSurfaceMemory("matrix", true);
315 }
316 }
Robert Carre1034cc32016-02-01 13:08:15 -0800317 }
318
319 boolean setSizeInTransaction(int width, int height, boolean recoveringMemory) {
Robert Carre6a83512015-11-03 16:09:21 -0800320 final boolean surfaceResized = mSurfaceW != width || mSurfaceH != height;
321 if (surfaceResized) {
322 mSurfaceW = width;
323 mSurfaceH = height;
324
325 try {
326 if (SHOW_TRANSACTIONS) logSurface(
327 "SIZE " + width + "x" + height, null);
328 mSurfaceControl.setSize(width, height);
Robert Carre6a83512015-11-03 16:09:21 -0800329 } catch (RuntimeException e) {
330 // If something goes wrong with the surface (such
331 // as running out of memory), don't take down the
332 // entire system.
333 Slog.e(TAG, "Error resizing surface of " + title
334 + " size=(" + width + "x" + height + ")", e);
335 if (!recoveringMemory) {
336 mAnimator.reclaimSomeSurfaceMemory("size", true);
337 }
338 return false;
339 }
340 return true;
341 }
342 return false;
343 }
344
Robert Carraf422a82017-04-10 18:34:33 -0700345 boolean prepareToShowInTransaction(float alpha,
Robert Carrda61ba92017-03-29 15:52:23 -0700346 float dsdx, float dtdx, float dsdy,
Robert Carre6a83512015-11-03 16:09:21 -0800347 float dtdy, boolean recoveringMemory) {
348 if (mSurfaceControl != null) {
349 try {
350 mSurfaceAlpha = alpha;
351 mSurfaceControl.setAlpha(alpha);
Matthew Bouyack8dd88f62016-10-24 14:01:26 -0700352 mLastDsdx = dsdx;
353 mLastDtdx = dtdx;
354 mLastDsdy = dsdy;
355 mLastDtdy = dtdy;
Robert Carre6a83512015-11-03 16:09:21 -0800356 mSurfaceControl.setMatrix(
357 dsdx, dtdx, dsdy, dtdy);
Robert Carre6a83512015-11-03 16:09:21 -0800358 } catch (RuntimeException e) {
359 Slog.w(TAG, "Error updating surface in " + title, e);
360 if (!recoveringMemory) {
361 mAnimator.reclaimSomeSurfaceMemory("update", true);
362 }
363 return false;
364 }
365 }
366 return true;
367 }
368
369 void setTransparentRegionHint(final Region region) {
370 if (mSurfaceControl == null) {
371 Slog.w(TAG, "setTransparentRegionHint: null mSurface after mHasSurface true");
372 return;
373 }
374 if (SHOW_LIGHT_TRANSACTIONS) Slog.i(TAG, ">>> OPEN TRANSACTION setTransparentRegion");
Robert Carr68e5c9e2016-09-14 10:50:09 -0700375 mService.openSurfaceTransaction();
Robert Carre6a83512015-11-03 16:09:21 -0800376 try {
377 mSurfaceControl.setTransparentRegionHint(region);
378 } finally {
Robert Carr68e5c9e2016-09-14 10:50:09 -0700379 mService.closeSurfaceTransaction();
Robert Carre6a83512015-11-03 16:09:21 -0800380 if (SHOW_LIGHT_TRANSACTIONS) Slog.i(TAG,
381 "<<< CLOSE TRANSACTION setTransparentRegion");
382 }
383 }
384
385 void setOpaque(boolean isOpaque) {
386 if (SHOW_TRANSACTIONS) logSurface("isOpaque=" + isOpaque,
387 null);
388
389 if (mSurfaceControl == null) {
390 return;
391 }
392 if (SHOW_LIGHT_TRANSACTIONS) Slog.i(TAG, ">>> OPEN TRANSACTION setOpaqueLocked");
Robert Carr68e5c9e2016-09-14 10:50:09 -0700393 mService.openSurfaceTransaction();
Robert Carre6a83512015-11-03 16:09:21 -0800394 try {
395 mSurfaceControl.setOpaque(isOpaque);
396 } finally {
Robert Carr68e5c9e2016-09-14 10:50:09 -0700397 mService.closeSurfaceTransaction();
Robert Carre6a83512015-11-03 16:09:21 -0800398 if (SHOW_LIGHT_TRANSACTIONS) Slog.i(TAG, "<<< CLOSE TRANSACTION setOpaqueLocked");
399 }
400 }
401
402 void setSecure(boolean isSecure) {
403 if (SHOW_TRANSACTIONS) logSurface("isSecure=" + isSecure,
404 null);
405
406 if (mSurfaceControl == null) {
407 return;
408 }
409 if (SHOW_LIGHT_TRANSACTIONS) Slog.i(TAG, ">>> OPEN TRANSACTION setSecureLocked");
Robert Carr68e5c9e2016-09-14 10:50:09 -0700410 mService.openSurfaceTransaction();
Robert Carre6a83512015-11-03 16:09:21 -0800411 try {
412 mSurfaceControl.setSecure(isSecure);
413 } finally {
Robert Carr68e5c9e2016-09-14 10:50:09 -0700414 mService.closeSurfaceTransaction();
Robert Carre6a83512015-11-03 16:09:21 -0800415 if (SHOW_LIGHT_TRANSACTIONS) Slog.i(TAG, "<<< CLOSE TRANSACTION setSecureLocked");
416 }
417 }
418
Andrii Kulian283acd22017-08-03 04:03:51 -0700419 void getContainerRect(Rect rect) {
420 mAnimator.getContainerRect(rect);
421 }
422
Robert Carre6a83512015-11-03 16:09:21 -0800423 boolean showRobustlyInTransaction() {
Filip Gruszczynski19723a42015-11-25 15:01:48 -0800424 if (SHOW_TRANSACTIONS) logSurface(
425 "SHOW (performLayout)", null);
426 if (DEBUG_VISIBILITY) Slog.v(TAG, "Showing " + this
427 + " during relayout");
Jorim Jaggi5e6968d2016-02-19 18:02:13 -0800428 mHiddenForOtherReasons = false;
429 return updateVisibility();
430 }
Robert Carre6a83512015-11-03 16:09:21 -0800431
Jorim Jaggi5e6968d2016-02-19 18:02:13 -0800432 private boolean updateVisibility() {
433 if (mHiddenForCrop || mHiddenForOtherReasons) {
434 if (mSurfaceShown) {
435 hideSurface();
436 }
Robert Carre6a83512015-11-03 16:09:21 -0800437 return false;
Jorim Jaggi5e6968d2016-02-19 18:02:13 -0800438 } else {
439 if (!mSurfaceShown) {
440 return showSurface();
441 } else {
442 return true;
443 }
Robert Carre6a83512015-11-03 16:09:21 -0800444 }
Jorim Jaggi5e6968d2016-02-19 18:02:13 -0800445 }
Robert Carre6a83512015-11-03 16:09:21 -0800446
Jorim Jaggi5e6968d2016-02-19 18:02:13 -0800447 private boolean showSurface() {
Robert Carre6a83512015-11-03 16:09:21 -0800448 try {
Wale Ogunwale943002b2017-02-15 19:34:01 -0800449 setShown(true);
Robert Carre6a83512015-11-03 16:09:21 -0800450 mSurfaceControl.show();
451 return true;
452 } catch (RuntimeException e) {
453 Slog.w(TAG, "Failure showing surface " + mSurfaceControl + " in " + this, e);
454 }
455
456 mAnimator.reclaimSomeSurfaceMemory("show", true);
457
458 return false;
459 }
460
461 void deferTransactionUntil(IBinder handle, long frame) {
462 // TODO: Logging
463 mSurfaceControl.deferTransactionUntil(handle, frame);
464 }
465
Robert Carr1ca6a332016-04-11 18:00:43 -0700466 void forceScaleableInTransaction(boolean force) {
467 // -1 means we don't override the default or client specified
468 // scaling mode.
469 int scalingMode = force ? SCALING_MODE_SCALE_TO_WINDOW : -1;
470 mSurfaceControl.setOverrideScalingMode(scalingMode);
471 }
472
Robert Carre6a83512015-11-03 16:09:21 -0800473 boolean clearWindowContentFrameStats() {
474 if (mSurfaceControl == null) {
475 return false;
476 }
477 return mSurfaceControl.clearContentFrameStats();
478 }
479
480 boolean getWindowContentFrameStats(WindowContentFrameStats outStats) {
481 if (mSurfaceControl == null) {
482 return false;
483 }
484 return mSurfaceControl.getContentFrameStats(outStats);
485 }
486
487
488 boolean hasSurface() {
489 return mSurfaceControl != null;
490 }
491
492 IBinder getHandle() {
493 if (mSurfaceControl == null) {
494 return null;
495 }
496 return mSurfaceControl.getHandle();
497 }
498
499 void getSurface(Surface outSurface) {
500 outSurface.copyFrom(mSurfaceControl);
501 }
502
503 int getLayer() {
504 return mSurfaceLayer;
505 }
506
507 boolean getShown() {
508 return mSurfaceShown;
509 }
510
Filip Gruszczynski10a80e02015-11-06 09:21:17 -0800511 void setShown(boolean surfaceShown) {
512 mSurfaceShown = surfaceShown;
Wale Ogunwale943002b2017-02-15 19:34:01 -0800513
Wale Ogunwale01ad4342017-06-30 07:07:01 -0700514 mService.updateNonSystemOverlayWindowsVisibilityIfNeeded(mAnimator.mWin, surfaceShown);
515
Wale Ogunwale943002b2017-02-15 19:34:01 -0800516 if (mWindowSession != null) {
517 mWindowSession.onWindowSurfaceVisibilityChanged(this, mSurfaceShown, mWindowType);
518 }
Filip Gruszczynski10a80e02015-11-06 09:21:17 -0800519 }
520
Robert Carre6a83512015-11-03 16:09:21 -0800521 float getX() {
522 return mSurfaceX;
523 }
524
525 float getY() {
526 return mSurfaceY;
527 }
528
Robert Carrfed10072016-05-26 11:48:49 -0700529 float getWidth() {
530 return mSurfaceW;
531 }
532
533 float getHeight() {
534 return mSurfaceH;
535 }
536
537
Robert Carre6a83512015-11-03 16:09:21 -0800538 public void dump(PrintWriter pw, String prefix, boolean dumpAll) {
539 if (dumpAll) {
540 pw.print(prefix); pw.print("mSurface="); pw.println(mSurfaceControl);
541 }
542 pw.print(prefix); pw.print("Surface: shown="); pw.print(mSurfaceShown);
543 pw.print(" layer="); pw.print(mSurfaceLayer);
544 pw.print(" alpha="); pw.print(mSurfaceAlpha);
545 pw.print(" rect=("); pw.print(mSurfaceX);
546 pw.print(","); pw.print(mSurfaceY);
547 pw.print(") "); pw.print(mSurfaceW);
Jorim Jaggid878e4c2017-03-03 17:17:06 +0100548 pw.print(" x "); pw.print(mSurfaceH);
549 pw.print(" transform=("); pw.print(mLastDsdx); pw.print(", ");
550 pw.print(mLastDtdx); pw.print(", "); pw.print(mLastDsdy);
551 pw.print(", "); pw.print(mLastDtdy); pw.println(")");
Robert Carre6a83512015-11-03 16:09:21 -0800552 }
553
Filip Gruszczynski78a08ee2015-11-08 18:04:32 -0800554 @Override
555 public String toString() {
556 return mSurfaceControl.toString();
557 }
558
Robert Carre6a83512015-11-03 16:09:21 -0800559 static class SurfaceTrace extends SurfaceControl {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800560 private final static String SURFACE_TAG = TAG_WITH_CLASS_NAME ? "SurfaceTrace" : TAG_WM;
Robert Carre6a83512015-11-03 16:09:21 -0800561 private final static boolean LOG_SURFACE_TRACE = DEBUG_SURFACE_TRACE;
562 final static ArrayList<SurfaceTrace> sSurfaces = new ArrayList<SurfaceTrace>();
563
564 private float mSurfaceTraceAlpha = 0;
565 private int mLayer;
566 private final PointF mPosition = new PointF();
567 private final Point mSize = new Point();
568 private final Rect mWindowCrop = new Rect();
Jorim Jaggi6a7c90a2016-03-11 15:04:59 +0100569 private final Rect mFinalCrop = new Rect();
Robert Carre6a83512015-11-03 16:09:21 -0800570 private boolean mShown = false;
571 private int mLayerStack;
572 private boolean mIsOpaque;
573 private float mDsdx, mDtdx, mDsdy, mDtdy;
574 private final String mName;
575
Albert Chaulk3bf2e572016-11-22 13:59:19 -0500576 public SurfaceTrace(SurfaceSession s, String name, int w, int h, int format, int flags,
577 int windowType, int ownerUid)
578 throws OutOfResourcesException {
579 super(s, name, w, h, format, flags, windowType, ownerUid);
580 mName = name != null ? name : "Not named";
581 mSize.set(w, h);
582 if (LOG_SURFACE_TRACE) Slog.v(SURFACE_TAG, "ctor: " + this + ". Called by "
583 + Debug.getCallers(3));
584 synchronized (sSurfaces) {
585 sSurfaces.add(0, this);
586 }
587 }
588
Robert Carre6a83512015-11-03 16:09:21 -0800589 public SurfaceTrace(SurfaceSession s,
Albert Chaulk3bf2e572016-11-22 13:59:19 -0500590 String name, int w, int h, int format, int flags) {
Robert Carre6a83512015-11-03 16:09:21 -0800591 super(s, name, w, h, format, flags);
592 mName = name != null ? name : "Not named";
593 mSize.set(w, h);
594 if (LOG_SURFACE_TRACE) Slog.v(SURFACE_TAG, "ctor: " + this + ". Called by "
595 + Debug.getCallers(3));
596 synchronized (sSurfaces) {
597 sSurfaces.add(0, this);
598 }
599 }
600
601 @Override
602 public void setAlpha(float alpha) {
603 if (mSurfaceTraceAlpha != alpha) {
604 if (LOG_SURFACE_TRACE) Slog.v(SURFACE_TAG, "setAlpha(" + alpha + "): OLD:" + this +
605 ". Called by " + Debug.getCallers(3));
606 mSurfaceTraceAlpha = alpha;
607 }
608 super.setAlpha(alpha);
609 }
610
611 @Override
612 public void setLayer(int zorder) {
613 if (zorder != mLayer) {
614 if (LOG_SURFACE_TRACE) Slog.v(SURFACE_TAG, "setLayer(" + zorder + "): OLD:" + this
615 + ". Called by " + Debug.getCallers(3));
616 mLayer = zorder;
617 }
618 super.setLayer(zorder);
619
620 synchronized (sSurfaces) {
621 sSurfaces.remove(this);
622 int i;
623 for (i = sSurfaces.size() - 1; i >= 0; i--) {
624 SurfaceTrace s = sSurfaces.get(i);
625 if (s.mLayer < zorder) {
626 break;
627 }
628 }
629 sSurfaces.add(i + 1, this);
630 }
631 }
632
633 @Override
634 public void setPosition(float x, float y) {
635 if (x != mPosition.x || y != mPosition.y) {
636 if (LOG_SURFACE_TRACE) Slog.v(SURFACE_TAG, "setPosition(" + x + "," + y + "): OLD:"
637 + this + ". Called by " + Debug.getCallers(3));
638 mPosition.set(x, y);
639 }
640 super.setPosition(x, y);
641 }
642
643 @Override
Robert Carr6da3cc02016-06-16 15:17:07 -0700644 public void setGeometryAppliesWithResize() {
645 if (LOG_SURFACE_TRACE) Slog.v(SURFACE_TAG, "setGeometryAppliesWithResize(): OLD: "
646 + this + ". Called by" + Debug.getCallers(3));
647 super.setGeometryAppliesWithResize();
Robert Carra9408d42016-06-03 13:28:48 -0700648 }
649
650 @Override
Robert Carre6a83512015-11-03 16:09:21 -0800651 public void setSize(int w, int h) {
652 if (w != mSize.x || h != mSize.y) {
653 if (LOG_SURFACE_TRACE) Slog.v(SURFACE_TAG, "setSize(" + w + "," + h + "): OLD:"
654 + this + ". Called by " + Debug.getCallers(3));
655 mSize.set(w, h);
656 }
657 super.setSize(w, h);
658 }
659
660 @Override
661 public void setWindowCrop(Rect crop) {
662 if (crop != null) {
663 if (!crop.equals(mWindowCrop)) {
664 if (LOG_SURFACE_TRACE) Slog.v(SURFACE_TAG, "setWindowCrop("
665 + crop.toShortString() + "): OLD:" + this + ". Called by "
666 + Debug.getCallers(3));
667 mWindowCrop.set(crop);
668 }
669 }
670 super.setWindowCrop(crop);
671 }
672
673 @Override
Jorim Jaggi6a7c90a2016-03-11 15:04:59 +0100674 public void setFinalCrop(Rect crop) {
675 if (crop != null) {
676 if (!crop.equals(mFinalCrop)) {
677 if (LOG_SURFACE_TRACE) Slog.v(SURFACE_TAG, "setFinalCrop("
678 + crop.toShortString() + "): OLD:" + this + ". Called by "
679 + Debug.getCallers(3));
680 mFinalCrop.set(crop);
681 }
682 }
683 super.setFinalCrop(crop);
684 }
685
686 @Override
Robert Carre6a83512015-11-03 16:09:21 -0800687 public void setLayerStack(int layerStack) {
688 if (layerStack != mLayerStack) {
689 if (LOG_SURFACE_TRACE) Slog.v(SURFACE_TAG, "setLayerStack(" + layerStack + "): OLD:"
690 + this + ". Called by " + Debug.getCallers(3));
691 mLayerStack = layerStack;
692 }
693 super.setLayerStack(layerStack);
694 }
695
696 @Override
697 public void setOpaque(boolean isOpaque) {
698 if (isOpaque != mIsOpaque) {
699 if (LOG_SURFACE_TRACE) Slog.v(SURFACE_TAG, "setOpaque(" + isOpaque + "): OLD:"
700 + this + ". Called by " + Debug.getCallers(3));
701 mIsOpaque = isOpaque;
702 }
703 super.setOpaque(isOpaque);
704 }
705
706 @Override
707 public void setSecure(boolean isSecure) {
708 super.setSecure(isSecure);
709 }
710
711 @Override
712 public void setMatrix(float dsdx, float dtdx, float dsdy, float dtdy) {
713 if (dsdx != mDsdx || dtdx != mDtdx || dsdy != mDsdy || dtdy != mDtdy) {
714 if (LOG_SURFACE_TRACE) Slog.v(SURFACE_TAG, "setMatrix(" + dsdx + "," + dtdx + ","
715 + dsdy + "," + dtdy + "): OLD:" + this + ". Called by "
716 + Debug.getCallers(3));
717 mDsdx = dsdx;
718 mDtdx = dtdx;
719 mDsdy = dsdy;
720 mDtdy = dtdy;
721 }
722 super.setMatrix(dsdx, dtdx, dsdy, dtdy);
723 }
724
725 @Override
726 public void hide() {
727 if (mShown) {
728 if (LOG_SURFACE_TRACE) Slog.v(SURFACE_TAG, "hide: OLD:" + this + ". Called by "
729 + Debug.getCallers(3));
730 mShown = false;
731 }
732 super.hide();
733 }
734
735 @Override
736 public void show() {
737 if (!mShown) {
738 if (LOG_SURFACE_TRACE) Slog.v(SURFACE_TAG, "show: OLD:" + this + ". Called by "
739 + Debug.getCallers(3));
740 mShown = true;
741 }
742 super.show();
743 }
744
745 @Override
746 public void destroy() {
747 super.destroy();
748 if (LOG_SURFACE_TRACE) Slog.v(SURFACE_TAG, "destroy: " + this + ". Called by "
749 + Debug.getCallers(3));
750 synchronized (sSurfaces) {
751 sSurfaces.remove(this);
752 }
753 }
754
755 @Override
756 public void release() {
757 super.release();
758 if (LOG_SURFACE_TRACE) Slog.v(SURFACE_TAG, "release: " + this + ". Called by "
759 + Debug.getCallers(3));
760 synchronized (sSurfaces) {
761 sSurfaces.remove(this);
762 }
763 }
764
Robert Carr6a19b4f2016-03-14 19:17:20 -0700765 @Override
766 public void setTransparentRegionHint(Region region) {
767 if (LOG_SURFACE_TRACE) Slog.v(SURFACE_TAG, "setTransparentRegionHint(" + region
768 + "): OLD: " + this + " . Called by " + Debug.getCallers(3));
769 super.setTransparentRegionHint(region);
770 }
771
Robert Carre6a83512015-11-03 16:09:21 -0800772 static void dumpAllSurfaces(PrintWriter pw, String header) {
773 synchronized (sSurfaces) {
774 final int N = sSurfaces.size();
775 if (N <= 0) {
776 return;
777 }
778 if (header != null) {
779 pw.println(header);
780 }
781 pw.println("WINDOW MANAGER SURFACES (dumpsys window surfaces)");
782 for (int i = 0; i < N; i++) {
783 SurfaceTrace s = sSurfaces.get(i);
784 pw.print(" Surface #"); pw.print(i); pw.print(": #");
785 pw.print(Integer.toHexString(System.identityHashCode(s)));
786 pw.print(" "); pw.println(s.mName);
787 pw.print(" mLayerStack="); pw.print(s.mLayerStack);
788 pw.print(" mLayer="); pw.println(s.mLayer);
789 pw.print(" mShown="); pw.print(s.mShown); pw.print(" mAlpha=");
790 pw.print(s.mSurfaceTraceAlpha); pw.print(" mIsOpaque=");
791 pw.println(s.mIsOpaque);
792 pw.print(" mPosition="); pw.print(s.mPosition.x); pw.print(",");
793 pw.print(s.mPosition.y);
794 pw.print(" mSize="); pw.print(s.mSize.x); pw.print("x");
795 pw.println(s.mSize.y);
796 pw.print(" mCrop="); s.mWindowCrop.printShortString(pw); pw.println();
Jorim Jaggi6a7c90a2016-03-11 15:04:59 +0100797 pw.print(" mFinalCrop="); s.mFinalCrop.printShortString(pw); pw.println();
Robert Carre6a83512015-11-03 16:09:21 -0800798 pw.print(" Transform: ("); pw.print(s.mDsdx); pw.print(", ");
799 pw.print(s.mDtdx); pw.print(", "); pw.print(s.mDsdy);
800 pw.print(", "); pw.print(s.mDtdy); pw.println(")");
801 }
802 }
803 }
804
805 @Override
806 public String toString() {
807 return "Surface " + Integer.toHexString(System.identityHashCode(this)) + " "
808 + mName + " (" + mLayerStack + "): shown=" + mShown + " layer=" + mLayer
809 + " alpha=" + mSurfaceTraceAlpha + " " + mPosition.x + "," + mPosition.y
810 + " " + mSize.x + "x" + mSize.y
811 + " crop=" + mWindowCrop.toShortString()
812 + " opaque=" + mIsOpaque
813 + " (" + mDsdx + "," + mDtdx + "," + mDsdy + "," + mDtdy + ")";
814 }
815 }
Robert Carre6a83512015-11-03 16:09:21 -0800816}