blob: 46f5d3f120a560daa40bfbb793edcf0f0d97fd82 [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 Derbyc1b482c2018-08-09 15:02:27 -040013#include "GrTextTarget.h"
Herb Derby46dbfbb2018-07-27 15:36:49 -040014#include "SkGlyphRun.h"
joshualitt1d89e8d2015-04-01 12:40:54 -070015
Hal Canary6f6961e2017-01-31 13:50:44 -050016#if GR_TEST_UTILS
Brian Salomon5ec9def2016-12-20 15:34:05 -050017#include "GrDrawOpTest.h"
joshualitt79dfb2b2015-05-11 08:58:08 -070018#endif
19
Brian Salomon9afd3712016-12-01 10:59:09 -050020class GrDrawOp;
joshualittb7133be2015-04-08 09:08:31 -070021class GrTextBlobCache;
joshualitt6c2c2b02015-07-24 10:37:00 -070022class SkGlyph;
Herb Derbydc214c22018-11-08 13:31:39 -050023class GrTextBlob;
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 Derbyc1b482c2018-08-09 15:02:27 -040047 void drawGlyphRunList(GrContext*, GrTextTarget*, const GrClip&,
Robert Phillipse4643cc2018-08-14 13:01:29 -040048 const SkMatrix& viewMatrix, const SkSurfaceProps&, const SkGlyphRunList&);
joshualitt1d89e8d2015-04-01 12:40:54 -070049
Robert Phillips7c525e62018-06-12 10:11:12 -040050 std::unique_ptr<GrDrawOp> createOp_TestingOnly(GrContext*,
51 GrTextContext*,
52 GrRenderTargetContext*,
53 const SkPaint&,
54 const SkMatrix& viewMatrix,
55 const char* text,
56 int x,
57 int y);
Robert Phillipsd2e9f762018-03-07 11:54:37 -050058
Khushal3e7548c2018-05-23 15:45:01 -070059 static void SanitizeOptions(Options* options);
Mike Reedf2b074e2018-12-03 16:52:59 -050060 static bool CanDrawAsDistanceFields(const SkPaint&, const SkFont&, const SkMatrix& viewMatrix,
Khushal3e7548c2018-05-23 15:45:01 -070061 const SkSurfaceProps& props,
62 bool contextSupportsDistanceFieldText,
63 const Options& options);
Herb Derby6f274892018-12-17 17:30:07 -050064 static void InitDistanceFieldPaint(SkScalar textSize,
Khushal3e7548c2018-05-23 15:45:01 -070065 const SkMatrix& viewMatrix,
66 const Options& options,
Herb Derby6f274892018-12-17 17:30:07 -050067 GrTextBlob* blob,
68 SkPaint* skPaint,
69 SkFont* skFont,
Khushal3e7548c2018-05-23 15:45:01 -070070 SkScalar* textRatio,
71 SkScalerContextFlags* flags);
72
Khushalfa8ff092018-06-06 17:46:38 -070073private:
74 GrTextContext(const Options& options);
75
joshualitt1d89e8d2015-04-01 12:40:54 -070076 // sets up the descriptor on the blob and returns a detached cache. Client must attach
Herb Derbyd8327a82018-01-22 14:39:27 -050077 static SkColor ComputeCanonicalColor(const SkPaint&, bool lcd);
brianosmana1e8f8d2016-04-08 06:47:54 -070078 // Determines if we need to use fake gamma (and contrast boost):
Herb Derbyd8327a82018-01-22 14:39:27 -050079 static SkScalerContextFlags ComputeScalerContextFlags(const GrColorSpaceInfo&);
Herb Derbycddab252018-07-16 11:19:04 -040080
Hal Canary144caf52016-11-07 17:57:18 -050081 const GrDistanceFieldAdjustTable* dfAdjustTable() const { return fDistanceAdjustTable.get(); }
joshualitt79dfb2b2015-05-11 08:58:08 -070082
Hal Canary144caf52016-11-07 17:57:18 -050083 sk_sp<const GrDistanceFieldAdjustTable> fDistanceAdjustTable;
joshualitt1d89e8d2015-04-01 12:40:54 -070084
Khushal3e7548c2018-05-23 15:45:01 -070085 Options fOptions;
Brian Salomonaf597482017-11-07 16:23:34 -050086
Hal Canary6f6961e2017-01-31 13:50:44 -050087#if GR_TEST_UTILS
Herb Derbyd8327a82018-01-22 14:39:27 -050088 static const SkScalerContextFlags kTextBlobOpScalerContextFlags =
89 SkScalerContextFlags::kFakeGammaAndBoostContrast;
Brian Salomon44acb5b2017-07-18 19:59:24 -040090 GR_DRAW_OP_TEST_FRIEND(GrAtlasTextOp);
joshualitt79dfb2b2015-05-11 08:58:08 -070091#endif
joshualitt1d89e8d2015-04-01 12:40:54 -070092};
93
Herb Derby86240592018-05-24 16:12:31 -040094#endif // GrTextContext_DEFINED