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 | 0607756 | 2016-05-04 13:50:29 -0700 | [diff] [blame] | 10 | int GrStyle::KeySize(const GrStyle &style, Apply apply, uint32_t flags) { |
bsalomon | fb08327 | 2016-05-04 08:27:41 -0700 | [diff] [blame] | 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 | |
bsalomon | 0607756 | 2016-05-04 13:50:29 -0700 | [diff] [blame] | 32 | void GrStyle::WriteKey(uint32_t *key, const GrStyle &style, Apply apply, uint32_t flags) { |
bsalomon | fb08327 | 2016-05-04 08:27:41 -0700 | [diff] [blame] | 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)); |
bsalomon | 0607756 | 2016-05-04 13:50:29 -0700 | [diff] [blame] | 66 | // The cap type only matters for unclosed shapes. However, a path effect could unclose |
| 67 | // the shape before it is stroked. |
| 68 | SkPaint::Cap cap; |
| 69 | if ((flags & kClosed_KeyFlag) && !style.pathEffect()) { |
| 70 | cap = SkPaint::kButt_Cap; |
| 71 | } else { |
| 72 | cap = style.strokeRec().getCap(); |
| 73 | } |
bsalomon | fb08327 | 2016-05-04 08:27:41 -0700 | [diff] [blame] | 74 | key[i++] = style.strokeRec().getStyle() | |
| 75 | style.strokeRec().getJoin() << kJoinShift | |
bsalomon | 0607756 | 2016-05-04 13:50:29 -0700 | [diff] [blame] | 76 | cap << kCapShift; |
bsalomon | fb08327 | 2016-05-04 08:27:41 -0700 | [diff] [blame] | 77 | |
| 78 | SkScalar scalar; |
| 79 | // Miter limit only affects miter joins |
| 80 | scalar = SkPaint::kMiter_Join == style.strokeRec().getJoin() |
| 81 | ? style.strokeRec().getMiter() |
| 82 | : -1.f; |
| 83 | memcpy(&key[i++], &scalar, sizeof(scalar)); |
| 84 | |
| 85 | scalar = style.strokeRec().getWidth(); |
| 86 | memcpy(&key[i++], &scalar, sizeof(scalar)); |
| 87 | } |
| 88 | SkASSERT(KeySize(style, apply) == i); |
| 89 | } |
| 90 | |
bsalomon | 308feba | 2016-05-03 10:11:55 -0700 | [diff] [blame] | 91 | void GrStyle::initPathEffect(SkPathEffect* pe) { |
bsalomon | fb08327 | 2016-05-04 08:27:41 -0700 | [diff] [blame] | 92 | SkASSERT(!fPathEffect) |
| 93 | SkASSERT(SkPathEffect::kNone_DashType == fDashInfo.fType); |
| 94 | SkASSERT(0 == fDashInfo.fIntervals.count()); |
bsalomon | 47cc769 | 2016-04-26 12:56:00 -0700 | [diff] [blame] | 95 | if (!pe) { |
bsalomon | 47cc769 | 2016-04-26 12:56:00 -0700 | [diff] [blame] | 96 | return; |
| 97 | } |
| 98 | SkPathEffect::DashInfo info; |
| 99 | if (SkPathEffect::kDash_DashType == pe->asADash(&info)) { |
bsalomon | fb08327 | 2016-05-04 08:27:41 -0700 | [diff] [blame] | 100 | if (fStrokeRec.getStyle() != SkStrokeRec::kFill_Style) { |
bsalomon | 47cc769 | 2016-04-26 12:56:00 -0700 | [diff] [blame] | 101 | fDashInfo.fType = SkPathEffect::kDash_DashType; |
| 102 | fDashInfo.fIntervals.reset(info.fCount); |
| 103 | fDashInfo.fPhase = info.fPhase; |
| 104 | info.fIntervals = fDashInfo.fIntervals.get(); |
| 105 | pe->asADash(&info); |
bsalomon | fb08327 | 2016-05-04 08:27:41 -0700 | [diff] [blame] | 106 | fPathEffect.reset(SkSafeRef(pe)); |
bsalomon | 47cc769 | 2016-04-26 12:56:00 -0700 | [diff] [blame] | 107 | } |
| 108 | } else { |
bsalomon | 308feba | 2016-05-03 10:11:55 -0700 | [diff] [blame] | 109 | fPathEffect.reset(SkSafeRef(pe)); |
bsalomon | 47cc769 | 2016-04-26 12:56:00 -0700 | [diff] [blame] | 110 | } |
bsalomon | fb08327 | 2016-05-04 08:27:41 -0700 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | static inline bool apply_path_effect(SkPath* dst, SkStrokeRec* strokeRec, |
| 114 | const sk_sp<SkPathEffect>& pe, const SkPath& src) { |
| 115 | if (!pe) { |
| 116 | return false; |
| 117 | } |
| 118 | if (!pe->filterPath(dst, src, strokeRec, nullptr)) { |
| 119 | return false; |
| 120 | } |
| 121 | dst->setIsVolatile(true); |
| 122 | return true; |
| 123 | } |
| 124 | |
| 125 | bool GrStyle::applyPathEffectToPath(SkPath *dst, SkStrokeRec *remainingStroke, |
| 126 | const SkPath &src) const { |
| 127 | SkASSERT(dst); |
| 128 | SkStrokeRec strokeRec = fStrokeRec; |
| 129 | if (!apply_path_effect(dst, &strokeRec, fPathEffect, src)) { |
| 130 | return false; |
| 131 | } |
| 132 | *remainingStroke = strokeRec; |
| 133 | return true; |
| 134 | } |
| 135 | |
| 136 | bool GrStyle::applyToPath(SkPath* dst, SkStrokeRec::InitStyle* style, const SkPath& src) const { |
| 137 | SkASSERT(style); |
| 138 | SkASSERT(dst); |
| 139 | SkStrokeRec strokeRec = fStrokeRec; |
| 140 | if (!apply_path_effect(dst, &strokeRec, fPathEffect, src)) { |
| 141 | return false; |
| 142 | } |
| 143 | if (strokeRec.needToApply()) { |
| 144 | if (!strokeRec.applyToPath(dst, *dst)) { |
| 145 | return false; |
| 146 | } |
| 147 | *style = SkStrokeRec::kFill_InitStyle; |
| 148 | } else { |
| 149 | SkASSERT(SkStrokeRec::kFill_Style == strokeRec.getStyle() || |
| 150 | SkStrokeRec::kHairline_Style == strokeRec.getStyle()); |
| 151 | *style = strokeRec.getStyle() == SkStrokeRec::kFill_Style |
| 152 | ? SkStrokeRec::kFill_InitStyle |
| 153 | : SkStrokeRec::kHairline_InitStyle; |
| 154 | } |
| 155 | return true; |
bsalomon | 47cc769 | 2016-04-26 12:56:00 -0700 | [diff] [blame] | 156 | } |