blob: cc582dbe853b6e077c4f43f421e20566ec17662c [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "src/core/SkGlyphRun.h"
12#include "src/gpu/GrGeometryProcessor.h"
13#include "src/gpu/text/GrDistanceFieldAdjustTable.h"
14#include "src/gpu/text/GrTextTarget.h"
joshualitt1d89e8d2015-04-01 12:40:54 -070015
Hal Canary6f6961e2017-01-31 13:50:44 -050016#if GR_TEST_UTILS
Mike Kleinc0bd9f92019-04-23 12:05:21 -050017#include "src/gpu/GrDrawOpTest.h"
joshualitt79dfb2b2015-05-11 08:58:08 -070018#endif
19
Brian Salomon9afd3712016-12-01 10:59:09 -050020class GrDrawOp;
Robert Phillipsb97da532019-02-12 15:24:12 -050021class GrRecordingContext;
joshualittb7133be2015-04-08 09:08:31 -070022class GrTextBlobCache;
joshualitt6c2c2b02015-07-24 10:37:00 -070023class SkGlyph;
Herb Derbydc214c22018-11-08 13:31:39 -050024class GrTextBlob;
joshualitt1d89e8d2015-04-01 12:40:54 -070025
26/*
joshualitt8e84a1e2016-02-16 11:09:25 -080027 * Renders text using some kind of an atlas, ie BitmapText or DistanceField text
joshualitt1d89e8d2015-04-01 12:40:54 -070028 */
Herb Derby26cbe512018-05-24 14:39:01 -040029class GrTextContext {
joshualitt1d89e8d2015-04-01 12:40:54 -070030public:
Brian Salomonaf597482017-11-07 16:23:34 -050031 struct Options {
32 /**
33 * Below this size (in device space) distance field text will not be used. Negative means
34 * use a default value.
35 */
36 SkScalar fMinDistanceFieldFontSize = -1.f;
37 /**
38 * Above this size (in device space) distance field text will not be used and glyphs will
39 * be rendered from outline as individual paths. Negative means use a default value.
40 */
41 SkScalar fMaxDistanceFieldFontSize = -1.f;
Brian Salomonb5086962017-12-13 10:59:33 -050042 /** Forces all distance field vertices to use 3 components, not just when in perspective. */
43 bool fDistanceFieldVerticesAlwaysHaveW = false;
Brian Salomonaf597482017-11-07 16:23:34 -050044 };
45
Herb Derby26cbe512018-05-24 14:39:01 -040046 static std::unique_ptr<GrTextContext> Make(const Options& options);
joshualitt1d89e8d2015-04-01 12:40:54 -070047
Robert Phillips69893702019-02-22 11:16:30 -050048 void drawGlyphRunList(GrRecordingContext*, GrTextTarget*, const GrClip&,
Robert Phillipse4643cc2018-08-14 13:01:29 -040049 const SkMatrix& viewMatrix, const SkSurfaceProps&, const SkGlyphRunList&);
joshualitt1d89e8d2015-04-01 12:40:54 -070050
Robert Phillipsb97da532019-02-12 15:24:12 -050051 std::unique_ptr<GrDrawOp> createOp_TestingOnly(GrRecordingContext*,
Robert Phillips7c525e62018-06-12 10:11:12 -040052 GrTextContext*,
53 GrRenderTargetContext*,
Mike Reed191e64b2019-01-02 15:35:29 -050054 const SkPaint&, const SkFont&,
Robert Phillips7c525e62018-06-12 10:11:12 -040055 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);
Mike Reedf2b074e2018-12-03 16:52:59 -050061 static bool CanDrawAsDistanceFields(const SkPaint&, const SkFont&, const SkMatrix& viewMatrix,
Khushal3e7548c2018-05-23 15:45:01 -070062 const SkSurfaceProps& props,
63 bool contextSupportsDistanceFieldText,
64 const Options& options);
Herb Derby881c9622019-02-19 11:43:12 -050065
66 static SkFont InitDistanceFieldFont(const SkFont& font,
67 const SkMatrix& viewMatrix,
68 const Options& options,
69 SkScalar* textRatio);
70
71 static SkPaint InitDistanceFieldPaint(const SkPaint& paint);
Herb Derby881c9622019-02-19 11:43:12 -050072
73 static std::pair<SkScalar, SkScalar> InitDistanceFieldMinMaxScale(SkScalar textSize,
74 const SkMatrix& viewMatrix,
75 const Options& options);
Khushal3e7548c2018-05-23 15:45:01 -070076
Khushalfa8ff092018-06-06 17:46:38 -070077private:
78 GrTextContext(const Options& options);
79
joshualitt1d89e8d2015-04-01 12:40:54 -070080 // sets up the descriptor on the blob and returns a detached cache. Client must attach
Herb Derbyd8327a82018-01-22 14:39:27 -050081 static SkColor ComputeCanonicalColor(const SkPaint&, bool lcd);
brianosmana1e8f8d2016-04-08 06:47:54 -070082 // Determines if we need to use fake gamma (and contrast boost):
Herb Derbyd8327a82018-01-22 14:39:27 -050083 static SkScalerContextFlags ComputeScalerContextFlags(const GrColorSpaceInfo&);
Herb Derbycddab252018-07-16 11:19:04 -040084
Hal Canary144caf52016-11-07 17:57:18 -050085 const GrDistanceFieldAdjustTable* dfAdjustTable() const { return fDistanceAdjustTable.get(); }
joshualitt79dfb2b2015-05-11 08:58:08 -070086
Hal Canary144caf52016-11-07 17:57:18 -050087 sk_sp<const GrDistanceFieldAdjustTable> fDistanceAdjustTable;
joshualitt1d89e8d2015-04-01 12:40:54 -070088
Khushal3e7548c2018-05-23 15:45:01 -070089 Options fOptions;
Brian Salomonaf597482017-11-07 16:23:34 -050090
Hal Canary6f6961e2017-01-31 13:50:44 -050091#if GR_TEST_UTILS
Herb Derbyd8327a82018-01-22 14:39:27 -050092 static const SkScalerContextFlags kTextBlobOpScalerContextFlags =
93 SkScalerContextFlags::kFakeGammaAndBoostContrast;
Brian Salomon44acb5b2017-07-18 19:59:24 -040094 GR_DRAW_OP_TEST_FRIEND(GrAtlasTextOp);
joshualitt79dfb2b2015-05-11 08:58:08 -070095#endif
joshualitt1d89e8d2015-04-01 12:40:54 -070096};
97
Herb Derby86240592018-05-24 16:12:31 -040098#endif // GrTextContext_DEFINED