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" |
Herb Derby | c1b482c | 2018-08-09 15:02:27 -0400 | [diff] [blame] | 14 | #include "GrTextTarget.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 | |
Herb Derby | c1b482c | 2018-08-09 15:02:27 -0400 | [diff] [blame] | 47 | void drawGlyphRunList(GrContext*, GrTextTarget*, const GrClip&, |
Robert Phillips | e4643cc | 2018-08-14 13:01:29 -0400 | [diff] [blame^] | 48 | const SkMatrix& viewMatrix, const SkSurfaceProps&, const SkGlyphRunList&); |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 49 | |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 50 | std::unique_ptr<GrDrawOp> createOp_TestingOnly(GrContext*, |
| 51 | GrTextContext*, |
| 52 | GrRenderTargetContext*, |
| 53 | const SkPaint&, |
| 54 | const SkMatrix& viewMatrix, |
| 55 | const char* text, |
| 56 | int x, |
| 57 | int y); |
Robert Phillips | d2e9f76 | 2018-03-07 11:54:37 -0500 | [diff] [blame] | 58 | |
Khushal | 3e7548c | 2018-05-23 15:45:01 -0700 | [diff] [blame] | 59 | static void SanitizeOptions(Options* options); |
| 60 | static bool CanDrawAsDistanceFields(const SkPaint& skPaint, const SkMatrix& viewMatrix, |
| 61 | const SkSurfaceProps& props, |
| 62 | bool contextSupportsDistanceFieldText, |
| 63 | const Options& options); |
Herb Derby | 8624059 | 2018-05-24 16:12:31 -0400 | [diff] [blame] | 64 | static void InitDistanceFieldPaint(GrTextBlob* blob, |
Khushal | 3e7548c | 2018-05-23 15:45:01 -0700 | [diff] [blame] | 65 | SkPaint* skPaint, |
| 66 | const SkMatrix& viewMatrix, |
| 67 | const Options& options, |
| 68 | SkScalar* textRatio, |
| 69 | SkScalerContextFlags* flags); |
| 70 | |
Herb Derby | f4f6bbf | 2018-07-27 11:58:37 -0400 | [diff] [blame] | 71 | class FallbackGlyphRunHelper { |
| 72 | public: |
| 73 | FallbackGlyphRunHelper(const SkMatrix& viewMatrix, |
| 74 | const SkPaint& pathPaint, |
| 75 | SkScalar maxTextSize, |
| 76 | SkScalar textRatio) |
| 77 | : fViewMatrix(viewMatrix) |
| 78 | , fTextSize(pathPaint.getTextSize()) |
| 79 | , fMaxTextSize(maxTextSize) |
| 80 | , fTextRatio(textRatio) |
| 81 | , fTransformedFallbackTextSize(fMaxTextSize) |
| 82 | , fUseTransformedFallback(false) { |
| 83 | fMaxScale = viewMatrix.getMaxScale(); |
| 84 | } |
| 85 | |
Herb Derby | 82c11e0 | 2018-08-06 16:40:12 -0400 | [diff] [blame] | 86 | void appendGlyph(const SkGlyph& glyph, SkGlyphID glyphID, SkPoint glyphPos); |
| 87 | void drawGlyphs( |
| 88 | GrTextBlob* blob, int runIndex, GrGlyphCache* cache, |
Herb Derby | 919a784 | 2018-08-07 17:02:00 -0400 | [diff] [blame] | 89 | const SkSurfaceProps& props, const SkPaint& paint, GrColor filteredColor, |
Herb Derby | 82c11e0 | 2018-08-06 16:40:12 -0400 | [diff] [blame] | 90 | SkScalerContextFlags scalerContextFlags); |
Herb Derby | f4f6bbf | 2018-07-27 11:58:37 -0400 | [diff] [blame] | 91 | |
| 92 | void initializeForDraw(SkPaint* paint, SkScalar* textRatio, SkMatrix* matrix) const; |
| 93 | const std::vector<SkGlyphID>& fallbackText() const { return fFallbackTxt; } |
| 94 | |
| 95 | private: |
| 96 | std::vector<SkGlyphID> fFallbackTxt; |
| 97 | std::vector<SkPoint> fFallbackPos; |
| 98 | |
| 99 | const SkMatrix& fViewMatrix; |
| 100 | SkScalar fTextSize; |
| 101 | SkScalar fMaxTextSize; |
| 102 | SkScalar fTextRatio; |
| 103 | SkScalar fTransformedFallbackTextSize; |
| 104 | SkScalar fMaxScale; |
| 105 | bool fUseTransformedFallback; |
| 106 | }; |
| 107 | |
Khushal | fa8ff09 | 2018-06-06 17:46:38 -0700 | [diff] [blame] | 108 | private: |
| 109 | GrTextContext(const Options& options); |
| 110 | |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 111 | // 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] | 112 | static SkColor ComputeCanonicalColor(const SkPaint&, bool lcd); |
brianosman | a1e8f8d | 2016-04-08 06:47:54 -0700 | [diff] [blame] | 113 | // Determines if we need to use fake gamma (and contrast boost): |
Herb Derby | d8327a8 | 2018-01-22 14:39:27 -0500 | [diff] [blame] | 114 | static SkScalerContextFlags ComputeScalerContextFlags(const GrColorSpaceInfo&); |
Herb Derby | cddab25 | 2018-07-16 11:19:04 -0400 | [diff] [blame] | 115 | |
| 116 | void regenerateGlyphRunList(GrTextBlob* bmp, |
Robert Phillips | c4039ea | 2018-03-01 11:36:45 -0500 | [diff] [blame] | 117 | GrGlyphCache*, |
Brian Salomon | af59748 | 2017-11-07 16:23:34 -0500 | [diff] [blame] | 118 | const GrShaderCaps&, |
Herb Derby | bc6f9c9 | 2018-08-08 13:58:45 -0400 | [diff] [blame] | 119 | const SkPaint&, |
Herb Derby | 919a784 | 2018-08-07 17:02:00 -0400 | [diff] [blame] | 120 | GrColor filteredColor, |
Herb Derby | d8327a8 | 2018-01-22 14:39:27 -0500 | [diff] [blame] | 121 | SkScalerContextFlags scalerContextFlags, |
Brian Salomon | af59748 | 2017-11-07 16:23:34 -0500 | [diff] [blame] | 122 | const SkMatrix& viewMatrix, |
| 123 | const SkSurfaceProps&, |
Herb Derby | 74c6ed3 | 2018-07-28 18:07:54 -0400 | [diff] [blame] | 124 | const SkGlyphRunList& glyphRunList, |
| 125 | SkGlyphRunListDrawer* glyphDrawer); |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 126 | |
Herb Derby | 049b5d9 | 2018-08-01 13:56:26 -0400 | [diff] [blame] | 127 | static void AppendGlyph(GrTextBlob*, int runIndex, GrGlyphCache*, |
| 128 | sk_sp<GrTextStrike>*, const SkGlyph&, GrGlyph::MaskStyle maskStyle, |
| 129 | SkScalar sx, SkScalar sy, |
| 130 | GrColor color, SkGlyphCache*, SkScalar textRatio, |
| 131 | bool needsTransform); |
Herb Derby | f4f6bbf | 2018-07-27 11:58:37 -0400 | [diff] [blame] | 132 | |
Brian Salomon | 52db940 | 2017-11-07 14:58:55 -0500 | [diff] [blame] | 133 | |
Hal Canary | 144caf5 | 2016-11-07 17:57:18 -0500 | [diff] [blame] | 134 | const GrDistanceFieldAdjustTable* dfAdjustTable() const { return fDistanceAdjustTable.get(); } |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 135 | |
Hal Canary | 144caf5 | 2016-11-07 17:57:18 -0500 | [diff] [blame] | 136 | sk_sp<const GrDistanceFieldAdjustTable> fDistanceAdjustTable; |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 137 | |
Khushal | 3e7548c | 2018-05-23 15:45:01 -0700 | [diff] [blame] | 138 | Options fOptions; |
Brian Salomon | af59748 | 2017-11-07 16:23:34 -0500 | [diff] [blame] | 139 | |
Hal Canary | 6f6961e | 2017-01-31 13:50:44 -0500 | [diff] [blame] | 140 | #if GR_TEST_UTILS |
Herb Derby | d8327a8 | 2018-01-22 14:39:27 -0500 | [diff] [blame] | 141 | static const SkScalerContextFlags kTextBlobOpScalerContextFlags = |
| 142 | SkScalerContextFlags::kFakeGammaAndBoostContrast; |
Brian Salomon | 44acb5b | 2017-07-18 19:59:24 -0400 | [diff] [blame] | 143 | GR_DRAW_OP_TEST_FRIEND(GrAtlasTextOp); |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 144 | #endif |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 145 | }; |
| 146 | |
Herb Derby | 8624059 | 2018-05-24 16:12:31 -0400 | [diff] [blame] | 147 | #endif // GrTextContext_DEFINED |