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

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

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

parents e4db99cf e651cc62
Loading
Loading
Loading
Loading
+25 −4
Original line number Diff line number Diff line
@@ -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) {
+21 −2
Original line number Diff line number Diff line
@@ -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 },

+31 −0
Original line number Diff line number Diff line
@@ -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);
+29 −9
Original line number Diff line number Diff line
@@ -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) {
+15 −0
Original line number Diff line number Diff line
@@ -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
Loading