blob: 6976eaa3d70ee1b5c8ae7a7285899f6a4375abf2 [file] [log] [blame]
Romain Guy1e45aae2010-08-13 19:39:53 -07001/*
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
Romain Guy059e12c2012-11-28 17:35:51 -080019#include <utils/JenkinsHash.h>
20
Romain Guyc9855a52011-01-21 21:14:15 -080021#include "Debug.h"
Romain Guy1e45aae2010-08-13 19:39:53 -070022#include "TextDropShadowCache.h"
Romain Guyb45c0c92010-08-26 20:35:23 -070023#include "Properties.h"
Romain Guy1e45aae2010-08-13 19:39:53 -070024
25namespace android {
26namespace uirenderer {
27
28///////////////////////////////////////////////////////////////////////////////
Romain Guy059e12c2012-11-28 17:35:51 -080029// Cache support
30///////////////////////////////////////////////////////////////////////////////
31
32hash_t ShadowText::hash() const {
Romain Guy69fcbcc2012-11-30 15:29:15 -080033 uint32_t charCount = len / sizeof(char16_t);
Romain Guy059e12c2012-11-28 17:35:51 -080034 uint32_t hash = JenkinsHashMix(0, len);
35 hash = JenkinsHashMix(hash, android::hash_type(radius));
36 hash = JenkinsHashMix(hash, android::hash_type(textSize));
37 hash = JenkinsHashMix(hash, android::hash_type(typeface));
38 hash = JenkinsHashMix(hash, flags);
39 hash = JenkinsHashMix(hash, android::hash_type(italicStyle));
40 hash = JenkinsHashMix(hash, android::hash_type(scaleX));
Romain Guy69fcbcc2012-11-30 15:29:15 -080041 if (text) {
42 hash = JenkinsHashMixShorts(hash, text, charCount);
43 }
44 if (positions) {
45 for (uint32_t i = 0; i < charCount * 2; i++) {
46 hash = JenkinsHashMix(hash, android::hash_type(positions[i]));
47 }
Romain Guy059e12c2012-11-28 17:35:51 -080048 }
49 return JenkinsHashWhiten(hash);
50}
51
52int ShadowText::compare(const ShadowText& lhs, const ShadowText& rhs) {
53 int deltaInt = int(lhs.len) - int(rhs.len);
54 if (deltaInt != 0) return deltaInt;
55
56 deltaInt = lhs.flags - rhs.flags;
57 if (deltaInt != 0) return deltaInt;
58
59 if (lhs.radius < rhs.radius) return -1;
60 if (lhs.radius > rhs.radius) return +1;
61
62 if (lhs.typeface < rhs.typeface) return -1;
63 if (lhs.typeface > rhs.typeface) return +1;
64
65 if (lhs.textSize < rhs.textSize) return -1;
66 if (lhs.textSize > rhs.textSize) return +1;
67
68 if (lhs.italicStyle < rhs.italicStyle) return -1;
69 if (lhs.italicStyle > rhs.italicStyle) return +1;
70
71 if (lhs.scaleX < rhs.scaleX) return -1;
72 if (lhs.scaleX > rhs.scaleX) return +1;
73
74 if (lhs.text != rhs.text) {
75 if (!lhs.text) return -1;
76 if (!rhs.text) return +1;
77
78 deltaInt = memcmp(lhs.text, rhs.text, lhs.len);
79 if (deltaInt != 0) return deltaInt;
80 }
81
82 if (lhs.positions != rhs.positions) {
83 if (!lhs.positions) return -1;
84 if (!rhs.positions) return +1;
85
86 return memcmp(lhs.positions, rhs.positions, lhs.len << 2);
87 }
88
89 return 0;
90}
91
92///////////////////////////////////////////////////////////////////////////////
Romain Guy1e45aae2010-08-13 19:39:53 -070093// Constructors/destructor
94///////////////////////////////////////////////////////////////////////////////
95
Romain Guyfb8b7632010-08-23 21:05:08 -070096TextDropShadowCache::TextDropShadowCache():
Romain Guy059e12c2012-11-28 17:35:51 -080097 mCache(LruCache<ShadowText, ShadowTexture*>::kUnlimitedCapacity),
Romain Guyfb8b7632010-08-23 21:05:08 -070098 mSize(0), mMaxSize(MB(DEFAULT_DROP_SHADOW_CACHE_SIZE)) {
99 char property[PROPERTY_VALUE_MAX];
100 if (property_get(PROPERTY_DROP_SHADOW_CACHE_SIZE, property, NULL) > 0) {
Romain Guyc9855a52011-01-21 21:14:15 -0800101 INIT_LOGD(" Setting drop shadow cache size to %sMB", property);
Romain Guyfb8b7632010-08-23 21:05:08 -0700102 setMaxSize(MB(atof(property)));
103 } else {
Romain Guyc9855a52011-01-21 21:14:15 -0800104 INIT_LOGD(" Using default drop shadow cache size of %.2fMB",
105 DEFAULT_DROP_SHADOW_CACHE_SIZE);
Romain Guyfb8b7632010-08-23 21:05:08 -0700106 }
107
Romain Guy25dc3a72010-12-10 12:33:05 -0800108 init();
Romain Guyfb8b7632010-08-23 21:05:08 -0700109}
110
Romain Guy1e45aae2010-08-13 19:39:53 -0700111TextDropShadowCache::TextDropShadowCache(uint32_t maxByteSize):
Romain Guy059e12c2012-11-28 17:35:51 -0800112 mCache(LruCache<ShadowText, ShadowTexture*>::kUnlimitedCapacity),
Romain Guy1e45aae2010-08-13 19:39:53 -0700113 mSize(0), mMaxSize(maxByteSize) {
Romain Guy25dc3a72010-12-10 12:33:05 -0800114 init();
Romain Guy1e45aae2010-08-13 19:39:53 -0700115}
116
117TextDropShadowCache::~TextDropShadowCache() {
118 mCache.clear();
119}
120
Romain Guy25dc3a72010-12-10 12:33:05 -0800121void TextDropShadowCache::init() {
122 mCache.setOnEntryRemovedListener(this);
123 mDebugEnabled = readDebugLevel() & kDebugMoreCaches;
124}
125
Romain Guy1e45aae2010-08-13 19:39:53 -0700126///////////////////////////////////////////////////////////////////////////////
127// Size management
128///////////////////////////////////////////////////////////////////////////////
129
130uint32_t TextDropShadowCache::getSize() {
131 return mSize;
132}
133
134uint32_t TextDropShadowCache::getMaxSize() {
135 return mMaxSize;
136}
137
138void TextDropShadowCache::setMaxSize(uint32_t maxSize) {
139 mMaxSize = maxSize;
140 while (mSize > mMaxSize) {
141 mCache.removeOldest();
142 }
143}
144
145///////////////////////////////////////////////////////////////////////////////
146// Callbacks
147///////////////////////////////////////////////////////////////////////////////
148
149void TextDropShadowCache::operator()(ShadowText& text, ShadowTexture*& texture) {
Romain Guy1e45aae2010-08-13 19:39:53 -0700150 if (texture) {
Romain Guy25dc3a72010-12-10 12:33:05 -0800151 mSize -= texture->bitmapSize;
152
153 if (mDebugEnabled) {
Steve Block5baa3a62011-12-20 16:23:08 +0000154 ALOGD("Shadow texture deleted, size = %d", texture->bitmapSize);
Romain Guy25dc3a72010-12-10 12:33:05 -0800155 }
Romain Guyf70a7e32010-11-09 16:37:03 -0800156
Romain Guy1e45aae2010-08-13 19:39:53 -0700157 glDeleteTextures(1, &texture->id);
158 delete texture;
159 }
160}
161
162///////////////////////////////////////////////////////////////////////////////
163// Caching
164///////////////////////////////////////////////////////////////////////////////
165
166void TextDropShadowCache::clear() {
167 mCache.clear();
168}
169
170ShadowTexture* TextDropShadowCache::get(SkPaint* paint, const char* text, uint32_t len,
Romain Guy059e12c2012-11-28 17:35:51 -0800171 int numGlyphs, float radius, const float* positions) {
Raph Levien416a8472012-07-19 22:48:17 -0700172 ShadowText entry(paint, radius, len, text, positions);
Romain Guy1e45aae2010-08-13 19:39:53 -0700173 ShadowTexture* texture = mCache.get(entry);
174
175 if (!texture) {
Raph Levien8b4072d2012-07-30 15:50:00 -0700176 SkPaint paintCopy(*paint);
177 paintCopy.setTextAlign(SkPaint::kLeft_Align);
178 FontRenderer::DropShadow shadow = mRenderer->renderDropShadow(&paintCopy, text, 0,
Raph Levien416a8472012-07-19 22:48:17 -0700179 len, numGlyphs, radius, positions);
Romain Guy1e45aae2010-08-13 19:39:53 -0700180
Romain Guycf51a412013-04-08 19:40:31 -0700181 if (!shadow.image) {
182 return NULL;
183 }
184
Romain Guy1e45aae2010-08-13 19:39:53 -0700185 texture = new ShadowTexture;
186 texture->left = shadow.penX;
187 texture->top = shadow.penY;
188 texture->width = shadow.width;
189 texture->height = shadow.height;
190 texture->generation = 0;
191 texture->blend = true;
192
193 const uint32_t size = shadow.width * shadow.height;
Romain Guy25dc3a72010-12-10 12:33:05 -0800194 texture->bitmapSize = size;
195
Romain Guy1e45aae2010-08-13 19:39:53 -0700196 // Don't even try to cache a bitmap that's bigger than the cache
197 if (size < mMaxSize) {
198 while (mSize + size > mMaxSize) {
199 mCache.removeOldest();
200 }
201 }
202
203 glGenTextures(1, &texture->id);
204
205 glBindTexture(GL_TEXTURE_2D, texture->id);
206 // Textures are Alpha8
207 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
208
209 glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, texture->width, texture->height, 0,
210 GL_ALPHA, GL_UNSIGNED_BYTE, shadow.image);
211
Romain Guy39d252a2011-12-12 18:14:06 -0800212 texture->setFilter(GL_LINEAR);
213 texture->setWrap(GL_CLAMP_TO_EDGE);
Romain Guy1e45aae2010-08-13 19:39:53 -0700214
215 if (size < mMaxSize) {
Romain Guy25dc3a72010-12-10 12:33:05 -0800216 if (mDebugEnabled) {
Steve Block5baa3a62011-12-20 16:23:08 +0000217 ALOGD("Shadow texture created, size = %d", texture->bitmapSize);
Romain Guy25dc3a72010-12-10 12:33:05 -0800218 }
Romain Guy321dce62011-03-01 11:45:33 -0800219
220 entry.copyTextLocally();
221
Romain Guy1e45aae2010-08-13 19:39:53 -0700222 mSize += size;
223 mCache.put(entry, texture);
224 } else {
225 texture->cleanup = true;
226 }
227
228 // Cleanup shadow
Ben Cheng15641a62013-02-20 13:20:03 -0800229 free(shadow.image);
Romain Guy1e45aae2010-08-13 19:39:53 -0700230 }
231
232 return texture;
233}
234
235}; // namespace uirenderer
236}; // namespace android