• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 "Debug.h"
20 #include "TextDropShadowCache.h"
21 #include "Properties.h"
22 
23 namespace android {
24 namespace uirenderer {
25 
26 ///////////////////////////////////////////////////////////////////////////////
27 // Constructors/destructor
28 ///////////////////////////////////////////////////////////////////////////////
29 
TextDropShadowCache()30 TextDropShadowCache::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) {
35         INIT_LOGD("  Setting drop shadow cache size to %sMB", property);
36         setMaxSize(MB(atof(property)));
37     } else {
38         INIT_LOGD("  Using default drop shadow cache size of %.2fMB",
39                 DEFAULT_DROP_SHADOW_CACHE_SIZE);
40     }
41 
42     init();
43 }
44 
TextDropShadowCache(uint32_t maxByteSize)45 TextDropShadowCache::TextDropShadowCache(uint32_t maxByteSize):
46         mCache(GenerationCache<ShadowText, ShadowTexture*>::kUnlimitedCapacity),
47         mSize(0), mMaxSize(maxByteSize) {
48     init();
49 }
50 
~TextDropShadowCache()51 TextDropShadowCache::~TextDropShadowCache() {
52     mCache.clear();
53 }
54 
init()55 void TextDropShadowCache::init() {
56     mCache.setOnEntryRemovedListener(this);
57     mDebugEnabled = readDebugLevel() & kDebugMoreCaches;
58 }
59 
60 ///////////////////////////////////////////////////////////////////////////////
61 // Size management
62 ///////////////////////////////////////////////////////////////////////////////
63 
getSize()64 uint32_t TextDropShadowCache::getSize() {
65     return mSize;
66 }
67 
getMaxSize()68 uint32_t TextDropShadowCache::getMaxSize() {
69     return mMaxSize;
70 }
71 
setMaxSize(uint32_t maxSize)72 void TextDropShadowCache::setMaxSize(uint32_t maxSize) {
73     mMaxSize = maxSize;
74     while (mSize > mMaxSize) {
75         mCache.removeOldest();
76     }
77 }
78 
79 ///////////////////////////////////////////////////////////////////////////////
80 // Callbacks
81 ///////////////////////////////////////////////////////////////////////////////
82 
operator ()(ShadowText & text,ShadowTexture * & texture)83 void TextDropShadowCache::operator()(ShadowText& text, ShadowTexture*& texture) {
84     if (texture) {
85         mSize -= texture->bitmapSize;
86 
87         if (mDebugEnabled) {
88             ALOGD("Shadow texture deleted, size = %d", texture->bitmapSize);
89         }
90 
91         glDeleteTextures(1, &texture->id);
92         delete texture;
93     }
94 }
95 
96 ///////////////////////////////////////////////////////////////////////////////
97 // Caching
98 ///////////////////////////////////////////////////////////////////////////////
99 
clear()100 void TextDropShadowCache::clear() {
101     mCache.clear();
102 }
103 
get(SkPaint * paint,const char * text,uint32_t len,int numGlyphs,uint32_t radius,const float * positions)104 ShadowTexture* TextDropShadowCache::get(SkPaint* paint, const char* text, uint32_t len,
105         int numGlyphs, uint32_t radius, const float* positions) {
106     ShadowText entry(paint, radius, len, text, positions);
107     ShadowTexture* texture = mCache.get(entry);
108 
109     if (!texture) {
110         SkPaint paintCopy(*paint);
111         paintCopy.setTextAlign(SkPaint::kLeft_Align);
112         FontRenderer::DropShadow shadow = mRenderer->renderDropShadow(&paintCopy, text, 0,
113                 len, numGlyphs, radius, positions);
114 
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;
124         texture->bitmapSize = size;
125 
126         // 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 
142         texture->setFilter(GL_LINEAR);
143         texture->setWrap(GL_CLAMP_TO_EDGE);
144 
145         if (size < mMaxSize) {
146             if (mDebugEnabled) {
147                 ALOGD("Shadow texture created, size = %d", texture->bitmapSize);
148             }
149 
150             entry.copyTextLocally();
151 
152             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
167