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#include <utils/JenkinsHash.h>
18
19#include "Caches.h"
20#include "Debug.h"
21#include "FontRenderer.h"
22#include "TextDropShadowCache.h"
23#include "Properties.h"
24
25namespace android {
26namespace uirenderer {
27
28///////////////////////////////////////////////////////////////////////////////
29// Cache support
30///////////////////////////////////////////////////////////////////////////////
31
32hash_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));
40    if (glyphs) {
41        hash = JenkinsHashMixShorts(
42            hash, reinterpret_cast<const uint16_t*>(glyphs), glyphCount);
43    }
44    if (positions) {
45        for (uint32_t i = 0; i < glyphCount * 2; i++) {
46            hash = JenkinsHashMix(hash, android::hash_type(positions[i]));
47        }
48    }
49    return JenkinsHashWhiten(hash);
50}
51
52int ShadowText::compare(const ShadowText& lhs, const ShadowText& rhs) {
53    int deltaInt = int(lhs.glyphCount) - int(rhs.glyphCount);
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.glyphs != rhs.glyphs) {
75        if (!lhs.glyphs) return -1;
76        if (!rhs.glyphs) return +1;
77
78        deltaInt = memcmp(lhs.glyphs, rhs.glyphs, lhs.glyphCount * sizeof(glyph_t));
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
86        return memcmp(lhs.positions, rhs.positions, lhs.glyphCount * sizeof(float) * 2);
87    }
88
89    return 0;
90}
91
92///////////////////////////////////////////////////////////////////////////////
93// Constructors/destructor
94///////////////////////////////////////////////////////////////////////////////
95
96TextDropShadowCache::TextDropShadowCache()
97        : TextDropShadowCache(Properties::textDropShadowCacheSize) {}
98
99TextDropShadowCache::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;
105}
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
123///////////////////////////////////////////////////////////////////////////////
124// Callbacks
125///////////////////////////////////////////////////////////////////////////////
126
127void TextDropShadowCache::operator()(ShadowText&, ShadowTexture*& texture) {
128    if (texture) {
129        mSize -= texture->objectSize();
130
131        if (mDebugEnabled) {
132            ALOGD("Shadow texture deleted, size = %d", texture->bitmapSize);
133        }
134
135        texture->deleteTexture();
136        delete texture;
137    }
138}
139
140///////////////////////////////////////////////////////////////////////////////
141// Caching
142///////////////////////////////////////////////////////////////////////////////
143
144void TextDropShadowCache::clear() {
145    mCache.clear();
146}
147
148ShadowTexture* 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);
152
153    if (!texture) {
154        SkPaint paintCopy(*paint);
155        paintCopy.setTextAlign(SkPaint::kLeft_Align);
156        FontRenderer::DropShadow shadow = mRenderer->renderDropShadow(&paintCopy, glyphs, numGlyphs,
157                radius, positions);
158
159        if (!shadow.image) {
160            return nullptr;
161        }
162
163        Caches& caches = Caches::getInstance();
164
165        texture = new ShadowTexture(caches);
166        texture->left = shadow.penX;
167        texture->top = shadow.penY;
168        texture->generation = 0;
169        texture->blend = true;
170
171        const uint32_t size = shadow.width * shadow.height;
172
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());
179            }
180        }
181
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);
187
188        if (size < mMaxSize) {
189            if (mDebugEnabled) {
190                ALOGD("Shadow texture created, size = %d", texture->bitmapSize);
191            }
192
193            entry.copyTextLocally();
194
195            mSize += texture->objectSize();
196            mCache.put(entry, texture);
197        } else {
198            texture->cleanup = true;
199        }
200
201        // Cleanup shadow
202        free(shadow.image);
203    }
204
205    return texture;
206}
207
208}; // namespace uirenderer
209}; // namespace android
210