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