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