FontRenderer.h revision bf5703e52e3304246cbf0e73f6976f7d7312d238
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/LruCache.h> 21#include <utils/Vector.h> 22 23#include <SkPaint.h> 24 25#include <GLES2/gl2.h> 26 27#include "font/FontUtil.h" 28#include "font/CacheTexture.h" 29#include "font/CachedGlyphInfo.h" 30#include "font/Font.h" 31#include "Matrix.h" 32#include "Properties.h" 33 34namespace android { 35namespace uirenderer { 36 37/////////////////////////////////////////////////////////////////////////////// 38// Renderer 39/////////////////////////////////////////////////////////////////////////////// 40 41class FontRenderer { 42public: 43 FontRenderer(); 44 ~FontRenderer(); 45 46 void flushLargeCaches(); 47 48 void setGammaTable(const uint8_t* gammaTable) { 49 mGammaTable = gammaTable; 50 } 51 52 void setFont(SkPaint* paint, const mat4& matrix); 53 54 void precache(SkPaint* paint, const char* text, int numGlyphs, const mat4& matrix); 55 56 // bounds is an out parameter 57 bool renderPosText(SkPaint* paint, const Rect* clip, const char *text, uint32_t startIndex, 58 uint32_t len, int numGlyphs, int x, int y, const float* positions, Rect* bounds); 59 // bounds is an out parameter 60 bool renderTextOnPath(SkPaint* paint, const Rect* clip, const char *text, uint32_t startIndex, 61 uint32_t len, int numGlyphs, SkPath* path, float hOffset, float vOffset, Rect* bounds); 62 63 struct DropShadow { 64 DropShadow() { }; 65 66 DropShadow(const DropShadow& dropShadow): 67 width(dropShadow.width), height(dropShadow.height), 68 image(dropShadow.image), penX(dropShadow.penX), 69 penY(dropShadow.penY) { 70 } 71 72 uint32_t width; 73 uint32_t height; 74 uint8_t* image; 75 int32_t penX; 76 int32_t penY; 77 }; 78 79 // After renderDropShadow returns, the called owns the memory in DropShadow.image 80 // and is responsible for releasing it when it's done with it 81 DropShadow renderDropShadow(SkPaint* paint, const char *text, uint32_t startIndex, 82 uint32_t len, int numGlyphs, uint32_t radius, const float* positions); 83 84 GLuint getTexture(bool linearFiltering = false) { 85 checkInit(); 86 87 mCurrentCacheTexture->setLinearFiltering(linearFiltering); 88 mLinearFiltering = linearFiltering; 89 90 return mCurrentCacheTexture->getTextureId(); 91 } 92 93 uint32_t getCacheSize() const { 94 uint32_t size = 0; 95 for (uint32_t i = 0; i < mCacheTextures.size(); i++) { 96 CacheTexture* cacheTexture = mCacheTextures[i]; 97 if (cacheTexture && cacheTexture->getTexture()) { 98 size += cacheTexture->getWidth() * cacheTexture->getHeight(); 99 } 100 } 101 return size; 102 } 103 104private: 105 friend class Font; 106 107 const uint8_t* mGammaTable; 108 109 void allocateTextureMemory(CacheTexture* cacheTexture); 110 void deallocateTextureMemory(CacheTexture* cacheTexture); 111 void initTextTexture(); 112 CacheTexture* createCacheTexture(int width, int height, bool allocate); 113 void cacheBitmap(const SkGlyph& glyph, CachedGlyphInfo* cachedGlyph, 114 uint32_t *retOriginX, uint32_t *retOriginY, bool precaching); 115 CacheTexture* cacheBitmapInTexture(const SkGlyph& glyph, uint32_t* startX, uint32_t* startY); 116 117 void flushAllAndInvalidate(); 118 void initVertexArrayBuffers(); 119 120 void checkInit(); 121 void initRender(const Rect* clip, Rect* bounds); 122 void finishRender(); 123 124 void issueDrawCommand(); 125 void appendMeshQuadNoClip(float x1, float y1, float u1, float v1, 126 float x2, float y2, float u2, float v2, 127 float x3, float y3, float u3, float v3, 128 float x4, float y4, float u4, float v4, CacheTexture* texture); 129 void appendMeshQuad(float x1, float y1, float u1, float v1, 130 float x2, float y2, float u2, float v2, 131 float x3, float y3, float u3, float v3, 132 float x4, float y4, float u4, float v4, CacheTexture* texture); 133 void appendRotatedMeshQuad(float x1, float y1, float u1, float v1, 134 float x2, float y2, float u2, float v2, 135 float x3, float y3, float u3, float v3, 136 float x4, float y4, float u4, float v4, CacheTexture* texture); 137 138 void removeFont(const Font* font); 139 140 void updateDrawParams(); 141 void checkTextureUpdate(); 142 143 void setTextureDirty() { 144 mUploadTexture = true; 145 } 146 147 uint32_t mSmallCacheWidth; 148 uint32_t mSmallCacheHeight; 149 uint32_t mLargeCacheWidth; 150 uint32_t mLargeCacheHeight; 151 152 Vector<CacheTexture*> mCacheTextures; 153 154 Font* mCurrentFont; 155 LruCache<Font::FontDescription, Font*> mActiveFonts; 156 157 CacheTexture* mCurrentCacheTexture; 158 159 bool mUploadTexture; 160 161 // Pointer to vertex data to speed up frame to frame work 162 float* mTextMesh; 163 uint32_t mCurrentQuadIndex; 164 uint32_t mLastQuadIndex; 165 uint32_t mMaxNumberOfQuads; 166 167 uint32_t mIndexBufferID; 168 169 const Rect* mClip; 170 Rect* mBounds; 171 bool mDrawn; 172 173 bool mInitialized; 174 175 bool mLinearFiltering; 176 177 Vector<uint16_t*> mDrawOffsets; 178 Vector<uint32_t> mDrawCounts; 179 Vector<CacheTexture*> mDrawCacheTextures; 180 181 /** We should consider multi-threading this code or using Renderscript **/ 182 static void computeGaussianWeights(float* weights, int32_t radius); 183 static void horizontalBlur(float* weights, int32_t radius, const uint8_t *source, uint8_t *dest, 184 int32_t width, int32_t height); 185 static void verticalBlur(float* weights, int32_t radius, const uint8_t *source, uint8_t *dest, 186 int32_t width, int32_t height); 187 static void blurImage(uint8_t* image, int32_t width, int32_t height, int32_t radius); 188}; 189 190}; // namespace uirenderer 191}; // namespace android 192 193#endif // ANDROID_HWUI_FONT_RENDERER_H 194