bsalomon | 47cc769 | 2016-04-26 12:56:00 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 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 "GrStyle.h" |
| 9 | |
bsalomon | fb08327 | 2016-05-04 08:27:41 -0700 | [diff] [blame^] | 10 | int GrStyle::KeySize(const GrStyle &style, Apply apply) { |
| 11 | GR_STATIC_ASSERT(sizeof(uint32_t) == sizeof(SkScalar)); |
| 12 | int size = 0; |
| 13 | if (style.isDashed()) { |
| 14 | // One scalar for dash phase and one for each dash value. |
| 15 | size += 1 + style.dashIntervalCnt(); |
| 16 | } else if (style.pathEffect()) { |
| 17 | // No key for a generic path effect. |
| 18 | return -1; |
| 19 | } |
| 20 | |
| 21 | if (Apply::kPathEffectOnly == apply) { |
| 22 | return size; |
| 23 | } |
| 24 | |
| 25 | if (style.strokeRec().needToApply()) { |
| 26 | // One for style/cap/join, 2 for miter and width. |
| 27 | size += 3; |
| 28 | } |
| 29 | return size; |
| 30 | } |
| 31 | |
| 32 | void GrStyle::WriteKey(uint32_t *key, const GrStyle &style, Apply apply) { |
| 33 | SkASSERT(key); |
| 34 | SkASSERT(KeySize(style, apply) >= 0); |
| 35 | GR_STATIC_ASSERT(sizeof(uint32_t) == sizeof(SkScalar)); |
| 36 | |
| 37 | int i = 0; |
| 38 | if (style.isDashed()) { |
| 39 | GR_STATIC_ASSERT(sizeof(style.dashPhase()) == sizeof(uint32_t)); |
| 40 | SkScalar phase = style.dashPhase(); |
| 41 | memcpy(&key[i++], &phase, sizeof(SkScalar)); |
| 42 | |
| 43 | int32_t count = style.dashIntervalCnt(); |
| 44 | // Dash count should always be even. |
| 45 | SkASSERT(0 == (count & 0x1)); |
| 46 | const SkScalar *intervals = style.dashIntervals(); |
| 47 | int intervalByteCnt = count * sizeof(SkScalar); |
| 48 | memcpy(&key[i], intervals, intervalByteCnt); |
| 49 | i += count; |
| 50 | } else { |
| 51 | SkASSERT(!style.pathEffect()); |
| 52 | } |
| 53 | |
| 54 | if (Apply::kPathEffectAndStrokeRec == apply && style.strokeRec().needToApply()) { |
| 55 | enum { |
| 56 | kStyleBits = 2, |
| 57 | kJoinBits = 2, |
| 58 | kCapBits = 32 - kStyleBits - kJoinBits, |
| 59 | |
| 60 | kJoinShift = kStyleBits, |
| 61 | kCapShift = kJoinShift + kJoinBits, |
| 62 | }; |
| 63 | GR_STATIC_ASSERT(SkStrokeRec::kStyleCount <= (1 << kStyleBits)); |
| 64 | GR_STATIC_ASSERT(SkPaint::kJoinCount <= (1 << kJoinBits)); |
| 65 | GR_STATIC_ASSERT(SkPaint::kCapCount <= (1 << kCapBits)); |
| 66 | key[i++] = style.strokeRec().getStyle() | |
| 67 | style.strokeRec().getJoin() << kJoinShift | |
| 68 | style.strokeRec().getCap() << kCapShift; |
| 69 | |
| 70 | SkScalar scalar; |
| 71 | // Miter limit only affects miter joins |
| 72 | scalar = SkPaint::kMiter_Join == style.strokeRec().getJoin() |
| 73 | ? style.strokeRec().getMiter() |
| 74 | : -1.f; |
| 75 | memcpy(&key[i++], &scalar, sizeof(scalar)); |
| 76 | |
| 77 | scalar = style.strokeRec().getWidth(); |
| 78 | memcpy(&key[i++], &scalar, sizeof(scalar)); |
| 79 | } |
| 80 | SkASSERT(KeySize(style, apply) == i); |
| 81 | } |
| 82 | |
bsalomon | 308feba | 2016-05-03 10:11:55 -0700 | [diff] [blame] | 83 | void GrStyle::initPathEffect(SkPathEffect* pe) { |
bsalomon | fb08327 | 2016-05-04 08:27:41 -0700 | [diff] [blame^] | 84 | SkASSERT(!fPathEffect) |
| 85 | SkASSERT(SkPathEffect::kNone_DashType == fDashInfo.fType); |
| 86 | SkASSERT(0 == fDashInfo.fIntervals.count()); |
bsalomon | 47cc769 | 2016-04-26 12:56:00 -0700 | [diff] [blame] | 87 | if (!pe) { |
bsalomon | 47cc769 | 2016-04-26 12:56:00 -0700 | [diff] [blame] | 88 | return; |
| 89 | } |
| 90 | SkPathEffect::DashInfo info; |
| 91 | if (SkPathEffect::kDash_DashType == pe->asADash(&info)) { |
bsalomon | fb08327 | 2016-05-04 08:27:41 -0700 | [diff] [blame^] | 92 | if (fStrokeRec.getStyle() != SkStrokeRec::kFill_Style) { |
bsalomon | 47cc769 | 2016-04-26 12:56:00 -0700 | [diff] [blame] | 93 | fDashInfo.fType = SkPathEffect::kDash_DashType; |
| 94 | fDashInfo.fIntervals.reset(info.fCount); |
| 95 | fDashInfo.fPhase = info.fPhase; |
| 96 | info.fIntervals = fDashInfo.fIntervals.get(); |
| 97 | pe->asADash(&info); |
bsalomon | fb08327 | 2016-05-04 08:27:41 -0700 | [diff] [blame^] | 98 | fPathEffect.reset(SkSafeRef(pe)); |
bsalomon | 47cc769 | 2016-04-26 12:56:00 -0700 | [diff] [blame] | 99 | } |
| 100 | } else { |
bsalomon | 308feba | 2016-05-03 10:11:55 -0700 | [diff] [blame] | 101 | fPathEffect.reset(SkSafeRef(pe)); |
bsalomon | 47cc769 | 2016-04-26 12:56:00 -0700 | [diff] [blame] | 102 | } |
bsalomon | fb08327 | 2016-05-04 08:27:41 -0700 | [diff] [blame^] | 103 | } |
| 104 | |
| 105 | static inline bool apply_path_effect(SkPath* dst, SkStrokeRec* strokeRec, |
| 106 | const sk_sp<SkPathEffect>& pe, const SkPath& src) { |
| 107 | if (!pe) { |
| 108 | return false; |
| 109 | } |
| 110 | if (!pe->filterPath(dst, src, strokeRec, nullptr)) { |
| 111 | return false; |
| 112 | } |
| 113 | dst->setIsVolatile(true); |
| 114 | return true; |
| 115 | } |
| 116 | |
| 117 | bool GrStyle::applyPathEffectToPath(SkPath *dst, SkStrokeRec *remainingStroke, |
| 118 | const SkPath &src) const { |
| 119 | SkASSERT(dst); |
| 120 | SkStrokeRec strokeRec = fStrokeRec; |
| 121 | if (!apply_path_effect(dst, &strokeRec, fPathEffect, src)) { |
| 122 | return false; |
| 123 | } |
| 124 | *remainingStroke = strokeRec; |
| 125 | return true; |
| 126 | } |
| 127 | |
| 128 | bool GrStyle::applyToPath(SkPath* dst, SkStrokeRec::InitStyle* style, const SkPath& src) const { |
| 129 | SkASSERT(style); |
| 130 | SkASSERT(dst); |
| 131 | SkStrokeRec strokeRec = fStrokeRec; |
| 132 | if (!apply_path_effect(dst, &strokeRec, fPathEffect, src)) { |
| 133 | return false; |
| 134 | } |
| 135 | if (strokeRec.needToApply()) { |
| 136 | if (!strokeRec.applyToPath(dst, *dst)) { |
| 137 | return false; |
| 138 | } |
| 139 | *style = SkStrokeRec::kFill_InitStyle; |
| 140 | } else { |
| 141 | SkASSERT(SkStrokeRec::kFill_Style == strokeRec.getStyle() || |
| 142 | SkStrokeRec::kHairline_Style == strokeRec.getStyle()); |
| 143 | *style = strokeRec.getStyle() == SkStrokeRec::kFill_Style |
| 144 | ? SkStrokeRec::kFill_InitStyle |
| 145 | : SkStrokeRec::kHairline_InitStyle; |
| 146 | } |
| 147 | return true; |
bsalomon | 47cc769 | 2016-04-26 12:56:00 -0700 | [diff] [blame] | 148 | } |