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