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