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