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