blob: 21657469c57f753f60254dc5c83956ed8ef4ff3f [file] [log] [blame]
joshualitt1d89e8d2015-04-01 12:40:54 -07001/*
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 Derby86240592018-05-24 16:12:31 -04008#ifndef GrTextContext_DEFINED
9#define GrTextContext_DEFINED
joshualitt1d89e8d2015-04-01 12:40:54 -070010
joshualitt1acabf32015-12-10 09:10:10 -080011#include "GrDistanceFieldAdjustTable.h"
joshualitt1d89e8d2015-04-01 12:40:54 -070012#include "GrGeometryProcessor.h"
Herb Derby46dbfbb2018-07-27 15:36:49 -040013#include "GrTextBlob.h"
Brian Salomon6f1d36c2017-01-13 12:02:17 -050014#include "GrTextUtils.h"
Herb Derby46dbfbb2018-07-27 15:36:49 -040015#include "SkGlyphRun.h"
joshualitt1d89e8d2015-04-01 12:40:54 -070016
Hal Canary6f6961e2017-01-31 13:50:44 -050017#if GR_TEST_UTILS
Brian Salomon5ec9def2016-12-20 15:34:05 -050018#include "GrDrawOpTest.h"
joshualitt79dfb2b2015-05-11 08:58:08 -070019#endif
20
Brian Salomon9afd3712016-12-01 10:59:09 -050021class GrDrawOp;
joshualittb7133be2015-04-08 09:08:31 -070022class GrTextBlobCache;
joshualitt6c2c2b02015-07-24 10:37:00 -070023class SkGlyph;
joshualitt1d89e8d2015-04-01 12:40:54 -070024
25/*
joshualitt8e84a1e2016-02-16 11:09:25 -080026 * Renders text using some kind of an atlas, ie BitmapText or DistanceField text
joshualitt1d89e8d2015-04-01 12:40:54 -070027 */
Herb Derby26cbe512018-05-24 14:39:01 -040028class GrTextContext {
joshualitt1d89e8d2015-04-01 12:40:54 -070029public:
Brian Salomonaf597482017-11-07 16:23:34 -050030 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 Salomonb5086962017-12-13 10:59:33 -050041 /** Forces all distance field vertices to use 3 components, not just when in perspective. */
42 bool fDistanceFieldVerticesAlwaysHaveW = false;
Brian Salomonaf597482017-11-07 16:23:34 -050043 };
44
Herb Derby26cbe512018-05-24 14:39:01 -040045 static std::unique_ptr<GrTextContext> Make(const Options& options);
joshualitt1d89e8d2015-04-01 12:40:54 -070046
Brian Salomonf18b1d82017-10-27 11:30:49 -040047 void drawPosText(GrContext*, GrTextUtils::Target*, const GrClip&, const SkPaint&,
Brian Salomon82f44312017-01-11 13:42:54 -050048 const SkMatrix& viewMatrix, const SkSurfaceProps&, const char text[],
49 size_t byteLength, const SkScalar pos[], int scalarsPerPosition,
joshualitt8e84a1e2016-02-16 11:09:25 -080050 const SkPoint& offset, const SkIRect& regionClipBounds);
Herb Derbycddab252018-07-16 11:19:04 -040051 void drawGlyphRunList(GrContext*, GrTextUtils::Target*, const GrClip&,
Herb Derbyb935cf82018-07-26 16:54:18 -040052 const SkMatrix& viewMatrix, const SkSurfaceProps&, const SkGlyphRunList&,
Herb Derbycddab252018-07-16 11:19:04 -040053 const SkIRect& clipBounds);
joshualitt1d89e8d2015-04-01 12:40:54 -070054
Robert Phillips7c525e62018-06-12 10:11:12 -040055 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 Phillipsd2e9f762018-03-07 11:54:37 -050063
Khushal3e7548c2018-05-23 15:45:01 -070064 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 Derby86240592018-05-24 16:12:31 -040069 static void InitDistanceFieldPaint(GrTextBlob* blob,
Khushal3e7548c2018-05-23 15:45:01 -070070 SkPaint* skPaint,
71 const SkMatrix& viewMatrix,
72 const Options& options,
73 SkScalar* textRatio,
74 SkScalerContextFlags* flags);
75
Jim Van Verthc401bb92018-02-15 14:05:24 -050076 class FallbackTextHelper {
77 public:
78 FallbackTextHelper(const SkMatrix& viewMatrix,
Jim Van Verth080a9282018-03-02 10:41:43 -050079 const SkPaint& pathPaint,
Khushalfa8ff092018-06-06 17:46:38 -070080 SkScalar maxTextSize,
Jim Van Verthc401bb92018-02-15 14:05:24 -050081 SkScalar textRatio)
Khushalfa8ff092018-06-06 17:46:38 -070082 : fViewMatrix(viewMatrix)
83 , fTextSize(pathPaint.getTextSize())
84 , fMaxTextSize(maxTextSize)
85 , fTextRatio(textRatio)
86 , fTransformedFallbackTextSize(fMaxTextSize)
87 , fUseTransformedFallback(false) {
Jim Van Verthc401bb92018-02-15 14:05:24 -050088 fMaxScale = viewMatrix.getMaxScale();
89 }
90
91 void appendText(const SkGlyph& glyph, int count, const char* text, SkPoint glyphPos);
Herb Derby86240592018-05-24 16:12:31 -040092 void drawText(GrTextBlob* blob, int runIndex, GrGlyphCache*, const SkSurfaceProps&,
Robert Phillipsc4039ea2018-03-01 11:36:45 -050093 const GrTextUtils::Paint&, SkScalerContextFlags);
Jim Van Verthc401bb92018-02-15 14:05:24 -050094
Khushalfa8ff092018-06-06 17:46:38 -070095 void initializeForDraw(SkPaint* paint, SkScalar* textRatio, SkMatrix* matrix) const;
96 const SkTDArray<char>& fallbackText() const { return fFallbackTxt; }
97
Jim Van Verthc401bb92018-02-15 14:05:24 -050098 private:
99 SkTDArray<char> fFallbackTxt;
100 SkTDArray<SkPoint> fFallbackPos;
101
102 const SkMatrix& fViewMatrix;
Jim Van Verthc401bb92018-02-15 14:05:24 -0500103 SkScalar fTextSize;
104 SkScalar fMaxTextSize;
Jim Van Verth080a9282018-03-02 10:41:43 -0500105 SkScalar fTextRatio;
Jim Van Verthb515ae72018-05-23 16:44:55 -0400106 SkScalar fTransformedFallbackTextSize;
Jim Van Verthc401bb92018-02-15 14:05:24 -0500107 SkScalar fMaxScale;
Jim Van Verthb515ae72018-05-23 16:44:55 -0400108 bool fUseTransformedFallback;
Jim Van Verthc401bb92018-02-15 14:05:24 -0500109 };
110
Herb Derbyf4f6bbf2018-07-27 11:58:37 -0400111 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
Khushalfa8ff092018-06-06 17:46:38 -0700146private:
147 GrTextContext(const Options& options);
148
joshualitt1d89e8d2015-04-01 12:40:54 -0700149 // sets up the descriptor on the blob and returns a detached cache. Client must attach
Herb Derbyd8327a82018-01-22 14:39:27 -0500150 static SkColor ComputeCanonicalColor(const SkPaint&, bool lcd);
brianosmana1e8f8d2016-04-08 06:47:54 -0700151 // Determines if we need to use fake gamma (and contrast boost):
Herb Derbyd8327a82018-01-22 14:39:27 -0500152 static SkScalerContextFlags ComputeScalerContextFlags(const GrColorSpaceInfo&);
Herb Derbycddab252018-07-16 11:19:04 -0400153
154 void regenerateGlyphRunList(GrTextBlob* bmp,
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500155 GrGlyphCache*,
Brian Salomonaf597482017-11-07 16:23:34 -0500156 const GrShaderCaps&,
157 const GrTextUtils::Paint&,
Herb Derbyd8327a82018-01-22 14:39:27 -0500158 SkScalerContextFlags scalerContextFlags,
Brian Salomonaf597482017-11-07 16:23:34 -0500159 const SkMatrix& viewMatrix,
160 const SkSurfaceProps&,
Herb Derbyb935cf82018-07-26 16:54:18 -0400161 const SkGlyphRunList& glyphRunList) const;
joshualitt9bd2daf2015-04-17 09:30:06 -0700162
Herb Derby86240592018-05-24 16:12:31 -0400163 sk_sp<GrTextBlob> makeDrawPosTextBlob(GrTextBlobCache*, GrGlyphCache*,
Herb Derbyd8327a82018-01-22 14:39:27 -0500164 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 Salomon52db9402017-11-07 14:58:55 -0500173
Herb Derby86240592018-05-24 16:12:31 -0400174 // Functions for appending BMP text to GrTextBlob
Herb Derby86240592018-05-24 16:12:31 -0400175 static void DrawBmpPosText(GrTextBlob*, int runIndex, GrGlyphCache*,
Brian Salomon52db9402017-11-07 14:58:55 -0500176 const SkSurfaceProps&, const GrTextUtils::Paint& paint,
Herb Derbyd8327a82018-01-22 14:39:27 -0500177 SkScalerContextFlags scalerContextFlags, const SkMatrix& viewMatrix,
Brian Salomon52db9402017-11-07 14:58:55 -0500178 const char text[], size_t byteLength, const SkScalar pos[],
Jim Van Verthc401bb92018-02-15 14:05:24 -0500179 int scalarsPerPosition, const SkPoint& offset);
Brian Salomon52db9402017-11-07 14:58:55 -0500180
Herb Derby46dbfbb2018-07-27 15:36:49 -0400181 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 Derby86240592018-05-24 16:12:31 -0400186 static void DrawBmpPosTextAsPaths(GrTextBlob*, int runIndex, GrGlyphCache*,
Jim Van Verthc401bb92018-02-15 14:05:24 -0500187 const SkSurfaceProps&, const GrTextUtils::Paint& paint,
188 SkScalerContextFlags scalerContextFlags,
189 const SkMatrix& viewMatrix,
Jim Van Verth54d9c882018-02-08 16:14:48 -0500190 const char text[], size_t byteLength,
191 const SkScalar pos[], int scalarsPerPosition,
Jim Van Verthc401bb92018-02-15 14:05:24 -0500192 const SkPoint& offset);
Jim Van Verth54d9c882018-02-08 16:14:48 -0500193
Brian Salomon52db9402017-11-07 14:58:55 -0500194 // functions for appending distance field text
Herb Derby86240592018-05-24 16:12:31 -0400195 void drawDFPosText(GrTextBlob* blob, int runIndex, GrGlyphCache*,
Brian Salomonaf597482017-11-07 16:23:34 -0500196 const SkSurfaceProps&, const GrTextUtils::Paint& paint,
Herb Derbyd8327a82018-01-22 14:39:27 -0500197 SkScalerContextFlags scalerContextFlags,
198 const SkMatrix& viewMatrix, const char text[],
Brian Salomonaf597482017-11-07 16:23:34 -0500199 size_t byteLength, const SkScalar pos[], int scalarsPerPosition,
200 const SkPoint& offset) const;
Brian Salomon52db9402017-11-07 14:58:55 -0500201
Herb Derbyf4f6bbf2018-07-27 11:58:37 -0400202 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 Derby86240592018-05-24 16:12:31 -0400208 static void BmpAppendGlyph(GrTextBlob*, int runIndex, GrGlyphCache*,
Robert Phillipscaf1ebb2018-03-01 14:28:44 -0500209 sk_sp<GrTextStrike>*, const SkGlyph&, SkScalar sx, SkScalar sy,
Jim Van Verthb515ae72018-05-23 16:44:55 -0400210 GrColor color, SkGlyphCache*, SkScalar textRatio, bool needsXform);
Brian Salomon52db9402017-11-07 14:58:55 -0500211
Herb Derby86240592018-05-24 16:12:31 -0400212 static void DfAppendGlyph(GrTextBlob*, int runIndex, GrGlyphCache*,
Robert Phillipscaf1ebb2018-03-01 14:28:44 -0500213 sk_sp<GrTextStrike>*, const SkGlyph&, SkScalar sx, SkScalar sy,
Jim Van Verthf4c13162018-01-11 16:40:24 -0500214 GrColor color, SkGlyphCache* cache, SkScalar textRatio);
Brian Salomon52db9402017-11-07 14:58:55 -0500215
Hal Canary144caf52016-11-07 17:57:18 -0500216 const GrDistanceFieldAdjustTable* dfAdjustTable() const { return fDistanceAdjustTable.get(); }
joshualitt79dfb2b2015-05-11 08:58:08 -0700217
Hal Canary144caf52016-11-07 17:57:18 -0500218 sk_sp<const GrDistanceFieldAdjustTable> fDistanceAdjustTable;
joshualitt1d89e8d2015-04-01 12:40:54 -0700219
Khushal3e7548c2018-05-23 15:45:01 -0700220 Options fOptions;
Brian Salomonaf597482017-11-07 16:23:34 -0500221
Hal Canary6f6961e2017-01-31 13:50:44 -0500222#if GR_TEST_UTILS
Herb Derbyd8327a82018-01-22 14:39:27 -0500223 static const SkScalerContextFlags kTextBlobOpScalerContextFlags =
224 SkScalerContextFlags::kFakeGammaAndBoostContrast;
Brian Salomon44acb5b2017-07-18 19:59:24 -0400225 GR_DRAW_OP_TEST_FRIEND(GrAtlasTextOp);
joshualitt79dfb2b2015-05-11 08:58:08 -0700226#endif
joshualitt1d89e8d2015-04-01 12:40:54 -0700227};
228
Herb Derby86240592018-05-24 16:12:31 -0400229#endif // GrTextContext_DEFINED