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

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

Merge "Add shader-based text gamma correction"

parents 5e457b70 41210633
Loading
Loading
Loading
Loading
+3 −0
Original line number Original line Diff line number Diff line
@@ -65,6 +65,9 @@
// Turn on to enable additional debugging in the font renderers
// Turn on to enable additional debugging in the font renderers
#define DEBUG_FONT_RENDERER 0
#define DEBUG_FONT_RENDERER 0


// Force gamma correction in shaders
#define DEBUG_FONT_RENDERER_FORCE_SHADER_GAMMA 0

// Turn on to dump display list state
// Turn on to dump display list state
#define DEBUG_DISPLAY_LIST 0
#define DEBUG_DISPLAY_LIST 0


+42 −7
Original line number Original line Diff line number Diff line
@@ -23,6 +23,18 @@
namespace android {
namespace android {
namespace uirenderer {
namespace uirenderer {


///////////////////////////////////////////////////////////////////////////////
// Utils
///////////////////////////////////////////////////////////////////////////////

static int luminance(const SkPaint* paint) {
    uint32_t c = paint->getColor();
    const int r = (c >> 16) & 0xFF;
    const int g = (c >>  8) & 0xFF;
    const int b = (c      ) & 0xFF;
    return (r * 2 + g * 5 + b) >> 3;
}

///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
// Base class GammaFontRenderer
// Base class GammaFontRenderer
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
@@ -36,7 +48,11 @@ GammaFontRenderer* GammaFontRenderer::createRenderer() {
        }
        }
    }
    }


#if DEBUG_FONT_RENDERER_FORCE_SHADER_GAMMA
    return new ShaderGammaFontRenderer();
#else
    return new LookupGammaFontRenderer();
    return new LookupGammaFontRenderer();
#endif
}
}


GammaFontRenderer::GammaFontRenderer() {
GammaFontRenderer::GammaFontRenderer() {
@@ -82,6 +98,29 @@ GammaFontRenderer::~GammaFontRenderer() {


ShaderGammaFontRenderer::ShaderGammaFontRenderer(): GammaFontRenderer() {
ShaderGammaFontRenderer::ShaderGammaFontRenderer(): GammaFontRenderer() {
    INIT_LOGD("Creating shader gamma font renderer");
    INIT_LOGD("Creating shader gamma font renderer");
    mRenderer = NULL;
}

void ShaderGammaFontRenderer::describe(ProgramDescription& description,
        const SkPaint* paint) const {
    if (paint->getShader() == NULL) {
        const int l = luminance(paint);

        if (l <= mBlackThreshold) {
            description.hasGammaCorrection = true;
            description.gamma = mGamma;
        } else if (l >= mWhiteThreshold) {
            description.hasGammaCorrection = true;
            description.gamma = 1.0f / mGamma;
        }
    }
}

void ShaderGammaFontRenderer::setupProgram(ProgramDescription& description,
        Program* program) const {
    if (description.hasGammaCorrection) {
        glUniform1f(program->getUniform("gamma"), description.gamma);
    }
}
}


///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
@@ -164,15 +203,11 @@ FontRenderer* LookupGammaFontRenderer::getRenderer(Gamma gamma) {


FontRenderer& LookupGammaFontRenderer::getFontRenderer(const SkPaint* paint) {
FontRenderer& LookupGammaFontRenderer::getFontRenderer(const SkPaint* paint) {
    if (paint->getShader() == NULL) {
    if (paint->getShader() == NULL) {
        uint32_t c = paint->getColor();
        const int l = luminance(paint);
        const int r = (c >> 16) & 0xFF;
        const int g = (c >>  8) & 0xFF;
        const int b = (c      ) & 0xFF;
        const int luminance = (r * 2 + g * 5 + b) >> 3;


        if (luminance <= mBlackThreshold) {
        if (l <= mBlackThreshold) {
            return *getRenderer(kGammaBlack);
            return *getRenderer(kGammaBlack);
        } else if (luminance >= mWhiteThreshold) {
        } else if (l >= mWhiteThreshold) {
            return *getRenderer(kGammaWhite);
            return *getRenderer(kGammaWhite);
        }
        }
    }
    }
+13 −1
Original line number Original line Diff line number Diff line
@@ -20,6 +20,7 @@
#include <SkPaint.h>
#include <SkPaint.h>


#include "FontRenderer.h"
#include "FontRenderer.h"
#include "Program.h"


namespace android {
namespace android {
namespace uirenderer {
namespace uirenderer {
@@ -34,9 +35,11 @@ public:
    virtual FontRenderer& getFontRenderer(const SkPaint* paint) = 0;
    virtual FontRenderer& getFontRenderer(const SkPaint* paint) = 0;


    virtual uint32_t getFontRendererCount() const = 0;
    virtual uint32_t getFontRendererCount() const = 0;

    virtual uint32_t getFontRendererSize(uint32_t fontRenderer) const = 0;
    virtual uint32_t getFontRendererSize(uint32_t fontRenderer) const = 0;


    virtual void describe(ProgramDescription& description, const SkPaint* paint) const = 0;
    virtual void setupProgram(ProgramDescription& description, Program* program) const = 0;

    static GammaFontRenderer* createRenderer();
    static GammaFontRenderer* createRenderer();


protected:
protected:
@@ -79,6 +82,9 @@ public:
        return mRenderer->getCacheSize();
        return mRenderer->getCacheSize();
    }
    }


    void describe(ProgramDescription& description, const SkPaint* paint) const;
    void setupProgram(ProgramDescription& description, Program* program) const;

private:
private:
    ShaderGammaFontRenderer();
    ShaderGammaFontRenderer();


@@ -109,6 +115,12 @@ public:
        return renderer->getCacheSize();
        return renderer->getCacheSize();
    }
    }


    void describe(ProgramDescription& description, const SkPaint* paint) const {
    }

    void setupProgram(ProgramDescription& description, Program* program) const {
    }

private:
private:
    LookupGammaFontRenderer();
    LookupGammaFontRenderer();


+16 −0
Original line number Original line Diff line number Diff line
@@ -1174,6 +1174,10 @@ void OpenGLRenderer::setupDrawAlpha8Color(int color, int alpha) {
    mSetShaderColor = mDescription.setAlpha8Color(mColorR, mColorG, mColorB, mColorA);
    mSetShaderColor = mDescription.setAlpha8Color(mColorR, mColorG, mColorB, mColorA);
}
}


void OpenGLRenderer::setupDrawTextGamma(const SkPaint* paint) {
    mCaches.fontRenderer->describe(mDescription, paint);
}

void OpenGLRenderer::setupDrawColor(float r, float g, float b, float a) {
void OpenGLRenderer::setupDrawColor(float r, float g, float b, float a) {
    mColorA = a;
    mColorA = a;
    mColorR = r;
    mColorR = r;
@@ -1301,6 +1305,10 @@ void OpenGLRenderer::setupDrawColorFilterUniforms() {
    }
    }
}
}


void OpenGLRenderer::setupDrawTextGammaUniforms() {
    mCaches.fontRenderer->setupProgram(mDescription, mCaches.currentProgram);
}

void OpenGLRenderer::setupDrawSimpleMesh() {
void OpenGLRenderer::setupDrawSimpleMesh() {
    bool force = mCaches.bindMeshBuffer();
    bool force = mCaches.bindMeshBuffer();
    mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position, 0);
    mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position, 0);
@@ -2302,6 +2310,7 @@ status_t OpenGLRenderer::drawPosText(const char* text, int bytesCount, int count


    mCaches.activeTexture(0);
    mCaches.activeTexture(0);
    setupDraw();
    setupDraw();
    setupDrawTextGamma(paint);
    setupDrawDirtyRegionsDisabled();
    setupDrawDirtyRegionsDisabled();
    setupDrawWithTexture(true);
    setupDrawWithTexture(true);
    setupDrawAlpha8Color(paint->getColor(), alpha);
    setupDrawAlpha8Color(paint->getColor(), alpha);
@@ -2314,6 +2323,7 @@ status_t OpenGLRenderer::drawPosText(const char* text, int bytesCount, int count
    setupDrawPureColorUniforms();
    setupDrawPureColorUniforms();
    setupDrawColorFilterUniforms();
    setupDrawColorFilterUniforms();
    setupDrawShaderUniforms(pureTranslate);
    setupDrawShaderUniforms(pureTranslate);
    setupDrawTextGammaUniforms();


    const Rect* clip = pureTranslate ? mSnapshot->clipRect : &mSnapshot->getLocalClip();
    const Rect* clip = pureTranslate ? mSnapshot->clipRect : &mSnapshot->getLocalClip();
    Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
    Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
@@ -2387,6 +2397,8 @@ status_t OpenGLRenderer::drawText(const char* text, int bytesCount, int count,
    if (CC_UNLIKELY(mHasShadow)) {
    if (CC_UNLIKELY(mHasShadow)) {
        mCaches.activeTexture(0);
        mCaches.activeTexture(0);


        // NOTE: The drop shadow will not perform gamma correction
        //       if shader-based correction is enabled
        mCaches.dropShadowCache.setFontRenderer(fontRenderer);
        mCaches.dropShadowCache.setFontRenderer(fontRenderer);
        const ShadowTexture* shadow = mCaches.dropShadowCache.get(
        const ShadowTexture* shadow = mCaches.dropShadowCache.get(
                paint, text, bytesCount, count, mShadowRadius);
                paint, text, bytesCount, count, mShadowRadius);
@@ -2427,6 +2439,7 @@ status_t OpenGLRenderer::drawText(const char* text, int bytesCount, int count,
    // The font renderer will always use texture unit 0
    // The font renderer will always use texture unit 0
    mCaches.activeTexture(0);
    mCaches.activeTexture(0);
    setupDraw();
    setupDraw();
    setupDrawTextGamma(paint);
    setupDrawDirtyRegionsDisabled();
    setupDrawDirtyRegionsDisabled();
    setupDrawWithTexture(true);
    setupDrawWithTexture(true);
    setupDrawAlpha8Color(paint->getColor(), alpha);
    setupDrawAlpha8Color(paint->getColor(), alpha);
@@ -2441,6 +2454,7 @@ status_t OpenGLRenderer::drawText(const char* text, int bytesCount, int count,
    setupDrawPureColorUniforms();
    setupDrawPureColorUniforms();
    setupDrawColorFilterUniforms();
    setupDrawColorFilterUniforms();
    setupDrawShaderUniforms(pureTranslate);
    setupDrawShaderUniforms(pureTranslate);
    setupDrawTextGammaUniforms();


    const Rect* clip = pureTranslate ? mSnapshot->clipRect : &mSnapshot->getLocalClip();
    const Rect* clip = pureTranslate ? mSnapshot->clipRect : &mSnapshot->getLocalClip();
    Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
    Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
@@ -2485,6 +2499,7 @@ status_t OpenGLRenderer::drawTextOnPath(const char* text, int bytesCount, int co


    mCaches.activeTexture(0);
    mCaches.activeTexture(0);
    setupDraw();
    setupDraw();
    setupDrawTextGamma(paint);
    setupDrawDirtyRegionsDisabled();
    setupDrawDirtyRegionsDisabled();
    setupDrawWithTexture(true);
    setupDrawWithTexture(true);
    setupDrawAlpha8Color(paint->getColor(), alpha);
    setupDrawAlpha8Color(paint->getColor(), alpha);
@@ -2497,6 +2512,7 @@ status_t OpenGLRenderer::drawTextOnPath(const char* text, int bytesCount, int co
    setupDrawPureColorUniforms();
    setupDrawPureColorUniforms();
    setupDrawColorFilterUniforms();
    setupDrawColorFilterUniforms();
    setupDrawShaderUniforms(false);
    setupDrawShaderUniforms(false);
    setupDrawTextGammaUniforms();


    const Rect* clip = &mSnapshot->getLocalClip();
    const Rect* clip = &mSnapshot->getLocalClip();
    Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
    Rect bounds(FLT_MAX / 2.0f, FLT_MAX / 2.0f, FLT_MIN / 2.0f, FLT_MIN / 2.0f);
+2 −0
Original line number Original line Diff line number Diff line
@@ -572,6 +572,7 @@ private:
    void setupDrawColor(int color, int alpha);
    void setupDrawColor(int color, int alpha);
    void setupDrawColor(float r, float g, float b, float a);
    void setupDrawColor(float r, float g, float b, float a);
    void setupDrawAlpha8Color(int color, int alpha);
    void setupDrawAlpha8Color(int color, int alpha);
    void setupDrawTextGamma(const SkPaint* paint);
    void setupDrawShader();
    void setupDrawShader();
    void setupDrawColorFilter();
    void setupDrawColorFilter();
    void setupDrawBlending(SkXfermode::Mode mode = SkXfermode::kSrcOver_Mode,
    void setupDrawBlending(SkXfermode::Mode mode = SkXfermode::kSrcOver_Mode,
@@ -596,6 +597,7 @@ private:
    void setupDrawExternalTexture(GLuint texture);
    void setupDrawExternalTexture(GLuint texture);
    void setupDrawTextureTransform();
    void setupDrawTextureTransform();
    void setupDrawTextureTransformUniforms(mat4& transform);
    void setupDrawTextureTransformUniforms(mat4& transform);
    void setupDrawTextGammaUniforms();
    void setupDrawMesh(GLvoid* vertices, GLvoid* texCoords = NULL, GLuint vbo = 0);
    void setupDrawMesh(GLvoid* vertices, GLvoid* texCoords = NULL, GLuint vbo = 0);
    void setupDrawMeshIndices(GLvoid* vertices, GLvoid* texCoords);
    void setupDrawMeshIndices(GLvoid* vertices, GLvoid* texCoords);
    void setupDrawVertices(GLvoid* vertices);
    void setupDrawVertices(GLvoid* vertices);
Loading