DimSurface.java revision d5523dc7b7318f2774109dd30716ff7b74560e61
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 android.graphics.PixelFormat;
20import android.util.Slog;
21import android.view.Surface;
22import android.view.SurfaceSession;
23
24import java.io.PrintWriter;
25
26class DimSurface {
27    static final String TAG = "DimSurface";
28
29    Surface mDimSurface;
30    boolean mDimShown = false;
31    int mDimColor = 0;
32    int mLayer = -1;
33    int mLastDimWidth, mLastDimHeight;
34
35    DimSurface(SurfaceSession session, final int layerStack) {
36        try {
37            if (WindowManagerService.DEBUG_SURFACE_TRACE) {
38                mDimSurface = new WindowStateAnimator.SurfaceTrace(session,
39                    "DimSurface",
40                    16, 16, PixelFormat.OPAQUE,
41                    Surface.FX_SURFACE_DIM | Surface.HIDDEN);
42            } else {
43                mDimSurface = new Surface(session, "DimSurface",
44                    16, 16, PixelFormat.OPAQUE,
45                    Surface.FX_SURFACE_DIM | Surface.HIDDEN);
46            }
47            if (WindowManagerService.SHOW_TRANSACTIONS ||
48                    WindowManagerService.SHOW_SURFACE_ALLOC) Slog.i(WindowManagerService.TAG,
49                            "  DIM " + mDimSurface + ": CREATE");
50            mDimSurface.setLayerStack(layerStack);
51            mDimSurface.setAlpha(0.0f);
52            mDimSurface.show();
53        } catch (Exception e) {
54            Slog.e(WindowManagerService.TAG, "Exception creating Dim surface", e);
55        }
56    }
57
58    /**
59     * Show the dim surface.
60     */
61    void show(int dw, int dh, int layer, int color) {
62        if (mDimSurface == null) {
63            Slog.e(TAG, "show: no Surface");
64            return;
65        }
66
67        if (!mDimShown) {
68            if (WindowManagerService.SHOW_TRANSACTIONS) Slog.i(WindowManagerService.TAG, "  DIM " + mDimSurface + ": SHOW pos=(0,0) (" +
69                    dw + "x" + dh + " layer=" + layer + ")");
70            mDimShown = true;
71            try {
72                mLastDimWidth = dw;
73                mLastDimHeight = dh;
74                mDimSurface.setPosition(0, 0);
75                mDimSurface.setSize(dw, dh);
76                mDimSurface.setLayer(layer);
77                mDimSurface.show();
78            } catch (RuntimeException e) {
79                Slog.w(WindowManagerService.TAG, "Failure showing dim surface", e);
80            }
81        } else if (mLastDimWidth != dw || mLastDimHeight != dh || mDimColor != color
82                || mLayer != layer) {
83            if (WindowManagerService.SHOW_TRANSACTIONS) Slog.i(WindowManagerService.TAG, "  DIM " + mDimSurface + ": pos=(0,0) (" +
84                    dw + "x" + dh + " layer=" + layer + ")");
85            mLastDimWidth = dw;
86            mLastDimHeight = dh;
87            mLayer = layer;
88            mDimColor = color;
89            mDimSurface.setSize(dw, dh);
90            mDimSurface.setLayer(layer);
91            mDimSurface.setAlpha(((color>>24)&0xff)/255.0f);
92        }
93    }
94
95    void hide() {
96        if (mDimSurface == null) {
97            Slog.e(TAG, "hide: no Surface");
98            return;
99        }
100
101        if (mDimShown) {
102            mDimShown = false;
103            try {
104                if (WindowManagerService.SHOW_TRANSACTIONS) Slog.i(WindowManagerService.TAG, "  HIDE " + mDimSurface);
105                mDimSurface.hide();
106            } catch (RuntimeException e) {
107                Slog.w(WindowManagerService.TAG, "Illegal argument exception hiding dim surface");
108            }
109        }
110    }
111
112    void kill() {
113        if (mDimSurface != null) {
114            mDimSurface.destroy();
115            mDimSurface = null;
116        }
117    }
118
119    public void printTo(String prefix, PrintWriter pw) {
120        pw.print(prefix); pw.print("mDimSurface="); pw.println(mDimSurface);
121        pw.print(prefix); pw.print("mDimShown="); pw.print(mDimShown);
122                pw.print(" mLayer="); pw.print(mLayer);
123                pw.print(" mDimColor=0x"); pw.println(Integer.toHexString(mDimColor));
124        pw.print(prefix); pw.print("mLastDimWidth="); pw.print(mLastDimWidth);
125                pw.print(" mLastDimWidth="); pw.println(mLastDimWidth);
126    }
127}
128