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

Commit 8ba548f8 authored by Romain Guy's avatar Romain Guy
Browse files

Add implementation for drawBitmap(Bitmap, Rect, Rect, Paint)

Change-Id: I10904d2325a5431d15801aebcec1048715678e8c
parent e07f6eeb
Loading
Loading
Loading
Loading
+0 −1
Original line number Diff line number Diff line
@@ -403,7 +403,6 @@ class GLES20Canvas extends Canvas {
    @Override
    public void drawBitmap(int[] colors, int offset, int stride, float x, float y,
            int width, int height, boolean hasAlpha, Paint paint) {

        // TODO: Implement
    }

+7 −3
Original line number Diff line number Diff line
@@ -196,9 +196,13 @@ static void android_view_GLES20Canvas_drawBitmapRect(JNIEnv* env, jobject canvas
        float srcLeft, float srcTop, float srcRight, float srcBottom,
        float dstLeft, float dstTop, float dstRight, float dstBottom,
        SkMatrix* matrix, SkPaint* paint,
        jint bitmapDenstiy, jint canvasDensity, jint screenDensity) {
    // TODO: Implement!
    LOGE("Not implemented: drawBitmap(IIFFFFFFFFIIIII)V");
        jint bitmapDensity, jint canvasDensity, jint screenDensity) {
    if (canvasDensity == bitmapDensity || canvasDensity == 0 || bitmapDensity == 0) {
        renderer->drawBitmap(bitmap, srcLeft, srcTop, srcRight, srcBottom,
                dstLeft, dstTop, dstRight, dstBottom, matrix, paint);
    } else {

    }
}

static void android_view_GLES20Canvas_drawColor(JNIEnv* env, jobject canvas,
+57 −24
Original line number Diff line number Diff line
@@ -205,12 +205,12 @@ void OpenGLRenderer::composeLayer(sp<Snapshot> current, sp<Snapshot> previous) {
    const float u2 = texCoords.right / float(mWidth);
    const float v2 = (mHeight - texCoords.bottom) / float(mHeight);

    resetDrawTextureTexCoords(u1, v1, u2, v1);
    resetDrawTextureTexCoords(u1, v1, u2, v2);

    drawTextureRect(layer.left, layer.top, layer.right, layer.bottom,
            current->texture, current->alpha, current->mode, true, true);

    resetDrawTextureTexCoords(0.0f, 1.0f, 1.0f, 0.0f);
    resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);

    glDeleteFramebuffers(1, &current->fbo);
    glDeleteTextures(1, &current->texture);
@@ -263,9 +263,11 @@ bool OpenGLRenderer::createLayer(sp<Snapshot> snapshot, float left, float top,
    // The FBO will not be scaled, so we can use lower quality filtering
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

    // TODO VERY IMPORTANT: Fix TextView to not call saveLayer() all the time
    // TODO ***** IMPORTANT *****
    // Creating a texture-backed FBO works only if the texture is the same size
    // as the original rendering buffer (in this case, mWidth and mHeight.)
@@ -381,30 +383,40 @@ bool OpenGLRenderer::clipRect(float left, float top, float right, float bottom)
void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, float left, float top, const SkPaint* paint) {
    Texture* texture = mTextureCache.get(bitmap);

    SkXfermode::Mode mode;
    int alpha;
    SkXfermode::Mode mode;
    getAlphaAndMode(paint, &alpha, &mode);

    if (paint) {
        const bool isMode = SkXfermode::IsMode(paint->getXfermode(), &mode);
        if (!isMode) {
            // Assume SRC_OVER
            mode = SkXfermode::kSrcOver_Mode;
    drawTextureRect(left, top, left + texture->width, top + texture->height, texture->id,
            alpha / 255.0f, mode, texture->blend, true);
}

        // Skia draws using the color's alpha channel if < 255
        // Otherwise, it uses the paint's alpha
        int color = paint->getColor();
        alpha = (color >> 24) & 0xFF;
        if (alpha == 255) {
            alpha = paint->getAlpha();
        }
    } else {
        mode = SkXfermode::kSrcOver_Mode;
        alpha = 255;
    }
void OpenGLRenderer::drawBitmap(SkBitmap* bitmap,
         float srcLeft, float srcTop, float srcRight, float srcBottom,
         float dstLeft, float dstTop, float dstRight, float dstBottom,
         const SkMatrix* matrix, const SkPaint* paint) {
    Texture* texture = mTextureCache.get(bitmap);

    drawTextureRect(left, top, left + texture->width, top + texture->height, texture->id,
    int alpha;
    SkXfermode::Mode mode;
    getAlphaAndMode(paint, &alpha, &mode);

    const float width = texture->width;
    const float height = texture->height;

    const float u1 = srcLeft / width;
    const float v1 = srcTop / height;
    const float u2 = srcRight / width;
    const float v2 = srcBottom / height;

    resetDrawTextureTexCoords(u1, v1, u2, v2);

    // TODO: implement Matrix

    drawTextureRect(dstLeft, dstTop, dstRight, dstBottom, texture->id,
            alpha / 255.0f, mode, texture->blend, true);

    resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
}

void OpenGLRenderer::drawColor(int color, SkXfermode::Mode mode) {
@@ -515,13 +527,34 @@ void OpenGLRenderer::drawTextureRect(float left, float top, float right, float b

void OpenGLRenderer::resetDrawTextureTexCoords(float u1, float v1, float u2, float v2) {
    mDrawTextureVertices[0].texture[0] = u1;
    mDrawTextureVertices[0].texture[1] = v2;
    mDrawTextureVertices[0].texture[1] = v1;
    mDrawTextureVertices[1].texture[0] = u2;
    mDrawTextureVertices[1].texture[1] = v2;
    mDrawTextureVertices[1].texture[1] = v1;
    mDrawTextureVertices[2].texture[0] = u1;
    mDrawTextureVertices[2].texture[1] = v1;
    mDrawTextureVertices[2].texture[1] = v2;
    mDrawTextureVertices[3].texture[0] = u2;
    mDrawTextureVertices[3].texture[1] = v1;
    mDrawTextureVertices[3].texture[1] = v2;
}

void OpenGLRenderer::getAlphaAndMode(const SkPaint* paint, int* alpha, SkXfermode::Mode* mode) {
    if (paint) {
        const bool isMode = SkXfermode::IsMode(paint->getXfermode(), mode);
        if (!isMode) {
            // Assume SRC_OVER
            *mode = SkXfermode::kSrcOver_Mode;
        }

        // Skia draws using the color's alpha channel if < 255
        // Otherwise, it uses the paint's alpha
        int color = paint->getColor();
        *alpha = (color >> 24) & 0xFF;
        if (*alpha == 255) {
            *alpha = paint->getAlpha();
        }
    } else {
        *mode = SkXfermode::kSrcOver_Mode;
        *alpha = 255;
    }
}

}; // namespace uirenderer
+14 −1
Original line number Diff line number Diff line
@@ -103,6 +103,9 @@ public:
    bool clipRect(float left, float top, float right, float bottom);

    void drawBitmap(SkBitmap* bitmap, float left, float top, const SkPaint* paint);
    void drawBitmap(SkBitmap* bitmap, float srcLeft, float srcTop, float srcRight, float srcBottom,
            float dstLeft, float dstTop, float dstRight, float dstBottom,
            const SkMatrix* matrix, const SkPaint* paint);
    void drawColor(int color, SkXfermode::Mode mode);
    void drawRect(float left, float top, float right, float bottom, const SkPaint* paint);

@@ -193,7 +196,7 @@ private:
     * Resets the texture coordinates stored in mDrawTextureVertices. Setting the values
     * back to default is achieved by calling:
     *
     * resetDrawTextureTexCoords(0.0f, 1.0f, 1.0f, 0.0f);
     * resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f);
     *
     * @param u1 The left coordinate of the texture
     * @param v1 The bottom coordinate of the texture
@@ -202,6 +205,16 @@ private:
     */
    void resetDrawTextureTexCoords(float u1, float v1, float u2, float v2);

    /**
     * Gets the alpha and xfermode out of a paint object. If the paint is null
     * alpha will be 255 and the xfermode will be SRC_OVER.
     *
     * @param paint The paint to extract values from
     * @param alpha Where to store the resulting alpha
     * @param mode Where to store the resulting xfermode
     */
    inline void getAlphaAndMode(const SkPaint* paint, int* alpha, SkXfermode::Mode* mode);

    // Dimensions of the drawing surface
    int mWidth, mHeight;

+1 −0
Original line number Diff line number Diff line
@@ -92,6 +92,7 @@ void TextureCache::generateTexture(SkBitmap* bitmap, Texture* texture, bool rege

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

Loading