blob: 8426f5869189840fe3181414a57db636c24d35cc [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) {
Steve Block5baa3a62011-12-20 16:23:08 +000088 ALOGD("Shadow texture deleted, size = %d", texture->bitmapSize);
Romain Guy25dc3a72010-12-10 12:33:05 -080089 }
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,
Raph Levien416a8472012-07-19 22:48:17 -0700105 int numGlyphs, uint32_t radius, const float* positions) {
106 ShadowText entry(paint, radius, len, text, positions);
Romain Guy1e45aae2010-08-13 19:39:53 -0700107 ShadowTexture* texture = mCache.get(entry);
108
109 if (!texture) {
Raph Levien8b4072d2012-07-30 15:50:00 -0700110 SkPaint paintCopy(*paint);
111 paintCopy.setTextAlign(SkPaint::kLeft_Align);
112 FontRenderer::DropShadow shadow = mRenderer->renderDropShadow(&paintCopy, text, 0,
Raph Levien416a8472012-07-19 22:48:17 -0700113 len, numGlyphs, radius, positions);
Romain Guy1e45aae2010-08-13 19:39:53 -0700114
115 texture = new ShadowTexture;
116 texture->left = shadow.penX;
117 texture->top = shadow.penY;
118 texture->width = shadow.width;
119 texture->height = shadow.height;
120 texture->generation = 0;
121 texture->blend = true;
122
123 const uint32_t size = shadow.width * shadow.height;
Romain Guy25dc3a72010-12-10 12:33:05 -0800124 texture->bitmapSize = size;
125
Romain Guy1e45aae2010-08-13 19:39:53 -0700126 // Don't even try to cache a bitmap that's bigger than the cache
127 if (size < mMaxSize) {
128 while (mSize + size > mMaxSize) {
129 mCache.removeOldest();
130 }
131 }
132
133 glGenTextures(1, &texture->id);
134
135 glBindTexture(GL_TEXTURE_2D, texture->id);
136 // Textures are Alpha8
137 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
138
139 glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, texture->width, texture->height, 0,
140 GL_ALPHA, GL_UNSIGNED_BYTE, shadow.image);
141
Romain Guy39d252a2011-12-12 18:14:06 -0800142 texture->setFilter(GL_LINEAR);
143 texture->setWrap(GL_CLAMP_TO_EDGE);
Romain Guy1e45aae2010-08-13 19:39:53 -0700144
145 if (size < mMaxSize) {
Romain Guy25dc3a72010-12-10 12:33:05 -0800146 if (mDebugEnabled) {
Steve Block5baa3a62011-12-20 16:23:08 +0000147 ALOGD("Shadow texture created, size = %d", texture->bitmapSize);
Romain Guy25dc3a72010-12-10 12:33:05 -0800148 }
Romain Guy321dce62011-03-01 11:45:33 -0800149
150 entry.copyTextLocally();
151
Romain Guy1e45aae2010-08-13 19:39:53 -0700152 mSize += size;
153 mCache.put(entry, texture);
154 } else {
155 texture->cleanup = true;
156 }
157
158 // Cleanup shadow
159 delete[] shadow.image;
160 }
161
162 return texture;
163}
164
165}; // namespace uirenderer
166}; // namespace android