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: |
joshualitt | 27004b7 | 2016-02-11 12:00:33 -0800 | [diff] [blame] | 30 | static GrAtlasTextContext* Create(); |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 31 | |
joshualitt | 27004b7 | 2016-02-11 12:00:33 -0800 | [diff] [blame] | 32 | bool canDraw(const SkPaint&, const SkMatrix& viewMatrix, const SkSurfaceProps&, |
| 33 | const GrShaderCaps&); |
Brian Salomon | 6f1d36c | 2017-01-13 12:02:17 -0500 | [diff] [blame] | 34 | |
Brian Salomon | f18b1d8 | 2017-10-27 11:30:49 -0400 | [diff] [blame] | 35 | void drawText(GrContext*, GrTextUtils::Target*, const GrClip&, const SkPaint&, |
joshualitt | 2c89bc1 | 2016-02-11 05:42:30 -0800 | [diff] [blame] | 36 | const SkMatrix& viewMatrix, const SkSurfaceProps&, const char text[], |
Brian Salomon | 82f4431 | 2017-01-11 13:42:54 -0500 | [diff] [blame] | 37 | size_t byteLength, SkScalar x, SkScalar y, const SkIRect& regionClipBounds); |
Brian Salomon | f18b1d8 | 2017-10-27 11:30:49 -0400 | [diff] [blame] | 38 | void drawPosText(GrContext*, GrTextUtils::Target*, const GrClip&, const SkPaint&, |
Brian Salomon | 82f4431 | 2017-01-11 13:42:54 -0500 | [diff] [blame] | 39 | const SkMatrix& viewMatrix, const SkSurfaceProps&, const char text[], |
| 40 | size_t byteLength, const SkScalar pos[], int scalarsPerPosition, |
joshualitt | 8e84a1e | 2016-02-16 11:09:25 -0800 | [diff] [blame] | 41 | const SkPoint& offset, const SkIRect& regionClipBounds); |
Brian Salomon | f18b1d8 | 2017-10-27 11:30:49 -0400 | [diff] [blame] | 42 | void drawTextBlob(GrContext*, GrTextUtils::Target*, const GrClip&, const SkPaint&, |
joshualitt | 2c89bc1 | 2016-02-11 05:42:30 -0800 | [diff] [blame] | 43 | const SkMatrix& viewMatrix, const SkSurfaceProps&, const SkTextBlob*, |
Brian Salomon | f18b1d8 | 2017-10-27 11:30:49 -0400 | [diff] [blame] | 44 | SkScalar x, SkScalar y, SkDrawFilter*, const SkIRect& clipBounds); |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 45 | |
joshualitt | e55750e | 2016-02-10 12:52:21 -0800 | [diff] [blame] | 46 | private: |
joshualitt | 27004b7 | 2016-02-11 12:00:33 -0800 | [diff] [blame] | 47 | GrAtlasTextContext(); |
joshualitt | e55750e | 2016-02-10 12:52:21 -0800 | [diff] [blame] | 48 | |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 49 | // 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] | 50 | inline static SkColor ComputeCanonicalColor(const SkPaint&, bool lcd); |
brianosman | a1e8f8d | 2016-04-08 06:47:54 -0700 | [diff] [blame] | 51 | // Determines if we need to use fake gamma (and contrast boost): |
Brian Salomon | f3569f0 | 2017-10-24 12:52:33 -0400 | [diff] [blame] | 52 | inline static uint32_t ComputeScalerContextFlags(const GrColorSpaceInfo&); |
joshualitt | 27004b7 | 2016-02-11 12:00:33 -0800 | [diff] [blame] | 53 | static void RegenerateTextBlob(GrAtlasTextBlob* bmp, |
Brian Salomon | f856fd1 | 2016-12-16 14:24:34 -0500 | [diff] [blame] | 54 | GrAtlasGlyphCache*, |
joshualitt | 27004b7 | 2016-02-11 12:00:33 -0800 | [diff] [blame] | 55 | const GrShaderCaps&, |
Brian Salomon | 6f1d36c | 2017-01-13 12:02:17 -0500 | [diff] [blame] | 56 | const GrTextUtils::Paint&, |
brianosman | a1e8f8d | 2016-04-08 06:47:54 -0700 | [diff] [blame] | 57 | uint32_t scalerContextFlags, |
joshualitt | 27004b7 | 2016-02-11 12:00:33 -0800 | [diff] [blame] | 58 | const SkMatrix& viewMatrix, |
| 59 | const SkSurfaceProps&, |
| 60 | const SkTextBlob* blob, SkScalar x, SkScalar y, |
| 61 | SkDrawFilter* drawFilter); |
joshualitt | 9e36c1a | 2015-04-14 12:17:27 -0700 | [diff] [blame] | 62 | inline static bool HasLCD(const SkTextBlob*); |
joshualitt | 9bd2daf | 2015-04-17 09:30:06 -0700 | [diff] [blame] | 63 | |
Florin Malita | c337c9e | 2017-03-10 18:02:29 +0000 | [diff] [blame] | 64 | static inline sk_sp<GrAtlasTextBlob> MakeDrawTextBlob(GrTextBlobCache*, GrAtlasGlyphCache*, |
| 65 | const GrShaderCaps&, |
| 66 | const GrTextUtils::Paint&, |
| 67 | uint32_t scalerContextFlags, |
| 68 | const SkMatrix& viewMatrix, |
| 69 | const SkSurfaceProps&, |
| 70 | const char text[], size_t byteLength, |
| 71 | SkScalar x, SkScalar y); |
| 72 | static inline sk_sp<GrAtlasTextBlob> MakeDrawPosTextBlob(GrTextBlobCache*, GrAtlasGlyphCache*, |
| 73 | const GrShaderCaps&, |
| 74 | const GrTextUtils::Paint&, |
| 75 | uint32_t scalerContextFlags, |
| 76 | const SkMatrix& viewMatrix, |
| 77 | const SkSurfaceProps&, |
| 78 | const char text[], size_t byteLength, |
| 79 | const SkScalar pos[], |
| 80 | int scalarsPerPosition, |
| 81 | const SkPoint& offset); |
Brian Salomon | 52db940 | 2017-11-07 14:58:55 -0500 | [diff] [blame^] | 82 | |
| 83 | // Functions for appending BMP text to GrAtlasTextBlob |
| 84 | static void DrawBmpText(GrAtlasTextBlob*, int runIndex, GrAtlasGlyphCache*, |
| 85 | const SkSurfaceProps&, const GrTextUtils::Paint& paint, |
| 86 | uint32_t scalerContextFlags, const SkMatrix& viewMatrix, |
| 87 | const char text[], size_t byteLength, SkScalar x, SkScalar y); |
| 88 | |
| 89 | static void DrawBmpPosText(GrAtlasTextBlob*, int runIndex, GrAtlasGlyphCache*, |
| 90 | const SkSurfaceProps&, const GrTextUtils::Paint& paint, |
| 91 | uint32_t scalerContextFlags, const SkMatrix& viewMatrix, |
| 92 | const char text[], size_t byteLength, const SkScalar pos[], |
| 93 | int scalarsPerPosition, const SkPoint& offset); |
| 94 | |
| 95 | // functions for appending distance field text |
| 96 | static bool CanDrawAsDistanceFields(const SkPaint& skPaint, const SkMatrix& viewMatrix, |
| 97 | const SkSurfaceProps& props, const GrShaderCaps& caps); |
| 98 | |
| 99 | static void DrawDFText(GrAtlasTextBlob* blob, int runIndex, GrAtlasGlyphCache*, |
| 100 | const SkSurfaceProps&, const GrTextUtils::Paint& paint, |
| 101 | uint32_t scalerContextFlags, const SkMatrix& viewMatrix, |
| 102 | const char text[], size_t byteLength, SkScalar x, SkScalar y); |
| 103 | |
| 104 | static void DrawDFPosText(GrAtlasTextBlob* blob, int runIndex, GrAtlasGlyphCache*, |
| 105 | const SkSurfaceProps&, const GrTextUtils::Paint& paint, |
| 106 | uint32_t scalerContextFlags, const SkMatrix& viewMatrix, |
| 107 | const char text[], size_t byteLength, const SkScalar pos[], |
| 108 | int scalarsPerPosition, const SkPoint& offset); |
| 109 | |
| 110 | static void InitDistanceFieldPaint(GrAtlasTextBlob* blob, |
| 111 | SkPaint* skPaint, |
| 112 | SkScalar* textRatio, |
| 113 | const SkMatrix& viewMatrix); |
| 114 | |
| 115 | static void BmpAppendGlyph(GrAtlasTextBlob*, int runIndex, GrAtlasGlyphCache*, |
| 116 | GrAtlasTextStrike**, const SkGlyph&, int left, int top, |
| 117 | GrColor color, SkGlyphCache*); |
| 118 | |
| 119 | static bool DfAppendGlyph(GrAtlasTextBlob*, int runIndex, GrAtlasGlyphCache*, |
| 120 | GrAtlasTextStrike**, const SkGlyph&, SkScalar sx, SkScalar sy, |
| 121 | GrColor color, SkGlyphCache* cache, SkScalar textRatio, |
| 122 | const SkMatrix& viewMatrix); |
| 123 | |
Hal Canary | 144caf5 | 2016-11-07 17:57:18 -0500 | [diff] [blame] | 124 | const GrDistanceFieldAdjustTable* dfAdjustTable() const { return fDistanceAdjustTable.get(); } |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 125 | |
Hal Canary | 144caf5 | 2016-11-07 17:57:18 -0500 | [diff] [blame] | 126 | sk_sp<const GrDistanceFieldAdjustTable> fDistanceAdjustTable; |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 127 | |
Hal Canary | 6f6961e | 2017-01-31 13:50:44 -0500 | [diff] [blame] | 128 | #if GR_TEST_UTILS |
Brian Salomon | 5ec9def | 2016-12-20 15:34:05 -0500 | [diff] [blame] | 129 | static const uint32_t kTextBlobOpScalerContextFlags = |
| 130 | SkPaint::kFakeGammaAndBoostContrast_ScalerContextFlags; |
Brian Salomon | 44acb5b | 2017-07-18 19:59:24 -0400 | [diff] [blame] | 131 | GR_DRAW_OP_TEST_FRIEND(GrAtlasTextOp); |
joshualitt | 79dfb2b | 2015-05-11 08:58:08 -0700 | [diff] [blame] | 132 | #endif |
joshualitt | 1d89e8d | 2015-04-01 12:40:54 -0700 | [diff] [blame] | 133 | }; |
| 134 | |
| 135 | #endif |