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