Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 77a81161 authored by Romain Guy's avatar Romain Guy
Browse files

Add TextureView.getBitmap()

This API can be used to get a Bitmap copy of the content of a
TextureView.

Change-Id: I07522216c353720fba5cab333174f58f484eb911
parent 113543c9
Loading
Loading
Loading
Loading
+4 −0
Original line number Diff line number Diff line
@@ -21566,8 +21566,12 @@ package android.view {
    ctor public TextureView(android.content.Context, android.util.AttributeSet);
    ctor public TextureView(android.content.Context, android.util.AttributeSet, int);
    method public final void draw(android.graphics.Canvas);
    method public android.graphics.Bitmap getBitmap();
    method public android.graphics.Bitmap getBitmap(int, int);
    method public android.graphics.Bitmap getBitmap(android.graphics.Bitmap);
    method public android.graphics.SurfaceTexture getSurfaceTexture();
    method public android.view.TextureView.SurfaceTextureListener getSurfaceTextureListener();
    method public boolean isAvailable();
    method protected final void onDraw(android.graphics.Canvas);
    method public void setSurfaceTextureListener(android.view.TextureView.SurfaceTextureListener);
  }
+1 −0
Original line number Diff line number Diff line
@@ -166,6 +166,7 @@ class GLES20Canvas extends HardwareCanvas {
    static native void nUpdateTextureLayer(int layerId, int width, int height, int surface);
    static native void nDestroyLayer(int layerId);
    static native void nDestroyLayerDeferred(int layerId);
    static native boolean nCopyLayer(int layerId, int bitmap);

    ///////////////////////////////////////////////////////////////////////////
    // Canvas management
+6 −1
Original line number Diff line number Diff line
@@ -17,6 +17,8 @@

package android.view;

import android.graphics.Bitmap;

/**
 * An OpenGL ES 2.0 implementation of {@link HardwareLayer}.
 */
@@ -40,6 +42,9 @@ abstract class GLES20Layer extends HardwareLayer {
        return mLayer;
    }

    boolean copyInto(Bitmap bitmap) {
        return GLES20Canvas.nCopyLayer(mLayer, bitmap.mNativeBitmap);
    }    

    @Override
    void destroy() {
+18 −2
Original line number Diff line number Diff line
@@ -17,6 +17,7 @@

package android.view;

import android.graphics.Bitmap;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.SurfaceTexture;
@@ -206,6 +207,16 @@ public abstract class HardwareRenderer {
    abstract void updateTextureLayer(HardwareLayer layer, int width, int height,
            SurfaceTexture surface);

    /**
     * Copies the content of the specified layer into the specified bitmap.
     * 
     * @param layer The hardware layer to copy
     * @param bitmap The bitmap to copy the layer into
     * 
     * @return True if the copy was successful, false otherwise
     */
    abstract boolean copyLayer(HardwareLayer layer, Bitmap bitmap);    

    /**
     * Initializes the hardware renderer for the specified surface and setup the
     * renderer for drawing, if needed. This is invoked when the ViewAncestor has
@@ -814,6 +825,11 @@ public abstract class HardwareRenderer {
            ((GLES20TextureLayer) layer).update(width, height, surface.mSurfaceTexture);
        }

        @Override
        boolean copyLayer(HardwareLayer layer, Bitmap bitmap) {
            return ((GLES20Layer) layer).copyInto(bitmap);
        }

        static HardwareRenderer create(boolean translucent) {
            if (GLES20Canvas.isAvailable()) {
                return new Gl20Renderer(translucent);
+92 −2
Original line number Diff line number Diff line
@@ -17,6 +17,7 @@
package android.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.SurfaceTexture;
@@ -89,6 +90,8 @@ import android.util.Log;
 * @see SurfaceTexture
 */
public class TextureView extends View {
    private static final String LOG_TAG = "TextureView";

    private HardwareLayer mLayer;
    private SurfaceTexture mSurface;
    private SurfaceTextureListener mListener;
@@ -148,7 +151,7 @@ public class TextureView extends View {
        super.onAttachedToWindow();

        if (!isHardwareAccelerated()) {
            Log.w("TextureView", "A TextureView or a subclass can only be "
            Log.w(LOG_TAG, "A TextureView or a subclass can only be "
                    + "used with hardware acceleration enabled.");
        }
    }
@@ -292,9 +295,96 @@ public class TextureView extends View {
        invalidate();
    }

    /**
     * <p>Returns a {@link android.graphics.Bitmap} representation of the content
     * of the associated surface texture. If the surface texture is not available,
     * this method returns null.</p>
     * 
     * <p>The bitmap returned by this method uses the {@link Bitmap.Config#ARGB_8888}
     * pixel format and its dimensions are the same as this view's.</p>
     * 
     * <p><strong>Do not</strong> invoke this method from a drawing method
     * ({@link #onDraw(android.graphics.Canvas)} for instance).</p>
     * 
     * @return A valid {@link Bitmap.Config#ARGB_8888} bitmap, or null if the surface
     *         texture is not available or the width &lt;= 0 or the height &lt;= 0
     * 
     * @see #isAvailable() 
     * @see #getBitmap(android.graphics.Bitmap) 
     * @see #getBitmap(int, int) 
     */
    public Bitmap getBitmap() {
        return getBitmap(getWidth(), getHeight());
    }

    /**
     * <p>Returns a {@link android.graphics.Bitmap} representation of the content
     * of the associated surface texture. If the surface texture is not available,
     * this method returns null.</p>
     * 
     * <p>The bitmap returned by this method uses the {@link Bitmap.Config#ARGB_8888}
     * pixel format.</p>
     * 
     * <p><strong>Do not</strong> invoke this method from a drawing method
     * ({@link #onDraw(android.graphics.Canvas)} for instance).</p>
     * 
     * @param width The width of the bitmap to create
     * @param height The height of the bitmap to create
     * 
     * @return A valid {@link Bitmap.Config#ARGB_8888} bitmap, or null if the surface
     *         texture is not available or width is &lt;= 0 or height is &lt;= 0
     * 
     * @see #isAvailable() 
     * @see #getBitmap(android.graphics.Bitmap) 
     * @see #getBitmap() 
     */
    public Bitmap getBitmap(int width, int height) {
        if (isAvailable() && width > 0 && height > 0) {
            return getBitmap(Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888));
        }
        return null;
    }

    /**
     * <p>Copies the content of this view's surface texture into the specified
     * bitmap. If the surface texture is not available, the copy is not executed.
     * The content of the surface texture will be scaled to fit exactly inside
     * the specified bitmap.</p>
     * 
     * <p><strong>Do not</strong> invoke this method from a drawing method
     * ({@link #onDraw(android.graphics.Canvas)} for instance).</p>
     * 
     * @param bitmap The bitmap to copy the content of the surface texture into,
     *               cannot be null, all configurations are supported
     * 
     * @return The bitmap specified as a parameter
     * 
     * @see #isAvailable() 
     * @see #getBitmap(int, int)  
     * @see #getBitmap() 
     */
    public Bitmap getBitmap(Bitmap bitmap) {
        if (bitmap != null && isAvailable()) {
            mAttachInfo.mHardwareRenderer.copyLayer(mLayer, bitmap);
        }
        return bitmap;
    }

    /**
     * Returns true if the {@link SurfaceTexture} associated with this
     * TextureView is available for rendering. When this method returns
     * true, {@link #getSurfaceTexture()} returns a valid surface texture.
     */
    public boolean isAvailable() {
        return mSurface != null;
    }

    /**
     * Returns the {@link SurfaceTexture} used by this view. This method
     * may return null if the view is not attached to a window.
     * may return null if the view is not attached to a window or if the surface
     * texture has not been initialized yet.
     * 
     * @see #isAvailable() 
     */
    public SurfaceTexture getSurfaceTexture() {
        return mSurface;
Loading