Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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 | |
Jorim Jaggi | 30d64f3 | 2017-04-07 16:33:17 +0200 | [diff] [blame] | 19 | import static android.graphics.Color.WHITE; |
| 20 | import static android.graphics.Color.alpha; |
| 21 | import static android.view.SurfaceControl.HIDDEN; |
| 22 | import static android.view.WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM; |
| 23 | import static android.view.WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED; |
| 24 | import static android.view.WindowManager.LayoutParams.FLAG_IGNORE_CHEEK_PRESSES; |
| 25 | import static android.view.WindowManager.LayoutParams.FLAG_LOCAL_FOCUS_MODE; |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 26 | import static android.view.WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE; |
| 27 | import static android.view.WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE; |
Jorim Jaggi | 30d64f3 | 2017-04-07 16:33:17 +0200 | [diff] [blame] | 28 | import static android.view.WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL; |
| 29 | import static android.view.WindowManager.LayoutParams.FLAG_SCALED; |
| 30 | import static android.view.WindowManager.LayoutParams.FLAG_SECURE; |
| 31 | import static android.view.WindowManager.LayoutParams.FLAG_SLIPPERY; |
| 32 | import static android.view.WindowManager.LayoutParams.FLAG_SPLIT_TOUCH; |
| 33 | import static android.view.WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH; |
| 34 | import static android.view.WindowManager.LayoutParams.PRIVATE_FLAG_FORCE_DRAW_STATUS_BAR_BACKGROUND; |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 35 | import static android.view.WindowManager.LayoutParams.PRIVATE_FLAG_TASK_SNAPSHOT; |
| 36 | import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION_STARTING; |
Jorim Jaggi | 30d64f3 | 2017-04-07 16:33:17 +0200 | [diff] [blame] | 37 | import static com.android.internal.policy.DecorView.NAVIGATION_BAR_COLOR_VIEW_ATTRIBUTES; |
| 38 | import static com.android.internal.policy.DecorView.STATUS_BAR_COLOR_VIEW_ATTRIBUTES; |
| 39 | import static com.android.internal.policy.DecorView.getColorViewLeftInset; |
| 40 | import static com.android.internal.policy.DecorView.getColorViewTopInset; |
| 41 | import static com.android.internal.policy.DecorView.getNavigationBarRect; |
Jorim Jaggi | e4b0f28 | 2017-05-17 15:10:29 +0200 | [diff] [blame] | 42 | import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_STARTING_WINDOW; |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 43 | import static com.android.server.wm.WindowManagerDebugConfig.TAG_WITH_CLASS_NAME; |
| 44 | import static com.android.server.wm.WindowManagerDebugConfig.TAG_WM; |
| 45 | |
Jorim Jaggi | d635a4a | 2017-05-03 15:21:26 +0200 | [diff] [blame] | 46 | import android.annotation.Nullable; |
Jorim Jaggi | 829b9cd | 2017-01-23 16:20:53 +0100 | [diff] [blame] | 47 | import android.app.ActivityManager.TaskDescription; |
Jorim Jaggi | 30d64f3 | 2017-04-07 16:33:17 +0200 | [diff] [blame] | 48 | import android.app.ActivityManager.TaskSnapshot; |
Jorim Jaggi | d635a4a | 2017-05-03 15:21:26 +0200 | [diff] [blame] | 49 | import android.app.ActivityThread; |
| 50 | import android.content.Context; |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 51 | import android.graphics.Canvas; |
| 52 | import android.graphics.GraphicBuffer; |
Jorim Jaggi | 829b9cd | 2017-01-23 16:20:53 +0100 | [diff] [blame] | 53 | import android.graphics.Paint; |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 54 | import android.graphics.Rect; |
| 55 | import android.os.Handler; |
Jorim Jaggi | 829b9cd | 2017-01-23 16:20:53 +0100 | [diff] [blame] | 56 | import android.os.Looper; |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 57 | import android.os.Message; |
| 58 | import android.os.RemoteException; |
Jorim Jaggi | 30d64f3 | 2017-04-07 16:33:17 +0200 | [diff] [blame] | 59 | import android.os.SystemClock; |
Andrii Kulian | 4460796 | 2017-03-16 11:06:24 -0700 | [diff] [blame] | 60 | import android.util.MergedConfiguration; |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 61 | import android.util.Slog; |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 62 | import android.view.IWindowSession; |
| 63 | import android.view.Surface; |
Jorim Jaggi | 30d64f3 | 2017-04-07 16:33:17 +0200 | [diff] [blame] | 64 | import android.view.SurfaceControl; |
| 65 | import android.view.SurfaceSession; |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 66 | import android.view.View; |
| 67 | import android.view.ViewGroup.LayoutParams; |
| 68 | import android.view.WindowManager; |
| 69 | import android.view.WindowManagerGlobal; |
| 70 | import android.view.WindowManagerPolicy.StartingSurface; |
| 71 | |
Jorim Jaggi | 30d64f3 | 2017-04-07 16:33:17 +0200 | [diff] [blame] | 72 | import com.android.internal.R; |
Jorim Jaggi | 829b9cd | 2017-01-23 16:20:53 +0100 | [diff] [blame] | 73 | import com.android.internal.annotations.VisibleForTesting; |
Jorim Jaggi | 30d64f3 | 2017-04-07 16:33:17 +0200 | [diff] [blame] | 74 | import com.android.internal.policy.DecorView; |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 75 | import com.android.internal.view.BaseIWindow; |
| 76 | |
| 77 | /** |
| 78 | * This class represents a starting window that shows a snapshot. |
| 79 | * <p> |
| 80 | * DO NOT HOLD THE WINDOW MANAGER LOCK WHEN CALLING METHODS OF THIS CLASS! |
| 81 | */ |
| 82 | class TaskSnapshotSurface implements StartingSurface { |
| 83 | |
Jorim Jaggi | 30d64f3 | 2017-04-07 16:33:17 +0200 | [diff] [blame] | 84 | private static final long SIZE_MISMATCH_MINIMUM_TIME_MS = 450; |
| 85 | |
| 86 | /** |
| 87 | * When creating the starting window, we use the exact same layout flags such that we end up |
| 88 | * with a window with the exact same dimensions etc. However, these flags are not used in layout |
| 89 | * and might cause other side effects so we exclude them. |
| 90 | */ |
| 91 | private static final int FLAG_INHERIT_EXCLUDES = FLAG_NOT_FOCUSABLE |
| 92 | | FLAG_NOT_TOUCHABLE |
| 93 | | FLAG_NOT_TOUCH_MODAL |
| 94 | | FLAG_ALT_FOCUSABLE_IM |
| 95 | | FLAG_NOT_FOCUSABLE |
| 96 | | FLAG_HARDWARE_ACCELERATED |
| 97 | | FLAG_IGNORE_CHEEK_PRESSES |
| 98 | | FLAG_LOCAL_FOCUS_MODE |
| 99 | | FLAG_SLIPPERY |
| 100 | | FLAG_WATCH_OUTSIDE_TOUCH |
| 101 | | FLAG_SPLIT_TOUCH |
| 102 | | FLAG_SCALED |
| 103 | | FLAG_SECURE; |
| 104 | |
Jorim Jaggi | d261632 | 2017-06-07 12:38:19 -0700 | [diff] [blame^] | 105 | private static final int PRIVATE_FLAG_INHERITS = PRIVATE_FLAG_FORCE_DRAW_STATUS_BAR_BACKGROUND; |
| 106 | |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 107 | private static final String TAG = TAG_WITH_CLASS_NAME ? "SnapshotStartingWindow" : TAG_WM; |
| 108 | private static final int MSG_REPORT_DRAW = 0; |
| 109 | private static final String TITLE_FORMAT = "SnapshotStartingWindow for taskId=%s"; |
| 110 | private final Window mWindow; |
| 111 | private final Surface mSurface; |
Jorim Jaggi | 30d64f3 | 2017-04-07 16:33:17 +0200 | [diff] [blame] | 112 | private SurfaceControl mChildSurfaceControl; |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 113 | private final IWindowSession mSession; |
| 114 | private final WindowManagerService mService; |
Jorim Jaggi | 30d64f3 | 2017-04-07 16:33:17 +0200 | [diff] [blame] | 115 | private final Rect mTaskBounds; |
| 116 | private final Rect mStableInsets = new Rect(); |
| 117 | private final Rect mContentInsets = new Rect(); |
| 118 | private final Rect mFrame = new Rect(); |
Jorim Jaggi | 4448e1e | 2017-05-16 22:26:02 +0200 | [diff] [blame] | 119 | private TaskSnapshot mSnapshot; |
Jorim Jaggi | 30d64f3 | 2017-04-07 16:33:17 +0200 | [diff] [blame] | 120 | private final CharSequence mTitle; |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 121 | private boolean mHasDrawn; |
Jorim Jaggi | 30d64f3 | 2017-04-07 16:33:17 +0200 | [diff] [blame] | 122 | private long mShownTime; |
| 123 | private final Handler mHandler; |
| 124 | private boolean mSizeMismatch; |
| 125 | private final Paint mBackgroundPaint = new Paint(); |
Jorim Jaggi | 30d64f3 | 2017-04-07 16:33:17 +0200 | [diff] [blame] | 126 | private final int mStatusBarColor; |
Jorim Jaggi | d635a4a | 2017-05-03 15:21:26 +0200 | [diff] [blame] | 127 | @VisibleForTesting final SystemBarBackgroundPainter mSystemBarBackgroundPainter; |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 128 | |
| 129 | static TaskSnapshotSurface create(WindowManagerService service, AppWindowToken token, |
Jorim Jaggi | 30d64f3 | 2017-04-07 16:33:17 +0200 | [diff] [blame] | 130 | TaskSnapshot snapshot) { |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 131 | |
| 132 | final WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams(); |
| 133 | final Window window = new Window(); |
| 134 | final IWindowSession session = WindowManagerGlobal.getWindowSession(); |
| 135 | window.setSession(session); |
| 136 | final Surface surface = new Surface(); |
| 137 | final Rect tmpRect = new Rect(); |
| 138 | final Rect tmpFrame = new Rect(); |
Jorim Jaggi | 30d64f3 | 2017-04-07 16:33:17 +0200 | [diff] [blame] | 139 | final Rect taskBounds; |
| 140 | final Rect tmpContentInsets = new Rect(); |
| 141 | final Rect tmpStableInsets = new Rect(); |
Andrii Kulian | 4460796 | 2017-03-16 11:06:24 -0700 | [diff] [blame] | 142 | final MergedConfiguration tmpMergedConfiguration = new MergedConfiguration(); |
Jorim Jaggi | 30d64f3 | 2017-04-07 16:33:17 +0200 | [diff] [blame] | 143 | int backgroundColor = WHITE; |
| 144 | int statusBarColor = 0; |
| 145 | int navigationBarColor = 0; |
| 146 | final int sysUiVis; |
| 147 | final int windowFlags; |
| 148 | final int windowPrivateFlags; |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 149 | synchronized (service.mWindowMap) { |
Jorim Jaggi | 30d64f3 | 2017-04-07 16:33:17 +0200 | [diff] [blame] | 150 | final WindowState mainWindow = token.findMainWindow(); |
| 151 | if (mainWindow == null) { |
| 152 | Slog.w(TAG, "TaskSnapshotSurface.create: Failed to find main window for token=" |
| 153 | + token); |
| 154 | return null; |
| 155 | } |
| 156 | sysUiVis = mainWindow.getSystemUiVisibility(); |
| 157 | windowFlags = mainWindow.getAttrs().flags; |
| 158 | windowPrivateFlags = mainWindow.getAttrs().privateFlags; |
| 159 | |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 160 | layoutParams.type = TYPE_APPLICATION_STARTING; |
Jorim Jaggi | 30d64f3 | 2017-04-07 16:33:17 +0200 | [diff] [blame] | 161 | layoutParams.format = snapshot.getSnapshot().getFormat(); |
| 162 | layoutParams.flags = (windowFlags & ~FLAG_INHERIT_EXCLUDES) |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 163 | | FLAG_NOT_FOCUSABLE |
Jorim Jaggi | 30d64f3 | 2017-04-07 16:33:17 +0200 | [diff] [blame] | 164 | | FLAG_NOT_TOUCHABLE; |
Jorim Jaggi | d261632 | 2017-06-07 12:38:19 -0700 | [diff] [blame^] | 165 | layoutParams.privateFlags = PRIVATE_FLAG_TASK_SNAPSHOT |
| 166 | | (windowPrivateFlags & PRIVATE_FLAG_INHERITS); |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 167 | layoutParams.token = token.token; |
| 168 | layoutParams.width = LayoutParams.MATCH_PARENT; |
| 169 | layoutParams.height = LayoutParams.MATCH_PARENT; |
Jorim Jaggi | 30d64f3 | 2017-04-07 16:33:17 +0200 | [diff] [blame] | 170 | layoutParams.systemUiVisibility = sysUiVis; |
Bryce Lee | 6d41026 | 2017-02-28 15:30:17 -0800 | [diff] [blame] | 171 | final Task task = token.getTask(); |
| 172 | if (task != null) { |
Jorim Jaggi | 30d64f3 | 2017-04-07 16:33:17 +0200 | [diff] [blame] | 173 | layoutParams.setTitle(String.format(TITLE_FORMAT, task.mTaskId)); |
Bryce Lee | 6d41026 | 2017-02-28 15:30:17 -0800 | [diff] [blame] | 174 | |
| 175 | final TaskDescription taskDescription = task.getTaskDescription(); |
Jorim Jaggi | 829b9cd | 2017-01-23 16:20:53 +0100 | [diff] [blame] | 176 | if (taskDescription != null) { |
Jorim Jaggi | 30d64f3 | 2017-04-07 16:33:17 +0200 | [diff] [blame] | 177 | backgroundColor = taskDescription.getBackgroundColor(); |
| 178 | statusBarColor = taskDescription.getStatusBarColor(); |
| 179 | navigationBarColor = taskDescription.getNavigationBarColor(); |
Jorim Jaggi | 829b9cd | 2017-01-23 16:20:53 +0100 | [diff] [blame] | 180 | } |
Jorim Jaggi | 30d64f3 | 2017-04-07 16:33:17 +0200 | [diff] [blame] | 181 | taskBounds = new Rect(); |
| 182 | task.getBounds(taskBounds); |
| 183 | } else { |
| 184 | taskBounds = null; |
Jorim Jaggi | 829b9cd | 2017-01-23 16:20:53 +0100 | [diff] [blame] | 185 | } |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 186 | } |
| 187 | try { |
| 188 | final int res = session.addToDisplay(window, window.mSeq, layoutParams, |
| 189 | View.VISIBLE, token.getDisplayContent().getDisplayId(), tmpRect, tmpRect, |
| 190 | tmpRect, null); |
| 191 | if (res < 0) { |
| 192 | Slog.w(TAG, "Failed to add snapshot starting window res=" + res); |
| 193 | return null; |
| 194 | } |
| 195 | } catch (RemoteException e) { |
| 196 | // Local call. |
| 197 | } |
| 198 | final TaskSnapshotSurface snapshotSurface = new TaskSnapshotSurface(service, window, |
Jorim Jaggi | 30d64f3 | 2017-04-07 16:33:17 +0200 | [diff] [blame] | 199 | surface, snapshot, layoutParams.getTitle(), backgroundColor, statusBarColor, |
| 200 | navigationBarColor, sysUiVis, windowFlags, windowPrivateFlags, taskBounds); |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 201 | window.setOuter(snapshotSurface); |
| 202 | try { |
| 203 | session.relayout(window, window.mSeq, layoutParams, -1, -1, View.VISIBLE, 0, tmpFrame, |
Jorim Jaggi | 30d64f3 | 2017-04-07 16:33:17 +0200 | [diff] [blame] | 204 | tmpRect, tmpContentInsets, tmpRect, tmpStableInsets, tmpRect, tmpRect, |
| 205 | tmpMergedConfiguration, surface); |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 206 | } catch (RemoteException e) { |
| 207 | // Local call. |
| 208 | } |
Jorim Jaggi | 30d64f3 | 2017-04-07 16:33:17 +0200 | [diff] [blame] | 209 | snapshotSurface.setFrames(tmpFrame, tmpContentInsets, tmpStableInsets); |
| 210 | snapshotSurface.drawSnapshot(); |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 211 | return snapshotSurface; |
| 212 | } |
| 213 | |
Jorim Jaggi | 829b9cd | 2017-01-23 16:20:53 +0100 | [diff] [blame] | 214 | @VisibleForTesting |
| 215 | TaskSnapshotSurface(WindowManagerService service, Window window, Surface surface, |
Jorim Jaggi | 30d64f3 | 2017-04-07 16:33:17 +0200 | [diff] [blame] | 216 | TaskSnapshot snapshot, CharSequence title, int backgroundColor, int statusBarColor, |
| 217 | int navigationBarColor, int sysUiVis, int windowFlags, int windowPrivateFlags, |
| 218 | Rect taskBounds) { |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 219 | mService = service; |
Jorim Jaggi | 30d64f3 | 2017-04-07 16:33:17 +0200 | [diff] [blame] | 220 | mHandler = new Handler(mService.mH.getLooper()); |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 221 | mSession = WindowManagerGlobal.getWindowSession(); |
| 222 | mWindow = window; |
| 223 | mSurface = surface; |
Jorim Jaggi | 30d64f3 | 2017-04-07 16:33:17 +0200 | [diff] [blame] | 224 | mSnapshot = snapshot; |
| 225 | mTitle = title; |
| 226 | mBackgroundPaint.setColor(backgroundColor != 0 ? backgroundColor : WHITE); |
| 227 | mTaskBounds = taskBounds; |
Jorim Jaggi | d635a4a | 2017-05-03 15:21:26 +0200 | [diff] [blame] | 228 | mSystemBarBackgroundPainter = new SystemBarBackgroundPainter(windowFlags, |
| 229 | windowPrivateFlags, sysUiVis, statusBarColor, navigationBarColor); |
| 230 | mStatusBarColor = statusBarColor; |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | @Override |
| 234 | public void remove() { |
Jorim Jaggi | 30d64f3 | 2017-04-07 16:33:17 +0200 | [diff] [blame] | 235 | synchronized (mService.mWindowMap) { |
| 236 | final long now = SystemClock.uptimeMillis(); |
| 237 | if (mSizeMismatch && now - mShownTime < SIZE_MISMATCH_MINIMUM_TIME_MS) { |
| 238 | mHandler.postAtTime(this::remove, mShownTime + SIZE_MISMATCH_MINIMUM_TIME_MS); |
Jorim Jaggi | e4b0f28 | 2017-05-17 15:10:29 +0200 | [diff] [blame] | 239 | if (DEBUG_STARTING_WINDOW) { |
| 240 | Slog.v(TAG, "Defer removing snapshot surface in " + (now - mShownTime) + "ms"); |
| 241 | } |
Jorim Jaggi | 30d64f3 | 2017-04-07 16:33:17 +0200 | [diff] [blame] | 242 | return; |
| 243 | } |
| 244 | } |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 245 | try { |
Jorim Jaggi | e4b0f28 | 2017-05-17 15:10:29 +0200 | [diff] [blame] | 246 | if (DEBUG_STARTING_WINDOW) Slog.v(TAG, "Removing snapshot surface"); |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 247 | mSession.remove(mWindow); |
| 248 | } catch (RemoteException e) { |
| 249 | // Local call. |
| 250 | } |
| 251 | } |
| 252 | |
Jorim Jaggi | 30d64f3 | 2017-04-07 16:33:17 +0200 | [diff] [blame] | 253 | @VisibleForTesting |
| 254 | void setFrames(Rect frame, Rect contentInsets, Rect stableInsets) { |
| 255 | mFrame.set(frame); |
| 256 | mContentInsets.set(contentInsets); |
| 257 | mStableInsets.set(stableInsets); |
| 258 | mSizeMismatch = (mFrame.width() != mSnapshot.getSnapshot().getWidth() |
| 259 | || mFrame.height() != mSnapshot.getSnapshot().getHeight()); |
Jorim Jaggi | d635a4a | 2017-05-03 15:21:26 +0200 | [diff] [blame] | 260 | mSystemBarBackgroundPainter.setInsets(contentInsets, stableInsets); |
Jorim Jaggi | 30d64f3 | 2017-04-07 16:33:17 +0200 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | private void drawSnapshot() { |
| 264 | final GraphicBuffer buffer = mSnapshot.getSnapshot(); |
Jorim Jaggi | e4b0f28 | 2017-05-17 15:10:29 +0200 | [diff] [blame] | 265 | if (DEBUG_STARTING_WINDOW) Slog.v(TAG, "Drawing snapshot surface sizeMismatch=" |
| 266 | + mSizeMismatch); |
Jorim Jaggi | 30d64f3 | 2017-04-07 16:33:17 +0200 | [diff] [blame] | 267 | if (mSizeMismatch) { |
| 268 | // The dimensions of the buffer and the window don't match, so attaching the buffer |
| 269 | // will fail. Better create a child window with the exact dimensions and fill the parent |
| 270 | // window with the background color! |
| 271 | drawSizeMismatchSnapshot(buffer); |
| 272 | } else { |
| 273 | drawSizeMatchSnapshot(buffer); |
| 274 | } |
Jorim Jaggi | 39367cf | 2017-03-13 16:41:13 +0100 | [diff] [blame] | 275 | synchronized (mService.mWindowMap) { |
Jorim Jaggi | 30d64f3 | 2017-04-07 16:33:17 +0200 | [diff] [blame] | 276 | mShownTime = SystemClock.uptimeMillis(); |
Jorim Jaggi | 39367cf | 2017-03-13 16:41:13 +0100 | [diff] [blame] | 277 | mHasDrawn = true; |
Jorim Jaggi | 39367cf | 2017-03-13 16:41:13 +0100 | [diff] [blame] | 278 | } |
Jorim Jaggi | dc9385a | 2017-05-13 02:00:31 +0200 | [diff] [blame] | 279 | reportDrawn(); |
Jorim Jaggi | 4448e1e | 2017-05-16 22:26:02 +0200 | [diff] [blame] | 280 | |
| 281 | // In case window manager leaks us, make sure we don't retain the snapshot. |
| 282 | mSnapshot = null; |
Jorim Jaggi | 30d64f3 | 2017-04-07 16:33:17 +0200 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | private void drawSizeMatchSnapshot(GraphicBuffer buffer) { |
| 286 | mSurface.attachAndQueueBuffer(buffer); |
| 287 | mSurface.release(); |
| 288 | } |
| 289 | |
| 290 | private void drawSizeMismatchSnapshot(GraphicBuffer buffer) { |
| 291 | final SurfaceSession session = new SurfaceSession(mSurface); |
| 292 | |
| 293 | // Keep a reference to it such that it doesn't get destroyed when finalized. |
| 294 | mChildSurfaceControl = new SurfaceControl(session, |
| 295 | mTitle + " - task-snapshot-surface", |
| 296 | buffer.getWidth(), buffer.getHeight(), buffer.getFormat(), HIDDEN); |
| 297 | Surface surface = new Surface(); |
| 298 | surface.copyFrom(mChildSurfaceControl); |
| 299 | |
| 300 | // Clip off ugly navigation bar. |
| 301 | final Rect crop = calculateSnapshotCrop(); |
| 302 | final Rect frame = calculateSnapshotFrame(crop); |
| 303 | SurfaceControl.openTransaction(); |
| 304 | try { |
| 305 | // We can just show the surface here as it will still be hidden as the parent is |
| 306 | // still hidden. |
| 307 | mChildSurfaceControl.show(); |
| 308 | mChildSurfaceControl.setWindowCrop(crop); |
| 309 | mChildSurfaceControl.setPosition(frame.left, frame.top); |
| 310 | } finally { |
| 311 | SurfaceControl.closeTransaction(); |
| 312 | } |
| 313 | surface.attachAndQueueBuffer(buffer); |
| 314 | surface.release(); |
| 315 | |
| 316 | final Canvas c = mSurface.lockCanvas(null); |
| 317 | drawBackgroundAndBars(c, frame); |
| 318 | mSurface.unlockCanvasAndPost(c); |
Jorim Jaggi | 2f24b65 | 2017-01-18 02:17:37 +0100 | [diff] [blame] | 319 | mSurface.release(); |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 320 | } |
| 321 | |
Jorim Jaggi | 829b9cd | 2017-01-23 16:20:53 +0100 | [diff] [blame] | 322 | @VisibleForTesting |
Jorim Jaggi | 30d64f3 | 2017-04-07 16:33:17 +0200 | [diff] [blame] | 323 | Rect calculateSnapshotCrop() { |
| 324 | final Rect rect = new Rect(); |
| 325 | rect.set(0, 0, mSnapshot.getSnapshot().getWidth(), mSnapshot.getSnapshot().getHeight()); |
| 326 | final Rect insets = mSnapshot.getContentInsets(); |
| 327 | |
| 328 | // Let's remove all system decorations except the status bar, but only if the task is at the |
| 329 | // very top of the screen. |
| 330 | rect.inset(insets.left, mTaskBounds.top != 0 ? insets.top : 0, insets.right, insets.bottom); |
| 331 | return rect; |
| 332 | } |
| 333 | |
| 334 | @VisibleForTesting |
| 335 | Rect calculateSnapshotFrame(Rect crop) { |
| 336 | final Rect frame = new Rect(crop); |
| 337 | |
| 338 | // By default, offset it to to top/left corner |
| 339 | frame.offsetTo(-crop.left, -crop.top); |
| 340 | |
| 341 | // However, we also need to make space for the navigation bar on the left side. |
| 342 | final int colorViewLeftInset = getColorViewLeftInset(mStableInsets.left, |
| 343 | mContentInsets.left); |
| 344 | frame.offset(colorViewLeftInset, 0); |
| 345 | return frame; |
| 346 | } |
| 347 | |
| 348 | @VisibleForTesting |
| 349 | void drawBackgroundAndBars(Canvas c, Rect frame) { |
Jorim Jaggi | d635a4a | 2017-05-03 15:21:26 +0200 | [diff] [blame] | 350 | final int statusBarHeight = mSystemBarBackgroundPainter.getStatusBarColorViewHeight(); |
Jorim Jaggi | 30d64f3 | 2017-04-07 16:33:17 +0200 | [diff] [blame] | 351 | final boolean fillHorizontally = c.getWidth() > frame.right; |
| 352 | final boolean fillVertically = c.getHeight() > frame.bottom; |
Jorim Jaggi | 829b9cd | 2017-01-23 16:20:53 +0100 | [diff] [blame] | 353 | if (fillHorizontally) { |
Jorim Jaggi | 30d64f3 | 2017-04-07 16:33:17 +0200 | [diff] [blame] | 354 | c.drawRect(frame.right, alpha(mStatusBarColor) == 0xFF ? statusBarHeight : 0, |
| 355 | c.getWidth(), fillVertically |
| 356 | ? frame.bottom |
| 357 | : c.getHeight(), |
| 358 | mBackgroundPaint); |
Jorim Jaggi | 829b9cd | 2017-01-23 16:20:53 +0100 | [diff] [blame] | 359 | } |
| 360 | if (fillVertically) { |
Jorim Jaggi | 30d64f3 | 2017-04-07 16:33:17 +0200 | [diff] [blame] | 361 | c.drawRect(0, frame.bottom, c.getWidth(), c.getHeight(), mBackgroundPaint); |
| 362 | } |
Jorim Jaggi | d635a4a | 2017-05-03 15:21:26 +0200 | [diff] [blame] | 363 | mSystemBarBackgroundPainter.drawDecors(c, frame); |
Jorim Jaggi | 829b9cd | 2017-01-23 16:20:53 +0100 | [diff] [blame] | 364 | } |
| 365 | |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 366 | private void reportDrawn() { |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 367 | try { |
| 368 | mSession.finishDrawing(mWindow); |
| 369 | } catch (RemoteException e) { |
| 370 | // Local call. |
| 371 | } |
| 372 | } |
| 373 | |
Jorim Jaggi | 829b9cd | 2017-01-23 16:20:53 +0100 | [diff] [blame] | 374 | private static Handler sHandler = new Handler(Looper.getMainLooper()) { |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 375 | |
| 376 | @Override |
| 377 | public void handleMessage(Message msg) { |
| 378 | switch (msg.what) { |
| 379 | case MSG_REPORT_DRAW: |
| 380 | final boolean hasDrawn; |
| 381 | final TaskSnapshotSurface surface = (TaskSnapshotSurface) msg.obj; |
| 382 | synchronized (surface.mService.mWindowMap) { |
| 383 | hasDrawn = surface.mHasDrawn; |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 384 | } |
| 385 | if (hasDrawn) { |
| 386 | surface.reportDrawn(); |
| 387 | } |
| 388 | break; |
| 389 | } |
| 390 | } |
| 391 | }; |
| 392 | |
Jorim Jaggi | 30d64f3 | 2017-04-07 16:33:17 +0200 | [diff] [blame] | 393 | @VisibleForTesting |
| 394 | static class Window extends BaseIWindow { |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 395 | |
| 396 | private TaskSnapshotSurface mOuter; |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 397 | public void setOuter(TaskSnapshotSurface outer) { |
| 398 | mOuter = outer; |
| 399 | } |
| 400 | |
| 401 | @Override |
| 402 | public void resized(Rect frame, Rect overscanInsets, Rect contentInsets, Rect visibleInsets, |
Andrii Kulian | 4460796 | 2017-03-16 11:06:24 -0700 | [diff] [blame] | 403 | Rect stableInsets, Rect outsets, boolean reportDraw, |
| 404 | MergedConfiguration mergedConfiguration, Rect backDropFrame, boolean forceLayout, |
| 405 | boolean alwaysConsumeNavBar, int displayId) { |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 406 | if (reportDraw) { |
| 407 | sHandler.obtainMessage(MSG_REPORT_DRAW, mOuter).sendToTarget(); |
| 408 | } |
| 409 | } |
| 410 | } |
Jorim Jaggi | d635a4a | 2017-05-03 15:21:26 +0200 | [diff] [blame] | 411 | |
| 412 | /** |
| 413 | * Helper class to draw the background of the system bars in regions the task snapshot isn't |
| 414 | * filling the window. |
| 415 | */ |
| 416 | static class SystemBarBackgroundPainter { |
| 417 | |
| 418 | private final Rect mContentInsets = new Rect(); |
| 419 | private final Rect mStableInsets = new Rect(); |
| 420 | private final Paint mStatusBarPaint = new Paint(); |
| 421 | private final Paint mNavigationBarPaint = new Paint(); |
| 422 | private final int mStatusBarColor; |
| 423 | private final int mNavigationBarColor; |
| 424 | private final int mWindowFlags; |
| 425 | private final int mWindowPrivateFlags; |
| 426 | private final int mSysUiVis; |
| 427 | |
| 428 | SystemBarBackgroundPainter( int windowFlags, int windowPrivateFlags, int sysUiVis, |
| 429 | int statusBarColor, int navigationBarColor) { |
| 430 | mWindowFlags = windowFlags; |
| 431 | mWindowPrivateFlags = windowPrivateFlags; |
| 432 | mSysUiVis = sysUiVis; |
| 433 | final Context context = ActivityThread.currentActivityThread().getSystemUiContext(); |
| 434 | mStatusBarColor = DecorView.calculateStatusBarColor(windowFlags, |
| 435 | context.getColor(R.color.system_bar_background_semi_transparent), |
| 436 | statusBarColor); |
| 437 | mNavigationBarColor = navigationBarColor; |
| 438 | mStatusBarPaint.setColor(mStatusBarColor); |
| 439 | mNavigationBarPaint.setColor(navigationBarColor); |
| 440 | } |
| 441 | |
| 442 | void setInsets(Rect contentInsets, Rect stableInsets) { |
| 443 | mContentInsets.set(contentInsets); |
| 444 | mStableInsets.set(stableInsets); |
| 445 | } |
| 446 | |
| 447 | int getStatusBarColorViewHeight() { |
| 448 | final boolean forceStatusBarBackground = |
| 449 | (mWindowPrivateFlags & PRIVATE_FLAG_FORCE_DRAW_STATUS_BAR_BACKGROUND) != 0; |
| 450 | if (STATUS_BAR_COLOR_VIEW_ATTRIBUTES.isVisible( |
| 451 | mSysUiVis, mStatusBarColor, mWindowFlags, forceStatusBarBackground)) { |
| 452 | return getColorViewTopInset(mStableInsets.top, mContentInsets.top); |
| 453 | } else { |
| 454 | return 0; |
| 455 | } |
| 456 | } |
| 457 | |
| 458 | private boolean isNavigationBarColorViewVisible() { |
| 459 | return NAVIGATION_BAR_COLOR_VIEW_ATTRIBUTES.isVisible( |
| 460 | mSysUiVis, mNavigationBarColor, mWindowFlags, false /* force */); |
| 461 | } |
| 462 | |
| 463 | void drawDecors(Canvas c, @Nullable Rect alreadyDrawnFrame) { |
| 464 | drawStatusBarBackground(c, alreadyDrawnFrame, getStatusBarColorViewHeight()); |
| 465 | drawNavigationBarBackground(c); |
| 466 | } |
| 467 | |
| 468 | @VisibleForTesting |
| 469 | void drawStatusBarBackground(Canvas c, @Nullable Rect alreadyDrawnFrame, |
| 470 | int statusBarHeight) { |
| 471 | if (statusBarHeight > 0 |
| 472 | && (alreadyDrawnFrame == null || c.getWidth() > alreadyDrawnFrame.right)) { |
| 473 | final int rightInset = DecorView.getColorViewRightInset(mStableInsets.right, |
| 474 | mContentInsets.right); |
| 475 | final int left = alreadyDrawnFrame != null ? alreadyDrawnFrame.right : 0; |
| 476 | c.drawRect(left, 0, c.getWidth() - rightInset, statusBarHeight, mStatusBarPaint); |
| 477 | } |
| 478 | } |
| 479 | |
| 480 | @VisibleForTesting |
| 481 | void drawNavigationBarBackground(Canvas c) { |
| 482 | final Rect navigationBarRect = new Rect(); |
| 483 | getNavigationBarRect(c.getWidth(), c.getHeight(), mStableInsets, mContentInsets, |
| 484 | navigationBarRect); |
| 485 | final boolean visible = isNavigationBarColorViewVisible(); |
| 486 | if (visible && !navigationBarRect.isEmpty()) { |
| 487 | c.drawRect(navigationBarRect, mNavigationBarPaint); |
| 488 | } |
| 489 | } |
| 490 | } |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 491 | } |