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 | |
Romain Guy | 8aa195d | 2013-06-04 18:00:09 -0700 | [diff] [blame] | 33 | class Caches; |
| 34 | |
Romain Guy | 1e45aae | 2010-08-13 19:39:53 -0700 | [diff] [blame] | 35 | struct ShadowText { |
Romain Guy | 059e12c | 2012-11-28 17:35:51 -0800 | [diff] [blame] | 36 | ShadowText(): len(0), radius(0.0f), textSize(0.0f), typeface(NULL), |
| 37 | flags(0), italicStyle(0.0f), scaleX(0), text(NULL), positions(NULL) { |
Romain Guy | c4d8eb6 | 2010-08-18 20:48:33 -0700 | [diff] [blame] | 38 | } |
Romain Guy | 1e45aae | 2010-08-13 19:39:53 -0700 | [diff] [blame] | 39 | |
Romain Guy | 69fcbcc | 2012-11-30 15:29:15 -0800 | [diff] [blame] | 40 | // len is the number of bytes in text |
Chris Craik | d218a92 | 2014-01-02 17:13:34 -0800 | [diff] [blame] | 41 | ShadowText(const SkPaint* paint, float radius, uint32_t len, const char* srcText, |
Raph Levien | 416a847 | 2012-07-19 22:48:17 -0700 | [diff] [blame] | 42 | const float* positions): |
Romain Guy | 059e12c | 2012-11-28 17:35:51 -0800 | [diff] [blame] | 43 | len(len), radius(radius), positions(positions) { |
Romain Guy | 321dce6 | 2011-03-01 11:45:33 -0800 | [diff] [blame] | 44 | // TODO: Propagate this through the API, we should not cast here |
| 45 | text = (const char16_t*) srcText; |
Romain Guy | 1e45aae | 2010-08-13 19:39:53 -0700 | [diff] [blame] | 46 | |
Romain Guy | c4d8eb6 | 2010-08-18 20:48:33 -0700 | [diff] [blame] | 47 | textSize = paint->getTextSize(); |
| 48 | typeface = paint->getTypeface(); |
Romain Guy | cabfcc1 | 2011-03-07 18:06:46 -0800 | [diff] [blame] | 49 | |
| 50 | flags = 0; |
| 51 | if (paint->isFakeBoldText()) { |
| 52 | flags |= Font::kFakeBold; |
| 53 | } |
| 54 | |
Romain Guy | 059e12c | 2012-11-28 17:35:51 -0800 | [diff] [blame] | 55 | italicStyle = paint->getTextSkewX(); |
| 56 | scaleX = paint->getTextScaleX(); |
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 | 059e12c | 2012-11-28 17:35:51 -0800 | [diff] [blame] | 62 | hash_t hash() const; |
| 63 | |
| 64 | static int compare(const ShadowText& lhs, const ShadowText& rhs); |
| 65 | |
| 66 | bool operator==(const ShadowText& other) const { |
| 67 | return compare(*this, other) == 0; |
| 68 | } |
| 69 | |
| 70 | bool operator!=(const ShadowText& other) const { |
| 71 | return compare(*this, other) != 0; |
| 72 | } |
Romain Guy | 321dce6 | 2011-03-01 11:45:33 -0800 | [diff] [blame] | 73 | |
| 74 | void copyTextLocally() { |
Romain Guy | 69fcbcc | 2012-11-30 15:29:15 -0800 | [diff] [blame] | 75 | uint32_t charCount = len / sizeof(char16_t); |
| 76 | str.setTo((const char16_t*) text, charCount); |
Romain Guy | 321dce6 | 2011-03-01 11:45:33 -0800 | [diff] [blame] | 77 | text = str.string(); |
Raph Levien | 416a847 | 2012-07-19 22:48:17 -0700 | [diff] [blame] | 78 | if (positions != NULL) { |
| 79 | positionsCopy.clear(); |
Romain Guy | 69fcbcc | 2012-11-30 15:29:15 -0800 | [diff] [blame] | 80 | positionsCopy.appendArray(positions, charCount * 2); |
Raph Levien | 416a847 | 2012-07-19 22:48:17 -0700 | [diff] [blame] | 81 | positions = positionsCopy.array(); |
| 82 | } |
Romain Guy | 321dce6 | 2011-03-01 11:45:33 -0800 | [diff] [blame] | 83 | } |
Romain Guy | 1e45aae | 2010-08-13 19:39:53 -0700 | [diff] [blame] | 84 | |
Romain Guy | 059e12c | 2012-11-28 17:35:51 -0800 | [diff] [blame] | 85 | uint32_t len; |
| 86 | float radius; |
| 87 | float textSize; |
| 88 | SkTypeface* typeface; |
| 89 | uint32_t flags; |
| 90 | float italicStyle; |
| 91 | float scaleX; |
| 92 | const char16_t* text; |
| 93 | const float* positions; |
| 94 | |
| 95 | // Not directly used to compute the cache key |
| 96 | String16 str; |
| 97 | Vector<float> positionsCopy; |
| 98 | |
Romain Guy | 1e45aae | 2010-08-13 19:39:53 -0700 | [diff] [blame] | 99 | }; // struct ShadowText |
| 100 | |
Romain Guy | 059e12c | 2012-11-28 17:35:51 -0800 | [diff] [blame] | 101 | // Caching support |
| 102 | |
| 103 | inline int strictly_order_type(const ShadowText& lhs, const ShadowText& rhs) { |
| 104 | return ShadowText::compare(lhs, rhs) < 0; |
| 105 | } |
| 106 | |
| 107 | inline int compare_type(const ShadowText& lhs, const ShadowText& rhs) { |
| 108 | return ShadowText::compare(lhs, rhs); |
| 109 | } |
| 110 | |
| 111 | inline hash_t hash_type(const ShadowText& entry) { |
| 112 | return entry.hash(); |
| 113 | } |
| 114 | |
Romain Guy | 1e45aae | 2010-08-13 19:39:53 -0700 | [diff] [blame] | 115 | /** |
| 116 | * Alpha texture used to represent a shadow. |
| 117 | */ |
| 118 | struct ShadowTexture: public Texture { |
Romain Guy | 8aa195d | 2013-06-04 18:00:09 -0700 | [diff] [blame] | 119 | ShadowTexture(Caches& caches): Texture(caches) { |
Romain Guy | 1e45aae | 2010-08-13 19:39:53 -0700 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | float left; |
| 123 | float top; |
| 124 | }; // struct ShadowTexture |
| 125 | |
| 126 | class TextDropShadowCache: public OnEntryRemoved<ShadowText, ShadowTexture*> { |
| 127 | public: |
Romain Guy | fb8b763 | 2010-08-23 21:05:08 -0700 | [diff] [blame] | 128 | TextDropShadowCache(); |
Romain Guy | 1e45aae | 2010-08-13 19:39:53 -0700 | [diff] [blame] | 129 | TextDropShadowCache(uint32_t maxByteSize); |
| 130 | ~TextDropShadowCache(); |
| 131 | |
| 132 | /** |
| 133 | * Used as a callback when an entry is removed from the cache. |
| 134 | * Do not invoke directly. |
| 135 | */ |
| 136 | void operator()(ShadowText& text, ShadowTexture*& texture); |
| 137 | |
Chris Craik | d218a92 | 2014-01-02 17:13:34 -0800 | [diff] [blame] | 138 | ShadowTexture* get(const SkPaint* paint, const char* text, uint32_t len, |
Romain Guy | 059e12c | 2012-11-28 17:35:51 -0800 | [diff] [blame] | 139 | int numGlyphs, float radius, const float* positions); |
Romain Guy | 1e45aae | 2010-08-13 19:39:53 -0700 | [diff] [blame] | 140 | |
| 141 | /** |
| 142 | * Clears the cache. This causes all textures to be deleted. |
| 143 | */ |
| 144 | void clear(); |
| 145 | |
| 146 | void setFontRenderer(FontRenderer& fontRenderer) { |
| 147 | mRenderer = &fontRenderer; |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * Sets the maximum size of the cache in bytes. |
| 152 | */ |
| 153 | void setMaxSize(uint32_t maxSize); |
| 154 | /** |
| 155 | * Returns the maximum size of the cache in bytes. |
| 156 | */ |
| 157 | uint32_t getMaxSize(); |
| 158 | /** |
| 159 | * Returns the current size of the cache in bytes. |
| 160 | */ |
| 161 | uint32_t getSize(); |
| 162 | |
| 163 | private: |
Romain Guy | 25dc3a7 | 2010-12-10 12:33:05 -0800 | [diff] [blame] | 164 | void init(); |
| 165 | |
Romain Guy | 059e12c | 2012-11-28 17:35:51 -0800 | [diff] [blame] | 166 | LruCache<ShadowText, ShadowTexture*> mCache; |
Romain Guy | 1e45aae | 2010-08-13 19:39:53 -0700 | [diff] [blame] | 167 | |
| 168 | uint32_t mSize; |
| 169 | uint32_t mMaxSize; |
| 170 | FontRenderer* mRenderer; |
Romain Guy | 25dc3a7 | 2010-12-10 12:33:05 -0800 | [diff] [blame] | 171 | bool mDebugEnabled; |
Romain Guy | 1e45aae | 2010-08-13 19:39:53 -0700 | [diff] [blame] | 172 | }; // class TextDropShadowCache |
| 173 | |
| 174 | }; // namespace uirenderer |
| 175 | }; // namespace android |
| 176 | |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 177 | #endif // ANDROID_HWUI_TEXT_DROP_SHADOW_CACHE_H |