blob: 38bf05a0a0d1eda7adda8243597039c379024674 [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 "FontRenderer.h"
28#include "Texture.h"
29
30namespace android {
31namespace uirenderer {
32
33struct ShadowText {
Romain Guy059e12c2012-11-28 17:35:51 -080034 ShadowText(): len(0), radius(0.0f), textSize(0.0f), typeface(NULL),
35 flags(0), italicStyle(0.0f), scaleX(0), text(NULL), positions(NULL) {
Romain Guyc4d8eb62010-08-18 20:48:33 -070036 }
Romain Guy1e45aae2010-08-13 19:39:53 -070037
Romain Guy059e12c2012-11-28 17:35:51 -080038 ShadowText(SkPaint* paint, float radius, uint32_t len, const char* srcText,
Raph Levien416a8472012-07-19 22:48:17 -070039 const float* positions):
Romain Guy059e12c2012-11-28 17:35:51 -080040 len(len), radius(radius), positions(positions) {
Romain Guy321dce62011-03-01 11:45:33 -080041 // TODO: Propagate this through the API, we should not cast here
42 text = (const char16_t*) srcText;
Romain Guy1e45aae2010-08-13 19:39:53 -070043
Romain Guyc4d8eb62010-08-18 20:48:33 -070044 textSize = paint->getTextSize();
45 typeface = paint->getTypeface();
Romain Guycabfcc12011-03-07 18:06:46 -080046
47 flags = 0;
48 if (paint->isFakeBoldText()) {
49 flags |= Font::kFakeBold;
50 }
51
Romain Guy059e12c2012-11-28 17:35:51 -080052 italicStyle = paint->getTextSkewX();
53 scaleX = paint->getTextScaleX();
Romain Guy1e45aae2010-08-13 19:39:53 -070054 }
55
Romain Guy1e45aae2010-08-13 19:39:53 -070056 ~ShadowText() {
Romain Guy1e45aae2010-08-13 19:39:53 -070057 }
58
Romain Guy059e12c2012-11-28 17:35:51 -080059 hash_t hash() const;
60
61 static int compare(const ShadowText& lhs, const ShadowText& rhs);
62
63 bool operator==(const ShadowText& other) const {
64 return compare(*this, other) == 0;
65 }
66
67 bool operator!=(const ShadowText& other) const {
68 return compare(*this, other) != 0;
69 }
Romain Guy321dce62011-03-01 11:45:33 -080070
71 void copyTextLocally() {
Romain Guy059e12c2012-11-28 17:35:51 -080072 str.setTo((const char16_t*) text, len * sizeof(char16_t));
Romain Guy321dce62011-03-01 11:45:33 -080073 text = str.string();
Raph Levien416a8472012-07-19 22:48:17 -070074 if (positions != NULL) {
75 positionsCopy.clear();
76 positionsCopy.appendArray(positions, len);
77 positions = positionsCopy.array();
78 }
Romain Guy321dce62011-03-01 11:45:33 -080079 }
Romain Guy1e45aae2010-08-13 19:39:53 -070080
Romain Guy059e12c2012-11-28 17:35:51 -080081 uint32_t len;
82 float radius;
83 float textSize;
84 SkTypeface* typeface;
85 uint32_t flags;
86 float italicStyle;
87 float scaleX;
88 const char16_t* text;
89 const float* positions;
90
91 // Not directly used to compute the cache key
92 String16 str;
93 Vector<float> positionsCopy;
94
Romain Guy1e45aae2010-08-13 19:39:53 -070095}; // struct ShadowText
96
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 */
114struct ShadowTexture: public Texture {
115 ShadowTexture(): Texture() {
116 }
117
118 float left;
119 float top;
120}; // struct ShadowTexture
121
122class TextDropShadowCache: public OnEntryRemoved<ShadowText, ShadowTexture*> {
123public:
Romain Guyfb8b7632010-08-23 21:05:08 -0700124 TextDropShadowCache();
Romain Guy1e45aae2010-08-13 19:39:53 -0700125 TextDropShadowCache(uint32_t maxByteSize);
126 ~TextDropShadowCache();
127
128 /**
129 * Used as a callback when an entry is removed from the cache.
130 * Do not invoke directly.
131 */
132 void operator()(ShadowText& text, ShadowTexture*& texture);
133
134 ShadowTexture* get(SkPaint* paint, const char* text, uint32_t len,
Romain Guy059e12c2012-11-28 17:35:51 -0800135 int numGlyphs, float radius, const float* positions);
Romain Guy1e45aae2010-08-13 19:39:53 -0700136
137 /**
138 * Clears the cache. This causes all textures to be deleted.
139 */
140 void clear();
141
142 void setFontRenderer(FontRenderer& fontRenderer) {
143 mRenderer = &fontRenderer;
144 }
145
146 /**
147 * Sets the maximum size of the cache in bytes.
148 */
149 void setMaxSize(uint32_t maxSize);
150 /**
151 * 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 Guy25dc3a72010-12-10 12:33:05 -0800160 void init();
161
Romain Guy059e12c2012-11-28 17:35:51 -0800162 LruCache<ShadowText, ShadowTexture*> mCache;
Romain Guy1e45aae2010-08-13 19:39:53 -0700163
164 uint32_t mSize;
165 uint32_t mMaxSize;
166 FontRenderer* mRenderer;
Romain Guy25dc3a72010-12-10 12:33:05 -0800167 bool mDebugEnabled;
Romain Guy1e45aae2010-08-13 19:39:53 -0700168}; // class TextDropShadowCache
169
170}; // namespace uirenderer
171}; // namespace android
172
Romain Guy5b3b3522010-10-27 18:57:51 -0700173#endif // ANDROID_HWUI_TEXT_DROP_SHADOW_CACHE_H