Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 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 | |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 17 | #ifndef ANDROID_HWUI_FONT_RENDERER_H |
| 18 | #define ANDROID_HWUI_FONT_RENDERER_H |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 19 | |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 20 | #include <utils/Vector.h> |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 21 | |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 22 | #include <SkPaint.h> |
| 23 | |
| 24 | #include <GLES2/gl2.h> |
| 25 | |
Romain Guy | 9f5dab3 | 2012-09-04 12:55:44 -0700 | [diff] [blame] | 26 | #include "font/FontUtil.h" |
| 27 | #include "font/CacheTexture.h" |
| 28 | #include "font/CachedGlyphInfo.h" |
| 29 | #include "font/Font.h" |
Romain Guy | 51769a6 | 2010-07-23 00:28:00 -0700 | [diff] [blame] | 30 | #include "Properties.h" |
Romain Guy | 09147fb | 2010-07-22 13:08:20 -0700 | [diff] [blame] | 31 | |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 32 | namespace android { |
| 33 | namespace uirenderer { |
| 34 | |
Romain Guy | 726aeba | 2011-06-01 14:52:00 -0700 | [diff] [blame] | 35 | /////////////////////////////////////////////////////////////////////////////// |
Romain Guy | 726aeba | 2011-06-01 14:52:00 -0700 | [diff] [blame] | 36 | // Renderer |
| 37 | /////////////////////////////////////////////////////////////////////////////// |
| 38 | |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 39 | class FontRenderer { |
| 40 | public: |
| 41 | FontRenderer(); |
| 42 | ~FontRenderer(); |
| 43 | |
Chet Haase | 9a82456 | 2011-12-16 15:44:59 -0800 | [diff] [blame] | 44 | void flushLargeCaches(); |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 45 | |
Romain Guy | b45c0c9 | 2010-08-26 20:35:23 -0700 | [diff] [blame] | 46 | void setGammaTable(const uint8_t* gammaTable) { |
| 47 | mGammaTable = gammaTable; |
| 48 | } |
| 49 | |
Alex Sakhartchouk | 65ef909 | 2010-07-26 11:09:33 -0700 | [diff] [blame] | 50 | void setFont(SkPaint* paint, uint32_t fontId, float fontSize); |
Chet Haase | e816bae | 2012-08-09 13:39:02 -0700 | [diff] [blame] | 51 | |
| 52 | void precache(SkPaint* paint, const char* text, int numGlyphs); |
| 53 | |
Romain Guy | 671d6cf | 2012-01-18 12:39:17 -0800 | [diff] [blame] | 54 | // bounds is an out parameter |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 55 | bool renderText(SkPaint* paint, const Rect* clip, const char *text, uint32_t startIndex, |
| 56 | uint32_t len, int numGlyphs, int x, int y, Rect* bounds); |
Romain Guy | 671d6cf | 2012-01-18 12:39:17 -0800 | [diff] [blame] | 57 | // bounds is an out parameter |
| 58 | bool renderPosText(SkPaint* paint, const Rect* clip, const char *text, uint32_t startIndex, |
| 59 | uint32_t len, int numGlyphs, int x, int y, const float* positions, Rect* bounds); |
Romain Guy | 9777173 | 2012-02-28 18:17:02 -0800 | [diff] [blame] | 60 | // bounds is an out parameter |
| 61 | bool renderTextOnPath(SkPaint* paint, const Rect* clip, const char *text, uint32_t startIndex, |
| 62 | uint32_t len, int numGlyphs, SkPath* path, float hOffset, float vOffset, Rect* bounds); |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 63 | |
Alex Sakhartchouk | f18136c | 2010-08-06 14:49:04 -0700 | [diff] [blame] | 64 | struct DropShadow { |
Romain Guy | 1e45aae | 2010-08-13 19:39:53 -0700 | [diff] [blame] | 65 | DropShadow() { }; |
| 66 | |
| 67 | DropShadow(const DropShadow& dropShadow): |
| 68 | width(dropShadow.width), height(dropShadow.height), |
| 69 | image(dropShadow.image), penX(dropShadow.penX), |
| 70 | penY(dropShadow.penY) { |
| 71 | } |
| 72 | |
Alex Sakhartchouk | f18136c | 2010-08-06 14:49:04 -0700 | [diff] [blame] | 73 | uint32_t width; |
| 74 | uint32_t height; |
| 75 | uint8_t* image; |
| 76 | int32_t penX; |
| 77 | int32_t penY; |
| 78 | }; |
| 79 | |
| 80 | // After renderDropShadow returns, the called owns the memory in DropShadow.image |
| 81 | // and is responsible for releasing it when it's done with it |
| 82 | DropShadow renderDropShadow(SkPaint* paint, const char *text, uint32_t startIndex, |
Raph Levien | 416a847 | 2012-07-19 22:48:17 -0700 | [diff] [blame] | 83 | uint32_t len, int numGlyphs, uint32_t radius, const float* positions); |
Alex Sakhartchouk | f18136c | 2010-08-06 14:49:04 -0700 | [diff] [blame] | 84 | |
Romain Guy | e8cb9c14 | 2010-10-04 14:14:11 -0700 | [diff] [blame] | 85 | GLuint getTexture(bool linearFiltering = false) { |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 86 | checkInit(); |
Romain Guy | ae91c4c | 2012-05-14 14:00:27 -0700 | [diff] [blame] | 87 | |
Chet Haase | 2a47c14 | 2011-12-14 15:22:56 -0800 | [diff] [blame] | 88 | if (linearFiltering != mCurrentCacheTexture->mLinearFiltering) { |
| 89 | mCurrentCacheTexture->mLinearFiltering = linearFiltering; |
Romain Guy | e8cb9c14 | 2010-10-04 14:14:11 -0700 | [diff] [blame] | 90 | mLinearFiltering = linearFiltering; |
| 91 | const GLenum filtering = linearFiltering ? GL_LINEAR : GL_NEAREST; |
| 92 | |
Chet Haase | 7de0cb1 | 2011-12-05 16:35:38 -0800 | [diff] [blame] | 93 | glBindTexture(GL_TEXTURE_2D, mCurrentCacheTexture->mTextureId); |
Romain Guy | e8cb9c14 | 2010-10-04 14:14:11 -0700 | [diff] [blame] | 94 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, filtering); |
| 95 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, filtering); |
| 96 | } |
Romain Guy | ae91c4c | 2012-05-14 14:00:27 -0700 | [diff] [blame] | 97 | |
Chet Haase | 7de0cb1 | 2011-12-05 16:35:38 -0800 | [diff] [blame] | 98 | return mCurrentCacheTexture->mTextureId; |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 99 | } |
| 100 | |
Chet Haase | 7de0cb1 | 2011-12-05 16:35:38 -0800 | [diff] [blame] | 101 | uint32_t getCacheSize() const { |
| 102 | uint32_t size = 0; |
Chet Haase | 378e919 | 2012-08-15 15:54:54 -0700 | [diff] [blame] | 103 | for (uint32_t i = 0; i < mCacheTextures.size(); i++) { |
| 104 | CacheTexture* cacheTexture = mCacheTextures[i]; |
| 105 | if (cacheTexture != NULL && cacheTexture->mTexture != NULL) { |
| 106 | size += cacheTexture->mWidth * cacheTexture->mHeight; |
| 107 | } |
Chet Haase | 7de0cb1 | 2011-12-05 16:35:38 -0800 | [diff] [blame] | 108 | } |
| 109 | return size; |
Romain Guy | c15008e | 2010-11-10 11:59:15 -0800 | [diff] [blame] | 110 | } |
| 111 | |
Romain Guy | 9b1204b | 2012-09-04 15:22:57 -0700 | [diff] [blame^] | 112 | private: |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 113 | friend class Font; |
| 114 | |
Romain Guy | b45c0c9 | 2010-08-26 20:35:23 -0700 | [diff] [blame] | 115 | const uint8_t* mGammaTable; |
| 116 | |
Chet Haase | 2a47c14 | 2011-12-14 15:22:56 -0800 | [diff] [blame] | 117 | void allocateTextureMemory(CacheTexture* cacheTexture); |
Chet Haase | 9a82456 | 2011-12-16 15:44:59 -0800 | [diff] [blame] | 118 | void deallocateTextureMemory(CacheTexture* cacheTexture); |
Chet Haase | 7de0cb1 | 2011-12-05 16:35:38 -0800 | [diff] [blame] | 119 | void initTextTexture(); |
Romain Guy | 9777173 | 2012-02-28 18:17:02 -0800 | [diff] [blame] | 120 | CacheTexture* createCacheTexture(int width, int height, bool allocate); |
Chet Haase | 7de0cb1 | 2011-12-05 16:35:38 -0800 | [diff] [blame] | 121 | void cacheBitmap(const SkGlyph& glyph, CachedGlyphInfo* cachedGlyph, |
Chet Haase | f942cf1 | 2012-08-30 09:06:46 -0700 | [diff] [blame] | 122 | uint32_t *retOriginX, uint32_t *retOriginY, bool precaching); |
Chet Haase | 378e919 | 2012-08-15 15:54:54 -0700 | [diff] [blame] | 123 | CacheTexture* cacheBitmapInTexture(const SkGlyph& glyph, uint32_t* startX, uint32_t* startY); |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 124 | |
| 125 | void flushAllAndInvalidate(); |
| 126 | void initVertexArrayBuffers(); |
| 127 | |
| 128 | void checkInit(); |
Romain Guy | 671d6cf | 2012-01-18 12:39:17 -0800 | [diff] [blame] | 129 | void initRender(const Rect* clip, Rect* bounds); |
| 130 | void finishRender(); |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 131 | |
| 132 | void issueDrawCommand(); |
Romain Guy | 9777173 | 2012-02-28 18:17:02 -0800 | [diff] [blame] | 133 | void appendMeshQuadNoClip(float x1, float y1, float u1, float v1, |
| 134 | float x2, float y2, float u2, float v2, |
| 135 | float x3, float y3, float u3, float v3, |
| 136 | float x4, float y4, float u4, float v4, CacheTexture* texture); |
Romain Guy | d71dd36 | 2011-12-12 19:03:35 -0800 | [diff] [blame] | 137 | void appendMeshQuad(float x1, float y1, float u1, float v1, |
| 138 | float x2, float y2, float u2, float v2, |
| 139 | float x3, float y3, float u3, float v3, |
Chet Haase | 7de0cb1 | 2011-12-05 16:35:38 -0800 | [diff] [blame] | 140 | float x4, float y4, float u4, float v4, CacheTexture* texture); |
Romain Guy | 9777173 | 2012-02-28 18:17:02 -0800 | [diff] [blame] | 141 | void appendRotatedMeshQuad(float x1, float y1, float u1, float v1, |
| 142 | float x2, float y2, float u2, float v2, |
| 143 | float x3, float y3, float u3, float v3, |
| 144 | float x4, float y4, float u4, float v4, CacheTexture* texture); |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 145 | |
Romain Guy | 9b1204b | 2012-09-04 15:22:57 -0700 | [diff] [blame^] | 146 | void removeFont(const Font* font); |
| 147 | |
| 148 | void checkTextureUpdate(); |
| 149 | |
| 150 | void setTextureDirty() { |
| 151 | mUploadTexture = true; |
| 152 | } |
| 153 | |
Chet Haase | 7de0cb1 | 2011-12-05 16:35:38 -0800 | [diff] [blame] | 154 | uint32_t mSmallCacheWidth; |
| 155 | uint32_t mSmallCacheHeight; |
Chet Haase | eb32a49 | 2012-08-31 13:54:03 -0700 | [diff] [blame] | 156 | uint32_t mLargeCacheWidth; |
| 157 | uint32_t mLargeCacheHeight; |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 158 | |
Chet Haase | 378e919 | 2012-08-15 15:54:54 -0700 | [diff] [blame] | 159 | Vector<CacheTexture*> mCacheTextures; |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 160 | |
Romain Guy | 09147fb | 2010-07-22 13:08:20 -0700 | [diff] [blame] | 161 | Font* mCurrentFont; |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 162 | Vector<Font*> mActiveFonts; |
| 163 | |
Chet Haase | 7de0cb1 | 2011-12-05 16:35:38 -0800 | [diff] [blame] | 164 | CacheTexture* mCurrentCacheTexture; |
| 165 | CacheTexture* mLastCacheTexture; |
Chet Haase | 7de0cb1 | 2011-12-05 16:35:38 -0800 | [diff] [blame] | 166 | |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 167 | bool mUploadTexture; |
| 168 | |
| 169 | // Pointer to vertex data to speed up frame to frame work |
Romain Guy | 9b1204b | 2012-09-04 15:22:57 -0700 | [diff] [blame^] | 170 | float* mTextMesh; |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 171 | uint32_t mCurrentQuadIndex; |
| 172 | uint32_t mMaxNumberOfQuads; |
| 173 | |
| 174 | uint32_t mIndexBufferID; |
| 175 | |
Romain Guy | 09147fb | 2010-07-22 13:08:20 -0700 | [diff] [blame] | 176 | const Rect* mClip; |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 177 | Rect* mBounds; |
| 178 | bool mDrawn; |
Romain Guy | 09147fb | 2010-07-22 13:08:20 -0700 | [diff] [blame] | 179 | |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 180 | bool mInitialized; |
Alex Sakhartchouk | 89a524a | 2010-08-02 17:52:30 -0700 | [diff] [blame] | 181 | |
Romain Guy | e8cb9c14 | 2010-10-04 14:14:11 -0700 | [diff] [blame] | 182 | bool mLinearFiltering; |
| 183 | |
Romain Guy | 9b1204b | 2012-09-04 15:22:57 -0700 | [diff] [blame^] | 184 | /** We should consider multi-threading this code or using Renderscript **/ |
| 185 | static void computeGaussianWeights(float* weights, int32_t radius); |
| 186 | static void horizontalBlur(float* weights, int32_t radius, const uint8_t *source, uint8_t *dest, |
Romain Guy | 1e45aae | 2010-08-13 19:39:53 -0700 | [diff] [blame] | 187 | int32_t width, int32_t height); |
Romain Guy | 9b1204b | 2012-09-04 15:22:57 -0700 | [diff] [blame^] | 188 | static void verticalBlur(float* weights, int32_t radius, const uint8_t *source, uint8_t *dest, |
Romain Guy | 1e45aae | 2010-08-13 19:39:53 -0700 | [diff] [blame] | 189 | int32_t width, int32_t height); |
Romain Guy | 9b1204b | 2012-09-04 15:22:57 -0700 | [diff] [blame^] | 190 | static void blurImage(uint8_t* image, int32_t width, int32_t height, int32_t radius); |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 191 | }; |
| 192 | |
| 193 | }; // namespace uirenderer |
| 194 | }; // namespace android |
| 195 | |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 196 | #endif // ANDROID_HWUI_FONT_RENDERER_H |