TextDropShadowCache.cpp revision 25dc3a7dbac2f90f5144035e9c8ed99c09cc3132
1/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "OpenGLRenderer"
18
19#include "TextDropShadowCache.h"
20#include "Properties.h"
21
22namespace android {
23namespace uirenderer {
24
25///////////////////////////////////////////////////////////////////////////////
26// Constructors/destructor
27///////////////////////////////////////////////////////////////////////////////
28
29TextDropShadowCache::TextDropShadowCache():
30        mCache(GenerationCache<ShadowText, ShadowTexture*>::kUnlimitedCapacity),
31        mSize(0), mMaxSize(MB(DEFAULT_DROP_SHADOW_CACHE_SIZE)) {
32    char property[PROPERTY_VALUE_MAX];
33    if (property_get(PROPERTY_DROP_SHADOW_CACHE_SIZE, property, NULL) > 0) {
34        LOGD("  Setting drop shadow cache size to %sMB", property);
35        setMaxSize(MB(atof(property)));
36    } else {
37        LOGD("  Using default drop shadow cache size of %.2fMB", DEFAULT_DROP_SHADOW_CACHE_SIZE);
38    }
39
40    init();
41}
42
43TextDropShadowCache::TextDropShadowCache(uint32_t maxByteSize):
44        mCache(GenerationCache<ShadowText, ShadowTexture*>::kUnlimitedCapacity),
45        mSize(0), mMaxSize(maxByteSize) {
46    init();
47}
48
49TextDropShadowCache::~TextDropShadowCache() {
50    mCache.clear();
51}
52
53void TextDropShadowCache::init() {
54    mCache.setOnEntryRemovedListener(this);
55    mDebugEnabled = readDebugLevel() & kDebugMoreCaches;
56}
57
58///////////////////////////////////////////////////////////////////////////////
59// Size management
60///////////////////////////////////////////////////////////////////////////////
61
62uint32_t TextDropShadowCache::getSize() {
63    return mSize;
64}
65
66uint32_t TextDropShadowCache::getMaxSize() {
67    return mMaxSize;
68}
69
70void TextDropShadowCache::setMaxSize(uint32_t maxSize) {
71    mMaxSize = maxSize;
72    while (mSize > mMaxSize) {
73        mCache.removeOldest();
74    }
75}
76
77///////////////////////////////////////////////////////////////////////////////
78// Callbacks
79///////////////////////////////////////////////////////////////////////////////
80
81void TextDropShadowCache::operator()(ShadowText& text, ShadowTexture*& texture) {
82    if (texture) {
83        mSize -= texture->bitmapSize;
84
85        if (mDebugEnabled) {
86            LOGD("Shadow texture deleted, size = %d", texture->bitmapSize);
87        }
88
89        glDeleteTextures(1, &texture->id);
90        delete texture;
91    }
92}
93
94///////////////////////////////////////////////////////////////////////////////
95// Caching
96///////////////////////////////////////////////////////////////////////////////
97
98void TextDropShadowCache::clear() {
99    mCache.clear();
100}
101
102ShadowTexture* TextDropShadowCache::get(SkPaint* paint, const char* text, uint32_t len,
103        int numGlyphs, uint32_t radius) {
104    ShadowText entry(paint, radius, len, text);
105    ShadowTexture* texture = mCache.get(entry);
106
107    if (!texture) {
108        FontRenderer::DropShadow shadow = mRenderer->renderDropShadow(paint, text, 0,
109                len, numGlyphs, radius);
110
111        texture = new ShadowTexture;
112        texture->left = shadow.penX;
113        texture->top = shadow.penY;
114        texture->width = shadow.width;
115        texture->height = shadow.height;
116        texture->generation = 0;
117        texture->blend = true;
118
119        const uint32_t size = shadow.width * shadow.height;
120        texture->bitmapSize = size;
121
122        // Don't even try to cache a bitmap that's bigger than the cache
123        if (size < mMaxSize) {
124            while (mSize + size > mMaxSize) {
125                mCache.removeOldest();
126            }
127        }
128
129        glGenTextures(1, &texture->id);
130
131        glBindTexture(GL_TEXTURE_2D, texture->id);
132        // Textures are Alpha8
133        glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
134
135        glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, texture->width, texture->height, 0,
136                GL_ALPHA, GL_UNSIGNED_BYTE, shadow.image);
137
138        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
139        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
140
141        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
142        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
143
144        if (size < mMaxSize) {
145            if (mDebugEnabled) {
146                LOGD("Shadow texture created, size = %d", texture->bitmapSize);
147            }
148            mSize += size;
149            mCache.put(entry, texture);
150        } else {
151            texture->cleanup = true;
152        }
153
154        // Cleanup shadow
155        delete[] shadow.image;
156    }
157
158    return texture;
159}
160
161}; // namespace uirenderer
162}; // namespace android
163