blob: d536c40756ff9eaf129ac421e0c64ed577b6569d [file] [log] [blame]
Romain Guy1e45aae2010-08-13 19:39:53 -07001/*
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
Romain Guy5b3b3522010-10-27 18:57:51 -070017#ifndef ANDROID_HWUI_TEXT_DROP_SHADOW_CACHE_H
18#define ANDROID_HWUI_TEXT_DROP_SHADOW_CACHE_H
Romain Guy1e45aae2010-08-13 19:39:53 -070019
20#include <GLES2/gl2.h>
21
22#include <SkPaint.h>
23
Romain Guy059e12c2012-11-28 17:35:51 -080024#include <utils/LruCache.h>
Romain Guy321dce62011-03-01 11:45:33 -080025#include <utils/String16.h>
Romain Guy25dc3a72010-12-10 12:33:05 -080026
Tom Hudson2dc236b2014-10-15 15:46:42 -040027#include "font/Font.h"
Romain Guy1e45aae2010-08-13 19:39:53 -070028#include "Texture.h"
29
30namespace android {
31namespace uirenderer {
32
Romain Guy8aa195d2013-06-04 18:00:09 -070033class Caches;
Tom Hudson2dc236b2014-10-15 15:46:42 -040034class FontRenderer;
Romain Guy8aa195d2013-06-04 18:00:09 -070035
Romain Guy1e45aae2010-08-13 19:39:53 -070036struct ShadowText {
Chris Craika1717272015-11-19 13:02:43 -080037 ShadowText(): glyphCount(0), radius(0.0f), textSize(0.0f), typeface(nullptr),
Chris Craike8c3c812016-02-05 20:10:50 -080038 flags(0), italicStyle(0.0f), scaleX(0), glyphs(nullptr), positions(nullptr) {
Romain Guyc4d8eb62010-08-18 20:48:33 -070039 }
Romain Guy1e45aae2010-08-13 19:39:53 -070040
Romain Guy69fcbcc2012-11-30 15:29:15 -080041 // len is the number of bytes in text
Chris Craike8c3c812016-02-05 20:10:50 -080042 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) {
Romain Guy1e45aae2010-08-13 19:39:53 -070053 }
54
Romain Guy1e45aae2010-08-13 19:39:53 -070055 ~ShadowText() {
Romain Guy1e45aae2010-08-13 19:39:53 -070056 }
57
Romain Guy059e12c2012-11-28 17:35:51 -080058 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 }
Romain Guy321dce62011-03-01 11:45:33 -080069
70 void copyTextLocally() {
Chris Craike8c3c812016-02-05 20:10:50 -080071 str.setTo(reinterpret_cast<const char16_t*>(glyphs), glyphCount);
72 glyphs = reinterpret_cast<const glyph_t*>(str.string());
Chris Craike84a2082014-12-22 14:28:49 -080073 if (positions != nullptr) {
Raph Levien416a8472012-07-19 22:48:17 -070074 positionsCopy.clear();
Chris Craika1717272015-11-19 13:02:43 -080075 positionsCopy.appendArray(positions, glyphCount * 2);
Raph Levien416a8472012-07-19 22:48:17 -070076 positions = positionsCopy.array();
77 }
Romain Guy321dce62011-03-01 11:45:33 -080078 }
Romain Guy1e45aae2010-08-13 19:39:53 -070079
Chris Craika1717272015-11-19 13:02:43 -080080 uint32_t glyphCount;
Romain Guy059e12c2012-11-28 17:35:51 -080081 float radius;
82 float textSize;
83 SkTypeface* typeface;
84 uint32_t flags;
85 float italicStyle;
86 float scaleX;
Chris Craike8c3c812016-02-05 20:10:50 -080087 const glyph_t* glyphs;
Romain Guy059e12c2012-11-28 17:35:51 -080088 const float* positions;
89
90 // Not directly used to compute the cache key
91 String16 str;
92 Vector<float> positionsCopy;
93
Romain Guy1e45aae2010-08-13 19:39:53 -070094}; // struct ShadowText
95
Romain Guy059e12c2012-11-28 17:35:51 -080096// Caching support
97
98inline int strictly_order_type(const ShadowText& lhs, const ShadowText& rhs) {
99 return ShadowText::compare(lhs, rhs) < 0;
100}
101
102inline int compare_type(const ShadowText& lhs, const ShadowText& rhs) {
103 return ShadowText::compare(lhs, rhs);
104}
105
106inline hash_t hash_type(const ShadowText& entry) {
107 return entry.hash();
108}
109
Romain Guy1e45aae2010-08-13 19:39:53 -0700110/**
111 * Alpha texture used to represent a shadow.
112 */
113struct ShadowTexture: public Texture {
Romain Guy8aa195d2013-06-04 18:00:09 -0700114 ShadowTexture(Caches& caches): Texture(caches) {
Romain Guy1e45aae2010-08-13 19:39:53 -0700115 }
116
117 float left;
118 float top;
119}; // struct ShadowTexture
120
121class TextDropShadowCache: public OnEntryRemoved<ShadowText, ShadowTexture*> {
122public:
Romain Guyfb8b7632010-08-23 21:05:08 -0700123 TextDropShadowCache();
Romain Guy1e45aae2010-08-13 19:39:53 -0700124 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 */
Chris Craike84a2082014-12-22 14:28:49 -0800131 void operator()(ShadowText& text, ShadowTexture*& texture) override;
Romain Guy1e45aae2010-08-13 19:39:53 -0700132
Chris Craike8c3c812016-02-05 20:10:50 -0800133 ShadowTexture* get(const SkPaint* paint, const glyph_t* text,
Romain Guy059e12c2012-11-28 17:35:51 -0800134 int numGlyphs, float radius, const float* positions);
Romain Guy1e45aae2010-08-13 19:39:53 -0700135
136 /**
137 * Clears the cache. This causes all textures to be deleted.
138 */
139 void clear();
140
141 void setFontRenderer(FontRenderer& fontRenderer) {
142 mRenderer = &fontRenderer;
143 }
144
145 /**
Romain Guy1e45aae2010-08-13 19:39:53 -0700146 * 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
154private:
Romain Guy059e12c2012-11-28 17:35:51 -0800155 LruCache<ShadowText, ShadowTexture*> mCache;
Romain Guy1e45aae2010-08-13 19:39:53 -0700156
157 uint32_t mSize;
Chris Craik48a8f432016-02-05 15:59:29 -0800158 const uint32_t mMaxSize;
159 FontRenderer* mRenderer = nullptr;
Romain Guy25dc3a72010-12-10 12:33:05 -0800160 bool mDebugEnabled;
Romain Guy1e45aae2010-08-13 19:39:53 -0700161}; // class TextDropShadowCache
162
163}; // namespace uirenderer
164}; // namespace android
165
Romain Guy5b3b3522010-10-27 18:57:51 -0700166#endif // ANDROID_HWUI_TEXT_DROP_SHADOW_CACHE_H