blob: 7cb4a43a9ede021adc10dcb235d627aa2ed476f8 [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
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -080019import static com.android.server.wm.WindowManagerDebugConfig.TAG_WITH_CLASS_NAME;
20import static com.android.server.wm.WindowManagerDebugConfig.TAG_WM;
21
Griff Hazen7fb61462014-09-08 21:34:52 -070022import android.content.Context;
23import android.graphics.Canvas;
24import android.graphics.Color;
25import android.graphics.PixelFormat;
26import android.graphics.Point;
27import android.graphics.PorterDuff;
28import android.graphics.Rect;
29import android.graphics.drawable.Drawable;
Griff Hazen7fb61462014-09-08 21:34:52 -070030import android.view.Display;
31import android.view.Surface;
32import android.view.Surface.OutOfResourcesException;
33import android.view.SurfaceControl;
Griff Hazen7fb61462014-09-08 21:34:52 -070034
35class EmulatorDisplayOverlay {
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -080036 private static final String TAG = TAG_WITH_CLASS_NAME ? "EmulatorDisplayOverlay" : TAG_WM;
Griff Hazen7fb61462014-09-08 21:34:52 -070037
38 // Display dimensions
39 private Point mScreenSize;
40
41 private final SurfaceControl mSurfaceControl;
42 private final Surface mSurface = new Surface();
43 private int mLastDW;
44 private int mLastDH;
45 private boolean mDrawNeeded;
46 private Drawable mOverlay;
47 private int mRotation;
48 private boolean mVisible;
49
Robert Carrb1579c82017-09-05 14:54:47 -070050 public EmulatorDisplayOverlay(Context context, DisplayContent dc,
Griff Hazen7fb61462014-09-08 21:34:52 -070051 int zOrder) {
Robert Carrb1579c82017-09-05 14:54:47 -070052 final Display display = dc.getDisplay();
Griff Hazen7fb61462014-09-08 21:34:52 -070053 mScreenSize = new Point();
54 display.getSize(mScreenSize);
55
56 SurfaceControl ctrl = null;
57 try {
Robert Carrb1579c82017-09-05 14:54:47 -070058 ctrl = dc.makeOverlay()
Robert Carre625fcf2017-09-01 12:36:28 -070059 .setName("EmulatorDisplayOverlay")
Vishnu Naire86bd982018-11-28 13:23:17 -080060 .setBufferSize(mScreenSize.x, mScreenSize.y)
Robert Carre625fcf2017-09-01 12:36:28 -070061 .setFormat(PixelFormat.TRANSLUCENT)
62 .build();
Griff Hazen7fb61462014-09-08 21:34:52 -070063 ctrl.setLayer(zOrder);
64 ctrl.setPosition(0, 0);
65 ctrl.show();
66 mSurface.copyFrom(ctrl);
67 } catch (OutOfResourcesException e) {
68 }
69 mSurfaceControl = ctrl;
70 mDrawNeeded = true;
71 mOverlay = context.getDrawable(
72 com.android.internal.R.drawable.emulator_circular_window_overlay);
73 }
74
75 private void drawIfNeeded() {
76 if (!mDrawNeeded || !mVisible) {
77 return;
78 }
79 mDrawNeeded = false;
80
81 Rect dirty = new Rect(0, 0, mScreenSize.x, mScreenSize.y);
82 Canvas c = null;
83 try {
84 c = mSurface.lockCanvas(dirty);
85 } catch (IllegalArgumentException e) {
86 } catch (OutOfResourcesException e) {
87 }
88 if (c == null) {
89 return;
90 }
91 c.drawColor(Color.TRANSPARENT, PorterDuff.Mode.SRC);
92 mSurfaceControl.setPosition(0, 0);
Griff Hazen1ad0fe62015-02-10 16:01:19 -080093 // Always draw the overlay with square dimensions
94 int size = Math.max(mScreenSize.x, mScreenSize.y);
95 mOverlay.setBounds(0, 0, size, size);
Griff Hazen7fb61462014-09-08 21:34:52 -070096 mOverlay.draw(c);
97 mSurface.unlockCanvasAndPost(c);
98 }
99
100 // Note: caller responsible for being inside
101 // Surface.openTransaction() / closeTransaction()
102 public void setVisibility(boolean on) {
103 if (mSurfaceControl == null) {
104 return;
105 }
106 mVisible = on;
107 drawIfNeeded();
108 if (on) {
109 mSurfaceControl.show();
110 } else {
111 mSurfaceControl.hide();
112 }
113 }
114
115 void positionSurface(int dw, int dh, int rotation) {
116 if (mLastDW == dw && mLastDH == dh && mRotation == rotation) {
117 return;
118 }
119 mLastDW = dw;
120 mLastDH = dh;
121 mDrawNeeded = true;
122 mRotation = rotation;
123 drawIfNeeded();
124 }
125
126}