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