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