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 | |
| 19 | import static android.view.WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS; |
| 20 | import static android.view.WindowManager.LayoutParams.FLAG_LAYOUT_INSET_DECOR; |
| 21 | import static android.view.WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN; |
| 22 | import static android.view.WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE; |
| 23 | import static android.view.WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE; |
| 24 | import static android.view.WindowManager.LayoutParams.PRIVATE_FLAG_TASK_SNAPSHOT; |
| 25 | import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION_STARTING; |
| 26 | import static com.android.server.wm.WindowManagerDebugConfig.TAG_WITH_CLASS_NAME; |
| 27 | import static com.android.server.wm.WindowManagerDebugConfig.TAG_WM; |
| 28 | |
Jorim Jaggi | 829b9cd | 2017-01-23 16:20:53 +0100 | [diff] [blame^] | 29 | import android.app.ActivityManager.TaskDescription; |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 30 | import android.content.res.Configuration; |
| 31 | import android.graphics.Bitmap; |
| 32 | import android.graphics.Canvas; |
Jorim Jaggi | 829b9cd | 2017-01-23 16:20:53 +0100 | [diff] [blame^] | 33 | import android.graphics.Color; |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 34 | import android.graphics.GraphicBuffer; |
Jorim Jaggi | 829b9cd | 2017-01-23 16:20:53 +0100 | [diff] [blame^] | 35 | import android.graphics.Paint; |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 36 | import android.graphics.Rect; |
| 37 | import android.os.Handler; |
Jorim Jaggi | 829b9cd | 2017-01-23 16:20:53 +0100 | [diff] [blame^] | 38 | import android.os.Looper; |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 39 | import android.os.Message; |
| 40 | import android.os.RemoteException; |
| 41 | import android.util.Slog; |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 42 | import android.view.IWindowSession; |
| 43 | import android.view.Surface; |
| 44 | import android.view.View; |
| 45 | import android.view.ViewGroup.LayoutParams; |
| 46 | import android.view.WindowManager; |
| 47 | import android.view.WindowManagerGlobal; |
| 48 | import android.view.WindowManagerPolicy.StartingSurface; |
| 49 | |
Jorim Jaggi | 829b9cd | 2017-01-23 16:20:53 +0100 | [diff] [blame^] | 50 | import com.android.internal.annotations.VisibleForTesting; |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 51 | import com.android.internal.view.BaseIWindow; |
| 52 | |
| 53 | /** |
| 54 | * This class represents a starting window that shows a snapshot. |
| 55 | * <p> |
| 56 | * DO NOT HOLD THE WINDOW MANAGER LOCK WHEN CALLING METHODS OF THIS CLASS! |
| 57 | */ |
| 58 | class TaskSnapshotSurface implements StartingSurface { |
| 59 | |
| 60 | private static final String TAG = TAG_WITH_CLASS_NAME ? "SnapshotStartingWindow" : TAG_WM; |
| 61 | private static final int MSG_REPORT_DRAW = 0; |
| 62 | private static final String TITLE_FORMAT = "SnapshotStartingWindow for taskId=%s"; |
| 63 | private final Window mWindow; |
| 64 | private final Surface mSurface; |
| 65 | private final IWindowSession mSession; |
| 66 | private final WindowManagerService mService; |
| 67 | private boolean mHasDrawn; |
| 68 | private boolean mReportNextDraw; |
Jorim Jaggi | 829b9cd | 2017-01-23 16:20:53 +0100 | [diff] [blame^] | 69 | private Paint mFillBackgroundPaint = new Paint(); |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 70 | |
| 71 | static TaskSnapshotSurface create(WindowManagerService service, AppWindowToken token, |
| 72 | GraphicBuffer snapshot) { |
| 73 | |
| 74 | final WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams(); |
| 75 | final Window window = new Window(); |
| 76 | final IWindowSession session = WindowManagerGlobal.getWindowSession(); |
| 77 | window.setSession(session); |
| 78 | final Surface surface = new Surface(); |
| 79 | final Rect tmpRect = new Rect(); |
| 80 | final Rect tmpFrame = new Rect(); |
| 81 | final Configuration tmpConfiguration = new Configuration(); |
Jorim Jaggi | 829b9cd | 2017-01-23 16:20:53 +0100 | [diff] [blame^] | 82 | int fillBackgroundColor = Color.WHITE; |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 83 | synchronized (service.mWindowMap) { |
| 84 | layoutParams.type = TYPE_APPLICATION_STARTING; |
| 85 | layoutParams.format = snapshot.getFormat(); |
| 86 | layoutParams.flags = FLAG_LAYOUT_INSET_DECOR |
| 87 | | FLAG_LAYOUT_IN_SCREEN |
| 88 | | FLAG_NOT_FOCUSABLE |
| 89 | | FLAG_NOT_TOUCHABLE |
| 90 | | FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS; |
| 91 | layoutParams.privateFlags = PRIVATE_FLAG_TASK_SNAPSHOT; |
| 92 | layoutParams.token = token.token; |
| 93 | layoutParams.width = LayoutParams.MATCH_PARENT; |
| 94 | layoutParams.height = LayoutParams.MATCH_PARENT; |
| 95 | |
| 96 | // TODO: Inherit behavior whether to draw behind status bar/nav bar. |
| 97 | layoutParams.systemUiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN |
| 98 | | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION; |
| 99 | layoutParams.setTitle(String.format(TITLE_FORMAT, token.mTask.mTaskId)); |
Jorim Jaggi | 829b9cd | 2017-01-23 16:20:53 +0100 | [diff] [blame^] | 100 | if (token.mTask != null) { |
| 101 | final TaskDescription taskDescription = token.mTask.getTaskDescription(); |
| 102 | if (taskDescription != null) { |
| 103 | fillBackgroundColor = taskDescription.getBackgroundColor(); |
| 104 | } |
| 105 | } |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 106 | } |
| 107 | try { |
| 108 | final int res = session.addToDisplay(window, window.mSeq, layoutParams, |
| 109 | View.VISIBLE, token.getDisplayContent().getDisplayId(), tmpRect, tmpRect, |
| 110 | tmpRect, null); |
| 111 | if (res < 0) { |
| 112 | Slog.w(TAG, "Failed to add snapshot starting window res=" + res); |
| 113 | return null; |
| 114 | } |
| 115 | } catch (RemoteException e) { |
| 116 | // Local call. |
| 117 | } |
| 118 | final TaskSnapshotSurface snapshotSurface = new TaskSnapshotSurface(service, window, |
Jorim Jaggi | 829b9cd | 2017-01-23 16:20:53 +0100 | [diff] [blame^] | 119 | surface, fillBackgroundColor); |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 120 | window.setOuter(snapshotSurface); |
| 121 | try { |
| 122 | session.relayout(window, window.mSeq, layoutParams, -1, -1, View.VISIBLE, 0, tmpFrame, |
| 123 | tmpRect, tmpRect, tmpRect, tmpRect, tmpRect, tmpRect, tmpConfiguration, |
| 124 | surface); |
| 125 | } catch (RemoteException e) { |
| 126 | // Local call. |
| 127 | } |
| 128 | snapshotSurface.drawSnapshot(snapshot); |
| 129 | return snapshotSurface; |
| 130 | } |
| 131 | |
Jorim Jaggi | 829b9cd | 2017-01-23 16:20:53 +0100 | [diff] [blame^] | 132 | @VisibleForTesting |
| 133 | TaskSnapshotSurface(WindowManagerService service, Window window, Surface surface, |
| 134 | int fillBackgroundColor) { |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 135 | mService = service; |
| 136 | mSession = WindowManagerGlobal.getWindowSession(); |
| 137 | mWindow = window; |
| 138 | mSurface = surface; |
Jorim Jaggi | 829b9cd | 2017-01-23 16:20:53 +0100 | [diff] [blame^] | 139 | mFillBackgroundPaint.setColor(fillBackgroundColor); |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | @Override |
| 143 | public void remove() { |
| 144 | try { |
| 145 | mSession.remove(mWindow); |
| 146 | } catch (RemoteException e) { |
| 147 | // Local call. |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | private void drawSnapshot(GraphicBuffer snapshot) { |
| 152 | |
| 153 | // TODO: Just wrap the buffer here without any copying. |
| 154 | final Canvas c = mSurface.lockHardwareCanvas(); |
Jorim Jaggi | 829b9cd | 2017-01-23 16:20:53 +0100 | [diff] [blame^] | 155 | final Bitmap b = Bitmap.createHardwareBitmap(snapshot); |
| 156 | fillEmptyBackground(c, b); |
| 157 | c.drawBitmap(b, 0, 0, null); |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 158 | mSurface.unlockCanvasAndPost(c); |
| 159 | final boolean reportNextDraw; |
| 160 | synchronized (mService.mWindowMap) { |
| 161 | mHasDrawn = true; |
| 162 | reportNextDraw = mReportNextDraw; |
| 163 | } |
| 164 | if (reportNextDraw) { |
| 165 | reportDrawn(); |
| 166 | } |
Jorim Jaggi | 2f24b65 | 2017-01-18 02:17:37 +0100 | [diff] [blame] | 167 | mSurface.release(); |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 168 | } |
| 169 | |
Jorim Jaggi | 829b9cd | 2017-01-23 16:20:53 +0100 | [diff] [blame^] | 170 | @VisibleForTesting |
| 171 | void fillEmptyBackground(Canvas c, Bitmap b) { |
| 172 | final boolean fillHorizontally = c.getWidth() > b.getWidth(); |
| 173 | final boolean fillVertically = c.getHeight() > b.getHeight(); |
| 174 | if (fillHorizontally) { |
| 175 | c.drawRect(b.getWidth(), 0, c.getWidth(), fillVertically |
| 176 | ? b.getHeight() |
| 177 | : c.getHeight(), |
| 178 | mFillBackgroundPaint); |
| 179 | } |
| 180 | if (fillVertically) { |
| 181 | c.drawRect(0, b.getHeight(), c.getWidth(), c.getHeight(), mFillBackgroundPaint); |
| 182 | } |
| 183 | } |
| 184 | |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 185 | private void reportDrawn() { |
| 186 | synchronized (mService.mWindowMap) { |
| 187 | mReportNextDraw = false; |
| 188 | } |
| 189 | try { |
| 190 | mSession.finishDrawing(mWindow); |
| 191 | } catch (RemoteException e) { |
| 192 | // Local call. |
| 193 | } |
| 194 | } |
| 195 | |
Jorim Jaggi | 829b9cd | 2017-01-23 16:20:53 +0100 | [diff] [blame^] | 196 | private static Handler sHandler = new Handler(Looper.getMainLooper()) { |
Jorim Jaggi | 02886a8 | 2016-12-06 09:10:06 -0800 | [diff] [blame] | 197 | |
| 198 | @Override |
| 199 | public void handleMessage(Message msg) { |
| 200 | switch (msg.what) { |
| 201 | case MSG_REPORT_DRAW: |
| 202 | final boolean hasDrawn; |
| 203 | final TaskSnapshotSurface surface = (TaskSnapshotSurface) msg.obj; |
| 204 | synchronized (surface.mService.mWindowMap) { |
| 205 | hasDrawn = surface.mHasDrawn; |
| 206 | if (!hasDrawn) { |
| 207 | surface.mReportNextDraw = true; |
| 208 | } |
| 209 | } |
| 210 | if (hasDrawn) { |
| 211 | surface.reportDrawn(); |
| 212 | } |
| 213 | break; |
| 214 | } |
| 215 | } |
| 216 | }; |
| 217 | |
| 218 | private static class Window extends BaseIWindow { |
| 219 | |
| 220 | private TaskSnapshotSurface mOuter; |
| 221 | |
| 222 | public void setOuter(TaskSnapshotSurface outer) { |
| 223 | mOuter = outer; |
| 224 | } |
| 225 | |
| 226 | @Override |
| 227 | public void resized(Rect frame, Rect overscanInsets, Rect contentInsets, Rect visibleInsets, |
| 228 | Rect stableInsets, Rect outsets, boolean reportDraw, Configuration newConfig, |
| 229 | Rect backDropFrame, boolean forceLayout, boolean alwaysConsumeNavBar) { |
| 230 | if (reportDraw) { |
| 231 | sHandler.obtainMessage(MSG_REPORT_DRAW, mOuter).sendToTarget(); |
| 232 | } |
| 233 | } |
| 234 | } |
| 235 | } |