blob: 86a012970f17c16534bdf00348de4623b625af8b [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
Romain Guy1e45aae2010-08-13 19:39:53 -070027#include "Texture.h"
John Reck1bcacfd2017-11-03 10:12:19 -070028#include "font/Font.h"
Romain Guy1e45aae2010-08-13 19:39:53 -070029
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 {
John Reck1bcacfd2017-11-03 10:12:19 -070037 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) {}
Romain Guy1e45aae2010-08-13 19:39:53 -070047
Romain Guy69fcbcc2012-11-30 15:29:15 -080048 // len is the number of bytes in text
Chris Craike8c3c812016-02-05 20:10:50 -080049 ShadowText(const SkPaint* paint, float radius, uint32_t glyphCount, const glyph_t* srcGlyphs,
John Reck1bcacfd2017-11-03 10:12:19 -070050 const float* positions)
Chris Craike8c3c812016-02-05 20:10:50 -080051 : 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)
John Reck1bcacfd2017-11-03 10:12:19 -070059 , positions(positions) {}
Romain Guy1e45aae2010-08-13 19:39:53 -070060
John Reck1bcacfd2017-11-03 10:12:19 -070061 ~ShadowText() {}
Romain Guy1e45aae2010-08-13 19:39:53 -070062
Romain Guy059e12c2012-11-28 17:35:51 -080063 hash_t hash() const;
64
65 static int compare(const ShadowText& lhs, const ShadowText& rhs);
66
John Reck1bcacfd2017-11-03 10:12:19 -070067 bool operator==(const ShadowText& other) const { return compare(*this, other) == 0; }
Romain Guy059e12c2012-11-28 17:35:51 -080068
John Reck1bcacfd2017-11-03 10:12:19 -070069 bool operator!=(const ShadowText& other) const { return compare(*this, other) != 0; }
Romain Guy321dce62011-03-01 11:45:33 -080070
71 void copyTextLocally() {
Chris Craike8c3c812016-02-05 20:10:50 -080072 str.setTo(reinterpret_cast<const char16_t*>(glyphs), glyphCount);
73 glyphs = reinterpret_cast<const glyph_t*>(str.string());
Chris Craike84a2082014-12-22 14:28:49 -080074 if (positions != nullptr) {
Raph Levien416a8472012-07-19 22:48:17 -070075 positionsCopy.clear();
Chris Craika1717272015-11-19 13:02:43 -080076 positionsCopy.appendArray(positions, glyphCount * 2);
Raph Levien416a8472012-07-19 22:48:17 -070077 positions = positionsCopy.array();
78 }
Romain Guy321dce62011-03-01 11:45:33 -080079 }
Romain Guy1e45aae2010-08-13 19:39:53 -070080
Chris Craika1717272015-11-19 13:02:43 -080081 uint32_t glyphCount;
Romain Guy059e12c2012-11-28 17:35:51 -080082 float radius;
83 float textSize;
84 SkTypeface* typeface;
85 uint32_t flags;
86 float italicStyle;
87 float scaleX;
Chris Craike8c3c812016-02-05 20:10:50 -080088 const glyph_t* glyphs;
Romain Guy059e12c2012-11-28 17:35:51 -080089 const float* positions;
90
91 // Not directly used to compute the cache key
92 String16 str;
93 Vector<float> positionsCopy;
94
John Reck1bcacfd2017-11-03 10:12:19 -070095}; // struct ShadowText
Romain Guy1e45aae2010-08-13 19:39:53 -070096
Romain Guy059e12c2012-11-28 17:35:51 -080097// Caching support
98
99inline int strictly_order_type(const ShadowText& lhs, const ShadowText& rhs) {
100 return ShadowText::compare(lhs, rhs) < 0;
101}
102
103inline int compare_type(const ShadowText& lhs, const ShadowText& rhs) {
104 return ShadowText::compare(lhs, rhs);
105}
106
107inline hash_t hash_type(const ShadowText& entry) {
108 return entry.hash();
109}
110
Romain Guy1e45aae2010-08-13 19:39:53 -0700111/**
112 * Alpha texture used to represent a shadow.
113 */
John Reck1bcacfd2017-11-03 10:12:19 -0700114struct ShadowTexture : public Texture {
115 explicit ShadowTexture(Caches& caches) : Texture(caches) {}
Romain Guy1e45aae2010-08-13 19:39:53 -0700116
117 float left;
118 float top;
John Reck1bcacfd2017-11-03 10:12:19 -0700119}; // struct ShadowTexture
Romain Guy1e45aae2010-08-13 19:39:53 -0700120
John Reck1bcacfd2017-11-03 10:12:19 -0700121class TextDropShadowCache : public OnEntryRemoved<ShadowText, ShadowTexture*> {
Romain Guy1e45aae2010-08-13 19:39:53 -0700122public:
Romain Guyfb8b7632010-08-23 21:05:08 -0700123 TextDropShadowCache();
Chih-Hung Hsiehfaecb782016-07-21 11:23:06 -0700124 explicit TextDropShadowCache(uint32_t maxByteSize);
Romain Guy1e45aae2010-08-13 19:39:53 -0700125 ~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
John Reck1bcacfd2017-11-03 10:12:19 -0700133 ShadowTexture* get(const SkPaint* paint, const glyph_t* text, int numGlyphs, float radius,
134 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
John Reck1bcacfd2017-11-03 10:12:19 -0700141 void setFontRenderer(FontRenderer& fontRenderer) { mRenderer = &fontRenderer; }
Romain Guy1e45aae2010-08-13 19:39:53 -0700142
143 /**
Romain Guy1e45aae2010-08-13 19:39:53 -0700144 * 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
152private:
Romain Guy059e12c2012-11-28 17:35:51 -0800153 LruCache<ShadowText, ShadowTexture*> mCache;
Romain Guy1e45aae2010-08-13 19:39:53 -0700154
155 uint32_t mSize;
Chris Craik48a8f432016-02-05 15:59:29 -0800156 const uint32_t mMaxSize;
157 FontRenderer* mRenderer = nullptr;
Romain Guy25dc3a72010-12-10 12:33:05 -0800158 bool mDebugEnabled;
John Reck1bcacfd2017-11-03 10:12:19 -0700159}; // class TextDropShadowCache
Romain Guy1e45aae2010-08-13 19:39:53 -0700160
John Reck1bcacfd2017-11-03 10:12:19 -0700161}; // namespace uirenderer
162}; // namespace android
Romain Guy1e45aae2010-08-13 19:39:53 -0700163
John Reck1bcacfd2017-11-03 10:12:19 -0700164#endif // ANDROID_HWUI_TEXT_DROP_SHADOW_CACHE_H