Romain Guy | 1e45aae | 2010-08-13 19:39:53 -0700 | [diff] [blame] | 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 | |
Romain Guy | c9855a5 | 2011-01-21 21:14:15 -0800 | [diff] [blame] | 19 | #include "Debug.h" |
Romain Guy | 1e45aae | 2010-08-13 19:39:53 -0700 | [diff] [blame] | 20 | #include "TextDropShadowCache.h" |
Romain Guy | b45c0c9 | 2010-08-26 20:35:23 -0700 | [diff] [blame] | 21 | #include "Properties.h" |
Romain Guy | 1e45aae | 2010-08-13 19:39:53 -0700 | [diff] [blame] | 22 | |
| 23 | namespace android { |
| 24 | namespace uirenderer { |
| 25 | |
| 26 | /////////////////////////////////////////////////////////////////////////////// |
| 27 | // Constructors/destructor |
| 28 | /////////////////////////////////////////////////////////////////////////////// |
| 29 | |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 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) { |
Romain Guy | c9855a5 | 2011-01-21 21:14:15 -0800 | [diff] [blame] | 35 | INIT_LOGD(" Setting drop shadow cache size to %sMB", property); |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 36 | setMaxSize(MB(atof(property))); |
| 37 | } else { |
Romain Guy | c9855a5 | 2011-01-21 21:14:15 -0800 | [diff] [blame] | 38 | INIT_LOGD(" Using default drop shadow cache size of %.2fMB", |
| 39 | DEFAULT_DROP_SHADOW_CACHE_SIZE); |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 40 | } |
| 41 | |
Romain Guy | 25dc3a7 | 2010-12-10 12:33:05 -0800 | [diff] [blame] | 42 | init(); |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 43 | } |
| 44 | |
Romain Guy | 1e45aae | 2010-08-13 19:39:53 -0700 | [diff] [blame] | 45 | TextDropShadowCache::TextDropShadowCache(uint32_t maxByteSize): |
| 46 | mCache(GenerationCache<ShadowText, ShadowTexture*>::kUnlimitedCapacity), |
| 47 | mSize(0), mMaxSize(maxByteSize) { |
Romain Guy | 25dc3a7 | 2010-12-10 12:33:05 -0800 | [diff] [blame] | 48 | init(); |
Romain Guy | 1e45aae | 2010-08-13 19:39:53 -0700 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | TextDropShadowCache::~TextDropShadowCache() { |
| 52 | mCache.clear(); |
| 53 | } |
| 54 | |
Romain Guy | 25dc3a7 | 2010-12-10 12:33:05 -0800 | [diff] [blame] | 55 | void TextDropShadowCache::init() { |
| 56 | mCache.setOnEntryRemovedListener(this); |
| 57 | mDebugEnabled = readDebugLevel() & kDebugMoreCaches; |
| 58 | } |
| 59 | |
Romain Guy | 1e45aae | 2010-08-13 19:39:53 -0700 | [diff] [blame] | 60 | /////////////////////////////////////////////////////////////////////////////// |
| 61 | // Size management |
| 62 | /////////////////////////////////////////////////////////////////////////////// |
| 63 | |
| 64 | uint32_t TextDropShadowCache::getSize() { |
| 65 | return mSize; |
| 66 | } |
| 67 | |
| 68 | uint32_t TextDropShadowCache::getMaxSize() { |
| 69 | return mMaxSize; |
| 70 | } |
| 71 | |
| 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 | |
| 83 | void TextDropShadowCache::operator()(ShadowText& text, ShadowTexture*& texture) { |
Romain Guy | 1e45aae | 2010-08-13 19:39:53 -0700 | [diff] [blame] | 84 | if (texture) { |
Romain Guy | 25dc3a7 | 2010-12-10 12:33:05 -0800 | [diff] [blame] | 85 | mSize -= texture->bitmapSize; |
| 86 | |
| 87 | if (mDebugEnabled) { |
| 88 | LOGD("Shadow texture deleted, size = %d", texture->bitmapSize); |
| 89 | } |
Romain Guy | f70a7e3 | 2010-11-09 16:37:03 -0800 | [diff] [blame] | 90 | |
Romain Guy | 1e45aae | 2010-08-13 19:39:53 -0700 | [diff] [blame] | 91 | glDeleteTextures(1, &texture->id); |
| 92 | delete texture; |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | /////////////////////////////////////////////////////////////////////////////// |
| 97 | // Caching |
| 98 | /////////////////////////////////////////////////////////////////////////////// |
| 99 | |
| 100 | void TextDropShadowCache::clear() { |
| 101 | mCache.clear(); |
| 102 | } |
| 103 | |
| 104 | ShadowTexture* 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 Guy | 25dc3a7 | 2010-12-10 12:33:05 -0800 | [diff] [blame] | 122 | texture->bitmapSize = size; |
| 123 | |
Romain Guy | 1e45aae | 2010-08-13 19:39:53 -0700 | [diff] [blame] | 124 | // 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 Guy | 25dc3a7 | 2010-12-10 12:33:05 -0800 | [diff] [blame] | 147 | if (mDebugEnabled) { |
| 148 | LOGD("Shadow texture created, size = %d", texture->bitmapSize); |
| 149 | } |
Romain Guy | 321dce6 | 2011-03-01 11:45:33 -0800 | [diff] [blame] | 150 | |
| 151 | entry.copyTextLocally(); |
| 152 | |
Romain Guy | 1e45aae | 2010-08-13 19:39:53 -0700 | [diff] [blame] | 153 | 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 |