cdalton | 855d83f | 2014-09-18 13:51:53 -0700 | [diff] [blame] | 1 | /* |
| 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 Phillips | 646e429 | 2017-06-13 12:44:56 -0400 | [diff] [blame] | 8 | #include "GrGpu.h" |
cdalton | 855d83f | 2014-09-18 13:51:53 -0700 | [diff] [blame] | 9 | #include "GrPathRendering.h" |
| 10 | #include "SkDescriptor.h" |
| 11 | #include "SkGlyph.h" |
| 12 | #include "SkMatrix.h" |
| 13 | #include "SkTypeface.h" |
| 14 | #include "GrPathRange.h" |
| 15 | |
cdalton | 193d9cf | 2016-05-12 11:52:02 -0700 | [diff] [blame] | 16 | const GrUserStencilSettings& GrPathRendering::GetStencilPassSettings(FillType fill) { |
| 17 | switch (fill) { |
| 18 | default: |
Ben Wagner | b4aab9a | 2017-08-16 10:53:04 -0400 | [diff] [blame^] | 19 | SK_ABORT("Unexpected path fill."); |
cdalton | 193d9cf | 2016-05-12 11:52:02 -0700 | [diff] [blame] | 20 | case GrPathRendering::kWinding_FillType: { |
| 21 | constexpr static GrUserStencilSettings kWindingStencilPass( |
| 22 | GrUserStencilSettings::StaticInit< |
| 23 | 0xffff, |
| 24 | GrUserStencilTest::kAlwaysIfInClip, |
| 25 | 0xffff, |
| 26 | GrUserStencilOp::kIncWrap, |
| 27 | GrUserStencilOp::kIncWrap, |
| 28 | 0xffff>() |
| 29 | ); |
| 30 | return kWindingStencilPass; |
| 31 | } |
| 32 | case GrPathRendering::kEvenOdd_FillType: { |
| 33 | constexpr static GrUserStencilSettings kEvenOddStencilPass( |
| 34 | GrUserStencilSettings::StaticInit< |
| 35 | 0xffff, |
| 36 | GrUserStencilTest::kAlwaysIfInClip, |
| 37 | 0xffff, |
| 38 | GrUserStencilOp::kInvert, |
| 39 | GrUserStencilOp::kInvert, |
| 40 | 0xffff>() |
| 41 | ); |
| 42 | return kEvenOddStencilPass; |
| 43 | } |
| 44 | } |
| 45 | } |
| 46 | |
cdalton | 855d83f | 2014-09-18 13:51:53 -0700 | [diff] [blame] | 47 | class GlyphGenerator : public GrPathRange::PathGenerator { |
| 48 | public: |
reed | a9322c2 | 2016-04-12 06:47:05 -0700 | [diff] [blame] | 49 | GlyphGenerator(const SkTypeface& typeface, const SkScalerContextEffects& effects, |
| 50 | const SkDescriptor& desc) |
| 51 | : fScalerContext(typeface.createScalerContext(effects, &desc)) |
kkinnunen | 50b58e6 | 2015-05-18 23:02:07 -0700 | [diff] [blame] | 52 | #ifdef SK_DEBUG |
| 53 | , fDesc(desc.copy()) |
| 54 | #endif |
cdalton | 7d5c950 | 2015-10-03 13:28:35 -0700 | [diff] [blame] | 55 | {} |
cdalton | 855d83f | 2014-09-18 13:51:53 -0700 | [diff] [blame] | 56 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 57 | int getNumPaths() override { |
cdalton | 855d83f | 2014-09-18 13:51:53 -0700 | [diff] [blame] | 58 | return fScalerContext->getGlyphCount(); |
| 59 | } |
| 60 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 61 | void generatePath(int glyphID, SkPath* out) override { |
Ben Wagner | 6e9ac12 | 2016-11-11 14:31:06 -0500 | [diff] [blame] | 62 | fScalerContext->getPath(glyphID, out); |
cdalton | 855d83f | 2014-09-18 13:51:53 -0700 | [diff] [blame] | 63 | } |
kkinnunen | 50b58e6 | 2015-05-18 23:02:07 -0700 | [diff] [blame] | 64 | #ifdef SK_DEBUG |
bsalomon | c5d07fa | 2016-05-17 10:17:45 -0700 | [diff] [blame] | 65 | bool isEqualTo(const SkDescriptor& desc) const override { return *fDesc == desc; } |
kkinnunen | 50b58e6 | 2015-05-18 23:02:07 -0700 | [diff] [blame] | 66 | #endif |
cdalton | 855d83f | 2014-09-18 13:51:53 -0700 | [diff] [blame] | 67 | private: |
bungeman | 7cfd46a | 2016-10-20 16:06:52 -0400 | [diff] [blame] | 68 | const std::unique_ptr<SkScalerContext> fScalerContext; |
kkinnunen | 50b58e6 | 2015-05-18 23:02:07 -0700 | [diff] [blame] | 69 | #ifdef SK_DEBUG |
bungeman | 520ced6 | 2016-10-18 08:03:42 -0400 | [diff] [blame] | 70 | const std::unique_ptr<SkDescriptor> fDesc; |
kkinnunen | 50b58e6 | 2015-05-18 23:02:07 -0700 | [diff] [blame] | 71 | #endif |
cdalton | 855d83f | 2014-09-18 13:51:53 -0700 | [diff] [blame] | 72 | }; |
| 73 | |
Robert Phillips | 67d52cf | 2017-06-05 13:38:13 -0400 | [diff] [blame] | 74 | sk_sp<GrPathRange> GrPathRendering::createGlyphs(const SkTypeface* typeface, |
| 75 | const SkScalerContextEffects& effects, |
| 76 | const SkDescriptor* desc, |
| 77 | const GrStyle& style) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 78 | if (nullptr == typeface) { |
cdalton | 855d83f | 2014-09-18 13:51:53 -0700 | [diff] [blame] | 79 | typeface = SkTypeface::GetDefaultTypeface(); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 80 | SkASSERT(nullptr != typeface); |
cdalton | 855d83f | 2014-09-18 13:51:53 -0700 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | if (desc) { |
Hal Canary | 144caf5 | 2016-11-07 17:57:18 -0500 | [diff] [blame] | 84 | sk_sp<GlyphGenerator> generator(new GlyphGenerator(*typeface, effects, *desc)); |
| 85 | return this->createPathRange(generator.get(), style); |
cdalton | 855d83f | 2014-09-18 13:51:53 -0700 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | SkScalerContextRec rec; |
| 89 | memset(&rec, 0, sizeof(rec)); |
| 90 | rec.fFontID = typeface->uniqueID(); |
| 91 | rec.fTextSize = SkPaint::kCanonicalTextSizeForPaths; |
| 92 | rec.fPreScaleX = rec.fPost2x2[0][0] = rec.fPost2x2[1][1] = SK_Scalar1; |
| 93 | // Don't bake stroke information into the glyphs, we'll let the GPU do the stroking. |
| 94 | |
| 95 | SkAutoDescriptor ad(sizeof(rec) + SkDescriptor::ComputeOverhead(1)); |
| 96 | SkDescriptor* genericDesc = ad.getDesc(); |
| 97 | |
| 98 | genericDesc->init(); |
| 99 | genericDesc->addEntry(kRec_SkDescriptorTag, sizeof(rec), &rec); |
| 100 | genericDesc->computeChecksum(); |
Robert Phillips | 646e429 | 2017-06-13 12:44:56 -0400 | [diff] [blame] | 101 | |
reed | a9322c2 | 2016-04-12 06:47:05 -0700 | [diff] [blame] | 102 | // No effects, so we make a dummy struct |
| 103 | SkScalerContextEffects noEffects; |
cdalton | 855d83f | 2014-09-18 13:51:53 -0700 | [diff] [blame] | 104 | |
Hal Canary | 144caf5 | 2016-11-07 17:57:18 -0500 | [diff] [blame] | 105 | sk_sp<GlyphGenerator> generator(new GlyphGenerator(*typeface, noEffects, *genericDesc)); |
| 106 | return this->createPathRange(generator.get(), style); |
cdalton | 855d83f | 2014-09-18 13:51:53 -0700 | [diff] [blame] | 107 | } |
Robert Phillips | 646e429 | 2017-06-13 12:44:56 -0400 | [diff] [blame] | 108 | |
| 109 | void GrPathRendering::stencilPath(const StencilPathArgs& args, const GrPath* path) { |
| 110 | fGpu->handleDirtyContext(); |
| 111 | this->onStencilPath(args, path); |
| 112 | } |
| 113 | |
| 114 | void GrPathRendering::drawPath(const GrPipeline& pipeline, |
| 115 | const GrPrimitiveProcessor& primProc, |
| 116 | // Cover pass settings in pipeline. |
| 117 | const GrStencilSettings& stencilPassSettings, |
| 118 | const GrPath* path) { |
| 119 | fGpu->handleDirtyContext(); |
| 120 | if (GrXferBarrierType barrierType = pipeline.xferBarrierType(*fGpu->caps())) { |
Robert Phillips | 2890fbf | 2017-07-26 15:48:41 -0400 | [diff] [blame] | 121 | fGpu->xferBarrier(pipeline.renderTarget(), barrierType); |
Robert Phillips | 646e429 | 2017-06-13 12:44:56 -0400 | [diff] [blame] | 122 | } |
| 123 | this->onDrawPath(pipeline, primProc, stencilPassSettings, path); |
| 124 | } |
| 125 | |
| 126 | void GrPathRendering::drawPaths(const GrPipeline& pipeline, |
| 127 | const GrPrimitiveProcessor& primProc, |
| 128 | // Cover pass settings in pipeline. |
| 129 | const GrStencilSettings& stencilPassSettings, |
| 130 | const GrPathRange* pathRange, |
| 131 | const void* indices, |
| 132 | PathIndexType indexType, |
| 133 | const float transformValues[], |
| 134 | PathTransformType transformType, |
| 135 | int count) { |
| 136 | fGpu->handleDirtyContext(); |
| 137 | if (GrXferBarrierType barrierType = pipeline.xferBarrierType(*fGpu->caps())) { |
Robert Phillips | 2890fbf | 2017-07-26 15:48:41 -0400 | [diff] [blame] | 138 | fGpu->xferBarrier(pipeline.renderTarget(), barrierType); |
Robert Phillips | 646e429 | 2017-06-13 12:44:56 -0400 | [diff] [blame] | 139 | } |
| 140 | #ifdef SK_DEBUG |
| 141 | pathRange->assertPathsLoaded(indices, indexType, count); |
| 142 | #endif |
| 143 | this->onDrawPaths(pipeline, primProc, stencilPassSettings, pathRange, indices, indexType, |
| 144 | transformValues, transformType, count); |
| 145 | } |