1 /* 2 * Copyright (C) 2011 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 17 package com.android.server.wm; 18 19 import java.io.PrintWriter; 20 21 import android.graphics.Matrix; 22 import android.graphics.PixelFormat; 23 import android.graphics.Rect; 24 import android.util.Slog; 25 import android.view.Surface; 26 import android.view.SurfaceSession; 27 28 /** 29 * Four black surfaces put together to make a black frame. 30 */ 31 public class BlackFrame { 32 class BlackSurface { 33 final int left; 34 final int top; 35 final int layer; 36 final Surface surface; 37 38 BlackSurface(SurfaceSession session, int layer, int l, int t, int r, int b) 39 throws Surface.OutOfResourcesException { 40 left = l; 41 top = t; 42 this.layer = layer; 43 int w = r-l; 44 int h = b-t; 45 if (WindowManagerService.DEBUG_SURFACE_TRACE) { 46 surface = new WindowStateAnimator.SurfaceTrace(session, 0, "BlackSurface(" 47 + l + ", " + t + ")", 48 -1, w, h, PixelFormat.OPAQUE, Surface.FX_SURFACE_DIM); 49 } else { 50 surface = new Surface(session, 0, "BlackSurface", 51 -1, w, h, PixelFormat.OPAQUE, Surface.FX_SURFACE_DIM); 52 } 53 surface.setAlpha(1); 54 surface.setLayer(layer); 55 surface.show(); 56 if (WindowManagerService.SHOW_TRANSACTIONS || 57 WindowManagerService.SHOW_SURFACE_ALLOC) Slog.i(WindowManagerService.TAG, 58 " BLACK " + surface + ": CREATE layer=" + layer); 59 } 60 61 void setMatrix(Matrix matrix) { 62 mTmpMatrix.setTranslate(left, top); 63 mTmpMatrix.postConcat(matrix); 64 mTmpMatrix.getValues(mTmpFloats); 65 surface.setPosition(mTmpFloats[Matrix.MTRANS_X], 66 mTmpFloats[Matrix.MTRANS_Y]); 67 surface.setMatrix( 68 mTmpFloats[Matrix.MSCALE_X], mTmpFloats[Matrix.MSKEW_Y], 69 mTmpFloats[Matrix.MSKEW_X], mTmpFloats[Matrix.MSCALE_Y]); 70 if (false) { 71 Slog.i(WindowManagerService.TAG, "Black Surface @ (" + left + "," + top + "): (" 72 + mTmpFloats[Matrix.MTRANS_X] + "," 73 + mTmpFloats[Matrix.MTRANS_Y] + ") matrix=[" 74 + mTmpFloats[Matrix.MSCALE_X] + "," 75 + mTmpFloats[Matrix.MSCALE_Y] + "][" 76 + mTmpFloats[Matrix.MSKEW_X] + "," 77 + mTmpFloats[Matrix.MSKEW_Y] + "]"); 78 } 79 } 80 81 void clearMatrix() { 82 surface.setMatrix(1, 0, 0, 1); 83 } 84 } 85 86 final Rect mOuterRect; 87 final Rect mInnerRect; 88 final Matrix mTmpMatrix = new Matrix(); 89 final float[] mTmpFloats = new float[9]; 90 final BlackSurface[] mBlackSurfaces = new BlackSurface[4]; 91 92 public void printTo(String prefix, PrintWriter pw) { 93 pw.print(prefix); pw.print("Outer: "); mOuterRect.printShortString(pw); 94 pw.print(" / Inner: "); mInnerRect.printShortString(pw); 95 pw.println(); 96 for (int i=0; i<mBlackSurfaces.length; i++) { 97 BlackSurface bs = mBlackSurfaces[i]; 98 pw.print(prefix); pw.print("#"); pw.print(i); 99 pw.print(": "); pw.print(bs.surface); 100 pw.print(" left="); pw.print(bs.left); 101 pw.print(" top="); pw.println(bs.top); 102 } 103 } 104 105 public BlackFrame(SurfaceSession session, Rect outer, Rect inner, 106 int layer) throws Surface.OutOfResourcesException { 107 boolean success = false; 108 109 mOuterRect = new Rect(outer); 110 mInnerRect = new Rect(inner); 111 try { 112 if (outer.top < inner.top) { 113 mBlackSurfaces[0] = new BlackSurface(session, layer, 114 outer.left, outer.top, inner.right, inner.top); 115 } 116 if (outer.left < inner.left) { 117 mBlackSurfaces[1] = new BlackSurface(session, layer, 118 outer.left, inner.top, inner.left, outer.bottom); 119 } 120 if (outer.bottom > inner.bottom) { 121 mBlackSurfaces[2] = new BlackSurface(session, layer, 122 inner.left, inner.bottom, outer.right, outer.bottom); 123 } 124 if (outer.right > inner.right) { 125 mBlackSurfaces[3] = new BlackSurface(session, layer, 126 inner.right, outer.top, outer.right, inner.bottom); 127 } 128 success = true; 129 } finally { 130 if (!success) { 131 kill(); 132 } 133 } 134 } 135 136 public void kill() { 137 if (mBlackSurfaces != null) { 138 for (int i=0; i<mBlackSurfaces.length; i++) { 139 if (mBlackSurfaces[i] != null) { 140 if (WindowManagerService.SHOW_TRANSACTIONS || 141 WindowManagerService.SHOW_SURFACE_ALLOC) Slog.i( 142 WindowManagerService.TAG, 143 " BLACK " + mBlackSurfaces[i].surface + ": DESTROY"); 144 mBlackSurfaces[i].surface.destroy(); 145 mBlackSurfaces[i] = null; 146 } 147 } 148 } 149 } 150 151 public void hide() { 152 if (mBlackSurfaces != null) { 153 for (int i=0; i<mBlackSurfaces.length; i++) { 154 if (mBlackSurfaces[i] != null) { 155 mBlackSurfaces[i].surface.hide(); 156 } 157 } 158 } 159 } 160 161 public void setMatrix(Matrix matrix) { 162 for (int i=0; i<mBlackSurfaces.length; i++) { 163 if (mBlackSurfaces[i] != null) { 164 mBlackSurfaces[i].setMatrix(matrix); 165 } 166 } 167 } 168 169 public void clearMatrix() { 170 for (int i=0; i<mBlackSurfaces.length; i++) { 171 if (mBlackSurfaces[i] != null) { 172 mBlackSurfaces[i].clearMatrix(); 173 } 174 } 175 } 176 } 177