blob: 109fc9c850892d7fb4ecd87eaf474a9936d742db [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
Herb Derbycddab252018-07-16 11:19:04 -040047 void drawGlyphRunList(GrContext*, GrTextUtils::Target*, const GrClip&,
Herb Derbyb935cf82018-07-26 16:54:18 -040048 const SkMatrix& viewMatrix, const SkSurfaceProps&, const SkGlyphRunList&,
Herb Derbycddab252018-07-16 11:19:04 -040049 const SkIRect& clipBounds);
joshualitt1d89e8d2015-04-01 12:40:54 -070050
Robert Phillips7c525e62018-06-12 10:11:12 -040051 std::unique_ptr<GrDrawOp> createOp_TestingOnly(GrContext*,
52 GrTextContext*,
53 GrRenderTargetContext*,
54 const SkPaint&,
55 const SkMatrix& viewMatrix,
56 const char* text,
57 int x,
58 int y);
Robert Phillipsd2e9f762018-03-07 11:54:37 -050059
Khushal3e7548c2018-05-23 15:45:01 -070060 static void SanitizeOptions(Options* options);
61 static bool CanDrawAsDistanceFields(const SkPaint& skPaint, const SkMatrix& viewMatrix,
62 const SkSurfaceProps& props,
63 bool contextSupportsDistanceFieldText,
64 const Options& options);
Herb Derby86240592018-05-24 16:12:31 -040065 static void InitDistanceFieldPaint(GrTextBlob* blob,
Khushal3e7548c2018-05-23 15:45:01 -070066 SkPaint* skPaint,
67 const SkMatrix& viewMatrix,
68 const Options& options,
69 SkScalar* textRatio,
70 SkScalerContextFlags* flags);
71
Herb Derbyf4f6bbf2018-07-27 11:58:37 -040072 class FallbackGlyphRunHelper {
73 public:
74 FallbackGlyphRunHelper(const SkMatrix& viewMatrix,
75 const SkPaint& pathPaint,
76 SkScalar maxTextSize,
77 SkScalar textRatio)
78 : fViewMatrix(viewMatrix)
79 , fTextSize(pathPaint.getTextSize())
80 , fMaxTextSize(maxTextSize)
81 , fTextRatio(textRatio)
82 , fTransformedFallbackTextSize(fMaxTextSize)
83 , fUseTransformedFallback(false) {
84 fMaxScale = viewMatrix.getMaxScale();
85 }
86
Herb Derby82c11e02018-08-06 16:40:12 -040087 void appendGlyph(const SkGlyph& glyph, SkGlyphID glyphID, SkPoint glyphPos);
88 void drawGlyphs(
89 GrTextBlob* blob, int runIndex, GrGlyphCache* cache,
90 const SkSurfaceProps& props, const GrTextUtils::Paint& flags,
91 SkScalerContextFlags scalerContextFlags);
Herb Derbyf4f6bbf2018-07-27 11:58:37 -040092
93 void initializeForDraw(SkPaint* paint, SkScalar* textRatio, SkMatrix* matrix) const;
94 const std::vector<SkGlyphID>& fallbackText() const { return fFallbackTxt; }
95
96 private:
97 std::vector<SkGlyphID> fFallbackTxt;
98 std::vector<SkPoint> fFallbackPos;
99
100 const SkMatrix& fViewMatrix;
101 SkScalar fTextSize;
102 SkScalar fMaxTextSize;
103 SkScalar fTextRatio;
104 SkScalar fTransformedFallbackTextSize;
105 SkScalar fMaxScale;
106 bool fUseTransformedFallback;
107 };
108
Khushalfa8ff092018-06-06 17:46:38 -0700109private:
110 GrTextContext(const Options& options);
111
joshualitt1d89e8d2015-04-01 12:40:54 -0700112 // sets up the descriptor on the blob and returns a detached cache. Client must attach
Herb Derbyd8327a82018-01-22 14:39:27 -0500113 static SkColor ComputeCanonicalColor(const SkPaint&, bool lcd);
brianosmana1e8f8d2016-04-08 06:47:54 -0700114 // Determines if we need to use fake gamma (and contrast boost):
Herb Derbyd8327a82018-01-22 14:39:27 -0500115 static SkScalerContextFlags ComputeScalerContextFlags(const GrColorSpaceInfo&);
Herb Derbycddab252018-07-16 11:19:04 -0400116
117 void regenerateGlyphRunList(GrTextBlob* bmp,
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500118 GrGlyphCache*,
Brian Salomonaf597482017-11-07 16:23:34 -0500119 const GrShaderCaps&,
120 const GrTextUtils::Paint&,
Herb Derbyd8327a82018-01-22 14:39:27 -0500121 SkScalerContextFlags scalerContextFlags,
Brian Salomonaf597482017-11-07 16:23:34 -0500122 const SkMatrix& viewMatrix,
123 const SkSurfaceProps&,
Herb Derby74c6ed32018-07-28 18:07:54 -0400124 const SkGlyphRunList& glyphRunList,
125 SkGlyphRunListDrawer* glyphDrawer);
joshualitt9bd2daf2015-04-17 09:30:06 -0700126
Herb Derby049b5d92018-08-01 13:56:26 -0400127 static void AppendGlyph(GrTextBlob*, int runIndex, GrGlyphCache*,
128 sk_sp<GrTextStrike>*, const SkGlyph&, GrGlyph::MaskStyle maskStyle,
129 SkScalar sx, SkScalar sy,
130 GrColor color, SkGlyphCache*, SkScalar textRatio,
131 bool needsTransform);
Herb Derbyf4f6bbf2018-07-27 11:58:37 -0400132
Brian Salomon52db9402017-11-07 14:58:55 -0500133
Hal Canary144caf52016-11-07 17:57:18 -0500134 const GrDistanceFieldAdjustTable* dfAdjustTable() const { return fDistanceAdjustTable.get(); }
joshualitt79dfb2b2015-05-11 08:58:08 -0700135
Hal Canary144caf52016-11-07 17:57:18 -0500136 sk_sp<const GrDistanceFieldAdjustTable> fDistanceAdjustTable;
joshualitt1d89e8d2015-04-01 12:40:54 -0700137
Khushal3e7548c2018-05-23 15:45:01 -0700138 Options fOptions;
Brian Salomonaf597482017-11-07 16:23:34 -0500139
Hal Canary6f6961e2017-01-31 13:50:44 -0500140#if GR_TEST_UTILS
Herb Derbyd8327a82018-01-22 14:39:27 -0500141 static const SkScalerContextFlags kTextBlobOpScalerContextFlags =
142 SkScalerContextFlags::kFakeGammaAndBoostContrast;
Brian Salomon44acb5b2017-07-18 19:59:24 -0400143 GR_DRAW_OP_TEST_FRIEND(GrAtlasTextOp);
joshualitt79dfb2b2015-05-11 08:58:08 -0700144#endif
joshualitt1d89e8d2015-04-01 12:40:54 -0700145};
146
Herb Derby86240592018-05-24 16:12:31 -0400147#endif // GrTextContext_DEFINED