blob: 4d80f7382318d31fcb127f68e659a21e44ca5fdc [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 {
33 uint32_t charCount = len * sizeof(char16_t);
34 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));
41 hash = JenkinsHashMixShorts(hash, text, charCount);
42 for (uint32_t i = 0; i < charCount * 2; i++) {
43 hash = JenkinsHashMix(hash, android::hash_type(positions[i]));
44 }
45 return JenkinsHashWhiten(hash);
46}
47
48int ShadowText::compare(const ShadowText& lhs, const ShadowText& rhs) {
49 int deltaInt = int(lhs.len) - int(rhs.len);
50 if (deltaInt != 0) return deltaInt;
51
52 deltaInt = lhs.flags - rhs.flags;
53 if (deltaInt != 0) return deltaInt;
54
55 if (lhs.radius < rhs.radius) return -1;
56 if (lhs.radius > rhs.radius) return +1;
57
58 if (lhs.typeface < rhs.typeface) return -1;
59 if (lhs.typeface > rhs.typeface) return +1;
60
61 if (lhs.textSize < rhs.textSize) return -1;
62 if (lhs.textSize > rhs.textSize) return +1;
63
64 if (lhs.italicStyle < rhs.italicStyle) return -1;
65 if (lhs.italicStyle > rhs.italicStyle) return +1;
66
67 if (lhs.scaleX < rhs.scaleX) return -1;
68 if (lhs.scaleX > rhs.scaleX) return +1;
69
70 if (lhs.text != rhs.text) {
71 if (!lhs.text) return -1;
72 if (!rhs.text) return +1;
73
74 deltaInt = memcmp(lhs.text, rhs.text, lhs.len);
75 if (deltaInt != 0) return deltaInt;
76 }
77
78 if (lhs.positions != rhs.positions) {
79 if (!lhs.positions) return -1;
80 if (!rhs.positions) return +1;
81
82 return memcmp(lhs.positions, rhs.positions, lhs.len << 2);
83 }
84
85 return 0;
86}
87
88///////////////////////////////////////////////////////////////////////////////
Romain Guy1e45aae2010-08-13 19:39:53 -070089// Constructors/destructor
90///////////////////////////////////////////////////////////////////////////////
91
Romain Guyfb8b7632010-08-23 21:05:08 -070092TextDropShadowCache::TextDropShadowCache():
Romain Guy059e12c2012-11-28 17:35:51 -080093 mCache(LruCache<ShadowText, ShadowTexture*>::kUnlimitedCapacity),
Romain Guyfb8b7632010-08-23 21:05:08 -070094 mSize(0), mMaxSize(MB(DEFAULT_DROP_SHADOW_CACHE_SIZE)) {
95 char property[PROPERTY_VALUE_MAX];
96 if (property_get(PROPERTY_DROP_SHADOW_CACHE_SIZE, property, NULL) > 0) {
Romain Guyc9855a52011-01-21 21:14:15 -080097 INIT_LOGD(" Setting drop shadow cache size to %sMB", property);
Romain Guyfb8b7632010-08-23 21:05:08 -070098 setMaxSize(MB(atof(property)));
99 } else {
Romain Guyc9855a52011-01-21 21:14:15 -0800100 INIT_LOGD(" Using default drop shadow cache size of %.2fMB",
101 DEFAULT_DROP_SHADOW_CACHE_SIZE);
Romain Guyfb8b7632010-08-23 21:05:08 -0700102 }
103
Romain Guy25dc3a72010-12-10 12:33:05 -0800104 init();
Romain Guyfb8b7632010-08-23 21:05:08 -0700105}
106
Romain Guy1e45aae2010-08-13 19:39:53 -0700107TextDropShadowCache::TextDropShadowCache(uint32_t maxByteSize):
Romain Guy059e12c2012-11-28 17:35:51 -0800108 mCache(LruCache<ShadowText, ShadowTexture*>::kUnlimitedCapacity),
Romain Guy1e45aae2010-08-13 19:39:53 -0700109 mSize(0), mMaxSize(maxByteSize) {
Romain Guy25dc3a72010-12-10 12:33:05 -0800110 init();
Romain Guy1e45aae2010-08-13 19:39:53 -0700111}
112
113TextDropShadowCache::~TextDropShadowCache() {
114 mCache.clear();
115}
116
Romain Guy25dc3a72010-12-10 12:33:05 -0800117void TextDropShadowCache::init() {
118 mCache.setOnEntryRemovedListener(this);
119 mDebugEnabled = readDebugLevel() & kDebugMoreCaches;
120}
121
Romain Guy1e45aae2010-08-13 19:39:53 -0700122///////////////////////////////////////////////////////////////////////////////
123// Size management
124///////////////////////////////////////////////////////////////////////////////
125
126uint32_t TextDropShadowCache::getSize() {
127 return mSize;
128}
129
130uint32_t TextDropShadowCache::getMaxSize() {
131 return mMaxSize;
132}
133
134void TextDropShadowCache::setMaxSize(uint32_t maxSize) {
135 mMaxSize = maxSize;
136 while (mSize > mMaxSize) {
137 mCache.removeOldest();
138 }
139}
140
141///////////////////////////////////////////////////////////////////////////////
142// Callbacks
143///////////////////////////////////////////////////////////////////////////////
144
145void TextDropShadowCache::operator()(ShadowText& text, ShadowTexture*& texture) {
Romain Guy1e45aae2010-08-13 19:39:53 -0700146 if (texture) {
Romain Guy25dc3a72010-12-10 12:33:05 -0800147 mSize -= texture->bitmapSize;
148
149 if (mDebugEnabled) {
Steve Block5baa3a62011-12-20 16:23:08 +0000150 ALOGD("Shadow texture deleted, size = %d", texture->bitmapSize);
Romain Guy25dc3a72010-12-10 12:33:05 -0800151 }
Romain Guyf70a7e32010-11-09 16:37:03 -0800152
Romain Guy1e45aae2010-08-13 19:39:53 -0700153 glDeleteTextures(1, &texture->id);
154 delete texture;
155 }
156}
157
158///////////////////////////////////////////////////////////////////////////////
159// Caching
160///////////////////////////////////////////////////////////////////////////////
161
162void TextDropShadowCache::clear() {
163 mCache.clear();
164}
165
166ShadowTexture* TextDropShadowCache::get(SkPaint* paint, const char* text, uint32_t len,
Romain Guy059e12c2012-11-28 17:35:51 -0800167 int numGlyphs, float radius, const float* positions) {
Raph Levien416a8472012-07-19 22:48:17 -0700168 ShadowText entry(paint, radius, len, text, positions);
Romain Guy1e45aae2010-08-13 19:39:53 -0700169 ShadowTexture* texture = mCache.get(entry);
170
171 if (!texture) {
Raph Levien8b4072d2012-07-30 15:50:00 -0700172 SkPaint paintCopy(*paint);
173 paintCopy.setTextAlign(SkPaint::kLeft_Align);
174 FontRenderer::DropShadow shadow = mRenderer->renderDropShadow(&paintCopy, text, 0,
Raph Levien416a8472012-07-19 22:48:17 -0700175 len, numGlyphs, radius, positions);
Romain Guy1e45aae2010-08-13 19:39:53 -0700176
177 texture = new ShadowTexture;
178 texture->left = shadow.penX;
179 texture->top = shadow.penY;
180 texture->width = shadow.width;
181 texture->height = shadow.height;
182 texture->generation = 0;
183 texture->blend = true;
184
185 const uint32_t size = shadow.width * shadow.height;
Romain Guy25dc3a72010-12-10 12:33:05 -0800186 texture->bitmapSize = size;
187
Romain Guy1e45aae2010-08-13 19:39:53 -0700188 // Don't even try to cache a bitmap that's bigger than the cache
189 if (size < mMaxSize) {
190 while (mSize + size > mMaxSize) {
191 mCache.removeOldest();
192 }
193 }
194
195 glGenTextures(1, &texture->id);
196
197 glBindTexture(GL_TEXTURE_2D, texture->id);
198 // Textures are Alpha8
199 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
200
201 glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, texture->width, texture->height, 0,
202 GL_ALPHA, GL_UNSIGNED_BYTE, shadow.image);
203
Romain Guy39d252a2011-12-12 18:14:06 -0800204 texture->setFilter(GL_LINEAR);
205 texture->setWrap(GL_CLAMP_TO_EDGE);
Romain Guy1e45aae2010-08-13 19:39:53 -0700206
207 if (size < mMaxSize) {
Romain Guy25dc3a72010-12-10 12:33:05 -0800208 if (mDebugEnabled) {
Steve Block5baa3a62011-12-20 16:23:08 +0000209 ALOGD("Shadow texture created, size = %d", texture->bitmapSize);
Romain Guy25dc3a72010-12-10 12:33:05 -0800210 }
Romain Guy321dce62011-03-01 11:45:33 -0800211
212 entry.copyTextLocally();
213
Romain Guy1e45aae2010-08-13 19:39:53 -0700214 mSize += size;
215 mCache.put(entry, texture);
216 } else {
217 texture->cleanup = true;
218 }
219
220 // Cleanup shadow
221 delete[] shadow.image;
222 }
223
224 return texture;
225}
226
227}; // namespace uirenderer
228}; // namespace android