blob: bae0c49349f662cb182cd9e2581eb5dc15206a96 [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 Guy321dce62011-03-01 11:45:33 -080024#include <utils/String16.h>
Romain Guy25dc3a72010-12-10 12:33:05 -080025
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
Raph Levien416a8472012-07-19 22:48:17 -070038 ShadowText(SkPaint* paint, uint32_t radius, uint32_t len, const char* srcText,
39 const float* positions):
40 radius(radius), len(len), 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
52 const float skewX = paint->getTextSkewX();
53 italicStyle = *(uint32_t*) &skewX;
54
55 const float scaleXFloat = paint->getTextScaleX();
56 scaleX = *(uint32_t*) &scaleXFloat;
Romain Guy1e45aae2010-08-13 19:39:53 -070057 }
58
Romain Guy1e45aae2010-08-13 19:39:53 -070059 ~ShadowText() {
Romain Guy1e45aae2010-08-13 19:39:53 -070060 }
61
Romain Guy1e45aae2010-08-13 19:39:53 -070062 uint32_t radius;
63 uint32_t len;
Romain Guyc4d8eb62010-08-18 20:48:33 -070064 float textSize;
65 SkTypeface* typeface;
Romain Guycabfcc12011-03-07 18:06:46 -080066 uint32_t flags;
67 uint32_t italicStyle;
68 uint32_t scaleX;
Romain Guy321dce62011-03-01 11:45:33 -080069 const char16_t* text;
Raph Levien416a8472012-07-19 22:48:17 -070070 const float* positions;
Romain Guy321dce62011-03-01 11:45:33 -080071 String16 str;
Raph Levien416a8472012-07-19 22:48:17 -070072 Vector<float> positionsCopy;
Romain Guy321dce62011-03-01 11:45:33 -080073
74 void copyTextLocally() {
75 str.setTo((const char16_t*) text, len >> 1);
76 text = str.string();
Raph Levien416a8472012-07-19 22:48:17 -070077 if (positions != NULL) {
78 positionsCopy.clear();
79 positionsCopy.appendArray(positions, len);
80 positions = positionsCopy.array();
81 }
Romain Guy321dce62011-03-01 11:45:33 -080082 }
Romain Guy1e45aae2010-08-13 19:39:53 -070083
84 bool operator<(const ShadowText& rhs) const {
Romain Guy2fc941e2011-02-03 15:06:05 -080085 LTE_INT(len) {
86 LTE_INT(radius) {
87 LTE_FLOAT(textSize) {
Romain Guy321dce62011-03-01 11:45:33 -080088 LTE_INT(typeface) {
Romain Guycabfcc12011-03-07 18:06:46 -080089 LTE_INT(flags) {
90 LTE_INT(italicStyle) {
91 LTE_INT(scaleX) {
Raph Levien416a8472012-07-19 22:48:17 -070092 int cmp = memcmp(text, rhs.text, len);
93 if (cmp < 0) return true;
94 if (cmp == 0 && rhs.positions != NULL) {
95 if (positions == NULL) return true;
96 return memcmp(positions, rhs.positions, len << 2) < 0;
97 }
Romain Guycabfcc12011-03-07 18:06:46 -080098 }
99 }
100 }
Romain Guy1e45aae2010-08-13 19:39:53 -0700101 }
102 }
103 }
104 }
105 return false;
106 }
107}; // struct ShadowText
108
109/**
110 * Alpha texture used to represent a shadow.
111 */
112struct ShadowTexture: public Texture {
113 ShadowTexture(): Texture() {
114 }
115
116 float left;
117 float top;
118}; // struct ShadowTexture
119
120class TextDropShadowCache: public OnEntryRemoved<ShadowText, ShadowTexture*> {
121public:
Romain Guyfb8b7632010-08-23 21:05:08 -0700122 TextDropShadowCache();
Romain Guy1e45aae2010-08-13 19:39:53 -0700123 TextDropShadowCache(uint32_t maxByteSize);
124 ~TextDropShadowCache();
125
126 /**
127 * Used as a callback when an entry is removed from the cache.
128 * Do not invoke directly.
129 */
130 void operator()(ShadowText& text, ShadowTexture*& texture);
131
132 ShadowTexture* get(SkPaint* paint, const char* text, uint32_t len,
Raph Levien416a8472012-07-19 22:48:17 -0700133 int numGlyphs, uint32_t radius, const float* positions);
Romain Guy1e45aae2010-08-13 19:39:53 -0700134
135 /**
136 * Clears the cache. This causes all textures to be deleted.
137 */
138 void clear();
139
140 void setFontRenderer(FontRenderer& fontRenderer) {
141 mRenderer = &fontRenderer;
142 }
143
144 /**
145 * Sets the maximum size of the cache in bytes.
146 */
147 void setMaxSize(uint32_t maxSize);
148 /**
149 * Returns the maximum size of the cache in bytes.
150 */
151 uint32_t getMaxSize();
152 /**
153 * Returns the current size of the cache in bytes.
154 */
155 uint32_t getSize();
156
157private:
Romain Guy25dc3a72010-12-10 12:33:05 -0800158 void init();
159
Romain Guy1e45aae2010-08-13 19:39:53 -0700160 GenerationCache<ShadowText, ShadowTexture*> mCache;
161
162 uint32_t mSize;
163 uint32_t mMaxSize;
164 FontRenderer* mRenderer;
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