blob: e97b36683362e0b44e05227c15ac5f3e9713bc68 [file] [log] [blame]
Brad Fitzpatrick68044332010-11-22 18:19:48 -08001/*
2 * Copyright (C) 2010 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
Dianne Hackborn6e1eb762011-02-17 16:07:28 -080017package com.android.server.wm;
Dianne Hackborna924dc0d2011-02-17 14:22:17 -080018
Brad Fitzpatrick68044332010-11-22 18:19:48 -080019
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -080020import static com.android.server.wm.WindowManagerDebugConfig.TAG_WITH_CLASS_NAME;
21import static com.android.server.wm.WindowManagerDebugConfig.TAG_WM;
22
Brad Fitzpatrick68044332010-11-22 18:19:48 -080023import android.graphics.Canvas;
24import android.graphics.Color;
25import android.graphics.PixelFormat;
Brad Fitzpatrick68044332010-11-22 18:19:48 -080026import android.graphics.Rect;
27import android.graphics.Region;
Brad Fitzpatrick68044332010-11-22 18:19:48 -080028import android.view.Display;
Igor Murashkina86ab6402013-08-30 12:58:36 -070029import android.view.Surface.OutOfResourcesException;
Brad Fitzpatrick68044332010-11-22 18:19:48 -080030import android.view.Surface;
Mathias Agopian3866f0d2013-02-11 22:08:48 -080031import android.view.SurfaceControl;
Brad Fitzpatrick68044332010-11-22 18:19:48 -080032import android.view.SurfaceSession;
33
34class StrictModeFlash {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -080035 private static final String TAG = TAG_WITH_CLASS_NAME ? "StrictModeFlash" : TAG_WM;
Brad Fitzpatrick68044332010-11-22 18:19:48 -080036
Mathias Agopian3866f0d2013-02-11 22:08:48 -080037 private final SurfaceControl mSurfaceControl;
38 private final Surface mSurface = new Surface();
39 private int mLastDW;
40 private int mLastDH;
41 private boolean mDrawNeeded;
42 private final int mThickness = 20;
Brad Fitzpatrick68044332010-11-22 18:19:48 -080043
Robert Carrb1579c82017-09-05 14:54:47 -070044 public StrictModeFlash(DisplayContent dc) {
Mathias Agopian3866f0d2013-02-11 22:08:48 -080045 SurfaceControl ctrl = null;
Brad Fitzpatrick68044332010-11-22 18:19:48 -080046 try {
Robert Carrb1579c82017-09-05 14:54:47 -070047 ctrl = dc.makeOverlay()
Robert Carre625fcf2017-09-01 12:36:28 -070048 .setName("StrictModeFlash")
49 .setSize(1, 1)
50 .setFormat(PixelFormat.TRANSLUCENT)
51 .build();
Mathias Agopian3866f0d2013-02-11 22:08:48 -080052 ctrl.setLayer(WindowManagerService.TYPE_LAYER_MULTIPLIER * 101); // one more than Watermark? arbitrary.
53 ctrl.setPosition(0, 0);
54 ctrl.show();
55 mSurface.copyFrom(ctrl);
Igor Murashkina86ab6402013-08-30 12:58:36 -070056 } catch (OutOfResourcesException e) {
Brad Fitzpatrick68044332010-11-22 18:19:48 -080057 }
Mathias Agopian3866f0d2013-02-11 22:08:48 -080058 mSurfaceControl = ctrl;
Brad Fitzpatrick68044332010-11-22 18:19:48 -080059 mDrawNeeded = true;
60 }
61
62 private void drawIfNeeded() {
63 if (!mDrawNeeded) {
64 return;
65 }
66 mDrawNeeded = false;
67 final int dw = mLastDW;
68 final int dh = mLastDH;
69
70 Rect dirty = new Rect(0, 0, dw, dh);
71 Canvas c = null;
72 try {
73 c = mSurface.lockCanvas(dirty);
74 } catch (IllegalArgumentException e) {
75 } catch (Surface.OutOfResourcesException e) {
76 }
77 if (c == null) {
78 return;
79 }
80
81 // Top
Derek Sollenberger5b860602018-03-29 17:29:29 -040082 c.save();
83 c.clipRect(new Rect(0, 0, dw, mThickness));
Brad Fitzpatrick68044332010-11-22 18:19:48 -080084 c.drawColor(Color.RED);
Derek Sollenberger5b860602018-03-29 17:29:29 -040085 c.restore();
Brad Fitzpatrick68044332010-11-22 18:19:48 -080086 // Left
Derek Sollenberger5b860602018-03-29 17:29:29 -040087 c.save();
88 c.clipRect(new Rect(0, 0, mThickness, dh));
Brad Fitzpatrick68044332010-11-22 18:19:48 -080089 c.drawColor(Color.RED);
Derek Sollenberger5b860602018-03-29 17:29:29 -040090 c.restore();
Brad Fitzpatrick68044332010-11-22 18:19:48 -080091 // Right
Derek Sollenberger5b860602018-03-29 17:29:29 -040092 c.save();
93 c.clipRect(new Rect(dw - mThickness, 0, dw, dh));
Brad Fitzpatrick68044332010-11-22 18:19:48 -080094 c.drawColor(Color.RED);
Derek Sollenberger5b860602018-03-29 17:29:29 -040095 c.restore();
Brad Fitzpatrick68044332010-11-22 18:19:48 -080096 // Bottom
Derek Sollenberger5b860602018-03-29 17:29:29 -040097 c.save();
98 c.clipRect(new Rect(0, dh - mThickness, dw, dh));
Brad Fitzpatrick68044332010-11-22 18:19:48 -080099 c.drawColor(Color.RED);
Derek Sollenberger5b860602018-03-29 17:29:29 -0400100 c.restore();
Brad Fitzpatrick68044332010-11-22 18:19:48 -0800101
102 mSurface.unlockCanvasAndPost(c);
103 }
104
105 // Note: caller responsible for being inside
106 // Surface.openTransaction() / closeTransaction()
107 public void setVisibility(boolean on) {
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800108 if (mSurfaceControl == null) {
Brad Fitzpatrick68044332010-11-22 18:19:48 -0800109 return;
110 }
111 drawIfNeeded();
112 if (on) {
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800113 mSurfaceControl.show();
Brad Fitzpatrick68044332010-11-22 18:19:48 -0800114 } else {
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800115 mSurfaceControl.hide();
Brad Fitzpatrick68044332010-11-22 18:19:48 -0800116 }
117 }
118
119 void positionSurface(int dw, int dh) {
120 if (mLastDW == dw && mLastDH == dh) {
121 return;
122 }
123 mLastDW = dw;
124 mLastDH = dh;
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800125 mSurfaceControl.setSize(dw, dh);
Brad Fitzpatrick68044332010-11-22 18:19:48 -0800126 mDrawNeeded = true;
127 }
128
129}