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