BlackFrame.java revision 9158825f9c41869689d6b1786d7c7aa8bdd524ce
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
17package com.android.server.wm;
18
19import java.io.PrintWriter;
20
21import android.graphics.Matrix;
22import android.graphics.PixelFormat;
23import android.graphics.Rect;
24import android.util.Slog;
25import android.view.Surface.OutOfResourcesException;
26import android.view.SurfaceControl;
27import android.view.SurfaceSession;
28
29/**
30 * Four black surfaces put together to make a black frame.
31 */
32public 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 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 setAlpha(float alpha) {
66            surface.setAlpha(alpha);
67        }
68
69        void setMatrix(Matrix matrix) {
70            mTmpMatrix.setTranslate(left, top);
71            mTmpMatrix.postConcat(matrix);
72            mTmpMatrix.getValues(mTmpFloats);
73            surface.setPosition(mTmpFloats[Matrix.MTRANS_X],
74                    mTmpFloats[Matrix.MTRANS_Y]);
75            surface.setMatrix(
76                    mTmpFloats[Matrix.MSCALE_X], mTmpFloats[Matrix.MSKEW_Y],
77                    mTmpFloats[Matrix.MSKEW_X], mTmpFloats[Matrix.MSCALE_Y]);
78            if (false) {
79                Slog.i(WindowManagerService.TAG, "Black Surface @ (" + left + "," + top + "): ("
80                        + mTmpFloats[Matrix.MTRANS_X] + ","
81                        + mTmpFloats[Matrix.MTRANS_Y] + ") matrix=["
82                        + mTmpFloats[Matrix.MSCALE_X] + ","
83                        + mTmpFloats[Matrix.MSCALE_Y] + "]["
84                        + mTmpFloats[Matrix.MSKEW_X] + ","
85                        + mTmpFloats[Matrix.MSKEW_Y] + "]");
86            }
87        }
88
89        void clearMatrix() {
90            surface.setMatrix(1, 0, 0, 1);
91        }
92    }
93
94    final Rect mOuterRect;
95    final Rect mInnerRect;
96    final Matrix mTmpMatrix = new Matrix();
97    final float[] mTmpFloats = new float[9];
98    final BlackSurface[] mBlackSurfaces = new BlackSurface[4];
99
100    final boolean mForceDefaultOrientation;
101
102    public void printTo(String prefix, PrintWriter pw) {
103        pw.print(prefix); pw.print("Outer: "); mOuterRect.printShortString(pw);
104                pw.print(" / Inner: "); mInnerRect.printShortString(pw);
105                pw.println();
106        for (int i=0; i<mBlackSurfaces.length; i++) {
107            BlackSurface bs = mBlackSurfaces[i];
108            pw.print(prefix); pw.print("#"); pw.print(i);
109                    pw.print(": "); pw.print(bs.surface);
110                    pw.print(" left="); pw.print(bs.left);
111                    pw.print(" top="); pw.println(bs.top);
112        }
113    }
114
115    public BlackFrame(SurfaceSession session, Rect outer, Rect inner, int layer, int layerStack,
116            boolean forceDefaultOrientation) throws OutOfResourcesException {
117        boolean success = false;
118
119        mForceDefaultOrientation = forceDefaultOrientation;
120
121        mOuterRect = new Rect(outer);
122        mInnerRect = new Rect(inner);
123        try {
124            if (outer.top < inner.top) {
125                mBlackSurfaces[0] = new BlackSurface(session, layer,
126                        outer.left, outer.top, inner.right, inner.top, layerStack);
127            }
128            if (outer.left < inner.left) {
129                mBlackSurfaces[1] = new BlackSurface(session, layer,
130                        outer.left, inner.top, inner.left, outer.bottom, layerStack);
131            }
132            if (outer.bottom > inner.bottom) {
133                mBlackSurfaces[2] = new BlackSurface(session, layer,
134                        inner.left, inner.bottom, outer.right, outer.bottom, layerStack);
135            }
136            if (outer.right > inner.right) {
137                mBlackSurfaces[3] = new BlackSurface(session, layer,
138                        inner.right, outer.top, outer.right, inner.bottom, layerStack);
139            }
140            success = true;
141        } finally {
142            if (!success) {
143                kill();
144            }
145        }
146    }
147
148    public void kill() {
149        if (mBlackSurfaces != null) {
150            for (int i=0; i<mBlackSurfaces.length; i++) {
151                if (mBlackSurfaces[i] != null) {
152                    if (WindowManagerService.SHOW_TRANSACTIONS ||
153                            WindowManagerService.SHOW_SURFACE_ALLOC) Slog.i(
154                                    WindowManagerService.TAG,
155                                    "  BLACK " + mBlackSurfaces[i].surface + ": DESTROY");
156                    mBlackSurfaces[i].surface.destroy();
157                    mBlackSurfaces[i] = null;
158                }
159            }
160        }
161    }
162
163    public void hide() {
164        if (mBlackSurfaces != null) {
165            for (int i=0; i<mBlackSurfaces.length; i++) {
166                if (mBlackSurfaces[i] != null) {
167                    mBlackSurfaces[i].surface.hide();
168                }
169            }
170        }
171    }
172
173    public void setAlpha(float alpha) {
174        for (int i=0; i<mBlackSurfaces.length; i++) {
175            if (mBlackSurfaces[i] != null) {
176                mBlackSurfaces[i].setAlpha(alpha);
177            }
178        }
179    }
180
181    public void setMatrix(Matrix matrix) {
182        for (int i=0; i<mBlackSurfaces.length; i++) {
183            if (mBlackSurfaces[i] != null) {
184                mBlackSurfaces[i].setMatrix(matrix);
185            }
186        }
187    }
188
189    public void clearMatrix() {
190        for (int i=0; i<mBlackSurfaces.length; i++) {
191            if (mBlackSurfaces[i] != null) {
192                mBlackSurfaces[i].clearMatrix();
193            }
194        }
195    }
196}
197