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