EmulatorDisplayOverlay.java revision 7fb614664707f4c67dbbad8320e7d3cc10750df1
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.content.Context; 21import android.graphics.Canvas; 22import android.graphics.Color; 23import android.graphics.PixelFormat; 24import android.graphics.Point; 25import android.graphics.PorterDuff; 26import android.graphics.Rect; 27import android.graphics.drawable.Drawable; 28import android.util.Slog; 29import android.view.Display; 30import android.view.Surface; 31import android.view.Surface.OutOfResourcesException; 32import android.view.SurfaceControl; 33import android.view.SurfaceSession; 34 35class EmulatorDisplayOverlay { 36 private static final String TAG = "EmulatorDisplayOverlay"; 37 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 50 public EmulatorDisplayOverlay(Context context, Display display, SurfaceSession session, 51 int zOrder) { 52 mScreenSize = new Point(); 53 display.getSize(mScreenSize); 54 55 SurfaceControl ctrl = null; 56 try { 57 if (WindowManagerService.DEBUG_SURFACE_TRACE) { 58 ctrl = new WindowStateAnimator.SurfaceTrace(session, "EmulatorDisplayOverlay", 59 mScreenSize.x, mScreenSize.y, PixelFormat.TRANSLUCENT, 60 SurfaceControl.HIDDEN); 61 } else { 62 ctrl = new SurfaceControl(session, "EmulatorDisplayOverlay", mScreenSize.x, 63 mScreenSize.y, PixelFormat.TRANSLUCENT, SurfaceControl.HIDDEN); 64 } 65 ctrl.setLayerStack(display.getLayerStack()); 66 ctrl.setLayer(zOrder); 67 ctrl.setPosition(0, 0); 68 ctrl.show(); 69 mSurface.copyFrom(ctrl); 70 } catch (OutOfResourcesException e) { 71 } 72 mSurfaceControl = ctrl; 73 mDrawNeeded = true; 74 mOverlay = context.getDrawable( 75 com.android.internal.R.drawable.emulator_circular_window_overlay); 76 } 77 78 private void drawIfNeeded() { 79 if (!mDrawNeeded || !mVisible) { 80 return; 81 } 82 mDrawNeeded = false; 83 84 Rect dirty = new Rect(0, 0, mScreenSize.x, mScreenSize.y); 85 Canvas c = null; 86 try { 87 c = mSurface.lockCanvas(dirty); 88 } catch (IllegalArgumentException e) { 89 } catch (OutOfResourcesException e) { 90 } 91 if (c == null) { 92 return; 93 } 94 c.drawColor(Color.TRANSPARENT, PorterDuff.Mode.SRC); 95 mSurfaceControl.setPosition(0, 0); 96 mOverlay.setBounds(0, 0, mScreenSize.x, mScreenSize.y); 97 mOverlay.draw(c); 98 mSurface.unlockCanvasAndPost(c); 99 } 100 101 // Note: caller responsible for being inside 102 // Surface.openTransaction() / closeTransaction() 103 public void setVisibility(boolean on) { 104 if (mSurfaceControl == null) { 105 return; 106 } 107 mVisible = on; 108 drawIfNeeded(); 109 if (on) { 110 mSurfaceControl.show(); 111 } else { 112 mSurfaceControl.hide(); 113 } 114 } 115 116 void positionSurface(int dw, int dh, int rotation) { 117 if (mLastDW == dw && mLastDH == dh && mRotation == rotation) { 118 return; 119 } 120 mLastDW = dw; 121 mLastDH = dh; 122 mDrawNeeded = true; 123 mRotation = rotation; 124 drawIfNeeded(); 125 } 126 127} 128