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