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

Commit 22158e13 authored by Romain Guy's avatar Romain Guy
Browse files

Automatically cleanup textures that don't fit in the cache.

Change-Id: I4f29ed96ea11118b391fb957e1e4d1b8fcef1537
parent ff0f9734
Loading
Loading
Loading
Loading
+8 −1
Original line number Diff line number Diff line
@@ -436,6 +436,8 @@ void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, float left, float top, const S
    }

    const Texture* texture = mTextureCache.get(bitmap);
    const AutoTexture autoCleanup(texture);

    drawTextureRect(left, top, right, bottom, texture, paint);
}

@@ -449,6 +451,8 @@ void OpenGLRenderer::drawBitmap(SkBitmap* bitmap, const SkMatrix* matrix, const
    }

    const Texture* texture = mTextureCache.get(bitmap);
    const AutoTexture autoCleanup(texture);

    drawTextureRect(r.left, r.top, r.right, r.bottom, texture, paint);
}

@@ -461,6 +465,7 @@ void OpenGLRenderer::drawBitmap(SkBitmap* bitmap,
    }

    const Texture* texture = mTextureCache.get(bitmap);
    const AutoTexture autoCleanup(texture);

    const float width = texture->width;
    const float height = texture->height;
@@ -484,6 +489,7 @@ void OpenGLRenderer::drawPatch(SkBitmap* bitmap, Res_png_9patch* patch,
    }

    const Texture* texture = mTextureCache.get(bitmap);
    const AutoTexture autoCleanup(texture);

    int alpha;
    SkXfermode::Mode mode;
@@ -610,7 +616,8 @@ void OpenGLRenderer::drawPath(SkPath* path, SkPaint* paint) {
    GLuint textureUnit = 0;
    glActiveTexture(gTextureUnits[textureUnit]);

    PathTexture* texture = mPathCache.get(path, paint);
    const PathTexture* texture = mPathCache.get(path, paint);
    const AutoTexture autoCleanup(texture);

    int alpha;
    SkXfermode::Mode mode;
+2 −1
Original line number Diff line number Diff line
@@ -88,7 +88,6 @@ PathTexture* PathCache::get(SkPath* path, SkPaint* paint) {
        texture = addTexture(entry, path, paint);
    }

    // TODO: Do something to destroy the texture object if it's too big for the cache
    return texture;
}

@@ -129,6 +128,8 @@ PathTexture* PathCache::addTexture(const PathCacheEntry& entry,
    if (size < mMaxSize) {
        mSize += size;
        mCache.put(entry, texture);
    } else {
        texture->cleanup = true;
    }

    return texture;
+3 −0
Original line number Diff line number Diff line
@@ -71,6 +71,9 @@ struct PathCacheEntry {
 * Alpha texture used to represent a path.
 */
struct PathTexture: public Texture {
    PathTexture(): Texture() {
    }

    /**
     * Left coordinate of the path bounds.
     */
+22 −0
Original line number Diff line number Diff line
@@ -26,6 +26,10 @@ namespace uirenderer {
 * Represents an OpenGL texture.
 */
struct Texture {
    Texture() {
        cleanup = false;
    }

    /**
     * Name of the texture.
     */
@@ -46,8 +50,26 @@ struct Texture {
     * Height of the backing bitmap.
     */
    uint32_t height;
    /**
     * Indicates whether this texture should be cleaned up after use.
     */
    bool cleanup;
}; // struct Texture

class AutoTexture {
public:
    AutoTexture(const Texture* texture): mTexture(texture) { }
    ~AutoTexture() {
        if (mTexture && mTexture->cleanup) {
            glDeleteTextures(1, &mTexture->id);
            delete mTexture;
        }
    }

private:
    const Texture* mTexture;
}; // class AutoTexture

}; // namespace uirenderer
}; // namespace android

+3 −1
Original line number Diff line number Diff line
@@ -93,11 +93,13 @@ Texture* TextureCache::get(SkBitmap* bitmap) {
        if (size < mMaxSize) {
            mSize += size;
            mCache.put(bitmap, texture);
        } else {
            texture->cleanup = true;
        }
    } else if (bitmap->getGenerationID() != texture->generation) {
        generateTexture(bitmap, texture, true);
    }
    // TODO: Do something to destroy the texture object if it's too big for the cache

    return texture;
}