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 | |
Brad Fitzpatrick | 6804433 | 2010-11-22 18:19:48 -0800 | [diff] [blame] | 19 | |
Filip Gruszczynski | 0bd180d | 2015-12-07 15:43:52 -0800 | [diff] [blame] | 20 | import static com.android.server.wm.WindowManagerDebugConfig.TAG_WITH_CLASS_NAME; |
| 21 | import static com.android.server.wm.WindowManagerDebugConfig.TAG_WM; |
| 22 | |
Brad Fitzpatrick | 6804433 | 2010-11-22 18:19:48 -0800 | [diff] [blame] | 23 | import android.graphics.Canvas; |
| 24 | import android.graphics.Color; |
| 25 | import android.graphics.PixelFormat; |
Brad Fitzpatrick | 6804433 | 2010-11-22 18:19:48 -0800 | [diff] [blame] | 26 | import android.graphics.Rect; |
| 27 | import android.graphics.Region; |
Brad Fitzpatrick | 6804433 | 2010-11-22 18:19:48 -0800 | [diff] [blame] | 28 | import android.view.Display; |
Igor Murashkin | a86ab640 | 2013-08-30 12:58:36 -0700 | [diff] [blame] | 29 | import android.view.Surface.OutOfResourcesException; |
Brad Fitzpatrick | 6804433 | 2010-11-22 18:19:48 -0800 | [diff] [blame] | 30 | import android.view.Surface; |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 31 | import android.view.SurfaceControl; |
Brad Fitzpatrick | 6804433 | 2010-11-22 18:19:48 -0800 | [diff] [blame] | 32 | import android.view.SurfaceSession; |
| 33 | |
| 34 | class StrictModeFlash { |
Filip Gruszczynski | 0bd180d | 2015-12-07 15:43:52 -0800 | [diff] [blame] | 35 | private static final String TAG = TAG_WITH_CLASS_NAME ? "StrictModeFlash" : TAG_WM; |
Brad Fitzpatrick | 6804433 | 2010-11-22 18:19:48 -0800 | [diff] [blame] | 36 | |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 37 | 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 Fitzpatrick | 6804433 | 2010-11-22 18:19:48 -0800 | [diff] [blame] | 43 | |
| 44 | public StrictModeFlash(Display display, SurfaceSession session) { |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 45 | SurfaceControl ctrl = null; |
Brad Fitzpatrick | 6804433 | 2010-11-22 18:19:48 -0800 | [diff] [blame] | 46 | try { |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 47 | ctrl = new SurfaceControl(session, "StrictModeFlash", |
| 48 | 1, 1, PixelFormat.TRANSLUCENT, SurfaceControl.HIDDEN); |
| 49 | ctrl.setLayerStack(display.getLayerStack()); |
| 50 | ctrl.setLayer(WindowManagerService.TYPE_LAYER_MULTIPLIER * 101); // one more than Watermark? arbitrary. |
| 51 | ctrl.setPosition(0, 0); |
| 52 | ctrl.show(); |
| 53 | mSurface.copyFrom(ctrl); |
Igor Murashkin | a86ab640 | 2013-08-30 12:58:36 -0700 | [diff] [blame] | 54 | } catch (OutOfResourcesException e) { |
Brad Fitzpatrick | 6804433 | 2010-11-22 18:19:48 -0800 | [diff] [blame] | 55 | } |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 56 | mSurfaceControl = ctrl; |
Brad Fitzpatrick | 6804433 | 2010-11-22 18:19:48 -0800 | [diff] [blame] | 57 | mDrawNeeded = true; |
| 58 | } |
| 59 | |
| 60 | private void drawIfNeeded() { |
| 61 | if (!mDrawNeeded) { |
| 62 | return; |
| 63 | } |
| 64 | mDrawNeeded = false; |
| 65 | final int dw = mLastDW; |
| 66 | final int dh = mLastDH; |
| 67 | |
| 68 | Rect dirty = new Rect(0, 0, dw, dh); |
| 69 | Canvas c = null; |
| 70 | try { |
| 71 | c = mSurface.lockCanvas(dirty); |
| 72 | } catch (IllegalArgumentException e) { |
| 73 | } catch (Surface.OutOfResourcesException e) { |
| 74 | } |
| 75 | if (c == null) { |
| 76 | return; |
| 77 | } |
| 78 | |
| 79 | // Top |
| 80 | c.clipRect(new Rect(0, 0, dw, mThickness), Region.Op.REPLACE); |
| 81 | c.drawColor(Color.RED); |
| 82 | // Left |
| 83 | c.clipRect(new Rect(0, 0, mThickness, dh), Region.Op.REPLACE); |
| 84 | c.drawColor(Color.RED); |
| 85 | // Right |
| 86 | c.clipRect(new Rect(dw - mThickness, 0, dw, dh), Region.Op.REPLACE); |
| 87 | c.drawColor(Color.RED); |
| 88 | // Bottom |
| 89 | c.clipRect(new Rect(0, dh - mThickness, dw, dh), Region.Op.REPLACE); |
| 90 | c.drawColor(Color.RED); |
| 91 | |
| 92 | mSurface.unlockCanvasAndPost(c); |
| 93 | } |
| 94 | |
| 95 | // Note: caller responsible for being inside |
| 96 | // Surface.openTransaction() / closeTransaction() |
| 97 | public void setVisibility(boolean on) { |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 98 | if (mSurfaceControl == null) { |
Brad Fitzpatrick | 6804433 | 2010-11-22 18:19:48 -0800 | [diff] [blame] | 99 | return; |
| 100 | } |
| 101 | drawIfNeeded(); |
| 102 | if (on) { |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 103 | mSurfaceControl.show(); |
Brad Fitzpatrick | 6804433 | 2010-11-22 18:19:48 -0800 | [diff] [blame] | 104 | } else { |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 105 | mSurfaceControl.hide(); |
Brad Fitzpatrick | 6804433 | 2010-11-22 18:19:48 -0800 | [diff] [blame] | 106 | } |
| 107 | } |
| 108 | |
| 109 | void positionSurface(int dw, int dh) { |
| 110 | if (mLastDW == dw && mLastDH == dh) { |
| 111 | return; |
| 112 | } |
| 113 | mLastDW = dw; |
| 114 | mLastDH = dh; |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame] | 115 | mSurfaceControl.setSize(dw, dh); |
Brad Fitzpatrick | 6804433 | 2010-11-22 18:19:48 -0800 | [diff] [blame] | 116 | mDrawNeeded = true; |
| 117 | } |
| 118 | |
| 119 | } |