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