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

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

Merge "Optimize state changes"

parents 4dbfa44a f3a910b4
Loading
Loading
Loading
Loading
+34 −4
Original line number Diff line number Diff line
@@ -73,6 +73,9 @@ void Caches::init() {
    glBufferData(GL_ARRAY_BUFFER, sizeof(gMeshVertices), gMeshVertices, GL_STATIC_DRAW);

    mCurrentBuffer = meshBuffer;
    mCurrentPositionPointer = this;
    mCurrentTexCoordsPointer = this;

    mRegionMesh = NULL;

    blend = false;
@@ -218,22 +221,49 @@ void Caches::flush(FlushMode mode) {
// VBO
///////////////////////////////////////////////////////////////////////////////

void Caches::bindMeshBuffer() {
    bindMeshBuffer(meshBuffer);
bool Caches::bindMeshBuffer() {
    return bindMeshBuffer(meshBuffer);
}

void Caches::bindMeshBuffer(const GLuint buffer) {
bool Caches::bindMeshBuffer(const GLuint buffer) {
    if (mCurrentBuffer != buffer) {
        glBindBuffer(GL_ARRAY_BUFFER, buffer);
        mCurrentBuffer = buffer;
        return true;
    }
    return false;
}

void Caches::unbindMeshBuffer() {
bool Caches::unbindMeshBuffer() {
    if (mCurrentBuffer) {
        glBindBuffer(GL_ARRAY_BUFFER, 0);
        mCurrentBuffer = 0;
        return true;
    }
    return false;
}

void Caches::bindPositionVertexPointer(bool force, GLuint slot, GLvoid* vertices, GLsizei stride) {
    if (force || vertices != mCurrentPositionPointer) {
        glVertexAttribPointer(slot, 2, GL_FLOAT, GL_FALSE, stride, vertices);
        mCurrentPositionPointer = vertices;
    }
}

void Caches::bindTexCoordsVertexPointer(bool force, GLuint slot, GLvoid* vertices) {
    if (force || vertices != mCurrentTexCoordsPointer) {
        glVertexAttribPointer(slot, 2, GL_FLOAT, GL_FALSE, gMeshStride, vertices);
        mCurrentTexCoordsPointer = vertices;
    }
}

void Caches::resetVertexPointers() {
    mCurrentPositionPointer = this;
    mCurrentTexCoordsPointer = this;
}

void Caches::resetTexCoordsVertexPointer() {
    mCurrentTexCoordsPointer = this;
}

TextureVertex* Caches::getRegionMesh() {
+33 −12
Original line number Diff line number Diff line
@@ -91,15 +91,6 @@ class ANDROID_API Caches: public Singleton<Caches> {

    CacheLogger mLogger;

    GLuint mCurrentBuffer;

    // Used to render layers
    TextureVertex* mRegionMesh;
    GLuint mRegionMeshIndices;

    mutable Mutex mGarbageLock;
    Vector<Layer*> mLayerGarbage;

public:
    enum FlushMode {
        kFlushMode_Layers = 0,
@@ -147,17 +138,36 @@ public:
    /**
     * Binds the VBO used to render simple textured quads.
     */
    void bindMeshBuffer();
    bool bindMeshBuffer();

    /**
     * Binds the specified VBO if needed.
     */
    void bindMeshBuffer(const GLuint buffer);
    bool bindMeshBuffer(const GLuint buffer);

    /**
     * Unbinds the VBO used to render simple textured quads.
     */
    void unbindMeshBuffer();
    bool unbindMeshBuffer();

    /**
     * Binds an attrib to the specified float vertex pointer.
     * Assumes a stride of gMeshStride and a size of 2.
     */
    void bindPositionVertexPointer(bool force, GLuint slot, GLvoid* vertices,
            GLsizei stride = gMeshStride);

    /**
     * Binds an attrib to the specified float vertex pointer.
     * Assumes a stride of gMeshStride and a size of 2.
     */
    void bindTexCoordsVertexPointer(bool force, GLuint slot, GLvoid* vertices);

    /**
     * Resets the vertex pointers.
     */
    void resetVertexPointers();
    void resetTexCoordsVertexPointer();

    /**
     * Returns the mesh used to draw regions. Calling this method will
@@ -203,6 +213,17 @@ public:
    ResourceCache resourceCache;

private:
    GLuint mCurrentBuffer;
    void* mCurrentPositionPointer;
    void* mCurrentTexCoordsPointer;

    // Used to render layers
    TextureVertex* mRegionMesh;
    GLuint mRegionMeshIndices;

    mutable Mutex mGarbageLock;
    Vector<Layer*> mLayerGarbage;

    DebugLevel mDebugLevel;
    bool mInitialized;
}; // class Caches
+0 −13
Original line number Diff line number Diff line
@@ -325,8 +325,6 @@ FontRenderer::FontRenderer() {
    mTextTexture = NULL;

    mIndexBufferID = 0;
    mPositionAttrSlot = -1;
    mTexcoordAttrSlot = -1;

    mCacheWidth = DEFAULT_TEXT_CACHE_WIDTH;
    mCacheHeight = DEFAULT_TEXT_CACHE_HEIGHT;
@@ -599,12 +597,6 @@ void FontRenderer::checkTextureUpdate() {
void FontRenderer::issueDrawCommand() {
    checkTextureUpdate();

    float* vtx = mTextMeshPtr;
    float* tex = vtx + 2;

    glVertexAttribPointer(mPositionAttrSlot, 2, GL_FLOAT, GL_FALSE, 16, vtx);
    glVertexAttribPointer(mTexcoordAttrSlot, 2, GL_FLOAT, GL_FALSE, 16, tex);

    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mIndexBufferID);
    glDrawElements(GL_TRIANGLES, mCurrentQuadIndex * 6, GL_UNSIGNED_SHORT, NULL);

@@ -760,11 +752,6 @@ bool FontRenderer::renderText(SkPaint* paint, const Rect* clip, const char *text
        return false;
    }

    if (mPositionAttrSlot < 0 || mTexcoordAttrSlot < 0) {
        LOGE("Font renderer unable to draw, attribute slots undefined");
        return false;
    }

    mDrawn = false;
    mBounds = bounds;
    mClip = clip;
+7 −6
Original line number Diff line number Diff line
@@ -178,9 +178,13 @@ public:
        mGammaTable = gammaTable;
    }

    void setAttributeBindingSlots(int positionSlot, int texCoordSlot) {
        mPositionAttrSlot = positionSlot;
        mTexcoordAttrSlot = texCoordSlot;
    inline float* getMeshBuffer() {
        checkInit();
        return mTextMeshPtr;
    }

    inline int getMeshTexCoordsOffset() const {
        return 2;
    }

    void setFont(SkPaint* paint, uint32_t fontId, float fontSize);
@@ -309,9 +313,6 @@ protected:

    uint32_t mIndexBufferID;

    int32_t mPositionAttrSlot;
    int32_t mTexcoordAttrSlot;

    const Rect* mClip;
    Rect* mBounds;
    bool mDrawn;
+25 −24
Original line number Diff line number Diff line
@@ -199,13 +199,13 @@ void OpenGLRenderer::interrupt() {
        }
    }
    mCaches.unbindMeshBuffer();
    mCaches.resetVertexPointers();
}

void OpenGLRenderer::resume() {
    sp<Snapshot> snapshot = (mSnapshot != NULL) ? mSnapshot : mFirstSnapshot;

    glViewport(0, 0, snapshot->viewport.getWidth(), snapshot->viewport.getHeight());

    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);

    glEnable(GL_SCISSOR_TEST);
@@ -1201,16 +1201,15 @@ void OpenGLRenderer::setupDrawColorFilterUniforms() {
}

void OpenGLRenderer::setupDrawSimpleMesh() {
    mCaches.bindMeshBuffer();
    glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE,
            gMeshStride, 0);
    bool force = mCaches.bindMeshBuffer();
    mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position, 0);
}

void OpenGLRenderer::setupDrawTexture(GLuint texture) {
    bindTexture(texture);
    glUniform1i(mCaches.currentProgram->getUniform("sampler"), mTextureUnit++);

    mTexCoordsSlot = mCaches.currentProgram->getAttrib("texCoords");
    mTexCoordsSlot = mCaches.currentProgram->texCoords;
    glEnableVertexAttribArray(mTexCoordsSlot);
}

@@ -1218,7 +1217,7 @@ void OpenGLRenderer::setupDrawExternalTexture(GLuint texture) {
    bindExternalTexture(texture);
    glUniform1i(mCaches.currentProgram->getUniform("sampler"), mTextureUnit++);

    mTexCoordsSlot = mCaches.currentProgram->getAttrib("texCoords");
    mTexCoordsSlot = mCaches.currentProgram->texCoords;
    glEnableVertexAttribArray(mTexCoordsSlot);
}

@@ -1232,23 +1231,23 @@ void OpenGLRenderer::setupDrawTextureTransformUniforms(mat4& transform) {
}

void OpenGLRenderer::setupDrawMesh(GLvoid* vertices, GLvoid* texCoords, GLuint vbo) {
    bool force = false;
    if (!vertices) {
        mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo);
        force = mCaches.bindMeshBuffer(vbo == 0 ? mCaches.meshBuffer : vbo);
    } else {
        mCaches.unbindMeshBuffer();
        force = mCaches.unbindMeshBuffer();
    }

    glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE,
            gMeshStride, vertices);
    mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position, vertices);
    if (mTexCoordsSlot >= 0) {
        glVertexAttribPointer(mTexCoordsSlot, 2, GL_FLOAT, GL_FALSE, gMeshStride, texCoords);
        mCaches.bindTexCoordsVertexPointer(force, mTexCoordsSlot, texCoords);
    }
}

void OpenGLRenderer::setupDrawVertices(GLvoid* vertices) {
    mCaches.unbindMeshBuffer();
    glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE,
            gVertexStride, vertices);
    bool force = mCaches.unbindMeshBuffer();
    mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position,
            vertices, gVertexStride);
}

/**
@@ -1264,10 +1263,10 @@ void OpenGLRenderer::setupDrawVertices(GLvoid* vertices) {
 */
void OpenGLRenderer::setupDrawAALine(GLvoid* vertices, GLvoid* widthCoords,
        GLvoid* lengthCoords, float boundaryWidthProportion) {
    mCaches.unbindMeshBuffer();

    glVertexAttribPointer(mCaches.currentProgram->position, 2, GL_FLOAT, GL_FALSE,
            gAAVertexStride, vertices);
    bool force = mCaches.unbindMeshBuffer();
    mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position,
            vertices, gAAVertexStride);
    mCaches.resetTexCoordsVertexPointer();

    int widthSlot = mCaches.currentProgram->getAttrib("vtxWidth");
    glEnableVertexAttribArray(widthSlot);
@@ -2186,12 +2185,14 @@ void OpenGLRenderer::drawText(const char* text, int bytesCount, int count,
#else
    bool hasActiveLayer = false;
#endif
    mCaches.unbindMeshBuffer();

    // Tell font renderer the locations of position and texture coord
    // attributes so it can bind its data properly
    int positionSlot = mCaches.currentProgram->position;
    fontRenderer.setAttributeBindingSlots(positionSlot, mTexCoordsSlot);
    float* buffer = fontRenderer.getMeshBuffer();
    int offset = fontRenderer.getMeshTexCoordsOffset();

    bool force = mCaches.unbindMeshBuffer();
    mCaches.bindPositionVertexPointer(force, mCaches.currentProgram->position, buffer);
    mCaches.bindTexCoordsVertexPointer(force, mTexCoordsSlot, buffer + offset);

    if (fontRenderer.renderText(paint, clip, text, 0, bytesCount, count, x, y,
            hasActiveLayer ? &bounds : NULL)) {
#if RENDER_LAYERS_AS_REGIONS
@@ -2205,7 +2206,7 @@ void OpenGLRenderer::drawText(const char* text, int bytesCount, int count,
    }

    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
    glDisableVertexAttribArray(mCaches.currentProgram->getAttrib("texCoords"));
    glDisableVertexAttribArray(mCaches.currentProgram->texCoords);

    drawTextDecorations(text, bytesCount, length, oldX, oldY, paint);
}
Loading