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