blob: 360f6ddbec88c8766a027ac2e9bf771bc8d15aee [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 */
joshualitt8e84a1e2016-02-16 11:09:25 -080028class GrAtlasTextContext {
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
45 static std::unique_ptr<GrAtlasTextContext> 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
joshualitte55750e2016-02-10 12:52:21 -080058private:
Brian Salomonaf597482017-11-07 16:23:34 -050059 GrAtlasTextContext(const Options& options);
joshualitte55750e2016-02-10 12:52:21 -080060
joshualitt1d89e8d2015-04-01 12:40:54 -070061 // sets up the descriptor on the blob and returns a detached cache. Client must attach
Herb Derbyd8327a82018-01-22 14:39:27 -050062 static SkColor ComputeCanonicalColor(const SkPaint&, bool lcd);
brianosmana1e8f8d2016-04-08 06:47:54 -070063 // Determines if we need to use fake gamma (and contrast boost):
Herb Derbyd8327a82018-01-22 14:39:27 -050064 static SkScalerContextFlags ComputeScalerContextFlags(const GrColorSpaceInfo&);
Brian Salomonaf597482017-11-07 16:23:34 -050065 void regenerateTextBlob(GrAtlasTextBlob* bmp,
66 GrAtlasGlyphCache*,
67 const GrShaderCaps&,
68 const GrTextUtils::Paint&,
Herb Derbyd8327a82018-01-22 14:39:27 -050069 SkScalerContextFlags scalerContextFlags,
Brian Salomonaf597482017-11-07 16:23:34 -050070 const SkMatrix& viewMatrix,
71 const SkSurfaceProps&,
72 const SkTextBlob* blob, SkScalar x, SkScalar y,
73 SkDrawFilter* drawFilter) const;
74
Herb Derbyd8327a82018-01-22 14:39:27 -050075 static bool HasLCD(const SkTextBlob*);
joshualitt9bd2daf2015-04-17 09:30:06 -070076
Herb Derbyd8327a82018-01-22 14:39:27 -050077 sk_sp<GrAtlasTextBlob> makeDrawTextBlob(GrTextBlobCache*, GrAtlasGlyphCache*,
78 const GrShaderCaps&,
79 const GrTextUtils::Paint&,
80 SkScalerContextFlags scalerContextFlags,
81 const SkMatrix& viewMatrix,
82 const SkSurfaceProps&,
83 const char text[], size_t byteLength,
84 SkScalar x, SkScalar y) const;
Brian Salomonaf597482017-11-07 16:23:34 -050085
Herb Derbyd8327a82018-01-22 14:39:27 -050086 sk_sp<GrAtlasTextBlob> makeDrawPosTextBlob(GrTextBlobCache*, GrAtlasGlyphCache*,
87 const GrShaderCaps&,
88 const GrTextUtils::Paint&,
89 SkScalerContextFlags scalerContextFlags,
90 const SkMatrix& viewMatrix,
91 const SkSurfaceProps&,
92 const char text[], size_t byteLength,
93 const SkScalar pos[],
94 int scalarsPerPosition,
95 const SkPoint& offset) const;
Brian Salomon52db9402017-11-07 14:58:55 -050096
97 // Functions for appending BMP text to GrAtlasTextBlob
98 static void DrawBmpText(GrAtlasTextBlob*, int runIndex, GrAtlasGlyphCache*,
99 const SkSurfaceProps&, const GrTextUtils::Paint& paint,
Herb Derbyd8327a82018-01-22 14:39:27 -0500100 SkScalerContextFlags scalerContextFlags, const SkMatrix& viewMatrix,
Brian Salomon52db9402017-11-07 14:58:55 -0500101 const char text[], size_t byteLength, SkScalar x, SkScalar y);
102
103 static void DrawBmpPosText(GrAtlasTextBlob*, int runIndex, GrAtlasGlyphCache*,
104 const SkSurfaceProps&, const GrTextUtils::Paint& paint,
Herb Derbyd8327a82018-01-22 14:39:27 -0500105 SkScalerContextFlags scalerContextFlags, const SkMatrix& viewMatrix,
Brian Salomon52db9402017-11-07 14:58:55 -0500106 const char text[], size_t byteLength, const SkScalar pos[],
Jim Van Verthf4c13162018-01-11 16:40:24 -0500107 int scalarsPerPosition, const SkPoint& offset,
108 SkScalar textRatio);
Brian Salomon52db9402017-11-07 14:58:55 -0500109
Jim Van Verth54d9c882018-02-08 16:14:48 -0500110 static void DrawBmpTextAsPaths(GrAtlasTextBlob* blob, int runIndex,
111 const GrTextUtils::Paint& paint, const char text[],
112 size_t byteLength, SkScalar x, SkScalar y);
113
114 static void DrawBmpPosTextAsPaths(GrAtlasTextBlob*, int runIndex,
115 const SkSurfaceProps& props, const GrTextUtils::Paint& paint,
116 const char text[], size_t byteLength,
117 const SkScalar pos[], int scalarsPerPosition,
118 const SkPoint& offset, SkScalar textRatio);
119
Brian Salomon52db9402017-11-07 14:58:55 -0500120 // functions for appending distance field text
Brian Salomonaf597482017-11-07 16:23:34 -0500121 bool canDrawAsDistanceFields(const SkPaint& skPaint, const SkMatrix& viewMatrix,
122 const SkSurfaceProps& props, const GrShaderCaps& caps) const;
Brian Salomon52db9402017-11-07 14:58:55 -0500123
Brian Salomonaf597482017-11-07 16:23:34 -0500124 void drawDFText(GrAtlasTextBlob* blob, int runIndex, GrAtlasGlyphCache*, const SkSurfaceProps&,
Herb Derbyd8327a82018-01-22 14:39:27 -0500125 const GrTextUtils::Paint& paint, SkScalerContextFlags scalerContextFlags,
Brian Salomonaf597482017-11-07 16:23:34 -0500126 const SkMatrix& viewMatrix, const char text[], size_t byteLength, SkScalar x,
127 SkScalar y) const;
Brian Salomon52db9402017-11-07 14:58:55 -0500128
Brian Salomonaf597482017-11-07 16:23:34 -0500129 void drawDFPosText(GrAtlasTextBlob* blob, int runIndex, GrAtlasGlyphCache*,
130 const SkSurfaceProps&, const GrTextUtils::Paint& paint,
Herb Derbyd8327a82018-01-22 14:39:27 -0500131 SkScalerContextFlags scalerContextFlags,
132 const SkMatrix& viewMatrix, const char text[],
Brian Salomonaf597482017-11-07 16:23:34 -0500133 size_t byteLength, const SkScalar pos[], int scalarsPerPosition,
134 const SkPoint& offset) const;
Brian Salomon52db9402017-11-07 14:58:55 -0500135
Brian Salomonaf597482017-11-07 16:23:34 -0500136 void initDistanceFieldPaint(GrAtlasTextBlob* blob,
137 SkPaint* skPaint,
138 SkScalar* textRatio,
139 const SkMatrix& viewMatrix) const;
Brian Salomon52db9402017-11-07 14:58:55 -0500140
141 static void BmpAppendGlyph(GrAtlasTextBlob*, int runIndex, GrAtlasGlyphCache*,
Jim Van Verthc65b65d2018-01-16 16:26:35 -0500142 GrAtlasTextStrike**, const SkGlyph&, SkScalar sx, SkScalar sy,
Jim Van Verthf4c13162018-01-11 16:40:24 -0500143 GrColor color, SkGlyphCache*, SkScalar textRatio);
Brian Salomon52db9402017-11-07 14:58:55 -0500144
Jim Van Verthf4c13162018-01-11 16:40:24 -0500145 static void DfAppendGlyph(GrAtlasTextBlob*, int runIndex, GrAtlasGlyphCache*,
Brian Salomon52db9402017-11-07 14:58:55 -0500146 GrAtlasTextStrike**, const SkGlyph&, SkScalar sx, SkScalar sy,
Jim Van Verthf4c13162018-01-11 16:40:24 -0500147 GrColor color, SkGlyphCache* cache, SkScalar textRatio);
Brian Salomon52db9402017-11-07 14:58:55 -0500148
Hal Canary144caf52016-11-07 17:57:18 -0500149 const GrDistanceFieldAdjustTable* dfAdjustTable() const { return fDistanceAdjustTable.get(); }
joshualitt79dfb2b2015-05-11 08:58:08 -0700150
Hal Canary144caf52016-11-07 17:57:18 -0500151 sk_sp<const GrDistanceFieldAdjustTable> fDistanceAdjustTable;
joshualitt1d89e8d2015-04-01 12:40:54 -0700152
Brian Salomonaf597482017-11-07 16:23:34 -0500153 SkScalar fMinDistanceFieldFontSize;
154 SkScalar fMaxDistanceFieldFontSize;
Brian Salomonb5086962017-12-13 10:59:33 -0500155 bool fDistanceFieldVerticesAlwaysHaveW;
Brian Salomonaf597482017-11-07 16:23:34 -0500156
Hal Canary6f6961e2017-01-31 13:50:44 -0500157#if GR_TEST_UTILS
Herb Derbyd8327a82018-01-22 14:39:27 -0500158 static const SkScalerContextFlags kTextBlobOpScalerContextFlags =
159 SkScalerContextFlags::kFakeGammaAndBoostContrast;
Brian Salomon44acb5b2017-07-18 19:59:24 -0400160 GR_DRAW_OP_TEST_FRIEND(GrAtlasTextOp);
joshualitt79dfb2b2015-05-11 08:58:08 -0700161#endif
joshualitt1d89e8d2015-04-01 12:40:54 -0700162};
163
164#endif