blob: 0bed72b6b03f361ec688fb31fcf3ee63d0bffadb [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 Guy69fcbcc2012-11-30 15:29:15 -080038 // len is the number of bytes in text
Romain Guy059e12c2012-11-28 17:35:51 -080039 ShadowText(SkPaint* paint, float radius, uint32_t len, const char* srcText,
Raph Levien416a8472012-07-19 22:48:17 -070040 const float* positions):
Romain Guy059e12c2012-11-28 17:35:51 -080041 len(len), radius(radius), positions(positions) {
Romain Guy321dce62011-03-01 11:45:33 -080042 // TODO: Propagate this through the API, we should not cast here
43 text = (const char16_t*) srcText;
Romain Guy1e45aae2010-08-13 19:39:53 -070044
Romain Guyc4d8eb62010-08-18 20:48:33 -070045 textSize = paint->getTextSize();
46 typeface = paint->getTypeface();
Romain Guycabfcc12011-03-07 18:06:46 -080047
48 flags = 0;
49 if (paint->isFakeBoldText()) {
50 flags |= Font::kFakeBold;
51 }
52
Romain Guy059e12c2012-11-28 17:35:51 -080053 italicStyle = paint->getTextSkewX();
54 scaleX = paint->getTextScaleX();
Romain Guy1e45aae2010-08-13 19:39:53 -070055 }
56
Romain Guy1e45aae2010-08-13 19:39:53 -070057 ~ShadowText() {
Romain Guy1e45aae2010-08-13 19:39:53 -070058 }
59
Romain Guy059e12c2012-11-28 17:35:51 -080060 hash_t hash() const;
61
62 static int compare(const ShadowText& lhs, const ShadowText& rhs);
63
64 bool operator==(const ShadowText& other) const {
65 return compare(*this, other) == 0;
66 }
67
68 bool operator!=(const ShadowText& other) const {
69 return compare(*this, other) != 0;
70 }
Romain Guy321dce62011-03-01 11:45:33 -080071
72 void copyTextLocally() {
Romain Guy69fcbcc2012-11-30 15:29:15 -080073 uint32_t charCount = len / sizeof(char16_t);
74 str.setTo((const char16_t*) text, charCount);
Romain Guy321dce62011-03-01 11:45:33 -080075 text = str.string();
Raph Levien416a8472012-07-19 22:48:17 -070076 if (positions != NULL) {
77 positionsCopy.clear();
Romain Guy69fcbcc2012-11-30 15:29:15 -080078 positionsCopy.appendArray(positions, charCount * 2);
Raph Levien416a8472012-07-19 22:48:17 -070079 positions = positionsCopy.array();
80 }
Romain Guy321dce62011-03-01 11:45:33 -080081 }
Romain Guy1e45aae2010-08-13 19:39:53 -070082
Romain Guy059e12c2012-11-28 17:35:51 -080083 uint32_t len;
84 float radius;
85 float textSize;
86 SkTypeface* typeface;
87 uint32_t flags;
88 float italicStyle;
89 float scaleX;
90 const char16_t* text;
91 const float* positions;
92
93 // Not directly used to compute the cache key
94 String16 str;
95 Vector<float> positionsCopy;
96
Romain Guy1e45aae2010-08-13 19:39:53 -070097}; // struct ShadowText
98
Romain Guy059e12c2012-11-28 17:35:51 -080099// Caching support
100
101inline int strictly_order_type(const ShadowText& lhs, const ShadowText& rhs) {
102 return ShadowText::compare(lhs, rhs) < 0;
103}
104
105inline int compare_type(const ShadowText& lhs, const ShadowText& rhs) {
106 return ShadowText::compare(lhs, rhs);
107}
108
109inline hash_t hash_type(const ShadowText& entry) {
110 return entry.hash();
111}
112
Romain Guy1e45aae2010-08-13 19:39:53 -0700113/**
114 * Alpha texture used to represent a shadow.
115 */
116struct ShadowTexture: public Texture {
117 ShadowTexture(): Texture() {
118 }
119
120 float left;
121 float top;
122}; // struct ShadowTexture
123
124class TextDropShadowCache: public OnEntryRemoved<ShadowText, ShadowTexture*> {
125public:
Romain Guyfb8b7632010-08-23 21:05:08 -0700126 TextDropShadowCache();
Romain Guy1e45aae2010-08-13 19:39:53 -0700127 TextDropShadowCache(uint32_t maxByteSize);
128 ~TextDropShadowCache();
129
130 /**
131 * Used as a callback when an entry is removed from the cache.
132 * Do not invoke directly.
133 */
134 void operator()(ShadowText& text, ShadowTexture*& texture);
135
136 ShadowTexture* get(SkPaint* paint, const char* text, uint32_t len,
Romain Guy059e12c2012-11-28 17:35:51 -0800137 int numGlyphs, float radius, const float* positions);
Romain Guy1e45aae2010-08-13 19:39:53 -0700138
139 /**
140 * Clears the cache. This causes all textures to be deleted.
141 */
142 void clear();
143
144 void setFontRenderer(FontRenderer& fontRenderer) {
145 mRenderer = &fontRenderer;
146 }
147
148 /**
149 * Sets the maximum size of the cache in bytes.
150 */
151 void setMaxSize(uint32_t maxSize);
152 /**
153 * Returns the maximum size of the cache in bytes.
154 */
155 uint32_t getMaxSize();
156 /**
157 * Returns the current size of the cache in bytes.
158 */
159 uint32_t getSize();
160
161private:
Romain Guy25dc3a72010-12-10 12:33:05 -0800162 void init();
163
Romain Guy059e12c2012-11-28 17:35:51 -0800164 LruCache<ShadowText, ShadowTexture*> mCache;
Romain Guy1e45aae2010-08-13 19:39:53 -0700165
166 uint32_t mSize;
167 uint32_t mMaxSize;
168 FontRenderer* mRenderer;
Romain Guy25dc3a72010-12-10 12:33:05 -0800169 bool mDebugEnabled;
Romain Guy1e45aae2010-08-13 19:39:53 -0700170}; // class TextDropShadowCache
171
172}; // namespace uirenderer
173}; // namespace android
174
Romain Guy5b3b3522010-10-27 18:57:51 -0700175#endif // ANDROID_HWUI_TEXT_DROP_SHADOW_CACHE_H