blob: c521892c69dfe812cc3aafce8e9969452ebeae4a [file] [log] [blame]
Romain Guy1e45aae2010-08-13 19:39:53 -07001/*
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 Guy059e12c2012-11-28 17:35:51 -080017#include <utils/JenkinsHash.h>
18
Romain Guy8aa195d2013-06-04 18:00:09 -070019#include "Caches.h"
Romain Guyc9855a52011-01-21 21:14:15 -080020#include "Debug.h"
Tom Hudson2dc236b2014-10-15 15:46:42 -040021#include "FontRenderer.h"
Romain Guy1e45aae2010-08-13 19:39:53 -070022#include "TextDropShadowCache.h"
Romain Guyb45c0c92010-08-26 20:35:23 -070023#include "Properties.h"
Romain Guy1e45aae2010-08-13 19:39:53 -070024
25namespace android {
26namespace uirenderer {
27
28///////////////////////////////////////////////////////////////////////////////
Romain Guy059e12c2012-11-28 17:35:51 -080029// Cache support
30///////////////////////////////////////////////////////////////////////////////
31
32hash_t ShadowText::hash() const {
Chris Craika1717272015-11-19 13:02:43 -080033 uint32_t hash = JenkinsHashMix(0, glyphCount);
Romain Guy059e12c2012-11-28 17:35:51 -080034 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 Craike8c3c812016-02-05 20:10:50 -080040 if (glyphs) {
Dan Albert66987492014-11-20 11:41:21 -080041 hash = JenkinsHashMixShorts(
Chris Craike8c3c812016-02-05 20:10:50 -080042 hash, reinterpret_cast<const uint16_t*>(glyphs), glyphCount);
Romain Guy69fcbcc2012-11-30 15:29:15 -080043 }
44 if (positions) {
Chris Craika1717272015-11-19 13:02:43 -080045 for (uint32_t i = 0; i < glyphCount * 2; i++) {
Romain Guy69fcbcc2012-11-30 15:29:15 -080046 hash = JenkinsHashMix(hash, android::hash_type(positions[i]));
47 }
Romain Guy059e12c2012-11-28 17:35:51 -080048 }
49 return JenkinsHashWhiten(hash);
50}
51
52int ShadowText::compare(const ShadowText& lhs, const ShadowText& rhs) {
Chris Craika1717272015-11-19 13:02:43 -080053 int deltaInt = int(lhs.glyphCount) - int(rhs.glyphCount);
Romain Guy059e12c2012-11-28 17:35:51 -080054 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
Chris Craike8c3c812016-02-05 20:10:50 -080074 if (lhs.glyphs != rhs.glyphs) {
75 if (!lhs.glyphs) return -1;
76 if (!rhs.glyphs) return +1;
Romain Guy059e12c2012-11-28 17:35:51 -080077
Chris Craike8c3c812016-02-05 20:10:50 -080078 deltaInt = memcmp(lhs.glyphs, rhs.glyphs, lhs.glyphCount * sizeof(glyph_t));
Romain Guy059e12c2012-11-28 17:35:51 -080079 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 Craik08684772015-12-04 09:16:58 -080086 return memcmp(lhs.positions, rhs.positions, lhs.glyphCount * sizeof(float) * 2);
Romain Guy059e12c2012-11-28 17:35:51 -080087 }
88
89 return 0;
90}
91
92///////////////////////////////////////////////////////////////////////////////
Romain Guy1e45aae2010-08-13 19:39:53 -070093// Constructors/destructor
94///////////////////////////////////////////////////////////////////////////////
95
Chris Craik48a8f432016-02-05 15:59:29 -080096TextDropShadowCache::TextDropShadowCache()
John Reck8dc02f92017-07-17 09:55:02 -070097 : TextDropShadowCache(DeviceInfo::multiplyByResolution(2)) {}
Romain Guyfb8b7632010-08-23 21:05:08 -070098
Chris Craik48a8f432016-02-05 15:59:29 -080099TextDropShadowCache::TextDropShadowCache(uint32_t maxByteSize)
100 : mCache(LruCache<ShadowText, ShadowTexture*>::kUnlimitedCapacity)
101 , mSize(0)
102 , mMaxSize(maxByteSize) {
103 mCache.setOnEntryRemovedListener(this);
104 mDebugEnabled = Properties::debugLevel & kDebugMoreCaches;
Romain Guy1e45aae2010-08-13 19:39:53 -0700105}
106
107TextDropShadowCache::~TextDropShadowCache() {
108 mCache.clear();
109}
110
111///////////////////////////////////////////////////////////////////////////////
112// Size management
113///////////////////////////////////////////////////////////////////////////////
114
115uint32_t TextDropShadowCache::getSize() {
116 return mSize;
117}
118
119uint32_t TextDropShadowCache::getMaxSize() {
120 return mMaxSize;
121}
122
Romain Guy1e45aae2010-08-13 19:39:53 -0700123///////////////////////////////////////////////////////////////////////////////
124// Callbacks
125///////////////////////////////////////////////////////////////////////////////
126
Chris Craike63f7c622013-10-17 10:30:55 -0700127void TextDropShadowCache::operator()(ShadowText&, ShadowTexture*& texture) {
Romain Guy1e45aae2010-08-13 19:39:53 -0700128 if (texture) {
John Reckc3127a72016-01-29 15:54:10 -0800129 mSize -= texture->objectSize();
Romain Guy25dc3a72010-12-10 12:33:05 -0800130
131 if (mDebugEnabled) {
Steve Block5baa3a62011-12-20 16:23:08 +0000132 ALOGD("Shadow texture deleted, size = %d", texture->bitmapSize);
Romain Guy25dc3a72010-12-10 12:33:05 -0800133 }
Romain Guyf70a7e32010-11-09 16:37:03 -0800134
Romain Guybe1b1272013-06-06 14:02:54 -0700135 texture->deleteTexture();
Romain Guy1e45aae2010-08-13 19:39:53 -0700136 delete texture;
137 }
138}
139
140///////////////////////////////////////////////////////////////////////////////
141// Caching
142///////////////////////////////////////////////////////////////////////////////
143
144void TextDropShadowCache::clear() {
145 mCache.clear();
146}
147
Chris Craike8c3c812016-02-05 20:10:50 -0800148ShadowTexture* TextDropShadowCache::get(const SkPaint* paint, const glyph_t* glyphs, int numGlyphs,
Chris Craika1717272015-11-19 13:02:43 -0800149 float radius, const float* positions) {
Chris Craik08684772015-12-04 09:16:58 -0800150 ShadowText entry(paint, radius, numGlyphs, glyphs, positions);
Romain Guy1e45aae2010-08-13 19:39:53 -0700151 ShadowTexture* texture = mCache.get(entry);
152
153 if (!texture) {
Raph Levien8b4072d2012-07-30 15:50:00 -0700154 SkPaint paintCopy(*paint);
155 paintCopy.setTextAlign(SkPaint::kLeft_Align);
Chris Craika1717272015-11-19 13:02:43 -0800156 FontRenderer::DropShadow shadow = mRenderer->renderDropShadow(&paintCopy, glyphs, numGlyphs,
157 radius, positions);
Romain Guy1e45aae2010-08-13 19:39:53 -0700158
Romain Guycf51a412013-04-08 19:40:31 -0700159 if (!shadow.image) {
Chris Craikd41c4d82015-01-05 15:51:13 -0800160 return nullptr;
Romain Guycf51a412013-04-08 19:40:31 -0700161 }
162
Romain Guy8aa195d2013-06-04 18:00:09 -0700163 Caches& caches = Caches::getInstance();
164
165 texture = new ShadowTexture(caches);
Romain Guy1e45aae2010-08-13 19:39:53 -0700166 texture->left = shadow.penX;
167 texture->top = shadow.penY;
Romain Guy1e45aae2010-08-13 19:39:53 -0700168 texture->generation = 0;
169 texture->blend = true;
170
171 const uint32_t size = shadow.width * shadow.height;
Romain Guy25dc3a72010-12-10 12:33:05 -0800172
Romain Guy1e45aae2010-08-13 19:39:53 -0700173 // Don't even try to cache a bitmap that's bigger than the cache
174 if (size < mMaxSize) {
175 while (mSize + size > mMaxSize) {
John Reckc3127a72016-01-29 15:54:10 -0800176 LOG_ALWAYS_FATAL_IF(!mCache.removeOldest(),
177 "Failed to remove oldest from cache. mSize = %"
178 PRIu32 ", mCache.size() = %zu", mSize, mCache.size());
Romain Guy1e45aae2010-08-13 19:39:53 -0700179 }
180 }
181
Romain Guy1e45aae2010-08-13 19:39:53 -0700182 // Textures are Alpha8
John Reck38e0c322015-11-10 12:19:17 -0800183 texture->upload(GL_ALPHA, shadow.width, shadow.height,
Romain Guy1e45aae2010-08-13 19:39:53 -0700184 GL_ALPHA, GL_UNSIGNED_BYTE, shadow.image);
Romain Guy39d252a2011-12-12 18:14:06 -0800185 texture->setFilter(GL_LINEAR);
186 texture->setWrap(GL_CLAMP_TO_EDGE);
Romain Guy1e45aae2010-08-13 19:39:53 -0700187
188 if (size < mMaxSize) {
Romain Guy25dc3a72010-12-10 12:33:05 -0800189 if (mDebugEnabled) {
Steve Block5baa3a62011-12-20 16:23:08 +0000190 ALOGD("Shadow texture created, size = %d", texture->bitmapSize);
Romain Guy25dc3a72010-12-10 12:33:05 -0800191 }
Romain Guy321dce62011-03-01 11:45:33 -0800192
193 entry.copyTextLocally();
194
John Reckc3127a72016-01-29 15:54:10 -0800195 mSize += texture->objectSize();
Romain Guy1e45aae2010-08-13 19:39:53 -0700196 mCache.put(entry, texture);
197 } else {
198 texture->cleanup = true;
199 }
200
201 // Cleanup shadow
Ben Cheng15641a62013-02-20 13:20:03 -0800202 free(shadow.image);
Romain Guy1e45aae2010-08-13 19:39:53 -0700203 }
204
205 return texture;
206}
207
208}; // namespace uirenderer
209}; // namespace android