CircularDisplayMask.java revision 39a6db7cd5199dbe83da388404be3fc8f386281e
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
17package com.android.server.wm;
18
19
20import android.graphics.Canvas;
21import android.graphics.Color;
22import android.graphics.Paint;
23import android.graphics.PixelFormat;
24import android.graphics.Rect;
25import android.util.Log;
26import android.view.Display;
27import android.view.Surface;
28import android.view.Surface.OutOfResourcesException;
29import android.view.SurfaceControl;
30import android.view.SurfaceSession;
31
32class CircularDisplayMask {
33    private static final String TAG = "CircularDisplayMask";
34
35    private static final int STROKE_WIDTH = 2;
36
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 Paint mPaint;
43
44    public CircularDisplayMask(Display display, SurfaceSession session, int zOrder) {
45        SurfaceControl ctrl = null;
46        try {
47            ctrl = new SurfaceControl(session, "CircularDisplayMask",
48                320, 290, PixelFormat.TRANSLUCENT, SurfaceControl.HIDDEN);
49            ctrl.setLayerStack(display.getLayerStack());
50            ctrl.setLayer(zOrder);
51            ctrl.setPosition(0, 0);
52            ctrl.show();
53            mSurface.copyFrom(ctrl);
54        } catch (OutOfResourcesException e) {
55        }
56        mSurfaceControl = ctrl;
57        mDrawNeeded = true;
58        mPaint = new Paint();
59        mPaint.setAntiAlias(true);
60        mPaint.setStyle(Paint.Style.STROKE);
61        mPaint.setColor(Color.BLACK);
62        mPaint.setStrokeWidth(STROKE_WIDTH);
63    }
64
65    private void drawIfNeeded() {
66        if (!mDrawNeeded) {
67            return;
68        }
69        mDrawNeeded = false;
70
71        Rect dirty = new Rect(0, 0, mLastDW, mLastDH);
72        Canvas c = null;
73        try {
74            c = mSurface.lockCanvas(dirty);
75        } catch (IllegalArgumentException e) {
76        } catch (Surface.OutOfResourcesException e) {
77        }
78        if (c == null) {
79            return;
80        }
81        c.drawCircle(160, 160, 160, mPaint);
82
83        mSurface.unlockCanvasAndPost(c);
84    }
85
86    // Note: caller responsible for being inside
87    // Surface.openTransaction() / closeTransaction()
88    public void setVisibility(boolean on) {
89        if (mSurfaceControl == null) {
90            return;
91        }
92        drawIfNeeded();
93        if (on) {
94            mSurfaceControl.show();
95        } else {
96            mSurfaceControl.hide();
97        }
98    }
99
100    void positionSurface(int dw, int dh) {
101        if (mLastDW == dw && mLastDH == dh) {
102            return;
103        }
104        mLastDW = dw;
105        mLastDH = dh;
106        mSurfaceControl.setSize(dw, dh);
107        mDrawNeeded = true;
108    }
109
110}
111