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 | |
| 8 | #include "GrPathRendering.h" |
| 9 | #include "SkDescriptor.h" |
| 10 | #include "SkGlyph.h" |
| 11 | #include "SkMatrix.h" |
| 12 | #include "SkTypeface.h" |
| 13 | #include "GrPathRange.h" |
| 14 | |
cdalton | 193d9cf | 2016-05-12 11:52:02 -0700 | [diff] [blame] | 15 | const GrUserStencilSettings& GrPathRendering::GetStencilPassSettings(FillType fill) { |
| 16 | switch (fill) { |
| 17 | default: |
| 18 | SkFAIL("Unexpected path fill."); |
| 19 | case GrPathRendering::kWinding_FillType: { |
| 20 | constexpr static GrUserStencilSettings kWindingStencilPass( |
| 21 | GrUserStencilSettings::StaticInit< |
| 22 | 0xffff, |
| 23 | GrUserStencilTest::kAlwaysIfInClip, |
| 24 | 0xffff, |
| 25 | GrUserStencilOp::kIncWrap, |
| 26 | GrUserStencilOp::kIncWrap, |
| 27 | 0xffff>() |
| 28 | ); |
| 29 | return kWindingStencilPass; |
| 30 | } |
| 31 | case GrPathRendering::kEvenOdd_FillType: { |
| 32 | constexpr static GrUserStencilSettings kEvenOddStencilPass( |
| 33 | GrUserStencilSettings::StaticInit< |
| 34 | 0xffff, |
| 35 | GrUserStencilTest::kAlwaysIfInClip, |
| 36 | 0xffff, |
| 37 | GrUserStencilOp::kInvert, |
| 38 | GrUserStencilOp::kInvert, |
| 39 | 0xffff>() |
| 40 | ); |
| 41 | return kEvenOddStencilPass; |
| 42 | } |
| 43 | } |
| 44 | } |
| 45 | |
cdalton | 855d83f | 2014-09-18 13:51:53 -0700 | [diff] [blame] | 46 | class GlyphGenerator : public GrPathRange::PathGenerator { |
| 47 | public: |
reed | a9322c2 | 2016-04-12 06:47:05 -0700 | [diff] [blame] | 48 | GlyphGenerator(const SkTypeface& typeface, const SkScalerContextEffects& effects, |
| 49 | const SkDescriptor& desc) |
| 50 | : fScalerContext(typeface.createScalerContext(effects, &desc)) |
kkinnunen | 50b58e6 | 2015-05-18 23:02:07 -0700 | [diff] [blame] | 51 | #ifdef SK_DEBUG |
| 52 | , fDesc(desc.copy()) |
| 53 | #endif |
cdalton | 7d5c950 | 2015-10-03 13:28:35 -0700 | [diff] [blame] | 54 | {} |
cdalton | 855d83f | 2014-09-18 13:51:53 -0700 | [diff] [blame] | 55 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 56 | int getNumPaths() override { |
cdalton | 855d83f | 2014-09-18 13:51:53 -0700 | [diff] [blame] | 57 | return fScalerContext->getGlyphCount(); |
| 58 | } |
| 59 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 60 | void generatePath(int glyphID, SkPath* out) override { |
Ben Wagner | 6e9ac12 | 2016-11-11 14:31:06 -0500 | [diff] [blame] | 61 | fScalerContext->getPath(glyphID, out); |
cdalton | 855d83f | 2014-09-18 13:51:53 -0700 | [diff] [blame] | 62 | } |
kkinnunen | 50b58e6 | 2015-05-18 23:02:07 -0700 | [diff] [blame] | 63 | #ifdef SK_DEBUG |
bsalomon | c5d07fa | 2016-05-17 10:17:45 -0700 | [diff] [blame] | 64 | bool isEqualTo(const SkDescriptor& desc) const override { return *fDesc == desc; } |
kkinnunen | 50b58e6 | 2015-05-18 23:02:07 -0700 | [diff] [blame] | 65 | #endif |
cdalton | 855d83f | 2014-09-18 13:51:53 -0700 | [diff] [blame] | 66 | private: |
bungeman | 7cfd46a | 2016-10-20 16:06:52 -0400 | [diff] [blame] | 67 | const std::unique_ptr<SkScalerContext> fScalerContext; |
kkinnunen | 50b58e6 | 2015-05-18 23:02:07 -0700 | [diff] [blame] | 68 | #ifdef SK_DEBUG |
bungeman | 520ced6 | 2016-10-18 08:03:42 -0400 | [diff] [blame] | 69 | const std::unique_ptr<SkDescriptor> fDesc; |
kkinnunen | 50b58e6 | 2015-05-18 23:02:07 -0700 | [diff] [blame] | 70 | #endif |
cdalton | 855d83f | 2014-09-18 13:51:53 -0700 | [diff] [blame] | 71 | }; |
| 72 | |
| 73 | GrPathRange* GrPathRendering::createGlyphs(const SkTypeface* typeface, |
reed | a9322c2 | 2016-04-12 06:47:05 -0700 | [diff] [blame] | 74 | const SkScalerContextEffects& effects, |
cdalton | 855d83f | 2014-09-18 13:51:53 -0700 | [diff] [blame] | 75 | const SkDescriptor* desc, |
bsalomon | 6663acf | 2016-05-10 09:14:17 -0700 | [diff] [blame] | 76 | const GrStyle& style) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 77 | if (nullptr == typeface) { |
cdalton | 855d83f | 2014-09-18 13:51:53 -0700 | [diff] [blame] | 78 | typeface = SkTypeface::GetDefaultTypeface(); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 79 | SkASSERT(nullptr != typeface); |
cdalton | 855d83f | 2014-09-18 13:51:53 -0700 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | if (desc) { |
Hal Canary | 144caf5 | 2016-11-07 17:57:18 -0500 | [diff] [blame] | 83 | sk_sp<GlyphGenerator> generator(new GlyphGenerator(*typeface, effects, *desc)); |
| 84 | return this->createPathRange(generator.get(), style); |
cdalton | 855d83f | 2014-09-18 13:51:53 -0700 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | SkScalerContextRec rec; |
| 88 | memset(&rec, 0, sizeof(rec)); |
| 89 | rec.fFontID = typeface->uniqueID(); |
| 90 | rec.fTextSize = SkPaint::kCanonicalTextSizeForPaths; |
| 91 | rec.fPreScaleX = rec.fPost2x2[0][0] = rec.fPost2x2[1][1] = SK_Scalar1; |
| 92 | // Don't bake stroke information into the glyphs, we'll let the GPU do the stroking. |
| 93 | |
| 94 | SkAutoDescriptor ad(sizeof(rec) + SkDescriptor::ComputeOverhead(1)); |
| 95 | SkDescriptor* genericDesc = ad.getDesc(); |
| 96 | |
| 97 | genericDesc->init(); |
| 98 | genericDesc->addEntry(kRec_SkDescriptorTag, sizeof(rec), &rec); |
| 99 | genericDesc->computeChecksum(); |
reed | a9322c2 | 2016-04-12 06:47:05 -0700 | [diff] [blame] | 100 | |
| 101 | // No effects, so we make a dummy struct |
| 102 | SkScalerContextEffects noEffects; |
cdalton | 855d83f | 2014-09-18 13:51:53 -0700 | [diff] [blame] | 103 | |
Hal Canary | 144caf5 | 2016-11-07 17:57:18 -0500 | [diff] [blame] | 104 | sk_sp<GlyphGenerator> generator(new GlyphGenerator(*typeface, noEffects, *genericDesc)); |
| 105 | return this->createPathRange(generator.get(), style); |
cdalton | 855d83f | 2014-09-18 13:51:53 -0700 | [diff] [blame] | 106 | } |