FileDocCategorySizeDatePackage
UploadedTexture.javaAPI DocAndroid 5.1 API9255Thu Mar 12 22:22:42 GMT 2015com.android.gallery3d.glrenderer

UploadedTexture

public abstract class UploadedTexture extends BasicTexture

Fields Summary
private static HashMap
sBorderLines
private static BorderKey
sBorderKey
private static final String
TAG
private boolean
mContentValid
private boolean
mIsUploading
private boolean
mOpaque
private boolean
mThrottled
private static int
sUploadedCount
private static final int
UPLOAD_LIMIT
protected android.graphics.Bitmap
mBitmap
private int
mBorder
Constructors Summary
protected UploadedTexture()


      
        this(false);
    
protected UploadedTexture(boolean hasBorder)

        super(null, 0, STATE_UNLOADED);
        if (hasBorder) {
            setBorder(true);
            mBorder = 1;
        }
    
Methods Summary
private voidfreeBitmap()

        Assert.assertTrue(mBitmap != null);
        onFreeBitmap(mBitmap);
        mBitmap = null;
    
private android.graphics.BitmapgetBitmap()

        if (mBitmap == null) {
            mBitmap = onGetBitmap();
            int w = mBitmap.getWidth() + mBorder * 2;
            int h = mBitmap.getHeight() + mBorder * 2;
            if (mWidth == UNSPECIFIED) {
                setSize(w, h);
            }
        }
        return mBitmap;
    
private static android.graphics.BitmapgetBorderLine(boolean vertical, android.graphics.Bitmap.Config config, int length)

        BorderKey key = sBorderKey;
        key.vertical = vertical;
        key.config = config;
        key.length = length;
        Bitmap bitmap = sBorderLines.get(key);
        if (bitmap == null) {
            bitmap = vertical
                    ? Bitmap.createBitmap(1, length, config)
                    : Bitmap.createBitmap(length, 1, config);
            sBorderLines.put(key.clone(), bitmap);
        }
        return bitmap;
    
public intgetHeight()

        if (mWidth == UNSPECIFIED) getBitmap();
        return mHeight;
    
protected intgetTarget()

        return GL11.GL_TEXTURE_2D;
    
public intgetWidth()

        if (mWidth == UNSPECIFIED) getBitmap();
        return mWidth;
    
protected voidinvalidateContent()

        if (mBitmap != null) freeBitmap();
        mContentValid = false;
        mWidth = UNSPECIFIED;
        mHeight = UNSPECIFIED;
    
public booleanisContentValid()
Whether the content on GPU is valid.

        return isLoaded() && mContentValid;
    
public booleanisOpaque()

        return mOpaque;
    
public booleanisUploading()

        return mIsUploading;
    
protected booleanonBind(GLCanvas canvas)

        updateContent(canvas);
        return isContentValid();
    
protected abstract voidonFreeBitmap(android.graphics.Bitmap bitmap)

protected abstract android.graphics.BitmaponGetBitmap()

public voidrecycle()

        super.recycle();
        if (mBitmap != null) freeBitmap();
    
public static voidresetUploadLimit()

        sUploadedCount = 0;
    
protected voidsetIsUploading(boolean uploading)

        mIsUploading = uploading;
    
public voidsetOpaque(boolean isOpaque)

        mOpaque = isOpaque;
    
protected voidsetThrottled(boolean throttled)

        mThrottled = throttled;
    
public voidupdateContent(GLCanvas canvas)
Updates the content on GPU's memory.

param
canvas

        if (!isLoaded()) {
            if (mThrottled && ++sUploadedCount > UPLOAD_LIMIT) {
                return;
            }
            uploadToCanvas(canvas);
        } else if (!mContentValid) {
            Bitmap bitmap = getBitmap();
            int format = GLUtils.getInternalFormat(bitmap);
            int type = GLUtils.getType(bitmap);
            canvas.texSubImage2D(this, mBorder, mBorder, bitmap, format, type);
            freeBitmap();
            mContentValid = true;
        }
    
public static booleanuploadLimitReached()

        return sUploadedCount > UPLOAD_LIMIT;
    
private voiduploadToCanvas(GLCanvas canvas)


        Bitmap bitmap = getBitmap();
        if (bitmap != null) {
            try {
                int bWidth = bitmap.getWidth();
                int bHeight = bitmap.getHeight();
                int width = bWidth + mBorder * 2;
                int height = bHeight + mBorder * 2;
                int texWidth = getTextureWidth();
                int texHeight = getTextureHeight();

                Assert.assertTrue(bWidth <= texWidth && bHeight <= texHeight);

                // Upload the bitmap to a new texture.
                mId = canvas.getGLId().generateTexture();
                canvas.setTextureParameters(this);

                if (bWidth == texWidth && bHeight == texHeight) {
                    canvas.initializeTexture(this, bitmap);
                } else {
                    int format = GLUtils.getInternalFormat(bitmap);
                    int type = GLUtils.getType(bitmap);
                    Config config = bitmap.getConfig();

                    canvas.initializeTextureSize(this, format, type);
                    canvas.texSubImage2D(this, mBorder, mBorder, bitmap, format, type);

                    if (mBorder > 0) {
                        // Left border
                        Bitmap line = getBorderLine(true, config, texHeight);
                        canvas.texSubImage2D(this, 0, 0, line, format, type);

                        // Top border
                        line = getBorderLine(false, config, texWidth);
                        canvas.texSubImage2D(this, 0, 0, line, format, type);
                    }

                    // Right border
                    if (mBorder + bWidth < texWidth) {
                        Bitmap line = getBorderLine(true, config, texHeight);
                        canvas.texSubImage2D(this, mBorder + bWidth, 0, line, format, type);
                    }

                    // Bottom border
                    if (mBorder + bHeight < texHeight) {
                        Bitmap line = getBorderLine(false, config, texWidth);
                        canvas.texSubImage2D(this, 0, mBorder + bHeight, line, format, type);
                    }
                }
            } finally {
                freeBitmap();
            }
            // Update texture state.
            setAssociatedCanvas(canvas);
            mState = STATE_LOADED;
            mContentValid = true;
        } else {
            mState = STATE_ERROR;
            throw new RuntimeException("Texture load fail, no bitmap");
        }