TextDropShadowCache.h revision 416a847633680d94efb926837efdc18726d54918
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 31namespace android { 32namespace uirenderer { 33 34struct 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 const float* positions): 40 radius(radius), len(len), 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 const float skewX = paint->getTextSkewX(); 53 italicStyle = *(uint32_t*) &skewX; 54 55 const float scaleXFloat = paint->getTextScaleX(); 56 scaleX = *(uint32_t*) &scaleXFloat; 57 } 58 59 ~ShadowText() { 60 } 61 62 uint32_t radius; 63 uint32_t len; 64 float textSize; 65 SkTypeface* typeface; 66 uint32_t flags; 67 uint32_t italicStyle; 68 uint32_t scaleX; 69 const char16_t* text; 70 const float* positions; 71 String16 str; 72 Vector<float> positionsCopy; 73 74 void copyTextLocally() { 75 str.setTo((const char16_t*) text, len >> 1); 76 text = str.string(); 77 if (positions != NULL) { 78 positionsCopy.clear(); 79 positionsCopy.appendArray(positions, len); 80 positions = positionsCopy.array(); 81 } 82 } 83 84 bool operator<(const ShadowText& rhs) const { 85 LTE_INT(len) { 86 LTE_INT(radius) { 87 LTE_FLOAT(textSize) { 88 LTE_INT(typeface) { 89 LTE_INT(flags) { 90 LTE_INT(italicStyle) { 91 LTE_INT(scaleX) { 92 int cmp = memcmp(text, rhs.text, len); 93 if (cmp < 0) return true; 94 if (cmp == 0 && rhs.positions != NULL) { 95 if (positions == NULL) return true; 96 return memcmp(positions, rhs.positions, len << 2) < 0; 97 } 98 } 99 } 100 } 101 } 102 } 103 } 104 } 105 return false; 106 } 107}; // struct ShadowText 108 109/** 110 * Alpha texture used to represent a shadow. 111 */ 112struct ShadowTexture: public Texture { 113 ShadowTexture(): Texture() { 114 } 115 116 float left; 117 float top; 118}; // struct ShadowTexture 119 120class TextDropShadowCache: public OnEntryRemoved<ShadowText, ShadowTexture*> { 121public: 122 TextDropShadowCache(); 123 TextDropShadowCache(uint32_t maxByteSize); 124 ~TextDropShadowCache(); 125 126 /** 127 * Used as a callback when an entry is removed from the cache. 128 * Do not invoke directly. 129 */ 130 void operator()(ShadowText& text, ShadowTexture*& texture); 131 132 ShadowTexture* get(SkPaint* paint, const char* text, uint32_t len, 133 int numGlyphs, uint32_t radius, const float* positions); 134 135 /** 136 * Clears the cache. This causes all textures to be deleted. 137 */ 138 void clear(); 139 140 void setFontRenderer(FontRenderer& fontRenderer) { 141 mRenderer = &fontRenderer; 142 } 143 144 /** 145 * Sets the maximum size of the cache in bytes. 146 */ 147 void setMaxSize(uint32_t maxSize); 148 /** 149 * Returns the maximum size of the cache in bytes. 150 */ 151 uint32_t getMaxSize(); 152 /** 153 * Returns the current size of the cache in bytes. 154 */ 155 uint32_t getSize(); 156 157private: 158 void init(); 159 160 GenerationCache<ShadowText, ShadowTexture*> mCache; 161 162 uint32_t mSize; 163 uint32_t mMaxSize; 164 FontRenderer* mRenderer; 165 bool mDebugEnabled; 166}; // class TextDropShadowCache 167 168}; // namespace uirenderer 169}; // namespace android 170 171#endif // ANDROID_HWUI_TEXT_DROP_SHADOW_CACHE_H 172