blob: 7d31d84a054c688041be8c2031d86436a2ab6e08 [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
Herb Derby86240592018-05-24 16:12:31 -040011#include "GrTextBlob.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 drawPosText(GrContext*, GrTextUtils::Target*, const GrClip&, const SkPaint&,
Brian Salomon82f44312017-01-11 13:42:54 -050048 const SkMatrix& viewMatrix, const SkSurfaceProps&, const char text[],
49 size_t byteLength, const SkScalar pos[], int scalarsPerPosition,
joshualitt8e84a1e2016-02-16 11:09:25 -080050 const SkPoint& offset, const SkIRect& regionClipBounds);
Brian Salomonf18b1d82017-10-27 11:30:49 -040051 void drawTextBlob(GrContext*, GrTextUtils::Target*, const GrClip&, const SkPaint&,
joshualitt2c89bc12016-02-11 05:42:30 -080052 const SkMatrix& viewMatrix, const SkSurfaceProps&, const SkTextBlob*,
Brian Salomonf18b1d82017-10-27 11:30:49 -040053 SkScalar x, SkScalar y, SkDrawFilter*, const SkIRect& clipBounds);
joshualitt1d89e8d2015-04-01 12:40:54 -070054
Herb Derby26cbe512018-05-24 14:39:01 -040055 std::unique_ptr<GrDrawOp> createOp_TestingOnly(GrContext*, GrTextContext*,
Robert Phillipsd2e9f762018-03-07 11:54:37 -050056 GrRenderTargetContext*, const SkPaint&,
57 const SkMatrix& viewMatrix, const char* text,
58 int x, int y);
59
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
joshualitte55750e2016-02-10 12:52:21 -080072private:
Herb Derby26cbe512018-05-24 14:39:01 -040073 GrTextContext(const Options& options);
joshualitte55750e2016-02-10 12:52:21 -080074
Jim Van Verthc401bb92018-02-15 14:05:24 -050075 class FallbackTextHelper {
76 public:
77 FallbackTextHelper(const SkMatrix& viewMatrix,
Jim Van Verth080a9282018-03-02 10:41:43 -050078 const SkPaint& pathPaint,
79 const GrGlyphCache* glyphCache,
Jim Van Verthc401bb92018-02-15 14:05:24 -050080 SkScalar textRatio)
81 : fViewMatrix(viewMatrix)
Jim Van Verth080a9282018-03-02 10:41:43 -050082 , fTextSize(pathPaint.getTextSize())
83 , fMaxTextSize(glyphCache->getGlyphSizeLimit())
Jim Van Verthc401bb92018-02-15 14:05:24 -050084 , fTextRatio(textRatio)
Jim Van Verthb515ae72018-05-23 16:44:55 -040085 , fTransformedFallbackTextSize(fMaxTextSize)
86 , fUseTransformedFallback(false) {
Jim Van Verthc401bb92018-02-15 14:05:24 -050087 fMaxScale = viewMatrix.getMaxScale();
88 }
89
90 void appendText(const SkGlyph& glyph, int count, const char* text, SkPoint glyphPos);
Herb Derby86240592018-05-24 16:12:31 -040091 void drawText(GrTextBlob* blob, int runIndex, GrGlyphCache*, const SkSurfaceProps&,
Robert Phillipsc4039ea2018-03-01 11:36:45 -050092 const GrTextUtils::Paint&, SkScalerContextFlags);
Jim Van Verthc401bb92018-02-15 14:05:24 -050093
94 private:
95 SkTDArray<char> fFallbackTxt;
96 SkTDArray<SkPoint> fFallbackPos;
97
98 const SkMatrix& fViewMatrix;
Jim Van Verthc401bb92018-02-15 14:05:24 -050099 SkScalar fTextSize;
100 SkScalar fMaxTextSize;
Jim Van Verth080a9282018-03-02 10:41:43 -0500101 SkScalar fTextRatio;
Jim Van Verthb515ae72018-05-23 16:44:55 -0400102 SkScalar fTransformedFallbackTextSize;
Jim Van Verthc401bb92018-02-15 14:05:24 -0500103 SkScalar fMaxScale;
Jim Van Verthb515ae72018-05-23 16:44:55 -0400104 bool fUseTransformedFallback;
Jim Van Verthc401bb92018-02-15 14:05:24 -0500105 };
106
joshualitt1d89e8d2015-04-01 12:40:54 -0700107 // sets up the descriptor on the blob and returns a detached cache. Client must attach
Herb Derbyd8327a82018-01-22 14:39:27 -0500108 static SkColor ComputeCanonicalColor(const SkPaint&, bool lcd);
brianosmana1e8f8d2016-04-08 06:47:54 -0700109 // Determines if we need to use fake gamma (and contrast boost):
Herb Derbyd8327a82018-01-22 14:39:27 -0500110 static SkScalerContextFlags ComputeScalerContextFlags(const GrColorSpaceInfo&);
Herb Derby86240592018-05-24 16:12:31 -0400111 void regenerateTextBlob(GrTextBlob* bmp,
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500112 GrGlyphCache*,
Brian Salomonaf597482017-11-07 16:23:34 -0500113 const GrShaderCaps&,
114 const GrTextUtils::Paint&,
Herb Derbyd8327a82018-01-22 14:39:27 -0500115 SkScalerContextFlags scalerContextFlags,
Brian Salomonaf597482017-11-07 16:23:34 -0500116 const SkMatrix& viewMatrix,
117 const SkSurfaceProps&,
118 const SkTextBlob* blob, SkScalar x, SkScalar y,
119 SkDrawFilter* drawFilter) const;
120
Herb Derbyd8327a82018-01-22 14:39:27 -0500121 static bool HasLCD(const SkTextBlob*);
joshualitt9bd2daf2015-04-17 09:30:06 -0700122
Herb Derby86240592018-05-24 16:12:31 -0400123 sk_sp<GrTextBlob> makeDrawPosTextBlob(GrTextBlobCache*, GrGlyphCache*,
Herb Derbyd8327a82018-01-22 14:39:27 -0500124 const GrShaderCaps&,
125 const GrTextUtils::Paint&,
126 SkScalerContextFlags scalerContextFlags,
127 const SkMatrix& viewMatrix,
128 const SkSurfaceProps&,
129 const char text[], size_t byteLength,
130 const SkScalar pos[],
131 int scalarsPerPosition,
132 const SkPoint& offset) const;
Brian Salomon52db9402017-11-07 14:58:55 -0500133
Herb Derby86240592018-05-24 16:12:31 -0400134 // Functions for appending BMP text to GrTextBlob
Herb Derby86240592018-05-24 16:12:31 -0400135 static void DrawBmpPosText(GrTextBlob*, int runIndex, GrGlyphCache*,
Brian Salomon52db9402017-11-07 14:58:55 -0500136 const SkSurfaceProps&, const GrTextUtils::Paint& paint,
Herb Derbyd8327a82018-01-22 14:39:27 -0500137 SkScalerContextFlags scalerContextFlags, const SkMatrix& viewMatrix,
Brian Salomon52db9402017-11-07 14:58:55 -0500138 const char text[], size_t byteLength, const SkScalar pos[],
Jim Van Verthc401bb92018-02-15 14:05:24 -0500139 int scalarsPerPosition, const SkPoint& offset);
Brian Salomon52db9402017-11-07 14:58:55 -0500140
Herb Derby86240592018-05-24 16:12:31 -0400141 static void DrawBmpPosTextAsPaths(GrTextBlob*, int runIndex, GrGlyphCache*,
Jim Van Verthc401bb92018-02-15 14:05:24 -0500142 const SkSurfaceProps&, const GrTextUtils::Paint& paint,
143 SkScalerContextFlags scalerContextFlags,
144 const SkMatrix& viewMatrix,
Jim Van Verth54d9c882018-02-08 16:14:48 -0500145 const char text[], size_t byteLength,
146 const SkScalar pos[], int scalarsPerPosition,
Jim Van Verthc401bb92018-02-15 14:05:24 -0500147 const SkPoint& offset);
Jim Van Verth54d9c882018-02-08 16:14:48 -0500148
Brian Salomon52db9402017-11-07 14:58:55 -0500149 // functions for appending distance field text
Herb Derby86240592018-05-24 16:12:31 -0400150 void drawDFPosText(GrTextBlob* blob, int runIndex, GrGlyphCache*,
Brian Salomonaf597482017-11-07 16:23:34 -0500151 const SkSurfaceProps&, const GrTextUtils::Paint& paint,
Herb Derbyd8327a82018-01-22 14:39:27 -0500152 SkScalerContextFlags scalerContextFlags,
153 const SkMatrix& viewMatrix, const char text[],
Brian Salomonaf597482017-11-07 16:23:34 -0500154 size_t byteLength, const SkScalar pos[], int scalarsPerPosition,
155 const SkPoint& offset) const;
Brian Salomon52db9402017-11-07 14:58:55 -0500156
Herb Derby86240592018-05-24 16:12:31 -0400157 static void BmpAppendGlyph(GrTextBlob*, int runIndex, GrGlyphCache*,
Robert Phillipscaf1ebb2018-03-01 14:28:44 -0500158 sk_sp<GrTextStrike>*, const SkGlyph&, SkScalar sx, SkScalar sy,
Jim Van Verthb515ae72018-05-23 16:44:55 -0400159 GrColor color, SkGlyphCache*, SkScalar textRatio, bool needsXform);
Brian Salomon52db9402017-11-07 14:58:55 -0500160
Herb Derby86240592018-05-24 16:12:31 -0400161 static void DfAppendGlyph(GrTextBlob*, int runIndex, GrGlyphCache*,
Robert Phillipscaf1ebb2018-03-01 14:28:44 -0500162 sk_sp<GrTextStrike>*, const SkGlyph&, SkScalar sx, SkScalar sy,
Jim Van Verthf4c13162018-01-11 16:40:24 -0500163 GrColor color, SkGlyphCache* cache, SkScalar textRatio);
Brian Salomon52db9402017-11-07 14:58:55 -0500164
Hal Canary144caf52016-11-07 17:57:18 -0500165 const GrDistanceFieldAdjustTable* dfAdjustTable() const { return fDistanceAdjustTable.get(); }
joshualitt79dfb2b2015-05-11 08:58:08 -0700166
Hal Canary144caf52016-11-07 17:57:18 -0500167 sk_sp<const GrDistanceFieldAdjustTable> fDistanceAdjustTable;
joshualitt1d89e8d2015-04-01 12:40:54 -0700168
Khushal3e7548c2018-05-23 15:45:01 -0700169 Options fOptions;
Brian Salomonaf597482017-11-07 16:23:34 -0500170
Hal Canary6f6961e2017-01-31 13:50:44 -0500171#if GR_TEST_UTILS
Herb Derbyd8327a82018-01-22 14:39:27 -0500172 static const SkScalerContextFlags kTextBlobOpScalerContextFlags =
173 SkScalerContextFlags::kFakeGammaAndBoostContrast;
Brian Salomon44acb5b2017-07-18 19:59:24 -0400174 GR_DRAW_OP_TEST_FRIEND(GrAtlasTextOp);
joshualitt79dfb2b2015-05-11 08:58:08 -0700175#endif
joshualitt1d89e8d2015-04-01 12:40:54 -0700176};
177
Herb Derby86240592018-05-24 16:12:31 -0400178#endif // GrTextContext_DEFINED