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 | |
| 8 | #ifndef GrAtlasTextContext_DEFINED |
| 9 | #define GrAtlasTextContext_DEFINED |
| 10 | |
joshualitt | 374b2f7 | 2015-07-21 08:05:03 -0700 | [diff] [blame] | 11 | #include "GrAtlasTextBlob.h" |
joshualitt | 1acabf3 | 2015-12-10 09:10:10 -0800 | [diff] [blame] | 12 | #include "GrDistanceFieldAdjustTable.h" |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 13 | #include "GrGeometryProcessor.h" |
Brian Salomon | 6f1d36c | 2017-01-13 12:02:17 -0500 | [diff] [blame] | 14 | #include "GrTextUtils.h" |
halcanary | 3377975 | 2015-10-27 14:01:05 -0700 | [diff] [blame] | 15 | #include "SkTextBlobRunIterator.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 | */ |
joshualitt | 8e84a1e | 2016-02-16 11:09:25 -0800 | [diff] [blame] | 28 | class GrAtlasTextContext { |
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 | |
| 45 | static std::unique_ptr<GrAtlasTextContext> Make(const Options& options); |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 46 | |
joshualitt | 27004b7 | 2016-02-11 12:00:33 -0800 | [diff] [blame] | 47 | bool canDraw(const SkPaint&, const SkMatrix& viewMatrix, const SkSurfaceProps&, |
| 48 | const GrShaderCaps&); |
Brian Salomon | 6f1d36c | 2017-01-13 12:02:17 -0500 | [diff] [blame] | 49 | |
Brian Salomon | f18b1d8 | 2017-10-27 11:30:49 -0400 | [diff] [blame] | 50 | void drawText(GrContext*, GrTextUtils::Target*, const GrClip&, const SkPaint&, |
joshualitt | 2c89bc1 | 2016-02-11 05:42:30 -0800 | [diff] [blame] | 51 | const SkMatrix& viewMatrix, const SkSurfaceProps&, const char text[], |
Brian Salomon | 82f4431 | 2017-01-11 13:42:54 -0500 | [diff] [blame] | 52 | size_t byteLength, SkScalar x, SkScalar y, const SkIRect& regionClipBounds); |
Brian Salomon | f18b1d8 | 2017-10-27 11:30:49 -0400 | [diff] [blame] | 53 | void drawPosText(GrContext*, GrTextUtils::Target*, const GrClip&, const SkPaint&, |
Brian Salomon | 82f4431 | 2017-01-11 13:42:54 -0500 | [diff] [blame] | 54 | const SkMatrix& viewMatrix, const SkSurfaceProps&, const char text[], |
| 55 | size_t byteLength, const SkScalar pos[], int scalarsPerPosition, |
joshualitt | 8e84a1e | 2016-02-16 11:09:25 -0800 | [diff] [blame] | 56 | const SkPoint& offset, const SkIRect& regionClipBounds); |
Brian Salomon | f18b1d8 | 2017-10-27 11:30:49 -0400 | [diff] [blame] | 57 | void drawTextBlob(GrContext*, GrTextUtils::Target*, const GrClip&, const SkPaint&, |
joshualitt | 2c89bc1 | 2016-02-11 05:42:30 -0800 | [diff] [blame] | 58 | const SkMatrix& viewMatrix, const SkSurfaceProps&, const SkTextBlob*, |
Brian Salomon | f18b1d8 | 2017-10-27 11:30:49 -0400 | [diff] [blame] | 59 | SkScalar x, SkScalar y, SkDrawFilter*, const SkIRect& clipBounds); |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 60 | |
joshualitt | e55750e | 2016-02-10 12:52:21 -0800 | [diff] [blame] | 61 | private: |
Brian Salomon | af59748 | 2017-11-07 16:23:34 -0500 | [diff] [blame] | 62 | GrAtlasTextContext(const Options& options); |
joshualitt | e55750e | 2016-02-10 12:52:21 -0800 | [diff] [blame] | 63 | |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 64 | // sets up the descriptor on the blob and returns a detached cache. Client must attach |
Brian Salomon | 6f1d36c | 2017-01-13 12:02:17 -0500 | [diff] [blame] | 65 | inline static SkColor ComputeCanonicalColor(const SkPaint&, bool lcd); |
brianosman | a1e8f8d | 2016-04-08 06:47:54 -0700 | [diff] [blame] | 66 | // Determines if we need to use fake gamma (and contrast boost): |
Brian Salomon | f3569f0 | 2017-10-24 12:52:33 -0400 | [diff] [blame] | 67 | inline static uint32_t ComputeScalerContextFlags(const GrColorSpaceInfo&); |
Brian Salomon | af59748 | 2017-11-07 16:23:34 -0500 | [diff] [blame] | 68 | void regenerateTextBlob(GrAtlasTextBlob* bmp, |
| 69 | GrAtlasGlyphCache*, |
| 70 | const GrShaderCaps&, |
| 71 | const GrTextUtils::Paint&, |
| 72 | uint32_t scalerContextFlags, |
| 73 | const SkMatrix& viewMatrix, |
| 74 | const SkSurfaceProps&, |
| 75 | const SkTextBlob* blob, SkScalar x, SkScalar y, |
| 76 | SkDrawFilter* drawFilter) const; |
| 77 | |
joshualitt | 9e36c1a | 2015-04-14 12:17:27 -0700 | [diff] [blame] | 78 | inline static bool HasLCD(const SkTextBlob*); |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 79 | |
Brian Salomon | af59748 | 2017-11-07 16:23:34 -0500 | [diff] [blame] | 80 | inline sk_sp<GrAtlasTextBlob> makeDrawTextBlob(GrTextBlobCache*, GrAtlasGlyphCache*, |
| 81 | const GrShaderCaps&, |
| 82 | const GrTextUtils::Paint&, |
| 83 | uint32_t scalerContextFlags, |
| 84 | const SkMatrix& viewMatrix, |
| 85 | const SkSurfaceProps&, |
| 86 | const char text[], size_t byteLength, |
| 87 | SkScalar x, SkScalar y) const; |
| 88 | |
| 89 | inline sk_sp<GrAtlasTextBlob> makeDrawPosTextBlob(GrTextBlobCache*, GrAtlasGlyphCache*, |
| 90 | const GrShaderCaps&, |
| 91 | const GrTextUtils::Paint&, |
| 92 | uint32_t scalerContextFlags, |
| 93 | const SkMatrix& viewMatrix, |
| 94 | const SkSurfaceProps&, |
| 95 | const char text[], size_t byteLength, |
| 96 | const SkScalar pos[], |
| 97 | int scalarsPerPosition, |
| 98 | const SkPoint& offset) const; |
Brian Salomon | 52db940 | 2017-11-07 14:58:55 -0500 | [diff] [blame] | 99 | |
| 100 | // Functions for appending BMP text to GrAtlasTextBlob |
| 101 | static void DrawBmpText(GrAtlasTextBlob*, int runIndex, GrAtlasGlyphCache*, |
| 102 | const SkSurfaceProps&, const GrTextUtils::Paint& paint, |
| 103 | uint32_t scalerContextFlags, const SkMatrix& viewMatrix, |
| 104 | const char text[], size_t byteLength, SkScalar x, SkScalar y); |
| 105 | |
| 106 | static void DrawBmpPosText(GrAtlasTextBlob*, int runIndex, GrAtlasGlyphCache*, |
| 107 | const SkSurfaceProps&, const GrTextUtils::Paint& paint, |
| 108 | uint32_t scalerContextFlags, const SkMatrix& viewMatrix, |
| 109 | const char text[], size_t byteLength, const SkScalar pos[], |
Jim Van Verth | f4c1316 | 2018-01-11 16:40:24 -0500 | [diff] [blame^] | 110 | int scalarsPerPosition, const SkPoint& offset, |
| 111 | SkScalar textRatio); |
Brian Salomon | 52db940 | 2017-11-07 14:58:55 -0500 | [diff] [blame] | 112 | |
| 113 | // functions for appending distance field text |
Brian Salomon | af59748 | 2017-11-07 16:23:34 -0500 | [diff] [blame] | 114 | bool canDrawAsDistanceFields(const SkPaint& skPaint, const SkMatrix& viewMatrix, |
| 115 | const SkSurfaceProps& props, const GrShaderCaps& caps) const; |
Brian Salomon | 52db940 | 2017-11-07 14:58:55 -0500 | [diff] [blame] | 116 | |
Brian Salomon | af59748 | 2017-11-07 16:23:34 -0500 | [diff] [blame] | 117 | void drawDFText(GrAtlasTextBlob* blob, int runIndex, GrAtlasGlyphCache*, const SkSurfaceProps&, |
| 118 | const GrTextUtils::Paint& paint, uint32_t scalerContextFlags, |
| 119 | const SkMatrix& viewMatrix, const char text[], size_t byteLength, SkScalar x, |
| 120 | SkScalar y) const; |
Brian Salomon | 52db940 | 2017-11-07 14:58:55 -0500 | [diff] [blame] | 121 | |
Brian Salomon | af59748 | 2017-11-07 16:23:34 -0500 | [diff] [blame] | 122 | void drawDFPosText(GrAtlasTextBlob* blob, int runIndex, GrAtlasGlyphCache*, |
| 123 | const SkSurfaceProps&, const GrTextUtils::Paint& paint, |
| 124 | uint32_t scalerContextFlags, const SkMatrix& viewMatrix, const char text[], |
| 125 | size_t byteLength, const SkScalar pos[], int scalarsPerPosition, |
| 126 | const SkPoint& offset) const; |
Brian Salomon | 52db940 | 2017-11-07 14:58:55 -0500 | [diff] [blame] | 127 | |
Brian Salomon | af59748 | 2017-11-07 16:23:34 -0500 | [diff] [blame] | 128 | void initDistanceFieldPaint(GrAtlasTextBlob* blob, |
| 129 | SkPaint* skPaint, |
| 130 | SkScalar* textRatio, |
| 131 | const SkMatrix& viewMatrix) const; |
Brian Salomon | 52db940 | 2017-11-07 14:58:55 -0500 | [diff] [blame] | 132 | |
| 133 | static void BmpAppendGlyph(GrAtlasTextBlob*, int runIndex, GrAtlasGlyphCache*, |
| 134 | GrAtlasTextStrike**, const SkGlyph&, int left, int top, |
Jim Van Verth | f4c1316 | 2018-01-11 16:40:24 -0500 | [diff] [blame^] | 135 | GrColor color, SkGlyphCache*, SkScalar textRatio); |
Brian Salomon | 52db940 | 2017-11-07 14:58:55 -0500 | [diff] [blame] | 136 | |
Jim Van Verth | f4c1316 | 2018-01-11 16:40:24 -0500 | [diff] [blame^] | 137 | static void DfAppendGlyph(GrAtlasTextBlob*, int runIndex, GrAtlasGlyphCache*, |
Brian Salomon | 52db940 | 2017-11-07 14:58:55 -0500 | [diff] [blame] | 138 | GrAtlasTextStrike**, const SkGlyph&, SkScalar sx, SkScalar sy, |
Jim Van Verth | f4c1316 | 2018-01-11 16:40:24 -0500 | [diff] [blame^] | 139 | GrColor color, SkGlyphCache* cache, SkScalar textRatio); |
Brian Salomon | 52db940 | 2017-11-07 14:58:55 -0500 | [diff] [blame] | 140 | |
Hal Canary | 144caf5 | 2016-11-07 17:57:18 -0500 | [diff] [blame] | 141 | const GrDistanceFieldAdjustTable* dfAdjustTable() const { return fDistanceAdjustTable.get(); } |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 142 | |
Hal Canary | 144caf5 | 2016-11-07 17:57:18 -0500 | [diff] [blame] | 143 | sk_sp<const GrDistanceFieldAdjustTable> fDistanceAdjustTable; |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 144 | |
Brian Salomon | af59748 | 2017-11-07 16:23:34 -0500 | [diff] [blame] | 145 | SkScalar fMinDistanceFieldFontSize; |
| 146 | SkScalar fMaxDistanceFieldFontSize; |
Brian Salomon | b508696 | 2017-12-13 10:59:33 -0500 | [diff] [blame] | 147 | bool fDistanceFieldVerticesAlwaysHaveW; |
Brian Salomon | af59748 | 2017-11-07 16:23:34 -0500 | [diff] [blame] | 148 | |
Hal Canary | 6f6961e | 2017-01-31 13:50:44 -0500 | [diff] [blame] | 149 | #if GR_TEST_UTILS |
Brian Salomon | 5ec9def | 2016-12-20 15:34:05 -0500 | [diff] [blame] | 150 | static const uint32_t kTextBlobOpScalerContextFlags = |
| 151 | SkPaint::kFakeGammaAndBoostContrast_ScalerContextFlags; |
Brian Salomon | 44acb5b | 2017-07-18 19:59:24 -0400 | [diff] [blame] | 152 | GR_DRAW_OP_TEST_FRIEND(GrAtlasTextOp); |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 153 | #endif |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 154 | }; |
| 155 | |
| 156 | #endif |