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, uint32_t flags) { | 10 int GrStyle::KeySize(const GrStyle &style, Apply apply, uint32_t flags) { |
11 GR_STATIC_ASSERT(sizeof(uint32_t) == sizeof(SkScalar)); | 11 GR_STATIC_ASSERT(sizeof(uint32_t) == sizeof(SkScalar)); |
12 int size = 0; | 12 int size = 0; |
13 if (style.isDashed()) { | 13 if (style.isDashed()) { |
14 // One scalar for dash phase and one for each dash value. | 14 // One scalar for scale, one for dash phase, and one for each dash value
. |
15 size += 1 + style.dashIntervalCnt(); | 15 size += 2 + style.dashIntervalCnt(); |
16 } else if (style.pathEffect()) { | 16 } else if (style.pathEffect()) { |
17 // No key for a generic path effect. | 17 // No key for a generic path effect. |
18 return -1; | 18 return -1; |
19 } | 19 } |
20 | 20 |
21 if (Apply::kPathEffectOnly == apply) { | 21 if (Apply::kPathEffectOnly == apply) { |
22 return size; | 22 return size; |
23 } | 23 } |
24 | 24 |
25 if (style.strokeRec().needToApply()) { | 25 if (style.strokeRec().needToApply()) { |
26 // One for style/cap/join, 2 for miter and width. | 26 // One for res scale, one for style/cap/join, one for miter limit, and o
ne for width. |
27 size += 3; | 27 size += 4; |
28 } | 28 } |
29 return size; | 29 return size; |
30 } | 30 } |
31 | 31 |
32 void GrStyle::WriteKey(uint32_t *key, const GrStyle &style, Apply apply, uint32_
t flags) { | 32 void GrStyle::WriteKey(uint32_t *key, const GrStyle &style, Apply apply, SkScala
r scale, |
| 33 uint32_t flags) { |
33 SkASSERT(key); | 34 SkASSERT(key); |
34 SkASSERT(KeySize(style, apply) >= 0); | 35 SkASSERT(KeySize(style, apply) >= 0); |
35 GR_STATIC_ASSERT(sizeof(uint32_t) == sizeof(SkScalar)); | 36 GR_STATIC_ASSERT(sizeof(uint32_t) == sizeof(SkScalar)); |
36 | 37 |
37 int i = 0; | 38 int i = 0; |
| 39 // The scale can influence both the path effect and stroking. We want to pre
serve the |
| 40 // property that the following two are equal: |
| 41 // 1. WriteKey with apply == kPathEffectAndStrokeRec |
| 42 // 2. WriteKey with apply == kPathEffectOnly followed by WriteKey of a GrSty
le made |
| 43 // from SkStrokeRec output by the the path effect (and no additional path
effect). |
| 44 // Since the scale can affect both parts of 2 we write it into the key twice
. |
38 if (style.isDashed()) { | 45 if (style.isDashed()) { |
39 GR_STATIC_ASSERT(sizeof(style.dashPhase()) == sizeof(uint32_t)); | 46 GR_STATIC_ASSERT(sizeof(style.dashPhase()) == sizeof(uint32_t)); |
40 SkScalar phase = style.dashPhase(); | 47 SkScalar phase = style.dashPhase(); |
| 48 memcpy(&key[i++], &scale, sizeof(SkScalar)); |
41 memcpy(&key[i++], &phase, sizeof(SkScalar)); | 49 memcpy(&key[i++], &phase, sizeof(SkScalar)); |
42 | 50 |
43 int32_t count = style.dashIntervalCnt(); | 51 int32_t count = style.dashIntervalCnt(); |
44 // Dash count should always be even. | 52 // Dash count should always be even. |
45 SkASSERT(0 == (count & 0x1)); | 53 SkASSERT(0 == (count & 0x1)); |
46 const SkScalar *intervals = style.dashIntervals(); | 54 const SkScalar *intervals = style.dashIntervals(); |
47 int intervalByteCnt = count * sizeof(SkScalar); | 55 int intervalByteCnt = count * sizeof(SkScalar); |
48 memcpy(&key[i], intervals, intervalByteCnt); | 56 memcpy(&key[i], intervals, intervalByteCnt); |
49 i += count; | 57 i += count; |
50 } else { | 58 } else { |
51 SkASSERT(!style.pathEffect()); | 59 SkASSERT(!style.pathEffect()); |
52 } | 60 } |
53 | 61 |
54 if (Apply::kPathEffectAndStrokeRec == apply && style.strokeRec().needToApply
()) { | 62 if (Apply::kPathEffectAndStrokeRec == apply && style.strokeRec().needToApply
()) { |
| 63 memcpy(&key[i++], &scale, sizeof(SkScalar)); |
55 enum { | 64 enum { |
56 kStyleBits = 2, | 65 kStyleBits = 2, |
57 kJoinBits = 2, | 66 kJoinBits = 2, |
58 kCapBits = 32 - kStyleBits - kJoinBits, | 67 kCapBits = 32 - kStyleBits - kJoinBits, |
59 | 68 |
60 kJoinShift = kStyleBits, | 69 kJoinShift = kStyleBits, |
61 kCapShift = kJoinShift + kJoinBits, | 70 kCapShift = kJoinShift + kJoinBits, |
62 }; | 71 }; |
63 GR_STATIC_ASSERT(SkStrokeRec::kStyleCount <= (1 << kStyleBits)); | 72 GR_STATIC_ASSERT(SkStrokeRec::kStyleCount <= (1 << kStyleBits)); |
64 GR_STATIC_ASSERT(SkPaint::kJoinCount <= (1 << kJoinBits)); | 73 GR_STATIC_ASSERT(SkPaint::kJoinCount <= (1 << kJoinBits)); |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
116 return false; | 125 return false; |
117 } | 126 } |
118 if (!pe->filterPath(dst, src, strokeRec, nullptr)) { | 127 if (!pe->filterPath(dst, src, strokeRec, nullptr)) { |
119 return false; | 128 return false; |
120 } | 129 } |
121 dst->setIsVolatile(true); | 130 dst->setIsVolatile(true); |
122 return true; | 131 return true; |
123 } | 132 } |
124 | 133 |
125 bool GrStyle::applyPathEffectToPath(SkPath *dst, SkStrokeRec *remainingStroke, | 134 bool GrStyle::applyPathEffectToPath(SkPath *dst, SkStrokeRec *remainingStroke, |
126 const SkPath &src) const { | 135 const SkPath &src, SkScalar resScale) const
{ |
127 SkASSERT(dst); | 136 SkASSERT(dst); |
128 SkStrokeRec strokeRec = fStrokeRec; | 137 SkStrokeRec strokeRec = fStrokeRec; |
| 138 strokeRec.setResScale(resScale); |
129 if (!apply_path_effect(dst, &strokeRec, fPathEffect, src)) { | 139 if (!apply_path_effect(dst, &strokeRec, fPathEffect, src)) { |
130 return false; | 140 return false; |
131 } | 141 } |
132 *remainingStroke = strokeRec; | 142 *remainingStroke = strokeRec; |
133 return true; | 143 return true; |
134 } | 144 } |
135 | 145 |
136 bool GrStyle::applyToPath(SkPath* dst, SkStrokeRec::InitStyle* style, const SkPa
th& src) const { | 146 bool GrStyle::applyToPath(SkPath* dst, SkStrokeRec::InitStyle* style, const SkPa
th& src, |
| 147 SkScalar resScale) const { |
137 SkASSERT(style); | 148 SkASSERT(style); |
138 SkASSERT(dst); | 149 SkASSERT(dst); |
139 SkStrokeRec strokeRec = fStrokeRec; | 150 SkStrokeRec strokeRec = fStrokeRec; |
| 151 strokeRec.setResScale(resScale); |
140 const SkPath* pathForStrokeRec = &src; | 152 const SkPath* pathForStrokeRec = &src; |
141 if (apply_path_effect(dst, &strokeRec, fPathEffect, src)) { | 153 if (apply_path_effect(dst, &strokeRec, fPathEffect, src)) { |
142 pathForStrokeRec = dst; | 154 pathForStrokeRec = dst; |
143 } else if (fPathEffect) { | 155 } else if (fPathEffect) { |
144 return false; | 156 return false; |
145 } | 157 } |
146 if (strokeRec.needToApply()) { | 158 if (strokeRec.needToApply()) { |
147 if (!strokeRec.applyToPath(dst, *pathForStrokeRec)) { | 159 if (!strokeRec.applyToPath(dst, *pathForStrokeRec)) { |
148 return false; | 160 return false; |
149 } | 161 } |
150 *style = SkStrokeRec::kFill_InitStyle; | 162 *style = SkStrokeRec::kFill_InitStyle; |
151 } else if (!fPathEffect) { | 163 } else if (!fPathEffect) { |
152 // Nothing to do for path effect or stroke, fail. | 164 // Nothing to do for path effect or stroke, fail. |
153 return false; | 165 return false; |
154 } else { | 166 } else { |
155 SkASSERT(SkStrokeRec::kFill_Style == strokeRec.getStyle() || | 167 SkASSERT(SkStrokeRec::kFill_Style == strokeRec.getStyle() || |
156 SkStrokeRec::kHairline_Style == strokeRec.getStyle()); | 168 SkStrokeRec::kHairline_Style == strokeRec.getStyle()); |
157 *style = strokeRec.getStyle() == SkStrokeRec::kFill_Style | 169 *style = strokeRec.getStyle() == SkStrokeRec::kFill_Style |
158 ? SkStrokeRec::kFill_InitStyle | 170 ? SkStrokeRec::kFill_InitStyle |
159 : SkStrokeRec::kHairline_InitStyle; | 171 : SkStrokeRec::kHairline_InitStyle; |
160 } | 172 } |
161 return true; | 173 return true; |
162 } | 174 } |
OLD | NEW |