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