TextDropShadowCache.cpp revision 6698749dd4d4d6513b26aa9071af290b956b68a7
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 <utils/JenkinsHash.h>
20
21#include "Caches.h"
22#include "Debug.h"
23#include "TextDropShadowCache.h"
24#include "Properties.h"
25
26namespace android {
27namespace uirenderer {
28
29///////////////////////////////////////////////////////////////////////////////
30// Cache support
31///////////////////////////////////////////////////////////////////////////////
32
33hash_t ShadowText::hash() const {
34    uint32_t charCount = len / sizeof(char16_t);
35    uint32_t hash = JenkinsHashMix(0, len);
36    hash = JenkinsHashMix(hash, android::hash_type(radius));
37    hash = JenkinsHashMix(hash, android::hash_type(textSize));
38    hash = JenkinsHashMix(hash, android::hash_type(typeface));
39    hash = JenkinsHashMix(hash, flags);
40    hash = JenkinsHashMix(hash, android::hash_type(italicStyle));
41    hash = JenkinsHashMix(hash, android::hash_type(scaleX));
42    if (text) {
43        hash = JenkinsHashMixShorts(
44            hash, reinterpret_cast<const uint16_t*>(text), charCount);
45    }
46    if (positions) {
47        for (uint32_t i = 0; i < charCount * 2; i++) {
48            hash = JenkinsHashMix(hash, android::hash_type(positions[i]));
49        }
50    }
51    return JenkinsHashWhiten(hash);
52}
53
54int ShadowText::compare(const ShadowText& lhs, const ShadowText& rhs) {
55    int deltaInt = int(lhs.len) - int(rhs.len);
56    if (deltaInt != 0) return deltaInt;
57
58    deltaInt = lhs.flags - rhs.flags;
59    if (deltaInt != 0) return deltaInt;
60
61    if (lhs.radius < rhs.radius) return -1;
62    if (lhs.radius > rhs.radius) return +1;
63
64    if (lhs.typeface < rhs.typeface) return -1;
65    if (lhs.typeface > rhs.typeface) return +1;
66
67    if (lhs.textSize < rhs.textSize) return -1;
68    if (lhs.textSize > rhs.textSize) return +1;
69
70    if (lhs.italicStyle < rhs.italicStyle) return -1;
71    if (lhs.italicStyle > rhs.italicStyle) return +1;
72
73    if (lhs.scaleX < rhs.scaleX) return -1;
74    if (lhs.scaleX > rhs.scaleX) return +1;
75
76    if (lhs.text != rhs.text) {
77        if (!lhs.text) return -1;
78        if (!rhs.text) return +1;
79
80        deltaInt = memcmp(lhs.text, rhs.text, lhs.len);
81        if (deltaInt != 0) return deltaInt;
82    }
83
84    if (lhs.positions != rhs.positions) {
85        if (!lhs.positions) return -1;
86        if (!rhs.positions) return +1;
87
88        return memcmp(lhs.positions, rhs.positions, lhs.len << 2);
89    }
90
91    return 0;
92}
93
94///////////////////////////////////////////////////////////////////////////////
95// Constructors/destructor
96///////////////////////////////////////////////////////////////////////////////
97
98TextDropShadowCache::TextDropShadowCache():
99        mCache(LruCache<ShadowText, ShadowTexture*>::kUnlimitedCapacity),
100        mSize(0), mMaxSize(MB(DEFAULT_DROP_SHADOW_CACHE_SIZE)) {
101    char property[PROPERTY_VALUE_MAX];
102    if (property_get(PROPERTY_DROP_SHADOW_CACHE_SIZE, property, NULL) > 0) {
103        INIT_LOGD("  Setting drop shadow cache size to %sMB", property);
104        setMaxSize(MB(atof(property)));
105    } else {
106        INIT_LOGD("  Using default drop shadow cache size of %.2fMB",
107                DEFAULT_DROP_SHADOW_CACHE_SIZE);
108    }
109
110    init();
111}
112
113TextDropShadowCache::TextDropShadowCache(uint32_t maxByteSize):
114        mCache(LruCache<ShadowText, ShadowTexture*>::kUnlimitedCapacity),
115        mSize(0), mMaxSize(maxByteSize) {
116    init();
117}
118
119TextDropShadowCache::~TextDropShadowCache() {
120    mCache.clear();
121}
122
123void TextDropShadowCache::init() {
124    mCache.setOnEntryRemovedListener(this);
125    mDebugEnabled = readDebugLevel() & kDebugMoreCaches;
126}
127
128///////////////////////////////////////////////////////////////////////////////
129// Size management
130///////////////////////////////////////////////////////////////////////////////
131
132uint32_t TextDropShadowCache::getSize() {
133    return mSize;
134}
135
136uint32_t TextDropShadowCache::getMaxSize() {
137    return mMaxSize;
138}
139
140void TextDropShadowCache::setMaxSize(uint32_t maxSize) {
141    mMaxSize = maxSize;
142    while (mSize > mMaxSize) {
143        mCache.removeOldest();
144    }
145}
146
147///////////////////////////////////////////////////////////////////////////////
148// Callbacks
149///////////////////////////////////////////////////////////////////////////////
150
151void TextDropShadowCache::operator()(ShadowText&, ShadowTexture*& texture) {
152    if (texture) {
153        mSize -= texture->bitmapSize;
154
155        if (mDebugEnabled) {
156            ALOGD("Shadow texture deleted, size = %d", texture->bitmapSize);
157        }
158
159        texture->deleteTexture();
160        delete texture;
161    }
162}
163
164///////////////////////////////////////////////////////////////////////////////
165// Caching
166///////////////////////////////////////////////////////////////////////////////
167
168void TextDropShadowCache::clear() {
169    mCache.clear();
170}
171
172ShadowTexture* TextDropShadowCache::get(const SkPaint* paint, const char* text, uint32_t len,
173        int numGlyphs, float radius, const float* positions) {
174    ShadowText entry(paint, radius, len, text, positions);
175    ShadowTexture* texture = mCache.get(entry);
176
177    if (!texture) {
178        SkPaint paintCopy(*paint);
179        paintCopy.setTextAlign(SkPaint::kLeft_Align);
180        FontRenderer::DropShadow shadow = mRenderer->renderDropShadow(&paintCopy, text, 0,
181                len, numGlyphs, radius, positions);
182
183        if (!shadow.image) {
184            return NULL;
185        }
186
187        Caches& caches = Caches::getInstance();
188
189        texture = new ShadowTexture(caches);
190        texture->left = shadow.penX;
191        texture->top = shadow.penY;
192        texture->width = shadow.width;
193        texture->height = shadow.height;
194        texture->generation = 0;
195        texture->blend = true;
196
197        const uint32_t size = shadow.width * shadow.height;
198        texture->bitmapSize = size;
199
200        // Don't even try to cache a bitmap that's bigger than the cache
201        if (size < mMaxSize) {
202            while (mSize + size > mMaxSize) {
203                mCache.removeOldest();
204            }
205        }
206
207        glGenTextures(1, &texture->id);
208
209        caches.bindTexture(texture->id);
210        // Textures are Alpha8
211        glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
212
213        glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, texture->width, texture->height, 0,
214                GL_ALPHA, GL_UNSIGNED_BYTE, shadow.image);
215
216        texture->setFilter(GL_LINEAR);
217        texture->setWrap(GL_CLAMP_TO_EDGE);
218
219        if (size < mMaxSize) {
220            if (mDebugEnabled) {
221                ALOGD("Shadow texture created, size = %d", texture->bitmapSize);
222            }
223
224            entry.copyTextLocally();
225
226            mSize += size;
227            mCache.put(entry, texture);
228        } else {
229            texture->cleanup = true;
230        }
231
232        // Cleanup shadow
233        free(shadow.image);
234    }
235
236    return texture;
237}
238
239}; // namespace uirenderer
240}; // namespace android
241