1 /* 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 17 package com.android.server.wm; 18 19 import static android.os.Trace.TRACE_TAG_WINDOW_MANAGER; 20 import static com.android.server.wm.WindowManagerDebugConfig.SHOW_SURFACE_ALLOC; 21 import static com.android.server.wm.WindowManagerDebugConfig.SHOW_TRANSACTIONS; 22 import static com.android.server.wm.WindowManagerDebugConfig.SHOW_LIGHT_TRANSACTIONS; 23 import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_SURFACE_TRACE; 24 import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_VISIBILITY; 25 import static com.android.server.wm.WindowManagerDebugConfig.TAG_WITH_CLASS_NAME; 26 import static com.android.server.wm.WindowManagerDebugConfig.TAG_WM; 27 import static android.view.Surface.SCALING_MODE_SCALE_TO_WINDOW; 28 29 import android.graphics.Point; 30 import android.graphics.PointF; 31 import android.graphics.Rect; 32 import android.graphics.Region; 33 import android.os.IBinder; 34 import android.os.Debug; 35 import android.os.Trace; 36 import android.view.Surface; 37 import android.view.SurfaceControl; 38 import android.view.SurfaceSession; 39 import android.view.WindowContentFrameStats; 40 import android.view.Surface.OutOfResourcesException; 41 42 import android.util.Slog; 43 44 import java.io.FileDescriptor; 45 import java.io.PrintWriter; 46 import java.util.ArrayList; 47 48 class WindowSurfaceController { 49 static final String TAG = TAG_WITH_CLASS_NAME ? "WindowSurfaceController" : TAG_WM; 50 51 final WindowStateAnimator mAnimator; 52 53 private SurfaceControl mSurfaceControl; 54 55 // Should only be set from within setShown(). 56 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 62 // 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 68 private float mSurfaceAlpha = 0; 69 70 private int mSurfaceLayer = 0; 71 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; 76 77 // Initially a surface is hidden after just being created. 78 private boolean mHiddenForOtherReasons = true; 79 private final String title; 80 81 private final WindowManagerService mService; 82 83 private final int mWindowType; 84 private final Session mWindowSession; 85 WindowSurfaceController(SurfaceSession s, String name, int w, int h, int format, int flags, WindowStateAnimator animator, int windowType, int ownerUid)86 public WindowSurfaceController(SurfaceSession s, String name, int w, int h, int format, 87 int flags, WindowStateAnimator animator, int windowType, int ownerUid) { 88 mAnimator = animator; 89 90 mSurfaceW = w; 91 mSurfaceH = h; 92 93 title = name; 94 95 mService = animator.mService; 96 final WindowState win = animator.mWin; 97 mWindowType = windowType; 98 mWindowSession = win.mSession; 99 100 if (DEBUG_SURFACE_TRACE) { 101 mSurfaceControl = new SurfaceTrace( 102 s, name, w, h, format, flags, windowType, ownerUid); 103 } else { 104 Trace.traceBegin(TRACE_TAG_WINDOW_MANAGER, "new SurfaceControl"); 105 mSurfaceControl = new SurfaceControl( 106 s, name, w, h, format, flags, windowType, ownerUid); 107 Trace.traceEnd(TRACE_TAG_WINDOW_MANAGER); 108 } 109 110 if (mService.mRoot.mSurfaceTraceEnabled) { 111 mSurfaceControl = new RemoteSurfaceTrace( 112 mService.mRoot.mSurfaceTraceFd.getFileDescriptor(), mSurfaceControl, win); 113 } 114 } 115 installRemoteTrace(FileDescriptor fd)116 void installRemoteTrace(FileDescriptor fd) { 117 mSurfaceControl = new RemoteSurfaceTrace(fd, mSurfaceControl, mAnimator.mWin); 118 } 119 removeRemoteTrace()120 void removeRemoteTrace() { 121 mSurfaceControl = new SurfaceControl(mSurfaceControl); 122 } 123 124 logSurface(String msg, RuntimeException where)125 private void logSurface(String msg, RuntimeException where) { 126 String str = " SURFACE " + msg + ": " + title; 127 if (where != null) { 128 Slog.i(TAG, str, where); 129 } else { 130 Slog.i(TAG, str); 131 } 132 } 133 reparentChildrenInTransaction(WindowSurfaceController other)134 void reparentChildrenInTransaction(WindowSurfaceController other) { 135 if (SHOW_TRANSACTIONS) Slog.i(TAG, "REPARENT from: " + this + " to: " + other); 136 if ((mSurfaceControl != null) && (other.mSurfaceControl != null)) { 137 mSurfaceControl.reparentChildren(other.getHandle()); 138 } 139 } 140 detachChildren()141 void detachChildren() { 142 if (SHOW_TRANSACTIONS) Slog.i(TAG, "SEVER CHILDREN"); 143 if (mSurfaceControl != null) { 144 mSurfaceControl.detachChildren(); 145 } 146 } 147 hideInTransaction(String reason)148 void hideInTransaction(String reason) { 149 if (SHOW_TRANSACTIONS) logSurface("HIDE ( " + reason + " )", null); 150 mHiddenForOtherReasons = true; 151 152 mAnimator.destroyPreservedSurfaceLocked(); 153 updateVisibility(); 154 } 155 hideSurface()156 private void hideSurface() { 157 if (mSurfaceControl == null) { 158 return; 159 } 160 setShown(false); 161 try { 162 mSurfaceControl.hide(); 163 } catch (RuntimeException e) { 164 Slog.w(TAG, "Exception hiding surface in " + this); 165 } 166 } 167 destroyInTransaction()168 void destroyInTransaction() { 169 if (SHOW_TRANSACTIONS || SHOW_SURFACE_ALLOC) { 170 Slog.i(TAG, "Destroying surface " + this + " called by " + Debug.getCallers(8)); 171 } 172 try { 173 if (mSurfaceControl != null) { 174 mSurfaceControl.destroy(); 175 } 176 } catch (RuntimeException e) { 177 Slog.w(TAG, "Error destroying surface in: " + this, e); 178 } finally { 179 setShown(false); 180 mSurfaceControl = null; 181 } 182 } 183 disconnectInTransaction()184 void disconnectInTransaction() { 185 if (SHOW_TRANSACTIONS || SHOW_SURFACE_ALLOC) { 186 Slog.i(TAG, "Disconnecting client: " + this); 187 } 188 189 try { 190 if (mSurfaceControl != null) { 191 mSurfaceControl.disconnect(); 192 } 193 } catch (RuntimeException e) { 194 Slog.w(TAG, "Error disconnecting surface in: " + this, e); 195 } 196 } 197 setCropInTransaction(Rect clipRect, boolean recoveringMemory)198 void setCropInTransaction(Rect clipRect, boolean recoveringMemory) { 199 if (SHOW_TRANSACTIONS) logSurface( 200 "CROP " + clipRect.toShortString(), null); 201 try { 202 if (clipRect.width() > 0 && clipRect.height() > 0) { 203 mSurfaceControl.setWindowCrop(clipRect); 204 mHiddenForCrop = false; 205 updateVisibility(); 206 } else { 207 mHiddenForCrop = true; 208 mAnimator.destroyPreservedSurfaceLocked(); 209 updateVisibility(); 210 } 211 } catch (RuntimeException e) { 212 Slog.w(TAG, "Error setting crop surface of " + this 213 + " crop=" + clipRect.toShortString(), e); 214 if (!recoveringMemory) { 215 mAnimator.reclaimSomeSurfaceMemory("crop", true); 216 } 217 } 218 } 219 clearCropInTransaction(boolean recoveringMemory)220 void clearCropInTransaction(boolean recoveringMemory) { 221 if (SHOW_TRANSACTIONS) logSurface( 222 "CLEAR CROP", null); 223 try { 224 Rect clipRect = new Rect(0, 0, -1, -1); 225 mSurfaceControl.setWindowCrop(clipRect); 226 } catch (RuntimeException e) { 227 Slog.w(TAG, "Error setting clearing crop of " + this, e); 228 if (!recoveringMemory) { 229 mAnimator.reclaimSomeSurfaceMemory("crop", true); 230 } 231 } 232 } 233 setFinalCropInTransaction(Rect clipRect)234 void setFinalCropInTransaction(Rect clipRect) { 235 if (SHOW_TRANSACTIONS) logSurface( 236 "FINAL CROP " + clipRect.toShortString(), null); 237 try { 238 mSurfaceControl.setFinalCrop(clipRect); 239 } catch (RuntimeException e) { 240 Slog.w(TAG, "Error disconnecting surface in: " + this, e); 241 } 242 } 243 setLayer(int layer)244 void setLayer(int layer) { 245 if (mSurfaceControl != null) { 246 mService.openSurfaceTransaction(); 247 try { 248 if (mAnimator.mWin.usesRelativeZOrdering()) { 249 mSurfaceControl.setRelativeLayer( 250 mAnimator.mWin.getParentWindow() 251 .mWinAnimator.mSurfaceController.mSurfaceControl.getHandle(), 252 -1); 253 } else { 254 mSurfaceLayer = layer; 255 mSurfaceControl.setLayer(layer); 256 } 257 } finally { 258 mService.closeSurfaceTransaction(); 259 } 260 } 261 } 262 setLayerStackInTransaction(int layerStack)263 void setLayerStackInTransaction(int layerStack) { 264 if (mSurfaceControl != null) { 265 mSurfaceControl.setLayerStack(layerStack); 266 } 267 } 268 setPositionInTransaction(float left, float top, boolean recoveringMemory)269 void setPositionInTransaction(float left, float top, boolean recoveringMemory) { 270 final boolean surfaceMoved = mSurfaceX != left || mSurfaceY != top; 271 if (surfaceMoved) { 272 mSurfaceX = left; 273 mSurfaceY = top; 274 275 try { 276 if (SHOW_TRANSACTIONS) logSurface( 277 "POS (setPositionInTransaction) @ (" + left + "," + top + ")", null); 278 279 mSurfaceControl.setPosition(left, top); 280 } catch (RuntimeException e) { 281 Slog.w(TAG, "Error positioning surface of " + this 282 + " pos=(" + left + "," + top + ")", e); 283 if (!recoveringMemory) { 284 mAnimator.reclaimSomeSurfaceMemory("position", true); 285 } 286 } 287 } 288 } 289 setGeometryAppliesWithResizeInTransaction(boolean recoveringMemory)290 void setGeometryAppliesWithResizeInTransaction(boolean recoveringMemory) { 291 mSurfaceControl.setGeometryAppliesWithResize(); 292 } 293 setMatrixInTransaction(float dsdx, float dtdx, float dsdy, float dtdy, boolean recoveringMemory)294 void setMatrixInTransaction(float dsdx, float dtdx, float dsdy, float dtdy, 295 boolean recoveringMemory) { 296 final boolean matrixChanged = mLastDsdx != dsdx || mLastDtdx != dtdx || 297 mLastDsdy != dsdy || mLastDtdy != dtdy; 298 if (!matrixChanged) { 299 return; 300 } 301 302 mLastDsdx = dsdx; 303 mLastDtdx = dtdx; 304 mLastDsdy = dsdy; 305 mLastDtdy = dtdy; 306 307 try { 308 if (SHOW_TRANSACTIONS) logSurface( 309 "MATRIX [" + dsdx + "," + dtdx + "," + dsdy + "," + dtdy + "]", null); 310 mSurfaceControl.setMatrix( 311 dsdx, dtdx, dsdy, dtdy); 312 } catch (RuntimeException e) { 313 // If something goes wrong with the surface (such 314 // as running out of memory), don't take down the 315 // entire system. 316 Slog.e(TAG, "Error setting matrix on surface surface" + title 317 + " MATRIX [" + dsdx + "," + dtdx + "," + dsdy + "," + dtdy + "]", null); 318 if (!recoveringMemory) { 319 mAnimator.reclaimSomeSurfaceMemory("matrix", true); 320 } 321 } 322 } 323 setSizeInTransaction(int width, int height, boolean recoveringMemory)324 boolean setSizeInTransaction(int width, int height, boolean recoveringMemory) { 325 final boolean surfaceResized = mSurfaceW != width || mSurfaceH != height; 326 if (surfaceResized) { 327 mSurfaceW = width; 328 mSurfaceH = height; 329 330 try { 331 if (SHOW_TRANSACTIONS) logSurface( 332 "SIZE " + width + "x" + height, null); 333 mSurfaceControl.setSize(width, height); 334 } catch (RuntimeException e) { 335 // If something goes wrong with the surface (such 336 // as running out of memory), don't take down the 337 // entire system. 338 Slog.e(TAG, "Error resizing surface of " + title 339 + " size=(" + width + "x" + height + ")", e); 340 if (!recoveringMemory) { 341 mAnimator.reclaimSomeSurfaceMemory("size", true); 342 } 343 return false; 344 } 345 return true; 346 } 347 return false; 348 } 349 prepareToShowInTransaction(float alpha, float dsdx, float dtdx, float dsdy, float dtdy, boolean recoveringMemory)350 boolean prepareToShowInTransaction(float alpha, 351 float dsdx, float dtdx, float dsdy, 352 float dtdy, boolean recoveringMemory) { 353 if (mSurfaceControl != null) { 354 try { 355 mSurfaceAlpha = alpha; 356 mSurfaceControl.setAlpha(alpha); 357 mLastDsdx = dsdx; 358 mLastDtdx = dtdx; 359 mLastDsdy = dsdy; 360 mLastDtdy = dtdy; 361 mSurfaceControl.setMatrix( 362 dsdx, dtdx, dsdy, dtdy); 363 } catch (RuntimeException e) { 364 Slog.w(TAG, "Error updating surface in " + title, e); 365 if (!recoveringMemory) { 366 mAnimator.reclaimSomeSurfaceMemory("update", true); 367 } 368 return false; 369 } 370 } 371 return true; 372 } 373 setTransparentRegionHint(final Region region)374 void setTransparentRegionHint(final Region region) { 375 if (mSurfaceControl == null) { 376 Slog.w(TAG, "setTransparentRegionHint: null mSurface after mHasSurface true"); 377 return; 378 } 379 if (SHOW_LIGHT_TRANSACTIONS) Slog.i(TAG, ">>> OPEN TRANSACTION setTransparentRegion"); 380 mService.openSurfaceTransaction(); 381 try { 382 mSurfaceControl.setTransparentRegionHint(region); 383 } finally { 384 mService.closeSurfaceTransaction(); 385 if (SHOW_LIGHT_TRANSACTIONS) Slog.i(TAG, 386 "<<< CLOSE TRANSACTION setTransparentRegion"); 387 } 388 } 389 setOpaque(boolean isOpaque)390 void setOpaque(boolean isOpaque) { 391 if (SHOW_TRANSACTIONS) logSurface("isOpaque=" + isOpaque, 392 null); 393 394 if (mSurfaceControl == null) { 395 return; 396 } 397 if (SHOW_LIGHT_TRANSACTIONS) Slog.i(TAG, ">>> OPEN TRANSACTION setOpaqueLocked"); 398 mService.openSurfaceTransaction(); 399 try { 400 mSurfaceControl.setOpaque(isOpaque); 401 } finally { 402 mService.closeSurfaceTransaction(); 403 if (SHOW_LIGHT_TRANSACTIONS) Slog.i(TAG, "<<< CLOSE TRANSACTION setOpaqueLocked"); 404 } 405 } 406 setSecure(boolean isSecure)407 void setSecure(boolean isSecure) { 408 if (SHOW_TRANSACTIONS) logSurface("isSecure=" + isSecure, 409 null); 410 411 if (mSurfaceControl == null) { 412 return; 413 } 414 if (SHOW_LIGHT_TRANSACTIONS) Slog.i(TAG, ">>> OPEN TRANSACTION setSecureLocked"); 415 mService.openSurfaceTransaction(); 416 try { 417 mSurfaceControl.setSecure(isSecure); 418 } finally { 419 mService.closeSurfaceTransaction(); 420 if (SHOW_LIGHT_TRANSACTIONS) Slog.i(TAG, "<<< CLOSE TRANSACTION setSecureLocked"); 421 } 422 } 423 showRobustlyInTransaction()424 boolean showRobustlyInTransaction() { 425 if (SHOW_TRANSACTIONS) logSurface( 426 "SHOW (performLayout)", null); 427 if (DEBUG_VISIBILITY) Slog.v(TAG, "Showing " + this 428 + " during relayout"); 429 mHiddenForOtherReasons = false; 430 return updateVisibility(); 431 } 432 updateVisibility()433 private boolean updateVisibility() { 434 if (mHiddenForCrop || mHiddenForOtherReasons) { 435 if (mSurfaceShown) { 436 hideSurface(); 437 } 438 return false; 439 } else { 440 if (!mSurfaceShown) { 441 return showSurface(); 442 } else { 443 return true; 444 } 445 } 446 } 447 showSurface()448 private boolean showSurface() { 449 try { 450 setShown(true); 451 mSurfaceControl.show(); 452 return true; 453 } catch (RuntimeException e) { 454 Slog.w(TAG, "Failure showing surface " + mSurfaceControl + " in " + this, e); 455 } 456 457 mAnimator.reclaimSomeSurfaceMemory("show", true); 458 459 return false; 460 } 461 deferTransactionUntil(IBinder handle, long frame)462 void deferTransactionUntil(IBinder handle, long frame) { 463 // TODO: Logging 464 mSurfaceControl.deferTransactionUntil(handle, frame); 465 } 466 forceScaleableInTransaction(boolean force)467 void forceScaleableInTransaction(boolean force) { 468 // -1 means we don't override the default or client specified 469 // scaling mode. 470 int scalingMode = force ? SCALING_MODE_SCALE_TO_WINDOW : -1; 471 mSurfaceControl.setOverrideScalingMode(scalingMode); 472 } 473 clearWindowContentFrameStats()474 boolean clearWindowContentFrameStats() { 475 if (mSurfaceControl == null) { 476 return false; 477 } 478 return mSurfaceControl.clearContentFrameStats(); 479 } 480 getWindowContentFrameStats(WindowContentFrameStats outStats)481 boolean getWindowContentFrameStats(WindowContentFrameStats outStats) { 482 if (mSurfaceControl == null) { 483 return false; 484 } 485 return mSurfaceControl.getContentFrameStats(outStats); 486 } 487 488 hasSurface()489 boolean hasSurface() { 490 return mSurfaceControl != null; 491 } 492 getHandle()493 IBinder getHandle() { 494 if (mSurfaceControl == null) { 495 return null; 496 } 497 return mSurfaceControl.getHandle(); 498 } 499 getSurface(Surface outSurface)500 void getSurface(Surface outSurface) { 501 outSurface.copyFrom(mSurfaceControl); 502 } 503 getLayer()504 int getLayer() { 505 return mSurfaceLayer; 506 } 507 getShown()508 boolean getShown() { 509 return mSurfaceShown; 510 } 511 setShown(boolean surfaceShown)512 void setShown(boolean surfaceShown) { 513 mSurfaceShown = surfaceShown; 514 515 mService.updateNonSystemOverlayWindowsVisibilityIfNeeded(mAnimator.mWin, surfaceShown); 516 517 if (mWindowSession != null) { 518 mWindowSession.onWindowSurfaceVisibilityChanged(this, mSurfaceShown, mWindowType); 519 } 520 } 521 getX()522 float getX() { 523 return mSurfaceX; 524 } 525 getY()526 float getY() { 527 return mSurfaceY; 528 } 529 getWidth()530 float getWidth() { 531 return mSurfaceW; 532 } 533 getHeight()534 float getHeight() { 535 return mSurfaceH; 536 } 537 538 dump(PrintWriter pw, String prefix, boolean dumpAll)539 public void dump(PrintWriter pw, String prefix, boolean dumpAll) { 540 if (dumpAll) { 541 pw.print(prefix); pw.print("mSurface="); pw.println(mSurfaceControl); 542 } 543 pw.print(prefix); pw.print("Surface: shown="); pw.print(mSurfaceShown); 544 pw.print(" layer="); pw.print(mSurfaceLayer); 545 pw.print(" alpha="); pw.print(mSurfaceAlpha); 546 pw.print(" rect=("); pw.print(mSurfaceX); 547 pw.print(","); pw.print(mSurfaceY); 548 pw.print(") "); pw.print(mSurfaceW); 549 pw.print(" x "); pw.print(mSurfaceH); 550 pw.print(" transform=("); pw.print(mLastDsdx); pw.print(", "); 551 pw.print(mLastDtdx); pw.print(", "); pw.print(mLastDsdy); 552 pw.print(", "); pw.print(mLastDtdy); pw.println(")"); 553 } 554 555 @Override toString()556 public String toString() { 557 return mSurfaceControl.toString(); 558 } 559 560 static class SurfaceTrace extends SurfaceControl { 561 private final static String SURFACE_TAG = TAG_WITH_CLASS_NAME ? "SurfaceTrace" : TAG_WM; 562 private final static boolean LOG_SURFACE_TRACE = DEBUG_SURFACE_TRACE; 563 final static ArrayList<SurfaceTrace> sSurfaces = new ArrayList<SurfaceTrace>(); 564 565 private float mSurfaceTraceAlpha = 0; 566 private int mLayer; 567 private final PointF mPosition = new PointF(); 568 private final Point mSize = new Point(); 569 private final Rect mWindowCrop = new Rect(); 570 private final Rect mFinalCrop = new Rect(); 571 private boolean mShown = false; 572 private int mLayerStack; 573 private boolean mIsOpaque; 574 private float mDsdx, mDtdx, mDsdy, mDtdy; 575 private final String mName; 576 SurfaceTrace(SurfaceSession s, String name, int w, int h, int format, int flags, int windowType, int ownerUid)577 public SurfaceTrace(SurfaceSession s, String name, int w, int h, int format, int flags, 578 int windowType, int ownerUid) 579 throws OutOfResourcesException { 580 super(s, name, w, h, format, flags, windowType, ownerUid); 581 mName = name != null ? name : "Not named"; 582 mSize.set(w, h); 583 if (LOG_SURFACE_TRACE) Slog.v(SURFACE_TAG, "ctor: " + this + ". Called by " 584 + Debug.getCallers(3)); 585 synchronized (sSurfaces) { 586 sSurfaces.add(0, this); 587 } 588 } 589 SurfaceTrace(SurfaceSession s, String name, int w, int h, int format, int flags)590 public SurfaceTrace(SurfaceSession s, 591 String name, int w, int h, int format, int flags) { 592 super(s, name, w, h, format, flags); 593 mName = name != null ? name : "Not named"; 594 mSize.set(w, h); 595 if (LOG_SURFACE_TRACE) Slog.v(SURFACE_TAG, "ctor: " + this + ". Called by " 596 + Debug.getCallers(3)); 597 synchronized (sSurfaces) { 598 sSurfaces.add(0, this); 599 } 600 } 601 602 @Override setAlpha(float alpha)603 public void setAlpha(float alpha) { 604 if (mSurfaceTraceAlpha != alpha) { 605 if (LOG_SURFACE_TRACE) Slog.v(SURFACE_TAG, "setAlpha(" + alpha + "): OLD:" + this + 606 ". Called by " + Debug.getCallers(3)); 607 mSurfaceTraceAlpha = alpha; 608 } 609 super.setAlpha(alpha); 610 } 611 612 @Override setLayer(int zorder)613 public void setLayer(int zorder) { 614 if (zorder != mLayer) { 615 if (LOG_SURFACE_TRACE) Slog.v(SURFACE_TAG, "setLayer(" + zorder + "): OLD:" + this 616 + ". Called by " + Debug.getCallers(3)); 617 mLayer = zorder; 618 } 619 super.setLayer(zorder); 620 621 synchronized (sSurfaces) { 622 sSurfaces.remove(this); 623 int i; 624 for (i = sSurfaces.size() - 1; i >= 0; i--) { 625 SurfaceTrace s = sSurfaces.get(i); 626 if (s.mLayer < zorder) { 627 break; 628 } 629 } 630 sSurfaces.add(i + 1, this); 631 } 632 } 633 634 @Override setPosition(float x, float y)635 public void setPosition(float x, float y) { 636 if (x != mPosition.x || y != mPosition.y) { 637 if (LOG_SURFACE_TRACE) Slog.v(SURFACE_TAG, "setPosition(" + x + "," + y + "): OLD:" 638 + this + ". Called by " + Debug.getCallers(3)); 639 mPosition.set(x, y); 640 } 641 super.setPosition(x, y); 642 } 643 644 @Override setGeometryAppliesWithResize()645 public void setGeometryAppliesWithResize() { 646 if (LOG_SURFACE_TRACE) Slog.v(SURFACE_TAG, "setGeometryAppliesWithResize(): OLD: " 647 + this + ". Called by" + Debug.getCallers(3)); 648 super.setGeometryAppliesWithResize(); 649 } 650 651 @Override setSize(int w, int h)652 public void setSize(int w, int h) { 653 if (w != mSize.x || h != mSize.y) { 654 if (LOG_SURFACE_TRACE) Slog.v(SURFACE_TAG, "setSize(" + w + "," + h + "): OLD:" 655 + this + ". Called by " + Debug.getCallers(3)); 656 mSize.set(w, h); 657 } 658 super.setSize(w, h); 659 } 660 661 @Override setWindowCrop(Rect crop)662 public void setWindowCrop(Rect crop) { 663 if (crop != null) { 664 if (!crop.equals(mWindowCrop)) { 665 if (LOG_SURFACE_TRACE) Slog.v(SURFACE_TAG, "setWindowCrop(" 666 + crop.toShortString() + "): OLD:" + this + ". Called by " 667 + Debug.getCallers(3)); 668 mWindowCrop.set(crop); 669 } 670 } 671 super.setWindowCrop(crop); 672 } 673 674 @Override setFinalCrop(Rect crop)675 public void setFinalCrop(Rect crop) { 676 if (crop != null) { 677 if (!crop.equals(mFinalCrop)) { 678 if (LOG_SURFACE_TRACE) Slog.v(SURFACE_TAG, "setFinalCrop(" 679 + crop.toShortString() + "): OLD:" + this + ". Called by " 680 + Debug.getCallers(3)); 681 mFinalCrop.set(crop); 682 } 683 } 684 super.setFinalCrop(crop); 685 } 686 687 @Override setLayerStack(int layerStack)688 public void setLayerStack(int layerStack) { 689 if (layerStack != mLayerStack) { 690 if (LOG_SURFACE_TRACE) Slog.v(SURFACE_TAG, "setLayerStack(" + layerStack + "): OLD:" 691 + this + ". Called by " + Debug.getCallers(3)); 692 mLayerStack = layerStack; 693 } 694 super.setLayerStack(layerStack); 695 } 696 697 @Override setOpaque(boolean isOpaque)698 public void setOpaque(boolean isOpaque) { 699 if (isOpaque != mIsOpaque) { 700 if (LOG_SURFACE_TRACE) Slog.v(SURFACE_TAG, "setOpaque(" + isOpaque + "): OLD:" 701 + this + ". Called by " + Debug.getCallers(3)); 702 mIsOpaque = isOpaque; 703 } 704 super.setOpaque(isOpaque); 705 } 706 707 @Override setSecure(boolean isSecure)708 public void setSecure(boolean isSecure) { 709 super.setSecure(isSecure); 710 } 711 712 @Override setMatrix(float dsdx, float dtdx, float dsdy, float dtdy)713 public void setMatrix(float dsdx, float dtdx, float dsdy, float dtdy) { 714 if (dsdx != mDsdx || dtdx != mDtdx || dsdy != mDsdy || dtdy != mDtdy) { 715 if (LOG_SURFACE_TRACE) Slog.v(SURFACE_TAG, "setMatrix(" + dsdx + "," + dtdx + "," 716 + dsdy + "," + dtdy + "): OLD:" + this + ". Called by " 717 + Debug.getCallers(3)); 718 mDsdx = dsdx; 719 mDtdx = dtdx; 720 mDsdy = dsdy; 721 mDtdy = dtdy; 722 } 723 super.setMatrix(dsdx, dtdx, dsdy, dtdy); 724 } 725 726 @Override hide()727 public void hide() { 728 if (mShown) { 729 if (LOG_SURFACE_TRACE) Slog.v(SURFACE_TAG, "hide: OLD:" + this + ". Called by " 730 + Debug.getCallers(3)); 731 mShown = false; 732 } 733 super.hide(); 734 } 735 736 @Override show()737 public void show() { 738 if (!mShown) { 739 if (LOG_SURFACE_TRACE) Slog.v(SURFACE_TAG, "show: OLD:" + this + ". Called by " 740 + Debug.getCallers(3)); 741 mShown = true; 742 } 743 super.show(); 744 } 745 746 @Override destroy()747 public void destroy() { 748 super.destroy(); 749 if (LOG_SURFACE_TRACE) Slog.v(SURFACE_TAG, "destroy: " + this + ". Called by " 750 + Debug.getCallers(3)); 751 synchronized (sSurfaces) { 752 sSurfaces.remove(this); 753 } 754 } 755 756 @Override release()757 public void release() { 758 super.release(); 759 if (LOG_SURFACE_TRACE) Slog.v(SURFACE_TAG, "release: " + this + ". Called by " 760 + Debug.getCallers(3)); 761 synchronized (sSurfaces) { 762 sSurfaces.remove(this); 763 } 764 } 765 766 @Override setTransparentRegionHint(Region region)767 public void setTransparentRegionHint(Region region) { 768 if (LOG_SURFACE_TRACE) Slog.v(SURFACE_TAG, "setTransparentRegionHint(" + region 769 + "): OLD: " + this + " . Called by " + Debug.getCallers(3)); 770 super.setTransparentRegionHint(region); 771 } 772 dumpAllSurfaces(PrintWriter pw, String header)773 static void dumpAllSurfaces(PrintWriter pw, String header) { 774 synchronized (sSurfaces) { 775 final int N = sSurfaces.size(); 776 if (N <= 0) { 777 return; 778 } 779 if (header != null) { 780 pw.println(header); 781 } 782 pw.println("WINDOW MANAGER SURFACES (dumpsys window surfaces)"); 783 for (int i = 0; i < N; i++) { 784 SurfaceTrace s = sSurfaces.get(i); 785 pw.print(" Surface #"); pw.print(i); pw.print(": #"); 786 pw.print(Integer.toHexString(System.identityHashCode(s))); 787 pw.print(" "); pw.println(s.mName); 788 pw.print(" mLayerStack="); pw.print(s.mLayerStack); 789 pw.print(" mLayer="); pw.println(s.mLayer); 790 pw.print(" mShown="); pw.print(s.mShown); pw.print(" mAlpha="); 791 pw.print(s.mSurfaceTraceAlpha); pw.print(" mIsOpaque="); 792 pw.println(s.mIsOpaque); 793 pw.print(" mPosition="); pw.print(s.mPosition.x); pw.print(","); 794 pw.print(s.mPosition.y); 795 pw.print(" mSize="); pw.print(s.mSize.x); pw.print("x"); 796 pw.println(s.mSize.y); 797 pw.print(" mCrop="); s.mWindowCrop.printShortString(pw); pw.println(); 798 pw.print(" mFinalCrop="); s.mFinalCrop.printShortString(pw); pw.println(); 799 pw.print(" Transform: ("); pw.print(s.mDsdx); pw.print(", "); 800 pw.print(s.mDtdx); pw.print(", "); pw.print(s.mDsdy); 801 pw.print(", "); pw.print(s.mDtdy); pw.println(")"); 802 } 803 } 804 } 805 806 @Override toString()807 public String toString() { 808 return "Surface " + Integer.toHexString(System.identityHashCode(this)) + " " 809 + mName + " (" + mLayerStack + "): shown=" + mShown + " layer=" + mLayer 810 + " alpha=" + mSurfaceTraceAlpha + " " + mPosition.x + "," + mPosition.y 811 + " " + mSize.x + "x" + mSize.y 812 + " crop=" + mWindowCrop.toShortString() 813 + " opaque=" + mIsOpaque 814 + " (" + mDsdx + "," + mDtdx + "," + mDsdy + "," + mDtdy + ")"; 815 } 816 } 817 } 818