blob: cf647882e5a7cf4057dbce9513e36546bc216196 [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 Craike84a2082014-12-22 14:28:49 -080038 flags(0), italicStyle(0.0f), scaleX(0), text(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 Craika1717272015-11-19 13:02:43 -080042 ShadowText(const SkPaint* paint, float radius, uint32_t glyphCount, const char* srcText,
Raph Levien416a8472012-07-19 22:48:17 -070043 const float* positions):
Chris Craika1717272015-11-19 13:02:43 -080044 glyphCount(glyphCount), radius(radius), positions(positions) {
Romain Guy321dce62011-03-01 11:45:33 -080045 // TODO: Propagate this through the API, we should not cast here
46 text = (const char16_t*) srcText;
Romain Guy1e45aae2010-08-13 19:39:53 -070047
Romain Guyc4d8eb62010-08-18 20:48:33 -070048 textSize = paint->getTextSize();
49 typeface = paint->getTypeface();
Romain Guycabfcc12011-03-07 18:06:46 -080050
51 flags = 0;
52 if (paint->isFakeBoldText()) {
53 flags |= Font::kFakeBold;
54 }
55
Romain Guy059e12c2012-11-28 17:35:51 -080056 italicStyle = paint->getTextSkewX();
57 scaleX = paint->getTextScaleX();
Romain Guy1e45aae2010-08-13 19:39:53 -070058 }
59
Romain Guy1e45aae2010-08-13 19:39:53 -070060 ~ShadowText() {
Romain Guy1e45aae2010-08-13 19:39:53 -070061 }
62
Romain Guy059e12c2012-11-28 17:35:51 -080063 hash_t hash() const;
64
65 static int compare(const ShadowText& lhs, const ShadowText& rhs);
66
67 bool operator==(const ShadowText& other) const {
68 return compare(*this, other) == 0;
69 }
70
71 bool operator!=(const ShadowText& other) const {
72 return compare(*this, other) != 0;
73 }
Romain Guy321dce62011-03-01 11:45:33 -080074
75 void copyTextLocally() {
Chris Craika1717272015-11-19 13:02:43 -080076 str.setTo((const char16_t*) text, glyphCount);
Romain Guy321dce62011-03-01 11:45:33 -080077 text = str.string();
Chris Craike84a2082014-12-22 14:28:49 -080078 if (positions != nullptr) {
Raph Levien416a8472012-07-19 22:48:17 -070079 positionsCopy.clear();
Chris Craika1717272015-11-19 13:02:43 -080080 positionsCopy.appendArray(positions, glyphCount * 2);
Raph Levien416a8472012-07-19 22:48:17 -070081 positions = positionsCopy.array();
82 }
Romain Guy321dce62011-03-01 11:45:33 -080083 }
Romain Guy1e45aae2010-08-13 19:39:53 -070084
Chris Craika1717272015-11-19 13:02:43 -080085 uint32_t glyphCount;
Romain Guy059e12c2012-11-28 17:35:51 -080086 float radius;
87 float textSize;
88 SkTypeface* typeface;
89 uint32_t flags;
90 float italicStyle;
91 float scaleX;
92 const char16_t* text;
93 const float* positions;
94
95 // Not directly used to compute the cache key
96 String16 str;
97 Vector<float> positionsCopy;
98
Romain Guy1e45aae2010-08-13 19:39:53 -070099}; // struct ShadowText
100
Romain Guy059e12c2012-11-28 17:35:51 -0800101// Caching support
102
103inline int strictly_order_type(const ShadowText& lhs, const ShadowText& rhs) {
104 return ShadowText::compare(lhs, rhs) < 0;
105}
106
107inline int compare_type(const ShadowText& lhs, const ShadowText& rhs) {
108 return ShadowText::compare(lhs, rhs);
109}
110
111inline hash_t hash_type(const ShadowText& entry) {
112 return entry.hash();
113}
114
Romain Guy1e45aae2010-08-13 19:39:53 -0700115/**
116 * Alpha texture used to represent a shadow.
117 */
118struct ShadowTexture: public Texture {
Romain Guy8aa195d2013-06-04 18:00:09 -0700119 ShadowTexture(Caches& caches): Texture(caches) {
Romain Guy1e45aae2010-08-13 19:39:53 -0700120 }
121
122 float left;
123 float top;
124}; // struct ShadowTexture
125
126class TextDropShadowCache: public OnEntryRemoved<ShadowText, ShadowTexture*> {
127public:
Romain Guyfb8b7632010-08-23 21:05:08 -0700128 TextDropShadowCache();
Romain Guy1e45aae2010-08-13 19:39:53 -0700129 TextDropShadowCache(uint32_t maxByteSize);
130 ~TextDropShadowCache();
131
132 /**
133 * Used as a callback when an entry is removed from the cache.
134 * Do not invoke directly.
135 */
Chris Craike84a2082014-12-22 14:28:49 -0800136 void operator()(ShadowText& text, ShadowTexture*& texture) override;
Romain Guy1e45aae2010-08-13 19:39:53 -0700137
Chris Craika1717272015-11-19 13:02:43 -0800138 ShadowTexture* get(const SkPaint* paint, const char* text,
Romain Guy059e12c2012-11-28 17:35:51 -0800139 int numGlyphs, float radius, const float* positions);
Romain Guy1e45aae2010-08-13 19:39:53 -0700140
141 /**
142 * Clears the cache. This causes all textures to be deleted.
143 */
144 void clear();
145
146 void setFontRenderer(FontRenderer& fontRenderer) {
147 mRenderer = &fontRenderer;
148 }
149
150 /**
Romain Guy1e45aae2010-08-13 19:39:53 -0700151 * Returns the maximum size of the cache in bytes.
152 */
153 uint32_t getMaxSize();
154 /**
155 * Returns the current size of the cache in bytes.
156 */
157 uint32_t getSize();
158
159private:
Romain Guy059e12c2012-11-28 17:35:51 -0800160 LruCache<ShadowText, ShadowTexture*> mCache;
Romain Guy1e45aae2010-08-13 19:39:53 -0700161
162 uint32_t mSize;
Chris Craik48a8f432016-02-05 15:59:29 -0800163 const uint32_t mMaxSize;
164 FontRenderer* mRenderer = nullptr;
Romain Guy25dc3a72010-12-10 12:33:05 -0800165 bool mDebugEnabled;
Romain Guy1e45aae2010-08-13 19:39:53 -0700166}; // class TextDropShadowCache
167
168}; // namespace uirenderer
169}; // namespace android
170
Romain Guy5b3b3522010-10-27 18:57:51 -0700171#endif // ANDROID_HWUI_TEXT_DROP_SHADOW_CACHE_H