• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "font/Font.h"
28 #include "Texture.h"
29 
30 namespace android {
31 namespace uirenderer {
32 
33 class Caches;
34 class FontRenderer;
35 
36 struct ShadowText {
ShadowTextShadowText37     ShadowText(): glyphCount(0), radius(0.0f), textSize(0.0f), typeface(nullptr),
38             flags(0), italicStyle(0.0f), scaleX(0), glyphs(nullptr), positions(nullptr) {
39     }
40 
41     // len is the number of bytes in text
ShadowTextShadowText42     ShadowText(const SkPaint* paint, float radius, uint32_t glyphCount, const glyph_t* srcGlyphs,
43             const float* positions)
44             : glyphCount(glyphCount)
45             , radius(radius)
46             , textSize(paint->getTextSize())
47             , typeface(paint->getTypeface())
48             , flags(paint->isFakeBoldText() ? Font::kFakeBold : 0)
49             , italicStyle(paint->getTextSkewX())
50             , scaleX(paint->getTextScaleX())
51             , glyphs(srcGlyphs)
52             , positions(positions) {
53     }
54 
~ShadowTextShadowText55     ~ShadowText() {
56     }
57 
58     hash_t hash() const;
59 
60     static int compare(const ShadowText& lhs, const ShadowText& rhs);
61 
62     bool operator==(const ShadowText& other) const {
63         return compare(*this, other) == 0;
64     }
65 
66     bool operator!=(const ShadowText& other) const {
67         return compare(*this, other) != 0;
68     }
69 
copyTextLocallyShadowText70     void copyTextLocally() {
71         str.setTo(reinterpret_cast<const char16_t*>(glyphs), glyphCount);
72         glyphs = reinterpret_cast<const glyph_t*>(str.string());
73         if (positions != nullptr) {
74             positionsCopy.clear();
75             positionsCopy.appendArray(positions, glyphCount * 2);
76             positions = positionsCopy.array();
77         }
78     }
79 
80     uint32_t glyphCount;
81     float radius;
82     float textSize;
83     SkTypeface* typeface;
84     uint32_t flags;
85     float italicStyle;
86     float scaleX;
87     const glyph_t* glyphs;
88     const float* positions;
89 
90     // Not directly used to compute the cache key
91     String16 str;
92     Vector<float> positionsCopy;
93 
94 }; // struct ShadowText
95 
96 // Caching support
97 
strictly_order_type(const ShadowText & lhs,const ShadowText & rhs)98 inline int strictly_order_type(const ShadowText& lhs, const ShadowText& rhs) {
99     return ShadowText::compare(lhs, rhs) < 0;
100 }
101 
compare_type(const ShadowText & lhs,const ShadowText & rhs)102 inline int compare_type(const ShadowText& lhs, const ShadowText& rhs) {
103     return ShadowText::compare(lhs, rhs);
104 }
105 
hash_type(const ShadowText & entry)106 inline hash_t hash_type(const ShadowText& entry) {
107     return entry.hash();
108 }
109 
110 /**
111  * Alpha texture used to represent a shadow.
112  */
113 struct ShadowTexture: public Texture {
ShadowTextureShadowTexture114     explicit ShadowTexture(Caches& caches): Texture(caches) {
115     }
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,
134             int numGlyphs, float radius, 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) {
142         mRenderer = &fontRenderer;
143     }
144 
145     /**
146      * Returns the maximum size of the cache in bytes.
147      */
148     uint32_t getMaxSize();
149     /**
150      * Returns the current size of the cache in bytes.
151      */
152     uint32_t getSize();
153 
154 private:
155     LruCache<ShadowText, ShadowTexture*> mCache;
156 
157     uint32_t mSize;
158     const uint32_t mMaxSize;
159     FontRenderer* mRenderer = nullptr;
160     bool mDebugEnabled;
161 }; // class TextDropShadowCache
162 
163 }; // namespace uirenderer
164 }; // namespace android
165 
166 #endif // ANDROID_HWUI_TEXT_DROP_SHADOW_CACHE_H
167