joshualitt | 0a42e68 | 2015-12-10 13:20:58 -0800 | [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 GrTextUtils_DEFINED |
| 9 | #define GrTextUtils_DEFINED |
| 10 | |
joshualitt | 2967798 | 2015-12-11 06:08:59 -0800 | [diff] [blame] | 11 | #include "GrColor.h" |
Brian Salomon | f3569f0 | 2017-10-24 12:52:33 -0400 | [diff] [blame] | 12 | #include "GrColorSpaceInfo.h" |
Brian Salomon | 6f1d36c | 2017-01-13 12:02:17 -0500 | [diff] [blame] | 13 | #include "SkColorFilter.h" |
brianosman | 32f7782 | 2016-04-07 06:25:45 -0700 | [diff] [blame] | 14 | #include "SkPaint.h" |
joshualitt | 0a42e68 | 2015-12-10 13:20:58 -0800 | [diff] [blame] | 15 | #include "SkScalar.h" |
Brian Salomon | 6f1d36c | 2017-01-13 12:02:17 -0500 | [diff] [blame] | 16 | #include "SkTLazy.h" |
joshualitt | 0a42e68 | 2015-12-10 13:20:58 -0800 | [diff] [blame] | 17 | |
Brian Salomon | f856fd1 | 2016-12-16 14:24:34 -0500 | [diff] [blame] | 18 | class GrAtlasGlyphCache; |
joshualitt | 2967798 | 2015-12-11 06:08:59 -0800 | [diff] [blame] | 19 | class GrAtlasTextBlob; |
Brian Salomon | f18b1d8 | 2017-10-27 11:30:49 -0400 | [diff] [blame] | 20 | class GrAtlasTextOp; |
Brian Salomon | f856fd1 | 2016-12-16 14:24:34 -0500 | [diff] [blame] | 21 | class GrAtlasTextStrike; |
joshualitt | 0a42e68 | 2015-12-10 13:20:58 -0800 | [diff] [blame] | 22 | class GrClip; |
Brian Osman | ec8f8b0 | 2017-05-11 10:57:37 -0400 | [diff] [blame] | 23 | class GrColorSpaceXform; |
joshualitt | 0a42e68 | 2015-12-10 13:20:58 -0800 | [diff] [blame] | 24 | class GrContext; |
Brian Salomon | 6f1d36c | 2017-01-13 12:02:17 -0500 | [diff] [blame] | 25 | class GrPaint; |
joshualitt | 0d2199b | 2016-01-20 06:36:09 -0800 | [diff] [blame] | 26 | class GrShaderCaps; |
Brian Osman | ec8f8b0 | 2017-05-11 10:57:37 -0400 | [diff] [blame] | 27 | class SkColorSpace; |
Brian Salomon | 6f1d36c | 2017-01-13 12:02:17 -0500 | [diff] [blame] | 28 | class SkDrawFilter; |
joshualitt | 2967798 | 2015-12-11 06:08:59 -0800 | [diff] [blame] | 29 | class SkGlyph; |
joshualitt | 0a42e68 | 2015-12-10 13:20:58 -0800 | [diff] [blame] | 30 | class SkMatrix; |
| 31 | struct SkIRect; |
joshualitt | 0a42e68 | 2015-12-10 13:20:58 -0800 | [diff] [blame] | 32 | struct SkPoint; |
joshualitt | 2967798 | 2015-12-11 06:08:59 -0800 | [diff] [blame] | 33 | class SkGlyphCache; |
Brian Salomon | 6f1d36c | 2017-01-13 12:02:17 -0500 | [diff] [blame] | 34 | class SkTextBlobRunIterator; |
joshualitt | 0a42e68 | 2015-12-10 13:20:58 -0800 | [diff] [blame] | 35 | class SkSurfaceProps; |
| 36 | |
Brian Salomon | 6f1d36c | 2017-01-13 12:02:17 -0500 | [diff] [blame] | 37 | /** |
joshualitt | 0a42e68 | 2015-12-10 13:20:58 -0800 | [diff] [blame] | 38 | * A class to house a bunch of common text utilities. This class should *ONLY* have static |
| 39 | * functions. It is not a namespace only because we wish to friend SkPaint |
joshualitt | 0a42e68 | 2015-12-10 13:20:58 -0800 | [diff] [blame] | 40 | */ |
| 41 | class GrTextUtils { |
| 42 | public: |
Brian Salomon | f18b1d8 | 2017-10-27 11:30:49 -0400 | [diff] [blame] | 43 | class Target { |
| 44 | public: |
| 45 | virtual ~Target() = default; |
| 46 | |
| 47 | int width() const { return fWidth; } |
| 48 | int height() const { return fHeight; } |
| 49 | const GrColorSpaceInfo& colorSpaceInfo() const { return fColorSpaceInfo; } |
| 50 | |
| 51 | virtual void addDrawOp(const GrClip&, std::unique_ptr<GrAtlasTextOp> op) = 0; |
| 52 | |
| 53 | virtual void drawPath(const GrClip&, const SkPath&, const SkPaint&, |
| 54 | const SkMatrix& viewMatrix, const SkMatrix* pathMatrix, |
| 55 | const SkIRect& clipBounds) = 0; |
| 56 | virtual void makeGrPaint(GrMaskFormat, const SkPaint&, const SkMatrix& viewMatrix, |
| 57 | GrPaint*) = 0; |
| 58 | |
| 59 | protected: |
| 60 | Target(int width, int height, const GrColorSpaceInfo& colorSpaceInfo) |
| 61 | : fWidth(width), fHeight(height), fColorSpaceInfo(colorSpaceInfo) {} |
| 62 | |
| 63 | private: |
| 64 | int fWidth; |
| 65 | int fHeight; |
| 66 | const GrColorSpaceInfo& fColorSpaceInfo; |
| 67 | }; |
| 68 | |
Brian Salomon | 6f1d36c | 2017-01-13 12:02:17 -0500 | [diff] [blame] | 69 | /** |
| 70 | * This is used to wrap a SkPaint and its post-color filter color. It is also used by RunPaint |
| 71 | * (below). This keeps a pointer to the SkPaint it is initialized with and expects it to remain |
| 72 | * const. It is also used to transform to GrPaint. |
| 73 | */ |
| 74 | class Paint { |
| 75 | public: |
Brian Salomon | 4cbb6e6 | 2017-10-25 15:12:19 -0400 | [diff] [blame] | 76 | explicit Paint(const SkPaint* paint, const GrColorSpaceInfo* dstColorSpaceInfo) |
| 77 | : fPaint(paint), fDstColorSpaceInfo(dstColorSpaceInfo) { |
Brian Osman | ec8f8b0 | 2017-05-11 10:57:37 -0400 | [diff] [blame] | 78 | this->initFilteredColor(); |
| 79 | } |
Brian Salomon | 6f1d36c | 2017-01-13 12:02:17 -0500 | [diff] [blame] | 80 | |
| 81 | // These expose the paint's color run through its color filter (if any). This is only valid |
| 82 | // when drawing grayscale/lcd glyph masks and not when drawing color glyphs. |
Brian Osman | ec8f8b0 | 2017-05-11 10:57:37 -0400 | [diff] [blame] | 83 | GrColor filteredPremulColor() const { return fFilteredPremulColor; } |
Jim Van Verth | bc2cdd1 | 2017-06-08 11:14:35 -0400 | [diff] [blame] | 84 | SkColor luminanceColor() const { return fPaint->computeLuminanceColor(); } |
Brian Salomon | 6f1d36c | 2017-01-13 12:02:17 -0500 | [diff] [blame] | 85 | |
| 86 | const SkPaint& skPaint() const { return *fPaint; } |
| 87 | operator const SkPaint&() const { return this->skPaint(); } |
| 88 | |
Brian Osman | ec8f8b0 | 2017-05-11 10:57:37 -0400 | [diff] [blame] | 89 | // Just for RunPaint's constructor |
Brian Salomon | 4cbb6e6 | 2017-10-25 15:12:19 -0400 | [diff] [blame] | 90 | const GrColorSpaceInfo* dstColorSpaceInfo() const { return fDstColorSpaceInfo; } |
Brian Osman | ec8f8b0 | 2017-05-11 10:57:37 -0400 | [diff] [blame] | 91 | |
Brian Salomon | 6f1d36c | 2017-01-13 12:02:17 -0500 | [diff] [blame] | 92 | protected: |
Brian Osman | ec8f8b0 | 2017-05-11 10:57:37 -0400 | [diff] [blame] | 93 | void initFilteredColor(); |
Brian Salomon | 6f1d36c | 2017-01-13 12:02:17 -0500 | [diff] [blame] | 94 | Paint() = default; |
| 95 | const SkPaint* fPaint; |
Brian Salomon | 4cbb6e6 | 2017-10-25 15:12:19 -0400 | [diff] [blame] | 96 | const GrColorSpaceInfo* fDstColorSpaceInfo; |
Brian Salomon | 6f1d36c | 2017-01-13 12:02:17 -0500 | [diff] [blame] | 97 | // This is the paint's color run through its color filter, if present. This color should |
| 98 | // be used except when rendering bitmap text, in which case the bitmap must be filtered in |
| 99 | // the fragment shader. |
Brian Osman | ec8f8b0 | 2017-05-11 10:57:37 -0400 | [diff] [blame] | 100 | GrColor fFilteredPremulColor; |
Brian Salomon | 6f1d36c | 2017-01-13 12:02:17 -0500 | [diff] [blame] | 101 | }; |
| 102 | |
| 103 | /** |
| 104 | * An extension of Paint that incorporated per-run modifications to the paint text settings and |
| 105 | * application of a draw filter. It expects its constructor arguments to remain alive and const |
| 106 | * during its lifetime. |
| 107 | */ |
| 108 | class RunPaint : public Paint { |
| 109 | public: |
| 110 | RunPaint(const Paint* paint, SkDrawFilter* filter, const SkSurfaceProps& props) |
| 111 | : fOriginalPaint(paint), fFilter(filter), fProps(props) { |
| 112 | // Initially we represent the original paint. |
| 113 | fPaint = &fOriginalPaint->skPaint(); |
Brian Salomon | 4cbb6e6 | 2017-10-25 15:12:19 -0400 | [diff] [blame] | 114 | fDstColorSpaceInfo = fOriginalPaint->dstColorSpaceInfo(); |
Brian Osman | ec8f8b0 | 2017-05-11 10:57:37 -0400 | [diff] [blame] | 115 | fFilteredPremulColor = fOriginalPaint->filteredPremulColor(); |
Brian Salomon | 6f1d36c | 2017-01-13 12:02:17 -0500 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | bool modifyForRun(const SkTextBlobRunIterator&); |
| 119 | |
| 120 | private: |
| 121 | SkTLazy<SkPaint> fModifiedPaint; |
| 122 | const Paint* fOriginalPaint; |
| 123 | SkDrawFilter* fFilter; |
| 124 | const SkSurfaceProps& fProps; |
| 125 | }; |
| 126 | |
Brian Salomon | 52db940 | 2017-11-07 14:58:55 -0500 | [diff] [blame] | 127 | static uint32_t FilterTextFlags(const SkSurfaceProps& surfaceProps, const SkPaint& paint); |
joshualitt | 8e84a1e | 2016-02-16 11:09:25 -0800 | [diff] [blame] | 128 | |
| 129 | static bool ShouldDisableLCD(const SkPaint& paint); |
| 130 | |
Jim Van Verth | f4c1316 | 2018-01-11 16:40:24 -0500 | [diff] [blame] | 131 | // Functions for drawing large text either as paths or (for color emoji) as scaled glyphs |
Robert Phillips | f440cec | 2018-01-19 11:51:20 -0500 | [diff] [blame^] | 132 | static void DrawBigText(GrTextUtils::Target*, const GrClip& clip, |
| 133 | const SkPaint& paint, const SkMatrix& viewMatrix, const char text[], |
| 134 | size_t byteLength, SkScalar x, SkScalar y, |
| 135 | const SkIRect& clipBounds); |
joshualitt | 8e84a1e | 2016-02-16 11:09:25 -0800 | [diff] [blame] | 136 | |
Robert Phillips | f440cec | 2018-01-19 11:51:20 -0500 | [diff] [blame^] | 137 | static void DrawBigPosText(GrTextUtils::Target*, |
| 138 | const SkSurfaceProps& props, const GrClip& clip, |
| 139 | const SkPaint& paint, const SkMatrix& viewMatrix, |
| 140 | const char text[], size_t byteLength, const SkScalar pos[], |
| 141 | int scalarsPerPosition, const SkPoint& offset, |
| 142 | const SkIRect& clipBounds); |
joshualitt | 0a42e68 | 2015-12-10 13:20:58 -0800 | [diff] [blame] | 143 | }; |
| 144 | |
| 145 | #endif |