TextDropShadowCache.h revision b37cbec6d06578a72bdd9c2caa3fd964ade71c53
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/String8.h>
25
26#include "utils/Compare.h"
27#include "utils/GenerationCache.h"
28#include "FontRenderer.h"
29#include "Texture.h"
30
31namespace android {
32namespace uirenderer {
33
34struct ShadowText {
35    ShadowText(): radius(0), len(0), textSize(0.0f), typeface(NULL) {
36    }
37
38    ShadowText(SkPaint* paint, uint32_t radius, uint32_t len, const char* srcText):
39            radius(radius), len(len) {
40        // The source text we receive is in UTF-16, convert to UTF-8
41        str.setTo((const char16_t*) srcText, len >> 1);
42
43        textSize = paint->getTextSize();
44        typeface = paint->getTypeface();
45    }
46
47    ShadowText(const ShadowText& shadow):
48            radius(shadow.radius), len(shadow.len), textSize(shadow.textSize),
49            typeface(shadow.typeface), str(shadow.str) {
50    }
51
52    ~ShadowText() {
53    }
54
55    uint32_t radius;
56    uint32_t len;
57    float textSize;
58    SkTypeface* typeface;
59    String8 str;
60
61    // TODO: Should take into account fake bold and text skew
62    bool operator<(const ShadowText& rhs) const {
63        LTE_INT(len) {
64            LTE_INT(radius) {
65                LTE_FLOAT(textSize) {
66                    if (typeface < rhs.typeface) return true;
67                    else if (typeface == rhs.typeface) {
68                        return str.compare(rhs.str) < 0;
69                    }
70                }
71            }
72        }
73        return false;
74    }
75}; // struct ShadowText
76
77/**
78 * Alpha texture used to represent a shadow.
79 */
80struct ShadowTexture: public Texture {
81    ShadowTexture(): Texture() {
82    }
83
84    float left;
85    float top;
86}; // struct ShadowTexture
87
88class TextDropShadowCache: public OnEntryRemoved<ShadowText, ShadowTexture*> {
89public:
90    TextDropShadowCache();
91    TextDropShadowCache(uint32_t maxByteSize);
92    ~TextDropShadowCache();
93
94    /**
95     * Used as a callback when an entry is removed from the cache.
96     * Do not invoke directly.
97     */
98    void operator()(ShadowText& text, ShadowTexture*& texture);
99
100    ShadowTexture* get(SkPaint* paint, const char* text, uint32_t len,
101            int numGlyphs, uint32_t radius);
102
103    /**
104     * Clears the cache. This causes all textures to be deleted.
105     */
106    void clear();
107
108    void setFontRenderer(FontRenderer& fontRenderer) {
109        mRenderer = &fontRenderer;
110    }
111
112    /**
113     * Sets the maximum size of the cache in bytes.
114     */
115    void setMaxSize(uint32_t maxSize);
116    /**
117     * Returns the maximum size of the cache in bytes.
118     */
119    uint32_t getMaxSize();
120    /**
121     * Returns the current size of the cache in bytes.
122     */
123    uint32_t getSize();
124
125private:
126    void init();
127
128    GenerationCache<ShadowText, ShadowTexture*> mCache;
129
130    uint32_t mSize;
131    uint32_t mMaxSize;
132    FontRenderer* mRenderer;
133    bool mDebugEnabled;
134}; // class TextDropShadowCache
135
136}; // namespace uirenderer
137}; // namespace android
138
139#endif // ANDROID_HWUI_TEXT_DROP_SHADOW_CACHE_H
140