Merge "Remove all Dalvik allocations from Cavnas.drawBitmap(int[], ...)" into jb-dev

This commit is contained in:
Romain Guy
2012-05-14 20:12:09 -07:00
committed by Android (Google) Code Review
9 changed files with 143 additions and 16 deletions

View File

@@ -833,19 +833,40 @@ class GLES20Canvas extends HardwareCanvas {
@Override
public void drawBitmap(int[] colors, int offset, int stride, float x, float y,
int width, int height, boolean hasAlpha, Paint paint) {
if (width < 0) {
throw new IllegalArgumentException("width must be >= 0");
}
if (height < 0) {
throw new IllegalArgumentException("height must be >= 0");
}
if (Math.abs(stride) < width) {
throw new IllegalArgumentException("abs(stride) must be >= width");
}
int lastScanline = offset + (height - 1) * stride;
int length = colors.length;
if (offset < 0 || (offset + width > length) || lastScanline < 0 ||
(lastScanline + width > length)) {
throw new ArrayIndexOutOfBoundsException();
}
// Shaders are ignored when drawing bitmaps
int modifier = paint != null ? setupColorFilter(paint) : MODIFIER_NONE;
try {
final Bitmap.Config config = hasAlpha ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565;
final Bitmap b = Bitmap.createBitmap(colors, offset, stride, width, height, config);
final int nativePaint = paint == null ? 0 : paint.mNativePaint;
nDrawBitmap(mRenderer, b.mNativeBitmap, b.mBuffer, x, y, nativePaint);
b.recycle();
nDrawBitmap(mRenderer, colors, offset, stride, x, y,
width, height, hasAlpha, nativePaint);
} finally {
if (modifier != MODIFIER_NONE) nResetModifiers(mRenderer, modifier);
}
}
private static native void nDrawBitmap(int renderer, int[] colors, int offset, int stride,
float x, float y, int width, int height, boolean hasAlpha, int nativePaint);
@Override
public void drawBitmap(int[] colors, int offset, int stride, int x, int y,
int width, int height, boolean hasAlpha, Paint paint) {

View File

@@ -326,8 +326,8 @@ static void android_view_GLES20Canvas_concatMatrix(JNIEnv* env, jobject clazz,
// ----------------------------------------------------------------------------
static void android_view_GLES20Canvas_drawBitmap(JNIEnv* env, jobject clazz,
OpenGLRenderer* renderer, SkBitmap* bitmap, jbyteArray buffer, float left,
float top, SkPaint* paint) {
OpenGLRenderer* renderer, SkBitmap* bitmap, jbyteArray buffer,
jfloat left, jfloat top, SkPaint* paint) {
// This object allows the renderer to allocate a global JNI ref to the buffer object.
JavaHeapBitmapRef bitmapRef(env, bitmap, buffer);
@@ -354,6 +354,24 @@ static void android_view_GLES20Canvas_drawBitmapMatrix(JNIEnv* env, jobject claz
renderer->drawBitmap(bitmap, matrix, paint);
}
static void android_view_GLES20Canvas_drawBitmapData(JNIEnv* env, jobject clazz,
OpenGLRenderer* renderer, jintArray colors, jint offset, jint stride,
jfloat left, jfloat top, jint width, jint height, jboolean hasAlpha, SkPaint* paint) {
SkBitmap bitmap;
SkBitmap::Config config = hasAlpha ? SkBitmap::kARGB_8888_Config : SkBitmap::kRGB_565_Config;
bitmap.setConfig(config, width, height);
if (!bitmap.allocPixels()) {
return;
}
if (!GraphicsJNI::SetPixels(env, colors, offset, stride, 0, 0, width, height, bitmap)) {
return;
}
renderer->drawBitmapData(&bitmap, left, top, paint);
}
static void android_view_GLES20Canvas_drawBitmapMesh(JNIEnv* env, jobject clazz,
OpenGLRenderer* renderer, SkBitmap* bitmap, jbyteArray buffer,
jint meshWidth, jint meshHeight, jfloatArray vertices, jint offset,
@@ -880,6 +898,7 @@ static JNINativeMethod gMethods[] = {
{ "nDrawBitmap", "(II[BFFI)V", (void*) android_view_GLES20Canvas_drawBitmap },
{ "nDrawBitmap", "(II[BFFFFFFFFI)V",(void*) android_view_GLES20Canvas_drawBitmapRect },
{ "nDrawBitmap", "(II[BII)V", (void*) android_view_GLES20Canvas_drawBitmapMatrix },
{ "nDrawBitmap", "(I[IIIFFIIZI)V", (void*) android_view_GLES20Canvas_drawBitmapData },
{ "nDrawBitmapMesh", "(II[BII[FI[III)V",(void*) android_view_GLES20Canvas_drawBitmapMesh },

View File

@@ -49,6 +49,7 @@ const char* DisplayList::OP_NAMES[] = {
"DrawBitmap",
"DrawBitmapMatrix",
"DrawBitmapRect",
"DrawBitmapData",
"DrawBitmapMesh",
"DrawPatch",
"DrawColor",
@@ -434,6 +435,14 @@ void DisplayList::output(OpenGLRenderer& renderer, uint32_t level) {
(char*) indent, OP_NAMES[op], bitmap, f1, f2, f3, f4, f5, f6, f7, f8, paint);
}
break;
case DrawBitmapData: {
SkBitmap* bitmap = getBitmapData();
float x = getFloat();
float y = getFloat();
SkPaint* paint = getPaint(renderer);
ALOGD("%s%s %.2f, %.2f, %p", (char*) indent, OP_NAMES[op], x, y, paint);
}
break;
case DrawBitmapMesh: {
int verticesCount = 0;
uint32_t colorsCount = 0;
@@ -1020,6 +1029,19 @@ status_t DisplayList::replay(OpenGLRenderer& renderer, Rect& dirty, int32_t flag
renderer.drawBitmap(bitmap, f1, f2, f3, f4, f5, f6, f7, f8, paint);
}
break;
case DrawBitmapData: {
SkBitmap* bitmap = getBitmapData();
float x = getFloat();
float y = getFloat();
SkPaint* paint = getPaint(renderer);
DISPLAY_LIST_LOGD("%s%s %p, %.2f, %.2f, %p", (char*) indent, OP_NAMES[op],
bitmap, x, y, paint);
if (bitmap) {
renderer.drawBitmap(bitmap, x, y, paint);
delete bitmap;
}
}
break;
case DrawBitmapMesh: {
int32_t verticesCount = 0;
uint32_t colorsCount = 0;
@@ -1487,6 +1509,15 @@ void DisplayListRenderer::drawBitmap(SkBitmap* bitmap, float srcLeft, float srcT
addSkip(location);
}
void DisplayListRenderer::drawBitmapData(SkBitmap* bitmap, float left, float top, SkPaint* paint) {
const bool reject = quickReject(left, top, left + bitmap->width(), bitmap->height());
uint32_t* location = addOp(DisplayList::DrawBitmapData, reject);
addBitmapData(bitmap);
addPoint(left, top);
addPaint(paint);
addSkip(location);
}
void DisplayListRenderer::drawBitmapMesh(SkBitmap* bitmap, int meshWidth, int meshHeight,
float* vertices, int* colors, SkPaint* paint) {
addOp(DisplayList::DrawBitmapMesh);

View File

@@ -91,6 +91,7 @@ public:
DrawBitmap,
DrawBitmapMatrix,
DrawBitmapRect,
DrawBitmapData,
DrawBitmapMesh,
DrawPatch,
DrawColor,
@@ -422,6 +423,19 @@ private:
return (SkBitmap*) getInt();
}
SkBitmap* getBitmapData() {
SkBitmap* bitmap = new SkBitmap;
bitmap->setConfig((SkBitmap::Config) getInt(), getInt(), getInt());
if (!bitmap->allocPixels()) {
delete bitmap;
return NULL;
}
bitmap->setPixels((void*) mReader.skip(bitmap->height() * bitmap->rowBytes()));
return bitmap;
}
SkiaShader* getShader() {
return (SkiaShader*) getInt();
}
@@ -574,6 +588,7 @@ public:
virtual void drawBitmap(SkBitmap* bitmap, float srcLeft, float srcTop,
float srcRight, float srcBottom, float dstLeft, float dstTop,
float dstRight, float dstBottom, SkPaint* paint);
virtual void drawBitmapData(SkBitmap* bitmap, float left, float top, SkPaint* paint);
virtual void drawBitmapMesh(SkBitmap* bitmap, int meshWidth, int meshHeight,
float* vertices, int* colors, SkPaint* paint);
virtual void drawPatch(SkBitmap* bitmap, const int32_t* xDivs, const int32_t* yDivs,
@@ -701,16 +716,23 @@ private:
void addInts(const int32_t* values, uint32_t count) {
mWriter.writeInt(count);
for (uint32_t i = 0; i < count; i++) {
mWriter.writeInt(values[i]);
}
mWriter.write(values, count * sizeof(int32_t));
}
void addBitmapData(SkBitmap* bitmap) {
mWriter.writeInt(bitmap->config());
mWriter.writeInt(bitmap->width());
mWriter.writeInt(bitmap->height());
SkAutoLockPixels alp(*bitmap);
void* src = bitmap->getPixels();
mWriter.write(src, bitmap->rowBytes() * bitmap->height());
}
void addUInts(const uint32_t* values, int8_t count) {
mWriter.writeInt(count);
for (int8_t i = 0; i < count; i++) {
mWriter.writeInt(values[i]);
}
mWriter.write(values, count * sizeof(uint32_t));
}
inline void addFloat(float value) {
@@ -719,9 +741,7 @@ private:
void addFloats(const float* values, int32_t count) {
mWriter.writeInt(count);
for (int32_t i = 0; i < count; i++) {
mWriter.writeScalar(values[i]);
}
mWriter.write(values, count * sizeof(float));
}
inline void addPoint(float x, float y) {

View File

@@ -1502,6 +1502,21 @@ void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, SkMatrix* matrix, SkPaint* pai
restore();
}
void OpenGLRenderer::drawBitmapData(SkBitmap* bitmap, float left, float top, SkPaint* paint) {
const float right = left + bitmap->width();
const float bottom = top + bitmap->height();
if (quickReject(left, top, right, bottom)) {
return;
}
mCaches.activeTexture(0);
Texture* texture = mCaches.textureCache.getTransient(bitmap);
const AutoTexture autoCleanup(texture);
drawTextureRect(left, top, right, bottom, texture, paint);
}
void OpenGLRenderer::drawBitmapMesh(SkBitmap* bitmap, int meshWidth, int meshHeight,
float* vertices, int* colors, SkPaint* paint) {
// TODO: Do a quickReject

View File

@@ -115,6 +115,7 @@ public:
virtual void drawBitmap(SkBitmap* bitmap, float srcLeft, float srcTop,
float srcRight, float srcBottom, float dstLeft, float dstTop,
float dstRight, float dstBottom, SkPaint* paint);
virtual void drawBitmapData(SkBitmap* bitmap, float left, float top, SkPaint* paint);
virtual void drawBitmapMesh(SkBitmap* bitmap, int meshWidth, int meshHeight,
float* vertices, int* colors, SkPaint* paint);
virtual void drawPatch(SkBitmap* bitmap, const int32_t* xDivs, const int32_t* yDivs,

View File

@@ -160,6 +160,16 @@ Texture* TextureCache::get(SkBitmap* bitmap) {
return texture;
}
Texture* TextureCache::getTransient(SkBitmap* bitmap) {
Texture* texture = new Texture;
texture->bitmapSize = bitmap->rowBytes() * bitmap->height();
texture->cleanup = true;
generateTexture(bitmap, texture, false);
return texture;
}
void TextureCache::remove(SkBitmap* bitmap) {
mCache.remove(bitmap);
}

View File

@@ -66,6 +66,11 @@ public:
* cannot be found in the cache, a new texture is generated.
*/
Texture* get(SkBitmap* bitmap);
/**
* Returns the texture associated with the specified bitmap. The generated
* texture is not kept in the cache. The caller must destroy the texture.
*/
Texture* getTransient(SkBitmap* bitmap);
/**
* Removes the texture associated with the specified bitmap.
* Upon remove the texture is freed.

View File

@@ -87,7 +87,7 @@ public class BitmapMutateActivity extends Activity {
int width = mBitmap1.getWidth();
int height = mBitmap1.getHeight();
canvas.translate((getWidth() - width) / 2, (getHeight() - height) / 2);
canvas.translate((getWidth() - width) / 2, 0);
for (int x = 0; x < width; x++) {
int color = 0xff000000;
@@ -101,6 +101,11 @@ public class BitmapMutateActivity extends Activity {
mBitmap1.setPixels(mPixels, 0, width, 0, 0, width, height);
canvas.drawBitmap(mBitmap1, 0.0f, 0.0f, mBitmapPaint);
canvas.save();
canvas.translate(0.0f, height + 32);
canvas.drawBitmap(mPixels, 0, width, 0.0f, 0.0f, width, height, false, mBitmapPaint);
canvas.restore();
}
}
}