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_HWUI_TEXT_DROP_SHADOW_CACHE_H 18 #define ANDROID_HWUI_TEXT_DROP_SHADOW_CACHE_H 19 20 #include <GLES2/gl2.h> 21 22 #include <SkPaint.h> 23 24 #include <utils/String16.h> 25 26 #include "utils/Compare.h" 27 #include "utils/GenerationCache.h" 28 #include "FontRenderer.h" 29 #include "Texture.h" 30 31 namespace android { 32 namespace uirenderer { 33 34 struct ShadowText { 35 ShadowText(): radius(0), len(0), textSize(0.0f), typeface(NULL) { 36 } 37 38 ShadowText(SkPaint* paint, uint32_t radius, uint32_t len, const char* srcText): 39 radius(radius), len(len) { 40 // TODO: Propagate this through the API, we should not cast here 41 text = (const char16_t*) srcText; 42 43 textSize = paint->getTextSize(); 44 typeface = paint->getTypeface(); 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; 56 } 57 58 ~ShadowText() { 59 } 60 61 uint32_t radius; 62 uint32_t len; 63 float textSize; 64 SkTypeface* typeface; 65 uint32_t flags; 66 uint32_t italicStyle; 67 uint32_t scaleX; 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 } 75 76 bool operator<(const ShadowText& rhs) const { 77 LTE_INT(len) { 78 LTE_INT(radius) { 79 LTE_FLOAT(textSize) { 80 LTE_INT(typeface) { 81 LTE_INT(flags) { 82 LTE_INT(italicStyle) { 83 LTE_INT(scaleX) { 84 return memcmp(text, rhs.text, len) < 0; 85 } 86 } 87 } 88 } 89 } 90 } 91 } 92 return false; 93 } 94 }; // struct ShadowText 95 96 /** 97 * Alpha texture used to represent a shadow. 98 */ 99 struct ShadowTexture: public Texture { 100 ShadowTexture(): Texture() { 101 } 102 103 float left; 104 float top; 105 }; // struct ShadowTexture 106 107 class TextDropShadowCache: public OnEntryRemoved<ShadowText, ShadowTexture*> { 108 public: 109 TextDropShadowCache(); 110 TextDropShadowCache(uint32_t maxByteSize); 111 ~TextDropShadowCache(); 112 113 /** 114 * Used as a callback when an entry is removed from the cache. 115 * Do not invoke directly. 116 */ 117 void operator()(ShadowText& text, ShadowTexture*& texture); 118 119 ShadowTexture* get(SkPaint* paint, const char* text, uint32_t len, 120 int numGlyphs, uint32_t radius); 121 122 /** 123 * Clears the cache. This causes all textures to be deleted. 124 */ 125 void clear(); 126 127 void setFontRenderer(FontRenderer& fontRenderer) { 128 mRenderer = &fontRenderer; 129 } 130 131 /** 132 * Sets the maximum size of the cache in bytes. 133 */ 134 void setMaxSize(uint32_t maxSize); 135 /** 136 * Returns the maximum size of the cache in bytes. 137 */ 138 uint32_t getMaxSize(); 139 /** 140 * Returns the current size of the cache in bytes. 141 */ 142 uint32_t getSize(); 143 144 private: 145 void init(); 146 147 GenerationCache<ShadowText, ShadowTexture*> mCache; 148 149 uint32_t mSize; 150 uint32_t mMaxSize; 151 FontRenderer* mRenderer; 152 bool mDebugEnabled; 153 }; // class TextDropShadowCache 154 155 }; // namespace uirenderer 156 }; // namespace android 157 158 #endif // ANDROID_HWUI_TEXT_DROP_SHADOW_CACHE_H 159