blob: 2a216abbe4aca828a0162df9a6579b4f5ad84991 [file] [log] [blame]
Prashant Malania04ea612014-07-18 17:27:56 -07001/*
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
17package com.android.server.wm;
18
19
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;
Prashant Malania04ea612014-07-18 17:27:56 -070022
23import android.graphics.Canvas;
24import android.graphics.Color;
25import android.graphics.Paint;
26import android.graphics.PixelFormat;
27import android.graphics.Point;
28import android.graphics.PorterDuff;
Prashant Malaniea481be2015-01-07 18:30:27 -080029import android.graphics.PorterDuffXfermode;
Prashant Malania04ea612014-07-18 17:27:56 -070030import android.graphics.Rect;
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -080031import android.util.Slog;
Prashant Malania04ea612014-07-18 17:27:56 -070032import android.view.Display;
33import android.view.Surface;
34import android.view.Surface.OutOfResourcesException;
35import android.view.SurfaceControl;
Prashant Malania04ea612014-07-18 17:27:56 -070036
37class CircularDisplayMask {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -080038 private static final String TAG = TAG_WITH_CLASS_NAME ? "CircularDisplayMask" : TAG_WM;
Prashant Malania04ea612014-07-18 17:27:56 -070039
Prashant Malania04ea612014-07-18 17:27:56 -070040 // 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 Malania98c21a2015-02-10 19:09:39 -080054 private int mMaskThickness;
Prashant Malania04ea612014-07-18 17:27:56 -070055
Robert Carrb1579c82017-09-05 14:54:47 -070056 public CircularDisplayMask(DisplayContent dc, int zOrder,
Prashant Malania98c21a2015-02-10 19:09:39 -080057 int screenOffset, int maskThickness) {
Robert Carrb1579c82017-09-05 14:54:47 -070058 final Display display = dc.getDisplay();
59
Prashant Malania04ea612014-07-18 17:27:56 -070060 mScreenSize = new Point();
61 display.getSize(mScreenSize);
Ching Tzung Lin9c57af42015-10-22 15:02:57 -070062 if (mScreenSize.x != mScreenSize.y + screenOffset) {
Prashant Malania04ea612014-07-18 17:27:56 -070063 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 Carrb1579c82017-09-05 14:54:47 -070070 ctrl = dc.makeOverlay()
Robert Carre625fcf2017-09-01 12:36:28 -070071 .setName("CircularDisplayMask")
72 .setSize(mScreenSize.x, mScreenSize.y) // not a typo
73 .setFormat(PixelFormat.TRANSLUCENT)
74 .build();
Robert Carre13b58e2017-08-31 14:50:44 -070075
Prashant Malania04ea612014-07-18 17:27:56 -070076 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 Malaniea481be2015-01-07 18:30:27 -080087 mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
Prashant Malania04ea612014-07-18 17:27:56 -070088 mScreenOffset = screenOffset;
Prashant Malania98c21a2015-02-10 19:09:39 -080089 mMaskThickness = maskThickness;
Prashant Malania04ea612014-07-18 17:27:56 -070090 }
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 Malania04ea612014-07-18 17:27:56 -0700108 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 Malaniea481be2015-01-07 18:30:27 -0800125 c.drawColor(Color.BLACK);
126
Prashant Malania98c21a2015-02-10 19:09:39 -0800127 // 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 Malania04ea612014-07-18 17:27:56 -0700129 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}