blob: 82f2ad89d40701b1e5937953f2370184d73e85c7 [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
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -080019import static com.android.server.wm.WindowManagerDebugConfig.TAG_WITH_CLASS_NAME;
20import static com.android.server.wm.WindowManagerDebugConfig.TAG_WM;
21
Brad Fitzpatrick68044332010-11-22 18:19:48 -080022import android.graphics.Canvas;
23import android.graphics.Color;
24import android.graphics.PixelFormat;
Brad Fitzpatrick68044332010-11-22 18:19:48 -080025import android.graphics.Rect;
Brad Fitzpatrick68044332010-11-22 18:19:48 -080026import android.view.Surface;
Vishnu Naire86bd982018-11-28 13:23:17 -080027import android.view.Surface.OutOfResourcesException;
Mathias Agopian3866f0d2013-02-11 22:08:48 -080028import android.view.SurfaceControl;
Brad Fitzpatrick68044332010-11-22 18:19:48 -080029
30class StrictModeFlash {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -080031 private static final String TAG = TAG_WITH_CLASS_NAME ? "StrictModeFlash" : TAG_WM;
Brad Fitzpatrick68044332010-11-22 18:19:48 -080032
Mathias Agopian3866f0d2013-02-11 22:08:48 -080033 private final SurfaceControl mSurfaceControl;
34 private final Surface mSurface = new Surface();
35 private int mLastDW;
36 private int mLastDH;
37 private boolean mDrawNeeded;
38 private final int mThickness = 20;
Brad Fitzpatrick68044332010-11-22 18:19:48 -080039
Robert Carrb1579c82017-09-05 14:54:47 -070040 public StrictModeFlash(DisplayContent dc) {
Mathias Agopian3866f0d2013-02-11 22:08:48 -080041 SurfaceControl ctrl = null;
Brad Fitzpatrick68044332010-11-22 18:19:48 -080042 try {
Robert Carrb1579c82017-09-05 14:54:47 -070043 ctrl = dc.makeOverlay()
Robert Carre625fcf2017-09-01 12:36:28 -070044 .setName("StrictModeFlash")
Vishnu Naire86bd982018-11-28 13:23:17 -080045 .setBufferSize(1, 1)
Robert Carre625fcf2017-09-01 12:36:28 -070046 .setFormat(PixelFormat.TRANSLUCENT)
47 .build();
Mathias Agopian3866f0d2013-02-11 22:08:48 -080048 ctrl.setLayer(WindowManagerService.TYPE_LAYER_MULTIPLIER * 101); // one more than Watermark? arbitrary.
49 ctrl.setPosition(0, 0);
50 ctrl.show();
51 mSurface.copyFrom(ctrl);
Igor Murashkina86ab6402013-08-30 12:58:36 -070052 } catch (OutOfResourcesException e) {
Brad Fitzpatrick68044332010-11-22 18:19:48 -080053 }
Mathias Agopian3866f0d2013-02-11 22:08:48 -080054 mSurfaceControl = ctrl;
Brad Fitzpatrick68044332010-11-22 18:19:48 -080055 mDrawNeeded = true;
56 }
57
58 private void drawIfNeeded() {
59 if (!mDrawNeeded) {
60 return;
61 }
62 mDrawNeeded = false;
63 final int dw = mLastDW;
64 final int dh = mLastDH;
65
66 Rect dirty = new Rect(0, 0, dw, dh);
67 Canvas c = null;
68 try {
69 c = mSurface.lockCanvas(dirty);
70 } catch (IllegalArgumentException e) {
71 } catch (Surface.OutOfResourcesException e) {
72 }
73 if (c == null) {
74 return;
75 }
76
77 // Top
Derek Sollenberger5b860602018-03-29 17:29:29 -040078 c.save();
79 c.clipRect(new Rect(0, 0, dw, mThickness));
Brad Fitzpatrick68044332010-11-22 18:19:48 -080080 c.drawColor(Color.RED);
Derek Sollenberger5b860602018-03-29 17:29:29 -040081 c.restore();
Brad Fitzpatrick68044332010-11-22 18:19:48 -080082 // Left
Derek Sollenberger5b860602018-03-29 17:29:29 -040083 c.save();
84 c.clipRect(new Rect(0, 0, mThickness, dh));
Brad Fitzpatrick68044332010-11-22 18:19:48 -080085 c.drawColor(Color.RED);
Derek Sollenberger5b860602018-03-29 17:29:29 -040086 c.restore();
Brad Fitzpatrick68044332010-11-22 18:19:48 -080087 // Right
Derek Sollenberger5b860602018-03-29 17:29:29 -040088 c.save();
89 c.clipRect(new Rect(dw - mThickness, 0, dw, dh));
Brad Fitzpatrick68044332010-11-22 18:19:48 -080090 c.drawColor(Color.RED);
Derek Sollenberger5b860602018-03-29 17:29:29 -040091 c.restore();
Brad Fitzpatrick68044332010-11-22 18:19:48 -080092 // Bottom
Derek Sollenberger5b860602018-03-29 17:29:29 -040093 c.save();
94 c.clipRect(new Rect(0, dh - mThickness, dw, dh));
Brad Fitzpatrick68044332010-11-22 18:19:48 -080095 c.drawColor(Color.RED);
Derek Sollenberger5b860602018-03-29 17:29:29 -040096 c.restore();
Brad Fitzpatrick68044332010-11-22 18:19:48 -080097
98 mSurface.unlockCanvasAndPost(c);
99 }
100
101 // Note: caller responsible for being inside
102 // Surface.openTransaction() / closeTransaction()
103 public void setVisibility(boolean on) {
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800104 if (mSurfaceControl == null) {
Brad Fitzpatrick68044332010-11-22 18:19:48 -0800105 return;
106 }
107 drawIfNeeded();
108 if (on) {
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800109 mSurfaceControl.show();
Brad Fitzpatrick68044332010-11-22 18:19:48 -0800110 } else {
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800111 mSurfaceControl.hide();
Brad Fitzpatrick68044332010-11-22 18:19:48 -0800112 }
113 }
114
115 void positionSurface(int dw, int dh) {
116 if (mLastDW == dw && mLastDH == dh) {
117 return;
118 }
119 mLastDW = dw;
120 mLastDH = dh;
Vishnu Naire86bd982018-11-28 13:23:17 -0800121 mSurfaceControl.setBufferSize(dw, dh);
Brad Fitzpatrick68044332010-11-22 18:19:48 -0800122 mDrawNeeded = true;
123 }
124
125}