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 | |
Victoria Lease | 1e54681 | 2013-06-25 14:25:17 -0700 | [diff] [blame] | 20 | #include <utils/Functor.h> |
Romain Guy | e3a9b24 | 2013-01-08 11:15:30 -0800 | [diff] [blame] | 21 | #include <utils/LruCache.h> |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 22 | #include <utils/Vector.h> |
Mathias Agopian | 1f5762e | 2013-05-06 20:20:34 -0700 | [diff] [blame] | 23 | #include <utils/StrongPointer.h> |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 24 | |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 25 | #include <SkPaint.h> |
| 26 | |
| 27 | #include <GLES2/gl2.h> |
| 28 | |
Romain Guy | 9f5dab3 | 2012-09-04 12:55:44 -0700 | [diff] [blame] | 29 | #include "font/FontUtil.h" |
| 30 | #include "font/CacheTexture.h" |
| 31 | #include "font/CachedGlyphInfo.h" |
| 32 | #include "font/Font.h" |
Romain Guy | 115096f | 2013-03-19 11:32:41 -0700 | [diff] [blame] | 33 | #include "utils/SortedList.h" |
Romain Guy | 09147fb | 2010-07-22 13:08:20 -0700 | [diff] [blame] | 34 | |
Dan Morrill | e4d9a01 | 2013-03-28 18:10:43 -0700 | [diff] [blame] | 35 | #ifdef ANDROID_ENABLE_RENDERSCRIPT |
Tim Murray | 250b1cf | 2013-08-01 14:49:22 -0700 | [diff] [blame] | 36 | #include "RenderScript.h" |
Chris Craik | f2d8ccc | 2013-02-13 16:14:17 -0800 | [diff] [blame] | 37 | namespace RSC { |
| 38 | class Element; |
| 39 | class RS; |
| 40 | class ScriptIntrinsicBlur; |
Tim Murray | 250b1cf | 2013-08-01 14:49:22 -0700 | [diff] [blame] | 41 | class sp; |
Chris Craik | f2d8ccc | 2013-02-13 16:14:17 -0800 | [diff] [blame] | 42 | } |
Dan Morrill | e4d9a01 | 2013-03-28 18:10:43 -0700 | [diff] [blame] | 43 | #endif |
Chris Craik | f2d8ccc | 2013-02-13 16:14:17 -0800 | [diff] [blame] | 44 | |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 45 | namespace android { |
| 46 | namespace uirenderer { |
| 47 | |
Victoria Lease | 1e54681 | 2013-06-25 14:25:17 -0700 | [diff] [blame] | 48 | class OpenGLRenderer; |
| 49 | |
Romain Guy | 726aeba | 2011-06-01 14:52:00 -0700 | [diff] [blame] | 50 | /////////////////////////////////////////////////////////////////////////////// |
Victoria Lease | 1e54681 | 2013-06-25 14:25:17 -0700 | [diff] [blame] | 51 | // TextSetupFunctor |
| 52 | /////////////////////////////////////////////////////////////////////////////// |
| 53 | class TextSetupFunctor: public Functor { |
| 54 | public: |
| 55 | struct Data { |
| 56 | Data(GLenum glyphFormat) : glyphFormat(glyphFormat) { |
| 57 | } |
| 58 | |
| 59 | GLenum glyphFormat; |
| 60 | }; |
| 61 | |
| 62 | TextSetupFunctor(OpenGLRenderer* renderer, float x, float y, bool pureTranslate, |
Chris Craik | d218a92 | 2014-01-02 17:13:34 -0800 | [diff] [blame] | 63 | int alpha, SkXfermode::Mode mode, const SkPaint* paint): Functor(), |
Victoria Lease | 1e54681 | 2013-06-25 14:25:17 -0700 | [diff] [blame] | 64 | renderer(renderer), x(x), y(y), pureTranslate(pureTranslate), |
| 65 | alpha(alpha), mode(mode), paint(paint) { |
| 66 | } |
| 67 | ~TextSetupFunctor() { } |
| 68 | |
Chris Craik | e84a208 | 2014-12-22 14:28:49 -0800 | [diff] [blame] | 69 | status_t operator ()(int what, void* data) override; |
Victoria Lease | 1e54681 | 2013-06-25 14:25:17 -0700 | [diff] [blame] | 70 | |
| 71 | OpenGLRenderer* renderer; |
| 72 | float x; |
| 73 | float y; |
| 74 | bool pureTranslate; |
| 75 | int alpha; |
| 76 | SkXfermode::Mode mode; |
Chris Craik | d218a92 | 2014-01-02 17:13:34 -0800 | [diff] [blame] | 77 | const SkPaint* paint; |
Victoria Lease | 1e54681 | 2013-06-25 14:25:17 -0700 | [diff] [blame] | 78 | }; |
| 79 | |
| 80 | /////////////////////////////////////////////////////////////////////////////// |
| 81 | // FontRenderer |
Romain Guy | 726aeba | 2011-06-01 14:52:00 -0700 | [diff] [blame] | 82 | /////////////////////////////////////////////////////////////////////////////// |
| 83 | |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 84 | class FontRenderer { |
| 85 | public: |
| 86 | FontRenderer(); |
| 87 | ~FontRenderer(); |
| 88 | |
Victoria Lease | 1e54681 | 2013-06-25 14:25:17 -0700 | [diff] [blame] | 89 | void flushLargeCaches(Vector<CacheTexture*>& cacheTextures); |
Chet Haase | 9a82456 | 2011-12-16 15:44:59 -0800 | [diff] [blame] | 90 | void flushLargeCaches(); |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 91 | |
Romain Guy | b45c0c9 | 2010-08-26 20:35:23 -0700 | [diff] [blame] | 92 | void setGammaTable(const uint8_t* gammaTable) { |
| 93 | mGammaTable = gammaTable; |
| 94 | } |
| 95 | |
Chris Craik | 59744b7 | 2014-07-01 17:56:52 -0700 | [diff] [blame] | 96 | void setFont(const SkPaint* paint, const SkMatrix& matrix); |
Chet Haase | e816bae | 2012-08-09 13:39:02 -0700 | [diff] [blame] | 97 | |
Chris Craik | 59744b7 | 2014-07-01 17:56:52 -0700 | [diff] [blame] | 98 | void precache(const SkPaint* paint, const char* text, int numGlyphs, const SkMatrix& matrix); |
Romain Guy | cf51a41 | 2013-04-08 19:40:31 -0700 | [diff] [blame] | 99 | void endPrecaching(); |
Chet Haase | e816bae | 2012-08-09 13:39:02 -0700 | [diff] [blame] | 100 | |
Romain Guy | 671d6cf | 2012-01-18 12:39:17 -0800 | [diff] [blame] | 101 | // bounds is an out parameter |
Chris Craik | d218a92 | 2014-01-02 17:13:34 -0800 | [diff] [blame] | 102 | bool renderPosText(const SkPaint* paint, const Rect* clip, const char *text, |
| 103 | uint32_t startIndex, uint32_t len, int numGlyphs, int x, int y, const float* positions, |
| 104 | Rect* bounds, Functor* functor, bool forceFinish = true); |
Chris Craik | 527a3aa | 2013-03-04 10:19:31 -0800 | [diff] [blame] | 105 | |
Romain Guy | 9777173 | 2012-02-28 18:17:02 -0800 | [diff] [blame] | 106 | // bounds is an out parameter |
Chris Craik | d218a92 | 2014-01-02 17:13:34 -0800 | [diff] [blame] | 107 | bool renderTextOnPath(const SkPaint* paint, const Rect* clip, const char *text, |
| 108 | uint32_t startIndex, uint32_t len, int numGlyphs, const SkPath* path, |
| 109 | float hOffset, float vOffset, Rect* bounds, Functor* functor); |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 110 | |
Alex Sakhartchouk | f18136c | 2010-08-06 14:49:04 -0700 | [diff] [blame] | 111 | struct DropShadow { |
Romain Guy | 1e45aae | 2010-08-13 19:39:53 -0700 | [diff] [blame] | 112 | DropShadow() { }; |
| 113 | |
| 114 | DropShadow(const DropShadow& dropShadow): |
| 115 | width(dropShadow.width), height(dropShadow.height), |
| 116 | image(dropShadow.image), penX(dropShadow.penX), |
| 117 | penY(dropShadow.penY) { |
| 118 | } |
| 119 | |
Alex Sakhartchouk | f18136c | 2010-08-06 14:49:04 -0700 | [diff] [blame] | 120 | uint32_t width; |
| 121 | uint32_t height; |
| 122 | uint8_t* image; |
| 123 | int32_t penX; |
| 124 | int32_t penY; |
| 125 | }; |
| 126 | |
| 127 | // After renderDropShadow returns, the called owns the memory in DropShadow.image |
| 128 | // and is responsible for releasing it when it's done with it |
Chris Craik | d218a92 | 2014-01-02 17:13:34 -0800 | [diff] [blame] | 129 | DropShadow renderDropShadow(const SkPaint* paint, const char *text, uint32_t startIndex, |
Derek Sollenberger | e392c81 | 2014-05-21 11:25:22 -0400 | [diff] [blame] | 130 | uint32_t len, int numGlyphs, float radius, const float* positions); |
Alex Sakhartchouk | f18136c | 2010-08-06 14:49:04 -0700 | [diff] [blame] | 131 | |
Romain Guy | 257ae35 | 2013-03-20 16:31:12 -0700 | [diff] [blame] | 132 | void setTextureFiltering(bool linearFiltering) { |
Romain Guy | 8087246 | 2012-09-04 16:42:01 -0700 | [diff] [blame] | 133 | mLinearFiltering = linearFiltering; |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 134 | } |
| 135 | |
Victoria Lease | 1e54681 | 2013-06-25 14:25:17 -0700 | [diff] [blame] | 136 | uint32_t getCacheSize(GLenum format) const; |
Romain Guy | c15008e | 2010-11-10 11:59:15 -0800 | [diff] [blame] | 137 | |
Romain Guy | 9b1204b | 2012-09-04 15:22:57 -0700 | [diff] [blame] | 138 | private: |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 139 | friend class Font; |
| 140 | |
Romain Guy | b45c0c9 | 2010-08-26 20:35:23 -0700 | [diff] [blame] | 141 | const uint8_t* mGammaTable; |
| 142 | |
Chet Haase | 2a47c14 | 2011-12-14 15:22:56 -0800 | [diff] [blame] | 143 | void allocateTextureMemory(CacheTexture* cacheTexture); |
Chet Haase | 9a82456 | 2011-12-16 15:44:59 -0800 | [diff] [blame] | 144 | void deallocateTextureMemory(CacheTexture* cacheTexture); |
Chet Haase | 7de0cb1 | 2011-12-05 16:35:38 -0800 | [diff] [blame] | 145 | void initTextTexture(); |
Victoria Lease | 1e54681 | 2013-06-25 14:25:17 -0700 | [diff] [blame] | 146 | CacheTexture* createCacheTexture(int width, int height, GLenum format, bool allocate); |
Chet Haase | 7de0cb1 | 2011-12-05 16:35:38 -0800 | [diff] [blame] | 147 | void cacheBitmap(const SkGlyph& glyph, CachedGlyphInfo* cachedGlyph, |
Chet Haase | f942cf1 | 2012-08-30 09:06:46 -0700 | [diff] [blame] | 148 | uint32_t *retOriginX, uint32_t *retOriginY, bool precaching); |
Victoria Lease | 1e54681 | 2013-06-25 14:25:17 -0700 | [diff] [blame] | 149 | CacheTexture* cacheBitmapInTexture(Vector<CacheTexture*>& cacheTextures, const SkGlyph& glyph, |
| 150 | uint32_t* startX, uint32_t* startY); |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 151 | |
| 152 | void flushAllAndInvalidate(); |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 153 | |
| 154 | void checkInit(); |
Romain Guy | 257ae35 | 2013-03-20 16:31:12 -0700 | [diff] [blame] | 155 | void initRender(const Rect* clip, Rect* bounds, Functor* functor); |
Romain Guy | 671d6cf | 2012-01-18 12:39:17 -0800 | [diff] [blame] | 156 | void finishRender(); |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 157 | |
Victoria Lease | 1e54681 | 2013-06-25 14:25:17 -0700 | [diff] [blame] | 158 | void issueDrawCommand(Vector<CacheTexture*>& cacheTextures); |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 159 | void issueDrawCommand(); |
Romain Guy | 9777173 | 2012-02-28 18:17:02 -0800 | [diff] [blame] | 160 | void appendMeshQuadNoClip(float x1, float y1, float u1, float v1, |
| 161 | float x2, float y2, float u2, float v2, |
| 162 | float x3, float y3, float u3, float v3, |
| 163 | float x4, float y4, float u4, float v4, CacheTexture* texture); |
Romain Guy | d71dd36 | 2011-12-12 19:03:35 -0800 | [diff] [blame] | 164 | void appendMeshQuad(float x1, float y1, float u1, float v1, |
| 165 | float x2, float y2, float u2, float v2, |
| 166 | float x3, float y3, float u3, float v3, |
Chet Haase | 7de0cb1 | 2011-12-05 16:35:38 -0800 | [diff] [blame] | 167 | float x4, float y4, float u4, float v4, CacheTexture* texture); |
Romain Guy | 9777173 | 2012-02-28 18:17:02 -0800 | [diff] [blame] | 168 | void appendRotatedMeshQuad(float x1, float y1, float u1, float v1, |
| 169 | float x2, float y2, float u2, float v2, |
| 170 | float x3, float y3, float u3, float v3, |
| 171 | float x4, float y4, float u4, float v4, CacheTexture* texture); |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 172 | |
Romain Guy | 9b1204b | 2012-09-04 15:22:57 -0700 | [diff] [blame] | 173 | void removeFont(const Font* font); |
| 174 | |
| 175 | void checkTextureUpdate(); |
| 176 | |
| 177 | void setTextureDirty() { |
| 178 | mUploadTexture = true; |
| 179 | } |
| 180 | |
Chet Haase | 7de0cb1 | 2011-12-05 16:35:38 -0800 | [diff] [blame] | 181 | uint32_t mSmallCacheWidth; |
| 182 | uint32_t mSmallCacheHeight; |
Chet Haase | eb32a49 | 2012-08-31 13:54:03 -0700 | [diff] [blame] | 183 | uint32_t mLargeCacheWidth; |
| 184 | uint32_t mLargeCacheHeight; |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 185 | |
Victoria Lease | 1e54681 | 2013-06-25 14:25:17 -0700 | [diff] [blame] | 186 | Vector<CacheTexture*> mACacheTextures; |
| 187 | Vector<CacheTexture*> mRGBACacheTextures; |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 188 | |
Romain Guy | 09147fb | 2010-07-22 13:08:20 -0700 | [diff] [blame] | 189 | Font* mCurrentFont; |
Romain Guy | e3a9b24 | 2013-01-08 11:15:30 -0800 | [diff] [blame] | 190 | LruCache<Font::FontDescription, Font*> mActiveFonts; |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 191 | |
Chet Haase | 7de0cb1 | 2011-12-05 16:35:38 -0800 | [diff] [blame] | 192 | CacheTexture* mCurrentCacheTexture; |
Chet Haase | 7de0cb1 | 2011-12-05 16:35:38 -0800 | [diff] [blame] | 193 | |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 194 | bool mUploadTexture; |
| 195 | |
Romain Guy | 257ae35 | 2013-03-20 16:31:12 -0700 | [diff] [blame] | 196 | Functor* mFunctor; |
Romain Guy | 09147fb | 2010-07-22 13:08:20 -0700 | [diff] [blame] | 197 | const Rect* mClip; |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 198 | Rect* mBounds; |
| 199 | bool mDrawn; |
Romain Guy | 09147fb | 2010-07-22 13:08:20 -0700 | [diff] [blame] | 200 | |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 201 | bool mInitialized; |
Alex Sakhartchouk | 89a524a | 2010-08-02 17:52:30 -0700 | [diff] [blame] | 202 | |
Romain Guy | e8cb9c14 | 2010-10-04 14:14:11 -0700 | [diff] [blame] | 203 | bool mLinearFiltering; |
| 204 | |
Dan Morrill | e4d9a01 | 2013-03-28 18:10:43 -0700 | [diff] [blame] | 205 | #ifdef ANDROID_ENABLE_RENDERSCRIPT |
Chris Craik | f2d8ccc | 2013-02-13 16:14:17 -0800 | [diff] [blame] | 206 | // RS constructs |
Tim Murray | 250b1cf | 2013-08-01 14:49:22 -0700 | [diff] [blame] | 207 | RSC::sp<RSC::RS> mRs; |
| 208 | RSC::sp<const RSC::Element> mRsElement; |
| 209 | RSC::sp<RSC::ScriptIntrinsicBlur> mRsScript; |
Dan Morrill | e4d9a01 | 2013-03-28 18:10:43 -0700 | [diff] [blame] | 210 | #endif |
Chris Craik | f2d8ccc | 2013-02-13 16:14:17 -0800 | [diff] [blame] | 211 | |
Romain Guy | 9b1204b | 2012-09-04 15:22:57 -0700 | [diff] [blame] | 212 | static void computeGaussianWeights(float* weights, int32_t radius); |
| 213 | 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] | 214 | int32_t width, int32_t height); |
Romain Guy | 9b1204b | 2012-09-04 15:22:57 -0700 | [diff] [blame] | 215 | 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] | 216 | int32_t width, int32_t height); |
Chris Craik | f2d8ccc | 2013-02-13 16:14:17 -0800 | [diff] [blame] | 217 | |
| 218 | // the input image handle may have its pointer replaced (to avoid copies) |
Derek Sollenberger | e392c81 | 2014-05-21 11:25:22 -0400 | [diff] [blame] | 219 | void blurImage(uint8_t** image, int32_t width, int32_t height, float radius); |
Romain Guy | 694b519 | 2010-07-21 21:33:20 -0700 | [diff] [blame] | 220 | }; |
| 221 | |
| 222 | }; // namespace uirenderer |
| 223 | }; // namespace android |
| 224 | |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 225 | #endif // ANDROID_HWUI_FONT_RENDERER_H |