FontRenderer.h revision 55709fc1c7b2741ba3cf7f160d7d8644c112bb99
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, bool forceFinish = true);
72
73    // bounds is an out parameter
74    bool renderTextOnPath(SkPaint* paint, const Rect* clip, const char *text, uint32_t startIndex,
75            uint32_t len, int numGlyphs, SkPath* path, float hOffset, float vOffset, Rect* bounds);
76
77    struct DropShadow {
78        DropShadow() { };
79
80        DropShadow(const DropShadow& dropShadow):
81            width(dropShadow.width), height(dropShadow.height),
82            image(dropShadow.image), penX(dropShadow.penX),
83            penY(dropShadow.penY) {
84        }
85
86        uint32_t width;
87        uint32_t height;
88        uint8_t* image;
89        int32_t penX;
90        int32_t penY;
91    };
92
93    // After renderDropShadow returns, the called owns the memory in DropShadow.image
94    // and is responsible for releasing it when it's done with it
95    DropShadow renderDropShadow(SkPaint* paint, const char *text, uint32_t startIndex,
96            uint32_t len, int numGlyphs, uint32_t radius, const float* positions);
97
98    void setTextureFiltering(bool linearFiltering) {
99        mLinearFiltering = linearFiltering;
100    }
101
102    uint32_t getCacheSize() const;
103
104private:
105    friend class Font;
106
107    static const uint32_t gMaxNumberOfQuads = 2048;
108
109    const uint8_t* mGammaTable;
110
111    void allocateTextureMemory(CacheTexture* cacheTexture);
112    void deallocateTextureMemory(CacheTexture* cacheTexture);
113    void initTextTexture();
114    CacheTexture* createCacheTexture(int width, int height, bool allocate);
115    void cacheBitmap(const SkGlyph& glyph, CachedGlyphInfo* cachedGlyph,
116            uint32_t *retOriginX, uint32_t *retOriginY, bool precaching);
117    CacheTexture* cacheBitmapInTexture(const SkGlyph& glyph, uint32_t* startX, uint32_t* startY);
118
119    void flushAllAndInvalidate();
120    void initVertexArrayBuffers();
121
122    void checkInit();
123    void initRender(const Rect* clip, Rect* bounds, Functor* functor);
124    void finishRender();
125
126    void issueDrawCommand();
127    void appendMeshQuadNoClip(float x1, float y1, float u1, float v1,
128            float x2, float y2, float u2, float v2,
129            float x3, float y3, float u3, float v3,
130            float x4, float y4, float u4, float v4, CacheTexture* texture);
131    void appendMeshQuad(float x1, float y1, float u1, float v1,
132            float x2, float y2, float u2, float v2,
133            float x3, float y3, float u3, float v3,
134            float x4, float y4, float u4, float v4, CacheTexture* texture);
135    void appendRotatedMeshQuad(float x1, float y1, float u1, float v1,
136            float x2, float y2, float u2, float v2,
137            float x3, float y3, float u3, float v3,
138            float x4, float y4, float u4, float v4, CacheTexture* texture);
139
140    void removeFont(const Font* font);
141
142    void checkTextureUpdate();
143
144    void setTextureDirty() {
145        mUploadTexture = true;
146    }
147
148    uint32_t mSmallCacheWidth;
149    uint32_t mSmallCacheHeight;
150    uint32_t mLargeCacheWidth;
151    uint32_t mLargeCacheHeight;
152
153    Vector<CacheTexture*> mCacheTextures;
154
155    Font* mCurrentFont;
156    LruCache<Font::FontDescription, Font*> mActiveFonts;
157
158    CacheTexture* mCurrentCacheTexture;
159
160    bool mUploadTexture;
161
162    uint32_t mIndexBufferID;
163
164    Functor* mFunctor;
165    const Rect* mClip;
166    Rect* mBounds;
167    bool mDrawn;
168
169    bool mInitialized;
170
171    bool mLinearFiltering;
172
173#ifdef ANDROID_ENABLE_RENDERSCRIPT
174    // RS constructs
175    sp<RSC::RS> mRs;
176    sp<const RSC::Element> mRsElement;
177    sp<RSC::ScriptIntrinsicBlur> mRsScript;
178#endif
179
180    static void computeGaussianWeights(float* weights, int32_t radius);
181    static void horizontalBlur(float* weights, int32_t radius, const uint8_t *source, uint8_t *dest,
182            int32_t width, int32_t height);
183    static void verticalBlur(float* weights, int32_t radius, const uint8_t *source, uint8_t *dest,
184            int32_t width, int32_t height);
185
186    // the input image handle may have its pointer replaced (to avoid copies)
187    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