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 | e3a9b24 | 2013-01-08 11:15:30 -0800 | [diff] [blame] | 20 | #include <utils/LruCache.h> |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 21 | #include <utils/Vector.h> |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 22 | |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 23 | #include <SkPaint.h> |
| 24 | |
| 25 | #include <GLES2/gl2.h> |
| 26 | |
Romain Guy | 9f5dab3 | 2012-09-04 12:55:44 -0700 | [diff] [blame] | 27 | #include "font/FontUtil.h" |
| 28 | #include "font/CacheTexture.h" |
| 29 | #include "font/CachedGlyphInfo.h" |
| 30 | #include "font/Font.h" |
Romain Guy | 115096f | 2013-03-19 11:32:41 -0700 | [diff] [blame^] | 31 | #include "utils/SortedList.h" |
Romain Guy | e3a9b24 | 2013-01-08 11:15:30 -0800 | [diff] [blame] | 32 | #include "Matrix.h" |
Romain Guy | 51769a6 | 2010-07-23 00:28:00 -0700 | [diff] [blame] | 33 | #include "Properties.h" |
Romain Guy | 09147fb | 2010-07-22 13:08:20 -0700 | [diff] [blame] | 34 | |
Chris Craik | f2d8ccc | 2013-02-13 16:14:17 -0800 | [diff] [blame] | 35 | namespace RSC { |
| 36 | class Element; |
| 37 | class RS; |
| 38 | class ScriptIntrinsicBlur; |
| 39 | } |
| 40 | |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 41 | namespace android { |
| 42 | namespace uirenderer { |
| 43 | |
Romain Guy | 726aeba | 2011-06-01 14:52:00 -0700 | [diff] [blame] | 44 | /////////////////////////////////////////////////////////////////////////////// |
Romain Guy | 726aeba | 2011-06-01 14:52:00 -0700 | [diff] [blame] | 45 | // Renderer |
| 46 | /////////////////////////////////////////////////////////////////////////////// |
| 47 | |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 48 | class FontRenderer { |
| 49 | public: |
| 50 | FontRenderer(); |
| 51 | ~FontRenderer(); |
| 52 | |
Chet Haase | 9a82456 | 2011-12-16 15:44:59 -0800 | [diff] [blame] | 53 | void flushLargeCaches(); |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 54 | |
Romain Guy | b45c0c9 | 2010-08-26 20:35:23 -0700 | [diff] [blame] | 55 | void setGammaTable(const uint8_t* gammaTable) { |
| 56 | mGammaTable = gammaTable; |
| 57 | } |
| 58 | |
Romain Guy | e3a9b24 | 2013-01-08 11:15:30 -0800 | [diff] [blame] | 59 | void setFont(SkPaint* paint, const mat4& matrix); |
Chet Haase | e816bae | 2012-08-09 13:39:02 -0700 | [diff] [blame] | 60 | |
Romain Guy | e3a9b24 | 2013-01-08 11:15:30 -0800 | [diff] [blame] | 61 | void precache(SkPaint* paint, const char* text, int numGlyphs, const mat4& matrix); |
Chet Haase | e816bae | 2012-08-09 13:39:02 -0700 | [diff] [blame] | 62 | |
Romain Guy | 671d6cf | 2012-01-18 12:39:17 -0800 | [diff] [blame] | 63 | // bounds is an out parameter |
Romain Guy | 671d6cf | 2012-01-18 12:39:17 -0800 | [diff] [blame] | 64 | bool renderPosText(SkPaint* paint, const Rect* clip, const char *text, uint32_t startIndex, |
| 65 | 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] | 66 | // bounds is an out parameter |
| 67 | bool renderTextOnPath(SkPaint* paint, const Rect* clip, const char *text, uint32_t startIndex, |
| 68 | 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] | 69 | |
Alex Sakhartchouk | f18136c | 2010-08-06 14:49:04 -0700 | [diff] [blame] | 70 | struct DropShadow { |
Romain Guy | 1e45aae | 2010-08-13 19:39:53 -0700 | [diff] [blame] | 71 | DropShadow() { }; |
| 72 | |
| 73 | DropShadow(const DropShadow& dropShadow): |
| 74 | width(dropShadow.width), height(dropShadow.height), |
| 75 | image(dropShadow.image), penX(dropShadow.penX), |
| 76 | penY(dropShadow.penY) { |
| 77 | } |
| 78 | |
Alex Sakhartchouk | f18136c | 2010-08-06 14:49:04 -0700 | [diff] [blame] | 79 | uint32_t width; |
| 80 | uint32_t height; |
| 81 | uint8_t* image; |
| 82 | int32_t penX; |
| 83 | int32_t penY; |
| 84 | }; |
| 85 | |
| 86 | // After renderDropShadow returns, the called owns the memory in DropShadow.image |
| 87 | // and is responsible for releasing it when it's done with it |
| 88 | DropShadow renderDropShadow(SkPaint* paint, const char *text, uint32_t startIndex, |
Raph Levien | 416a847 | 2012-07-19 22:48:17 -0700 | [diff] [blame] | 89 | uint32_t len, int numGlyphs, uint32_t radius, const float* positions); |
Alex Sakhartchouk | f18136c | 2010-08-06 14:49:04 -0700 | [diff] [blame] | 90 | |
Romain Guy | e8cb9c14 | 2010-10-04 14:14:11 -0700 | [diff] [blame] | 91 | GLuint getTexture(bool linearFiltering = false) { |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 92 | checkInit(); |
Romain Guy | ae91c4c | 2012-05-14 14:00:27 -0700 | [diff] [blame] | 93 | |
Romain Guy | 8087246 | 2012-09-04 16:42:01 -0700 | [diff] [blame] | 94 | mCurrentCacheTexture->setLinearFiltering(linearFiltering); |
| 95 | mLinearFiltering = linearFiltering; |
Romain Guy | e8cb9c14 | 2010-10-04 14:14:11 -0700 | [diff] [blame] | 96 | |
Romain Guy | 8087246 | 2012-09-04 16:42:01 -0700 | [diff] [blame] | 97 | return mCurrentCacheTexture->getTextureId(); |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 98 | } |
| 99 | |
Chet Haase | 7de0cb1 | 2011-12-05 16:35:38 -0800 | [diff] [blame] | 100 | uint32_t getCacheSize() const { |
| 101 | uint32_t size = 0; |
Chet Haase | 378e919 | 2012-08-15 15:54:54 -0700 | [diff] [blame] | 102 | for (uint32_t i = 0; i < mCacheTextures.size(); i++) { |
| 103 | CacheTexture* cacheTexture = mCacheTextures[i]; |
Romain Guy | 8087246 | 2012-09-04 16:42:01 -0700 | [diff] [blame] | 104 | if (cacheTexture && cacheTexture->getTexture()) { |
| 105 | size += cacheTexture->getWidth() * cacheTexture->getHeight(); |
Chet Haase | 378e919 | 2012-08-15 15:54:54 -0700 | [diff] [blame] | 106 | } |
Chet Haase | 7de0cb1 | 2011-12-05 16:35:38 -0800 | [diff] [blame] | 107 | } |
| 108 | return size; |
Romain Guy | c15008e | 2010-11-10 11:59:15 -0800 | [diff] [blame] | 109 | } |
| 110 | |
Romain Guy | 9b1204b | 2012-09-04 15:22:57 -0700 | [diff] [blame] | 111 | private: |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 112 | friend class Font; |
| 113 | |
Romain Guy | b45c0c9 | 2010-08-26 20:35:23 -0700 | [diff] [blame] | 114 | const uint8_t* mGammaTable; |
| 115 | |
Chet Haase | 2a47c14 | 2011-12-14 15:22:56 -0800 | [diff] [blame] | 116 | void allocateTextureMemory(CacheTexture* cacheTexture); |
Chet Haase | 9a82456 | 2011-12-16 15:44:59 -0800 | [diff] [blame] | 117 | void deallocateTextureMemory(CacheTexture* cacheTexture); |
Chet Haase | 7de0cb1 | 2011-12-05 16:35:38 -0800 | [diff] [blame] | 118 | void initTextTexture(); |
Romain Guy | 9777173 | 2012-02-28 18:17:02 -0800 | [diff] [blame] | 119 | CacheTexture* createCacheTexture(int width, int height, bool allocate); |
Chet Haase | 7de0cb1 | 2011-12-05 16:35:38 -0800 | [diff] [blame] | 120 | void cacheBitmap(const SkGlyph& glyph, CachedGlyphInfo* cachedGlyph, |
Chet Haase | f942cf1 | 2012-08-30 09:06:46 -0700 | [diff] [blame] | 121 | uint32_t *retOriginX, uint32_t *retOriginY, bool precaching); |
Chet Haase | 378e919 | 2012-08-15 15:54:54 -0700 | [diff] [blame] | 122 | CacheTexture* cacheBitmapInTexture(const SkGlyph& glyph, uint32_t* startX, uint32_t* startY); |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 123 | |
| 124 | void flushAllAndInvalidate(); |
| 125 | void initVertexArrayBuffers(); |
| 126 | |
| 127 | void checkInit(); |
Romain Guy | 671d6cf | 2012-01-18 12:39:17 -0800 | [diff] [blame] | 128 | void initRender(const Rect* clip, Rect* bounds); |
| 129 | void finishRender(); |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 130 | |
| 131 | void issueDrawCommand(); |
Romain Guy | 9777173 | 2012-02-28 18:17:02 -0800 | [diff] [blame] | 132 | void appendMeshQuadNoClip(float x1, float y1, float u1, float v1, |
| 133 | float x2, float y2, float u2, float v2, |
| 134 | float x3, float y3, float u3, float v3, |
| 135 | float x4, float y4, float u4, float v4, CacheTexture* texture); |
Romain Guy | d71dd36 | 2011-12-12 19:03:35 -0800 | [diff] [blame] | 136 | void appendMeshQuad(float x1, float y1, float u1, float v1, |
| 137 | float x2, float y2, float u2, float v2, |
| 138 | float x3, float y3, float u3, float v3, |
Chet Haase | 7de0cb1 | 2011-12-05 16:35:38 -0800 | [diff] [blame] | 139 | float x4, float y4, float u4, float v4, CacheTexture* texture); |
Romain Guy | 9777173 | 2012-02-28 18:17:02 -0800 | [diff] [blame] | 140 | void appendRotatedMeshQuad(float x1, float y1, float u1, float v1, |
| 141 | float x2, float y2, float u2, float v2, |
| 142 | float x3, float y3, float u3, float v3, |
| 143 | float x4, float y4, float u4, float v4, CacheTexture* texture); |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 144 | |
Romain Guy | 9b1204b | 2012-09-04 15:22:57 -0700 | [diff] [blame] | 145 | void removeFont(const Font* font); |
| 146 | |
Sangkyu Lee | 7bb3cfe | 2012-11-16 00:03:17 +0900 | [diff] [blame] | 147 | void updateDrawParams(); |
Romain Guy | 9b1204b | 2012-09-04 15:22:57 -0700 | [diff] [blame] | 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 | e3a9b24 | 2013-01-08 11:15:30 -0800 | [diff] [blame] | 162 | LruCache<Font::FontDescription, Font*> mActiveFonts; |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 163 | |
Chet Haase | 7de0cb1 | 2011-12-05 16:35:38 -0800 | [diff] [blame] | 164 | CacheTexture* mCurrentCacheTexture; |
Chet Haase | 7de0cb1 | 2011-12-05 16:35:38 -0800 | [diff] [blame] | 165 | |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 166 | bool mUploadTexture; |
| 167 | |
| 168 | // Pointer to vertex data to speed up frame to frame work |
Romain Guy | 9b1204b | 2012-09-04 15:22:57 -0700 | [diff] [blame] | 169 | float* mTextMesh; |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 170 | uint32_t mCurrentQuadIndex; |
Sangkyu Lee | 7bb3cfe | 2012-11-16 00:03:17 +0900 | [diff] [blame] | 171 | uint32_t mLastQuadIndex; |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 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 | 115096f | 2013-03-19 11:32:41 -0700 | [diff] [blame^] | 184 | struct TextBatch { |
| 185 | TextBatch(): offset(NULL), count(0), texture(NULL) { |
| 186 | } |
| 187 | |
| 188 | TextBatch(uint16_t* offset, uint32_t count, CacheTexture* texture): |
| 189 | offset(offset), count(count), texture(texture) { |
| 190 | } |
| 191 | |
| 192 | static int compare(const TextBatch& lhs, const TextBatch& rhs) { |
| 193 | return lhs.texture->getTextureId() - rhs.texture->getTextureId(); |
| 194 | } |
| 195 | |
| 196 | friend inline int strictly_order_type(const TextBatch& lhs, const TextBatch& rhs) { |
| 197 | return compare(lhs, rhs) < 0; |
| 198 | } |
| 199 | |
| 200 | friend inline int compare_type(const TextBatch& lhs, const TextBatch& rhs) { |
| 201 | return compare(lhs, rhs); |
| 202 | } |
| 203 | |
| 204 | uint16_t* offset; |
| 205 | uint32_t count; |
| 206 | CacheTexture* texture; |
| 207 | }; |
| 208 | |
| 209 | SortedList<TextBatch> mDrawBatch; |
Sangkyu Lee | 7bb3cfe | 2012-11-16 00:03:17 +0900 | [diff] [blame] | 210 | |
Chris Craik | f2d8ccc | 2013-02-13 16:14:17 -0800 | [diff] [blame] | 211 | // RS constructs |
| 212 | sp<RSC::RS> mRs; |
| 213 | sp<const RSC::Element> mRsElement; |
| 214 | sp<RSC::ScriptIntrinsicBlur> mRsScript; |
| 215 | |
Romain Guy | 9b1204b | 2012-09-04 15:22:57 -0700 | [diff] [blame] | 216 | static void computeGaussianWeights(float* weights, int32_t radius); |
| 217 | 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] | 218 | int32_t width, int32_t height); |
Romain Guy | 9b1204b | 2012-09-04 15:22:57 -0700 | [diff] [blame] | 219 | 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] | 220 | int32_t width, int32_t height); |
Chris Craik | f2d8ccc | 2013-02-13 16:14:17 -0800 | [diff] [blame] | 221 | |
| 222 | // the input image handle may have its pointer replaced (to avoid copies) |
| 223 | 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] | 224 | }; |
| 225 | |
| 226 | }; // namespace uirenderer |
| 227 | }; // namespace android |
| 228 | |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 229 | #endif // ANDROID_HWUI_FONT_RENDERER_H |