aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/gpu/text/GrTextContext.h
blob: 7c8c5760413940e120ac83f2c484e46e58a8b86d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/*
 * Copyright 2015 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#ifndef GrTextContext_DEFINED
#define GrTextContext_DEFINED

#include "GrTextBlob.h"
#include "GrDistanceFieldAdjustTable.h"
#include "GrGeometryProcessor.h"
#include "GrTextUtils.h"

#if GR_TEST_UTILS
#include "GrDrawOpTest.h"
#endif

class GrDrawOp;
class GrTextBlobCache;
class SkGlyph;

/*
 * Renders text using some kind of an atlas, ie BitmapText or DistanceField text
 */
class GrTextContext {
public:
    struct Options {
        /**
         * Below this size (in device space) distance field text will not be used. Negative means
         * use a default value.
         */
        SkScalar fMinDistanceFieldFontSize = -1.f;
        /**
         * Above this size (in device space) distance field text will not be used and glyphs will
         * be rendered from outline as individual paths. Negative means use a default value.
         */
        SkScalar fMaxDistanceFieldFontSize = -1.f;
        /** Forces all distance field vertices to use 3 components, not just when in perspective. */
        bool fDistanceFieldVerticesAlwaysHaveW = false;
    };

    static std::unique_ptr<GrTextContext> Make(const Options& options);

    void drawPosText(GrContext*, GrTextUtils::Target*, const GrClip&, const SkPaint&,
                     const SkMatrix& viewMatrix, const SkSurfaceProps&, const char text[],
                     size_t byteLength, const SkScalar pos[], int scalarsPerPosition,
                     const SkPoint& offset, const SkIRect& regionClipBounds);
    void drawGlyphRunList(GrContext*, GrTextUtils::Target*, const GrClip&,
                          const SkMatrix& viewMatrix, const SkSurfaceProps&, const SkGlyphRunList&,
                          const SkIRect& clipBounds);

    std::unique_ptr<GrDrawOp> createOp_TestingOnly(GrContext*,
                                                   GrTextContext*,
                                                   GrRenderTargetContext*,
                                                   const SkPaint&,
                                                   const SkMatrix& viewMatrix,
                                                   const char* text,
                                                   int x,
                                                   int y);

    static void SanitizeOptions(Options* options);
    static bool CanDrawAsDistanceFields(const SkPaint& skPaint, const SkMatrix& viewMatrix,
                                        const SkSurfaceProps& props,
                                        bool contextSupportsDistanceFieldText,
                                        const Options& options);
    static void InitDistanceFieldPaint(GrTextBlob* blob,
                                       SkPaint* skPaint,
                                       const SkMatrix& viewMatrix,
                                       const Options& options,
                                       SkScalar* textRatio,
                                       SkScalerContextFlags* flags);

    class FallbackTextHelper {
    public:
        FallbackTextHelper(const SkMatrix& viewMatrix,
                           const SkPaint& pathPaint,
                           SkScalar maxTextSize,
                           SkScalar textRatio)
                : fViewMatrix(viewMatrix)
                , fTextSize(pathPaint.getTextSize())
                , fMaxTextSize(maxTextSize)
                , fTextRatio(textRatio)
                , fTransformedFallbackTextSize(fMaxTextSize)
                , fUseTransformedFallback(false) {
            fMaxScale = viewMatrix.getMaxScale();
        }

        void appendText(const SkGlyph& glyph, int count, const char* text, SkPoint glyphPos);
        void drawText(GrTextBlob* blob, int runIndex, GrGlyphCache*, const SkSurfaceProps&,
                      const GrTextUtils::Paint&, SkScalerContextFlags);

        void initializeForDraw(SkPaint* paint, SkScalar* textRatio, SkMatrix* matrix) const;
        const SkTDArray<char>& fallbackText() const { return fFallbackTxt; }

    private:
        SkTDArray<char> fFallbackTxt;
        SkTDArray<SkPoint> fFallbackPos;

        const SkMatrix& fViewMatrix;
        SkScalar fTextSize;
        SkScalar fMaxTextSize;
        SkScalar fTextRatio;
        SkScalar fTransformedFallbackTextSize;
        SkScalar fMaxScale;
        bool fUseTransformedFallback;
    };

    class FallbackGlyphRunHelper {
    public:
        FallbackGlyphRunHelper(const SkMatrix& viewMatrix,
                           const SkPaint& pathPaint,
                           SkScalar maxTextSize,
                           SkScalar textRatio)
                : fViewMatrix(viewMatrix)
                , fTextSize(pathPaint.getTextSize())
                , fMaxTextSize(maxTextSize)
                , fTextRatio(textRatio)
                , fTransformedFallbackTextSize(fMaxTextSize)
                , fUseTransformedFallback(false) {
            fMaxScale = viewMatrix.getMaxScale();
        }

        void appendText(const SkGlyph& glyph, SkGlyphID, SkPoint glyphPos);
        void drawText(GrTextBlob* blob, int runIndex, GrGlyphCache*, const SkSurfaceProps&,
                      const GrTextUtils::Paint&, SkScalerContextFlags);

        void initializeForDraw(SkPaint* paint, SkScalar* textRatio, SkMatrix* matrix) const;
        const std::vector<SkGlyphID>& fallbackText() const { return fFallbackTxt; }

    private:
        std::vector<SkGlyphID> fFallbackTxt;
        std::vector<SkPoint> fFallbackPos;

        const SkMatrix& fViewMatrix;
        SkScalar fTextSize;
        SkScalar fMaxTextSize;
        SkScalar fTextRatio;
        SkScalar fTransformedFallbackTextSize;
        SkScalar fMaxScale;
        bool fUseTransformedFallback;
    };

private:
    GrTextContext(const Options& options);

    // sets up the descriptor on the blob and returns a detached cache.  Client must attach
    static SkColor ComputeCanonicalColor(const SkPaint&, bool lcd);
    // Determines if we need to use fake gamma (and contrast boost):
    static SkScalerContextFlags ComputeScalerContextFlags(const GrColorSpaceInfo&);

    void regenerateGlyphRunList(GrTextBlob* bmp,
                            GrGlyphCache*,
                            const GrShaderCaps&,
                            const GrTextUtils::Paint&,
                            SkScalerContextFlags scalerContextFlags,
                            const SkMatrix& viewMatrix,
                            const SkSurfaceProps&,
                            const SkGlyphRunList& glyphRunList) const;

    sk_sp<GrTextBlob> makeDrawPosTextBlob(GrTextBlobCache*, GrGlyphCache*,
                                               const GrShaderCaps&,
                                               const GrTextUtils::Paint&,
                                               SkScalerContextFlags scalerContextFlags,
                                               const SkMatrix& viewMatrix,
                                               const SkSurfaceProps&,
                                               const char text[], size_t byteLength,
                                               const SkScalar pos[],
                                               int scalarsPerPosition,
                                               const SkPoint& offset) const;

    // Functions for appending BMP text to GrTextBlob
    static void DrawBmpPosText(GrTextBlob*, int runIndex, GrGlyphCache*,
                               const SkSurfaceProps&, const GrTextUtils::Paint& paint,
                               SkScalerContextFlags scalerContextFlags, const SkMatrix& viewMatrix,
                               const char text[], size_t byteLength, const SkScalar pos[],
                               int scalarsPerPosition, const SkPoint& offset);

    static void DrawBmpPosTextAsPaths(GrTextBlob*, int runIndex, GrGlyphCache*,
                                      const SkSurfaceProps&, const GrTextUtils::Paint& paint,
                                      SkScalerContextFlags scalerContextFlags,
                                      const SkMatrix& viewMatrix,
                                      const char text[], size_t byteLength,
                                      const SkScalar pos[], int scalarsPerPosition,
                                      const SkPoint& offset);

    // functions for appending distance field text
    void drawDFPosText(GrTextBlob* blob, int runIndex, GrGlyphCache*,
                       const SkSurfaceProps&, const GrTextUtils::Paint& paint,
                       SkScalerContextFlags scalerContextFlags,
                       const SkMatrix& viewMatrix, const char text[],
                       size_t byteLength, const SkScalar pos[], int scalarsPerPosition,
                       const SkPoint& offset) const;

    void drawDFGlyphRun(GrTextBlob* blob, int runIndex, GrGlyphCache*,
                       const SkSurfaceProps&, const GrTextUtils::Paint& paint,
                       SkScalerContextFlags scalerContextFlags,
                       const SkMatrix& viewMatrix, const SkGlyphRun& glyphRun,
                       const SkPoint& offset) const;

    static void BmpAppendGlyph(GrTextBlob*, int runIndex, GrGlyphCache*,
                               sk_sp<GrTextStrike>*, const SkGlyph&, SkScalar sx, SkScalar sy,
                               GrColor color, SkGlyphCache*, SkScalar textRatio, bool needsXform);

    static void DfAppendGlyph(GrTextBlob*, int runIndex, GrGlyphCache*,
                              sk_sp<GrTextStrike>*, const SkGlyph&, SkScalar sx, SkScalar sy,
                              GrColor color, SkGlyphCache* cache, SkScalar textRatio);

    const GrDistanceFieldAdjustTable* dfAdjustTable() const { return fDistanceAdjustTable.get(); }

    sk_sp<const GrDistanceFieldAdjustTable> fDistanceAdjustTable;

    Options fOptions;

#if GR_TEST_UTILS
    static const SkScalerContextFlags kTextBlobOpScalerContextFlags =
            SkScalerContextFlags::kFakeGammaAndBoostContrast;
    GR_DRAW_OP_TEST_FRIEND(GrAtlasTextOp);
#endif
};

#endif  // GrTextContext_DEFINED