blob: 16e28146fdd770ff3f9eca8aee571a1e1caf998e [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
17#ifndef ANDROID_UI_TEXT_DROP_SHADOW_CACHE_H
18#define ANDROID_UI_TEXT_DROP_SHADOW_CACHE_H
19
20#include <GLES2/gl2.h>
21
22#include <SkPaint.h>
23
Romain Guy21b028a2010-10-08 18:43:58 -070024#include "utils/GenerationCache.h"
Romain Guy1e45aae2010-08-13 19:39:53 -070025#include "FontRenderer.h"
26#include "Texture.h"
27
28namespace android {
29namespace uirenderer {
30
31struct ShadowText {
Romain Guyc4d8eb62010-08-18 20:48:33 -070032 ShadowText() {
33 text = NULL;
34 }
Romain Guy1e45aae2010-08-13 19:39:53 -070035
36 ShadowText(SkPaint* paint, uint32_t radius, uint32_t len, const char* srcText):
Romain Guyc4d8eb62010-08-18 20:48:33 -070037 radius(radius), len(len) {
Romain Guy1e45aae2010-08-13 19:39:53 -070038 text = new char[len];
39 memcpy(text, srcText, len);
40
Romain Guyc4d8eb62010-08-18 20:48:33 -070041 textSize = paint->getTextSize();
42 typeface = paint->getTypeface();
43
Romain Guy1e45aae2010-08-13 19:39:53 -070044 hash = 0;
45 uint32_t multiplier = 1;
46 for (uint32_t i = 0; i < len; i++) {
47 hash += text[i] * multiplier;
48 uint32_t shifted = multiplier << 5;
49 multiplier = shifted - multiplier;
50 }
51 }
52
53 ShadowText(const ShadowText& shadow):
Romain Guyc4d8eb62010-08-18 20:48:33 -070054 radius(shadow.radius), len(shadow.len), hash(shadow.hash),
55 textSize(shadow.textSize), typeface(shadow.typeface) {
Romain Guy1d83e192010-08-17 11:37:00 -070056 text = new char[shadow.len];
Romain Guy1e45aae2010-08-13 19:39:53 -070057 memcpy(text, shadow.text, shadow.len);
58 }
59
60 ~ShadowText() {
61 delete[] text;
62 }
63
Romain Guy1e45aae2010-08-13 19:39:53 -070064 uint32_t radius;
65 uint32_t len;
66 uint32_t hash;
Romain Guyc4d8eb62010-08-18 20:48:33 -070067 float textSize;
68 SkTypeface* typeface;
Romain Guy1e45aae2010-08-13 19:39:53 -070069 char *text;
70
71 bool operator<(const ShadowText& rhs) const {
Romain Guyfb8b7632010-08-23 21:05:08 -070072 if (hash < rhs.hash) return true;
73 else if (hash == rhs.hash) {
74 if (len < rhs.len) return true;
75 else if (len == rhs.len) {
76 if (radius < rhs.radius) return true;
77 else if (radius == rhs.radius) {
78 if (textSize < rhs.textSize) return true;
79 else if (textSize == rhs.textSize) {
80 if (typeface < rhs.typeface) return true;
81 else if (typeface == rhs.typeface) {
Romain Guyc4d8eb62010-08-18 20:48:33 -070082 return strncmp(text, rhs.text, len) < 0;
83 }
Romain Guy1e45aae2010-08-13 19:39:53 -070084 }
85 }
86 }
87 }
88 return false;
89 }
90}; // struct ShadowText
91
92/**
93 * Alpha texture used to represent a shadow.
94 */
95struct ShadowTexture: public Texture {
96 ShadowTexture(): Texture() {
97 }
98
99 float left;
100 float top;
101}; // struct ShadowTexture
102
103class TextDropShadowCache: public OnEntryRemoved<ShadowText, ShadowTexture*> {
104public:
Romain Guyfb8b7632010-08-23 21:05:08 -0700105 TextDropShadowCache();
Romain Guy1e45aae2010-08-13 19:39:53 -0700106 TextDropShadowCache(uint32_t maxByteSize);
107 ~TextDropShadowCache();
108
109 /**
110 * Used as a callback when an entry is removed from the cache.
111 * Do not invoke directly.
112 */
113 void operator()(ShadowText& text, ShadowTexture*& texture);
114
115 ShadowTexture* get(SkPaint* paint, const char* text, uint32_t len,
116 int numGlyphs, uint32_t radius);
117
118 /**
119 * Clears the cache. This causes all textures to be deleted.
120 */
121 void clear();
122
123 void setFontRenderer(FontRenderer& fontRenderer) {
124 mRenderer = &fontRenderer;
125 }
126
127 /**
128 * Sets the maximum size of the cache in bytes.
129 */
130 void setMaxSize(uint32_t maxSize);
131 /**
132 * Returns the maximum size of the cache in bytes.
133 */
134 uint32_t getMaxSize();
135 /**
136 * Returns the current size of the cache in bytes.
137 */
138 uint32_t getSize();
139
140private:
141 GenerationCache<ShadowText, ShadowTexture*> mCache;
142
143 uint32_t mSize;
144 uint32_t mMaxSize;
145 FontRenderer* mRenderer;
146}; // class TextDropShadowCache
147
148}; // namespace uirenderer
149}; // namespace android
150
151#endif // ANDROID_UI_TEXT_DROP_SHADOW_CACHE_H