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

This commit is contained in:
Romain Guy
2010-07-07 17:57:12 -07:00
committed by Android (Google) Code Review
7 changed files with 95 additions and 24 deletions

View File

@@ -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;

View File

@@ -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 },

View File

@@ -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.

View File

@@ -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) {
nativeDraw(canvas.mNativeCanvas, location,
mBitmap.ni(), mChunk,
mPaint != null ? mPaint.mNativePaint : 0,
canvas.mDensity, mBitmap.mDensity);
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) {
nativeDraw(canvas.mNativeCanvas, location,
mBitmap.ni(), mChunk,
mPaint != null ? mPaint.mNativePaint : 0,
canvas.mDensity, mBitmap.mDensity);
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) {
nativeDraw(canvas.mNativeCanvas, location,
mBitmap.ni(), mChunk, paint != null ? paint.mNativePaint : 0,
canvas.mDensity, mBitmap.mDensity);
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,

View File

@@ -175,16 +175,9 @@ public class NinePatchDrawable extends Drawable {
dest.bottom = Bitmap.scaleFromDensity(src.bottom, sdensity, tdensity);
}
}
// 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);
}

View File

@@ -35,7 +35,7 @@ namespace uirenderer {
///////////////////////////////////////////////////////////////////////////////
// Debug
#define DEBUG_LAYERS 1
#define DEBUG_LAYERS 0
// These properties are defined in mega-bytes
#define PROPERTY_TEXTURE_CACHE_SIZE "ro.hwui.texture_cache_size"
@@ -521,6 +521,12 @@ void OpenGLRenderer::drawBitmap(SkBitmap* bitmap,
resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
}
void OpenGLRenderer::drawPatch(SkBitmap* bitmap, Res_png_9patch* patch,
float left, float top, float right, float bottom, const SkPaint* paint) {
// TODO: Implement
LOGD("Draw 9patch, paddingLeft=%d", patch->paddingLeft);
}
void OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
const Rect& clip = mSnapshot->clipRect;
drawColorRect(clip.left, clip.top, clip.right, clip.bottom, color, mode);

View File

@@ -26,6 +26,7 @@
#include <SkXfermode.h>
#include <utils/RefBase.h>
#include <utils/ResourceTypes.h>
#include "Matrix.h"
#include "Program.h"
@@ -108,6 +109,8 @@ public:
void drawBitmap(SkBitmap* bitmap, const SkMatrix* matrix, const SkPaint* paint);
void drawBitmap(SkBitmap* bitmap, float srcLeft, float srcTop, float srcRight, float srcBottom,
float dstLeft, float dstTop, float dstRight, float dstBottom, const SkPaint* paint);
void drawPatch(SkBitmap* bitmap, Res_png_9patch* patch, float left, float top,
float right, float bottom, const SkPaint* paint);
void drawColor(int color, SkXfermode::Mode mode);
void drawRect(float left, float top, float right, float bottom, const SkPaint* paint);