TextDropShadowCache.h revision 5b3b35296e8b2c8d3f07d32bb645d5414db41a1d
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#ifndef ANDROID_HWUI_TEXT_DROP_SHADOW_CACHE_H
18#define ANDROID_HWUI_TEXT_DROP_SHADOW_CACHE_H
19
20#include <GLES2/gl2.h>
21
22#include <SkPaint.h>
23
24#include "utils/GenerationCache.h"
25#include "FontRenderer.h"
26#include "Texture.h"
27
28namespace android {
29namespace uirenderer {
30
31struct ShadowText {
32    ShadowText() {
33        text = NULL;
34    }
35
36    ShadowText(SkPaint* paint, uint32_t radius, uint32_t len, const char* srcText):
37            radius(radius), len(len) {
38        text = new char[len];
39        memcpy(text, srcText, len);
40
41        textSize = paint->getTextSize();
42        typeface = paint->getTypeface();
43
44        hash = 0;
45        uint32_t multiplier = 1;
46        for (uint32_t i = 0; i < len; i++) {
47            hash += text[i] * multiplier;
48            uint32_t shifted = multiplier << 5;
49            multiplier = shifted - multiplier;
50        }
51    }
52
53    ShadowText(const ShadowText& shadow):
54            radius(shadow.radius), len(shadow.len), hash(shadow.hash),
55            textSize(shadow.textSize), typeface(shadow.typeface) {
56        text = new char[shadow.len];
57        memcpy(text, shadow.text, shadow.len);
58    }
59
60    ~ShadowText() {
61        delete[] text;
62    }
63
64    uint32_t radius;
65    uint32_t len;
66    uint32_t hash;
67    float textSize;
68    SkTypeface* typeface;
69    char *text;
70
71    bool operator<(const ShadowText& rhs) const {
72        if (hash < rhs.hash) return true;
73        else if (hash == rhs.hash) {
74            if (len < rhs.len) return true;
75            else if (len == rhs.len) {
76                if (radius < rhs.radius) return true;
77                else if (radius == rhs.radius) {
78                    if (textSize < rhs.textSize) return true;
79                    else if (textSize == rhs.textSize) {
80                        if (typeface < rhs.typeface) return true;
81                        else if (typeface == rhs.typeface) {
82                            return strncmp(text, rhs.text, len) < 0;
83                        }
84                    }
85                }
86            }
87        }
88        return false;
89    }
90}; // struct ShadowText
91
92/**
93 * Alpha texture used to represent a shadow.
94 */
95struct ShadowTexture: public Texture {
96    ShadowTexture(): Texture() {
97    }
98
99    float left;
100    float top;
101}; // struct ShadowTexture
102
103class TextDropShadowCache: public OnEntryRemoved<ShadowText, ShadowTexture*> {
104public:
105    TextDropShadowCache();
106    TextDropShadowCache(uint32_t maxByteSize);
107    ~TextDropShadowCache();
108
109    /**
110     * Used as a callback when an entry is removed from the cache.
111     * Do not invoke directly.
112     */
113    void operator()(ShadowText& text, ShadowTexture*& texture);
114
115    ShadowTexture* get(SkPaint* paint, const char* text, uint32_t len,
116            int numGlyphs, uint32_t radius);
117
118    /**
119     * Clears the cache. This causes all textures to be deleted.
120     */
121    void clear();
122
123    void setFontRenderer(FontRenderer& fontRenderer) {
124        mRenderer = &fontRenderer;
125    }
126
127    /**
128     * Sets the maximum size of the cache in bytes.
129     */
130    void setMaxSize(uint32_t maxSize);
131    /**
132     * Returns the maximum size of the cache in bytes.
133     */
134    uint32_t getMaxSize();
135    /**
136     * Returns the current size of the cache in bytes.
137     */
138    uint32_t getSize();
139
140private:
141    GenerationCache<ShadowText, ShadowTexture*> mCache;
142
143    uint32_t mSize;
144    uint32_t mMaxSize;
145    FontRenderer* mRenderer;
146}; // class TextDropShadowCache
147
148}; // namespace uirenderer
149}; // namespace android
150
151#endif // ANDROID_HWUI_TEXT_DROP_SHADOW_CACHE_H
152