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