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" |
Brian Salomon | 2bbdcc4 | 2017-09-07 12:36:34 -0400 | [diff] [blame] | 11 | #include "SkScalerContext.h" |
cdalton | 855d83f | 2014-09-18 13:51:53 -0700 | [diff] [blame] | 12 | #include "SkGlyph.h" |
| 13 | #include "SkMatrix.h" |
| 14 | #include "SkTypeface.h" |
| 15 | #include "GrPathRange.h" |
| 16 | |
cdalton | 193d9cf | 2016-05-12 11:52:02 -0700 | [diff] [blame] | 17 | const GrUserStencilSettings& GrPathRendering::GetStencilPassSettings(FillType fill) { |
| 18 | switch (fill) { |
| 19 | default: |
Ben Wagner | b4aab9a | 2017-08-16 10:53:04 -0400 | [diff] [blame] | 20 | SK_ABORT("Unexpected path fill."); |
cdalton | 193d9cf | 2016-05-12 11:52:02 -0700 | [diff] [blame] | 21 | 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 | |
cdalton | 855d83f | 2014-09-18 13:51:53 -0700 | [diff] [blame] | 48 | class GlyphGenerator : public GrPathRange::PathGenerator { |
| 49 | public: |
reed | a9322c2 | 2016-04-12 06:47:05 -0700 | [diff] [blame] | 50 | GlyphGenerator(const SkTypeface& typeface, const SkScalerContextEffects& effects, |
| 51 | const SkDescriptor& desc) |
| 52 | : fScalerContext(typeface.createScalerContext(effects, &desc)) |
kkinnunen | 50b58e6 | 2015-05-18 23:02:07 -0700 | [diff] [blame] | 53 | #ifdef SK_DEBUG |
| 54 | , fDesc(desc.copy()) |
| 55 | #endif |
cdalton | 7d5c950 | 2015-10-03 13:28:35 -0700 | [diff] [blame] | 56 | {} |
cdalton | 855d83f | 2014-09-18 13:51:53 -0700 | [diff] [blame] | 57 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 58 | int getNumPaths() override { |
cdalton | 855d83f | 2014-09-18 13:51:53 -0700 | [diff] [blame] | 59 | return fScalerContext->getGlyphCount(); |
| 60 | } |
| 61 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 62 | void generatePath(int glyphID, SkPath* out) override { |
Ben Wagner | 6e9ac12 | 2016-11-11 14:31:06 -0500 | [diff] [blame] | 63 | fScalerContext->getPath(glyphID, out); |
cdalton | 855d83f | 2014-09-18 13:51:53 -0700 | [diff] [blame] | 64 | } |
kkinnunen | 50b58e6 | 2015-05-18 23:02:07 -0700 | [diff] [blame] | 65 | #ifdef SK_DEBUG |
bsalomon | c5d07fa | 2016-05-17 10:17:45 -0700 | [diff] [blame] | 66 | bool isEqualTo(const SkDescriptor& desc) const override { return *fDesc == desc; } |
kkinnunen | 50b58e6 | 2015-05-18 23:02:07 -0700 | [diff] [blame] | 67 | #endif |
cdalton | 855d83f | 2014-09-18 13:51:53 -0700 | [diff] [blame] | 68 | private: |
bungeman | 7cfd46a | 2016-10-20 16:06:52 -0400 | [diff] [blame] | 69 | const std::unique_ptr<SkScalerContext> fScalerContext; |
kkinnunen | 50b58e6 | 2015-05-18 23:02:07 -0700 | [diff] [blame] | 70 | #ifdef SK_DEBUG |
bungeman | 520ced6 | 2016-10-18 08:03:42 -0400 | [diff] [blame] | 71 | const std::unique_ptr<SkDescriptor> fDesc; |
kkinnunen | 50b58e6 | 2015-05-18 23:02:07 -0700 | [diff] [blame] | 72 | #endif |
cdalton | 855d83f | 2014-09-18 13:51:53 -0700 | [diff] [blame] | 73 | }; |
| 74 | |
Robert Phillips | 67d52cf | 2017-06-05 13:38:13 -0400 | [diff] [blame] | 75 | sk_sp<GrPathRange> GrPathRendering::createGlyphs(const SkTypeface* typeface, |
| 76 | const SkScalerContextEffects& effects, |
| 77 | const SkDescriptor* desc, |
| 78 | const GrStyle& style) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 79 | if (nullptr == typeface) { |
cdalton | 855d83f | 2014-09-18 13:51:53 -0700 | [diff] [blame] | 80 | typeface = SkTypeface::GetDefaultTypeface(); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 81 | SkASSERT(nullptr != typeface); |
cdalton | 855d83f | 2014-09-18 13:51:53 -0700 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | if (desc) { |
Hal Canary | 144caf5 | 2016-11-07 17:57:18 -0500 | [diff] [blame] | 85 | sk_sp<GlyphGenerator> generator(new GlyphGenerator(*typeface, effects, *desc)); |
| 86 | return this->createPathRange(generator.get(), style); |
cdalton | 855d83f | 2014-09-18 13:51:53 -0700 | [diff] [blame] | 87 | } |
| 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 Phillips | 646e429 | 2017-06-13 12:44:56 -0400 | [diff] [blame] | 102 | |
reed | a9322c2 | 2016-04-12 06:47:05 -0700 | [diff] [blame] | 103 | // No effects, so we make a dummy struct |
| 104 | SkScalerContextEffects noEffects; |
cdalton | 855d83f | 2014-09-18 13:51:53 -0700 | [diff] [blame] | 105 | |
Hal Canary | 144caf5 | 2016-11-07 17:57:18 -0500 | [diff] [blame] | 106 | sk_sp<GlyphGenerator> generator(new GlyphGenerator(*typeface, noEffects, *genericDesc)); |
| 107 | return this->createPathRange(generator.get(), style); |
cdalton | 855d83f | 2014-09-18 13:51:53 -0700 | [diff] [blame] | 108 | } |
Robert Phillips | 646e429 | 2017-06-13 12:44:56 -0400 | [diff] [blame] | 109 | |
| 110 | void GrPathRendering::stencilPath(const StencilPathArgs& args, const GrPath* path) { |
| 111 | fGpu->handleDirtyContext(); |
| 112 | this->onStencilPath(args, path); |
| 113 | } |
| 114 | |
| 115 | void 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 Phillips | 2890fbf | 2017-07-26 15:48:41 -0400 | [diff] [blame] | 122 | fGpu->xferBarrier(pipeline.renderTarget(), barrierType); |
Robert Phillips | 646e429 | 2017-06-13 12:44:56 -0400 | [diff] [blame] | 123 | } |
| 124 | this->onDrawPath(pipeline, primProc, stencilPassSettings, path); |
| 125 | } |
| 126 | |
| 127 | void 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 Phillips | 2890fbf | 2017-07-26 15:48:41 -0400 | [diff] [blame] | 139 | fGpu->xferBarrier(pipeline.renderTarget(), barrierType); |
Robert Phillips | 646e429 | 2017-06-13 12:44:56 -0400 | [diff] [blame] | 140 | } |
| 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 | } |