blob: b5bb22f3d5ffa64c90748646053783ef26e3cd90 [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
8#ifndef GrAtlasTextContext_DEFINED
9#define GrAtlasTextContext_DEFINED
10
joshualitt374b2f72015-07-21 08:05:03 -070011#include "GrAtlasTextBlob.h"
joshualitt1acabf32015-12-10 09:10:10 -080012#include "GrDistanceFieldAdjustTable.h"
joshualitt1d89e8d2015-04-01 12:40:54 -070013#include "GrGeometryProcessor.h"
Brian Salomon6f1d36c2017-01-13 12:02:17 -050014#include "GrTextUtils.h"
halcanary33779752015-10-27 14:01:05 -070015#include "SkTextBlobRunIterator.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 drawText(GrContext*, GrTextUtils::Target*, const GrClip&, const SkPaint&,
joshualitt2c89bc12016-02-11 05:42:30 -080048 const SkMatrix& viewMatrix, const SkSurfaceProps&, const char text[],
Brian Salomon82f44312017-01-11 13:42:54 -050049 size_t byteLength, SkScalar x, SkScalar y, const SkIRect& regionClipBounds);
Brian Salomonf18b1d82017-10-27 11:30:49 -040050 void drawPosText(GrContext*, GrTextUtils::Target*, const GrClip&, const SkPaint&,
Brian Salomon82f44312017-01-11 13:42:54 -050051 const SkMatrix& viewMatrix, const SkSurfaceProps&, const char text[],
52 size_t byteLength, const SkScalar pos[], int scalarsPerPosition,
joshualitt8e84a1e2016-02-16 11:09:25 -080053 const SkPoint& offset, const SkIRect& regionClipBounds);
Brian Salomonf18b1d82017-10-27 11:30:49 -040054 void drawTextBlob(GrContext*, GrTextUtils::Target*, const GrClip&, const SkPaint&,
joshualitt2c89bc12016-02-11 05:42:30 -080055 const SkMatrix& viewMatrix, const SkSurfaceProps&, const SkTextBlob*,
Brian Salomonf18b1d82017-10-27 11:30:49 -040056 SkScalar x, SkScalar y, SkDrawFilter*, const SkIRect& clipBounds);
joshualitt1d89e8d2015-04-01 12:40:54 -070057
Herb Derby26cbe512018-05-24 14:39:01 -040058 std::unique_ptr<GrDrawOp> createOp_TestingOnly(GrContext*, GrTextContext*,
Robert Phillipsd2e9f762018-03-07 11:54:37 -050059 GrRenderTargetContext*, const SkPaint&,
60 const SkMatrix& viewMatrix, const char* text,
61 int x, int y);
62
Khushal3e7548c2018-05-23 15:45:01 -070063 static void SanitizeOptions(Options* options);
64 static bool CanDrawAsDistanceFields(const SkPaint& skPaint, const SkMatrix& viewMatrix,
65 const SkSurfaceProps& props,
66 bool contextSupportsDistanceFieldText,
67 const Options& options);
68 static void InitDistanceFieldPaint(GrAtlasTextBlob* blob,
69 SkPaint* skPaint,
70 const SkMatrix& viewMatrix,
71 const Options& options,
72 SkScalar* textRatio,
73 SkScalerContextFlags* flags);
74
joshualitte55750e2016-02-10 12:52:21 -080075private:
Herb Derby26cbe512018-05-24 14:39:01 -040076 GrTextContext(const Options& options);
joshualitte55750e2016-02-10 12:52:21 -080077
Jim Van Verthc401bb92018-02-15 14:05:24 -050078 class FallbackTextHelper {
79 public:
80 FallbackTextHelper(const SkMatrix& viewMatrix,
Jim Van Verth080a9282018-03-02 10:41:43 -050081 const SkPaint& pathPaint,
82 const GrGlyphCache* glyphCache,
Jim Van Verthc401bb92018-02-15 14:05:24 -050083 SkScalar textRatio)
84 : fViewMatrix(viewMatrix)
Jim Van Verth080a9282018-03-02 10:41:43 -050085 , fTextSize(pathPaint.getTextSize())
86 , fMaxTextSize(glyphCache->getGlyphSizeLimit())
Jim Van Verthc401bb92018-02-15 14:05:24 -050087 , fTextRatio(textRatio)
Jim Van Verthb515ae72018-05-23 16:44:55 -040088 , fTransformedFallbackTextSize(fMaxTextSize)
89 , fUseTransformedFallback(false) {
Jim Van Verthc401bb92018-02-15 14:05:24 -050090 fMaxScale = viewMatrix.getMaxScale();
91 }
92
93 void appendText(const SkGlyph& glyph, int count, const char* text, SkPoint glyphPos);
Robert Phillipsc4039ea2018-03-01 11:36:45 -050094 void drawText(GrAtlasTextBlob* blob, int runIndex, GrGlyphCache*, const SkSurfaceProps&,
95 const GrTextUtils::Paint&, SkScalerContextFlags);
Jim Van Verthc401bb92018-02-15 14:05:24 -050096
97 private:
98 SkTDArray<char> fFallbackTxt;
99 SkTDArray<SkPoint> fFallbackPos;
100
101 const SkMatrix& fViewMatrix;
Jim Van Verthc401bb92018-02-15 14:05:24 -0500102 SkScalar fTextSize;
103 SkScalar fMaxTextSize;
Jim Van Verth080a9282018-03-02 10:41:43 -0500104 SkScalar fTextRatio;
Jim Van Verthb515ae72018-05-23 16:44:55 -0400105 SkScalar fTransformedFallbackTextSize;
Jim Van Verthc401bb92018-02-15 14:05:24 -0500106 SkScalar fMaxScale;
Jim Van Verthb515ae72018-05-23 16:44:55 -0400107 bool fUseTransformedFallback;
Jim Van Verthc401bb92018-02-15 14:05:24 -0500108 };
109
joshualitt1d89e8d2015-04-01 12:40:54 -0700110 // sets up the descriptor on the blob and returns a detached cache. Client must attach
Herb Derbyd8327a82018-01-22 14:39:27 -0500111 static SkColor ComputeCanonicalColor(const SkPaint&, bool lcd);
brianosmana1e8f8d2016-04-08 06:47:54 -0700112 // Determines if we need to use fake gamma (and contrast boost):
Herb Derbyd8327a82018-01-22 14:39:27 -0500113 static SkScalerContextFlags ComputeScalerContextFlags(const GrColorSpaceInfo&);
Brian Salomonaf597482017-11-07 16:23:34 -0500114 void regenerateTextBlob(GrAtlasTextBlob* bmp,
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500115 GrGlyphCache*,
Brian Salomonaf597482017-11-07 16:23:34 -0500116 const GrShaderCaps&,
117 const GrTextUtils::Paint&,
Herb Derbyd8327a82018-01-22 14:39:27 -0500118 SkScalerContextFlags scalerContextFlags,
Brian Salomonaf597482017-11-07 16:23:34 -0500119 const SkMatrix& viewMatrix,
120 const SkSurfaceProps&,
121 const SkTextBlob* blob, SkScalar x, SkScalar y,
122 SkDrawFilter* drawFilter) const;
123
Herb Derbyd8327a82018-01-22 14:39:27 -0500124 static bool HasLCD(const SkTextBlob*);
joshualitt9bd2daf2015-04-17 09:30:06 -0700125
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500126 sk_sp<GrAtlasTextBlob> makeDrawTextBlob(GrTextBlobCache*, GrGlyphCache*,
Herb Derbyd8327a82018-01-22 14:39:27 -0500127 const GrShaderCaps&,
128 const GrTextUtils::Paint&,
129 SkScalerContextFlags scalerContextFlags,
130 const SkMatrix& viewMatrix,
131 const SkSurfaceProps&,
132 const char text[], size_t byteLength,
133 SkScalar x, SkScalar y) const;
Brian Salomonaf597482017-11-07 16:23:34 -0500134
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500135 sk_sp<GrAtlasTextBlob> makeDrawPosTextBlob(GrTextBlobCache*, GrGlyphCache*,
Herb Derbyd8327a82018-01-22 14:39:27 -0500136 const GrShaderCaps&,
137 const GrTextUtils::Paint&,
138 SkScalerContextFlags scalerContextFlags,
139 const SkMatrix& viewMatrix,
140 const SkSurfaceProps&,
141 const char text[], size_t byteLength,
142 const SkScalar pos[],
143 int scalarsPerPosition,
144 const SkPoint& offset) const;
Brian Salomon52db9402017-11-07 14:58:55 -0500145
146 // Functions for appending BMP text to GrAtlasTextBlob
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500147 static void DrawBmpText(GrAtlasTextBlob*, int runIndex, GrGlyphCache*,
Brian Salomon52db9402017-11-07 14:58:55 -0500148 const SkSurfaceProps&, const GrTextUtils::Paint& paint,
Herb Derbyd8327a82018-01-22 14:39:27 -0500149 SkScalerContextFlags scalerContextFlags, const SkMatrix& viewMatrix,
Brian Salomon52db9402017-11-07 14:58:55 -0500150 const char text[], size_t byteLength, SkScalar x, SkScalar y);
151
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500152 static void DrawBmpPosText(GrAtlasTextBlob*, int runIndex, GrGlyphCache*,
Brian Salomon52db9402017-11-07 14:58:55 -0500153 const SkSurfaceProps&, const GrTextUtils::Paint& paint,
Herb Derbyd8327a82018-01-22 14:39:27 -0500154 SkScalerContextFlags scalerContextFlags, const SkMatrix& viewMatrix,
Brian Salomon52db9402017-11-07 14:58:55 -0500155 const char text[], size_t byteLength, const SkScalar pos[],
Jim Van Verthc401bb92018-02-15 14:05:24 -0500156 int scalarsPerPosition, const SkPoint& offset);
Brian Salomon52db9402017-11-07 14:58:55 -0500157
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500158 static void DrawBmpTextAsPaths(GrAtlasTextBlob*, int runIndex, GrGlyphCache*,
Jim Van Verthc401bb92018-02-15 14:05:24 -0500159 const SkSurfaceProps&, const GrTextUtils::Paint& paint,
160 SkScalerContextFlags scalerContextFlags,
161 const SkMatrix& viewMatrix, const char text[],
Jim Van Verth54d9c882018-02-08 16:14:48 -0500162 size_t byteLength, SkScalar x, SkScalar y);
163
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500164 static void DrawBmpPosTextAsPaths(GrAtlasTextBlob*, int runIndex, GrGlyphCache*,
Jim Van Verthc401bb92018-02-15 14:05:24 -0500165 const SkSurfaceProps&, const GrTextUtils::Paint& paint,
166 SkScalerContextFlags scalerContextFlags,
167 const SkMatrix& viewMatrix,
Jim Van Verth54d9c882018-02-08 16:14:48 -0500168 const char text[], size_t byteLength,
169 const SkScalar pos[], int scalarsPerPosition,
Jim Van Verthc401bb92018-02-15 14:05:24 -0500170 const SkPoint& offset);
Jim Van Verth54d9c882018-02-08 16:14:48 -0500171
Brian Salomon52db9402017-11-07 14:58:55 -0500172 // functions for appending distance field text
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500173 void drawDFText(GrAtlasTextBlob* blob, int runIndex, GrGlyphCache*, const SkSurfaceProps&,
Herb Derbyd8327a82018-01-22 14:39:27 -0500174 const GrTextUtils::Paint& paint, SkScalerContextFlags scalerContextFlags,
Brian Salomonaf597482017-11-07 16:23:34 -0500175 const SkMatrix& viewMatrix, const char text[], size_t byteLength, SkScalar x,
176 SkScalar y) const;
Brian Salomon52db9402017-11-07 14:58:55 -0500177
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500178 void drawDFPosText(GrAtlasTextBlob* blob, int runIndex, GrGlyphCache*,
Brian Salomonaf597482017-11-07 16:23:34 -0500179 const SkSurfaceProps&, const GrTextUtils::Paint& paint,
Herb Derbyd8327a82018-01-22 14:39:27 -0500180 SkScalerContextFlags scalerContextFlags,
181 const SkMatrix& viewMatrix, const char text[],
Brian Salomonaf597482017-11-07 16:23:34 -0500182 size_t byteLength, const SkScalar pos[], int scalarsPerPosition,
183 const SkPoint& offset) const;
Brian Salomon52db9402017-11-07 14:58:55 -0500184
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500185 static void BmpAppendGlyph(GrAtlasTextBlob*, int runIndex, GrGlyphCache*,
Robert Phillipscaf1ebb2018-03-01 14:28:44 -0500186 sk_sp<GrTextStrike>*, const SkGlyph&, SkScalar sx, SkScalar sy,
Jim Van Verthb515ae72018-05-23 16:44:55 -0400187 GrColor color, SkGlyphCache*, SkScalar textRatio, bool needsXform);
Brian Salomon52db9402017-11-07 14:58:55 -0500188
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500189 static void DfAppendGlyph(GrAtlasTextBlob*, int runIndex, GrGlyphCache*,
Robert Phillipscaf1ebb2018-03-01 14:28:44 -0500190 sk_sp<GrTextStrike>*, const SkGlyph&, SkScalar sx, SkScalar sy,
Jim Van Verthf4c13162018-01-11 16:40:24 -0500191 GrColor color, SkGlyphCache* cache, SkScalar textRatio);
Brian Salomon52db9402017-11-07 14:58:55 -0500192
Hal Canary144caf52016-11-07 17:57:18 -0500193 const GrDistanceFieldAdjustTable* dfAdjustTable() const { return fDistanceAdjustTable.get(); }
joshualitt79dfb2b2015-05-11 08:58:08 -0700194
Hal Canary144caf52016-11-07 17:57:18 -0500195 sk_sp<const GrDistanceFieldAdjustTable> fDistanceAdjustTable;
joshualitt1d89e8d2015-04-01 12:40:54 -0700196
Khushal3e7548c2018-05-23 15:45:01 -0700197 Options fOptions;
Brian Salomonaf597482017-11-07 16:23:34 -0500198
Hal Canary6f6961e2017-01-31 13:50:44 -0500199#if GR_TEST_UTILS
Herb Derbyd8327a82018-01-22 14:39:27 -0500200 static const SkScalerContextFlags kTextBlobOpScalerContextFlags =
201 SkScalerContextFlags::kFakeGammaAndBoostContrast;
Brian Salomon44acb5b2017-07-18 19:59:24 -0400202 GR_DRAW_OP_TEST_FRIEND(GrAtlasTextOp);
joshualitt79dfb2b2015-05-11 08:58:08 -0700203#endif
joshualitt1d89e8d2015-04-01 12:40:54 -0700204};
205
206#endif