blob: cfcbbd0358b57187e6383ce0afc5b1b85766a6ec [file] [log] [blame]
Jorim Jaggi02886a82016-12-06 09:10:06 -08001/*
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
17package com.android.server.wm;
18
19import static android.view.WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS;
20import static android.view.WindowManager.LayoutParams.FLAG_LAYOUT_INSET_DECOR;
21import static android.view.WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN;
22import static android.view.WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
23import static android.view.WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE;
24import static android.view.WindowManager.LayoutParams.PRIVATE_FLAG_TASK_SNAPSHOT;
25import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION_STARTING;
26import static com.android.server.wm.WindowManagerDebugConfig.TAG_WITH_CLASS_NAME;
27import static com.android.server.wm.WindowManagerDebugConfig.TAG_WM;
28
Jorim Jaggi829b9cd2017-01-23 16:20:53 +010029import android.app.ActivityManager.TaskDescription;
Jorim Jaggi02886a82016-12-06 09:10:06 -080030import android.content.res.Configuration;
31import android.graphics.Bitmap;
32import android.graphics.Canvas;
Jorim Jaggi829b9cd2017-01-23 16:20:53 +010033import android.graphics.Color;
Jorim Jaggi02886a82016-12-06 09:10:06 -080034import android.graphics.GraphicBuffer;
Jorim Jaggi829b9cd2017-01-23 16:20:53 +010035import android.graphics.Paint;
Jorim Jaggi02886a82016-12-06 09:10:06 -080036import android.graphics.Rect;
37import android.os.Handler;
Jorim Jaggi829b9cd2017-01-23 16:20:53 +010038import android.os.Looper;
Jorim Jaggi02886a82016-12-06 09:10:06 -080039import android.os.Message;
40import android.os.RemoteException;
41import android.util.Slog;
Jorim Jaggi02886a82016-12-06 09:10:06 -080042import android.view.IWindowSession;
43import android.view.Surface;
44import android.view.View;
45import android.view.ViewGroup.LayoutParams;
46import android.view.WindowManager;
47import android.view.WindowManagerGlobal;
48import android.view.WindowManagerPolicy.StartingSurface;
49
Jorim Jaggi829b9cd2017-01-23 16:20:53 +010050import com.android.internal.annotations.VisibleForTesting;
Jorim Jaggi02886a82016-12-06 09:10:06 -080051import 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 */
58class 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 Jaggi829b9cd2017-01-23 16:20:53 +010069 private Paint mFillBackgroundPaint = new Paint();
Jorim Jaggi02886a82016-12-06 09:10:06 -080070
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 Jaggi829b9cd2017-01-23 16:20:53 +010082 int fillBackgroundColor = Color.WHITE;
Jorim Jaggi02886a82016-12-06 09:10:06 -080083 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 Jaggi829b9cd2017-01-23 16:20:53 +0100100 if (token.mTask != null) {
101 final TaskDescription taskDescription = token.mTask.getTaskDescription();
102 if (taskDescription != null) {
103 fillBackgroundColor = taskDescription.getBackgroundColor();
104 }
105 }
Jorim Jaggi02886a82016-12-06 09:10:06 -0800106 }
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 Jaggi829b9cd2017-01-23 16:20:53 +0100119 surface, fillBackgroundColor);
Jorim Jaggi02886a82016-12-06 09:10:06 -0800120 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 Jaggi829b9cd2017-01-23 16:20:53 +0100132 @VisibleForTesting
133 TaskSnapshotSurface(WindowManagerService service, Window window, Surface surface,
134 int fillBackgroundColor) {
Jorim Jaggi02886a82016-12-06 09:10:06 -0800135 mService = service;
136 mSession = WindowManagerGlobal.getWindowSession();
137 mWindow = window;
138 mSurface = surface;
Jorim Jaggi829b9cd2017-01-23 16:20:53 +0100139 mFillBackgroundPaint.setColor(fillBackgroundColor);
Jorim Jaggi02886a82016-12-06 09:10:06 -0800140 }
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 Jaggi829b9cd2017-01-23 16:20:53 +0100155 final Bitmap b = Bitmap.createHardwareBitmap(snapshot);
156 fillEmptyBackground(c, b);
157 c.drawBitmap(b, 0, 0, null);
Jorim Jaggi02886a82016-12-06 09:10:06 -0800158 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 Jaggi2f24b652017-01-18 02:17:37 +0100167 mSurface.release();
Jorim Jaggi02886a82016-12-06 09:10:06 -0800168 }
169
Jorim Jaggi829b9cd2017-01-23 16:20:53 +0100170 @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 Jaggi02886a82016-12-06 09:10:06 -0800185 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 Jaggi829b9cd2017-01-23 16:20:53 +0100196 private static Handler sHandler = new Handler(Looper.getMainLooper()) {
Jorim Jaggi02886a82016-12-06 09:10:06 -0800197
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}