blob: fddf6ca2a698ce2af458941504154404a2009972 [file] [log] [blame]
Griff Hazen7fb61462014-09-08 21:34:52 -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;
22
Griff Hazen7fb61462014-09-08 21:34:52 -070023import android.content.Context;
24import android.graphics.Canvas;
25import android.graphics.Color;
26import android.graphics.PixelFormat;
27import android.graphics.Point;
28import android.graphics.PorterDuff;
29import android.graphics.Rect;
30import android.graphics.drawable.Drawable;
Griff Hazen7fb61462014-09-08 21:34:52 -070031import android.view.Display;
32import android.view.Surface;
33import android.view.Surface.OutOfResourcesException;
34import android.view.SurfaceControl;
35import android.view.SurfaceSession;
36
37class EmulatorDisplayOverlay {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -080038 private static final String TAG = TAG_WITH_CLASS_NAME ? "EmulatorDisplayOverlay" : TAG_WM;
Griff Hazen7fb61462014-09-08 21:34:52 -070039
40 // Display dimensions
41 private Point mScreenSize;
42
43 private final SurfaceControl mSurfaceControl;
44 private final Surface mSurface = new Surface();
45 private int mLastDW;
46 private int mLastDH;
47 private boolean mDrawNeeded;
48 private Drawable mOverlay;
49 private int mRotation;
50 private boolean mVisible;
51
Robert Carrb1579c82017-09-05 14:54:47 -070052 public EmulatorDisplayOverlay(Context context, DisplayContent dc,
Griff Hazen7fb61462014-09-08 21:34:52 -070053 int zOrder) {
Robert Carrb1579c82017-09-05 14:54:47 -070054 final Display display = dc.getDisplay();
Griff Hazen7fb61462014-09-08 21:34:52 -070055 mScreenSize = new Point();
56 display.getSize(mScreenSize);
57
58 SurfaceControl ctrl = null;
59 try {
Robert Carrb1579c82017-09-05 14:54:47 -070060 ctrl = dc.makeOverlay()
Robert Carre625fcf2017-09-01 12:36:28 -070061 .setName("EmulatorDisplayOverlay")
62 .setSize(mScreenSize.x, mScreenSize.y)
63 .setFormat(PixelFormat.TRANSLUCENT)
64 .build();
Griff Hazen7fb61462014-09-08 21:34:52 -070065 ctrl.setLayer(zOrder);
66 ctrl.setPosition(0, 0);
67 ctrl.show();
68 mSurface.copyFrom(ctrl);
69 } catch (OutOfResourcesException e) {
70 }
71 mSurfaceControl = ctrl;
72 mDrawNeeded = true;
73 mOverlay = context.getDrawable(
74 com.android.internal.R.drawable.emulator_circular_window_overlay);
75 }
76
77 private void drawIfNeeded() {
78 if (!mDrawNeeded || !mVisible) {
79 return;
80 }
81 mDrawNeeded = false;
82
83 Rect dirty = new Rect(0, 0, mScreenSize.x, mScreenSize.y);
84 Canvas c = null;
85 try {
86 c = mSurface.lockCanvas(dirty);
87 } catch (IllegalArgumentException e) {
88 } catch (OutOfResourcesException e) {
89 }
90 if (c == null) {
91 return;
92 }
93 c.drawColor(Color.TRANSPARENT, PorterDuff.Mode.SRC);
94 mSurfaceControl.setPosition(0, 0);
Griff Hazen1ad0fe62015-02-10 16:01:19 -080095 // Always draw the overlay with square dimensions
96 int size = Math.max(mScreenSize.x, mScreenSize.y);
97 mOverlay.setBounds(0, 0, size, size);
Griff Hazen7fb61462014-09-08 21:34:52 -070098 mOverlay.draw(c);
99 mSurface.unlockCanvasAndPost(c);
100 }
101
102 // Note: caller responsible for being inside
103 // Surface.openTransaction() / closeTransaction()
104 public void setVisibility(boolean on) {
105 if (mSurfaceControl == null) {
106 return;
107 }
108 mVisible = on;
109 drawIfNeeded();
110 if (on) {
111 mSurfaceControl.show();
112 } else {
113 mSurfaceControl.hide();
114 }
115 }
116
117 void positionSurface(int dw, int dh, int rotation) {
118 if (mLastDW == dw && mLastDH == dh && mRotation == rotation) {
119 return;
120 }
121 mLastDW = dw;
122 mLastDH = dh;
123 mDrawNeeded = true;
124 mRotation = rotation;
125 drawIfNeeded();
126 }
127
128}