FontRenderer.h revision 257ae3502cfad43df681b1783528d645bdabc63f
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);
64e816baea651476aca4407200d4a5e629b9ab8dfaChet Haase
65671d6cf460531825a321edb200523d0faa7792c9Romain Guy    // bounds is an out parameter
66671d6cf460531825a321edb200523d0faa7792c9Romain Guy    bool renderPosText(SkPaint* paint, const Rect* clip, const char *text, uint32_t startIndex,
67257ae3502cfad43df681b1783528d645bdabc63fRomain Guy            uint32_t len, int numGlyphs, int x, int y, const float* positions, Rect* bounds,
68257ae3502cfad43df681b1783528d645bdabc63fRomain Guy            Functor* functor);
699777173eb6c9eb97c7921c8288ebc65e3ab3ce6fRomain Guy    // bounds is an out parameter
709777173eb6c9eb97c7921c8288ebc65e3ab3ce6fRomain Guy    bool renderTextOnPath(SkPaint* paint, const Rect* clip, const char *text, uint32_t startIndex,
719777173eb6c9eb97c7921c8288ebc65e3ab3ce6fRomain Guy            uint32_t len, int numGlyphs, SkPath* path, float hOffset, float vOffset, Rect* bounds);
72694b519ac647fe998fd396fe0784cc8e179aadc4Romain Guy
73f18136cb3c881a9d16c1a4f0f341732c276936bfAlex Sakhartchouk    struct DropShadow {
741e45aae5de003657e5d18f74d34998f5de5db5b7Romain Guy        DropShadow() { };
751e45aae5de003657e5d18f74d34998f5de5db5b7Romain Guy
761e45aae5de003657e5d18f74d34998f5de5db5b7Romain Guy        DropShadow(const DropShadow& dropShadow):
771e45aae5de003657e5d18f74d34998f5de5db5b7Romain Guy            width(dropShadow.width), height(dropShadow.height),
781e45aae5de003657e5d18f74d34998f5de5db5b7Romain Guy            image(dropShadow.image), penX(dropShadow.penX),
791e45aae5de003657e5d18f74d34998f5de5db5b7Romain Guy            penY(dropShadow.penY) {
801e45aae5de003657e5d18f74d34998f5de5db5b7Romain Guy        }
811e45aae5de003657e5d18f74d34998f5de5db5b7Romain Guy
82f18136cb3c881a9d16c1a4f0f341732c276936bfAlex Sakhartchouk        uint32_t width;
83f18136cb3c881a9d16c1a4f0f341732c276936bfAlex Sakhartchouk        uint32_t height;
84f18136cb3c881a9d16c1a4f0f341732c276936bfAlex Sakhartchouk        uint8_t* image;
85f18136cb3c881a9d16c1a4f0f341732c276936bfAlex Sakhartchouk        int32_t penX;
86f18136cb3c881a9d16c1a4f0f341732c276936bfAlex Sakhartchouk        int32_t penY;
87f18136cb3c881a9d16c1a4f0f341732c276936bfAlex Sakhartchouk    };
88f18136cb3c881a9d16c1a4f0f341732c276936bfAlex Sakhartchouk
89f18136cb3c881a9d16c1a4f0f341732c276936bfAlex Sakhartchouk    // After renderDropShadow returns, the called owns the memory in DropShadow.image
90f18136cb3c881a9d16c1a4f0f341732c276936bfAlex Sakhartchouk    // and is responsible for releasing it when it's done with it
91f18136cb3c881a9d16c1a4f0f341732c276936bfAlex Sakhartchouk    DropShadow renderDropShadow(SkPaint* paint, const char *text, uint32_t startIndex,
92416a847633680d94efb926837efdc18726d54918Raph Levien            uint32_t len, int numGlyphs, uint32_t radius, const float* positions);
93f18136cb3c881a9d16c1a4f0f341732c276936bfAlex Sakhartchouk
94257ae3502cfad43df681b1783528d645bdabc63fRomain Guy    void setTextureFiltering(bool linearFiltering) {
958087246d9964b11de8ce116bc63b156faa4197e0Romain Guy        mLinearFiltering = linearFiltering;
96c15008e72ec00ca20a271c3006dac649fd07533bRomain Guy    }
97c15008e72ec00ca20a271c3006dac649fd07533bRomain Guy
987de0cb12d0e5fd64811da0b5d1ae0c0d58b86f86Chet Haase    uint32_t getCacheSize() const {
997de0cb12d0e5fd64811da0b5d1ae0c0d58b86f86Chet Haase        uint32_t size = 0;
100378e919ccb75efe24d5a5aa75ac2c6ef255dcb48Chet Haase        for (uint32_t i = 0; i < mCacheTextures.size(); i++) {
101378e919ccb75efe24d5a5aa75ac2c6ef255dcb48Chet Haase            CacheTexture* cacheTexture = mCacheTextures[i];
1028087246d9964b11de8ce116bc63b156faa4197e0Romain Guy            if (cacheTexture && cacheTexture->getTexture()) {
1038087246d9964b11de8ce116bc63b156faa4197e0Romain Guy                size += cacheTexture->getWidth() * cacheTexture->getHeight();
104378e919ccb75efe24d5a5aa75ac2c6ef255dcb48Chet Haase            }
1057de0cb12d0e5fd64811da0b5d1ae0c0d58b86f86Chet Haase        }
1067de0cb12d0e5fd64811da0b5d1ae0c0d58b86f86Chet Haase        return size;
107c15008e72ec00ca20a271c3006dac649fd07533bRomain Guy    }
108c15008e72ec00ca20a271c3006dac649fd07533bRomain Guy
1099b1204baf4740b4d443e72157dea98571cf84e1fRomain Guyprivate:
110694b519ac647fe998fd396fe0784cc8e179aadc4Romain Guy    friend class Font;
111694b519ac647fe998fd396fe0784cc8e179aadc4Romain Guy
112b45c0c9774bd19a9dbe77d149abae4e124b08bf6Romain Guy    const uint8_t* mGammaTable;
113b45c0c9774bd19a9dbe77d149abae4e124b08bf6Romain Guy
1142a47c14e2a6f152496b43104bc785c488583fd59Chet Haase    void allocateTextureMemory(CacheTexture* cacheTexture);
1159a8245629d69d81e0b62e52970feaf9c02580e75Chet Haase    void deallocateTextureMemory(CacheTexture* cacheTexture);
1167de0cb12d0e5fd64811da0b5d1ae0c0d58b86f86Chet Haase    void initTextTexture();
1179777173eb6c9eb97c7921c8288ebc65e3ab3ce6fRomain Guy    CacheTexture* createCacheTexture(int width, int height, bool allocate);
1187de0cb12d0e5fd64811da0b5d1ae0c0d58b86f86Chet Haase    void cacheBitmap(const SkGlyph& glyph, CachedGlyphInfo* cachedGlyph,
119f942cf10e04567f6b9456f6258e29c803b8bfb41Chet Haase            uint32_t *retOriginX, uint32_t *retOriginY, bool precaching);
120378e919ccb75efe24d5a5aa75ac2c6ef255dcb48Chet Haase    CacheTexture* cacheBitmapInTexture(const SkGlyph& glyph, uint32_t* startX, uint32_t* startY);
121694b519ac647fe998fd396fe0784cc8e179aadc4Romain Guy
122694b519ac647fe998fd396fe0784cc8e179aadc4Romain Guy    void flushAllAndInvalidate();
123694b519ac647fe998fd396fe0784cc8e179aadc4Romain Guy    void initVertexArrayBuffers();
124694b519ac647fe998fd396fe0784cc8e179aadc4Romain Guy
125694b519ac647fe998fd396fe0784cc8e179aadc4Romain Guy    void checkInit();
126257ae3502cfad43df681b1783528d645bdabc63fRomain Guy    void initRender(const Rect* clip, Rect* bounds, Functor* functor);
127671d6cf460531825a321edb200523d0faa7792c9Romain Guy    void finishRender();
128694b519ac647fe998fd396fe0784cc8e179aadc4Romain Guy
129694b519ac647fe998fd396fe0784cc8e179aadc4Romain Guy    void issueDrawCommand();
1309777173eb6c9eb97c7921c8288ebc65e3ab3ce6fRomain Guy    void appendMeshQuadNoClip(float x1, float y1, float u1, float v1,
1319777173eb6c9eb97c7921c8288ebc65e3ab3ce6fRomain Guy            float x2, float y2, float u2, float v2,
1329777173eb6c9eb97c7921c8288ebc65e3ab3ce6fRomain Guy            float x3, float y3, float u3, float v3,
1339777173eb6c9eb97c7921c8288ebc65e3ab3ce6fRomain Guy            float x4, float y4, float u4, float v4, CacheTexture* texture);
134d71dd367af604571c7d00ca473184a1b9240eca2Romain Guy    void appendMeshQuad(float x1, float y1, float u1, float v1,
135d71dd367af604571c7d00ca473184a1b9240eca2Romain Guy            float x2, float y2, float u2, float v2,
136d71dd367af604571c7d00ca473184a1b9240eca2Romain Guy            float x3, float y3, float u3, float v3,
1377de0cb12d0e5fd64811da0b5d1ae0c0d58b86f86Chet Haase            float x4, float y4, float u4, float v4, CacheTexture* texture);
1389777173eb6c9eb97c7921c8288ebc65e3ab3ce6fRomain Guy    void appendRotatedMeshQuad(float x1, float y1, float u1, float v1,
1399777173eb6c9eb97c7921c8288ebc65e3ab3ce6fRomain Guy            float x2, float y2, float u2, float v2,
1409777173eb6c9eb97c7921c8288ebc65e3ab3ce6fRomain Guy            float x3, float y3, float u3, float v3,
1419777173eb6c9eb97c7921c8288ebc65e3ab3ce6fRomain Guy            float x4, float y4, float u4, float v4, CacheTexture* texture);
142694b519ac647fe998fd396fe0784cc8e179aadc4Romain Guy
1439b1204baf4740b4d443e72157dea98571cf84e1fRomain Guy    void removeFont(const Font* font);
1449b1204baf4740b4d443e72157dea98571cf84e1fRomain Guy
1459b1204baf4740b4d443e72157dea98571cf84e1fRomain Guy    void checkTextureUpdate();
1469b1204baf4740b4d443e72157dea98571cf84e1fRomain Guy
1479b1204baf4740b4d443e72157dea98571cf84e1fRomain Guy    void setTextureDirty() {
1489b1204baf4740b4d443e72157dea98571cf84e1fRomain Guy        mUploadTexture = true;
1499b1204baf4740b4d443e72157dea98571cf84e1fRomain Guy    }
1509b1204baf4740b4d443e72157dea98571cf84e1fRomain Guy
1517de0cb12d0e5fd64811da0b5d1ae0c0d58b86f86Chet Haase    uint32_t mSmallCacheWidth;
1527de0cb12d0e5fd64811da0b5d1ae0c0d58b86f86Chet Haase    uint32_t mSmallCacheHeight;
153eb32a499194119b3783b86c925172df02e5d2685Chet Haase    uint32_t mLargeCacheWidth;
154eb32a499194119b3783b86c925172df02e5d2685Chet Haase    uint32_t mLargeCacheHeight;
155694b519ac647fe998fd396fe0784cc8e179aadc4Romain Guy
156378e919ccb75efe24d5a5aa75ac2c6ef255dcb48Chet Haase    Vector<CacheTexture*> mCacheTextures;
157694b519ac647fe998fd396fe0784cc8e179aadc4Romain Guy
15809147fbdc8206a0cac78bfe9083e7e15b3c5689cRomain Guy    Font* mCurrentFont;
159e3a9b24b5e3f9b2058486814a6d27729e51ad466Romain Guy    LruCache<Font::FontDescription, Font*> mActiveFonts;
160694b519ac647fe998fd396fe0784cc8e179aadc4Romain Guy
1617de0cb12d0e5fd64811da0b5d1ae0c0d58b86f86Chet Haase    CacheTexture* mCurrentCacheTexture;
1627de0cb12d0e5fd64811da0b5d1ae0c0d58b86f86Chet Haase
163694b519ac647fe998fd396fe0784cc8e179aadc4Romain Guy    bool mUploadTexture;
164694b519ac647fe998fd396fe0784cc8e179aadc4Romain Guy
165694b519ac647fe998fd396fe0784cc8e179aadc4Romain Guy    uint32_t mMaxNumberOfQuads;
166694b519ac647fe998fd396fe0784cc8e179aadc4Romain Guy    uint32_t mIndexBufferID;
167694b519ac647fe998fd396fe0784cc8e179aadc4Romain Guy
168257ae3502cfad43df681b1783528d645bdabc63fRomain Guy    Functor* mFunctor;
16909147fbdc8206a0cac78bfe9083e7e15b3c5689cRomain Guy    const Rect* mClip;
1705b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    Rect* mBounds;
1715b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy    bool mDrawn;
17209147fbdc8206a0cac78bfe9083e7e15b3c5689cRomain Guy
173694b519ac647fe998fd396fe0784cc8e179aadc4Romain Guy    bool mInitialized;
17489a524ac2d4a36739e51b01b336c0bade77e2ee0Alex Sakhartchouk
175e8cb9c14309b0f01c0159efdf9a7198f44a62642Romain Guy    bool mLinearFiltering;
176e8cb9c14309b0f01c0159efdf9a7198f44a62642Romain Guy
177f2d8ccc15d7272b3416f73605c1f31d1d346bd40Chris Craik    // RS constructs
178f2d8ccc15d7272b3416f73605c1f31d1d346bd40Chris Craik    sp<RSC::RS> mRs;
179f2d8ccc15d7272b3416f73605c1f31d1d346bd40Chris Craik    sp<const RSC::Element> mRsElement;
180f2d8ccc15d7272b3416f73605c1f31d1d346bd40Chris Craik    sp<RSC::ScriptIntrinsicBlur> mRsScript;
181f2d8ccc15d7272b3416f73605c1f31d1d346bd40Chris Craik
1829b1204baf4740b4d443e72157dea98571cf84e1fRomain Guy    static void computeGaussianWeights(float* weights, int32_t radius);
1839b1204baf4740b4d443e72157dea98571cf84e1fRomain Guy    static void horizontalBlur(float* weights, int32_t radius, const uint8_t *source, uint8_t *dest,
1841e45aae5de003657e5d18f74d34998f5de5db5b7Romain Guy            int32_t width, int32_t height);
1859b1204baf4740b4d443e72157dea98571cf84e1fRomain Guy    static void verticalBlur(float* weights, int32_t radius, const uint8_t *source, uint8_t *dest,
1861e45aae5de003657e5d18f74d34998f5de5db5b7Romain Guy            int32_t width, int32_t height);
187f2d8ccc15d7272b3416f73605c1f31d1d346bd40Chris Craik
188f2d8ccc15d7272b3416f73605c1f31d1d346bd40Chris Craik    // the input image handle may have its pointer replaced (to avoid copies)
189f2d8ccc15d7272b3416f73605c1f31d1d346bd40Chris Craik    void blurImage(uint8_t** image, int32_t width, int32_t height, int32_t radius);
190694b519ac647fe998fd396fe0784cc8e179aadc4Romain Guy};
191694b519ac647fe998fd396fe0784cc8e179aadc4Romain Guy
192694b519ac647fe998fd396fe0784cc8e179aadc4Romain Guy}; // namespace uirenderer
193694b519ac647fe998fd396fe0784cc8e179aadc4Romain Guy}; // namespace android
194694b519ac647fe998fd396fe0784cc8e179aadc4Romain Guy
1955b3b35296e8b2c8d3f07d32bb645d5414db41a1dRomain Guy#endif // ANDROID_HWUI_FONT_RENDERER_H
196