blob: 8f6f860ff14e1e957fac2f085cf1c43b0cce2a0b [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 Guyc9855a52011-01-21 21:14:15 -080019#include "Debug.h"
Romain Guy1e45aae2010-08-13 19:39:53 -070020#include "TextDropShadowCache.h"
Romain Guyb45c0c92010-08-26 20:35:23 -070021#include "Properties.h"
Romain Guy1e45aae2010-08-13 19:39:53 -070022
23namespace android {
24namespace uirenderer {
25
26///////////////////////////////////////////////////////////////////////////////
27// Constructors/destructor
28///////////////////////////////////////////////////////////////////////////////
29
Romain Guyfb8b7632010-08-23 21:05:08 -070030TextDropShadowCache::TextDropShadowCache():
31 mCache(GenerationCache<ShadowText, ShadowTexture*>::kUnlimitedCapacity),
32 mSize(0), mMaxSize(MB(DEFAULT_DROP_SHADOW_CACHE_SIZE)) {
33 char property[PROPERTY_VALUE_MAX];
34 if (property_get(PROPERTY_DROP_SHADOW_CACHE_SIZE, property, NULL) > 0) {
Romain Guyc9855a52011-01-21 21:14:15 -080035 INIT_LOGD(" Setting drop shadow cache size to %sMB", property);
Romain Guyfb8b7632010-08-23 21:05:08 -070036 setMaxSize(MB(atof(property)));
37 } else {
Romain Guyc9855a52011-01-21 21:14:15 -080038 INIT_LOGD(" Using default drop shadow cache size of %.2fMB",
39 DEFAULT_DROP_SHADOW_CACHE_SIZE);
Romain Guyfb8b7632010-08-23 21:05:08 -070040 }
41
Romain Guy25dc3a72010-12-10 12:33:05 -080042 init();
Romain Guyfb8b7632010-08-23 21:05:08 -070043}
44
Romain Guy1e45aae2010-08-13 19:39:53 -070045TextDropShadowCache::TextDropShadowCache(uint32_t maxByteSize):
46 mCache(GenerationCache<ShadowText, ShadowTexture*>::kUnlimitedCapacity),
47 mSize(0), mMaxSize(maxByteSize) {
Romain Guy25dc3a72010-12-10 12:33:05 -080048 init();
Romain Guy1e45aae2010-08-13 19:39:53 -070049}
50
51TextDropShadowCache::~TextDropShadowCache() {
52 mCache.clear();
53}
54
Romain Guy25dc3a72010-12-10 12:33:05 -080055void TextDropShadowCache::init() {
56 mCache.setOnEntryRemovedListener(this);
57 mDebugEnabled = readDebugLevel() & kDebugMoreCaches;
58}
59
Romain Guy1e45aae2010-08-13 19:39:53 -070060///////////////////////////////////////////////////////////////////////////////
61// Size management
62///////////////////////////////////////////////////////////////////////////////
63
64uint32_t TextDropShadowCache::getSize() {
65 return mSize;
66}
67
68uint32_t TextDropShadowCache::getMaxSize() {
69 return mMaxSize;
70}
71
72void TextDropShadowCache::setMaxSize(uint32_t maxSize) {
73 mMaxSize = maxSize;
74 while (mSize > mMaxSize) {
75 mCache.removeOldest();
76 }
77}
78
79///////////////////////////////////////////////////////////////////////////////
80// Callbacks
81///////////////////////////////////////////////////////////////////////////////
82
83void TextDropShadowCache::operator()(ShadowText& text, ShadowTexture*& texture) {
Romain Guy1e45aae2010-08-13 19:39:53 -070084 if (texture) {
Romain Guy25dc3a72010-12-10 12:33:05 -080085 mSize -= texture->bitmapSize;
86
87 if (mDebugEnabled) {
88 LOGD("Shadow texture deleted, size = %d", texture->bitmapSize);
89 }
Romain Guyf70a7e32010-11-09 16:37:03 -080090
Romain Guy1e45aae2010-08-13 19:39:53 -070091 glDeleteTextures(1, &texture->id);
92 delete texture;
93 }
94}
95
96///////////////////////////////////////////////////////////////////////////////
97// Caching
98///////////////////////////////////////////////////////////////////////////////
99
100void TextDropShadowCache::clear() {
101 mCache.clear();
102}
103
104ShadowTexture* TextDropShadowCache::get(SkPaint* paint, const char* text, uint32_t len,
105 int numGlyphs, uint32_t radius) {
106 ShadowText entry(paint, radius, len, text);
107 ShadowTexture* texture = mCache.get(entry);
108
109 if (!texture) {
110 FontRenderer::DropShadow shadow = mRenderer->renderDropShadow(paint, text, 0,
111 len, numGlyphs, radius);
112
113 texture = new ShadowTexture;
114 texture->left = shadow.penX;
115 texture->top = shadow.penY;
116 texture->width = shadow.width;
117 texture->height = shadow.height;
118 texture->generation = 0;
119 texture->blend = true;
120
121 const uint32_t size = shadow.width * shadow.height;
Romain Guy25dc3a72010-12-10 12:33:05 -0800122 texture->bitmapSize = size;
123
Romain Guy1e45aae2010-08-13 19:39:53 -0700124 // Don't even try to cache a bitmap that's bigger than the cache
125 if (size < mMaxSize) {
126 while (mSize + size > mMaxSize) {
127 mCache.removeOldest();
128 }
129 }
130
131 glGenTextures(1, &texture->id);
132
133 glBindTexture(GL_TEXTURE_2D, texture->id);
134 // Textures are Alpha8
135 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
136
137 glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, texture->width, texture->height, 0,
138 GL_ALPHA, GL_UNSIGNED_BYTE, shadow.image);
139
140 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
141 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
142
143 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
144 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
145
146 if (size < mMaxSize) {
Romain Guy25dc3a72010-12-10 12:33:05 -0800147 if (mDebugEnabled) {
148 LOGD("Shadow texture created, size = %d", texture->bitmapSize);
149 }
Romain Guy321dce62011-03-01 11:45:33 -0800150
151 entry.copyTextLocally();
152
Romain Guy1e45aae2010-08-13 19:39:53 -0700153 mSize += size;
154 mCache.put(entry, texture);
155 } else {
156 texture->cleanup = true;
157 }
158
159 // Cleanup shadow
160 delete[] shadow.image;
161 }
162
163 return texture;
164}
165
166}; // namespace uirenderer
167}; // namespace android