Romain Guy | 1e45aae | 2010-08-13 19:39:53 -0700 | [diff] [blame] | 1 | /* |
| 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 Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 17 | #ifndef ANDROID_HWUI_TEXT_DROP_SHADOW_CACHE_H |
| 18 | #define ANDROID_HWUI_TEXT_DROP_SHADOW_CACHE_H |
Romain Guy | 1e45aae | 2010-08-13 19:39:53 -0700 | [diff] [blame] | 19 | |
| 20 | #include <GLES2/gl2.h> |
| 21 | |
| 22 | #include <SkPaint.h> |
| 23 | |
Romain Guy | 321dce6 | 2011-03-01 11:45:33 -0800 | [diff] [blame] | 24 | #include <utils/String16.h> |
Romain Guy | 25dc3a7 | 2010-12-10 12:33:05 -0800 | [diff] [blame] | 25 | |
| 26 | #include "utils/Compare.h" |
Romain Guy | 21b028a | 2010-10-08 18:43:58 -0700 | [diff] [blame] | 27 | #include "utils/GenerationCache.h" |
Romain Guy | 1e45aae | 2010-08-13 19:39:53 -0700 | [diff] [blame] | 28 | #include "FontRenderer.h" |
| 29 | #include "Texture.h" |
| 30 | |
| 31 | namespace android { |
| 32 | namespace uirenderer { |
| 33 | |
| 34 | struct ShadowText { |
Romain Guy | 2fc941e | 2011-02-03 15:06:05 -0800 | [diff] [blame] | 35 | ShadowText(): radius(0), len(0), textSize(0.0f), typeface(NULL) { |
Romain Guy | c4d8eb6 | 2010-08-18 20:48:33 -0700 | [diff] [blame] | 36 | } |
Romain Guy | 1e45aae | 2010-08-13 19:39:53 -0700 | [diff] [blame] | 37 | |
Raph Levien | 416a847 | 2012-07-19 22:48:17 -0700 | [diff] [blame] | 38 | ShadowText(SkPaint* paint, uint32_t radius, uint32_t len, const char* srcText, |
| 39 | const float* positions): |
| 40 | radius(radius), len(len), positions(positions) { |
Romain Guy | 321dce6 | 2011-03-01 11:45:33 -0800 | [diff] [blame] | 41 | // TODO: Propagate this through the API, we should not cast here |
| 42 | text = (const char16_t*) srcText; |
Romain Guy | 1e45aae | 2010-08-13 19:39:53 -0700 | [diff] [blame] | 43 | |
Romain Guy | c4d8eb6 | 2010-08-18 20:48:33 -0700 | [diff] [blame] | 44 | textSize = paint->getTextSize(); |
| 45 | typeface = paint->getTypeface(); |
Romain Guy | cabfcc1 | 2011-03-07 18:06:46 -0800 | [diff] [blame] | 46 | |
| 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 Guy | 1e45aae | 2010-08-13 19:39:53 -0700 | [diff] [blame] | 57 | } |
| 58 | |
Romain Guy | 1e45aae | 2010-08-13 19:39:53 -0700 | [diff] [blame] | 59 | ~ShadowText() { |
Romain Guy | 1e45aae | 2010-08-13 19:39:53 -0700 | [diff] [blame] | 60 | } |
| 61 | |
Romain Guy | 1e45aae | 2010-08-13 19:39:53 -0700 | [diff] [blame] | 62 | uint32_t radius; |
| 63 | uint32_t len; |
Romain Guy | c4d8eb6 | 2010-08-18 20:48:33 -0700 | [diff] [blame] | 64 | float textSize; |
| 65 | SkTypeface* typeface; |
Romain Guy | cabfcc1 | 2011-03-07 18:06:46 -0800 | [diff] [blame] | 66 | uint32_t flags; |
| 67 | uint32_t italicStyle; |
| 68 | uint32_t scaleX; |
Romain Guy | 321dce6 | 2011-03-01 11:45:33 -0800 | [diff] [blame] | 69 | const char16_t* text; |
Raph Levien | 416a847 | 2012-07-19 22:48:17 -0700 | [diff] [blame] | 70 | const float* positions; |
Romain Guy | 321dce6 | 2011-03-01 11:45:33 -0800 | [diff] [blame] | 71 | String16 str; |
Raph Levien | 416a847 | 2012-07-19 22:48:17 -0700 | [diff] [blame] | 72 | Vector<float> positionsCopy; |
Romain Guy | 321dce6 | 2011-03-01 11:45:33 -0800 | [diff] [blame] | 73 | |
| 74 | void copyTextLocally() { |
| 75 | str.setTo((const char16_t*) text, len >> 1); |
| 76 | text = str.string(); |
Raph Levien | 416a847 | 2012-07-19 22:48:17 -0700 | [diff] [blame] | 77 | if (positions != NULL) { |
| 78 | positionsCopy.clear(); |
| 79 | positionsCopy.appendArray(positions, len); |
| 80 | positions = positionsCopy.array(); |
| 81 | } |
Romain Guy | 321dce6 | 2011-03-01 11:45:33 -0800 | [diff] [blame] | 82 | } |
Romain Guy | 1e45aae | 2010-08-13 19:39:53 -0700 | [diff] [blame] | 83 | |
| 84 | bool operator<(const ShadowText& rhs) const { |
Romain Guy | 2fc941e | 2011-02-03 15:06:05 -0800 | [diff] [blame] | 85 | LTE_INT(len) { |
| 86 | LTE_INT(radius) { |
| 87 | LTE_FLOAT(textSize) { |
Romain Guy | 321dce6 | 2011-03-01 11:45:33 -0800 | [diff] [blame] | 88 | LTE_INT(typeface) { |
Romain Guy | cabfcc1 | 2011-03-07 18:06:46 -0800 | [diff] [blame] | 89 | LTE_INT(flags) { |
| 90 | LTE_INT(italicStyle) { |
| 91 | LTE_INT(scaleX) { |
Raph Levien | 416a847 | 2012-07-19 22:48:17 -0700 | [diff] [blame] | 92 | 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 Guy | cabfcc1 | 2011-03-07 18:06:46 -0800 | [diff] [blame] | 98 | } |
| 99 | } |
| 100 | } |
Romain Guy | 1e45aae | 2010-08-13 19:39:53 -0700 | [diff] [blame] | 101 | } |
| 102 | } |
| 103 | } |
| 104 | } |
| 105 | return false; |
| 106 | } |
| 107 | }; // struct ShadowText |
| 108 | |
| 109 | /** |
| 110 | * Alpha texture used to represent a shadow. |
| 111 | */ |
| 112 | struct ShadowTexture: public Texture { |
| 113 | ShadowTexture(): Texture() { |
| 114 | } |
| 115 | |
| 116 | float left; |
| 117 | float top; |
| 118 | }; // struct ShadowTexture |
| 119 | |
| 120 | class TextDropShadowCache: public OnEntryRemoved<ShadowText, ShadowTexture*> { |
| 121 | public: |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 122 | TextDropShadowCache(); |
Romain Guy | 1e45aae | 2010-08-13 19:39:53 -0700 | [diff] [blame] | 123 | 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 Levien | 416a847 | 2012-07-19 22:48:17 -0700 | [diff] [blame] | 133 | int numGlyphs, uint32_t radius, const float* positions); |
Romain Guy | 1e45aae | 2010-08-13 19:39:53 -0700 | [diff] [blame] | 134 | |
| 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 | |
| 157 | private: |
Romain Guy | 25dc3a7 | 2010-12-10 12:33:05 -0800 | [diff] [blame] | 158 | void init(); |
| 159 | |
Romain Guy | 1e45aae | 2010-08-13 19:39:53 -0700 | [diff] [blame] | 160 | GenerationCache<ShadowText, ShadowTexture*> mCache; |
| 161 | |
| 162 | uint32_t mSize; |
| 163 | uint32_t mMaxSize; |
| 164 | FontRenderer* mRenderer; |
Romain Guy | 25dc3a7 | 2010-12-10 12:33:05 -0800 | [diff] [blame] | 165 | bool mDebugEnabled; |
Romain Guy | 1e45aae | 2010-08-13 19:39:53 -0700 | [diff] [blame] | 166 | }; // class TextDropShadowCache |
| 167 | |
| 168 | }; // namespace uirenderer |
| 169 | }; // namespace android |
| 170 | |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 171 | #endif // ANDROID_HWUI_TEXT_DROP_SHADOW_CACHE_H |