FontRenderer.h revision 2dc236b2bae13b9a0ed9b3f7320502aecd7983b3
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_FONT_RENDERER_H 18#define ANDROID_HWUI_FONT_RENDERER_H 19 20#include <utils/Functor.h> 21#include <utils/LruCache.h> 22#include <utils/Vector.h> 23#include <utils/StrongPointer.h> 24 25#include <SkPaint.h> 26 27#include <GLES2/gl2.h> 28 29#include "font/FontUtil.h" 30#include "font/CacheTexture.h" 31#include "font/CachedGlyphInfo.h" 32#include "font/Font.h" 33#include "utils/SortedList.h" 34 35#ifdef ANDROID_ENABLE_RENDERSCRIPT 36#include "RenderScript.h" 37namespace RSC { 38 class Element; 39 class RS; 40 class ScriptIntrinsicBlur; 41 class sp; 42} 43#endif 44 45namespace android { 46namespace uirenderer { 47 48class OpenGLRenderer; 49 50/////////////////////////////////////////////////////////////////////////////// 51// TextSetupFunctor 52/////////////////////////////////////////////////////////////////////////////// 53class TextSetupFunctor: public Functor { 54public: 55 struct Data { 56 Data(GLenum glyphFormat) : glyphFormat(glyphFormat) { 57 } 58 59 GLenum glyphFormat; 60 }; 61 62 TextSetupFunctor(OpenGLRenderer* renderer, float x, float y, bool pureTranslate, 63 int alpha, SkXfermode::Mode mode, const SkPaint* paint): Functor(), 64 renderer(renderer), x(x), y(y), pureTranslate(pureTranslate), 65 alpha(alpha), mode(mode), paint(paint) { 66 } 67 ~TextSetupFunctor() { } 68 69 status_t operator ()(int what, void* data); 70 71 OpenGLRenderer* renderer; 72 float x; 73 float y; 74 bool pureTranslate; 75 int alpha; 76 SkXfermode::Mode mode; 77 const SkPaint* paint; 78}; 79 80/////////////////////////////////////////////////////////////////////////////// 81// FontRenderer 82/////////////////////////////////////////////////////////////////////////////// 83 84class FontRenderer { 85public: 86 FontRenderer(); 87 ~FontRenderer(); 88 89 void flushLargeCaches(Vector<CacheTexture*>& cacheTextures); 90 void flushLargeCaches(); 91 92 void setGammaTable(const uint8_t* gammaTable) { 93 mGammaTable = gammaTable; 94 } 95 96 void setFont(const SkPaint* paint, const SkMatrix& matrix); 97 98 void precache(const SkPaint* paint, const char* text, int numGlyphs, const SkMatrix& matrix); 99 void endPrecaching(); 100 101 // bounds is an out parameter 102 bool renderPosText(const SkPaint* paint, const Rect* clip, const char *text, 103 uint32_t startIndex, uint32_t len, int numGlyphs, int x, int y, const float* positions, 104 Rect* bounds, Functor* functor, bool forceFinish = true); 105 106 // bounds is an out parameter 107 bool renderTextOnPath(const SkPaint* paint, const Rect* clip, const char *text, 108 uint32_t startIndex, uint32_t len, int numGlyphs, const SkPath* path, 109 float hOffset, float vOffset, Rect* bounds, Functor* functor); 110 111 struct DropShadow { 112 DropShadow() { }; 113 114 DropShadow(const DropShadow& dropShadow): 115 width(dropShadow.width), height(dropShadow.height), 116 image(dropShadow.image), penX(dropShadow.penX), 117 penY(dropShadow.penY) { 118 } 119 120 uint32_t width; 121 uint32_t height; 122 uint8_t* image; 123 int32_t penX; 124 int32_t penY; 125 }; 126 127 // After renderDropShadow returns, the called owns the memory in DropShadow.image 128 // and is responsible for releasing it when it's done with it 129 DropShadow renderDropShadow(const SkPaint* paint, const char *text, uint32_t startIndex, 130 uint32_t len, int numGlyphs, float radius, const float* positions); 131 132 void setTextureFiltering(bool linearFiltering) { 133 mLinearFiltering = linearFiltering; 134 } 135 136 uint32_t getCacheSize(GLenum format) const; 137 138private: 139 friend class Font; 140 141 const uint8_t* mGammaTable; 142 143 void allocateTextureMemory(CacheTexture* cacheTexture); 144 void deallocateTextureMemory(CacheTexture* cacheTexture); 145 void initTextTexture(); 146 CacheTexture* createCacheTexture(int width, int height, GLenum format, bool allocate); 147 void cacheBitmap(const SkGlyph& glyph, CachedGlyphInfo* cachedGlyph, 148 uint32_t *retOriginX, uint32_t *retOriginY, bool precaching); 149 CacheTexture* cacheBitmapInTexture(Vector<CacheTexture*>& cacheTextures, const SkGlyph& glyph, 150 uint32_t* startX, uint32_t* startY); 151 152 void flushAllAndInvalidate(); 153 154 void checkInit(); 155 void initRender(const Rect* clip, Rect* bounds, Functor* functor); 156 void finishRender(); 157 158 void issueDrawCommand(Vector<CacheTexture*>& cacheTextures); 159 void issueDrawCommand(); 160 void appendMeshQuadNoClip(float x1, float y1, float u1, float v1, 161 float x2, float y2, float u2, float v2, 162 float x3, float y3, float u3, float v3, 163 float x4, float y4, float u4, float v4, CacheTexture* texture); 164 void appendMeshQuad(float x1, float y1, float u1, float v1, 165 float x2, float y2, float u2, float v2, 166 float x3, float y3, float u3, float v3, 167 float x4, float y4, float u4, float v4, CacheTexture* texture); 168 void appendRotatedMeshQuad(float x1, float y1, float u1, float v1, 169 float x2, float y2, float u2, float v2, 170 float x3, float y3, float u3, float v3, 171 float x4, float y4, float u4, float v4, CacheTexture* texture); 172 173 void removeFont(const Font* font); 174 175 void checkTextureUpdate(); 176 177 void setTextureDirty() { 178 mUploadTexture = true; 179 } 180 181 uint32_t mSmallCacheWidth; 182 uint32_t mSmallCacheHeight; 183 uint32_t mLargeCacheWidth; 184 uint32_t mLargeCacheHeight; 185 186 Vector<CacheTexture*> mACacheTextures; 187 Vector<CacheTexture*> mRGBACacheTextures; 188 189 Font* mCurrentFont; 190 LruCache<Font::FontDescription, Font*> mActiveFonts; 191 192 CacheTexture* mCurrentCacheTexture; 193 194 bool mUploadTexture; 195 196 Functor* mFunctor; 197 const Rect* mClip; 198 Rect* mBounds; 199 bool mDrawn; 200 201 bool mInitialized; 202 203 bool mLinearFiltering; 204 205#ifdef ANDROID_ENABLE_RENDERSCRIPT 206 // RS constructs 207 RSC::sp<RSC::RS> mRs; 208 RSC::sp<const RSC::Element> mRsElement; 209 RSC::sp<RSC::ScriptIntrinsicBlur> mRsScript; 210#endif 211 212 static void computeGaussianWeights(float* weights, int32_t radius); 213 static void horizontalBlur(float* weights, int32_t radius, const uint8_t *source, uint8_t *dest, 214 int32_t width, int32_t height); 215 static void verticalBlur(float* weights, int32_t radius, const uint8_t *source, uint8_t *dest, 216 int32_t width, int32_t height); 217 218 // the input image handle may have its pointer replaced (to avoid copies) 219 void blurImage(uint8_t** image, int32_t width, int32_t height, float radius); 220}; 221 222}; // namespace uirenderer 223}; // namespace android 224 225#endif // ANDROID_HWUI_FONT_RENDERER_H 226