2 * Copyright (C) 2010 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include <utils/JenkinsHash.h>
21 #include "FontRenderer.h"
22 #include "TextDropShadowCache.h"
23 #include "Properties.h"
26 namespace uirenderer {
28 ///////////////////////////////////////////////////////////////////////////////
30 ///////////////////////////////////////////////////////////////////////////////
32 hash_t ShadowText::hash() const {
33 uint32_t hash = JenkinsHashMix(0, glyphCount);
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));
41 hash = JenkinsHashMixShorts(
42 hash, reinterpret_cast<const uint16_t*>(glyphs), glyphCount);
45 for (uint32_t i = 0; i < glyphCount * 2; i++) {
46 hash = JenkinsHashMix(hash, android::hash_type(positions[i]));
49 return JenkinsHashWhiten(hash);
52 int ShadowText::compare(const ShadowText& lhs, const ShadowText& rhs) {
53 int deltaInt = int(lhs.glyphCount) - int(rhs.glyphCount);
54 if (deltaInt != 0) return deltaInt;
56 deltaInt = lhs.flags - rhs.flags;
57 if (deltaInt != 0) return deltaInt;
59 if (lhs.radius < rhs.radius) return -1;
60 if (lhs.radius > rhs.radius) return +1;
62 if (lhs.typeface < rhs.typeface) return -1;
63 if (lhs.typeface > rhs.typeface) return +1;
65 if (lhs.textSize < rhs.textSize) return -1;
66 if (lhs.textSize > rhs.textSize) return +1;
68 if (lhs.italicStyle < rhs.italicStyle) return -1;
69 if (lhs.italicStyle > rhs.italicStyle) return +1;
71 if (lhs.scaleX < rhs.scaleX) return -1;
72 if (lhs.scaleX > rhs.scaleX) return +1;
74 if (lhs.glyphs != rhs.glyphs) {
75 if (!lhs.glyphs) return -1;
76 if (!rhs.glyphs) return +1;
78 deltaInt = memcmp(lhs.glyphs, rhs.glyphs, lhs.glyphCount * sizeof(glyph_t));
79 if (deltaInt != 0) return deltaInt;
82 if (lhs.positions != rhs.positions) {
83 if (!lhs.positions) return -1;
84 if (!rhs.positions) return +1;
86 return memcmp(lhs.positions, rhs.positions, lhs.glyphCount * sizeof(float) * 2);
92 ///////////////////////////////////////////////////////////////////////////////
93 // Constructors/destructor
94 ///////////////////////////////////////////////////////////////////////////////
96 TextDropShadowCache::TextDropShadowCache()
97 : TextDropShadowCache(Properties::textDropShadowCacheSize) {}
99 TextDropShadowCache::TextDropShadowCache(uint32_t maxByteSize)
100 : mCache(LruCache<ShadowText, ShadowTexture*>::kUnlimitedCapacity)
102 , mMaxSize(maxByteSize) {
103 mCache.setOnEntryRemovedListener(this);
104 mDebugEnabled = Properties::debugLevel & kDebugMoreCaches;
107 TextDropShadowCache::~TextDropShadowCache() {
111 ///////////////////////////////////////////////////////////////////////////////
113 ///////////////////////////////////////////////////////////////////////////////
115 uint32_t TextDropShadowCache::getSize() {
119 uint32_t TextDropShadowCache::getMaxSize() {
123 ///////////////////////////////////////////////////////////////////////////////
125 ///////////////////////////////////////////////////////////////////////////////
127 void TextDropShadowCache::operator()(ShadowText&, ShadowTexture*& texture) {
129 mSize -= texture->objectSize();
132 ALOGD("Shadow texture deleted, size = %d", texture->bitmapSize);
135 texture->deleteTexture();
140 ///////////////////////////////////////////////////////////////////////////////
142 ///////////////////////////////////////////////////////////////////////////////
144 void TextDropShadowCache::clear() {
148 ShadowTexture* TextDropShadowCache::get(const SkPaint* paint, const glyph_t* glyphs, int numGlyphs,
149 float radius, const float* positions) {
150 ShadowText entry(paint, radius, numGlyphs, glyphs, positions);
151 ShadowTexture* texture = mCache.get(entry);
154 SkPaint paintCopy(*paint);
155 paintCopy.setTextAlign(SkPaint::kLeft_Align);
156 FontRenderer::DropShadow shadow = mRenderer->renderDropShadow(&paintCopy, glyphs, numGlyphs,
163 Caches& caches = Caches::getInstance();
165 texture = new ShadowTexture(caches);
166 texture->left = shadow.penX;
167 texture->top = shadow.penY;
168 texture->generation = 0;
169 texture->blend = true;
171 const uint32_t size = shadow.width * shadow.height;
173 // Don't even try to cache a bitmap that's bigger than the cache
174 if (size < mMaxSize) {
175 while (mSize + size > mMaxSize) {
176 LOG_ALWAYS_FATAL_IF(!mCache.removeOldest(),
177 "Failed to remove oldest from cache. mSize = %"
178 PRIu32 ", mCache.size() = %zu", mSize, mCache.size());
182 // Textures are Alpha8
183 texture->upload(GL_ALPHA, shadow.width, shadow.height,
184 GL_ALPHA, GL_UNSIGNED_BYTE, shadow.image);
185 texture->setFilter(GL_LINEAR);
186 texture->setWrap(GL_CLAMP_TO_EDGE);
188 if (size < mMaxSize) {
190 ALOGD("Shadow texture created, size = %d", texture->bitmapSize);
193 entry.copyTextLocally();
195 mSize += texture->objectSize();
196 mCache.put(entry, texture);
198 texture->cleanup = true;
208 }; // namespace uirenderer
209 }; // namespace android