Robert Carr | e6a8351 | 2015-11-03 16:09:21 -0800 | [diff] [blame] | 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 | |
Wale Ogunwale | 4958ad2 | 2017-06-22 09:08:14 -0700 | [diff] [blame] | 19 | import static android.os.Trace.TRACE_TAG_WINDOW_MANAGER; |
Steven Timotius | af03df6 | 2017-07-18 16:56:43 -0700 | [diff] [blame] | 20 | import static android.view.Surface.SCALING_MODE_SCALE_TO_WINDOW; |
| 21 | |
Wale Ogunwale | 722ff89 | 2016-02-18 13:37:55 -0800 | [diff] [blame] | 22 | import static com.android.server.wm.WindowManagerDebugConfig.SHOW_SURFACE_ALLOC; |
Filip Gruszczynski | 0bd180d | 2015-12-07 15:43:52 -0800 | [diff] [blame] | 23 | import static com.android.server.wm.WindowManagerDebugConfig.SHOW_TRANSACTIONS; |
| 24 | import static com.android.server.wm.WindowManagerDebugConfig.SHOW_LIGHT_TRANSACTIONS; |
Filip Gruszczynski | 0bd180d | 2015-12-07 15:43:52 -0800 | [diff] [blame] | 25 | import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_VISIBILITY; |
| 26 | import static com.android.server.wm.WindowManagerDebugConfig.TAG_WITH_CLASS_NAME; |
| 27 | import static com.android.server.wm.WindowManagerDebugConfig.TAG_WM; |
Yi Jin | 6c6e9ca | 2018-03-20 16:53:35 -0700 | [diff] [blame] | 28 | import static com.android.server.wm.WindowSurfaceControllerProto.LAYER; |
| 29 | import static com.android.server.wm.WindowSurfaceControllerProto.SHOWN; |
Robert Carr | e6a8351 | 2015-11-03 16:09:21 -0800 | [diff] [blame] | 30 | |
Robert Carr | e6a8351 | 2015-11-03 16:09:21 -0800 | [diff] [blame] | 31 | import android.graphics.Point; |
| 32 | import android.graphics.PointF; |
| 33 | import android.graphics.Rect; |
| 34 | import android.graphics.Region; |
| 35 | import android.os.IBinder; |
| 36 | import android.os.Debug; |
Wale Ogunwale | 4958ad2 | 2017-06-22 09:08:14 -0700 | [diff] [blame] | 37 | import android.os.Trace; |
Steven Timotius | af03df6 | 2017-07-18 16:56:43 -0700 | [diff] [blame] | 38 | import android.util.proto.ProtoOutputStream; |
Robert Carr | e6a8351 | 2015-11-03 16:09:21 -0800 | [diff] [blame] | 39 | import android.view.Surface; |
| 40 | import android.view.SurfaceControl; |
| 41 | import android.view.SurfaceSession; |
| 42 | import android.view.WindowContentFrameStats; |
| 43 | import android.view.Surface.OutOfResourcesException; |
| 44 | |
| 45 | import android.util.Slog; |
| 46 | |
Robert Carr | 3b71624 | 2016-08-16 16:02:21 -0700 | [diff] [blame] | 47 | import java.io.FileDescriptor; |
Robert Carr | e6a8351 | 2015-11-03 16:09:21 -0800 | [diff] [blame] | 48 | import java.io.PrintWriter; |
| 49 | import java.util.ArrayList; |
| 50 | |
| 51 | class WindowSurfaceController { |
Filip Gruszczynski | 0bd180d | 2015-12-07 15:43:52 -0800 | [diff] [blame] | 52 | static final String TAG = TAG_WITH_CLASS_NAME ? "WindowSurfaceController" : TAG_WM; |
Robert Carr | e6a8351 | 2015-11-03 16:09:21 -0800 | [diff] [blame] | 53 | |
| 54 | final WindowStateAnimator mAnimator; |
| 55 | |
Adrian Roos | 4d18a2e | 2017-12-19 19:08:05 +0100 | [diff] [blame] | 56 | SurfaceControl mSurfaceControl; |
Robert Carr | e6a8351 | 2015-11-03 16:09:21 -0800 | [diff] [blame] | 57 | |
Wale Ogunwale | 943002b | 2017-02-15 19:34:01 -0800 | [diff] [blame] | 58 | // Should only be set from within setShown(). |
Robert Carr | e6a8351 | 2015-11-03 16:09:21 -0800 | [diff] [blame] | 59 | private boolean mSurfaceShown = false; |
| 60 | private float mSurfaceX = 0; |
| 61 | private float mSurfaceY = 0; |
Jorim Jaggi | a5e1057 | 2017-11-15 14:36:26 +0100 | [diff] [blame] | 62 | private int mSurfaceW = 0; |
| 63 | private int mSurfaceH = 0; |
Robert Carr | e6a8351 | 2015-11-03 16:09:21 -0800 | [diff] [blame] | 64 | |
Matthew Bouyack | 8dd88f6 | 2016-10-24 14:01:26 -0700 | [diff] [blame] | 65 | // Initialize to the identity matrix. |
| 66 | private float mLastDsdx = 1; |
| 67 | private float mLastDtdx = 0; |
| 68 | private float mLastDsdy = 0; |
| 69 | private float mLastDtdy = 1; |
| 70 | |
Robert Carr | e6a8351 | 2015-11-03 16:09:21 -0800 | [diff] [blame] | 71 | private float mSurfaceAlpha = 0; |
| 72 | |
| 73 | private int mSurfaceLayer = 0; |
Robert Carr | e6a8351 | 2015-11-03 16:09:21 -0800 | [diff] [blame] | 74 | |
| 75 | // Surface flinger doesn't support crop rectangles where width or height is non-positive. |
| 76 | // However, we need to somehow handle the situation where the cropping would completely hide |
| 77 | // the window. We achieve this by explicitly hiding the surface and not letting it be shown. |
| 78 | private boolean mHiddenForCrop = false; |
Jorim Jaggi | 1eb39b0 | 2016-02-25 13:36:14 -0500 | [diff] [blame] | 79 | |
| 80 | // Initially a surface is hidden after just being created. |
| 81 | private boolean mHiddenForOtherReasons = true; |
Robert Carr | e6a8351 | 2015-11-03 16:09:21 -0800 | [diff] [blame] | 82 | private final String title; |
| 83 | |
Robert Carr | 68e5c9e | 2016-09-14 10:50:09 -0700 | [diff] [blame] | 84 | private final WindowManagerService mService; |
| 85 | |
Wale Ogunwale | 943002b | 2017-02-15 19:34:01 -0800 | [diff] [blame] | 86 | private final int mWindowType; |
| 87 | private final Session mWindowSession; |
| 88 | |
Chavi Weingarten | b736e32 | 2018-02-23 00:27:54 +0000 | [diff] [blame] | 89 | private final SurfaceControl.Transaction mTmpTransaction = new SurfaceControl.Transaction(); |
| 90 | |
Albert Chaulk | 3bf2e57 | 2016-11-22 13:59:19 -0500 | [diff] [blame] | 91 | public WindowSurfaceController(SurfaceSession s, String name, int w, int h, int format, |
| 92 | int flags, WindowStateAnimator animator, int windowType, int ownerUid) { |
Robert Carr | e6a8351 | 2015-11-03 16:09:21 -0800 | [diff] [blame] | 93 | mAnimator = animator; |
| 94 | |
| 95 | mSurfaceW = w; |
| 96 | mSurfaceH = h; |
Robert Carr | e6a8351 | 2015-11-03 16:09:21 -0800 | [diff] [blame] | 97 | |
| 98 | title = name; |
| 99 | |
Robert Carr | 68e5c9e | 2016-09-14 10:50:09 -0700 | [diff] [blame] | 100 | mService = animator.mService; |
Wale Ogunwale | 943002b | 2017-02-15 19:34:01 -0800 | [diff] [blame] | 101 | final WindowState win = animator.mWin; |
| 102 | mWindowType = windowType; |
| 103 | mWindowSession = win.mSession; |
Robert Carr | 68e5c9e | 2016-09-14 10:50:09 -0700 | [diff] [blame] | 104 | |
Andrii Kulian | 283acd2 | 2017-08-03 04:03:51 -0700 | [diff] [blame] | 105 | Trace.traceBegin(TRACE_TAG_WINDOW_MANAGER, "new SurfaceControl"); |
Robert Carr | b1579c8 | 2017-09-05 14:54:47 -0700 | [diff] [blame] | 106 | final SurfaceControl.Builder b = win.makeSurface() |
| 107 | .setParent(win.getSurfaceControl()) |
Robert Carr | e625fcf | 2017-09-01 12:36:28 -0700 | [diff] [blame] | 108 | .setName(name) |
| 109 | .setSize(w, h) |
| 110 | .setFormat(format) |
| 111 | .setFlags(flags) |
| 112 | .setMetadata(windowType, ownerUid); |
Adrian Roos | 4d18a2e | 2017-12-19 19:08:05 +0100 | [diff] [blame] | 113 | mSurfaceControl = b.build(); |
Andrii Kulian | 283acd2 | 2017-08-03 04:03:51 -0700 | [diff] [blame] | 114 | Trace.traceEnd(TRACE_TAG_WINDOW_MANAGER); |
Robert Carr | 3b71624 | 2016-08-16 16:02:21 -0700 | [diff] [blame] | 115 | } |
| 116 | |
Wale Ogunwale | e05f501 | 2016-09-16 16:27:29 -0700 | [diff] [blame] | 117 | private void logSurface(String msg, RuntimeException where) { |
Robert Carr | e6a8351 | 2015-11-03 16:09:21 -0800 | [diff] [blame] | 118 | String str = " SURFACE " + msg + ": " + title; |
| 119 | if (where != null) { |
| 120 | Slog.i(TAG, str, where); |
| 121 | } else { |
| 122 | Slog.i(TAG, str); |
| 123 | } |
| 124 | } |
| 125 | |
Robert Carr | d5c7dd6 | 2017-03-08 10:39:30 -0800 | [diff] [blame] | 126 | void reparentChildrenInTransaction(WindowSurfaceController other) { |
| 127 | if (SHOW_TRANSACTIONS) Slog.i(TAG, "REPARENT from: " + this + " to: " + other); |
| 128 | if ((mSurfaceControl != null) && (other.mSurfaceControl != null)) { |
| 129 | mSurfaceControl.reparentChildren(other.getHandle()); |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | void detachChildren() { |
| 134 | if (SHOW_TRANSACTIONS) Slog.i(TAG, "SEVER CHILDREN"); |
| 135 | if (mSurfaceControl != null) { |
| 136 | mSurfaceControl.detachChildren(); |
| 137 | } |
| 138 | } |
| 139 | |
Chavi Weingarten | b736e32 | 2018-02-23 00:27:54 +0000 | [diff] [blame] | 140 | void hide(SurfaceControl.Transaction transaction, String reason) { |
Filip Gruszczynski | 63a35e2 | 2015-11-05 15:38:59 -0800 | [diff] [blame] | 141 | if (SHOW_TRANSACTIONS) logSurface("HIDE ( " + reason + " )", null); |
Jorim Jaggi | 5e6968d | 2016-02-19 18:02:13 -0800 | [diff] [blame] | 142 | mHiddenForOtherReasons = true; |
Chong Zhang | 3cc58dd | 2016-04-20 17:45:24 -0700 | [diff] [blame] | 143 | |
| 144 | mAnimator.destroyPreservedSurfaceLocked(); |
Chavi Weingarten | b736e32 | 2018-02-23 00:27:54 +0000 | [diff] [blame] | 145 | if (mSurfaceShown) { |
| 146 | hideSurface(transaction); |
| 147 | } |
Jorim Jaggi | 5e6968d | 2016-02-19 18:02:13 -0800 | [diff] [blame] | 148 | } |
| 149 | |
Chavi Weingarten | b736e32 | 2018-02-23 00:27:54 +0000 | [diff] [blame] | 150 | private void hideSurface(SurfaceControl.Transaction transaction) { |
Wale Ogunwale | 943002b | 2017-02-15 19:34:01 -0800 | [diff] [blame] | 151 | if (mSurfaceControl == null) { |
| 152 | return; |
| 153 | } |
| 154 | setShown(false); |
| 155 | try { |
Chavi Weingarten | b736e32 | 2018-02-23 00:27:54 +0000 | [diff] [blame] | 156 | transaction.hide(mSurfaceControl); |
Wale Ogunwale | 943002b | 2017-02-15 19:34:01 -0800 | [diff] [blame] | 157 | } catch (RuntimeException e) { |
| 158 | Slog.w(TAG, "Exception hiding surface in " + this); |
Robert Carr | e6a8351 | 2015-11-03 16:09:21 -0800 | [diff] [blame] | 159 | } |
| 160 | } |
| 161 | |
Robert Carr | a882886 | 2018-02-05 16:17:36 -0800 | [diff] [blame] | 162 | void destroyNotInTransaction() { |
Chong Zhang | ad23b3f | 2016-08-31 16:05:27 -0700 | [diff] [blame] | 163 | if (SHOW_TRANSACTIONS || SHOW_SURFACE_ALLOC) { |
| 164 | Slog.i(TAG, "Destroying surface " + this + " called by " + Debug.getCallers(8)); |
| 165 | } |
Robert Carr | e6a8351 | 2015-11-03 16:09:21 -0800 | [diff] [blame] | 166 | try { |
Wale Ogunwale | 722ff89 | 2016-02-18 13:37:55 -0800 | [diff] [blame] | 167 | if (mSurfaceControl != null) { |
| 168 | mSurfaceControl.destroy(); |
| 169 | } |
Robert Carr | e6a8351 | 2015-11-03 16:09:21 -0800 | [diff] [blame] | 170 | } catch (RuntimeException e) { |
| 171 | Slog.w(TAG, "Error destroying surface in: " + this, e); |
Wale Ogunwale | 722ff89 | 2016-02-18 13:37:55 -0800 | [diff] [blame] | 172 | } finally { |
Wale Ogunwale | 943002b | 2017-02-15 19:34:01 -0800 | [diff] [blame] | 173 | setShown(false); |
Wale Ogunwale | 722ff89 | 2016-02-18 13:37:55 -0800 | [diff] [blame] | 174 | mSurfaceControl = null; |
Robert Carr | e6a8351 | 2015-11-03 16:09:21 -0800 | [diff] [blame] | 175 | } |
| 176 | } |
| 177 | |
Chong Zhang | 47e36a3 | 2016-02-29 16:44:33 -0800 | [diff] [blame] | 178 | void disconnectInTransaction() { |
| 179 | if (SHOW_TRANSACTIONS || SHOW_SURFACE_ALLOC) { |
| 180 | Slog.i(TAG, "Disconnecting client: " + this); |
| 181 | } |
| 182 | |
| 183 | try { |
| 184 | if (mSurfaceControl != null) { |
| 185 | mSurfaceControl.disconnect(); |
| 186 | } |
| 187 | } catch (RuntimeException e) { |
| 188 | Slog.w(TAG, "Error disconnecting surface in: " + this, e); |
| 189 | } |
| 190 | } |
| 191 | |
Robert Carr | e6a8351 | 2015-11-03 16:09:21 -0800 | [diff] [blame] | 192 | void setCropInTransaction(Rect clipRect, boolean recoveringMemory) { |
| 193 | if (SHOW_TRANSACTIONS) logSurface( |
| 194 | "CROP " + clipRect.toShortString(), null); |
| 195 | try { |
Robert Carr | 4320d33 | 2016-06-10 15:13:32 -0700 | [diff] [blame] | 196 | if (clipRect.width() > 0 && clipRect.height() > 0) { |
Robert Carr | e6a8351 | 2015-11-03 16:09:21 -0800 | [diff] [blame] | 197 | mSurfaceControl.setWindowCrop(clipRect); |
| 198 | mHiddenForCrop = false; |
Jorim Jaggi | 5e6968d | 2016-02-19 18:02:13 -0800 | [diff] [blame] | 199 | updateVisibility(); |
Robert Carr | e6a8351 | 2015-11-03 16:09:21 -0800 | [diff] [blame] | 200 | } else { |
Robert Carr | e6a8351 | 2015-11-03 16:09:21 -0800 | [diff] [blame] | 201 | mHiddenForCrop = true; |
Chong Zhang | 3cc58dd | 2016-04-20 17:45:24 -0700 | [diff] [blame] | 202 | mAnimator.destroyPreservedSurfaceLocked(); |
Jorim Jaggi | 5e6968d | 2016-02-19 18:02:13 -0800 | [diff] [blame] | 203 | updateVisibility(); |
Robert Carr | e6a8351 | 2015-11-03 16:09:21 -0800 | [diff] [blame] | 204 | } |
| 205 | } catch (RuntimeException e) { |
| 206 | Slog.w(TAG, "Error setting crop surface of " + this |
| 207 | + " crop=" + clipRect.toShortString(), e); |
| 208 | if (!recoveringMemory) { |
| 209 | mAnimator.reclaimSomeSurfaceMemory("crop", true); |
| 210 | } |
| 211 | } |
| 212 | } |
| 213 | |
Robert Carr | 4320d33 | 2016-06-10 15:13:32 -0700 | [diff] [blame] | 214 | void clearCropInTransaction(boolean recoveringMemory) { |
| 215 | if (SHOW_TRANSACTIONS) logSurface( |
| 216 | "CLEAR CROP", null); |
| 217 | try { |
| 218 | Rect clipRect = new Rect(0, 0, -1, -1); |
| 219 | mSurfaceControl.setWindowCrop(clipRect); |
| 220 | } catch (RuntimeException e) { |
| 221 | Slog.w(TAG, "Error setting clearing crop of " + this, e); |
| 222 | if (!recoveringMemory) { |
| 223 | mAnimator.reclaimSomeSurfaceMemory("crop", true); |
| 224 | } |
| 225 | } |
| 226 | } |
| 227 | |
Jorim Jaggi | 6a7c90a | 2016-03-11 15:04:59 +0100 | [diff] [blame] | 228 | void setFinalCropInTransaction(Rect clipRect) { |
| 229 | if (SHOW_TRANSACTIONS) logSurface( |
| 230 | "FINAL CROP " + clipRect.toShortString(), null); |
| 231 | try { |
| 232 | mSurfaceControl.setFinalCrop(clipRect); |
| 233 | } catch (RuntimeException e) { |
| 234 | Slog.w(TAG, "Error disconnecting surface in: " + this, e); |
| 235 | } |
| 236 | } |
| 237 | |
Robert Carr | da61ba9 | 2017-03-29 15:52:23 -0700 | [diff] [blame] | 238 | void setLayerStackInTransaction(int layerStack) { |
| 239 | if (mSurfaceControl != null) { |
| 240 | mSurfaceControl.setLayerStack(layerStack); |
| 241 | } |
| 242 | } |
| 243 | |
Robert Carr | e6a8351 | 2015-11-03 16:09:21 -0800 | [diff] [blame] | 244 | void setPositionInTransaction(float left, float top, boolean recoveringMemory) { |
Robert Carr | 19cdd09 | 2018-03-14 11:50:52 -0700 | [diff] [blame] | 245 | setPosition(null, left, top, recoveringMemory); |
| 246 | } |
| 247 | |
| 248 | void setPosition(SurfaceControl.Transaction t, float left, float top, |
| 249 | boolean recoveringMemory) { |
Robert Carr | e6a8351 | 2015-11-03 16:09:21 -0800 | [diff] [blame] | 250 | final boolean surfaceMoved = mSurfaceX != left || mSurfaceY != top; |
| 251 | if (surfaceMoved) { |
| 252 | mSurfaceX = left; |
| 253 | mSurfaceY = top; |
| 254 | |
| 255 | try { |
Chong Zhang | ae35fef | 2016-03-16 15:56:55 -0700 | [diff] [blame] | 256 | if (SHOW_TRANSACTIONS) logSurface( |
| 257 | "POS (setPositionInTransaction) @ (" + left + "," + top + ")", null); |
| 258 | |
Robert Carr | 19cdd09 | 2018-03-14 11:50:52 -0700 | [diff] [blame] | 259 | if (t == null) { |
| 260 | mSurfaceControl.setPosition(left, top); |
| 261 | } else { |
| 262 | t.setPosition(mSurfaceControl, left, top); |
| 263 | } |
Robert Carr | e6a8351 | 2015-11-03 16:09:21 -0800 | [diff] [blame] | 264 | } catch (RuntimeException e) { |
| 265 | Slog.w(TAG, "Error positioning surface of " + this |
| 266 | + " pos=(" + left + "," + top + ")", e); |
| 267 | if (!recoveringMemory) { |
| 268 | mAnimator.reclaimSomeSurfaceMemory("position", true); |
| 269 | } |
| 270 | } |
| 271 | } |
| 272 | } |
| 273 | |
Robert Carr | 6da3cc0 | 2016-06-16 15:17:07 -0700 | [diff] [blame] | 274 | void setGeometryAppliesWithResizeInTransaction(boolean recoveringMemory) { |
| 275 | mSurfaceControl.setGeometryAppliesWithResize(); |
Robert Carr | a9408d4 | 2016-06-03 13:28:48 -0700 | [diff] [blame] | 276 | } |
| 277 | |
Andrii Kulian | 283acd2 | 2017-08-03 04:03:51 -0700 | [diff] [blame] | 278 | void setMatrixInTransaction(float dsdx, float dtdx, float dtdy, float dsdy, |
Robert Carr | e6a8351 | 2015-11-03 16:09:21 -0800 | [diff] [blame] | 279 | boolean recoveringMemory) { |
Robert Carr | 19cdd09 | 2018-03-14 11:50:52 -0700 | [diff] [blame] | 280 | setMatrix(null, dsdx, dtdx, dtdy, dsdy, false); |
| 281 | } |
| 282 | |
| 283 | void setMatrix(SurfaceControl.Transaction t, float dsdx, float dtdx, |
| 284 | float dtdy, float dsdy, boolean recoveringMemory) { |
Matthew Bouyack | 8dd88f6 | 2016-10-24 14:01:26 -0700 | [diff] [blame] | 285 | final boolean matrixChanged = mLastDsdx != dsdx || mLastDtdx != dtdx || |
Andrii Kulian | 283acd2 | 2017-08-03 04:03:51 -0700 | [diff] [blame] | 286 | mLastDtdy != dtdy || mLastDsdy != dsdy; |
Matthew Bouyack | 8dd88f6 | 2016-10-24 14:01:26 -0700 | [diff] [blame] | 287 | if (!matrixChanged) { |
| 288 | return; |
| 289 | } |
| 290 | |
| 291 | mLastDsdx = dsdx; |
| 292 | mLastDtdx = dtdx; |
Matthew Bouyack | 8dd88f6 | 2016-10-24 14:01:26 -0700 | [diff] [blame] | 293 | mLastDtdy = dtdy; |
Andrii Kulian | 283acd2 | 2017-08-03 04:03:51 -0700 | [diff] [blame] | 294 | mLastDsdy = dsdy; |
Matthew Bouyack | 8dd88f6 | 2016-10-24 14:01:26 -0700 | [diff] [blame] | 295 | |
Robert Carr | e1034cc3 | 2016-02-01 13:08:15 -0800 | [diff] [blame] | 296 | try { |
| 297 | if (SHOW_TRANSACTIONS) logSurface( |
Andrii Kulian | 283acd2 | 2017-08-03 04:03:51 -0700 | [diff] [blame] | 298 | "MATRIX [" + dsdx + "," + dtdx + "," + dtdy + "," + dsdy + "]", null); |
Robert Carr | 19cdd09 | 2018-03-14 11:50:52 -0700 | [diff] [blame] | 299 | if (t == null) { |
| 300 | mSurfaceControl.setMatrix(dsdx, dtdx, dtdy, dsdy); |
| 301 | } else { |
| 302 | t.setMatrix(mSurfaceControl, dsdx, dtdx, dtdy, dsdy); |
| 303 | } |
Robert Carr | e1034cc3 | 2016-02-01 13:08:15 -0800 | [diff] [blame] | 304 | } catch (RuntimeException e) { |
| 305 | // If something goes wrong with the surface (such |
| 306 | // as running out of memory), don't take down the |
| 307 | // entire system. |
| 308 | Slog.e(TAG, "Error setting matrix on surface surface" + title |
Andrii Kulian | 283acd2 | 2017-08-03 04:03:51 -0700 | [diff] [blame] | 309 | + " MATRIX [" + dsdx + "," + dtdx + "," + dtdy + "," + dsdy + "]", null); |
Robert Carr | e1034cc3 | 2016-02-01 13:08:15 -0800 | [diff] [blame] | 310 | if (!recoveringMemory) { |
| 311 | mAnimator.reclaimSomeSurfaceMemory("matrix", true); |
| 312 | } |
| 313 | } |
Robert Carr | e1034cc3 | 2016-02-01 13:08:15 -0800 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | boolean setSizeInTransaction(int width, int height, boolean recoveringMemory) { |
Robert Carr | e6a8351 | 2015-11-03 16:09:21 -0800 | [diff] [blame] | 317 | final boolean surfaceResized = mSurfaceW != width || mSurfaceH != height; |
| 318 | if (surfaceResized) { |
| 319 | mSurfaceW = width; |
| 320 | mSurfaceH = height; |
| 321 | |
| 322 | try { |
| 323 | if (SHOW_TRANSACTIONS) logSurface( |
| 324 | "SIZE " + width + "x" + height, null); |
| 325 | mSurfaceControl.setSize(width, height); |
Robert Carr | e6a8351 | 2015-11-03 16:09:21 -0800 | [diff] [blame] | 326 | } catch (RuntimeException e) { |
| 327 | // If something goes wrong with the surface (such |
| 328 | // as running out of memory), don't take down the |
| 329 | // entire system. |
| 330 | Slog.e(TAG, "Error resizing surface of " + title |
| 331 | + " size=(" + width + "x" + height + ")", e); |
| 332 | if (!recoveringMemory) { |
| 333 | mAnimator.reclaimSomeSurfaceMemory("size", true); |
| 334 | } |
| 335 | return false; |
| 336 | } |
| 337 | return true; |
| 338 | } |
| 339 | return false; |
| 340 | } |
| 341 | |
Robert Carr | af422a8 | 2017-04-10 18:34:33 -0700 | [diff] [blame] | 342 | boolean prepareToShowInTransaction(float alpha, |
Robert Carr | da61ba9 | 2017-03-29 15:52:23 -0700 | [diff] [blame] | 343 | float dsdx, float dtdx, float dsdy, |
Robert Carr | e6a8351 | 2015-11-03 16:09:21 -0800 | [diff] [blame] | 344 | float dtdy, boolean recoveringMemory) { |
| 345 | if (mSurfaceControl != null) { |
| 346 | try { |
| 347 | mSurfaceAlpha = alpha; |
| 348 | mSurfaceControl.setAlpha(alpha); |
Matthew Bouyack | 8dd88f6 | 2016-10-24 14:01:26 -0700 | [diff] [blame] | 349 | mLastDsdx = dsdx; |
| 350 | mLastDtdx = dtdx; |
| 351 | mLastDsdy = dsdy; |
| 352 | mLastDtdy = dtdy; |
Robert Carr | e6a8351 | 2015-11-03 16:09:21 -0800 | [diff] [blame] | 353 | mSurfaceControl.setMatrix( |
| 354 | dsdx, dtdx, dsdy, dtdy); |
Robert Carr | e6a8351 | 2015-11-03 16:09:21 -0800 | [diff] [blame] | 355 | } catch (RuntimeException e) { |
| 356 | Slog.w(TAG, "Error updating surface in " + title, e); |
| 357 | if (!recoveringMemory) { |
| 358 | mAnimator.reclaimSomeSurfaceMemory("update", true); |
| 359 | } |
| 360 | return false; |
| 361 | } |
| 362 | } |
| 363 | return true; |
| 364 | } |
| 365 | |
| 366 | void setTransparentRegionHint(final Region region) { |
| 367 | if (mSurfaceControl == null) { |
| 368 | Slog.w(TAG, "setTransparentRegionHint: null mSurface after mHasSurface true"); |
| 369 | return; |
| 370 | } |
| 371 | if (SHOW_LIGHT_TRANSACTIONS) Slog.i(TAG, ">>> OPEN TRANSACTION setTransparentRegion"); |
Robert Carr | 68e5c9e | 2016-09-14 10:50:09 -0700 | [diff] [blame] | 372 | mService.openSurfaceTransaction(); |
Robert Carr | e6a8351 | 2015-11-03 16:09:21 -0800 | [diff] [blame] | 373 | try { |
| 374 | mSurfaceControl.setTransparentRegionHint(region); |
| 375 | } finally { |
Adrian Roos | 111aff9 | 2017-09-27 18:11:46 +0200 | [diff] [blame] | 376 | mService.closeSurfaceTransaction("setTransparentRegion"); |
Robert Carr | e6a8351 | 2015-11-03 16:09:21 -0800 | [diff] [blame] | 377 | if (SHOW_LIGHT_TRANSACTIONS) Slog.i(TAG, |
| 378 | "<<< CLOSE TRANSACTION setTransparentRegion"); |
| 379 | } |
| 380 | } |
| 381 | |
| 382 | void setOpaque(boolean isOpaque) { |
| 383 | if (SHOW_TRANSACTIONS) logSurface("isOpaque=" + isOpaque, |
| 384 | null); |
| 385 | |
| 386 | if (mSurfaceControl == null) { |
| 387 | return; |
| 388 | } |
| 389 | if (SHOW_LIGHT_TRANSACTIONS) Slog.i(TAG, ">>> OPEN TRANSACTION setOpaqueLocked"); |
Robert Carr | 68e5c9e | 2016-09-14 10:50:09 -0700 | [diff] [blame] | 390 | mService.openSurfaceTransaction(); |
Robert Carr | e6a8351 | 2015-11-03 16:09:21 -0800 | [diff] [blame] | 391 | try { |
| 392 | mSurfaceControl.setOpaque(isOpaque); |
| 393 | } finally { |
Adrian Roos | 111aff9 | 2017-09-27 18:11:46 +0200 | [diff] [blame] | 394 | mService.closeSurfaceTransaction("setOpaqueLocked"); |
Robert Carr | e6a8351 | 2015-11-03 16:09:21 -0800 | [diff] [blame] | 395 | if (SHOW_LIGHT_TRANSACTIONS) Slog.i(TAG, "<<< CLOSE TRANSACTION setOpaqueLocked"); |
| 396 | } |
| 397 | } |
| 398 | |
| 399 | void setSecure(boolean isSecure) { |
| 400 | if (SHOW_TRANSACTIONS) logSurface("isSecure=" + isSecure, |
| 401 | null); |
| 402 | |
| 403 | if (mSurfaceControl == null) { |
| 404 | return; |
| 405 | } |
| 406 | if (SHOW_LIGHT_TRANSACTIONS) Slog.i(TAG, ">>> OPEN TRANSACTION setSecureLocked"); |
Robert Carr | 68e5c9e | 2016-09-14 10:50:09 -0700 | [diff] [blame] | 407 | mService.openSurfaceTransaction(); |
Robert Carr | e6a8351 | 2015-11-03 16:09:21 -0800 | [diff] [blame] | 408 | try { |
| 409 | mSurfaceControl.setSecure(isSecure); |
| 410 | } finally { |
Adrian Roos | 111aff9 | 2017-09-27 18:11:46 +0200 | [diff] [blame] | 411 | mService.closeSurfaceTransaction("setSecure"); |
Robert Carr | e6a8351 | 2015-11-03 16:09:21 -0800 | [diff] [blame] | 412 | if (SHOW_LIGHT_TRANSACTIONS) Slog.i(TAG, "<<< CLOSE TRANSACTION setSecureLocked"); |
| 413 | } |
| 414 | } |
| 415 | |
Andrii Kulian | 283acd2 | 2017-08-03 04:03:51 -0700 | [diff] [blame] | 416 | void getContainerRect(Rect rect) { |
| 417 | mAnimator.getContainerRect(rect); |
| 418 | } |
| 419 | |
Robert Carr | e6a8351 | 2015-11-03 16:09:21 -0800 | [diff] [blame] | 420 | boolean showRobustlyInTransaction() { |
Filip Gruszczynski | 19723a4 | 2015-11-25 15:01:48 -0800 | [diff] [blame] | 421 | if (SHOW_TRANSACTIONS) logSurface( |
| 422 | "SHOW (performLayout)", null); |
| 423 | if (DEBUG_VISIBILITY) Slog.v(TAG, "Showing " + this |
| 424 | + " during relayout"); |
Jorim Jaggi | 5e6968d | 2016-02-19 18:02:13 -0800 | [diff] [blame] | 425 | mHiddenForOtherReasons = false; |
| 426 | return updateVisibility(); |
| 427 | } |
Robert Carr | e6a8351 | 2015-11-03 16:09:21 -0800 | [diff] [blame] | 428 | |
Jorim Jaggi | 5e6968d | 2016-02-19 18:02:13 -0800 | [diff] [blame] | 429 | private boolean updateVisibility() { |
| 430 | if (mHiddenForCrop || mHiddenForOtherReasons) { |
| 431 | if (mSurfaceShown) { |
Chavi Weingarten | b736e32 | 2018-02-23 00:27:54 +0000 | [diff] [blame] | 432 | hideSurface(mTmpTransaction); |
| 433 | SurfaceControl.mergeToGlobalTransaction(mTmpTransaction); |
Jorim Jaggi | 5e6968d | 2016-02-19 18:02:13 -0800 | [diff] [blame] | 434 | } |
Robert Carr | e6a8351 | 2015-11-03 16:09:21 -0800 | [diff] [blame] | 435 | return false; |
Jorim Jaggi | 5e6968d | 2016-02-19 18:02:13 -0800 | [diff] [blame] | 436 | } else { |
| 437 | if (!mSurfaceShown) { |
| 438 | return showSurface(); |
| 439 | } else { |
| 440 | return true; |
| 441 | } |
Robert Carr | e6a8351 | 2015-11-03 16:09:21 -0800 | [diff] [blame] | 442 | } |
Jorim Jaggi | 5e6968d | 2016-02-19 18:02:13 -0800 | [diff] [blame] | 443 | } |
Robert Carr | e6a8351 | 2015-11-03 16:09:21 -0800 | [diff] [blame] | 444 | |
Jorim Jaggi | 5e6968d | 2016-02-19 18:02:13 -0800 | [diff] [blame] | 445 | private boolean showSurface() { |
Robert Carr | e6a8351 | 2015-11-03 16:09:21 -0800 | [diff] [blame] | 446 | try { |
Wale Ogunwale | 943002b | 2017-02-15 19:34:01 -0800 | [diff] [blame] | 447 | setShown(true); |
Robert Carr | e6a8351 | 2015-11-03 16:09:21 -0800 | [diff] [blame] | 448 | mSurfaceControl.show(); |
| 449 | return true; |
| 450 | } catch (RuntimeException e) { |
| 451 | Slog.w(TAG, "Failure showing surface " + mSurfaceControl + " in " + this, e); |
| 452 | } |
| 453 | |
| 454 | mAnimator.reclaimSomeSurfaceMemory("show", true); |
| 455 | |
| 456 | return false; |
| 457 | } |
| 458 | |
| 459 | void deferTransactionUntil(IBinder handle, long frame) { |
| 460 | // TODO: Logging |
| 461 | mSurfaceControl.deferTransactionUntil(handle, frame); |
| 462 | } |
| 463 | |
Robert Carr | 1ca6a33 | 2016-04-11 18:00:43 -0700 | [diff] [blame] | 464 | void forceScaleableInTransaction(boolean force) { |
| 465 | // -1 means we don't override the default or client specified |
| 466 | // scaling mode. |
| 467 | int scalingMode = force ? SCALING_MODE_SCALE_TO_WINDOW : -1; |
| 468 | mSurfaceControl.setOverrideScalingMode(scalingMode); |
| 469 | } |
| 470 | |
Robert Carr | e6a8351 | 2015-11-03 16:09:21 -0800 | [diff] [blame] | 471 | boolean clearWindowContentFrameStats() { |
| 472 | if (mSurfaceControl == null) { |
| 473 | return false; |
| 474 | } |
| 475 | return mSurfaceControl.clearContentFrameStats(); |
| 476 | } |
| 477 | |
| 478 | boolean getWindowContentFrameStats(WindowContentFrameStats outStats) { |
| 479 | if (mSurfaceControl == null) { |
| 480 | return false; |
| 481 | } |
| 482 | return mSurfaceControl.getContentFrameStats(outStats); |
| 483 | } |
| 484 | |
| 485 | |
| 486 | boolean hasSurface() { |
| 487 | return mSurfaceControl != null; |
| 488 | } |
| 489 | |
| 490 | IBinder getHandle() { |
| 491 | if (mSurfaceControl == null) { |
| 492 | return null; |
| 493 | } |
| 494 | return mSurfaceControl.getHandle(); |
| 495 | } |
| 496 | |
| 497 | void getSurface(Surface outSurface) { |
| 498 | outSurface.copyFrom(mSurfaceControl); |
| 499 | } |
| 500 | |
| 501 | int getLayer() { |
| 502 | return mSurfaceLayer; |
| 503 | } |
| 504 | |
| 505 | boolean getShown() { |
| 506 | return mSurfaceShown; |
| 507 | } |
| 508 | |
Filip Gruszczynski | 10a80e0 | 2015-11-06 09:21:17 -0800 | [diff] [blame] | 509 | void setShown(boolean surfaceShown) { |
| 510 | mSurfaceShown = surfaceShown; |
Wale Ogunwale | 943002b | 2017-02-15 19:34:01 -0800 | [diff] [blame] | 511 | |
Wale Ogunwale | 01ad434 | 2017-06-30 07:07:01 -0700 | [diff] [blame] | 512 | mService.updateNonSystemOverlayWindowsVisibilityIfNeeded(mAnimator.mWin, surfaceShown); |
| 513 | |
Wale Ogunwale | 943002b | 2017-02-15 19:34:01 -0800 | [diff] [blame] | 514 | if (mWindowSession != null) { |
| 515 | mWindowSession.onWindowSurfaceVisibilityChanged(this, mSurfaceShown, mWindowType); |
| 516 | } |
Filip Gruszczynski | 10a80e0 | 2015-11-06 09:21:17 -0800 | [diff] [blame] | 517 | } |
| 518 | |
Robert Carr | e6a8351 | 2015-11-03 16:09:21 -0800 | [diff] [blame] | 519 | float getX() { |
| 520 | return mSurfaceX; |
| 521 | } |
| 522 | |
| 523 | float getY() { |
| 524 | return mSurfaceY; |
| 525 | } |
| 526 | |
Jorim Jaggi | a5e1057 | 2017-11-15 14:36:26 +0100 | [diff] [blame] | 527 | int getWidth() { |
Robert Carr | fed1007 | 2016-05-26 11:48:49 -0700 | [diff] [blame] | 528 | return mSurfaceW; |
| 529 | } |
| 530 | |
Jorim Jaggi | a5e1057 | 2017-11-15 14:36:26 +0100 | [diff] [blame] | 531 | int getHeight() { |
Robert Carr | fed1007 | 2016-05-26 11:48:49 -0700 | [diff] [blame] | 532 | return mSurfaceH; |
| 533 | } |
| 534 | |
Steven Timotius | af03df6 | 2017-07-18 16:56:43 -0700 | [diff] [blame] | 535 | void writeToProto(ProtoOutputStream proto, long fieldId) { |
| 536 | final long token = proto.start(fieldId); |
| 537 | proto.write(SHOWN, mSurfaceShown); |
| 538 | proto.write(LAYER, mSurfaceLayer); |
| 539 | proto.end(token); |
| 540 | } |
Robert Carr | fed1007 | 2016-05-26 11:48:49 -0700 | [diff] [blame] | 541 | |
Robert Carr | e6a8351 | 2015-11-03 16:09:21 -0800 | [diff] [blame] | 542 | public void dump(PrintWriter pw, String prefix, boolean dumpAll) { |
| 543 | if (dumpAll) { |
| 544 | pw.print(prefix); pw.print("mSurface="); pw.println(mSurfaceControl); |
| 545 | } |
| 546 | pw.print(prefix); pw.print("Surface: shown="); pw.print(mSurfaceShown); |
| 547 | pw.print(" layer="); pw.print(mSurfaceLayer); |
| 548 | pw.print(" alpha="); pw.print(mSurfaceAlpha); |
| 549 | pw.print(" rect=("); pw.print(mSurfaceX); |
| 550 | pw.print(","); pw.print(mSurfaceY); |
| 551 | pw.print(") "); pw.print(mSurfaceW); |
Jorim Jaggi | d878e4c | 2017-03-03 17:17:06 +0100 | [diff] [blame] | 552 | pw.print(" x "); pw.print(mSurfaceH); |
| 553 | pw.print(" transform=("); pw.print(mLastDsdx); pw.print(", "); |
| 554 | pw.print(mLastDtdx); pw.print(", "); pw.print(mLastDsdy); |
| 555 | pw.print(", "); pw.print(mLastDtdy); pw.println(")"); |
Robert Carr | e6a8351 | 2015-11-03 16:09:21 -0800 | [diff] [blame] | 556 | } |
| 557 | |
Filip Gruszczynski | 78a08ee | 2015-11-08 18:04:32 -0800 | [diff] [blame] | 558 | @Override |
| 559 | public String toString() { |
| 560 | return mSurfaceControl.toString(); |
| 561 | } |
Robert Carr | e6a8351 | 2015-11-03 16:09:21 -0800 | [diff] [blame] | 562 | } |