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