blob: ffccfa25732472a7975034daa3880321c3c837e3 [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 Guy25dc3a72010-12-10 12:33:05 -080024#include <utils/String8.h>
25
26#include "utils/Compare.h"
Romain Guy21b028a2010-10-08 18:43:58 -070027#include "utils/GenerationCache.h"
Romain Guy1e45aae2010-08-13 19:39:53 -070028#include "FontRenderer.h"
29#include "Texture.h"
30
31namespace android {
32namespace uirenderer {
33
34struct ShadowText {
Romain Guy2fc941e2011-02-03 15:06:05 -080035 ShadowText(): radius(0), len(0), textSize(0.0f), typeface(NULL) {
Romain Guyc4d8eb62010-08-18 20:48:33 -070036 }
Romain Guy1e45aae2010-08-13 19:39:53 -070037
38 ShadowText(SkPaint* paint, uint32_t radius, uint32_t len, const char* srcText):
Romain Guyc4d8eb62010-08-18 20:48:33 -070039 radius(radius), len(len) {
Romain Guy25dc3a72010-12-10 12:33:05 -080040 // The source text we receive is in UTF-16, convert to UTF-8
41 str.setTo((const char16_t*) srcText, len >> 1);
Romain Guy1e45aae2010-08-13 19:39:53 -070042
Romain Guyc4d8eb62010-08-18 20:48:33 -070043 textSize = paint->getTextSize();
44 typeface = paint->getTypeface();
Romain Guy1e45aae2010-08-13 19:39:53 -070045 }
46
47 ShadowText(const ShadowText& shadow):
Romain Guy2fc941e2011-02-03 15:06:05 -080048 radius(shadow.radius), len(shadow.len), textSize(shadow.textSize),
49 typeface(shadow.typeface), str(shadow.str) {
Romain Guy1e45aae2010-08-13 19:39:53 -070050 }
51
52 ~ShadowText() {
Romain Guy1e45aae2010-08-13 19:39:53 -070053 }
54
Romain Guy1e45aae2010-08-13 19:39:53 -070055 uint32_t radius;
56 uint32_t len;
Romain Guyc4d8eb62010-08-18 20:48:33 -070057 float textSize;
58 SkTypeface* typeface;
Romain Guy25dc3a72010-12-10 12:33:05 -080059 String8 str;
Romain Guy1e45aae2010-08-13 19:39:53 -070060
61 bool operator<(const ShadowText& rhs) const {
Romain Guy2fc941e2011-02-03 15:06:05 -080062 LTE_INT(len) {
63 LTE_INT(radius) {
64 LTE_FLOAT(textSize) {
65 if (typeface < rhs.typeface) return true;
66 else if (typeface == rhs.typeface) {
67 return str.compare(rhs.str) < 0;
Romain Guy1e45aae2010-08-13 19:39:53 -070068 }
69 }
70 }
71 }
72 return false;
73 }
74}; // struct ShadowText
75
76/**
77 * Alpha texture used to represent a shadow.
78 */
79struct ShadowTexture: public Texture {
80 ShadowTexture(): Texture() {
81 }
82
83 float left;
84 float top;
85}; // struct ShadowTexture
86
87class TextDropShadowCache: public OnEntryRemoved<ShadowText, ShadowTexture*> {
88public:
Romain Guyfb8b7632010-08-23 21:05:08 -070089 TextDropShadowCache();
Romain Guy1e45aae2010-08-13 19:39:53 -070090 TextDropShadowCache(uint32_t maxByteSize);
91 ~TextDropShadowCache();
92
93 /**
94 * Used as a callback when an entry is removed from the cache.
95 * Do not invoke directly.
96 */
97 void operator()(ShadowText& text, ShadowTexture*& texture);
98
99 ShadowTexture* get(SkPaint* paint, const char* text, uint32_t len,
100 int numGlyphs, uint32_t radius);
101
102 /**
103 * Clears the cache. This causes all textures to be deleted.
104 */
105 void clear();
106
107 void setFontRenderer(FontRenderer& fontRenderer) {
108 mRenderer = &fontRenderer;
109 }
110
111 /**
112 * Sets the maximum size of the cache in bytes.
113 */
114 void setMaxSize(uint32_t maxSize);
115 /**
116 * Returns the maximum size of the cache in bytes.
117 */
118 uint32_t getMaxSize();
119 /**
120 * Returns the current size of the cache in bytes.
121 */
122 uint32_t getSize();
123
124private:
Romain Guy25dc3a72010-12-10 12:33:05 -0800125 void init();
126
Romain Guy1e45aae2010-08-13 19:39:53 -0700127 GenerationCache<ShadowText, ShadowTexture*> mCache;
128
129 uint32_t mSize;
130 uint32_t mMaxSize;
131 FontRenderer* mRenderer;
Romain Guy25dc3a72010-12-10 12:33:05 -0800132 bool mDebugEnabled;
Romain Guy1e45aae2010-08-13 19:39:53 -0700133}; // class TextDropShadowCache
134
135}; // namespace uirenderer
136}; // namespace android
137
Romain Guy5b3b3522010-10-27 18:57:51 -0700138#endif // ANDROID_HWUI_TEXT_DROP_SHADOW_CACHE_H