FileDocCategorySizeDatePackage
GraphicBuffer.javaAPI DocAndroid 5.1 API9443Thu Mar 12 22:22:10 GMT 2015android.view

GraphicBuffer

public class GraphicBuffer extends Object implements android.os.Parcelable
Simple wrapper for the native GraphicBuffer class.
hide

Fields Summary
public static final int
USAGE_SW_READ_NEVER
public static final int
USAGE_SW_READ_RARELY
public static final int
USAGE_SW_READ_OFTEN
public static final int
USAGE_SW_READ_MASK
public static final int
USAGE_SW_WRITE_NEVER
public static final int
USAGE_SW_WRITE_RARELY
public static final int
USAGE_SW_WRITE_OFTEN
public static final int
USAGE_SW_WRITE_MASK
public static final int
USAGE_SOFTWARE_MASK
public static final int
USAGE_PROTECTED
public static final int
USAGE_HW_TEXTURE
public static final int
USAGE_HW_RENDER
public static final int
USAGE_HW_2D
public static final int
USAGE_HW_COMPOSER
public static final int
USAGE_HW_VIDEO_ENCODER
public static final int
USAGE_HW_MASK
private final int
mWidth
private final int
mHeight
private final int
mFormat
private final int
mUsage
private final long
mNativeObject
private android.graphics.Canvas
mCanvas
private int
mSaveCount
private boolean
mDestroyed
public static final Parcelable.Creator
CREATOR
Constructors Summary
private GraphicBuffer(int width, int height, int format, int usage, long nativeObject)
Private use only. See {@link #create(int, int, int, int)}.

        mWidth = width;
        mHeight = height;
        mFormat = format;
        mUsage = usage;
        mNativeObject = nativeObject;
    
Methods Summary
public static android.view.GraphicBuffercreate(int width, int height, int format, int usage)
Creates new GraphicBuffer instance. This method will return null if the buffer cannot be created.

param
width The width in pixels of the buffer
param
height The height in pixels of the buffer
param
format The format of each pixel as specified in {@link PixelFormat}
param
usage Hint indicating how the buffer will be used
return
A GraphicBuffer instance or null


                                                                      
               
        long nativeObject = nCreateGraphicBuffer(width, height, format, usage);
        if (nativeObject != 0) {
            return new GraphicBuffer(width, height, format, usage, nativeObject);
        }
        return null;
    
public intdescribeContents()

        return 0;
    
public voiddestroy()
Destroyes this buffer immediately. Calling this method frees up any underlying native resources. After calling this method, this buffer must not be used in any way ({@link #lockCanvas()} must not be called, etc.)

see
#isDestroyed()

        if (!mDestroyed) {
            mDestroyed = true;
            nDestroyGraphicBuffer(mNativeObject);
        }
    
protected voidfinalize()

        try {
            if (!mDestroyed) nDestroyGraphicBuffer(mNativeObject);
        } finally {
            super.finalize();
        }
    
public intgetFormat()
Returns the pixel format of this buffer. The pixel format must be one of the formats defined in {@link PixelFormat}.

        return mFormat;
    
public intgetHeight()
Returns the height of this buffer in pixels.

        return mHeight;
    
public intgetUsage()
Returns the usage hint set on this buffer.

        return mUsage;
    
public intgetWidth()
Returns the width of this buffer in pixels.

        return mWidth;
    
public booleanisDestroyed()
Indicates whether this buffer has been destroyed. A destroyed buffer cannot be used in any way: locking a Canvas will return null, the buffer cannot be written to a parcel, etc.

return
True if this GraphicBuffer is in a destroyed state, false otherwise.
see
#destroy()

        return mDestroyed;
    
public android.graphics.CanvaslockCanvas()

Start editing the pixels in the buffer. A null is returned if the buffer cannot be locked for editing.

The content of the buffer is preserved between unlockCanvas() and lockCanvas().

If this method is called after {@link #destroy()}, the return value will always be null.

return
A Canvas used to draw into the buffer, or null.
see
#lockCanvas(android.graphics.Rect)
see
#unlockCanvasAndPost(android.graphics.Canvas)
see
#isDestroyed()

        return lockCanvas(null);
    
public android.graphics.CanvaslockCanvas(android.graphics.Rect dirty)
Just like {@link #lockCanvas()} but allows specification of a dirty rectangle.

If this method is called after {@link #destroy()}, the return value will always be null.

param
dirty Area of the buffer that may be modified.
return
A Canvas used to draw into the surface, or null.
see
#lockCanvas()
see
#unlockCanvasAndPost(android.graphics.Canvas)
see
#isDestroyed()

        if (mDestroyed) {
            return null;
        }

        if (mCanvas == null) {
            mCanvas = new Canvas();
        }

        if (nLockCanvas(mNativeObject, mCanvas, dirty)) {
            mSaveCount = mCanvas.save();
            return mCanvas;
        }

        return null;
    
private static native longnCreateGraphicBuffer(int width, int height, int format, int usage)

private static native voidnDestroyGraphicBuffer(long nativeObject)

private static native booleannLockCanvas(long nativeObject, android.graphics.Canvas canvas, android.graphics.Rect dirty)

private static native longnReadGraphicBufferFromParcel(android.os.Parcel in)

private static native booleannUnlockCanvasAndPost(long nativeObject, android.graphics.Canvas canvas)

private static native voidnWriteGraphicBufferToParcel(long nativeObject, android.os.Parcel dest)

public voidunlockCanvasAndPost(android.graphics.Canvas canvas)
Finish editing pixels in the buffer.

This method doesn't do anything if {@link #destroy()} was previously called.

param
canvas The Canvas previously returned by lockCanvas()
see
#lockCanvas()
see
#lockCanvas(android.graphics.Rect)
see
#isDestroyed()

        if (!mDestroyed && mCanvas != null && canvas == mCanvas) {
            canvas.restoreToCount(mSaveCount);
            mSaveCount = 0;

            nUnlockCanvasAndPost(mNativeObject, mCanvas);
        }
    
public voidwriteToParcel(android.os.Parcel dest, int flags)
Flatten this object in to a Parcel.

Calling this method will throw an IllegalStateException if {@link #destroy()} has been previously called.

param
dest The Parcel in which the object should be written.
param
flags Additional flags about how the object should be written. May be 0 or {@link #PARCELABLE_WRITE_RETURN_VALUE}.

        if (mDestroyed) {
            throw new IllegalStateException("This GraphicBuffer has been destroyed and cannot be "
                    + "written to a parcel.");
        }

        dest.writeInt(mWidth);
        dest.writeInt(mHeight);
        dest.writeInt(mFormat);
        dest.writeInt(mUsage);
        nWriteGraphicBufferToParcel(mNativeObject, dest);