Android的surface类源码深度剖析

surface用来处理一段单独的缓冲区,这段缓冲区是由屏幕合成器来管理的。

surface 包含安全的内容,使用一些特殊的措施来阻止在其他的进程中复制surface内容。尤其是截屏和VNC服务。surface不能被硬件加速。它创建的缓冲区有几种:BUFFERS,NORMAL,BLUR,DIM。

当surface不能被创建或更改大小时候,系统会抛出异常。surface创建过程中,创建了一个画布,并且在之后的程序设计中我们可以使用。我们可以锁定或解锁一个surface,但解锁时,只有调用post()或postAll() 是才会更新屏幕。

源码:


package Android.view;

import android.graphics.*;
import android.os.Parcelable;
import android.os.Parcel;
import android.util.Log;


public class Surface implements Parcelable {
    private static final String LOG_TAG = "Surface";
   
 
    public static final int HIDDEN              = 0x00000004;

    public static final int HARDWARE            = 0x00000010;

    public static final int GPU                 = 0x00000028;

    public static final int SECURE              = 0x00000080;
   
   
    public static final int NON_PREMULTIPLIED   = 0x00000100;
   
   
    public static final int PUSH_BUFFERS        = 0x00000200;
   
  
    public static final int FX_SURFACE_NORMAL   = 0x00000000;
   
  
    public static final int FX_SURFACE_BLUR     = 0x00010000;
   
  
    public static final int FX_SURFACE_DIM     = 0x00020000;

   
    public static final int FX_SURFACE_MASK     = 0x000F0000;

  
   
  
    public static final int SURFACE_HIDDEN    = 0x01;
   
  
    public static final int SURACE_FROZEN     = 0x02;
   
    public static final int SURFACE_DITHER    = 0x04;

    public static final int SURFACE_BLUR_FREEZE= 0x10;

 
    public static final int ROTATION_0       = 0;
    public static final int ROTATION_90      = 1;
    public static final int ROTATION_180     = 2;
    public static final int ROTATION_270     = 3;
   
  
    public static final int FLAGS_ORIENTATION_ANIMATION_DISABLE = 0x000000001;

    @SuppressWarnings("unused")
    private int mSurface;
    @SuppressWarnings("unused")
    private int mSaveCount;
    @SuppressWarnings("unused")
    private Canvas mCanvas;

    /**
     * Exception thrown when a surface couldn't be created or resized
     */
    public static class OutOfResourcesException extends Exception {
        public OutOfResourcesException() {
        }
        public OutOfResourcesException(String name) {
            super(name);
        }
    }

  
    native private static void nativeClassInit();
    static { nativeClassInit(); }

   
    public Surface(SurfaceSession s,
            int pid, int display, int w, int h, int format, int flags)
        throws OutOfResourcesException {
        mCanvas = new Canvas();
        init(s,pid,display,w,h,format,flags);
    }

  
    public Surface() {
        mCanvas = new Canvas();
    }
   
    public native   void copyFrom(Surface o);
   
   
    public native   boolean isValid();
   
    public native   void clear();
   
    public Canvas lockCanvas(Rect dirty) throws OutOfResourcesException {
     
        return lockCanvasNative(dirty);
    }

    private native Canvas lockCanvasNative(Rect dirty);

   
    public native   void unlockCanvasAndPost(Canvas canvas);

  
    public native   void unlockCanvas(Canvas canvas);
 
    public static native   void openTransaction();
 
    public static native   void closeTransaction();

   
 
    public static native   void freezeDisplay(int display);

   
    public static native   void unfreezeDisplay(int display);

    public static native   void setOrientation(int display, int orientation, int flags);

    public static void setOrientation(int display, int orientation) {
        setOrientation(display, orientation, 0);
    }
   
  
    public native   void setLayer(int zorder);
    public native   void setPosition(int x, int y);
    public native   void setSize(int w, int h);

    public native   void hide();
    public native   void show();
    public native   void setTransparentRegionHint(Region region);
    public native   void setAlpha(float alpha);
    public native   void setMatrix(float dsdx, float dtdx,
                                   float dsdy, float dtdy);

    public native   void freeze();
    public native   void unfreeze();

    public native   void setFreezeTint(int tint);

    public native   void setFlags(int flags, int mask);

    @Override
    public String toString() {
        return "Surface(native-token=" + mSurface + ")";
    }

    private Surface(Parcel source) throws OutOfResourcesException {
        init(source);
    }
   
    public int describeContents() {
        return 0;
    }

    public native   void readFromParcel(Parcel source);
    public native   void writeToParcel(Parcel dest, int flags);

    public static final Parcelable.Creator<Surface> CREATOR
            = new Parcelable.Creator<Surface>()
    {
        public Surface createFromParcel(Parcel source) {
            try {
                return new Surface(source);
            } catch (Exception e) {
                Log.e(LOG_TAG, "Exception creating surface from parcel", e);
            }
            return null;
        }

        public Surface[] newArray(int size) {
            return new Surface[size];
        }
    };

    /* no user serviceable parts here ... */
    @Override
    protected void finalize() throws Throwable {
        clear();
    }
   
    private native void init(SurfaceSession s,
            int pid, int display, int w, int h, int format, int flags)
            throws OutOfResourcesException;

    private native void init(Parcel source);
}

相关推荐