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 | |
| 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 | 321dce6 | 2011-03-01 11:45:33 -0800 | [diff] [blame] | 40 | // TODO: Propagate this through the API, we should not cast here |
| 41 | text = (const char16_t*) srcText; |
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(); |
Romain Guy | cabfcc1 | 2011-03-07 18:06:46 -0800 | [diff] [blame] | 45 | |
| 46 | flags = 0; |
| 47 | if (paint->isFakeBoldText()) { |
| 48 | flags |= Font::kFakeBold; |
| 49 | } |
| 50 | |
| 51 | const float skewX = paint->getTextSkewX(); |
| 52 | italicStyle = *(uint32_t*) &skewX; |
| 53 | |
| 54 | const float scaleXFloat = paint->getTextScaleX(); |
| 55 | scaleX = *(uint32_t*) &scaleXFloat; |
Romain Guy | 1e45aae | 2010-08-13 19:39:53 -0700 | [diff] [blame] | 56 | } |
| 57 | |
Romain Guy | 1e45aae | 2010-08-13 19:39:53 -0700 | [diff] [blame] | 58 | ~ShadowText() { |
Romain Guy | 1e45aae | 2010-08-13 19:39:53 -0700 | [diff] [blame] | 59 | } |
| 60 | |
Romain Guy | 1e45aae | 2010-08-13 19:39:53 -0700 | [diff] [blame] | 61 | uint32_t radius; |
| 62 | uint32_t len; |
Romain Guy | c4d8eb6 | 2010-08-18 20:48:33 -0700 | [diff] [blame] | 63 | float textSize; |
| 64 | SkTypeface* typeface; |
Romain Guy | cabfcc1 | 2011-03-07 18:06:46 -0800 | [diff] [blame] | 65 | uint32_t flags; |
| 66 | uint32_t italicStyle; |
| 67 | uint32_t scaleX; |
Romain Guy | 321dce6 | 2011-03-01 11:45:33 -0800 | [diff] [blame] | 68 | const char16_t* text; |
| 69 | String16 str; |
| 70 | |
| 71 | void copyTextLocally() { |
| 72 | str.setTo((const char16_t*) text, len >> 1); |
| 73 | text = str.string(); |
| 74 | } |
Romain Guy | 1e45aae | 2010-08-13 19:39:53 -0700 | [diff] [blame] | 75 | |
Romain Guy | b37cbec | 2011-02-24 17:21:29 -0800 | [diff] [blame] | 76 | // TODO: Should take into account fake bold and text skew |
Romain Guy | 1e45aae | 2010-08-13 19:39:53 -0700 | [diff] [blame] | 77 | bool operator<(const ShadowText& rhs) const { |
Romain Guy | 2fc941e | 2011-02-03 15:06:05 -0800 | [diff] [blame] | 78 | LTE_INT(len) { |
| 79 | LTE_INT(radius) { |
| 80 | LTE_FLOAT(textSize) { |
Romain Guy | 321dce6 | 2011-03-01 11:45:33 -0800 | [diff] [blame] | 81 | LTE_INT(typeface) { |
Romain Guy | cabfcc1 | 2011-03-07 18:06:46 -0800 | [diff] [blame] | 82 | LTE_INT(flags) { |
| 83 | LTE_INT(italicStyle) { |
| 84 | LTE_INT(scaleX) { |
| 85 | return strncmp16(text, rhs.text, len >> 1) < 0; |
| 86 | } |
| 87 | } |
| 88 | } |
Romain Guy | 1e45aae | 2010-08-13 19:39:53 -0700 | [diff] [blame] | 89 | } |
| 90 | } |
| 91 | } |
| 92 | } |
| 93 | return false; |
| 94 | } |
| 95 | }; // struct ShadowText |
| 96 | |
| 97 | /** |
| 98 | * Alpha texture used to represent a shadow. |
| 99 | */ |
| 100 | struct ShadowTexture: public Texture { |
| 101 | ShadowTexture(): Texture() { |
| 102 | } |
| 103 | |
| 104 | float left; |
| 105 | float top; |
| 106 | }; // struct ShadowTexture |
| 107 | |
| 108 | class TextDropShadowCache: public OnEntryRemoved<ShadowText, ShadowTexture*> { |
| 109 | public: |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 110 | TextDropShadowCache(); |
Romain Guy | 1e45aae | 2010-08-13 19:39:53 -0700 | [diff] [blame] | 111 | TextDropShadowCache(uint32_t maxByteSize); |
| 112 | ~TextDropShadowCache(); |
| 113 | |
| 114 | /** |
| 115 | * Used as a callback when an entry is removed from the cache. |
| 116 | * Do not invoke directly. |
| 117 | */ |
| 118 | void operator()(ShadowText& text, ShadowTexture*& texture); |
| 119 | |
| 120 | ShadowTexture* get(SkPaint* paint, const char* text, uint32_t len, |
| 121 | int numGlyphs, uint32_t radius); |
| 122 | |
| 123 | /** |
| 124 | * Clears the cache. This causes all textures to be deleted. |
| 125 | */ |
| 126 | void clear(); |
| 127 | |
| 128 | void setFontRenderer(FontRenderer& fontRenderer) { |
| 129 | mRenderer = &fontRenderer; |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * Sets the maximum size of the cache in bytes. |
| 134 | */ |
| 135 | void setMaxSize(uint32_t maxSize); |
| 136 | /** |
| 137 | * Returns the maximum size of the cache in bytes. |
| 138 | */ |
| 139 | uint32_t getMaxSize(); |
| 140 | /** |
| 141 | * Returns the current size of the cache in bytes. |
| 142 | */ |
| 143 | uint32_t getSize(); |
| 144 | |
| 145 | private: |
Romain Guy | 25dc3a7 | 2010-12-10 12:33:05 -0800 | [diff] [blame] | 146 | void init(); |
| 147 | |
Romain Guy | 1e45aae | 2010-08-13 19:39:53 -0700 | [diff] [blame] | 148 | GenerationCache<ShadowText, ShadowTexture*> mCache; |
| 149 | |
| 150 | uint32_t mSize; |
| 151 | uint32_t mMaxSize; |
| 152 | FontRenderer* mRenderer; |
Romain Guy | 25dc3a7 | 2010-12-10 12:33:05 -0800 | [diff] [blame] | 153 | bool mDebugEnabled; |
Romain Guy | 1e45aae | 2010-08-13 19:39:53 -0700 | [diff] [blame] | 154 | }; // class TextDropShadowCache |
| 155 | |
| 156 | }; // namespace uirenderer |
| 157 | }; // namespace android |
| 158 | |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 159 | #endif // ANDROID_HWUI_TEXT_DROP_SHADOW_CACHE_H |