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 | 059e12c | 2012-11-28 17:35:51 -0800 | [diff] [blame] | 24 | #include <utils/LruCache.h> |
Romain Guy | 321dce6 | 2011-03-01 11:45:33 -0800 | [diff] [blame] | 25 | #include <utils/String16.h> |
Romain Guy | 25dc3a7 | 2010-12-10 12:33:05 -0800 | [diff] [blame] | 26 | |
Romain Guy | 1e45aae | 2010-08-13 19:39:53 -0700 | [diff] [blame] | 27 | #include "FontRenderer.h" |
| 28 | #include "Texture.h" |
| 29 | |
| 30 | namespace android { |
| 31 | namespace uirenderer { |
| 32 | |
| 33 | struct ShadowText { |
Romain Guy | 059e12c | 2012-11-28 17:35:51 -0800 | [diff] [blame] | 34 | ShadowText(): len(0), radius(0.0f), textSize(0.0f), typeface(NULL), |
| 35 | flags(0), italicStyle(0.0f), scaleX(0), text(NULL), positions(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 | |
Romain Guy | 69fcbcc | 2012-11-30 15:29:15 -0800 | [diff] [blame] | 38 | // len is the number of bytes in text |
Romain Guy | 059e12c | 2012-11-28 17:35:51 -0800 | [diff] [blame] | 39 | ShadowText(SkPaint* paint, float radius, uint32_t len, const char* srcText, |
Raph Levien | 416a847 | 2012-07-19 22:48:17 -0700 | [diff] [blame] | 40 | const float* positions): |
Romain Guy | 059e12c | 2012-11-28 17:35:51 -0800 | [diff] [blame] | 41 | len(len), radius(radius), positions(positions) { |
Romain Guy | 321dce6 | 2011-03-01 11:45:33 -0800 | [diff] [blame] | 42 | // TODO: Propagate this through the API, we should not cast here |
| 43 | text = (const char16_t*) srcText; |
Romain Guy | 1e45aae | 2010-08-13 19:39:53 -0700 | [diff] [blame] | 44 | |
Romain Guy | c4d8eb6 | 2010-08-18 20:48:33 -0700 | [diff] [blame] | 45 | textSize = paint->getTextSize(); |
| 46 | typeface = paint->getTypeface(); |
Romain Guy | cabfcc1 | 2011-03-07 18:06:46 -0800 | [diff] [blame] | 47 | |
| 48 | flags = 0; |
| 49 | if (paint->isFakeBoldText()) { |
| 50 | flags |= Font::kFakeBold; |
| 51 | } |
| 52 | |
Romain Guy | 059e12c | 2012-11-28 17:35:51 -0800 | [diff] [blame] | 53 | italicStyle = paint->getTextSkewX(); |
| 54 | scaleX = paint->getTextScaleX(); |
Romain Guy | 1e45aae | 2010-08-13 19:39:53 -0700 | [diff] [blame] | 55 | } |
| 56 | |
Romain Guy | 1e45aae | 2010-08-13 19:39:53 -0700 | [diff] [blame] | 57 | ~ShadowText() { |
Romain Guy | 1e45aae | 2010-08-13 19:39:53 -0700 | [diff] [blame] | 58 | } |
| 59 | |
Romain Guy | 059e12c | 2012-11-28 17:35:51 -0800 | [diff] [blame] | 60 | 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 Guy | 321dce6 | 2011-03-01 11:45:33 -0800 | [diff] [blame] | 71 | |
| 72 | void copyTextLocally() { |
Romain Guy | 69fcbcc | 2012-11-30 15:29:15 -0800 | [diff] [blame] | 73 | uint32_t charCount = len / sizeof(char16_t); |
| 74 | str.setTo((const char16_t*) text, charCount); |
Romain Guy | 321dce6 | 2011-03-01 11:45:33 -0800 | [diff] [blame] | 75 | text = str.string(); |
Raph Levien | 416a847 | 2012-07-19 22:48:17 -0700 | [diff] [blame] | 76 | if (positions != NULL) { |
| 77 | positionsCopy.clear(); |
Romain Guy | 69fcbcc | 2012-11-30 15:29:15 -0800 | [diff] [blame] | 78 | positionsCopy.appendArray(positions, charCount * 2); |
Raph Levien | 416a847 | 2012-07-19 22:48:17 -0700 | [diff] [blame] | 79 | positions = positionsCopy.array(); |
| 80 | } |
Romain Guy | 321dce6 | 2011-03-01 11:45:33 -0800 | [diff] [blame] | 81 | } |
Romain Guy | 1e45aae | 2010-08-13 19:39:53 -0700 | [diff] [blame] | 82 | |
Romain Guy | 059e12c | 2012-11-28 17:35:51 -0800 | [diff] [blame] | 83 | 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 Guy | 1e45aae | 2010-08-13 19:39:53 -0700 | [diff] [blame] | 97 | }; // struct ShadowText |
| 98 | |
Romain Guy | 059e12c | 2012-11-28 17:35:51 -0800 | [diff] [blame] | 99 | // Caching support |
| 100 | |
| 101 | inline int strictly_order_type(const ShadowText& lhs, const ShadowText& rhs) { |
| 102 | return ShadowText::compare(lhs, rhs) < 0; |
| 103 | } |
| 104 | |
| 105 | inline int compare_type(const ShadowText& lhs, const ShadowText& rhs) { |
| 106 | return ShadowText::compare(lhs, rhs); |
| 107 | } |
| 108 | |
| 109 | inline hash_t hash_type(const ShadowText& entry) { |
| 110 | return entry.hash(); |
| 111 | } |
| 112 | |
Romain Guy | 1e45aae | 2010-08-13 19:39:53 -0700 | [diff] [blame] | 113 | /** |
| 114 | * Alpha texture used to represent a shadow. |
| 115 | */ |
| 116 | struct ShadowTexture: public Texture { |
| 117 | ShadowTexture(): Texture() { |
| 118 | } |
| 119 | |
| 120 | float left; |
| 121 | float top; |
| 122 | }; // struct ShadowTexture |
| 123 | |
| 124 | class TextDropShadowCache: public OnEntryRemoved<ShadowText, ShadowTexture*> { |
| 125 | public: |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 126 | TextDropShadowCache(); |
Romain Guy | 1e45aae | 2010-08-13 19:39:53 -0700 | [diff] [blame] | 127 | 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 Guy | 059e12c | 2012-11-28 17:35:51 -0800 | [diff] [blame] | 137 | int numGlyphs, float radius, const float* positions); |
Romain Guy | 1e45aae | 2010-08-13 19:39:53 -0700 | [diff] [blame] | 138 | |
| 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 | |
| 161 | private: |
Romain Guy | 25dc3a7 | 2010-12-10 12:33:05 -0800 | [diff] [blame] | 162 | void init(); |
| 163 | |
Romain Guy | 059e12c | 2012-11-28 17:35:51 -0800 | [diff] [blame] | 164 | LruCache<ShadowText, ShadowTexture*> mCache; |
Romain Guy | 1e45aae | 2010-08-13 19:39:53 -0700 | [diff] [blame] | 165 | |
| 166 | uint32_t mSize; |
| 167 | uint32_t mMaxSize; |
| 168 | FontRenderer* mRenderer; |
Romain Guy | 25dc3a7 | 2010-12-10 12:33:05 -0800 | [diff] [blame] | 169 | bool mDebugEnabled; |
Romain Guy | 1e45aae | 2010-08-13 19:39:53 -0700 | [diff] [blame] | 170 | }; // class TextDropShadowCache |
| 171 | |
| 172 | }; // namespace uirenderer |
| 173 | }; // namespace android |
| 174 | |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 175 | #endif // ANDROID_HWUI_TEXT_DROP_SHADOW_CACHE_H |