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/LruCache.h>
25 #include <utils/String16.h>
26
27 #include "Texture.h"
28 #include "font/Font.h"
29
30 namespace android {
31 namespace uirenderer {
32
33 class Caches;
34 class FontRenderer;
35
36 struct ShadowText {
ShadowTextShadowText37 ShadowText()
38 : glyphCount(0)
39 , radius(0.0f)
40 , textSize(0.0f)
41 , typeface(nullptr)
42 , flags(0)
43 , italicStyle(0.0f)
44 , scaleX(0)
45 , glyphs(nullptr)
46 , positions(nullptr) {}
47
48 // len is the number of bytes in text
ShadowTextShadowText49 ShadowText(const SkPaint* paint, float radius, uint32_t glyphCount, const glyph_t* srcGlyphs,
50 const float* positions)
51 : glyphCount(glyphCount)
52 , radius(radius)
53 , textSize(paint->getTextSize())
54 , typeface(paint->getTypeface())
55 , flags(paint->isFakeBoldText() ? Font::kFakeBold : 0)
56 , italicStyle(paint->getTextSkewX())
57 , scaleX(paint->getTextScaleX())
58 , glyphs(srcGlyphs)
59 , positions(positions) {}
60
~ShadowTextShadowText61 ~ShadowText() {}
62
63 hash_t hash() const;
64
65 static int compare(const ShadowText& lhs, const ShadowText& rhs);
66
67 bool operator==(const ShadowText& other) const { return compare(*this, other) == 0; }
68
69 bool operator!=(const ShadowText& other) const { return compare(*this, other) != 0; }
70
copyTextLocallyShadowText71 void copyTextLocally() {
72 str.setTo(reinterpret_cast<const char16_t*>(glyphs), glyphCount);
73 glyphs = reinterpret_cast<const glyph_t*>(str.string());
74 if (positions != nullptr) {
75 positionsCopy.clear();
76 positionsCopy.appendArray(positions, glyphCount * 2);
77 positions = positionsCopy.array();
78 }
79 }
80
81 uint32_t glyphCount;
82 float radius;
83 float textSize;
84 SkTypeface* typeface;
85 uint32_t flags;
86 float italicStyle;
87 float scaleX;
88 const glyph_t* glyphs;
89 const float* positions;
90
91 // Not directly used to compute the cache key
92 String16 str;
93 Vector<float> positionsCopy;
94
95 }; // struct ShadowText
96
97 // Caching support
98
strictly_order_type(const ShadowText & lhs,const ShadowText & rhs)99 inline int strictly_order_type(const ShadowText& lhs, const ShadowText& rhs) {
100 return ShadowText::compare(lhs, rhs) < 0;
101 }
102
compare_type(const ShadowText & lhs,const ShadowText & rhs)103 inline int compare_type(const ShadowText& lhs, const ShadowText& rhs) {
104 return ShadowText::compare(lhs, rhs);
105 }
106
hash_type(const ShadowText & entry)107 inline hash_t hash_type(const ShadowText& entry) {
108 return entry.hash();
109 }
110
111 /**
112 * Alpha texture used to represent a shadow.
113 */
114 struct ShadowTexture : public Texture {
ShadowTextureShadowTexture115 explicit ShadowTexture(Caches& caches) : Texture(caches) {}
116
117 float left;
118 float top;
119 }; // struct ShadowTexture
120
121 class TextDropShadowCache : public OnEntryRemoved<ShadowText, ShadowTexture*> {
122 public:
123 TextDropShadowCache();
124 explicit TextDropShadowCache(uint32_t maxByteSize);
125 ~TextDropShadowCache();
126
127 /**
128 * Used as a callback when an entry is removed from the cache.
129 * Do not invoke directly.
130 */
131 void operator()(ShadowText& text, ShadowTexture*& texture) override;
132
133 ShadowTexture* get(const SkPaint* paint, const glyph_t* text, int numGlyphs, float radius,
134 const float* positions);
135
136 /**
137 * Clears the cache. This causes all textures to be deleted.
138 */
139 void clear();
140
setFontRenderer(FontRenderer & fontRenderer)141 void setFontRenderer(FontRenderer& fontRenderer) { mRenderer = &fontRenderer; }
142
143 /**
144 * Returns the maximum size of the cache in bytes.
145 */
146 uint32_t getMaxSize();
147 /**
148 * Returns the current size of the cache in bytes.
149 */
150 uint32_t getSize();
151
152 private:
153 LruCache<ShadowText, ShadowTexture*> mCache;
154
155 uint32_t mSize;
156 const uint32_t mMaxSize;
157 FontRenderer* mRenderer = nullptr;
158 bool mDebugEnabled;
159 }; // class TextDropShadowCache
160
161 }; // namespace uirenderer
162 }; // namespace android
163
164 #endif // ANDROID_HWUI_TEXT_DROP_SHADOW_CACHE_H
165