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 | 25dc3a7 | 2010-12-10 12:33:05 -0800 | [diff] [blame^] | 24 | #include <utils/String8.h> |
| 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 | 25dc3a7 | 2010-12-10 12:33:05 -0800 | [diff] [blame^] | 35 | ShadowText(): radius(0), len(0), hash(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 | |
| 38 | ShadowText(SkPaint* paint, uint32_t radius, uint32_t len, const char* srcText): |
Romain Guy | c4d8eb6 | 2010-08-18 20:48:33 -0700 | [diff] [blame] | 39 | radius(radius), len(len) { |
Romain Guy | 25dc3a7 | 2010-12-10 12:33:05 -0800 | [diff] [blame^] | 40 | // The source text we receive is in UTF-16, convert to UTF-8 |
| 41 | str.setTo((const char16_t*) srcText, len >> 1); |
Romain Guy | 1e45aae | 2010-08-13 19:39:53 -0700 | [diff] [blame] | 42 | |
Romain Guy | c4d8eb6 | 2010-08-18 20:48:33 -0700 | [diff] [blame] | 43 | textSize = paint->getTextSize(); |
| 44 | typeface = paint->getTypeface(); |
| 45 | |
Romain Guy | 1e45aae | 2010-08-13 19:39:53 -0700 | [diff] [blame] | 46 | hash = 0; |
| 47 | uint32_t multiplier = 1; |
Romain Guy | 25dc3a7 | 2010-12-10 12:33:05 -0800 | [diff] [blame^] | 48 | const char* text = str.string(); |
Romain Guy | 1e45aae | 2010-08-13 19:39:53 -0700 | [diff] [blame] | 49 | for (uint32_t i = 0; i < len; i++) { |
| 50 | hash += text[i] * multiplier; |
| 51 | uint32_t shifted = multiplier << 5; |
| 52 | multiplier = shifted - multiplier; |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | ShadowText(const ShadowText& shadow): |
Romain Guy | c4d8eb6 | 2010-08-18 20:48:33 -0700 | [diff] [blame] | 57 | radius(shadow.radius), len(shadow.len), hash(shadow.hash), |
Romain Guy | 25dc3a7 | 2010-12-10 12:33:05 -0800 | [diff] [blame^] | 58 | textSize(shadow.textSize), typeface(shadow.typeface), str(shadow.str) { |
Romain Guy | 1e45aae | 2010-08-13 19:39:53 -0700 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | ~ShadowText() { |
Romain Guy | 1e45aae | 2010-08-13 19:39:53 -0700 | [diff] [blame] | 62 | } |
| 63 | |
Romain Guy | 1e45aae | 2010-08-13 19:39:53 -0700 | [diff] [blame] | 64 | uint32_t radius; |
| 65 | uint32_t len; |
| 66 | uint32_t hash; |
Romain Guy | c4d8eb6 | 2010-08-18 20:48:33 -0700 | [diff] [blame] | 67 | float textSize; |
| 68 | SkTypeface* typeface; |
Romain Guy | 25dc3a7 | 2010-12-10 12:33:05 -0800 | [diff] [blame^] | 69 | String8 str; |
Romain Guy | 1e45aae | 2010-08-13 19:39:53 -0700 | [diff] [blame] | 70 | |
| 71 | bool operator<(const ShadowText& rhs) const { |
Romain Guy | 25dc3a7 | 2010-12-10 12:33:05 -0800 | [diff] [blame^] | 72 | LTE_INT(hash) { |
| 73 | LTE_INT(len) { |
| 74 | LTE_INT(radius) { |
| 75 | LTE_FLOAT(textSize) { |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 76 | if (typeface < rhs.typeface) return true; |
| 77 | else if (typeface == rhs.typeface) { |
Romain Guy | 25dc3a7 | 2010-12-10 12:33:05 -0800 | [diff] [blame^] | 78 | return str.compare(rhs.str) < 0; |
Romain Guy | c4d8eb6 | 2010-08-18 20:48:33 -0700 | [diff] [blame] | 79 | } |
Romain Guy | 1e45aae | 2010-08-13 19:39:53 -0700 | [diff] [blame] | 80 | } |
| 81 | } |
| 82 | } |
| 83 | } |
| 84 | return false; |
| 85 | } |
| 86 | }; // struct ShadowText |
| 87 | |
| 88 | /** |
| 89 | * Alpha texture used to represent a shadow. |
| 90 | */ |
| 91 | struct ShadowTexture: public Texture { |
| 92 | ShadowTexture(): Texture() { |
| 93 | } |
| 94 | |
| 95 | float left; |
| 96 | float top; |
| 97 | }; // struct ShadowTexture |
| 98 | |
| 99 | class TextDropShadowCache: public OnEntryRemoved<ShadowText, ShadowTexture*> { |
| 100 | public: |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 101 | TextDropShadowCache(); |
Romain Guy | 1e45aae | 2010-08-13 19:39:53 -0700 | [diff] [blame] | 102 | TextDropShadowCache(uint32_t maxByteSize); |
| 103 | ~TextDropShadowCache(); |
| 104 | |
| 105 | /** |
| 106 | * Used as a callback when an entry is removed from the cache. |
| 107 | * Do not invoke directly. |
| 108 | */ |
| 109 | void operator()(ShadowText& text, ShadowTexture*& texture); |
| 110 | |
| 111 | ShadowTexture* get(SkPaint* paint, const char* text, uint32_t len, |
| 112 | int numGlyphs, uint32_t radius); |
| 113 | |
| 114 | /** |
| 115 | * Clears the cache. This causes all textures to be deleted. |
| 116 | */ |
| 117 | void clear(); |
| 118 | |
| 119 | void setFontRenderer(FontRenderer& fontRenderer) { |
| 120 | mRenderer = &fontRenderer; |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * Sets the maximum size of the cache in bytes. |
| 125 | */ |
| 126 | void setMaxSize(uint32_t maxSize); |
| 127 | /** |
| 128 | * Returns the maximum size of the cache in bytes. |
| 129 | */ |
| 130 | uint32_t getMaxSize(); |
| 131 | /** |
| 132 | * Returns the current size of the cache in bytes. |
| 133 | */ |
| 134 | uint32_t getSize(); |
| 135 | |
| 136 | private: |
Romain Guy | 25dc3a7 | 2010-12-10 12:33:05 -0800 | [diff] [blame^] | 137 | void init(); |
| 138 | |
Romain Guy | 1e45aae | 2010-08-13 19:39:53 -0700 | [diff] [blame] | 139 | GenerationCache<ShadowText, ShadowTexture*> mCache; |
| 140 | |
| 141 | uint32_t mSize; |
| 142 | uint32_t mMaxSize; |
| 143 | FontRenderer* mRenderer; |
Romain Guy | 25dc3a7 | 2010-12-10 12:33:05 -0800 | [diff] [blame^] | 144 | bool mDebugEnabled; |
Romain Guy | 1e45aae | 2010-08-13 19:39:53 -0700 | [diff] [blame] | 145 | }; // class TextDropShadowCache |
| 146 | |
| 147 | }; // namespace uirenderer |
| 148 | }; // namespace android |
| 149 | |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 150 | #endif // ANDROID_HWUI_TEXT_DROP_SHADOW_CACHE_H |