blob: a1cbc1d075756055d992ffb0df0a1f3c1d008002 [file] [log] [blame]
Jeff Brown98365d72012-08-19 20:30:52 -07001/*
2 * Copyright (C) 2012 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 android.view;
18
19import android.animation.ValueAnimator;
20import android.app.ActivityManager;
21import android.content.ComponentCallbacks2;
Alan Viverette5e1565e2014-07-29 16:14:25 -070022import android.content.Context;
Alan Viveretted70b9e72015-05-27 14:29:20 -070023import android.content.pm.ApplicationInfo;
Jeff Brown98365d72012-08-19 20:30:52 -070024import android.content.res.Configuration;
Jeff Brown98365d72012-08-19 20:30:52 -070025import android.os.IBinder;
Jeff Brown98365d72012-08-19 20:30:52 -070026import android.os.RemoteException;
27import android.os.ServiceManager;
28import android.os.SystemProperties;
29import android.util.AndroidRuntimeException;
Craig Mautner8f303ad2013-06-14 11:32:22 -070030import android.util.ArraySet;
Jeff Brown98365d72012-08-19 20:30:52 -070031import android.util.Log;
32import android.view.inputmethod.InputMethodManager;
Jorim Jaggi4846ee32016-01-07 17:39:12 +010033
Dianne Hackborn8c841092013-06-24 13:46:13 -070034import com.android.internal.util.FastPrintWriter;
Jeff Brown98365d72012-08-19 20:30:52 -070035
36import java.io.FileDescriptor;
37import java.io.FileOutputStream;
38import java.io.PrintWriter;
Craig Mautner652fdfa2013-06-06 07:51:57 -070039import java.util.ArrayList;
Jeff Brown98365d72012-08-19 20:30:52 -070040
41/**
42 * Provides low-level communication with the system window manager for
43 * operations that are not associated with any particular context.
44 *
45 * This class is only used internally to implement global functions where
46 * the caller already knows the display and relevant compatibility information
47 * for the operation. For most purposes, you should use {@link WindowManager} instead
48 * since it is bound to a context.
49 *
50 * @see WindowManagerImpl
51 * @hide
52 */
53public final class WindowManagerGlobal {
54 private static final String TAG = "WindowManager";
55
56 /**
57 * The user is navigating with keys (not the touch screen), so
58 * navigational focus should be shown.
59 */
60 public static final int RELAYOUT_RES_IN_TOUCH_MODE = 0x1;
61
62 /**
63 * This is the first time the window is being drawn,
64 * so the client must call drawingFinished() when done
65 */
66 public static final int RELAYOUT_RES_FIRST_TIME = 0x2;
67
68 /**
69 * The window manager has changed the surface from the last call.
70 */
71 public static final int RELAYOUT_RES_SURFACE_CHANGED = 0x4;
72
73 /**
Jorim Jaggi4846ee32016-01-07 17:39:12 +010074 * The window is being resized by dragging on the docked divider. The client should render
75 * at (0, 0) and extend its background to the background frame passed into
76 * {@link IWindow#resized}.
77 */
78 public static final int RELAYOUT_RES_DRAG_RESIZING_DOCKED = 0x8;
79
80 /**
Chong Zhang0275e392015-09-17 10:41:44 -070081 * The window is being resized by dragging one of the window corners,
Filip Gruszczynski63a35e22015-11-05 15:38:59 -080082 * in this case the surface would be fullscreen-sized. The client should
Chong Zhang0275e392015-09-17 10:41:44 -070083 * render to the actual frame location (instead of (0,curScrollY)).
84 */
Jorim Jaggi4846ee32016-01-07 17:39:12 +010085 public static final int RELAYOUT_RES_DRAG_RESIZING_FREEFORM = 0x10;
Chong Zhang0275e392015-09-17 10:41:44 -070086
87 /**
Chong Zhangf4abc2b2015-11-12 23:40:58 -080088 * The window manager has changed the size of the surface from the last call.
89 */
Jorim Jaggi4846ee32016-01-07 17:39:12 +010090 public static final int RELAYOUT_RES_SURFACE_RESIZED = 0x20;
Chong Zhangf4abc2b2015-11-12 23:40:58 -080091
92 /**
Jorim Jaggi0ffd49c2016-02-12 15:04:21 -080093 * In multi-window we force show the navigation bar. Because we don't want that the surface size
94 * changes in this mode, we instead have a flag whether the navigation bar size should always be
95 * consumed, so the app is treated like there is no virtual navigation bar at all.
96 */
97 public static final int RELAYOUT_RES_CONSUME_ALWAYS_NAV_BAR = 0x40;
98
99 /**
Jeff Brown98365d72012-08-19 20:30:52 -0700100 * Flag for relayout: the client will be later giving
101 * internal insets; as a result, the window will not impact other window
102 * layouts until the insets are given.
103 */
104 public static final int RELAYOUT_INSETS_PENDING = 0x1;
105
106 /**
107 * Flag for relayout: the client may be currently using the current surface,
108 * so if it is to be destroyed as a part of the relayout the destroy must
109 * be deferred until later. The client will call performDeferredDestroy()
110 * when it is okay.
111 */
112 public static final int RELAYOUT_DEFER_SURFACE_DESTROY = 0x2;
113
114 public static final int ADD_FLAG_APP_VISIBLE = 0x2;
115 public static final int ADD_FLAG_IN_TOUCH_MODE = RELAYOUT_RES_IN_TOUCH_MODE;
116
Jorim Jaggi0ffd49c2016-02-12 15:04:21 -0800117 /**
118 * Like {@link #RELAYOUT_RES_CONSUME_ALWAYS_NAV_BAR}, but as a "hint" when adding the window.
119 */
120 public static final int ADD_FLAG_ALWAYS_CONSUME_NAV_BAR = 0x4;
121
Jeff Brown98365d72012-08-19 20:30:52 -0700122 public static final int ADD_OKAY = 0;
123 public static final int ADD_BAD_APP_TOKEN = -1;
124 public static final int ADD_BAD_SUBWINDOW_TOKEN = -2;
125 public static final int ADD_NOT_APP_TOKEN = -3;
126 public static final int ADD_APP_EXITING = -4;
127 public static final int ADD_DUPLICATE_ADD = -5;
128 public static final int ADD_STARTING_NOT_NEEDED = -6;
129 public static final int ADD_MULTIPLE_SINGLETON = -7;
130 public static final int ADD_PERMISSION_DENIED = -8;
Craig Mautner2d5618c2012-10-18 13:55:47 -0700131 public static final int ADD_INVALID_DISPLAY = -9;
Wale Ogunwale74bf0652015-01-12 10:24:36 -0800132 public static final int ADD_INVALID_TYPE = -10;
Jeff Brown98365d72012-08-19 20:30:52 -0700133
134 private static WindowManagerGlobal sDefaultWindowManager;
135 private static IWindowManager sWindowManagerService;
136 private static IWindowSession sWindowSession;
137
138 private final Object mLock = new Object();
139
Craig Mautner652fdfa2013-06-06 07:51:57 -0700140 private final ArrayList<View> mViews = new ArrayList<View>();
141 private final ArrayList<ViewRootImpl> mRoots = new ArrayList<ViewRootImpl>();
142 private final ArrayList<WindowManager.LayoutParams> mParams =
143 new ArrayList<WindowManager.LayoutParams>();
Craig Mautner8f303ad2013-06-14 11:32:22 -0700144 private final ArraySet<View> mDyingViews = new ArraySet<View>();
Jeff Brown98365d72012-08-19 20:30:52 -0700145
146 private Runnable mSystemPropertyUpdater;
147
148 private WindowManagerGlobal() {
149 }
150
Chet Haase0d1c27a2014-11-03 18:35:16 +0000151 public static void initialize() {
152 getWindowManagerService();
153 }
154
Jeff Brown98365d72012-08-19 20:30:52 -0700155 public static WindowManagerGlobal getInstance() {
156 synchronized (WindowManagerGlobal.class) {
157 if (sDefaultWindowManager == null) {
158 sDefaultWindowManager = new WindowManagerGlobal();
159 }
160 return sDefaultWindowManager;
161 }
162 }
163
164 public static IWindowManager getWindowManagerService() {
165 synchronized (WindowManagerGlobal.class) {
166 if (sWindowManagerService == null) {
167 sWindowManagerService = IWindowManager.Stub.asInterface(
168 ServiceManager.getService("window"));
Chet Haase0d1c27a2014-11-03 18:35:16 +0000169 try {
170 sWindowManagerService = getWindowManagerService();
171 ValueAnimator.setDurationScale(sWindowManagerService.getCurrentAnimatorScale());
172 } catch (RemoteException e) {
173 Log.e(TAG, "Failed to get WindowManagerService, cannot set animator scale", e);
174 }
Jeff Brown98365d72012-08-19 20:30:52 -0700175 }
176 return sWindowManagerService;
177 }
178 }
179
Jeff Brownf9e989d2013-04-04 23:04:03 -0700180 public static IWindowSession getWindowSession() {
Jeff Brown98365d72012-08-19 20:30:52 -0700181 synchronized (WindowManagerGlobal.class) {
182 if (sWindowSession == null) {
183 try {
Jeff Brownf9e989d2013-04-04 23:04:03 -0700184 InputMethodManager imm = InputMethodManager.getInstance();
Jeff Brown98365d72012-08-19 20:30:52 -0700185 IWindowManager windowManager = getWindowManagerService();
186 sWindowSession = windowManager.openSession(
Dianne Hackborneb94fa72014-06-03 17:48:12 -0700187 new IWindowSessionCallback.Stub() {
188 @Override
189 public void onAnimatorScaleChanged(float scale) {
190 ValueAnimator.setDurationScale(scale);
191 }
192 },
Jeff Brown98365d72012-08-19 20:30:52 -0700193 imm.getClient(), imm.getInputContext());
Jeff Brown98365d72012-08-19 20:30:52 -0700194 } catch (RemoteException e) {
195 Log.e(TAG, "Failed to open window session", e);
196 }
197 }
198 return sWindowSession;
199 }
200 }
201
202 public static IWindowSession peekWindowSession() {
203 synchronized (WindowManagerGlobal.class) {
204 return sWindowSession;
205 }
206 }
207
Siva Velusamy945bfb62013-01-06 16:03:12 -0800208 public String[] getViewRootNames() {
209 synchronized (mLock) {
Craig Mautner652fdfa2013-06-06 07:51:57 -0700210 final int numRoots = mRoots.size();
211 String[] mViewRoots = new String[numRoots];
212 for (int i = 0; i < numRoots; ++i) {
213 mViewRoots[i] = getWindowName(mRoots.get(i));
Siva Velusamy945bfb62013-01-06 16:03:12 -0800214 }
215 return mViewRoots;
216 }
217 }
218
Dianne Hackborna7bb6fb2015-02-03 18:13:40 -0800219 public ArrayList<ViewRootImpl> getRootViews(IBinder token) {
220 ArrayList<ViewRootImpl> views = new ArrayList<>();
221 synchronized (mLock) {
222 final int numRoots = mRoots.size();
223 for (int i = 0; i < numRoots; ++i) {
224 WindowManager.LayoutParams params = mParams.get(i);
225 if (params.token == null) {
226 continue;
227 }
228 if (params.token != token) {
229 boolean isChild = false;
230 if (params.type >= WindowManager.LayoutParams.FIRST_SUB_WINDOW
231 && params.type <= WindowManager.LayoutParams.LAST_SUB_WINDOW) {
232 for (int j = 0 ; j < numRoots; ++j) {
233 View viewj = mViews.get(j);
234 WindowManager.LayoutParams paramsj = mParams.get(j);
235 if (params.token == viewj.getWindowToken()
236 && paramsj.token == token) {
237 isChild = true;
238 break;
239 }
240 }
241 }
242 if (!isChild) {
243 continue;
244 }
245 }
246 views.add(mRoots.get(i));
247 }
248 }
249 return views;
250 }
251
Siva Velusamy945bfb62013-01-06 16:03:12 -0800252 public View getRootView(String name) {
253 synchronized (mLock) {
Craig Mautner652fdfa2013-06-06 07:51:57 -0700254 for (int i = mRoots.size() - 1; i >= 0; --i) {
255 final ViewRootImpl root = mRoots.get(i);
Siva Velusamy945bfb62013-01-06 16:03:12 -0800256 if (name.equals(getWindowName(root))) return root.getView();
257 }
258 }
259
260 return null;
261 }
262
Jeff Brown98365d72012-08-19 20:30:52 -0700263 public void addView(View view, ViewGroup.LayoutParams params,
264 Display display, Window parentWindow) {
265 if (view == null) {
266 throw new IllegalArgumentException("view must not be null");
267 }
268 if (display == null) {
269 throw new IllegalArgumentException("display must not be null");
270 }
271 if (!(params instanceof WindowManager.LayoutParams)) {
272 throw new IllegalArgumentException("Params must be WindowManager.LayoutParams");
273 }
274
Alan Viverette9ecb73c2014-12-15 13:40:28 -0800275 final WindowManager.LayoutParams wparams = (WindowManager.LayoutParams) params;
Jeff Brown98365d72012-08-19 20:30:52 -0700276 if (parentWindow != null) {
277 parentWindow.adjustLayoutParamsForSubWindow(wparams);
Alan Viverette9b0ab652015-03-18 14:21:04 -0700278 } else {
279 // If there's no parent, then hardware acceleration for this view is
280 // set from the application's hardware acceleration setting.
Alan Viverette5e1565e2014-07-29 16:14:25 -0700281 final Context context = view.getContext();
Alan Viverette9b0ab652015-03-18 14:21:04 -0700282 if (context != null
Alan Viveretted70b9e72015-05-27 14:29:20 -0700283 && (context.getApplicationInfo().flags
284 & ApplicationInfo.FLAG_HARDWARE_ACCELERATED) != 0) {
Alan Viverette5e1565e2014-07-29 16:14:25 -0700285 wparams.flags |= WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED;
286 }
Jeff Brown98365d72012-08-19 20:30:52 -0700287 }
288
289 ViewRootImpl root;
290 View panelParentView = null;
291
292 synchronized (mLock) {
293 // Start watching for system property changes.
294 if (mSystemPropertyUpdater == null) {
295 mSystemPropertyUpdater = new Runnable() {
296 @Override public void run() {
297 synchronized (mLock) {
Craig Mautner652fdfa2013-06-06 07:51:57 -0700298 for (int i = mRoots.size() - 1; i >= 0; --i) {
299 mRoots.get(i).loadSystemProperties();
Jeff Brown98365d72012-08-19 20:30:52 -0700300 }
301 }
302 }
303 };
304 SystemProperties.addChangeCallback(mSystemPropertyUpdater);
305 }
306
307 int index = findViewLocked(view, false);
308 if (index >= 0) {
Craig Mautner8f303ad2013-06-14 11:32:22 -0700309 if (mDyingViews.contains(view)) {
310 // Don't wait for MSG_DIE to make it's way through root's queue.
311 mRoots.get(index).doDie();
312 } else {
313 throw new IllegalStateException("View " + view
314 + " has already been added to the window manager.");
315 }
316 // The previous removeView() had not completed executing. Now it has.
Jeff Brown98365d72012-08-19 20:30:52 -0700317 }
318
319 // If this is a panel window, then find the window it is being
320 // attached to for future reference.
321 if (wparams.type >= WindowManager.LayoutParams.FIRST_SUB_WINDOW &&
322 wparams.type <= WindowManager.LayoutParams.LAST_SUB_WINDOW) {
Craig Mautner652fdfa2013-06-06 07:51:57 -0700323 final int count = mViews.size();
324 for (int i = 0; i < count; i++) {
325 if (mRoots.get(i).mWindow.asBinder() == wparams.token) {
326 panelParentView = mViews.get(i);
Jeff Brown98365d72012-08-19 20:30:52 -0700327 }
328 }
329 }
330
331 root = new ViewRootImpl(view.getContext(), display);
332
333 view.setLayoutParams(wparams);
334
Craig Mautner652fdfa2013-06-06 07:51:57 -0700335 mViews.add(view);
336 mRoots.add(root);
337 mParams.add(wparams);
Jeff Brown98365d72012-08-19 20:30:52 -0700338 }
339
340 // do this last because it fires off messages to start doing things
Craig Mautner6018aee2012-10-23 14:27:49 -0700341 try {
342 root.setView(view, wparams, panelParentView);
343 } catch (RuntimeException e) {
344 // BadTokenException or InvalidDisplayException, clean up.
345 synchronized (mLock) {
346 final int index = findViewLocked(view, false);
347 if (index >= 0) {
348 removeViewLocked(index, true);
349 }
350 }
351 throw e;
352 }
Jeff Brown98365d72012-08-19 20:30:52 -0700353 }
354
355 public void updateViewLayout(View view, ViewGroup.LayoutParams params) {
356 if (view == null) {
357 throw new IllegalArgumentException("view must not be null");
358 }
359 if (!(params instanceof WindowManager.LayoutParams)) {
360 throw new IllegalArgumentException("Params must be WindowManager.LayoutParams");
361 }
362
363 final WindowManager.LayoutParams wparams = (WindowManager.LayoutParams)params;
364
365 view.setLayoutParams(wparams);
366
367 synchronized (mLock) {
368 int index = findViewLocked(view, true);
Craig Mautner652fdfa2013-06-06 07:51:57 -0700369 ViewRootImpl root = mRoots.get(index);
370 mParams.remove(index);
371 mParams.add(index, wparams);
Jeff Brown98365d72012-08-19 20:30:52 -0700372 root.setLayoutParams(wparams, false);
373 }
374 }
375
376 public void removeView(View view, boolean immediate) {
377 if (view == null) {
378 throw new IllegalArgumentException("view must not be null");
379 }
380
381 synchronized (mLock) {
382 int index = findViewLocked(view, true);
Craig Mautner652fdfa2013-06-06 07:51:57 -0700383 View curView = mRoots.get(index).getView();
Craig Mautner05eb7302013-06-03 17:24:21 -0700384 removeViewLocked(index, immediate);
Jeff Brown98365d72012-08-19 20:30:52 -0700385 if (curView == view) {
386 return;
387 }
388
389 throw new IllegalStateException("Calling with view " + view
390 + " but the ViewAncestor is attached to " + curView);
391 }
392 }
393
394 public void closeAll(IBinder token, String who, String what) {
395 synchronized (mLock) {
Craig Mautner652fdfa2013-06-06 07:51:57 -0700396 int count = mViews.size();
Jeff Brown98365d72012-08-19 20:30:52 -0700397 //Log.i("foo", "Closing all windows of " + token);
Craig Mautner05eb7302013-06-03 17:24:21 -0700398 for (int i = 0; i < count; i++) {
Jeff Brown98365d72012-08-19 20:30:52 -0700399 //Log.i("foo", "@ " + i + " token " + mParams[i].token
400 // + " view " + mRoots[i].getView());
Craig Mautner652fdfa2013-06-06 07:51:57 -0700401 if (token == null || mParams.get(i).token == token) {
402 ViewRootImpl root = mRoots.get(i);
Jeff Brown98365d72012-08-19 20:30:52 -0700403
404 //Log.i("foo", "Force closing " + root);
405 if (who != null) {
406 WindowLeaked leak = new WindowLeaked(
407 what + " " + who + " has leaked window "
408 + root.getView() + " that was originally added here");
409 leak.setStackTrace(root.getLocation().getStackTrace());
Craig Mautner05eb7302013-06-03 17:24:21 -0700410 Log.e(TAG, "", leak);
Jeff Brown98365d72012-08-19 20:30:52 -0700411 }
412
413 removeViewLocked(i, false);
Jeff Brown98365d72012-08-19 20:30:52 -0700414 }
415 }
416 }
417 }
418
Craig Mautner05eb7302013-06-03 17:24:21 -0700419 private void removeViewLocked(int index, boolean immediate) {
Craig Mautner652fdfa2013-06-06 07:51:57 -0700420 ViewRootImpl root = mRoots.get(index);
Jeff Brown98365d72012-08-19 20:30:52 -0700421 View view = root.getView();
422
423 if (view != null) {
Jeff Brownf9e989d2013-04-04 23:04:03 -0700424 InputMethodManager imm = InputMethodManager.getInstance();
Jeff Brown98365d72012-08-19 20:30:52 -0700425 if (imm != null) {
Craig Mautner652fdfa2013-06-06 07:51:57 -0700426 imm.windowDismissed(mViews.get(index).getWindowToken());
Jeff Brown98365d72012-08-19 20:30:52 -0700427 }
428 }
Craig Mautner8f303ad2013-06-14 11:32:22 -0700429 boolean deferred = root.die(immediate);
Craig Mautner92098c72013-06-10 11:27:26 -0700430 if (view != null) {
431 view.assignParent(null);
Craig Mautner8f303ad2013-06-14 11:32:22 -0700432 if (deferred) {
433 mDyingViews.add(view);
434 }
Craig Mautner92098c72013-06-10 11:27:26 -0700435 }
Craig Mautner05eb7302013-06-03 17:24:21 -0700436 }
Jeff Brown98365d72012-08-19 20:30:52 -0700437
Craig Mautner05eb7302013-06-03 17:24:21 -0700438 void doRemoveView(ViewRootImpl root) {
439 synchronized (mLock) {
Craig Mautner652fdfa2013-06-06 07:51:57 -0700440 final int index = mRoots.indexOf(root);
441 if (index >= 0) {
442 mRoots.remove(index);
Craig Mautner652fdfa2013-06-06 07:51:57 -0700443 mParams.remove(index);
Craig Mautner8f303ad2013-06-14 11:32:22 -0700444 final View view = mViews.remove(index);
445 mDyingViews.remove(view);
Jeff Brown98365d72012-08-19 20:30:52 -0700446 }
447 }
John Reck51aaf902015-12-02 15:08:07 -0800448 if (ThreadedRenderer.sTrimForeground && ThreadedRenderer.isAvailable()) {
John Reck73840ea2014-09-22 07:39:18 -0700449 doTrimForeground();
450 }
Jeff Brown98365d72012-08-19 20:30:52 -0700451 }
452
453 private int findViewLocked(View view, boolean required) {
Craig Mautner652fdfa2013-06-06 07:51:57 -0700454 final int index = mViews.indexOf(view);
455 if (required && index < 0) {
Craig Mautner05eb7302013-06-03 17:24:21 -0700456 throw new IllegalArgumentException("View=" + view + " not attached to window manager");
Craig Mautner6018aee2012-10-23 14:27:49 -0700457 }
Craig Mautner652fdfa2013-06-06 07:51:57 -0700458 return index;
Jeff Brown98365d72012-08-19 20:30:52 -0700459 }
460
John Reckf47a5942014-06-30 16:20:04 -0700461 public static boolean shouldDestroyEglContext(int trimLevel) {
462 // On low-end gfx devices we trim when memory is moderate;
463 // on high-end devices we do this when low.
464 if (trimLevel >= ComponentCallbacks2.TRIM_MEMORY_COMPLETE) {
465 return true;
466 }
467 if (trimLevel >= ComponentCallbacks2.TRIM_MEMORY_MODERATE
468 && !ActivityManager.isHighEndGfx()) {
469 return true;
470 }
471 return false;
472 }
473
474 public void trimMemory(int level) {
John Reck51aaf902015-12-02 15:08:07 -0800475 if (ThreadedRenderer.isAvailable()) {
John Reckf47a5942014-06-30 16:20:04 -0700476 if (shouldDestroyEglContext(level)) {
Jeff Brown98365d72012-08-19 20:30:52 -0700477 // Destroy all hardware surfaces and resources associated to
478 // known windows
479 synchronized (mLock) {
Craig Mautner652fdfa2013-06-06 07:51:57 -0700480 for (int i = mRoots.size() - 1; i >= 0; --i) {
Romain Guy46bfc482013-08-16 18:38:29 -0700481 mRoots.get(i).destroyHardwareResources();
Jeff Brown98365d72012-08-19 20:30:52 -0700482 }
483 }
484 // Force a full memory flush
John Reckf47a5942014-06-30 16:20:04 -0700485 level = ComponentCallbacks2.TRIM_MEMORY_COMPLETE;
Jeff Brown98365d72012-08-19 20:30:52 -0700486 }
487
John Reck51aaf902015-12-02 15:08:07 -0800488 ThreadedRenderer.trimMemory(level);
John Reck73840ea2014-09-22 07:39:18 -0700489
John Reck51aaf902015-12-02 15:08:07 -0800490 if (ThreadedRenderer.sTrimForeground) {
John Reck73840ea2014-09-22 07:39:18 -0700491 doTrimForeground();
492 }
493 }
494 }
495
496 public static void trimForeground() {
John Reck51aaf902015-12-02 15:08:07 -0800497 if (ThreadedRenderer.sTrimForeground && ThreadedRenderer.isAvailable()) {
John Reck73840ea2014-09-22 07:39:18 -0700498 WindowManagerGlobal wm = WindowManagerGlobal.getInstance();
499 wm.doTrimForeground();
500 }
501 }
502
503 private void doTrimForeground() {
504 boolean hasVisibleWindows = false;
505 synchronized (mLock) {
506 for (int i = mRoots.size() - 1; i >= 0; --i) {
John Reckccf2fa02014-09-25 08:33:05 -0700507 final ViewRootImpl root = mRoots.get(i);
508 if (root.mView != null && root.getHostVisibility() == View.VISIBLE
509 && root.mAttachInfo.mHardwareRenderer != null) {
John Reck73840ea2014-09-22 07:39:18 -0700510 hasVisibleWindows = true;
511 } else {
John Reckccf2fa02014-09-25 08:33:05 -0700512 root.destroyHardwareResources();
John Reck73840ea2014-09-22 07:39:18 -0700513 }
514 }
515 }
516 if (!hasVisibleWindows) {
John Reck51aaf902015-12-02 15:08:07 -0800517 ThreadedRenderer.trimMemory(
John Reck73840ea2014-09-22 07:39:18 -0700518 ComponentCallbacks2.TRIM_MEMORY_COMPLETE);
Jeff Brown98365d72012-08-19 20:30:52 -0700519 }
520 }
521
John Reckba6adf62015-02-19 14:36:50 -0800522 public void dumpGfxInfo(FileDescriptor fd, String[] args) {
Jeff Brown98365d72012-08-19 20:30:52 -0700523 FileOutputStream fout = new FileOutputStream(fd);
Dianne Hackborn8c841092013-06-24 13:46:13 -0700524 PrintWriter pw = new FastPrintWriter(fout);
Jeff Brown98365d72012-08-19 20:30:52 -0700525 try {
526 synchronized (mLock) {
Craig Mautner652fdfa2013-06-06 07:51:57 -0700527 final int count = mViews.size();
Jeff Brown98365d72012-08-19 20:30:52 -0700528
Craig Mautner652fdfa2013-06-06 07:51:57 -0700529 pw.println("Profile data in ms:");
Jeff Brown98365d72012-08-19 20:30:52 -0700530
Craig Mautner652fdfa2013-06-06 07:51:57 -0700531 for (int i = 0; i < count; i++) {
532 ViewRootImpl root = mRoots.get(i);
533 String name = getWindowName(root);
John Reck73840ea2014-09-22 07:39:18 -0700534 pw.printf("\n\t%s (visibility=%d)", name, root.getHostVisibility());
Jeff Brown98365d72012-08-19 20:30:52 -0700535
John Reck51aaf902015-12-02 15:08:07 -0800536 ThreadedRenderer renderer =
Craig Mautner652fdfa2013-06-06 07:51:57 -0700537 root.getView().mAttachInfo.mHardwareRenderer;
538 if (renderer != null) {
John Reckba6adf62015-02-19 14:36:50 -0800539 renderer.dumpGfxInfo(pw, fd, args);
Jeff Brown98365d72012-08-19 20:30:52 -0700540 }
Jeff Brown98365d72012-08-19 20:30:52 -0700541 }
Craig Mautner652fdfa2013-06-06 07:51:57 -0700542
543 pw.println("\nView hierarchy:\n");
544
545 int viewsCount = 0;
546 int displayListsSize = 0;
547 int[] info = new int[2];
548
549 for (int i = 0; i < count; i++) {
550 ViewRootImpl root = mRoots.get(i);
551 root.dumpGfxInfo(info);
552
553 String name = getWindowName(root);
554 pw.printf(" %s\n %d views, %.2f kB of display lists",
555 name, info[0], info[1] / 1024.0f);
Craig Mautner652fdfa2013-06-06 07:51:57 -0700556 pw.printf("\n\n");
557
558 viewsCount += info[0];
559 displayListsSize += info[1];
560 }
561
562 pw.printf("\nTotal ViewRootImpl: %d\n", count);
563 pw.printf("Total Views: %d\n", viewsCount);
564 pw.printf("Total DisplayList: %.2f kB\n\n", displayListsSize / 1024.0f);
Jeff Brown98365d72012-08-19 20:30:52 -0700565 }
566 } finally {
567 pw.flush();
568 }
569 }
570
571 private static String getWindowName(ViewRootImpl root) {
572 return root.mWindowAttributes.getTitle() + "/" +
573 root.getClass().getName() + '@' + Integer.toHexString(root.hashCode());
574 }
575
576 public void setStoppedState(IBinder token, boolean stopped) {
577 synchronized (mLock) {
Craig Mautner652fdfa2013-06-06 07:51:57 -0700578 int count = mViews.size();
579 for (int i = 0; i < count; i++) {
580 if (token == null || mParams.get(i).token == token) {
581 ViewRootImpl root = mRoots.get(i);
George Mount41725de2015-04-09 08:23:05 -0700582 root.setWindowStopped(stopped);
Jeff Brown98365d72012-08-19 20:30:52 -0700583 }
584 }
585 }
586 }
587
588 public void reportNewConfiguration(Configuration config) {
589 synchronized (mLock) {
Craig Mautner652fdfa2013-06-06 07:51:57 -0700590 int count = mViews.size();
591 config = new Configuration(config);
592 for (int i=0; i < count; i++) {
593 ViewRootImpl root = mRoots.get(i);
594 root.requestUpdateConfiguration(config);
Jeff Brown98365d72012-08-19 20:30:52 -0700595 }
596 }
597 }
Craig Mautnerbc57cd12013-08-19 15:47:42 -0700598
599 /** @hide */
600 public void changeCanvasOpacity(IBinder token, boolean opaque) {
601 if (token == null) {
602 return;
603 }
604 synchronized (mLock) {
605 for (int i = mParams.size() - 1; i >= 0; --i) {
606 if (mParams.get(i).token == token) {
607 mRoots.get(i).changeCanvasOpacity(opaque);
608 return;
609 }
610 }
611 }
612 }
Jeff Brown98365d72012-08-19 20:30:52 -0700613}
614
615final class WindowLeaked extends AndroidRuntimeException {
616 public WindowLeaked(String msg) {
617 super(msg);
618 }
619}