Brad Fitzpatrick | 6804433 | 2010-11-22 18:19:48 -0800 | [diff] [blame] | 1 | /* |
| 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 Hackborn | 6e1eb76 | 2011-02-17 16:07:28 -0800 | [diff] [blame] | 17 | package com.android.server.wm; |
Dianne Hackborn | a924dc0d | 2011-02-17 14:22:17 -0800 | [diff] [blame] | 18 | |
Filip Gruszczynski | 0bd180d | 2015-12-07 15:43:52 -0800 | [diff] [blame] | 19 | import static com.android.server.wm.WindowManagerDebugConfig.TAG_WITH_CLASS_NAME; |
| 20 | import static com.android.server.wm.WindowManagerDebugConfig.TAG_WM; |
| 21 | |
Brad Fitzpatrick | 6804433 | 2010-11-22 18:19:48 -0800 | [diff] [blame] | 22 | import android.graphics.Canvas; |
| 23 | import android.graphics.Color; |
| 24 | import android.graphics.PixelFormat; |
Brad Fitzpatrick | 6804433 | 2010-11-22 18:19:48 -0800 | [diff] [blame] | 25 | import android.graphics.Rect; |
Brad Fitzpatrick | 6804433 | 2010-11-22 18:19:48 -0800 | [diff] [blame] | 26 | import android.view.Surface; |
Vishnu Nair | e86bd98 | 2018-11-28 13:23:17 -0800 | [diff] [blame] | 27 | import android.view.Surface.OutOfResourcesException; |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 28 | import android.view.SurfaceControl; |
Brad Fitzpatrick | 6804433 | 2010-11-22 18:19:48 -0800 | [diff] [blame] | 29 | |
| 30 | class StrictModeFlash { |
Filip Gruszczynski | 0bd180d | 2015-12-07 15:43:52 -0800 | [diff] [blame] | 31 | private static final String TAG = TAG_WITH_CLASS_NAME ? "StrictModeFlash" : TAG_WM; |
Brad Fitzpatrick | 6804433 | 2010-11-22 18:19:48 -0800 | [diff] [blame] | 32 | |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 33 | 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 Fitzpatrick | 6804433 | 2010-11-22 18:19:48 -0800 | [diff] [blame] | 39 | |
Robert Carr | b1579c8 | 2017-09-05 14:54:47 -0700 | [diff] [blame] | 40 | public StrictModeFlash(DisplayContent dc) { |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 41 | SurfaceControl ctrl = null; |
Brad Fitzpatrick | 6804433 | 2010-11-22 18:19:48 -0800 | [diff] [blame] | 42 | try { |
Robert Carr | b1579c8 | 2017-09-05 14:54:47 -0700 | [diff] [blame] | 43 | ctrl = dc.makeOverlay() |
Robert Carr | e625fcf | 2017-09-01 12:36:28 -0700 | [diff] [blame] | 44 | .setName("StrictModeFlash") |
Vishnu Nair | e86bd98 | 2018-11-28 13:23:17 -0800 | [diff] [blame] | 45 | .setBufferSize(1, 1) |
Robert Carr | e625fcf | 2017-09-01 12:36:28 -0700 | [diff] [blame] | 46 | .setFormat(PixelFormat.TRANSLUCENT) |
| 47 | .build(); |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 48 | 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 Murashkin | a86ab640 | 2013-08-30 12:58:36 -0700 | [diff] [blame] | 52 | } catch (OutOfResourcesException e) { |
Brad Fitzpatrick | 6804433 | 2010-11-22 18:19:48 -0800 | [diff] [blame] | 53 | } |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 54 | mSurfaceControl = ctrl; |
Brad Fitzpatrick | 6804433 | 2010-11-22 18:19:48 -0800 | [diff] [blame] | 55 | 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 Sollenberger | 5b86060 | 2018-03-29 17:29:29 -0400 | [diff] [blame] | 78 | c.save(); |
| 79 | c.clipRect(new Rect(0, 0, dw, mThickness)); |
Brad Fitzpatrick | 6804433 | 2010-11-22 18:19:48 -0800 | [diff] [blame] | 80 | c.drawColor(Color.RED); |
Derek Sollenberger | 5b86060 | 2018-03-29 17:29:29 -0400 | [diff] [blame] | 81 | c.restore(); |
Brad Fitzpatrick | 6804433 | 2010-11-22 18:19:48 -0800 | [diff] [blame] | 82 | // Left |
Derek Sollenberger | 5b86060 | 2018-03-29 17:29:29 -0400 | [diff] [blame] | 83 | c.save(); |
| 84 | c.clipRect(new Rect(0, 0, mThickness, dh)); |
Brad Fitzpatrick | 6804433 | 2010-11-22 18:19:48 -0800 | [diff] [blame] | 85 | c.drawColor(Color.RED); |
Derek Sollenberger | 5b86060 | 2018-03-29 17:29:29 -0400 | [diff] [blame] | 86 | c.restore(); |
Brad Fitzpatrick | 6804433 | 2010-11-22 18:19:48 -0800 | [diff] [blame] | 87 | // Right |
Derek Sollenberger | 5b86060 | 2018-03-29 17:29:29 -0400 | [diff] [blame] | 88 | c.save(); |
| 89 | c.clipRect(new Rect(dw - mThickness, 0, dw, dh)); |
Brad Fitzpatrick | 6804433 | 2010-11-22 18:19:48 -0800 | [diff] [blame] | 90 | c.drawColor(Color.RED); |
Derek Sollenberger | 5b86060 | 2018-03-29 17:29:29 -0400 | [diff] [blame] | 91 | c.restore(); |
Brad Fitzpatrick | 6804433 | 2010-11-22 18:19:48 -0800 | [diff] [blame] | 92 | // Bottom |
Derek Sollenberger | 5b86060 | 2018-03-29 17:29:29 -0400 | [diff] [blame] | 93 | c.save(); |
| 94 | c.clipRect(new Rect(0, dh - mThickness, dw, dh)); |
Brad Fitzpatrick | 6804433 | 2010-11-22 18:19:48 -0800 | [diff] [blame] | 95 | c.drawColor(Color.RED); |
Derek Sollenberger | 5b86060 | 2018-03-29 17:29:29 -0400 | [diff] [blame] | 96 | c.restore(); |
Brad Fitzpatrick | 6804433 | 2010-11-22 18:19:48 -0800 | [diff] [blame] | 97 | |
| 98 | mSurface.unlockCanvasAndPost(c); |
| 99 | } |
| 100 | |
| 101 | // Note: caller responsible for being inside |
| 102 | // Surface.openTransaction() / closeTransaction() |
| 103 | public void setVisibility(boolean on) { |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 104 | if (mSurfaceControl == null) { |
Brad Fitzpatrick | 6804433 | 2010-11-22 18:19:48 -0800 | [diff] [blame] | 105 | return; |
| 106 | } |
| 107 | drawIfNeeded(); |
| 108 | if (on) { |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 109 | mSurfaceControl.show(); |
Brad Fitzpatrick | 6804433 | 2010-11-22 18:19:48 -0800 | [diff] [blame] | 110 | } else { |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 111 | mSurfaceControl.hide(); |
Brad Fitzpatrick | 6804433 | 2010-11-22 18:19:48 -0800 | [diff] [blame] | 112 | } |
| 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 Nair | e86bd98 | 2018-11-28 13:23:17 -0800 | [diff] [blame] | 121 | mSurfaceControl.setBufferSize(dw, dh); |
Brad Fitzpatrick | 6804433 | 2010-11-22 18:19:48 -0800 | [diff] [blame] | 122 | mDrawNeeded = true; |
| 123 | } |
| 124 | |
| 125 | } |