TextDropShadowCache.h revision c4d8eb6fb7c88c5c4da38b0b113c24cc4b78c0b7
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_UI_TEXT_DROP_SHADOW_CACHE_H
18#define ANDROID_UI_TEXT_DROP_SHADOW_CACHE_H
19
20#include <GLES2/gl2.h>
21
22#include <SkPaint.h>
23
24#include "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 (len < rhs.len) return true;
73        else if (len == rhs.len) {
74            if (radius < rhs.radius) return true;
75            else if (radius == rhs.radius) {
76                if (textSize < rhs.textSize) return true;
77                else if (textSize == rhs.textSize) {
78                    if (typeface < rhs.typeface) return true;
79                    else if (typeface == rhs.typeface) {
80                        if (hash < rhs.hash) return true;
81                        if (hash == rhs.hash) {
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(uint32_t maxByteSize);
106    ~TextDropShadowCache();
107
108    /**
109     * Used as a callback when an entry is removed from the cache.
110     * Do not invoke directly.
111     */
112    void operator()(ShadowText& text, ShadowTexture*& texture);
113
114    ShadowTexture* get(SkPaint* paint, const char* text, uint32_t len,
115            int numGlyphs, uint32_t radius);
116
117    /**
118     * Clears the cache. This causes all textures to be deleted.
119     */
120    void clear();
121
122    void setFontRenderer(FontRenderer& fontRenderer) {
123        mRenderer = &fontRenderer;
124    }
125
126    /**
127     * Sets the maximum size of the cache in bytes.
128     */
129    void setMaxSize(uint32_t maxSize);
130    /**
131     * Returns the maximum size of the cache in bytes.
132     */
133    uint32_t getMaxSize();
134    /**
135     * Returns the current size of the cache in bytes.
136     */
137    uint32_t getSize();
138
139private:
140    GenerationCache<ShadowText, ShadowTexture*> mCache;
141
142    uint32_t mSize;
143    uint32_t mMaxSize;
144    FontRenderer* mRenderer;
145}; // class TextDropShadowCache
146
147}; // namespace uirenderer
148}; // namespace android
149
150#endif // ANDROID_UI_TEXT_DROP_SHADOW_CACHE_H
151