blob: f0d5f90ab88559c771fb585d723914186b946f84 [file] [log] [blame]
cdalton855d83f2014-09-18 13:51:53 -07001/*
2 * Copyright 2014 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
Robert Phillips646e4292017-06-13 12:44:56 -04008#include "GrGpu.h"
cdalton855d83f2014-09-18 13:51:53 -07009#include "GrPathRendering.h"
10#include "SkDescriptor.h"
Brian Salomon2bbdcc42017-09-07 12:36:34 -040011#include "SkScalerContext.h"
cdalton855d83f2014-09-18 13:51:53 -070012#include "SkGlyph.h"
13#include "SkMatrix.h"
14#include "SkTypeface.h"
15#include "GrPathRange.h"
16
cdalton193d9cf2016-05-12 11:52:02 -070017const GrUserStencilSettings& GrPathRendering::GetStencilPassSettings(FillType fill) {
18 switch (fill) {
19 default:
Ben Wagnerb4aab9a2017-08-16 10:53:04 -040020 SK_ABORT("Unexpected path fill.");
cdalton193d9cf2016-05-12 11:52:02 -070021 case GrPathRendering::kWinding_FillType: {
22 constexpr static GrUserStencilSettings kWindingStencilPass(
23 GrUserStencilSettings::StaticInit<
24 0xffff,
25 GrUserStencilTest::kAlwaysIfInClip,
26 0xffff,
27 GrUserStencilOp::kIncWrap,
28 GrUserStencilOp::kIncWrap,
29 0xffff>()
30 );
31 return kWindingStencilPass;
32 }
33 case GrPathRendering::kEvenOdd_FillType: {
34 constexpr static GrUserStencilSettings kEvenOddStencilPass(
35 GrUserStencilSettings::StaticInit<
36 0xffff,
37 GrUserStencilTest::kAlwaysIfInClip,
38 0xffff,
39 GrUserStencilOp::kInvert,
40 GrUserStencilOp::kInvert,
41 0xffff>()
42 );
43 return kEvenOddStencilPass;
44 }
45 }
46}
47
cdalton855d83f2014-09-18 13:51:53 -070048class GlyphGenerator : public GrPathRange::PathGenerator {
49public:
reeda9322c22016-04-12 06:47:05 -070050 GlyphGenerator(const SkTypeface& typeface, const SkScalerContextEffects& effects,
51 const SkDescriptor& desc)
52 : fScalerContext(typeface.createScalerContext(effects, &desc))
kkinnunen50b58e62015-05-18 23:02:07 -070053#ifdef SK_DEBUG
54 , fDesc(desc.copy())
55#endif
cdalton7d5c9502015-10-03 13:28:35 -070056 {}
cdalton855d83f2014-09-18 13:51:53 -070057
mtklein36352bf2015-03-25 18:17:31 -070058 int getNumPaths() override {
cdalton855d83f2014-09-18 13:51:53 -070059 return fScalerContext->getGlyphCount();
60 }
61
mtklein36352bf2015-03-25 18:17:31 -070062 void generatePath(int glyphID, SkPath* out) override {
Ben Wagner6e9ac122016-11-11 14:31:06 -050063 fScalerContext->getPath(glyphID, out);
cdalton855d83f2014-09-18 13:51:53 -070064 }
kkinnunen50b58e62015-05-18 23:02:07 -070065#ifdef SK_DEBUG
bsalomonc5d07fa2016-05-17 10:17:45 -070066 bool isEqualTo(const SkDescriptor& desc) const override { return *fDesc == desc; }
kkinnunen50b58e62015-05-18 23:02:07 -070067#endif
cdalton855d83f2014-09-18 13:51:53 -070068private:
bungeman7cfd46a2016-10-20 16:06:52 -040069 const std::unique_ptr<SkScalerContext> fScalerContext;
kkinnunen50b58e62015-05-18 23:02:07 -070070#ifdef SK_DEBUG
bungeman520ced62016-10-18 08:03:42 -040071 const std::unique_ptr<SkDescriptor> fDesc;
kkinnunen50b58e62015-05-18 23:02:07 -070072#endif
cdalton855d83f2014-09-18 13:51:53 -070073};
74
Robert Phillips67d52cf2017-06-05 13:38:13 -040075sk_sp<GrPathRange> GrPathRendering::createGlyphs(const SkTypeface* typeface,
76 const SkScalerContextEffects& effects,
77 const SkDescriptor* desc,
78 const GrStyle& style) {
halcanary96fcdcc2015-08-27 07:41:13 -070079 if (nullptr == typeface) {
cdalton855d83f2014-09-18 13:51:53 -070080 typeface = SkTypeface::GetDefaultTypeface();
halcanary96fcdcc2015-08-27 07:41:13 -070081 SkASSERT(nullptr != typeface);
cdalton855d83f2014-09-18 13:51:53 -070082 }
83
84 if (desc) {
Hal Canary144caf52016-11-07 17:57:18 -050085 sk_sp<GlyphGenerator> generator(new GlyphGenerator(*typeface, effects, *desc));
86 return this->createPathRange(generator.get(), style);
cdalton855d83f2014-09-18 13:51:53 -070087 }
88
89 SkScalerContextRec rec;
90 memset(&rec, 0, sizeof(rec));
91 rec.fFontID = typeface->uniqueID();
92 rec.fTextSize = SkPaint::kCanonicalTextSizeForPaths;
93 rec.fPreScaleX = rec.fPost2x2[0][0] = rec.fPost2x2[1][1] = SK_Scalar1;
94 // Don't bake stroke information into the glyphs, we'll let the GPU do the stroking.
95
96 SkAutoDescriptor ad(sizeof(rec) + SkDescriptor::ComputeOverhead(1));
97 SkDescriptor* genericDesc = ad.getDesc();
98
99 genericDesc->init();
100 genericDesc->addEntry(kRec_SkDescriptorTag, sizeof(rec), &rec);
101 genericDesc->computeChecksum();
Robert Phillips646e4292017-06-13 12:44:56 -0400102
reeda9322c22016-04-12 06:47:05 -0700103 // No effects, so we make a dummy struct
104 SkScalerContextEffects noEffects;
cdalton855d83f2014-09-18 13:51:53 -0700105
Hal Canary144caf52016-11-07 17:57:18 -0500106 sk_sp<GlyphGenerator> generator(new GlyphGenerator(*typeface, noEffects, *genericDesc));
107 return this->createPathRange(generator.get(), style);
cdalton855d83f2014-09-18 13:51:53 -0700108}
Robert Phillips646e4292017-06-13 12:44:56 -0400109
110void GrPathRendering::stencilPath(const StencilPathArgs& args, const GrPath* path) {
111 fGpu->handleDirtyContext();
112 this->onStencilPath(args, path);
113}
114
115void GrPathRendering::drawPath(const GrPipeline& pipeline,
116 const GrPrimitiveProcessor& primProc,
117 // Cover pass settings in pipeline.
118 const GrStencilSettings& stencilPassSettings,
119 const GrPath* path) {
120 fGpu->handleDirtyContext();
121 if (GrXferBarrierType barrierType = pipeline.xferBarrierType(*fGpu->caps())) {
Robert Phillips2890fbf2017-07-26 15:48:41 -0400122 fGpu->xferBarrier(pipeline.renderTarget(), barrierType);
Robert Phillips646e4292017-06-13 12:44:56 -0400123 }
124 this->onDrawPath(pipeline, primProc, stencilPassSettings, path);
125}
126
127void GrPathRendering::drawPaths(const GrPipeline& pipeline,
128 const GrPrimitiveProcessor& primProc,
129 // Cover pass settings in pipeline.
130 const GrStencilSettings& stencilPassSettings,
131 const GrPathRange* pathRange,
132 const void* indices,
133 PathIndexType indexType,
134 const float transformValues[],
135 PathTransformType transformType,
136 int count) {
137 fGpu->handleDirtyContext();
138 if (GrXferBarrierType barrierType = pipeline.xferBarrierType(*fGpu->caps())) {
Robert Phillips2890fbf2017-07-26 15:48:41 -0400139 fGpu->xferBarrier(pipeline.renderTarget(), barrierType);
Robert Phillips646e4292017-06-13 12:44:56 -0400140 }
141#ifdef SK_DEBUG
142 pathRange->assertPathsLoaded(indices, indexType, count);
143#endif
144 this->onDrawPaths(pipeline, primProc, stencilPassSettings, pathRange, indices, indexType,
145 transformValues, transformType, count);
146}