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