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 | 059e12c | 2012-11-28 17:35:51 -0800 | [diff] [blame] | 19 | #include <utils/JenkinsHash.h> |
| 20 | |
Romain Guy | 8aa195d | 2013-06-04 18:00:09 -0700 | [diff] [blame] | 21 | #include "Caches.h" |
Romain Guy | c9855a5 | 2011-01-21 21:14:15 -0800 | [diff] [blame] | 22 | #include "Debug.h" |
Tom Hudson | 2dc236b | 2014-10-15 15:46:42 -0400 | [diff] [blame] | 23 | #include "FontRenderer.h" |
Romain Guy | 1e45aae | 2010-08-13 19:39:53 -0700 | [diff] [blame] | 24 | #include "TextDropShadowCache.h" |
Romain Guy | b45c0c9 | 2010-08-26 20:35:23 -0700 | [diff] [blame] | 25 | #include "Properties.h" |
Romain Guy | 1e45aae | 2010-08-13 19:39:53 -0700 | [diff] [blame] | 26 | |
| 27 | namespace android { |
| 28 | namespace uirenderer { |
| 29 | |
| 30 | /////////////////////////////////////////////////////////////////////////////// |
Romain Guy | 059e12c | 2012-11-28 17:35:51 -0800 | [diff] [blame] | 31 | // Cache support |
| 32 | /////////////////////////////////////////////////////////////////////////////// |
| 33 | |
| 34 | hash_t ShadowText::hash() const { |
Romain Guy | 69fcbcc | 2012-11-30 15:29:15 -0800 | [diff] [blame] | 35 | uint32_t charCount = len / sizeof(char16_t); |
Romain Guy | 059e12c | 2012-11-28 17:35:51 -0800 | [diff] [blame] | 36 | uint32_t hash = JenkinsHashMix(0, len); |
| 37 | hash = JenkinsHashMix(hash, android::hash_type(radius)); |
| 38 | hash = JenkinsHashMix(hash, android::hash_type(textSize)); |
| 39 | hash = JenkinsHashMix(hash, android::hash_type(typeface)); |
| 40 | hash = JenkinsHashMix(hash, flags); |
| 41 | hash = JenkinsHashMix(hash, android::hash_type(italicStyle)); |
| 42 | hash = JenkinsHashMix(hash, android::hash_type(scaleX)); |
Romain Guy | 69fcbcc | 2012-11-30 15:29:15 -0800 | [diff] [blame] | 43 | if (text) { |
Dan Albert | 6698749 | 2014-11-20 11:41:21 -0800 | [diff] [blame] | 44 | hash = JenkinsHashMixShorts( |
| 45 | hash, reinterpret_cast<const uint16_t*>(text), charCount); |
Romain Guy | 69fcbcc | 2012-11-30 15:29:15 -0800 | [diff] [blame] | 46 | } |
| 47 | if (positions) { |
| 48 | for (uint32_t i = 0; i < charCount * 2; i++) { |
| 49 | hash = JenkinsHashMix(hash, android::hash_type(positions[i])); |
| 50 | } |
Romain Guy | 059e12c | 2012-11-28 17:35:51 -0800 | [diff] [blame] | 51 | } |
| 52 | return JenkinsHashWhiten(hash); |
| 53 | } |
| 54 | |
| 55 | int ShadowText::compare(const ShadowText& lhs, const ShadowText& rhs) { |
| 56 | int deltaInt = int(lhs.len) - int(rhs.len); |
| 57 | if (deltaInt != 0) return deltaInt; |
| 58 | |
| 59 | deltaInt = lhs.flags - rhs.flags; |
| 60 | if (deltaInt != 0) return deltaInt; |
| 61 | |
| 62 | if (lhs.radius < rhs.radius) return -1; |
| 63 | if (lhs.radius > rhs.radius) return +1; |
| 64 | |
| 65 | if (lhs.typeface < rhs.typeface) return -1; |
| 66 | if (lhs.typeface > rhs.typeface) return +1; |
| 67 | |
| 68 | if (lhs.textSize < rhs.textSize) return -1; |
| 69 | if (lhs.textSize > rhs.textSize) return +1; |
| 70 | |
| 71 | if (lhs.italicStyle < rhs.italicStyle) return -1; |
| 72 | if (lhs.italicStyle > rhs.italicStyle) return +1; |
| 73 | |
| 74 | if (lhs.scaleX < rhs.scaleX) return -1; |
| 75 | if (lhs.scaleX > rhs.scaleX) return +1; |
| 76 | |
| 77 | if (lhs.text != rhs.text) { |
| 78 | if (!lhs.text) return -1; |
| 79 | if (!rhs.text) return +1; |
| 80 | |
| 81 | deltaInt = memcmp(lhs.text, rhs.text, lhs.len); |
| 82 | if (deltaInt != 0) return deltaInt; |
| 83 | } |
| 84 | |
| 85 | if (lhs.positions != rhs.positions) { |
| 86 | if (!lhs.positions) return -1; |
| 87 | if (!rhs.positions) return +1; |
| 88 | |
| 89 | return memcmp(lhs.positions, rhs.positions, lhs.len << 2); |
| 90 | } |
| 91 | |
| 92 | return 0; |
| 93 | } |
| 94 | |
| 95 | /////////////////////////////////////////////////////////////////////////////// |
Romain Guy | 1e45aae | 2010-08-13 19:39:53 -0700 | [diff] [blame] | 96 | // Constructors/destructor |
| 97 | /////////////////////////////////////////////////////////////////////////////// |
| 98 | |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 99 | TextDropShadowCache::TextDropShadowCache(): |
Romain Guy | 059e12c | 2012-11-28 17:35:51 -0800 | [diff] [blame] | 100 | mCache(LruCache<ShadowText, ShadowTexture*>::kUnlimitedCapacity), |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 101 | mSize(0), mMaxSize(MB(DEFAULT_DROP_SHADOW_CACHE_SIZE)) { |
| 102 | char property[PROPERTY_VALUE_MAX]; |
Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 103 | if (property_get(PROPERTY_DROP_SHADOW_CACHE_SIZE, property, nullptr) > 0) { |
Romain Guy | c9855a5 | 2011-01-21 21:14:15 -0800 | [diff] [blame] | 104 | INIT_LOGD(" Setting drop shadow cache size to %sMB", property); |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 105 | setMaxSize(MB(atof(property))); |
| 106 | } else { |
Romain Guy | c9855a5 | 2011-01-21 21:14:15 -0800 | [diff] [blame] | 107 | INIT_LOGD(" Using default drop shadow cache size of %.2fMB", |
| 108 | DEFAULT_DROP_SHADOW_CACHE_SIZE); |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 109 | } |
| 110 | |
Romain Guy | 25dc3a7 | 2010-12-10 12:33:05 -0800 | [diff] [blame] | 111 | init(); |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 112 | } |
| 113 | |
Romain Guy | 1e45aae | 2010-08-13 19:39:53 -0700 | [diff] [blame] | 114 | TextDropShadowCache::TextDropShadowCache(uint32_t maxByteSize): |
Romain Guy | 059e12c | 2012-11-28 17:35:51 -0800 | [diff] [blame] | 115 | mCache(LruCache<ShadowText, ShadowTexture*>::kUnlimitedCapacity), |
Romain Guy | 1e45aae | 2010-08-13 19:39:53 -0700 | [diff] [blame] | 116 | mSize(0), mMaxSize(maxByteSize) { |
Romain Guy | 25dc3a7 | 2010-12-10 12:33:05 -0800 | [diff] [blame] | 117 | init(); |
Romain Guy | 1e45aae | 2010-08-13 19:39:53 -0700 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | TextDropShadowCache::~TextDropShadowCache() { |
| 121 | mCache.clear(); |
| 122 | } |
| 123 | |
Romain Guy | 25dc3a7 | 2010-12-10 12:33:05 -0800 | [diff] [blame] | 124 | void TextDropShadowCache::init() { |
| 125 | mCache.setOnEntryRemovedListener(this); |
| 126 | mDebugEnabled = readDebugLevel() & kDebugMoreCaches; |
| 127 | } |
| 128 | |
Romain Guy | 1e45aae | 2010-08-13 19:39:53 -0700 | [diff] [blame] | 129 | /////////////////////////////////////////////////////////////////////////////// |
| 130 | // Size management |
| 131 | /////////////////////////////////////////////////////////////////////////////// |
| 132 | |
| 133 | uint32_t TextDropShadowCache::getSize() { |
| 134 | return mSize; |
| 135 | } |
| 136 | |
| 137 | uint32_t TextDropShadowCache::getMaxSize() { |
| 138 | return mMaxSize; |
| 139 | } |
| 140 | |
| 141 | void TextDropShadowCache::setMaxSize(uint32_t maxSize) { |
| 142 | mMaxSize = maxSize; |
| 143 | while (mSize > mMaxSize) { |
| 144 | mCache.removeOldest(); |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | /////////////////////////////////////////////////////////////////////////////// |
| 149 | // Callbacks |
| 150 | /////////////////////////////////////////////////////////////////////////////// |
| 151 | |
Chris Craik | e63f7c62 | 2013-10-17 10:30:55 -0700 | [diff] [blame] | 152 | void TextDropShadowCache::operator()(ShadowText&, ShadowTexture*& texture) { |
Romain Guy | 1e45aae | 2010-08-13 19:39:53 -0700 | [diff] [blame] | 153 | if (texture) { |
Romain Guy | 25dc3a7 | 2010-12-10 12:33:05 -0800 | [diff] [blame] | 154 | mSize -= texture->bitmapSize; |
| 155 | |
| 156 | if (mDebugEnabled) { |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 157 | ALOGD("Shadow texture deleted, size = %d", texture->bitmapSize); |
Romain Guy | 25dc3a7 | 2010-12-10 12:33:05 -0800 | [diff] [blame] | 158 | } |
Romain Guy | f70a7e3 | 2010-11-09 16:37:03 -0800 | [diff] [blame] | 159 | |
Romain Guy | be1b127 | 2013-06-06 14:02:54 -0700 | [diff] [blame] | 160 | texture->deleteTexture(); |
Romain Guy | 1e45aae | 2010-08-13 19:39:53 -0700 | [diff] [blame] | 161 | delete texture; |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | /////////////////////////////////////////////////////////////////////////////// |
| 166 | // Caching |
| 167 | /////////////////////////////////////////////////////////////////////////////// |
| 168 | |
| 169 | void TextDropShadowCache::clear() { |
| 170 | mCache.clear(); |
| 171 | } |
| 172 | |
Chris Craik | d218a92 | 2014-01-02 17:13:34 -0800 | [diff] [blame] | 173 | ShadowTexture* TextDropShadowCache::get(const SkPaint* paint, const char* text, uint32_t len, |
Romain Guy | 059e12c | 2012-11-28 17:35:51 -0800 | [diff] [blame] | 174 | int numGlyphs, float radius, const float* positions) { |
Raph Levien | 416a847 | 2012-07-19 22:48:17 -0700 | [diff] [blame] | 175 | ShadowText entry(paint, radius, len, text, positions); |
Romain Guy | 1e45aae | 2010-08-13 19:39:53 -0700 | [diff] [blame] | 176 | ShadowTexture* texture = mCache.get(entry); |
| 177 | |
| 178 | if (!texture) { |
Raph Levien | 8b4072d | 2012-07-30 15:50:00 -0700 | [diff] [blame] | 179 | SkPaint paintCopy(*paint); |
| 180 | paintCopy.setTextAlign(SkPaint::kLeft_Align); |
| 181 | FontRenderer::DropShadow shadow = mRenderer->renderDropShadow(&paintCopy, text, 0, |
Raph Levien | 416a847 | 2012-07-19 22:48:17 -0700 | [diff] [blame] | 182 | len, numGlyphs, radius, positions); |
Romain Guy | 1e45aae | 2010-08-13 19:39:53 -0700 | [diff] [blame] | 183 | |
Romain Guy | cf51a41 | 2013-04-08 19:40:31 -0700 | [diff] [blame] | 184 | if (!shadow.image) { |
Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 185 | return nullptr; |
Romain Guy | cf51a41 | 2013-04-08 19:40:31 -0700 | [diff] [blame] | 186 | } |
| 187 | |
Romain Guy | 8aa195d | 2013-06-04 18:00:09 -0700 | [diff] [blame] | 188 | Caches& caches = Caches::getInstance(); |
| 189 | |
| 190 | texture = new ShadowTexture(caches); |
Romain Guy | 1e45aae | 2010-08-13 19:39:53 -0700 | [diff] [blame] | 191 | texture->left = shadow.penX; |
| 192 | texture->top = shadow.penY; |
| 193 | texture->width = shadow.width; |
| 194 | texture->height = shadow.height; |
| 195 | texture->generation = 0; |
| 196 | texture->blend = true; |
| 197 | |
| 198 | const uint32_t size = shadow.width * shadow.height; |
Romain Guy | 25dc3a7 | 2010-12-10 12:33:05 -0800 | [diff] [blame] | 199 | texture->bitmapSize = size; |
| 200 | |
Romain Guy | 1e45aae | 2010-08-13 19:39:53 -0700 | [diff] [blame] | 201 | // Don't even try to cache a bitmap that's bigger than the cache |
| 202 | if (size < mMaxSize) { |
| 203 | while (mSize + size > mMaxSize) { |
| 204 | mCache.removeOldest(); |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | glGenTextures(1, &texture->id); |
| 209 | |
Chris Craik | 44eb2c0 | 2015-01-29 09:45:09 -0800 | [diff] [blame] | 210 | caches.textureState().bindTexture(texture->id); |
Romain Guy | 1e45aae | 2010-08-13 19:39:53 -0700 | [diff] [blame] | 211 | // Textures are Alpha8 |
| 212 | glPixelStorei(GL_UNPACK_ALIGNMENT, 1); |
| 213 | |
| 214 | glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, texture->width, texture->height, 0, |
| 215 | GL_ALPHA, GL_UNSIGNED_BYTE, shadow.image); |
| 216 | |
Romain Guy | 39d252a | 2011-12-12 18:14:06 -0800 | [diff] [blame] | 217 | texture->setFilter(GL_LINEAR); |
| 218 | texture->setWrap(GL_CLAMP_TO_EDGE); |
Romain Guy | 1e45aae | 2010-08-13 19:39:53 -0700 | [diff] [blame] | 219 | |
| 220 | if (size < mMaxSize) { |
Romain Guy | 25dc3a7 | 2010-12-10 12:33:05 -0800 | [diff] [blame] | 221 | if (mDebugEnabled) { |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 222 | ALOGD("Shadow texture created, size = %d", texture->bitmapSize); |
Romain Guy | 25dc3a7 | 2010-12-10 12:33:05 -0800 | [diff] [blame] | 223 | } |
Romain Guy | 321dce6 | 2011-03-01 11:45:33 -0800 | [diff] [blame] | 224 | |
| 225 | entry.copyTextLocally(); |
| 226 | |
Romain Guy | 1e45aae | 2010-08-13 19:39:53 -0700 | [diff] [blame] | 227 | mSize += size; |
| 228 | mCache.put(entry, texture); |
| 229 | } else { |
| 230 | texture->cleanup = true; |
| 231 | } |
| 232 | |
| 233 | // Cleanup shadow |
Ben Cheng | 15641a6 | 2013-02-20 13:20:03 -0800 | [diff] [blame] | 234 | free(shadow.image); |
Romain Guy | 1e45aae | 2010-08-13 19:39:53 -0700 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | return texture; |
| 238 | } |
| 239 | |
| 240 | }; // namespace uirenderer |
| 241 | }; // namespace android |