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