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 | |
| 19 | #include "TextDropShadowCache.h" |
| 20 | |
| 21 | namespace android { |
| 22 | namespace uirenderer { |
| 23 | |
| 24 | /////////////////////////////////////////////////////////////////////////////// |
| 25 | // Constructors/destructor |
| 26 | /////////////////////////////////////////////////////////////////////////////// |
| 27 | |
| 28 | TextDropShadowCache::TextDropShadowCache(uint32_t maxByteSize): |
| 29 | mCache(GenerationCache<ShadowText, ShadowTexture*>::kUnlimitedCapacity), |
| 30 | mSize(0), mMaxSize(maxByteSize) { |
| 31 | mCache.setOnEntryRemovedListener(this); |
| 32 | } |
| 33 | |
| 34 | TextDropShadowCache::~TextDropShadowCache() { |
| 35 | mCache.clear(); |
| 36 | } |
| 37 | |
| 38 | /////////////////////////////////////////////////////////////////////////////// |
| 39 | // Size management |
| 40 | /////////////////////////////////////////////////////////////////////////////// |
| 41 | |
| 42 | uint32_t TextDropShadowCache::getSize() { |
| 43 | return mSize; |
| 44 | } |
| 45 | |
| 46 | uint32_t TextDropShadowCache::getMaxSize() { |
| 47 | return mMaxSize; |
| 48 | } |
| 49 | |
| 50 | void TextDropShadowCache::setMaxSize(uint32_t maxSize) { |
| 51 | mMaxSize = maxSize; |
| 52 | while (mSize > mMaxSize) { |
| 53 | mCache.removeOldest(); |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | /////////////////////////////////////////////////////////////////////////////// |
| 58 | // Callbacks |
| 59 | /////////////////////////////////////////////////////////////////////////////// |
| 60 | |
| 61 | void TextDropShadowCache::operator()(ShadowText& text, ShadowTexture*& texture) { |
| 62 | const uint32_t size = texture->width * texture->height; |
| 63 | mSize -= size; |
| 64 | |
| 65 | if (texture) { |
| 66 | glDeleteTextures(1, &texture->id); |
| 67 | delete texture; |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | /////////////////////////////////////////////////////////////////////////////// |
| 72 | // Caching |
| 73 | /////////////////////////////////////////////////////////////////////////////// |
| 74 | |
| 75 | void TextDropShadowCache::clear() { |
| 76 | mCache.clear(); |
| 77 | } |
| 78 | |
| 79 | ShadowTexture* TextDropShadowCache::get(SkPaint* paint, const char* text, uint32_t len, |
| 80 | int numGlyphs, uint32_t radius) { |
| 81 | ShadowText entry(paint, radius, len, text); |
| 82 | ShadowTexture* texture = mCache.get(entry); |
| 83 | |
| 84 | if (!texture) { |
| 85 | FontRenderer::DropShadow shadow = mRenderer->renderDropShadow(paint, text, 0, |
| 86 | len, numGlyphs, radius); |
| 87 | |
| 88 | texture = new ShadowTexture; |
| 89 | texture->left = shadow.penX; |
| 90 | texture->top = shadow.penY; |
| 91 | texture->width = shadow.width; |
| 92 | texture->height = shadow.height; |
| 93 | texture->generation = 0; |
| 94 | texture->blend = true; |
| 95 | |
| 96 | const uint32_t size = shadow.width * shadow.height; |
| 97 | // Don't even try to cache a bitmap that's bigger than the cache |
| 98 | if (size < mMaxSize) { |
| 99 | while (mSize + size > mMaxSize) { |
| 100 | mCache.removeOldest(); |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | glGenTextures(1, &texture->id); |
| 105 | |
| 106 | glBindTexture(GL_TEXTURE_2D, texture->id); |
| 107 | // Textures are Alpha8 |
| 108 | glPixelStorei(GL_UNPACK_ALIGNMENT, 1); |
| 109 | |
| 110 | glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, texture->width, texture->height, 0, |
| 111 | GL_ALPHA, GL_UNSIGNED_BYTE, shadow.image); |
| 112 | |
| 113 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
| 114 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
| 115 | |
| 116 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); |
| 117 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); |
| 118 | |
| 119 | if (size < mMaxSize) { |
| 120 | mSize += size; |
| 121 | mCache.put(entry, texture); |
| 122 | } else { |
| 123 | texture->cleanup = true; |
| 124 | } |
| 125 | |
| 126 | // Cleanup shadow |
| 127 | delete[] shadow.image; |
| 128 | } |
| 129 | |
| 130 | return texture; |
| 131 | } |
| 132 | |
| 133 | }; // namespace uirenderer |
| 134 | }; // namespace android |