blob: d96a7f516022849e00170a5b0f2ac4645f23bf32 [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
19#include "TextDropShadowCache.h"
Romain Guyb45c0c92010-08-26 20:35:23 -070020#include "Properties.h"
Romain Guy1e45aae2010-08-13 19:39:53 -070021
22namespace android {
23namespace uirenderer {
24
25///////////////////////////////////////////////////////////////////////////////
26// Constructors/destructor
27///////////////////////////////////////////////////////////////////////////////
28
Romain Guyfb8b7632010-08-23 21:05:08 -070029TextDropShadowCache::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
Romain Guy25dc3a72010-12-10 12:33:05 -080040 init();
Romain Guyfb8b7632010-08-23 21:05:08 -070041}
42
Romain Guy1e45aae2010-08-13 19:39:53 -070043TextDropShadowCache::TextDropShadowCache(uint32_t maxByteSize):
44 mCache(GenerationCache<ShadowText, ShadowTexture*>::kUnlimitedCapacity),
45 mSize(0), mMaxSize(maxByteSize) {
Romain Guy25dc3a72010-12-10 12:33:05 -080046 init();
Romain Guy1e45aae2010-08-13 19:39:53 -070047}
48
49TextDropShadowCache::~TextDropShadowCache() {
50 mCache.clear();
51}
52
Romain Guy25dc3a72010-12-10 12:33:05 -080053void TextDropShadowCache::init() {
54 mCache.setOnEntryRemovedListener(this);
55 mDebugEnabled = readDebugLevel() & kDebugMoreCaches;
56}
57
Romain Guy1e45aae2010-08-13 19:39:53 -070058///////////////////////////////////////////////////////////////////////////////
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) {
Romain Guy1e45aae2010-08-13 19:39:53 -070082 if (texture) {
Romain Guy25dc3a72010-12-10 12:33:05 -080083 mSize -= texture->bitmapSize;
84
85 if (mDebugEnabled) {
86 LOGD("Shadow texture deleted, size = %d", texture->bitmapSize);
87 }
Romain Guyf70a7e32010-11-09 16:37:03 -080088
Romain Guy1e45aae2010-08-13 19:39:53 -070089 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;
Romain Guy25dc3a72010-12-10 12:33:05 -0800120 texture->bitmapSize = size;
121
Romain Guy1e45aae2010-08-13 19:39:53 -0700122 // 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) {
Romain Guy25dc3a72010-12-10 12:33:05 -0800145 if (mDebugEnabled) {
146 LOGD("Shadow texture created, size = %d", texture->bitmapSize);
147 }
Romain Guy1e45aae2010-08-13 19:39:53 -0700148 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