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