OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2016 Google Inc. | 2 * Copyright 2016 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "GrStyle.h" | 8 #include "GrStyle.h" |
9 | 9 |
| 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 |
10 void GrStyle::initPathEffect(SkPathEffect* pe) { | 83 void GrStyle::initPathEffect(SkPathEffect* pe) { |
| 84 SkASSERT(!fPathEffect) |
| 85 SkASSERT(SkPathEffect::kNone_DashType == fDashInfo.fType); |
| 86 SkASSERT(0 == fDashInfo.fIntervals.count()); |
11 if (!pe) { | 87 if (!pe) { |
12 fDashInfo.fType = SkPathEffect::kNone_DashType; | |
13 return; | 88 return; |
14 } | 89 } |
15 SkPathEffect::DashInfo info; | 90 SkPathEffect::DashInfo info; |
16 if (SkPathEffect::kDash_DashType == pe->asADash(&info)) { | 91 if (SkPathEffect::kDash_DashType == pe->asADash(&info)) { |
17 if (fStrokeRec.getStyle() == SkStrokeRec::kFill_Style) { | 92 if (fStrokeRec.getStyle() != SkStrokeRec::kFill_Style) { |
18 fPathEffect.reset(nullptr); | |
19 } else { | |
20 fPathEffect.reset(SkSafeRef(pe)); | |
21 fDashInfo.fType = SkPathEffect::kDash_DashType; | 93 fDashInfo.fType = SkPathEffect::kDash_DashType; |
22 fDashInfo.fIntervals.reset(info.fCount); | 94 fDashInfo.fIntervals.reset(info.fCount); |
23 fDashInfo.fPhase = info.fPhase; | 95 fDashInfo.fPhase = info.fPhase; |
24 info.fIntervals = fDashInfo.fIntervals.get(); | 96 info.fIntervals = fDashInfo.fIntervals.get(); |
25 pe->asADash(&info); | 97 pe->asADash(&info); |
26 return; | 98 fPathEffect.reset(SkSafeRef(pe)); |
27 } | 99 } |
28 } else { | 100 } else { |
29 fPathEffect.reset(SkSafeRef(pe)); | 101 fPathEffect.reset(SkSafeRef(pe)); |
30 } | 102 } |
31 fDashInfo.fType = SkPathEffect::kNone_DashType; | |
32 fDashInfo.fIntervals.reset(0); | |
33 } | 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 SkPa
th& 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; |
| 148 } |
OLD | NEW |