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

Commit 132f2259 authored by Romain Guy's avatar Romain Guy Committed by Android (Google) Code Review
Browse files

Merge "Add support to draw 9patches in OpenGL."

parents 9bf225ef deba785f
Loading
Loading
Loading
Loading
+10 −0
Original line number Diff line number Diff line
@@ -359,6 +359,16 @@ class GLES20Canvas extends Canvas {
        drawColor((a & 0xFF) << 24 | (r & 0xFF) << 16 | (g & 0xFF) << 8 | (b & 0xFF));
    }

    @Override
    public void drawPatch(Bitmap bitmap, byte[] chunks, RectF dst, Paint paint) {
        final int nativePaint = paint == null ? 0 : paint.mNativePaint;
        nDrawPatch(mRenderer, bitmap.mNativeBitmap, chunks, dst.left, dst.top, dst.right,
                dst.bottom, nativePaint, bitmap.getDensity(), mDensity, mScreenDensity);
    }

    private native void nDrawPatch(int renderer, int bitmap, byte[] chunks, float left, float top,
            float right, float bottom, int paint, int bitmapDensity, int canvasDensity, int screenDensity);

    @Override
    public void drawBitmap(Bitmap bitmap, float left, float top, Paint paint) {
        final int nativePaint = paint == null ? 0 : paint.mNativePaint;
+29 −0
Original line number Diff line number Diff line
@@ -17,6 +17,7 @@
#include "jni.h"
#include <nativehelper/JNIHelp.h>
#include <android_runtime/AndroidRuntime.h>
#include <utils/ResourceTypes.h>

#include <SkBitmap.h>
#include <SkCanvas.h>
@@ -214,6 +215,33 @@ static void android_view_GLES20Canvas_drawBitmapMatrix(JNIEnv* env, jobject canv
    }
}

static void android_view_GLES20Canvas_drawPatch(JNIEnv* env, jobject canvas,
        OpenGLRenderer* renderer, SkBitmap* bitmap, jbyteArray chunks,
        float left, float top, float right, float bottom, SkPaint* paint,
        jint bitmapDensity, jint canvasDensity,jint screenDensity) {

    jbyte* storage = env->GetByteArrayElements(chunks, NULL);
    Res_png_9patch* patch = reinterpret_cast<Res_png_9patch*>(storage);
    Res_png_9patch::deserialize(patch);

    if (canvasDensity == bitmapDensity || canvasDensity == 0 || bitmapDensity == 0) {
        renderer->drawPatch(bitmap, patch, left, top, right, bottom, paint);
    } else {
        renderer->save(0);
        const float scale = canvasDensity / float(bitmapDensity);
        renderer->translate(left, top);
        renderer->scale(scale, scale);
        left = top = 0.0f;
        right = (right - left) / scale;
        bottom = (bottom - top) / scale;
        renderer->drawPatch(bitmap, patch, left, top, right, bottom, paint);
        renderer->restore();
    }

    // TODO: make sure that 0 is correct for the flags
    env->ReleaseByteArrayElements(chunks, storage, 0);
}

static void android_view_GLES20Canvas_drawColor(JNIEnv* env, jobject canvas,
        OpenGLRenderer* renderer, jint color, SkXfermode::Mode mode) {
    renderer->drawColor(color, mode);
@@ -260,6 +288,7 @@ static JNINativeMethod gMethods[] = {
    {   "nDrawBitmap",        "(IIFFIIII)V",        (void*) android_view_GLES20Canvas_drawBitmap },
    {   "nDrawBitmap",        "(IIFFFFFFFFIIII)V",  (void*) android_view_GLES20Canvas_drawBitmapRect },
    {   "nDrawBitmap",        "(IIIIIII)V",         (void*) android_view_GLES20Canvas_drawBitmapMatrix },
    {   "nDrawPatch",         "(II[BFFFFIIII)V",    (void*) android_view_GLES20Canvas_drawPatch },
    {   "nDrawColor",         "(III)V",             (void*) android_view_GLES20Canvas_drawColor },
    {   "nDrawRect",          "(IFFFFI)V",          (void*) android_view_GLES20Canvas_drawRect },

+15 −0
Original line number Diff line number Diff line
@@ -999,6 +999,21 @@ public class Canvas {
        }
    }

    /**
     * Draws the specified bitmap as an N-patch (most often, a 9-patches.)
     *
     * Note: Only supported by hardware accelerated canvas at the moment.
     *
     * @param bitmap The bitmap to draw as an N-patch
     * @param chunks The patches information (matches the native struct Res_png_9patch)
     * @param dst The destination rectangle.
     * @param paint The paint to draw the bitmap with. may be null
     * 
     * @hide
     */
    public void drawPatch(Bitmap bitmap, byte[] chunks, RectF dst, Paint paint) {
    }    
    
    /**
     * Draw the specified bitmap, with its top/left corner at (x,y), using
     * the specified paint, transformed by the current matrix.
+31 −16
Original line number Diff line number Diff line
@@ -35,6 +35,12 @@ package android.graphics;
 * </p>
 */
public class NinePatch {
    private final Bitmap mBitmap;
    private final byte[] mChunk;
    private Paint mPaint;
    private String mSrcName;  // Useful for debugging
    private final RectF mRect = new RectF();
    
    /** 
     * Create a drawable projection from a bitmap to nine patches.
     *
@@ -74,10 +80,14 @@ public class NinePatch {
     * @param location  Where to draw the bitmap.
     */
    public void draw(Canvas canvas, RectF location) {
        if (!canvas.isHardwareAccelerated()) {
            nativeDraw(canvas.mNativeCanvas, location,
                       mBitmap.ni(), mChunk,
                       mPaint != null ? mPaint.mNativePaint : 0,
                       canvas.mDensity, mBitmap.mDensity);
        } else {
            canvas.drawPatch(mBitmap, mChunk, location, null);
        }
    }
    
    /** 
@@ -87,10 +97,15 @@ public class NinePatch {
     * @param location  Where to draw the bitmap.
     */
    public void draw(Canvas canvas, Rect location) {
        if (!canvas.isHardwareAccelerated()) {
            nativeDraw(canvas.mNativeCanvas, location,
                        mBitmap.ni(), mChunk,
                        mPaint != null ? mPaint.mNativePaint : 0,
                        canvas.mDensity, mBitmap.mDensity);
        } else {
            mRect.set(location);
            canvas.drawPatch(mBitmap, mChunk, mRect, null);
        }
    }

    /** 
@@ -101,9 +116,14 @@ public class NinePatch {
     * @param paint     The Paint to draw through.
     */
    public void draw(Canvas canvas, Rect location, Paint paint) {
        if (!canvas.isHardwareAccelerated()) {
            nativeDraw(canvas.mNativeCanvas, location,
                    mBitmap.ni(), mChunk, paint != null ? paint.mNativePaint : 0,
                    canvas.mDensity, mBitmap.mDensity);
        } else {
            mRect.set(location);
            canvas.drawPatch(mBitmap, mChunk, mRect, paint);
        }
    }

    /**
@@ -133,11 +153,6 @@ public class NinePatch {
    
    public native static boolean isNinePatchChunk(byte[] chunk);

    private final Bitmap mBitmap;
    private final byte[] mChunk;
    private Paint        mPaint;
    private String       mSrcName;  // Useful for debugging

    private static native void validateNinePatchChunk(int bitmap, byte[] chunk);
    private static native void nativeDraw(int canvas_instance, RectF loc, int bitmap_instance,
                                          byte[] c, int paint_instance_or_null,
+0 −7
Original line number Diff line number Diff line
@@ -176,15 +176,8 @@ public class NinePatchDrawable extends Drawable {
        }
    }

    // overrides

    @Override
    public void draw(Canvas canvas) {
        if (false) {
            float[] pts = new float[2];
            canvas.getMatrix().mapPoints(pts);
            Log.v("9patch", "Drawing 9-patch @ " + pts[0] + "," + pts[1] + ": " + getBounds());
        }
        mNinePatch.draw(canvas, getBounds(), mPaint);
    }

Loading