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 | |
| 17 | #ifndef ANDROID_UI_TEXT_DROP_SHADOW_CACHE_H |
| 18 | #define ANDROID_UI_TEXT_DROP_SHADOW_CACHE_H |
| 19 | |
| 20 | #include <GLES2/gl2.h> |
| 21 | |
| 22 | #include <SkPaint.h> |
| 23 | |
| 24 | #include "GenerationCache.h" |
| 25 | #include "FontRenderer.h" |
| 26 | #include "Texture.h" |
| 27 | |
| 28 | namespace android { |
| 29 | namespace uirenderer { |
| 30 | |
| 31 | struct ShadowText { |
| 32 | ShadowText() { } |
| 33 | |
| 34 | ShadowText(SkPaint* paint, uint32_t radius, uint32_t len, const char* srcText): |
| 35 | paint(paint), radius(radius), len(len) { |
| 36 | text = new char[len]; |
| 37 | memcpy(text, srcText, len); |
| 38 | |
| 39 | hash = 0; |
| 40 | uint32_t multiplier = 1; |
| 41 | for (uint32_t i = 0; i < len; i++) { |
| 42 | hash += text[i] * multiplier; |
| 43 | uint32_t shifted = multiplier << 5; |
| 44 | multiplier = shifted - multiplier; |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | ShadowText(const ShadowText& shadow): |
| 49 | paint(shadow.paint), radius(shadow.radius), len(shadow.len), hash(shadow.hash) { |
Romain Guy | 1d83e19 | 2010-08-17 11:37:00 -0700 | [diff] [blame] | 50 | text = new char[shadow.len]; |
Romain Guy | 1e45aae | 2010-08-13 19:39:53 -0700 | [diff] [blame] | 51 | memcpy(text, shadow.text, shadow.len); |
| 52 | } |
| 53 | |
| 54 | ~ShadowText() { |
| 55 | delete[] text; |
| 56 | } |
| 57 | |
| 58 | SkPaint* paint; |
| 59 | uint32_t radius; |
| 60 | uint32_t len; |
| 61 | uint32_t hash; |
| 62 | char *text; |
| 63 | |
| 64 | bool operator<(const ShadowText& rhs) const { |
| 65 | if (len < rhs.len) return true; |
| 66 | else if (len == rhs.len) { |
| 67 | if (radius < rhs.radius) return true; |
| 68 | else if (radius == rhs.radius) { |
| 69 | if (paint < rhs.paint) return true; |
| 70 | else if (paint == rhs.paint) { |
| 71 | if (hash < rhs.hash) return true; |
| 72 | if (hash == rhs.hash) { |
| 73 | return strncmp(text, rhs.text, len) < 0; |
| 74 | } |
| 75 | } |
| 76 | } |
| 77 | } |
| 78 | return false; |
| 79 | } |
| 80 | }; // struct ShadowText |
| 81 | |
| 82 | /** |
| 83 | * Alpha texture used to represent a shadow. |
| 84 | */ |
| 85 | struct ShadowTexture: public Texture { |
| 86 | ShadowTexture(): Texture() { |
| 87 | } |
| 88 | |
| 89 | float left; |
| 90 | float top; |
| 91 | }; // struct ShadowTexture |
| 92 | |
| 93 | class TextDropShadowCache: public OnEntryRemoved<ShadowText, ShadowTexture*> { |
| 94 | public: |
| 95 | TextDropShadowCache(uint32_t maxByteSize); |
| 96 | ~TextDropShadowCache(); |
| 97 | |
| 98 | /** |
| 99 | * Used as a callback when an entry is removed from the cache. |
| 100 | * Do not invoke directly. |
| 101 | */ |
| 102 | void operator()(ShadowText& text, ShadowTexture*& texture); |
| 103 | |
| 104 | ShadowTexture* get(SkPaint* paint, const char* text, uint32_t len, |
| 105 | int numGlyphs, uint32_t radius); |
| 106 | |
| 107 | /** |
| 108 | * Clears the cache. This causes all textures to be deleted. |
| 109 | */ |
| 110 | void clear(); |
| 111 | |
| 112 | void setFontRenderer(FontRenderer& fontRenderer) { |
| 113 | mRenderer = &fontRenderer; |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * Sets the maximum size of the cache in bytes. |
| 118 | */ |
| 119 | void setMaxSize(uint32_t maxSize); |
| 120 | /** |
| 121 | * Returns the maximum size of the cache in bytes. |
| 122 | */ |
| 123 | uint32_t getMaxSize(); |
| 124 | /** |
| 125 | * Returns the current size of the cache in bytes. |
| 126 | */ |
| 127 | uint32_t getSize(); |
| 128 | |
| 129 | private: |
| 130 | GenerationCache<ShadowText, ShadowTexture*> mCache; |
| 131 | |
| 132 | uint32_t mSize; |
| 133 | uint32_t mMaxSize; |
| 134 | FontRenderer* mRenderer; |
| 135 | }; // class TextDropShadowCache |
| 136 | |
| 137 | }; // namespace uirenderer |
| 138 | }; // namespace android |
| 139 | |
| 140 | #endif // ANDROID_UI_TEXT_DROP_SHADOW_CACHE_H |