1694b519ac647fe998fd396fe0784cc8e179aadc4Romain Guy/*
2694b519ac647fe998fd396fe0784cc8e179aadc4Romain Guy * Copyright (C) 2010 The Android Open Source Project
3694b519ac647fe998fd396fe0784cc8e179aadc4Romain Guy *
4694b519ac647fe998fd396fe0784cc8e179aadc4Romain Guy * Licensed under the Apache License, Version 2.0 (the "License");
5694b519ac647fe998fd396fe0784cc8e179aadc4Romain Guy * you may not use this file except in compliance with the License.
6694b519ac647fe998fd396fe0784cc8e179aadc4Romain Guy * You may obtain a copy of the License at
7694b519ac647fe998fd396fe0784cc8e179aadc4Romain Guy *
8694b519ac647fe998fd396fe0784cc8e179aadc4Romain Guy *      http://www.apache.org/licenses/LICENSE-2.0
9694b519ac647fe998fd396fe0784cc8e179aadc4Romain Guy *
10694b519ac647fe998fd396fe0784cc8e179aadc4Romain Guy * Unless required by applicable law or agreed to in writing, software
11694b519ac647fe998fd396fe0784cc8e179aadc4Romain Guy * distributed under the License is distributed on an "AS IS" BASIS,
12694b519ac647fe998fd396fe0784cc8e179aadc4Romain Guy * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13694b519ac647fe998fd396fe0784cc8e179aadc4Romain Guy * See the License for the specific language governing permissions and
14694b519ac647fe998fd396fe0784cc8e179aadc4Romain Guy * limitations under the License.
15694b519ac647fe998fd396fe0784cc8e179aadc4Romain Guy */
16694b519ac647fe998fd396fe0784cc8e179aadc4Romain Guy
175b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy#ifndef ANDROID_HWUI_FONT_RENDERER_H
185b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy#define ANDROID_HWUI_FONT_RENDERER_H
19694b519ac647fe998fd396fe0784cc8e179aadc4Romain Guy
20e3a9b24b5e3f9b2058486814a6d27729e51ad466Romain Guy#include <utils/LruCache.h>
21694b519ac647fe998fd396fe0784cc8e179aadc4Romain Guy#include <utils/Vector.h>
22694b519ac647fe998fd396fe0784cc8e179aadc4Romain Guy
23694b519ac647fe998fd396fe0784cc8e179aadc4Romain Guy#include <SkPaint.h>
24694b519ac647fe998fd396fe0784cc8e179aadc4Romain Guy
25694b519ac647fe998fd396fe0784cc8e179aadc4Romain Guy#include <GLES2/gl2.h>
26694b519ac647fe998fd396fe0784cc8e179aadc4Romain Guy
279f5dab3fc228fa11c32b483e6101ec086895a32bRomain Guy#include "font/FontUtil.h"
289f5dab3fc228fa11c32b483e6101ec086895a32bRomain Guy#include "font/CacheTexture.h"
299f5dab3fc228fa11c32b483e6101ec086895a32bRomain Guy#include "font/CachedGlyphInfo.h"
309f5dab3fc228fa11c32b483e6101ec086895a32bRomain Guy#include "font/Font.h"
31115096f50a560e64a7f95d37686d4861042c7aebRomain Guy#include "utils/SortedList.h"
32e3a9b24b5e3f9b2058486814a6d27729e51ad466Romain Guy#include "Matrix.h"
3351769a68a5cb34e9564740c6a854fcb93018789dRomain Guy#include "Properties.h"
3409147fbdc8206a0cac78bfe9083e7e15b3c5689cRomain Guy
35f2d8ccc15d7272b3416f73605c1f31d1d346bd40Chris Craiknamespace RSC {
36f2d8ccc15d7272b3416f73605c1f31d1d346bd40Chris Craik    class Element;
37f2d8ccc15d7272b3416f73605c1f31d1d346bd40Chris Craik    class RS;
38f2d8ccc15d7272b3416f73605c1f31d1d346bd40Chris Craik    class ScriptIntrinsicBlur;
39f2d8ccc15d7272b3416f73605c1f31d1d346bd40Chris Craik}
40f2d8ccc15d7272b3416f73605c1f31d1d346bd40Chris Craik
41257ae3502cfad43df681b1783528d645bdabc63fRomain Guyclass Functor;
42257ae3502cfad43df681b1783528d645bdabc63fRomain Guy
43694b519ac647fe998fd396fe0784cc8e179aadc4Romain Guynamespace android {
44694b519ac647fe998fd396fe0784cc8e179aadc4Romain Guynamespace uirenderer {
45694b519ac647fe998fd396fe0784cc8e179aadc4Romain Guy
46726aeba80ffc6778a9bc3e0ee957b8d644183505Romain Guy///////////////////////////////////////////////////////////////////////////////
47726aeba80ffc6778a9bc3e0ee957b8d644183505Romain Guy// Renderer
48726aeba80ffc6778a9bc3e0ee957b8d644183505Romain Guy///////////////////////////////////////////////////////////////////////////////
49726aeba80ffc6778a9bc3e0ee957b8d644183505Romain Guy
50694b519ac647fe998fd396fe0784cc8e179aadc4Romain Guyclass FontRenderer {
51694b519ac647fe998fd396fe0784cc8e179aadc4Romain Guypublic:
52694b519ac647fe998fd396fe0784cc8e179aadc4Romain Guy    FontRenderer();
53694b519ac647fe998fd396fe0784cc8e179aadc4Romain Guy    ~FontRenderer();
54694b519ac647fe998fd396fe0784cc8e179aadc4Romain Guy
559a8245629d69d81e0b62e52970feaf9c02580e75Chet Haase    void flushLargeCaches();
56694b519ac647fe998fd396fe0784cc8e179aadc4Romain Guy
57b45c0c9774bd19a9dbe77d149abae4e124b08bf6Romain Guy    void setGammaTable(const uint8_t* gammaTable) {
58b45c0c9774bd19a9dbe77d149abae4e124b08bf6Romain Guy        mGammaTable = gammaTable;
59b45c0c9774bd19a9dbe77d149abae4e124b08bf6Romain Guy    }
60b45c0c9774bd19a9dbe77d149abae4e124b08bf6Romain Guy
61e3a9b24b5e3f9b2058486814a6d27729e51ad466Romain Guy    void setFont(SkPaint* paint, const mat4& matrix);
62e816baea651476aca4407200d4a5e629b9ab8dfaChet Haase
63e3a9b24b5e3f9b2058486814a6d27729e51ad466Romain Guy    void precache(SkPaint* paint, const char* text, int numGlyphs, const mat4& matrix);
64cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy    void endPrecaching();
65e816baea651476aca4407200d4a5e629b9ab8dfaChet Haase
66671d6cf460531825a321edb200523d0faa7792c9Romain Guy    // bounds is an out parameter
67671d6cf460531825a321edb200523d0faa7792c9Romain Guy    bool renderPosText(SkPaint* paint, const Rect* clip, const char *text, uint32_t startIndex,
68257ae3502cfad43df681b1783528d645bdabc63fRomain Guy            uint32_t len, int numGlyphs, int x, int y, const float* positions, Rect* bounds,
69527a3aace1dd72432c2e0472a570e030ad04bf16Chris Craik            Functor* functor, bool forceFinish = true);
70527a3aace1dd72432c2e0472a570e030ad04bf16Chris Craik
719777173eb6c9eb97c7921c8288ebc65e3ab3ce6fRomain Guy    // bounds is an out parameter
729777173eb6c9eb97c7921c8288ebc65e3ab3ce6fRomain Guy    bool renderTextOnPath(SkPaint* paint, const Rect* clip, const char *text, uint32_t startIndex,
739777173eb6c9eb97c7921c8288ebc65e3ab3ce6fRomain Guy            uint32_t len, int numGlyphs, SkPath* path, float hOffset, float vOffset, Rect* bounds);
74694b519ac647fe998fd396fe0784cc8e179aadc4Romain Guy
75f18136cb3c881a9d16c1a4f0f341732c276936bfAlex Sakhartchouk    struct DropShadow {
761e45aae5de003657e5d18f74d34998f5de5db5b7Romain Guy        DropShadow() { };
771e45aae5de003657e5d18f74d34998f5de5db5b7Romain Guy
781e45aae5de003657e5d18f74d34998f5de5db5b7Romain Guy        DropShadow(const DropShadow& dropShadow):
791e45aae5de003657e5d18f74d34998f5de5db5b7Romain Guy            width(dropShadow.width), height(dropShadow.height),
801e45aae5de003657e5d18f74d34998f5de5db5b7Romain Guy            image(dropShadow.image), penX(dropShadow.penX),
811e45aae5de003657e5d18f74d34998f5de5db5b7Romain Guy            penY(dropShadow.penY) {
821e45aae5de003657e5d18f74d34998f5de5db5b7Romain Guy        }
831e45aae5de003657e5d18f74d34998f5de5db5b7Romain Guy
84f18136cb3c881a9d16c1a4f0f341732c276936bfAlex Sakhartchouk        uint32_t width;
85f18136cb3c881a9d16c1a4f0f341732c276936bfAlex Sakhartchouk        uint32_t height;
86f18136cb3c881a9d16c1a4f0f341732c276936bfAlex Sakhartchouk        uint8_t* image;
87f18136cb3c881a9d16c1a4f0f341732c276936bfAlex Sakhartchouk        int32_t penX;
88f18136cb3c881a9d16c1a4f0f341732c276936bfAlex Sakhartchouk        int32_t penY;
89f18136cb3c881a9d16c1a4f0f341732c276936bfAlex Sakhartchouk    };
90f18136cb3c881a9d16c1a4f0f341732c276936bfAlex Sakhartchouk
91f18136cb3c881a9d16c1a4f0f341732c276936bfAlex Sakhartchouk    // After renderDropShadow returns, the called owns the memory in DropShadow.image
92f18136cb3c881a9d16c1a4f0f341732c276936bfAlex Sakhartchouk    // and is responsible for releasing it when it's done with it
93f18136cb3c881a9d16c1a4f0f341732c276936bfAlex Sakhartchouk    DropShadow renderDropShadow(SkPaint* paint, const char *text, uint32_t startIndex,
94416a847633680d94efb926837efdc18726d54918Raph Levien            uint32_t len, int numGlyphs, uint32_t radius, const float* positions);
95f18136cb3c881a9d16c1a4f0f341732c276936bfAlex Sakhartchouk
96257ae3502cfad43df681b1783528d645bdabc63fRomain Guy    void setTextureFiltering(bool linearFiltering) {
978087246d9964b11de8ce116bc63b156faa4197e0Romain Guy        mLinearFiltering = linearFiltering;
98c15008e72ec00ca20a271c3006dac649fd07533bRomain Guy    }
99c15008e72ec00ca20a271c3006dac649fd07533bRomain Guy
100cf51a4199835e9604aa4c8b3854306f8fbabbf33Romain Guy    uint32_t getCacheSize() const;
101c15008e72ec00ca20a271c3006dac649fd07533bRomain Guy
1029b1204baf4740b4d443e72157dea98571cf84e1fRomain Guyprivate:
103694b519ac647fe998fd396fe0784cc8e179aadc4Romain Guy    friend class Font;
104694b519ac647fe998fd396fe0784cc8e179aadc4Romain Guy
105527a3aace1dd72432c2e0472a570e030ad04bf16Chris Craik    static const uint32_t gMaxNumberOfQuads = 2048;
106527a3aace1dd72432c2e0472a570e030ad04bf16Chris Craik
107b45c0c9774bd19a9dbe77d149abae4e124b08bf6Romain Guy    const uint8_t* mGammaTable;
108b45c0c9774bd19a9dbe77d149abae4e124b08bf6Romain Guy
1092a47c14e2a6f152496b43104bc785c488583fd59Chet Haase    void allocateTextureMemory(CacheTexture* cacheTexture);
1109a8245629d69d81e0b62e52970feaf9c02580e75Chet Haase    void deallocateTextureMemory(CacheTexture* cacheTexture);
1117de0cb12d0e5fd64811da0b5d1ae0c0d58b86f86Chet Haase    void initTextTexture();
1129777173eb6c9eb97c7921c8288ebc65e3ab3ce6fRomain Guy    CacheTexture* createCacheTexture(int width, int height, bool allocate);
1137de0cb12d0e5fd64811da0b5d1ae0c0d58b86f86Chet Haase    void cacheBitmap(const SkGlyph& glyph, CachedGlyphInfo* cachedGlyph,
114f942cf10e04567f6b9456f6258e29c803b8bfb41Chet Haase            uint32_t *retOriginX, uint32_t *retOriginY, bool precaching);
115378e919ccb75efe24d5a5aa75ac2c6ef255dcb48Chet Haase    CacheTexture* cacheBitmapInTexture(const SkGlyph& glyph, uint32_t* startX, uint32_t* startY);
116694b519ac647fe998fd396fe0784cc8e179aadc4Romain Guy
117694b519ac647fe998fd396fe0784cc8e179aadc4Romain Guy    void flushAllAndInvalidate();
118694b519ac647fe998fd396fe0784cc8e179aadc4Romain Guy    void initVertexArrayBuffers();
119694b519ac647fe998fd396fe0784cc8e179aadc4Romain Guy
120694b519ac647fe998fd396fe0784cc8e179aadc4Romain Guy    void checkInit();
121257ae3502cfad43df681b1783528d645bdabc63fRomain Guy    void initRender(const Rect* clip, Rect* bounds, Functor* functor);
122671d6cf460531825a321edb200523d0faa7792c9Romain Guy    void finishRender();
123694b519ac647fe998fd396fe0784cc8e179aadc4Romain Guy
124694b519ac647fe998fd396fe0784cc8e179aadc4Romain Guy    void issueDrawCommand();
1259777173eb6c9eb97c7921c8288ebc65e3ab3ce6fRomain Guy    void appendMeshQuadNoClip(float x1, float y1, float u1, float v1,
1269777173eb6c9eb97c7921c8288ebc65e3ab3ce6fRomain Guy            float x2, float y2, float u2, float v2,
1279777173eb6c9eb97c7921c8288ebc65e3ab3ce6fRomain Guy            float x3, float y3, float u3, float v3,
1289777173eb6c9eb97c7921c8288ebc65e3ab3ce6fRomain Guy            float x4, float y4, float u4, float v4, CacheTexture* texture);
129d71dd367af604571c7d00ca473184a1b9240eca2Romain Guy    void appendMeshQuad(float x1, float y1, float u1, float v1,
130d71dd367af604571c7d00ca473184a1b9240eca2Romain Guy            float x2, float y2, float u2, float v2,
131d71dd367af604571c7d00ca473184a1b9240eca2Romain Guy            float x3, float y3, float u3, float v3,
1327de0cb12d0e5fd64811da0b5d1ae0c0d58b86f86Chet Haase            float x4, float y4, float u4, float v4, CacheTexture* texture);
1339777173eb6c9eb97c7921c8288ebc65e3ab3ce6fRomain Guy    void appendRotatedMeshQuad(float x1, float y1, float u1, float v1,
1349777173eb6c9eb97c7921c8288ebc65e3ab3ce6fRomain Guy            float x2, float y2, float u2, float v2,
1359777173eb6c9eb97c7921c8288ebc65e3ab3ce6fRomain Guy            float x3, float y3, float u3, float v3,
1369777173eb6c9eb97c7921c8288ebc65e3ab3ce6fRomain Guy            float x4, float y4, float u4, float v4, CacheTexture* texture);
137694b519ac647fe998fd396fe0784cc8e179aadc4Romain Guy
1389b1204baf4740b4d443e72157dea98571cf84e1fRomain Guy    void removeFont(const Font* font);
1399b1204baf4740b4d443e72157dea98571cf84e1fRomain Guy
1409b1204baf4740b4d443e72157dea98571cf84e1fRomain Guy    void checkTextureUpdate();
1419b1204baf4740b4d443e72157dea98571cf84e1fRomain Guy
1429b1204baf4740b4d443e72157dea98571cf84e1fRomain Guy    void setTextureDirty() {
1439b1204baf4740b4d443e72157dea98571cf84e1fRomain Guy        mUploadTexture = true;
1449b1204baf4740b4d443e72157dea98571cf84e1fRomain Guy    }
1459b1204baf4740b4d443e72157dea98571cf84e1fRomain Guy
1467de0cb12d0e5fd64811da0b5d1ae0c0d58b86f86Chet Haase    uint32_t mSmallCacheWidth;
1477de0cb12d0e5fd64811da0b5d1ae0c0d58b86f86Chet Haase    uint32_t mSmallCacheHeight;
148eb32a499194119b3783b86c925172df02e5d2685Chet Haase    uint32_t mLargeCacheWidth;
149eb32a499194119b3783b86c925172df02e5d2685Chet Haase    uint32_t mLargeCacheHeight;
150694b519ac647fe998fd396fe0784cc8e179aadc4Romain Guy
151378e919ccb75efe24d5a5aa75ac2c6ef255dcb48Chet Haase    Vector<CacheTexture*> mCacheTextures;
152694b519ac647fe998fd396fe0784cc8e179aadc4Romain Guy
15309147fbdc8206a0cac78bfe9083e7e15b3c5689cRomain Guy    Font* mCurrentFont;
154e3a9b24b5e3f9b2058486814a6d27729e51ad466Romain Guy    LruCache<Font::FontDescription, Font*> mActiveFonts;
155694b519ac647fe998fd396fe0784cc8e179aadc4Romain Guy
1567de0cb12d0e5fd64811da0b5d1ae0c0d58b86f86Chet Haase    CacheTexture* mCurrentCacheTexture;
1577de0cb12d0e5fd64811da0b5d1ae0c0d58b86f86Chet Haase
158694b519ac647fe998fd396fe0784cc8e179aadc4Romain Guy    bool mUploadTexture;
159694b519ac647fe998fd396fe0784cc8e179aadc4Romain Guy
160694b519ac647fe998fd396fe0784cc8e179aadc4Romain Guy    uint32_t mIndexBufferID;
161694b519ac647fe998fd396fe0784cc8e179aadc4Romain Guy
162257ae3502cfad43df681b1783528d645bdabc63fRomain Guy    Functor* mFunctor;
16309147fbdc8206a0cac78bfe9083e7e15b3c5689cRomain Guy    const Rect* mClip;
1645b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    Rect* mBounds;
1655b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    bool mDrawn;
16609147fbdc8206a0cac78bfe9083e7e15b3c5689cRomain Guy
167694b519ac647fe998fd396fe0784cc8e179aadc4Romain Guy    bool mInitialized;
16889a524ac2d4a36739e51b01b336c0bade77e2ee0Alex Sakhartchouk
169e8cb9c14309b0f01c0159efdf9a7198f44a62642Romain Guy    bool mLinearFiltering;
170e8cb9c14309b0f01c0159efdf9a7198f44a62642Romain Guy
171f2d8ccc15d7272b3416f73605c1f31d1d346bd40Chris Craik    // RS constructs
172f2d8ccc15d7272b3416f73605c1f31d1d346bd40Chris Craik    sp<RSC::RS> mRs;
173f2d8ccc15d7272b3416f73605c1f31d1d346bd40Chris Craik    sp<const RSC::Element> mRsElement;
174f2d8ccc15d7272b3416f73605c1f31d1d346bd40Chris Craik    sp<RSC::ScriptIntrinsicBlur> mRsScript;
175f2d8ccc15d7272b3416f73605c1f31d1d346bd40Chris Craik
1769b1204baf4740b4d443e72157dea98571cf84e1fRomain Guy    static void computeGaussianWeights(float* weights, int32_t radius);
1779b1204baf4740b4d443e72157dea98571cf84e1fRomain Guy    static void horizontalBlur(float* weights, int32_t radius, const uint8_t *source, uint8_t *dest,
1781e45aae5de003657e5d18f74d34998f5de5db5b7Romain Guy            int32_t width, int32_t height);
1799b1204baf4740b4d443e72157dea98571cf84e1fRomain Guy    static void verticalBlur(float* weights, int32_t radius, const uint8_t *source, uint8_t *dest,
1801e45aae5de003657e5d18f74d34998f5de5db5b7Romain Guy            int32_t width, int32_t height);
181f2d8ccc15d7272b3416f73605c1f31d1d346bd40Chris Craik
182f2d8ccc15d7272b3416f73605c1f31d1d346bd40Chris Craik    // the input image handle may have its pointer replaced (to avoid copies)
183f2d8ccc15d7272b3416f73605c1f31d1d346bd40Chris Craik    void blurImage(uint8_t** image, int32_t width, int32_t height, int32_t radius);
184694b519ac647fe998fd396fe0784cc8e179aadc4Romain Guy};
185694b519ac647fe998fd396fe0784cc8e179aadc4Romain Guy
186694b519ac647fe998fd396fe0784cc8e179aadc4Romain Guy}; // namespace uirenderer
187694b519ac647fe998fd396fe0784cc8e179aadc4Romain Guy}; // namespace android
188694b519ac647fe998fd396fe0784cc8e179aadc4Romain Guy
1895b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy#endif // ANDROID_HWUI_FONT_RENDERER_H
190