joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
Herb Derby | 8624059 | 2018-05-24 16:12:31 -0400 | [diff] [blame] | 8 | #ifndef GrTextContext_DEFINED |
| 9 | #define GrTextContext_DEFINED |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 10 | |
joshualitt | 1acabf3 | 2015-12-10 09:10:10 -0800 | [diff] [blame] | 11 | #include "GrDistanceFieldAdjustTable.h" |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 12 | #include "GrGeometryProcessor.h" |
Herb Derby | 46dbfbb | 2018-07-27 15:36:49 -0400 | [diff] [blame^] | 13 | #include "GrTextBlob.h" |
Brian Salomon | 6f1d36c | 2017-01-13 12:02:17 -0500 | [diff] [blame] | 14 | #include "GrTextUtils.h" |
Herb Derby | 46dbfbb | 2018-07-27 15:36:49 -0400 | [diff] [blame^] | 15 | #include "SkGlyphRun.h" |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 16 | |
Hal Canary | 6f6961e | 2017-01-31 13:50:44 -0500 | [diff] [blame] | 17 | #if GR_TEST_UTILS |
Brian Salomon | 5ec9def | 2016-12-20 15:34:05 -0500 | [diff] [blame] | 18 | #include "GrDrawOpTest.h" |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 19 | #endif |
| 20 | |
Brian Salomon | 9afd371 | 2016-12-01 10:59:09 -0500 | [diff] [blame] | 21 | class GrDrawOp; |
joshualitt | b7133be | 2015-04-08 09:08:31 -0700 | [diff] [blame] | 22 | class GrTextBlobCache; |
joshualitt | 6c2c2b0 | 2015-07-24 10:37:00 -0700 | [diff] [blame] | 23 | class SkGlyph; |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 24 | |
| 25 | /* |
joshualitt | 8e84a1e | 2016-02-16 11:09:25 -0800 | [diff] [blame] | 26 | * Renders text using some kind of an atlas, ie BitmapText or DistanceField text |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 27 | */ |
Herb Derby | 26cbe51 | 2018-05-24 14:39:01 -0400 | [diff] [blame] | 28 | class GrTextContext { |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 29 | public: |
Brian Salomon | af59748 | 2017-11-07 16:23:34 -0500 | [diff] [blame] | 30 | struct Options { |
| 31 | /** |
| 32 | * Below this size (in device space) distance field text will not be used. Negative means |
| 33 | * use a default value. |
| 34 | */ |
| 35 | SkScalar fMinDistanceFieldFontSize = -1.f; |
| 36 | /** |
| 37 | * Above this size (in device space) distance field text will not be used and glyphs will |
| 38 | * be rendered from outline as individual paths. Negative means use a default value. |
| 39 | */ |
| 40 | SkScalar fMaxDistanceFieldFontSize = -1.f; |
Brian Salomon | b508696 | 2017-12-13 10:59:33 -0500 | [diff] [blame] | 41 | /** Forces all distance field vertices to use 3 components, not just when in perspective. */ |
| 42 | bool fDistanceFieldVerticesAlwaysHaveW = false; |
Brian Salomon | af59748 | 2017-11-07 16:23:34 -0500 | [diff] [blame] | 43 | }; |
| 44 | |
Herb Derby | 26cbe51 | 2018-05-24 14:39:01 -0400 | [diff] [blame] | 45 | static std::unique_ptr<GrTextContext> Make(const Options& options); |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 46 | |
Brian Salomon | f18b1d8 | 2017-10-27 11:30:49 -0400 | [diff] [blame] | 47 | void drawPosText(GrContext*, GrTextUtils::Target*, const GrClip&, const SkPaint&, |
Brian Salomon | 82f4431 | 2017-01-11 13:42:54 -0500 | [diff] [blame] | 48 | const SkMatrix& viewMatrix, const SkSurfaceProps&, const char text[], |
| 49 | size_t byteLength, const SkScalar pos[], int scalarsPerPosition, |
joshualitt | 8e84a1e | 2016-02-16 11:09:25 -0800 | [diff] [blame] | 50 | const SkPoint& offset, const SkIRect& regionClipBounds); |
Herb Derby | cddab25 | 2018-07-16 11:19:04 -0400 | [diff] [blame] | 51 | void drawGlyphRunList(GrContext*, GrTextUtils::Target*, const GrClip&, |
Herb Derby | b935cf8 | 2018-07-26 16:54:18 -0400 | [diff] [blame] | 52 | const SkMatrix& viewMatrix, const SkSurfaceProps&, const SkGlyphRunList&, |
Herb Derby | cddab25 | 2018-07-16 11:19:04 -0400 | [diff] [blame] | 53 | const SkIRect& clipBounds); |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 54 | |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 55 | std::unique_ptr<GrDrawOp> createOp_TestingOnly(GrContext*, |
| 56 | GrTextContext*, |
| 57 | GrRenderTargetContext*, |
| 58 | const SkPaint&, |
| 59 | const SkMatrix& viewMatrix, |
| 60 | const char* text, |
| 61 | int x, |
| 62 | int y); |
Robert Phillips | d2e9f76 | 2018-03-07 11:54:37 -0500 | [diff] [blame] | 63 | |
Khushal | 3e7548c | 2018-05-23 15:45:01 -0700 | [diff] [blame] | 64 | static void SanitizeOptions(Options* options); |
| 65 | static bool CanDrawAsDistanceFields(const SkPaint& skPaint, const SkMatrix& viewMatrix, |
| 66 | const SkSurfaceProps& props, |
| 67 | bool contextSupportsDistanceFieldText, |
| 68 | const Options& options); |
Herb Derby | 8624059 | 2018-05-24 16:12:31 -0400 | [diff] [blame] | 69 | static void InitDistanceFieldPaint(GrTextBlob* blob, |
Khushal | 3e7548c | 2018-05-23 15:45:01 -0700 | [diff] [blame] | 70 | SkPaint* skPaint, |
| 71 | const SkMatrix& viewMatrix, |
| 72 | const Options& options, |
| 73 | SkScalar* textRatio, |
| 74 | SkScalerContextFlags* flags); |
| 75 | |
Jim Van Verth | c401bb9 | 2018-02-15 14:05:24 -0500 | [diff] [blame] | 76 | class FallbackTextHelper { |
| 77 | public: |
| 78 | FallbackTextHelper(const SkMatrix& viewMatrix, |
Jim Van Verth | 080a928 | 2018-03-02 10:41:43 -0500 | [diff] [blame] | 79 | const SkPaint& pathPaint, |
Khushal | fa8ff09 | 2018-06-06 17:46:38 -0700 | [diff] [blame] | 80 | SkScalar maxTextSize, |
Jim Van Verth | c401bb9 | 2018-02-15 14:05:24 -0500 | [diff] [blame] | 81 | SkScalar textRatio) |
Khushal | fa8ff09 | 2018-06-06 17:46:38 -0700 | [diff] [blame] | 82 | : fViewMatrix(viewMatrix) |
| 83 | , fTextSize(pathPaint.getTextSize()) |
| 84 | , fMaxTextSize(maxTextSize) |
| 85 | , fTextRatio(textRatio) |
| 86 | , fTransformedFallbackTextSize(fMaxTextSize) |
| 87 | , fUseTransformedFallback(false) { |
Jim Van Verth | c401bb9 | 2018-02-15 14:05:24 -0500 | [diff] [blame] | 88 | fMaxScale = viewMatrix.getMaxScale(); |
| 89 | } |
| 90 | |
| 91 | void appendText(const SkGlyph& glyph, int count, const char* text, SkPoint glyphPos); |
Herb Derby | 8624059 | 2018-05-24 16:12:31 -0400 | [diff] [blame] | 92 | void drawText(GrTextBlob* blob, int runIndex, GrGlyphCache*, const SkSurfaceProps&, |
Robert Phillips | c4039ea | 2018-03-01 11:36:45 -0500 | [diff] [blame] | 93 | const GrTextUtils::Paint&, SkScalerContextFlags); |
Jim Van Verth | c401bb9 | 2018-02-15 14:05:24 -0500 | [diff] [blame] | 94 | |
Khushal | fa8ff09 | 2018-06-06 17:46:38 -0700 | [diff] [blame] | 95 | void initializeForDraw(SkPaint* paint, SkScalar* textRatio, SkMatrix* matrix) const; |
| 96 | const SkTDArray<char>& fallbackText() const { return fFallbackTxt; } |
| 97 | |
Jim Van Verth | c401bb9 | 2018-02-15 14:05:24 -0500 | [diff] [blame] | 98 | private: |
| 99 | SkTDArray<char> fFallbackTxt; |
| 100 | SkTDArray<SkPoint> fFallbackPos; |
| 101 | |
| 102 | const SkMatrix& fViewMatrix; |
Jim Van Verth | c401bb9 | 2018-02-15 14:05:24 -0500 | [diff] [blame] | 103 | SkScalar fTextSize; |
| 104 | SkScalar fMaxTextSize; |
Jim Van Verth | 080a928 | 2018-03-02 10:41:43 -0500 | [diff] [blame] | 105 | SkScalar fTextRatio; |
Jim Van Verth | b515ae7 | 2018-05-23 16:44:55 -0400 | [diff] [blame] | 106 | SkScalar fTransformedFallbackTextSize; |
Jim Van Verth | c401bb9 | 2018-02-15 14:05:24 -0500 | [diff] [blame] | 107 | SkScalar fMaxScale; |
Jim Van Verth | b515ae7 | 2018-05-23 16:44:55 -0400 | [diff] [blame] | 108 | bool fUseTransformedFallback; |
Jim Van Verth | c401bb9 | 2018-02-15 14:05:24 -0500 | [diff] [blame] | 109 | }; |
| 110 | |
Herb Derby | f4f6bbf | 2018-07-27 11:58:37 -0400 | [diff] [blame] | 111 | class FallbackGlyphRunHelper { |
| 112 | public: |
| 113 | FallbackGlyphRunHelper(const SkMatrix& viewMatrix, |
| 114 | const SkPaint& pathPaint, |
| 115 | SkScalar maxTextSize, |
| 116 | SkScalar textRatio) |
| 117 | : fViewMatrix(viewMatrix) |
| 118 | , fTextSize(pathPaint.getTextSize()) |
| 119 | , fMaxTextSize(maxTextSize) |
| 120 | , fTextRatio(textRatio) |
| 121 | , fTransformedFallbackTextSize(fMaxTextSize) |
| 122 | , fUseTransformedFallback(false) { |
| 123 | fMaxScale = viewMatrix.getMaxScale(); |
| 124 | } |
| 125 | |
| 126 | void appendText(const SkGlyph& glyph, SkGlyphID, SkPoint glyphPos); |
| 127 | void drawText(GrTextBlob* blob, int runIndex, GrGlyphCache*, const SkSurfaceProps&, |
| 128 | const GrTextUtils::Paint&, SkScalerContextFlags); |
| 129 | |
| 130 | void initializeForDraw(SkPaint* paint, SkScalar* textRatio, SkMatrix* matrix) const; |
| 131 | const std::vector<SkGlyphID>& fallbackText() const { return fFallbackTxt; } |
| 132 | |
| 133 | private: |
| 134 | std::vector<SkGlyphID> fFallbackTxt; |
| 135 | std::vector<SkPoint> fFallbackPos; |
| 136 | |
| 137 | const SkMatrix& fViewMatrix; |
| 138 | SkScalar fTextSize; |
| 139 | SkScalar fMaxTextSize; |
| 140 | SkScalar fTextRatio; |
| 141 | SkScalar fTransformedFallbackTextSize; |
| 142 | SkScalar fMaxScale; |
| 143 | bool fUseTransformedFallback; |
| 144 | }; |
| 145 | |
Khushal | fa8ff09 | 2018-06-06 17:46:38 -0700 | [diff] [blame] | 146 | private: |
| 147 | GrTextContext(const Options& options); |
| 148 | |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 149 | // sets up the descriptor on the blob and returns a detached cache. Client must attach |
Herb Derby | d8327a8 | 2018-01-22 14:39:27 -0500 | [diff] [blame] | 150 | static SkColor ComputeCanonicalColor(const SkPaint&, bool lcd); |
brianosman | a1e8f8d | 2016-04-08 06:47:54 -0700 | [diff] [blame] | 151 | // Determines if we need to use fake gamma (and contrast boost): |
Herb Derby | d8327a8 | 2018-01-22 14:39:27 -0500 | [diff] [blame] | 152 | static SkScalerContextFlags ComputeScalerContextFlags(const GrColorSpaceInfo&); |
Herb Derby | cddab25 | 2018-07-16 11:19:04 -0400 | [diff] [blame] | 153 | |
| 154 | void regenerateGlyphRunList(GrTextBlob* bmp, |
Robert Phillips | c4039ea | 2018-03-01 11:36:45 -0500 | [diff] [blame] | 155 | GrGlyphCache*, |
Brian Salomon | af59748 | 2017-11-07 16:23:34 -0500 | [diff] [blame] | 156 | const GrShaderCaps&, |
| 157 | const GrTextUtils::Paint&, |
Herb Derby | d8327a8 | 2018-01-22 14:39:27 -0500 | [diff] [blame] | 158 | SkScalerContextFlags scalerContextFlags, |
Brian Salomon | af59748 | 2017-11-07 16:23:34 -0500 | [diff] [blame] | 159 | const SkMatrix& viewMatrix, |
| 160 | const SkSurfaceProps&, |
Herb Derby | b935cf8 | 2018-07-26 16:54:18 -0400 | [diff] [blame] | 161 | const SkGlyphRunList& glyphRunList) const; |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 162 | |
Herb Derby | 8624059 | 2018-05-24 16:12:31 -0400 | [diff] [blame] | 163 | sk_sp<GrTextBlob> makeDrawPosTextBlob(GrTextBlobCache*, GrGlyphCache*, |
Herb Derby | d8327a8 | 2018-01-22 14:39:27 -0500 | [diff] [blame] | 164 | const GrShaderCaps&, |
| 165 | const GrTextUtils::Paint&, |
| 166 | SkScalerContextFlags scalerContextFlags, |
| 167 | const SkMatrix& viewMatrix, |
| 168 | const SkSurfaceProps&, |
| 169 | const char text[], size_t byteLength, |
| 170 | const SkScalar pos[], |
| 171 | int scalarsPerPosition, |
| 172 | const SkPoint& offset) const; |
Brian Salomon | 52db940 | 2017-11-07 14:58:55 -0500 | [diff] [blame] | 173 | |
Herb Derby | 8624059 | 2018-05-24 16:12:31 -0400 | [diff] [blame] | 174 | // Functions for appending BMP text to GrTextBlob |
Herb Derby | 8624059 | 2018-05-24 16:12:31 -0400 | [diff] [blame] | 175 | static void DrawBmpPosText(GrTextBlob*, int runIndex, GrGlyphCache*, |
Brian Salomon | 52db940 | 2017-11-07 14:58:55 -0500 | [diff] [blame] | 176 | const SkSurfaceProps&, const GrTextUtils::Paint& paint, |
Herb Derby | d8327a8 | 2018-01-22 14:39:27 -0500 | [diff] [blame] | 177 | SkScalerContextFlags scalerContextFlags, const SkMatrix& viewMatrix, |
Brian Salomon | 52db940 | 2017-11-07 14:58:55 -0500 | [diff] [blame] | 178 | const char text[], size_t byteLength, const SkScalar pos[], |
Jim Van Verth | c401bb9 | 2018-02-15 14:05:24 -0500 | [diff] [blame] | 179 | int scalarsPerPosition, const SkPoint& offset); |
Brian Salomon | 52db940 | 2017-11-07 14:58:55 -0500 | [diff] [blame] | 180 | |
Herb Derby | 46dbfbb | 2018-07-27 15:36:49 -0400 | [diff] [blame^] | 181 | static void DrawBmpGlyphRun(GrTextBlob*, int runIndex, GrGlyphCache*, |
| 182 | const SkSurfaceProps&, const GrTextUtils::Paint& paint, |
| 183 | SkScalerContextFlags scalerContextFlags, const SkMatrix& viewMatrix, |
| 184 | const SkGlyphRun& glyphRun, const SkPoint& offset); |
| 185 | |
Herb Derby | 8624059 | 2018-05-24 16:12:31 -0400 | [diff] [blame] | 186 | static void DrawBmpPosTextAsPaths(GrTextBlob*, int runIndex, GrGlyphCache*, |
Jim Van Verth | c401bb9 | 2018-02-15 14:05:24 -0500 | [diff] [blame] | 187 | const SkSurfaceProps&, const GrTextUtils::Paint& paint, |
| 188 | SkScalerContextFlags scalerContextFlags, |
| 189 | const SkMatrix& viewMatrix, |
Jim Van Verth | 54d9c88 | 2018-02-08 16:14:48 -0500 | [diff] [blame] | 190 | const char text[], size_t byteLength, |
| 191 | const SkScalar pos[], int scalarsPerPosition, |
Jim Van Verth | c401bb9 | 2018-02-15 14:05:24 -0500 | [diff] [blame] | 192 | const SkPoint& offset); |
Jim Van Verth | 54d9c88 | 2018-02-08 16:14:48 -0500 | [diff] [blame] | 193 | |
Brian Salomon | 52db940 | 2017-11-07 14:58:55 -0500 | [diff] [blame] | 194 | // functions for appending distance field text |
Herb Derby | 8624059 | 2018-05-24 16:12:31 -0400 | [diff] [blame] | 195 | void drawDFPosText(GrTextBlob* blob, int runIndex, GrGlyphCache*, |
Brian Salomon | af59748 | 2017-11-07 16:23:34 -0500 | [diff] [blame] | 196 | const SkSurfaceProps&, const GrTextUtils::Paint& paint, |
Herb Derby | d8327a8 | 2018-01-22 14:39:27 -0500 | [diff] [blame] | 197 | SkScalerContextFlags scalerContextFlags, |
| 198 | const SkMatrix& viewMatrix, const char text[], |
Brian Salomon | af59748 | 2017-11-07 16:23:34 -0500 | [diff] [blame] | 199 | size_t byteLength, const SkScalar pos[], int scalarsPerPosition, |
| 200 | const SkPoint& offset) const; |
Brian Salomon | 52db940 | 2017-11-07 14:58:55 -0500 | [diff] [blame] | 201 | |
Herb Derby | f4f6bbf | 2018-07-27 11:58:37 -0400 | [diff] [blame] | 202 | void drawDFGlyphRun(GrTextBlob* blob, int runIndex, GrGlyphCache*, |
| 203 | const SkSurfaceProps&, const GrTextUtils::Paint& paint, |
| 204 | SkScalerContextFlags scalerContextFlags, |
| 205 | const SkMatrix& viewMatrix, const SkGlyphRun& glyphRun, |
| 206 | const SkPoint& offset) const; |
| 207 | |
Herb Derby | 8624059 | 2018-05-24 16:12:31 -0400 | [diff] [blame] | 208 | static void BmpAppendGlyph(GrTextBlob*, int runIndex, GrGlyphCache*, |
Robert Phillips | caf1ebb | 2018-03-01 14:28:44 -0500 | [diff] [blame] | 209 | sk_sp<GrTextStrike>*, const SkGlyph&, SkScalar sx, SkScalar sy, |
Jim Van Verth | b515ae7 | 2018-05-23 16:44:55 -0400 | [diff] [blame] | 210 | GrColor color, SkGlyphCache*, SkScalar textRatio, bool needsXform); |
Brian Salomon | 52db940 | 2017-11-07 14:58:55 -0500 | [diff] [blame] | 211 | |
Herb Derby | 8624059 | 2018-05-24 16:12:31 -0400 | [diff] [blame] | 212 | static void DfAppendGlyph(GrTextBlob*, int runIndex, GrGlyphCache*, |
Robert Phillips | caf1ebb | 2018-03-01 14:28:44 -0500 | [diff] [blame] | 213 | sk_sp<GrTextStrike>*, const SkGlyph&, SkScalar sx, SkScalar sy, |
Jim Van Verth | f4c1316 | 2018-01-11 16:40:24 -0500 | [diff] [blame] | 214 | GrColor color, SkGlyphCache* cache, SkScalar textRatio); |
Brian Salomon | 52db940 | 2017-11-07 14:58:55 -0500 | [diff] [blame] | 215 | |
Hal Canary | 144caf5 | 2016-11-07 17:57:18 -0500 | [diff] [blame] | 216 | const GrDistanceFieldAdjustTable* dfAdjustTable() const { return fDistanceAdjustTable.get(); } |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 217 | |
Hal Canary | 144caf5 | 2016-11-07 17:57:18 -0500 | [diff] [blame] | 218 | sk_sp<const GrDistanceFieldAdjustTable> fDistanceAdjustTable; |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 219 | |
Khushal | 3e7548c | 2018-05-23 15:45:01 -0700 | [diff] [blame] | 220 | Options fOptions; |
Brian Salomon | af59748 | 2017-11-07 16:23:34 -0500 | [diff] [blame] | 221 | |
Hal Canary | 6f6961e | 2017-01-31 13:50:44 -0500 | [diff] [blame] | 222 | #if GR_TEST_UTILS |
Herb Derby | d8327a8 | 2018-01-22 14:39:27 -0500 | [diff] [blame] | 223 | static const SkScalerContextFlags kTextBlobOpScalerContextFlags = |
| 224 | SkScalerContextFlags::kFakeGammaAndBoostContrast; |
Brian Salomon | 44acb5b | 2017-07-18 19:59:24 -0400 | [diff] [blame] | 225 | GR_DRAW_OP_TEST_FRIEND(GrAtlasTextOp); |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 226 | #endif |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 227 | }; |
| 228 | |
Herb Derby | 8624059 | 2018-05-24 16:12:31 -0400 | [diff] [blame] | 229 | #endif // GrTextContext_DEFINED |