blob: f8e74284fafd15cf58f2d3815ac2653c182ad613 [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 Ogunwale722ff892016-02-18 13:37:55 -080019import static com.android.server.wm.WindowManagerDebugConfig.SHOW_SURFACE_ALLOC;
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -080020import static com.android.server.wm.WindowManagerDebugConfig.SHOW_TRANSACTIONS;
21import static com.android.server.wm.WindowManagerDebugConfig.SHOW_LIGHT_TRANSACTIONS;
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -080022import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_SURFACE_TRACE;
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -080023import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_VISIBILITY;
24import static com.android.server.wm.WindowManagerDebugConfig.TAG_WITH_CLASS_NAME;
25import static com.android.server.wm.WindowManagerDebugConfig.TAG_WM;
Robert Carr1ca6a332016-04-11 18:00:43 -070026import static android.view.Surface.SCALING_MODE_FREEZE;
27import static android.view.Surface.SCALING_MODE_SCALE_TO_WINDOW;
Robert Carre6a83512015-11-03 16:09:21 -080028
Robert Carr086d2922016-06-09 10:22:38 -070029import android.graphics.PixelFormat;
Robert Carre6a83512015-11-03 16:09:21 -080030import android.graphics.Point;
31import android.graphics.PointF;
32import android.graphics.Rect;
33import android.graphics.Region;
34import android.os.IBinder;
35import android.os.Debug;
36import 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
53 private SurfaceControl mSurfaceControl;
54
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
Wale Ogunwale943002b2017-02-15 19:34:01 -0800100 // For opaque child windows placed under parent windows, we use a special SurfaceControl
101 // which mirrors commands to a black-out layer placed one Z-layer below the surface.
Robert Carr086d2922016-06-09 10:22:38 -0700102 // This prevents holes to whatever app/wallpaper is underneath.
Wale Ogunwale943002b2017-02-15 19:34:01 -0800103 if (win.isChildWindow() && win.mSubLayer < 0 && win.mAppToken != null) {
104 mSurfaceControl = new SurfaceControlWithBackground(
105 s, name, w, h, format, flags, win.mAppToken, windowType, ownerUid);
Robert Carr086d2922016-06-09 10:22:38 -0700106 } else if (DEBUG_SURFACE_TRACE) {
Robert Carre6a83512015-11-03 16:09:21 -0800107 mSurfaceControl = new SurfaceTrace(
Albert Chaulk3bf2e572016-11-22 13:59:19 -0500108 s, name, w, h, format, flags, windowType, ownerUid);
Robert Carre6a83512015-11-03 16:09:21 -0800109 } else {
110 mSurfaceControl = new SurfaceControl(
Albert Chaulk3bf2e572016-11-22 13:59:19 -0500111 s, name, w, h, format, flags, windowType, ownerUid);
Robert Carre6a83512015-11-03 16:09:21 -0800112 }
Robert Carr3b716242016-08-16 16:02:21 -0700113
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700114 if (mService.mRoot.mSurfaceTraceEnabled) {
115 mSurfaceControl = new RemoteSurfaceTrace(
Wale Ogunwale943002b2017-02-15 19:34:01 -0800116 mService.mRoot.mSurfaceTraceFd.getFileDescriptor(), mSurfaceControl, win);
Robert Carr3b716242016-08-16 16:02:21 -0700117 }
118 }
119
120 void installRemoteTrace(FileDescriptor fd) {
121 mSurfaceControl = new RemoteSurfaceTrace(fd, mSurfaceControl, mAnimator.mWin);
122 }
123
124 void removeRemoteTrace() {
125 mSurfaceControl = new SurfaceControl(mSurfaceControl);
Robert Carre6a83512015-11-03 16:09:21 -0800126 }
127
128
Wale Ogunwalee05f5012016-09-16 16:27:29 -0700129 private void logSurface(String msg, RuntimeException where) {
Robert Carre6a83512015-11-03 16:09:21 -0800130 String str = " SURFACE " + msg + ": " + title;
131 if (where != null) {
132 Slog.i(TAG, str, where);
133 } else {
134 Slog.i(TAG, str);
135 }
136 }
137
Filip Gruszczynski63a35e22015-11-05 15:38:59 -0800138 void hideInTransaction(String reason) {
139 if (SHOW_TRANSACTIONS) logSurface("HIDE ( " + reason + " )", null);
Jorim Jaggi5e6968d2016-02-19 18:02:13 -0800140 mHiddenForOtherReasons = true;
Chong Zhang3cc58dd2016-04-20 17:45:24 -0700141
142 mAnimator.destroyPreservedSurfaceLocked();
Jorim Jaggi5e6968d2016-02-19 18:02:13 -0800143 updateVisibility();
144 }
145
146 private void hideSurface() {
Wale Ogunwale943002b2017-02-15 19:34:01 -0800147 if (mSurfaceControl == null) {
148 return;
149 }
150 setShown(false);
151 try {
152 mSurfaceControl.hide();
153 } catch (RuntimeException e) {
154 Slog.w(TAG, "Exception hiding surface in " + this);
Robert Carre6a83512015-11-03 16:09:21 -0800155 }
156 }
157
158 void setPositionAndLayer(float left, float top, int layerStack, int layer) {
Robert Carr68e5c9e2016-09-14 10:50:09 -0700159 mService.openSurfaceTransaction();
Robert Carre6a83512015-11-03 16:09:21 -0800160 try {
161 mSurfaceX = left;
162 mSurfaceY = top;
163
164 try {
Chong Zhangae35fef2016-03-16 15:56:55 -0700165 if (SHOW_TRANSACTIONS) logSurface(
166 "POS (setPositionAndLayer) @ (" + left + "," + top + ")", null);
Robert Carre6a83512015-11-03 16:09:21 -0800167 mSurfaceControl.setPosition(left, top);
168 mSurfaceControl.setLayerStack(layerStack);
169
170 mSurfaceControl.setLayer(layer);
171 mSurfaceControl.setAlpha(0);
Wale Ogunwale943002b2017-02-15 19:34:01 -0800172 setShown(false);
Robert Carre6a83512015-11-03 16:09:21 -0800173 } catch (RuntimeException e) {
174 Slog.w(TAG, "Error creating surface in " + this, e);
175 mAnimator.reclaimSomeSurfaceMemory("create-init", true);
176 }
177 } finally {
Robert Carr68e5c9e2016-09-14 10:50:09 -0700178 mService.closeSurfaceTransaction();
Robert Carre6a83512015-11-03 16:09:21 -0800179 if (SHOW_LIGHT_TRANSACTIONS) Slog.i(TAG,
180 "<<< CLOSE TRANSACTION setPositionAndLayer");
181 }
182 }
183
184 void destroyInTransaction() {
Chong Zhangad23b3f2016-08-31 16:05:27 -0700185 if (SHOW_TRANSACTIONS || SHOW_SURFACE_ALLOC) {
186 Slog.i(TAG, "Destroying surface " + this + " called by " + Debug.getCallers(8));
187 }
Robert Carre6a83512015-11-03 16:09:21 -0800188 try {
Wale Ogunwale722ff892016-02-18 13:37:55 -0800189 if (mSurfaceControl != null) {
190 mSurfaceControl.destroy();
191 }
Robert Carre6a83512015-11-03 16:09:21 -0800192 } catch (RuntimeException e) {
193 Slog.w(TAG, "Error destroying surface in: " + this, e);
Wale Ogunwale722ff892016-02-18 13:37:55 -0800194 } finally {
Wale Ogunwale943002b2017-02-15 19:34:01 -0800195 setShown(false);
Wale Ogunwale722ff892016-02-18 13:37:55 -0800196 mSurfaceControl = null;
Robert Carre6a83512015-11-03 16:09:21 -0800197 }
198 }
199
Chong Zhang47e36a32016-02-29 16:44:33 -0800200 void disconnectInTransaction() {
201 if (SHOW_TRANSACTIONS || SHOW_SURFACE_ALLOC) {
202 Slog.i(TAG, "Disconnecting client: " + this);
203 }
204
205 try {
206 if (mSurfaceControl != null) {
207 mSurfaceControl.disconnect();
208 }
209 } catch (RuntimeException e) {
210 Slog.w(TAG, "Error disconnecting surface in: " + this, e);
211 }
212 }
213
Robert Carre6a83512015-11-03 16:09:21 -0800214 void setCropInTransaction(Rect clipRect, boolean recoveringMemory) {
215 if (SHOW_TRANSACTIONS) logSurface(
216 "CROP " + clipRect.toShortString(), null);
217 try {
Robert Carr4320d332016-06-10 15:13:32 -0700218 if (clipRect.width() > 0 && clipRect.height() > 0) {
Robert Carre6a83512015-11-03 16:09:21 -0800219 mSurfaceControl.setWindowCrop(clipRect);
220 mHiddenForCrop = false;
Jorim Jaggi5e6968d2016-02-19 18:02:13 -0800221 updateVisibility();
Robert Carre6a83512015-11-03 16:09:21 -0800222 } else {
Robert Carre6a83512015-11-03 16:09:21 -0800223 mHiddenForCrop = true;
Chong Zhang3cc58dd2016-04-20 17:45:24 -0700224 mAnimator.destroyPreservedSurfaceLocked();
Jorim Jaggi5e6968d2016-02-19 18:02:13 -0800225 updateVisibility();
Robert Carre6a83512015-11-03 16:09:21 -0800226 }
227 } catch (RuntimeException e) {
228 Slog.w(TAG, "Error setting crop surface of " + this
229 + " crop=" + clipRect.toShortString(), e);
230 if (!recoveringMemory) {
231 mAnimator.reclaimSomeSurfaceMemory("crop", true);
232 }
233 }
234 }
235
Robert Carr4320d332016-06-10 15:13:32 -0700236 void clearCropInTransaction(boolean recoveringMemory) {
237 if (SHOW_TRANSACTIONS) logSurface(
238 "CLEAR CROP", null);
239 try {
240 Rect clipRect = new Rect(0, 0, -1, -1);
241 mSurfaceControl.setWindowCrop(clipRect);
242 } catch (RuntimeException e) {
243 Slog.w(TAG, "Error setting clearing crop of " + this, e);
244 if (!recoveringMemory) {
245 mAnimator.reclaimSomeSurfaceMemory("crop", true);
246 }
247 }
248 }
249
Jorim Jaggi6a7c90a2016-03-11 15:04:59 +0100250 void setFinalCropInTransaction(Rect clipRect) {
251 if (SHOW_TRANSACTIONS) logSurface(
252 "FINAL CROP " + clipRect.toShortString(), null);
253 try {
254 mSurfaceControl.setFinalCrop(clipRect);
255 } catch (RuntimeException e) {
256 Slog.w(TAG, "Error disconnecting surface in: " + this, e);
257 }
258 }
259
Robert Carre6a83512015-11-03 16:09:21 -0800260 void setLayer(int layer) {
261 if (mSurfaceControl != null) {
Robert Carr68e5c9e2016-09-14 10:50:09 -0700262 mService.openSurfaceTransaction();
Robert Carre6a83512015-11-03 16:09:21 -0800263 try {
264 mSurfaceControl.setLayer(layer);
265 } finally {
Robert Carr68e5c9e2016-09-14 10:50:09 -0700266 mService.closeSurfaceTransaction();
Robert Carre6a83512015-11-03 16:09:21 -0800267 }
268 }
269 }
270
271 void setPositionInTransaction(float left, float top, boolean recoveringMemory) {
272 final boolean surfaceMoved = mSurfaceX != left || mSurfaceY != top;
273 if (surfaceMoved) {
274 mSurfaceX = left;
275 mSurfaceY = top;
276
277 try {
Chong Zhangae35fef2016-03-16 15:56:55 -0700278 if (SHOW_TRANSACTIONS) logSurface(
279 "POS (setPositionInTransaction) @ (" + left + "," + top + ")", null);
280
Robert Carre6a83512015-11-03 16:09:21 -0800281 mSurfaceControl.setPosition(left, top);
282 } catch (RuntimeException e) {
283 Slog.w(TAG, "Error positioning surface of " + this
284 + " pos=(" + left + "," + top + ")", e);
285 if (!recoveringMemory) {
286 mAnimator.reclaimSomeSurfaceMemory("position", true);
287 }
288 }
289 }
290 }
291
Robert Carr6da3cc02016-06-16 15:17:07 -0700292 void setGeometryAppliesWithResizeInTransaction(boolean recoveringMemory) {
293 mSurfaceControl.setGeometryAppliesWithResize();
Robert Carra9408d42016-06-03 13:28:48 -0700294 }
295
Robert Carre1034cc32016-02-01 13:08:15 -0800296 void setMatrixInTransaction(float dsdx, float dtdx, float dsdy, float dtdy,
Robert Carre6a83512015-11-03 16:09:21 -0800297 boolean recoveringMemory) {
Matthew Bouyack8dd88f62016-10-24 14:01:26 -0700298 final boolean matrixChanged = mLastDsdx != dsdx || mLastDtdx != dtdx ||
299 mLastDsdy != dsdy || mLastDtdy != dtdy;
300 if (!matrixChanged) {
301 return;
302 }
303
304 mLastDsdx = dsdx;
305 mLastDtdx = dtdx;
306 mLastDsdy = dsdy;
307 mLastDtdy = dtdy;
308
Robert Carre1034cc32016-02-01 13:08:15 -0800309 try {
310 if (SHOW_TRANSACTIONS) logSurface(
311 "MATRIX [" + dsdx + "," + dtdx + "," + dsdy + "," + dtdy + "]", null);
312 mSurfaceControl.setMatrix(
313 dsdx, dtdx, dsdy, dtdy);
314 } catch (RuntimeException e) {
315 // If something goes wrong with the surface (such
316 // as running out of memory), don't take down the
317 // entire system.
318 Slog.e(TAG, "Error setting matrix on surface surface" + title
319 + " MATRIX [" + dsdx + "," + dtdx + "," + dsdy + "," + dtdy + "]", null);
320 if (!recoveringMemory) {
321 mAnimator.reclaimSomeSurfaceMemory("matrix", true);
322 }
323 }
Robert Carre1034cc32016-02-01 13:08:15 -0800324 }
325
326 boolean setSizeInTransaction(int width, int height, boolean recoveringMemory) {
Robert Carre6a83512015-11-03 16:09:21 -0800327 final boolean surfaceResized = mSurfaceW != width || mSurfaceH != height;
328 if (surfaceResized) {
329 mSurfaceW = width;
330 mSurfaceH = height;
331
332 try {
333 if (SHOW_TRANSACTIONS) logSurface(
334 "SIZE " + width + "x" + height, null);
335 mSurfaceControl.setSize(width, height);
Robert Carre6a83512015-11-03 16:09:21 -0800336 } catch (RuntimeException e) {
337 // If something goes wrong with the surface (such
338 // as running out of memory), don't take down the
339 // entire system.
340 Slog.e(TAG, "Error resizing surface of " + title
341 + " size=(" + width + "x" + height + ")", e);
342 if (!recoveringMemory) {
343 mAnimator.reclaimSomeSurfaceMemory("size", true);
344 }
345 return false;
346 }
347 return true;
348 }
349 return false;
350 }
351
352 boolean prepareToShowInTransaction(float alpha, int layer, float dsdx, float dtdx, float dsdy,
353 float dtdy, boolean recoveringMemory) {
354 if (mSurfaceControl != null) {
355 try {
356 mSurfaceAlpha = alpha;
357 mSurfaceControl.setAlpha(alpha);
358 mSurfaceLayer = layer;
359 mSurfaceControl.setLayer(layer);
Matthew Bouyack8dd88f62016-10-24 14:01:26 -0700360 mLastDsdx = dsdx;
361 mLastDtdx = dtdx;
362 mLastDsdy = dsdy;
363 mLastDtdy = dtdy;
Robert Carre6a83512015-11-03 16:09:21 -0800364 mSurfaceControl.setMatrix(
365 dsdx, dtdx, dsdy, dtdy);
366
367 } catch (RuntimeException e) {
368 Slog.w(TAG, "Error updating surface in " + title, e);
369 if (!recoveringMemory) {
370 mAnimator.reclaimSomeSurfaceMemory("update", true);
371 }
372 return false;
373 }
374 }
375 return true;
376 }
377
378 void setTransparentRegionHint(final Region region) {
379 if (mSurfaceControl == null) {
380 Slog.w(TAG, "setTransparentRegionHint: null mSurface after mHasSurface true");
381 return;
382 }
383 if (SHOW_LIGHT_TRANSACTIONS) Slog.i(TAG, ">>> OPEN TRANSACTION setTransparentRegion");
Robert Carr68e5c9e2016-09-14 10:50:09 -0700384 mService.openSurfaceTransaction();
Robert Carre6a83512015-11-03 16:09:21 -0800385 try {
386 mSurfaceControl.setTransparentRegionHint(region);
387 } finally {
Robert Carr68e5c9e2016-09-14 10:50:09 -0700388 mService.closeSurfaceTransaction();
Robert Carre6a83512015-11-03 16:09:21 -0800389 if (SHOW_LIGHT_TRANSACTIONS) Slog.i(TAG,
390 "<<< CLOSE TRANSACTION setTransparentRegion");
391 }
392 }
393
394 void setOpaque(boolean isOpaque) {
395 if (SHOW_TRANSACTIONS) logSurface("isOpaque=" + isOpaque,
396 null);
397
398 if (mSurfaceControl == null) {
399 return;
400 }
401 if (SHOW_LIGHT_TRANSACTIONS) Slog.i(TAG, ">>> OPEN TRANSACTION setOpaqueLocked");
Robert Carr68e5c9e2016-09-14 10:50:09 -0700402 mService.openSurfaceTransaction();
Robert Carre6a83512015-11-03 16:09:21 -0800403 try {
404 mSurfaceControl.setOpaque(isOpaque);
405 } finally {
Robert Carr68e5c9e2016-09-14 10:50:09 -0700406 mService.closeSurfaceTransaction();
Robert Carre6a83512015-11-03 16:09:21 -0800407 if (SHOW_LIGHT_TRANSACTIONS) Slog.i(TAG, "<<< CLOSE TRANSACTION setOpaqueLocked");
408 }
409 }
410
411 void setSecure(boolean isSecure) {
412 if (SHOW_TRANSACTIONS) logSurface("isSecure=" + isSecure,
413 null);
414
415 if (mSurfaceControl == null) {
416 return;
417 }
418 if (SHOW_LIGHT_TRANSACTIONS) Slog.i(TAG, ">>> OPEN TRANSACTION setSecureLocked");
Robert Carr68e5c9e2016-09-14 10:50:09 -0700419 mService.openSurfaceTransaction();
Robert Carre6a83512015-11-03 16:09:21 -0800420 try {
421 mSurfaceControl.setSecure(isSecure);
422 } finally {
Robert Carr68e5c9e2016-09-14 10:50:09 -0700423 mService.closeSurfaceTransaction();
Robert Carre6a83512015-11-03 16:09:21 -0800424 if (SHOW_LIGHT_TRANSACTIONS) Slog.i(TAG, "<<< CLOSE TRANSACTION setSecureLocked");
425 }
426 }
427
428 boolean showRobustlyInTransaction() {
Filip Gruszczynski19723a42015-11-25 15:01:48 -0800429 if (SHOW_TRANSACTIONS) logSurface(
430 "SHOW (performLayout)", null);
431 if (DEBUG_VISIBILITY) Slog.v(TAG, "Showing " + this
432 + " during relayout");
Jorim Jaggi5e6968d2016-02-19 18:02:13 -0800433 mHiddenForOtherReasons = false;
434 return updateVisibility();
435 }
Robert Carre6a83512015-11-03 16:09:21 -0800436
Jorim Jaggi5e6968d2016-02-19 18:02:13 -0800437 private boolean updateVisibility() {
438 if (mHiddenForCrop || mHiddenForOtherReasons) {
439 if (mSurfaceShown) {
440 hideSurface();
441 }
Robert Carre6a83512015-11-03 16:09:21 -0800442 return false;
Jorim Jaggi5e6968d2016-02-19 18:02:13 -0800443 } else {
444 if (!mSurfaceShown) {
445 return showSurface();
446 } else {
447 return true;
448 }
Robert Carre6a83512015-11-03 16:09:21 -0800449 }
Jorim Jaggi5e6968d2016-02-19 18:02:13 -0800450 }
Robert Carre6a83512015-11-03 16:09:21 -0800451
Jorim Jaggi5e6968d2016-02-19 18:02:13 -0800452 private boolean showSurface() {
Robert Carre6a83512015-11-03 16:09:21 -0800453 try {
Wale Ogunwale943002b2017-02-15 19:34:01 -0800454 setShown(true);
Robert Carre6a83512015-11-03 16:09:21 -0800455 mSurfaceControl.show();
456 return true;
457 } catch (RuntimeException e) {
458 Slog.w(TAG, "Failure showing surface " + mSurfaceControl + " in " + this, e);
459 }
460
461 mAnimator.reclaimSomeSurfaceMemory("show", true);
462
463 return false;
464 }
465
466 void deferTransactionUntil(IBinder handle, long frame) {
467 // TODO: Logging
468 mSurfaceControl.deferTransactionUntil(handle, frame);
469 }
470
Robert Carr1ca6a332016-04-11 18:00:43 -0700471 void forceScaleableInTransaction(boolean force) {
472 // -1 means we don't override the default or client specified
473 // scaling mode.
474 int scalingMode = force ? SCALING_MODE_SCALE_TO_WINDOW : -1;
475 mSurfaceControl.setOverrideScalingMode(scalingMode);
476 }
477
Robert Carre6a83512015-11-03 16:09:21 -0800478 boolean clearWindowContentFrameStats() {
479 if (mSurfaceControl == null) {
480 return false;
481 }
482 return mSurfaceControl.clearContentFrameStats();
483 }
484
485 boolean getWindowContentFrameStats(WindowContentFrameStats outStats) {
486 if (mSurfaceControl == null) {
487 return false;
488 }
489 return mSurfaceControl.getContentFrameStats(outStats);
490 }
491
492
493 boolean hasSurface() {
494 return mSurfaceControl != null;
495 }
496
497 IBinder getHandle() {
498 if (mSurfaceControl == null) {
499 return null;
500 }
501 return mSurfaceControl.getHandle();
502 }
503
Robert Carr6da3cc02016-06-16 15:17:07 -0700504 boolean getTransformToDisplayInverse() {
505 return mSurfaceControl.getTransformToDisplayInverse();
506 }
507
Robert Carre6a83512015-11-03 16:09:21 -0800508 void getSurface(Surface outSurface) {
509 outSurface.copyFrom(mSurfaceControl);
510 }
511
512 int getLayer() {
513 return mSurfaceLayer;
514 }
515
516 boolean getShown() {
517 return mSurfaceShown;
518 }
519
Filip Gruszczynski10a80e02015-11-06 09:21:17 -0800520 void setShown(boolean surfaceShown) {
521 mSurfaceShown = surfaceShown;
Wale Ogunwale943002b2017-02-15 19:34:01 -0800522
523 if (mWindowSession != null) {
524 mWindowSession.onWindowSurfaceVisibilityChanged(this, mSurfaceShown, mWindowType);
525 }
Filip Gruszczynski10a80e02015-11-06 09:21:17 -0800526 }
527
Robert Carre6a83512015-11-03 16:09:21 -0800528 float getX() {
529 return mSurfaceX;
530 }
531
532 float getY() {
533 return mSurfaceY;
534 }
535
Robert Carrfed10072016-05-26 11:48:49 -0700536 float getWidth() {
537 return mSurfaceW;
538 }
539
540 float getHeight() {
541 return mSurfaceH;
542 }
543
544
Robert Carre6a83512015-11-03 16:09:21 -0800545 public void dump(PrintWriter pw, String prefix, boolean dumpAll) {
546 if (dumpAll) {
547 pw.print(prefix); pw.print("mSurface="); pw.println(mSurfaceControl);
548 }
549 pw.print(prefix); pw.print("Surface: shown="); pw.print(mSurfaceShown);
550 pw.print(" layer="); pw.print(mSurfaceLayer);
551 pw.print(" alpha="); pw.print(mSurfaceAlpha);
552 pw.print(" rect=("); pw.print(mSurfaceX);
553 pw.print(","); pw.print(mSurfaceY);
554 pw.print(") "); pw.print(mSurfaceW);
555 pw.print(" x "); pw.println(mSurfaceH);
556 }
557
Filip Gruszczynski78a08ee2015-11-08 18:04:32 -0800558 @Override
559 public String toString() {
560 return mSurfaceControl.toString();
561 }
562
Robert Carre6a83512015-11-03 16:09:21 -0800563 static class SurfaceTrace extends SurfaceControl {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800564 private final static String SURFACE_TAG = TAG_WITH_CLASS_NAME ? "SurfaceTrace" : TAG_WM;
Robert Carre6a83512015-11-03 16:09:21 -0800565 private final static boolean LOG_SURFACE_TRACE = DEBUG_SURFACE_TRACE;
566 final static ArrayList<SurfaceTrace> sSurfaces = new ArrayList<SurfaceTrace>();
567
568 private float mSurfaceTraceAlpha = 0;
569 private int mLayer;
570 private final PointF mPosition = new PointF();
571 private final Point mSize = new Point();
572 private final Rect mWindowCrop = new Rect();
Jorim Jaggi6a7c90a2016-03-11 15:04:59 +0100573 private final Rect mFinalCrop = new Rect();
Robert Carre6a83512015-11-03 16:09:21 -0800574 private boolean mShown = false;
575 private int mLayerStack;
576 private boolean mIsOpaque;
577 private float mDsdx, mDtdx, mDsdy, mDtdy;
578 private final String mName;
579
Albert Chaulk3bf2e572016-11-22 13:59:19 -0500580 public SurfaceTrace(SurfaceSession s, String name, int w, int h, int format, int flags,
581 int windowType, int ownerUid)
582 throws OutOfResourcesException {
583 super(s, name, w, h, format, flags, windowType, ownerUid);
584 mName = name != null ? name : "Not named";
585 mSize.set(w, h);
586 if (LOG_SURFACE_TRACE) Slog.v(SURFACE_TAG, "ctor: " + this + ". Called by "
587 + Debug.getCallers(3));
588 synchronized (sSurfaces) {
589 sSurfaces.add(0, this);
590 }
591 }
592
Robert Carre6a83512015-11-03 16:09:21 -0800593 public SurfaceTrace(SurfaceSession s,
Albert Chaulk3bf2e572016-11-22 13:59:19 -0500594 String name, int w, int h, int format, int flags) {
Robert Carre6a83512015-11-03 16:09:21 -0800595 super(s, name, w, h, format, flags);
596 mName = name != null ? name : "Not named";
597 mSize.set(w, h);
598 if (LOG_SURFACE_TRACE) Slog.v(SURFACE_TAG, "ctor: " + this + ". Called by "
599 + Debug.getCallers(3));
600 synchronized (sSurfaces) {
601 sSurfaces.add(0, this);
602 }
603 }
604
605 @Override
606 public void setAlpha(float alpha) {
607 if (mSurfaceTraceAlpha != alpha) {
608 if (LOG_SURFACE_TRACE) Slog.v(SURFACE_TAG, "setAlpha(" + alpha + "): OLD:" + this +
609 ". Called by " + Debug.getCallers(3));
610 mSurfaceTraceAlpha = alpha;
611 }
612 super.setAlpha(alpha);
613 }
614
615 @Override
616 public void setLayer(int zorder) {
617 if (zorder != mLayer) {
618 if (LOG_SURFACE_TRACE) Slog.v(SURFACE_TAG, "setLayer(" + zorder + "): OLD:" + this
619 + ". Called by " + Debug.getCallers(3));
620 mLayer = zorder;
621 }
622 super.setLayer(zorder);
623
624 synchronized (sSurfaces) {
625 sSurfaces.remove(this);
626 int i;
627 for (i = sSurfaces.size() - 1; i >= 0; i--) {
628 SurfaceTrace s = sSurfaces.get(i);
629 if (s.mLayer < zorder) {
630 break;
631 }
632 }
633 sSurfaces.add(i + 1, this);
634 }
635 }
636
637 @Override
638 public void setPosition(float x, float y) {
639 if (x != mPosition.x || y != mPosition.y) {
640 if (LOG_SURFACE_TRACE) Slog.v(SURFACE_TAG, "setPosition(" + x + "," + y + "): OLD:"
641 + this + ". Called by " + Debug.getCallers(3));
642 mPosition.set(x, y);
643 }
644 super.setPosition(x, y);
645 }
646
647 @Override
Robert Carr6da3cc02016-06-16 15:17:07 -0700648 public void setGeometryAppliesWithResize() {
649 if (LOG_SURFACE_TRACE) Slog.v(SURFACE_TAG, "setGeometryAppliesWithResize(): OLD: "
650 + this + ". Called by" + Debug.getCallers(3));
651 super.setGeometryAppliesWithResize();
Robert Carra9408d42016-06-03 13:28:48 -0700652 }
653
654 @Override
Robert Carre6a83512015-11-03 16:09:21 -0800655 public void setSize(int w, int h) {
656 if (w != mSize.x || h != mSize.y) {
657 if (LOG_SURFACE_TRACE) Slog.v(SURFACE_TAG, "setSize(" + w + "," + h + "): OLD:"
658 + this + ". Called by " + Debug.getCallers(3));
659 mSize.set(w, h);
660 }
661 super.setSize(w, h);
662 }
663
664 @Override
665 public void setWindowCrop(Rect crop) {
666 if (crop != null) {
667 if (!crop.equals(mWindowCrop)) {
668 if (LOG_SURFACE_TRACE) Slog.v(SURFACE_TAG, "setWindowCrop("
669 + crop.toShortString() + "): OLD:" + this + ". Called by "
670 + Debug.getCallers(3));
671 mWindowCrop.set(crop);
672 }
673 }
674 super.setWindowCrop(crop);
675 }
676
677 @Override
Jorim Jaggi6a7c90a2016-03-11 15:04:59 +0100678 public void setFinalCrop(Rect crop) {
679 if (crop != null) {
680 if (!crop.equals(mFinalCrop)) {
681 if (LOG_SURFACE_TRACE) Slog.v(SURFACE_TAG, "setFinalCrop("
682 + crop.toShortString() + "): OLD:" + this + ". Called by "
683 + Debug.getCallers(3));
684 mFinalCrop.set(crop);
685 }
686 }
687 super.setFinalCrop(crop);
688 }
689
690 @Override
Robert Carre6a83512015-11-03 16:09:21 -0800691 public void setLayerStack(int layerStack) {
692 if (layerStack != mLayerStack) {
693 if (LOG_SURFACE_TRACE) Slog.v(SURFACE_TAG, "setLayerStack(" + layerStack + "): OLD:"
694 + this + ". Called by " + Debug.getCallers(3));
695 mLayerStack = layerStack;
696 }
697 super.setLayerStack(layerStack);
698 }
699
700 @Override
701 public void setOpaque(boolean isOpaque) {
702 if (isOpaque != mIsOpaque) {
703 if (LOG_SURFACE_TRACE) Slog.v(SURFACE_TAG, "setOpaque(" + isOpaque + "): OLD:"
704 + this + ". Called by " + Debug.getCallers(3));
705 mIsOpaque = isOpaque;
706 }
707 super.setOpaque(isOpaque);
708 }
709
710 @Override
711 public void setSecure(boolean isSecure) {
712 super.setSecure(isSecure);
713 }
714
715 @Override
716 public void setMatrix(float dsdx, float dtdx, float dsdy, float dtdy) {
717 if (dsdx != mDsdx || dtdx != mDtdx || dsdy != mDsdy || dtdy != mDtdy) {
718 if (LOG_SURFACE_TRACE) Slog.v(SURFACE_TAG, "setMatrix(" + dsdx + "," + dtdx + ","
719 + dsdy + "," + dtdy + "): OLD:" + this + ". Called by "
720 + Debug.getCallers(3));
721 mDsdx = dsdx;
722 mDtdx = dtdx;
723 mDsdy = dsdy;
724 mDtdy = dtdy;
725 }
726 super.setMatrix(dsdx, dtdx, dsdy, dtdy);
727 }
728
729 @Override
730 public void hide() {
731 if (mShown) {
732 if (LOG_SURFACE_TRACE) Slog.v(SURFACE_TAG, "hide: OLD:" + this + ". Called by "
733 + Debug.getCallers(3));
734 mShown = false;
735 }
736 super.hide();
737 }
738
739 @Override
740 public void show() {
741 if (!mShown) {
742 if (LOG_SURFACE_TRACE) Slog.v(SURFACE_TAG, "show: OLD:" + this + ". Called by "
743 + Debug.getCallers(3));
744 mShown = true;
745 }
746 super.show();
747 }
748
749 @Override
750 public void destroy() {
751 super.destroy();
752 if (LOG_SURFACE_TRACE) Slog.v(SURFACE_TAG, "destroy: " + this + ". Called by "
753 + Debug.getCallers(3));
754 synchronized (sSurfaces) {
755 sSurfaces.remove(this);
756 }
757 }
758
759 @Override
760 public void release() {
761 super.release();
762 if (LOG_SURFACE_TRACE) Slog.v(SURFACE_TAG, "release: " + this + ". Called by "
763 + Debug.getCallers(3));
764 synchronized (sSurfaces) {
765 sSurfaces.remove(this);
766 }
767 }
768
Robert Carr6a19b4f2016-03-14 19:17:20 -0700769 @Override
770 public void setTransparentRegionHint(Region region) {
771 if (LOG_SURFACE_TRACE) Slog.v(SURFACE_TAG, "setTransparentRegionHint(" + region
772 + "): OLD: " + this + " . Called by " + Debug.getCallers(3));
773 super.setTransparentRegionHint(region);
774 }
775
Robert Carre6a83512015-11-03 16:09:21 -0800776 static void dumpAllSurfaces(PrintWriter pw, String header) {
777 synchronized (sSurfaces) {
778 final int N = sSurfaces.size();
779 if (N <= 0) {
780 return;
781 }
782 if (header != null) {
783 pw.println(header);
784 }
785 pw.println("WINDOW MANAGER SURFACES (dumpsys window surfaces)");
786 for (int i = 0; i < N; i++) {
787 SurfaceTrace s = sSurfaces.get(i);
788 pw.print(" Surface #"); pw.print(i); pw.print(": #");
789 pw.print(Integer.toHexString(System.identityHashCode(s)));
790 pw.print(" "); pw.println(s.mName);
791 pw.print(" mLayerStack="); pw.print(s.mLayerStack);
792 pw.print(" mLayer="); pw.println(s.mLayer);
793 pw.print(" mShown="); pw.print(s.mShown); pw.print(" mAlpha=");
794 pw.print(s.mSurfaceTraceAlpha); pw.print(" mIsOpaque=");
795 pw.println(s.mIsOpaque);
796 pw.print(" mPosition="); pw.print(s.mPosition.x); pw.print(",");
797 pw.print(s.mPosition.y);
798 pw.print(" mSize="); pw.print(s.mSize.x); pw.print("x");
799 pw.println(s.mSize.y);
800 pw.print(" mCrop="); s.mWindowCrop.printShortString(pw); pw.println();
Jorim Jaggi6a7c90a2016-03-11 15:04:59 +0100801 pw.print(" mFinalCrop="); s.mFinalCrop.printShortString(pw); pw.println();
Robert Carre6a83512015-11-03 16:09:21 -0800802 pw.print(" Transform: ("); pw.print(s.mDsdx); pw.print(", ");
803 pw.print(s.mDtdx); pw.print(", "); pw.print(s.mDsdy);
804 pw.print(", "); pw.print(s.mDtdy); pw.println(")");
805 }
806 }
807 }
808
809 @Override
810 public String toString() {
811 return "Surface " + Integer.toHexString(System.identityHashCode(this)) + " "
812 + mName + " (" + mLayerStack + "): shown=" + mShown + " layer=" + mLayer
813 + " alpha=" + mSurfaceTraceAlpha + " " + mPosition.x + "," + mPosition.y
814 + " " + mSize.x + "x" + mSize.y
815 + " crop=" + mWindowCrop.toShortString()
816 + " opaque=" + mIsOpaque
817 + " (" + mDsdx + "," + mDtdx + "," + mDsdy + "," + mDtdy + ")";
818 }
819 }
Robert Carr086d2922016-06-09 10:22:38 -0700820
Robert Carr91b228092016-06-28 17:32:37 -0700821 class SurfaceControlWithBackground extends SurfaceControl {
Robert Carr086d2922016-06-09 10:22:38 -0700822 private SurfaceControl mBackgroundControl;
823 private boolean mOpaque = true;
Robert Carr91b228092016-06-28 17:32:37 -0700824 private boolean mAppForcedInvisible = false;
825 private AppWindowToken mAppToken;
826 public boolean mVisible = false;
827 public int mLayer = -1;
Robert Carr086d2922016-06-09 10:22:38 -0700828
Albert Chaulk3bf2e572016-11-22 13:59:19 -0500829 public SurfaceControlWithBackground(SurfaceSession s, String name, int w, int h, int format,
830 int flags, AppWindowToken token, int windowType, int ownerUid)
831 throws OutOfResourcesException {
832 super(s, name, w, h, format, flags, windowType, ownerUid);
Robert Carr086d2922016-06-09 10:22:38 -0700833 mBackgroundControl = new SurfaceControl(s, name, w, h,
834 PixelFormat.OPAQUE, flags | SurfaceControl.FX_SURFACE_DIM);
835 mOpaque = (flags & SurfaceControl.OPAQUE) != 0;
Robert Carr91b228092016-06-28 17:32:37 -0700836 mAppToken = token;
837
838 mAppToken.addSurfaceViewBackground(this);
Robert Carr086d2922016-06-09 10:22:38 -0700839 }
840
841 @Override
842 public void setAlpha(float alpha) {
843 super.setAlpha(alpha);
844 mBackgroundControl.setAlpha(alpha);
845 }
846
847 @Override
848 public void setLayer(int zorder) {
849 super.setLayer(zorder);
850 mBackgroundControl.setLayer(zorder - 1);
Robert Carr91b228092016-06-28 17:32:37 -0700851 if (mLayer != zorder) {
852 mLayer = zorder;
853 mAppToken.updateSurfaceViewBackgroundVisibilities();
854 }
Robert Carr086d2922016-06-09 10:22:38 -0700855 }
856
857 @Override
858 public void setPosition(float x, float y) {
859 super.setPosition(x, y);
860 mBackgroundControl.setPosition(x, y);
861 }
862
863 @Override
864 public void setSize(int w, int h) {
865 super.setSize(w, h);
866 mBackgroundControl.setSize(w, h);
867 }
868
869 @Override
870 public void setWindowCrop(Rect crop) {
871 super.setWindowCrop(crop);
872 mBackgroundControl.setWindowCrop(crop);
873 }
874
875 @Override
876 public void setFinalCrop(Rect crop) {
877 super.setFinalCrop(crop);
878 mBackgroundControl.setFinalCrop(crop);
879 }
880
881 @Override
882 public void setLayerStack(int layerStack) {
883 super.setLayerStack(layerStack);
884 mBackgroundControl.setLayerStack(layerStack);
885 }
886
887 @Override
888 public void setOpaque(boolean isOpaque) {
889 super.setOpaque(isOpaque);
890 mOpaque = isOpaque;
Robert Carr91b228092016-06-28 17:32:37 -0700891 updateBackgroundVisibility(mAppForcedInvisible);
Robert Carr086d2922016-06-09 10:22:38 -0700892 }
893
894 @Override
895 public void setSecure(boolean isSecure) {
896 super.setSecure(isSecure);
897 }
898
899 @Override
900 public void setMatrix(float dsdx, float dtdx, float dsdy, float dtdy) {
901 super.setMatrix(dsdx, dtdx, dsdy, dtdy);
902 mBackgroundControl.setMatrix(dsdx, dtdx, dsdy, dtdy);
903 }
904
905 @Override
906 public void hide() {
Robert Carr086d2922016-06-09 10:22:38 -0700907 super.hide();
Robert Carr91b228092016-06-28 17:32:37 -0700908 if (mVisible) {
909 mVisible = false;
910 mAppToken.updateSurfaceViewBackgroundVisibilities();
911 }
Robert Carr086d2922016-06-09 10:22:38 -0700912 }
913
914 @Override
915 public void show() {
Robert Carr086d2922016-06-09 10:22:38 -0700916 super.show();
Robert Carr91b228092016-06-28 17:32:37 -0700917 if (!mVisible) {
918 mVisible = true;
919 mAppToken.updateSurfaceViewBackgroundVisibilities();
920 }
Robert Carr086d2922016-06-09 10:22:38 -0700921 }
922
923 @Override
924 public void destroy() {
925 super.destroy();
926 mBackgroundControl.destroy();
Robert Carr91b228092016-06-28 17:32:37 -0700927 mAppToken.removeSurfaceViewBackground(this);
928 }
Robert Carr086d2922016-06-09 10:22:38 -0700929
930 @Override
931 public void release() {
932 super.release();
933 mBackgroundControl.release();
934 }
935
936 @Override
937 public void setTransparentRegionHint(Region region) {
938 super.setTransparentRegionHint(region);
939 mBackgroundControl.setTransparentRegionHint(region);
940 }
941
942 @Override
943 public void deferTransactionUntil(IBinder handle, long frame) {
944 super.deferTransactionUntil(handle, frame);
945 mBackgroundControl.deferTransactionUntil(handle, frame);
946 }
947
Robert Carr91b228092016-06-28 17:32:37 -0700948 void updateBackgroundVisibility(boolean forcedInvisible) {
949 mAppForcedInvisible = forcedInvisible;
950 if (mOpaque && mVisible && !mAppForcedInvisible) {
Robert Carr086d2922016-06-09 10:22:38 -0700951 mBackgroundControl.show();
952 } else {
953 mBackgroundControl.hide();
954 }
955 }
956 }
Robert Carre6a83512015-11-03 16:09:21 -0800957}