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