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