Prashant Malani | a04ea61 | 2014-07-18 17:27:56 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 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 | |
| 17 | package com.android.server.wm; |
| 18 | |
| 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; |
Prashant Malani | a04ea61 | 2014-07-18 17:27:56 -0700 | [diff] [blame] | 22 | |
| 23 | import android.graphics.Canvas; |
| 24 | import android.graphics.Color; |
| 25 | import android.graphics.Paint; |
| 26 | import android.graphics.PixelFormat; |
| 27 | import android.graphics.Point; |
| 28 | import android.graphics.PorterDuff; |
Prashant Malani | ea481be | 2015-01-07 18:30:27 -0800 | [diff] [blame] | 29 | import android.graphics.PorterDuffXfermode; |
Prashant Malani | a04ea61 | 2014-07-18 17:27:56 -0700 | [diff] [blame] | 30 | import android.graphics.Rect; |
Filip Gruszczynski | 0bd180d | 2015-12-07 15:43:52 -0800 | [diff] [blame] | 31 | import android.util.Slog; |
Prashant Malani | a04ea61 | 2014-07-18 17:27:56 -0700 | [diff] [blame] | 32 | import android.view.Display; |
| 33 | import android.view.Surface; |
| 34 | import android.view.Surface.OutOfResourcesException; |
| 35 | import android.view.SurfaceControl; |
Prashant Malani | a04ea61 | 2014-07-18 17:27:56 -0700 | [diff] [blame] | 36 | |
| 37 | class CircularDisplayMask { |
Filip Gruszczynski | 0bd180d | 2015-12-07 15:43:52 -0800 | [diff] [blame] | 38 | private static final String TAG = TAG_WITH_CLASS_NAME ? "CircularDisplayMask" : TAG_WM; |
Prashant Malani | a04ea61 | 2014-07-18 17:27:56 -0700 | [diff] [blame] | 39 | |
Prashant Malani | a04ea61 | 2014-07-18 17:27:56 -0700 | [diff] [blame] | 40 | // size of the chin |
| 41 | private int mScreenOffset = 0; |
| 42 | // Display dimensions |
| 43 | private Point mScreenSize; |
| 44 | |
| 45 | private final SurfaceControl mSurfaceControl; |
| 46 | private final Surface mSurface = new Surface(); |
| 47 | private int mLastDW; |
| 48 | private int mLastDH; |
| 49 | private boolean mDrawNeeded; |
| 50 | private Paint mPaint; |
| 51 | private int mRotation; |
| 52 | private boolean mVisible; |
| 53 | private boolean mDimensionsUnequal = false; |
Prashant Malani | a98c21a | 2015-02-10 19:09:39 -0800 | [diff] [blame] | 54 | private int mMaskThickness; |
Prashant Malani | a04ea61 | 2014-07-18 17:27:56 -0700 | [diff] [blame] | 55 | |
Robert Carr | b1579c8 | 2017-09-05 14:54:47 -0700 | [diff] [blame] | 56 | public CircularDisplayMask(DisplayContent dc, int zOrder, |
Prashant Malani | a98c21a | 2015-02-10 19:09:39 -0800 | [diff] [blame] | 57 | int screenOffset, int maskThickness) { |
Robert Carr | b1579c8 | 2017-09-05 14:54:47 -0700 | [diff] [blame] | 58 | final Display display = dc.getDisplay(); |
| 59 | |
Prashant Malani | a04ea61 | 2014-07-18 17:27:56 -0700 | [diff] [blame] | 60 | mScreenSize = new Point(); |
| 61 | display.getSize(mScreenSize); |
Ching Tzung Lin | 9c57af4 | 2015-10-22 15:02:57 -0700 | [diff] [blame] | 62 | if (mScreenSize.x != mScreenSize.y + screenOffset) { |
Prashant Malani | a04ea61 | 2014-07-18 17:27:56 -0700 | [diff] [blame] | 63 | Slog.w(TAG, "Screen dimensions of displayId = " + display.getDisplayId() + |
| 64 | "are not equal, circularMask will not be drawn."); |
| 65 | mDimensionsUnequal = true; |
| 66 | } |
| 67 | |
| 68 | SurfaceControl ctrl = null; |
| 69 | try { |
Robert Carr | b1579c8 | 2017-09-05 14:54:47 -0700 | [diff] [blame] | 70 | ctrl = dc.makeOverlay() |
Robert Carr | e625fcf | 2017-09-01 12:36:28 -0700 | [diff] [blame] | 71 | .setName("CircularDisplayMask") |
| 72 | .setSize(mScreenSize.x, mScreenSize.y) // not a typo |
| 73 | .setFormat(PixelFormat.TRANSLUCENT) |
| 74 | .build(); |
Robert Carr | e13b58e | 2017-08-31 14:50:44 -0700 | [diff] [blame] | 75 | |
Prashant Malani | a04ea61 | 2014-07-18 17:27:56 -0700 | [diff] [blame] | 76 | ctrl.setLayerStack(display.getLayerStack()); |
| 77 | ctrl.setLayer(zOrder); |
| 78 | ctrl.setPosition(0, 0); |
| 79 | ctrl.show(); |
| 80 | mSurface.copyFrom(ctrl); |
| 81 | } catch (OutOfResourcesException e) { |
| 82 | } |
| 83 | mSurfaceControl = ctrl; |
| 84 | mDrawNeeded = true; |
| 85 | mPaint = new Paint(); |
| 86 | mPaint.setAntiAlias(true); |
Prashant Malani | ea481be | 2015-01-07 18:30:27 -0800 | [diff] [blame] | 87 | mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR)); |
Prashant Malani | a04ea61 | 2014-07-18 17:27:56 -0700 | [diff] [blame] | 88 | mScreenOffset = screenOffset; |
Prashant Malani | a98c21a | 2015-02-10 19:09:39 -0800 | [diff] [blame] | 89 | mMaskThickness = maskThickness; |
Prashant Malani | a04ea61 | 2014-07-18 17:27:56 -0700 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | private void drawIfNeeded() { |
| 93 | if (!mDrawNeeded || !mVisible || mDimensionsUnequal) { |
| 94 | return; |
| 95 | } |
| 96 | mDrawNeeded = false; |
| 97 | |
| 98 | Rect dirty = new Rect(0, 0, mScreenSize.x, mScreenSize.y); |
| 99 | Canvas c = null; |
| 100 | try { |
| 101 | c = mSurface.lockCanvas(dirty); |
| 102 | } catch (IllegalArgumentException e) { |
| 103 | } catch (Surface.OutOfResourcesException e) { |
| 104 | } |
| 105 | if (c == null) { |
| 106 | return; |
| 107 | } |
Prashant Malani | a04ea61 | 2014-07-18 17:27:56 -0700 | [diff] [blame] | 108 | switch (mRotation) { |
| 109 | case Surface.ROTATION_0: |
| 110 | case Surface.ROTATION_90: |
| 111 | // chin bottom or right |
| 112 | mSurfaceControl.setPosition(0, 0); |
| 113 | break; |
| 114 | case Surface.ROTATION_180: |
| 115 | // chin top |
| 116 | mSurfaceControl.setPosition(0, -mScreenOffset); |
| 117 | break; |
| 118 | case Surface.ROTATION_270: |
| 119 | // chin left |
| 120 | mSurfaceControl.setPosition(-mScreenOffset, 0); |
| 121 | break; |
| 122 | } |
| 123 | |
| 124 | int circleRadius = mScreenSize.x / 2; |
Prashant Malani | ea481be | 2015-01-07 18:30:27 -0800 | [diff] [blame] | 125 | c.drawColor(Color.BLACK); |
| 126 | |
Prashant Malani | a98c21a | 2015-02-10 19:09:39 -0800 | [diff] [blame] | 127 | // The radius is reduced by mMaskThickness to provide an anti aliasing effect on the display edges. |
| 128 | c.drawCircle(circleRadius, circleRadius, circleRadius - mMaskThickness, mPaint); |
Prashant Malani | a04ea61 | 2014-07-18 17:27:56 -0700 | [diff] [blame] | 129 | mSurface.unlockCanvasAndPost(c); |
| 130 | } |
| 131 | |
| 132 | // Note: caller responsible for being inside |
| 133 | // Surface.openTransaction() / closeTransaction() |
| 134 | public void setVisibility(boolean on) { |
| 135 | if (mSurfaceControl == null) { |
| 136 | return; |
| 137 | } |
| 138 | mVisible = on; |
| 139 | drawIfNeeded(); |
| 140 | if (on) { |
| 141 | mSurfaceControl.show(); |
| 142 | } else { |
| 143 | mSurfaceControl.hide(); |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | void positionSurface(int dw, int dh, int rotation) { |
| 148 | if (mLastDW == dw && mLastDH == dh && mRotation == rotation) { |
| 149 | return; |
| 150 | } |
| 151 | mLastDW = dw; |
| 152 | mLastDH = dh; |
| 153 | mDrawNeeded = true; |
| 154 | mRotation = rotation; |
| 155 | drawIfNeeded(); |
| 156 | } |
| 157 | |
| 158 | } |