| 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 "src/gpu/ganesh/GrStyle.h" |
| 9 |
|
| 10 |
#include "include/core/SkMatrix.h" |
| 11 |
#include "include/core/SkPath.h" |
| 12 |
#include "include/core/SkPathBuilder.h" |
| 13 |
#include "include/core/SkSpan.h" |
| 14 |
#include "src/utils/SkDashPathPriv.h" |
| 15 |
|
| 16 |
#include <cstring> |
| 17 |
#include <optional> |
| 18 |
|
| 19 |
int GrStyle::KeySize(const GrStyle &style, Apply apply, uint32_t flags) { |
| 20 |
static_assert(sizeof(uint32_t) == sizeof(SkScalar)); |
| 21 |
|
| 22 |
// We embed the dash interval pattern into the key, and the key size must fit within 16-bits. |
| 23 |
// However, we put a more conservative upper limit on the dashes because we don't want to keep |
| 24 |
// key memory locked up in caches during pathological cases. |
| 25 |
static constexpr int kDashIntervalKeyLimit = 512; |
| 26 |
|
| 27 |
int size = 0; |
| 28 |
if (style.isDashed()) { |
| 29 |
if (style.dashIntervalCnt() > kDashIntervalKeyLimit) { |
| 30 |
return -1; // Disable caching for pathologically large dash patterns |
| 31 |
} |
| 32 |
|
| 33 |
// One scalar for scale, one for dash phase, and one for each dash value. |
| 34 |
size += 2 + style.dashIntervalCnt(); |
| 35 |
} else if (style.pathEffect()) { |
| 36 |
// No key for a generic path effect. |
| 37 |
return -1; |
| 38 |
} |
| 39 |
|
| 40 |
if (Apply::kPathEffectOnly == apply) { |
| 41 |
return size; |
| 42 |
} |
| 43 |
|
| 44 |
if (style.strokeRec().needToApply()) { |
| 45 |
// One for res scale, one for style/cap/join, one for miter limit, and one for width. |
| 46 |
size += 4; |
| 47 |
} |
| 48 |
return size; |
| 49 |
} |
| 50 |
|
| 51 |
void GrStyle::WriteKey(uint32_t *key, const GrStyle &style, Apply apply, SkScalar scale, |
| 52 |
uint32_t flags) { |
| 53 |
SkASSERT(key); |
| 54 |
SkASSERT(KeySize(style, apply) >= 0); |
| 55 |
static_assert(sizeof(uint32_t) == sizeof(SkScalar)); |
| 56 |
|
| 57 |
int i = 0; |
| 58 |
// The scale can influence both the path effect and stroking. We want to preserve the |
| 59 |
// property that the following two are equal: |
| 60 |
// 1. WriteKey with apply == kPathEffectAndStrokeRec |
| 61 |
// 2. WriteKey with apply == kPathEffectOnly followed by WriteKey of a GrStyle made |
| 62 |
// from SkStrokeRec output by the the path effect (and no additional path effect). |
| 63 |
// Since the scale can affect both parts of 2 we write it into the key twice. |
| 64 |
if (style.isDashed()) { |
| 65 |
static_assert(sizeof(style.dashPhase()) == sizeof(uint32_t)); |
| 66 |
SkScalar phase = style.dashPhase(); |
| 67 |
memcpy(&key[i++], &scale, sizeof(SkScalar)); |
| 68 |
memcpy(&key[i++], &phase, sizeof(SkScalar)); |
| 69 |
|
| 70 |
int32_t count = style.dashIntervalCnt(); |
| 71 |
// Dash count should always be even. |
| 72 |
SkASSERT(0 == (count & 0x1)); |
| 73 |
const SkScalar *intervals = style.dashIntervals(); |
| 74 |
int intervalByteCnt = count * sizeof(SkScalar); |
| 75 |
memcpy(&key[i], intervals, intervalByteCnt); |
| 76 |
i += count; |
| 77 |
} else { |
| 78 |
SkASSERT(!style.pathEffect()); |
| 79 |
} |
| 80 |
|
| 81 |
if (Apply::kPathEffectAndStrokeRec == apply && style.strokeRec().needToApply()) { |
| 82 |
memcpy(&key[i++], &scale, sizeof(SkScalar)); |
| 83 |
static constexpr uint32_t kStyleBits = 2; |
| 84 |
static constexpr uint32_t kJoinBits = 2; |
| 85 |
static constexpr uint32_t kCapBits = 32 - kStyleBits - kJoinBits; |
| 86 |
static constexpr uint32_t kJoinShift = kStyleBits; |
| 87 |
static constexpr uint32_t kCapShift = kJoinShift + kJoinBits; |
| 88 |
|
| 89 |
static_assert(SkStrokeRec::kStyleCount <= (1 << kStyleBits)); |
| 90 |
static_assert(SkPaint::kJoinCount <= (1 << kJoinBits)); |
| 91 |
static_assert(SkPaint::kCapCount <= (1 << kCapBits)); |
| 92 |
// The cap type only matters for unclosed shapes. However, a path effect could unclose |
| 93 |
// the shape before it is stroked. |
| 94 |
SkPaint::Cap cap = SkPaint::kDefault_Cap; |
| 95 |
if (!(flags & kClosed_KeyFlag) || style.pathEffect()) { |
| 96 |
cap = style.strokeRec().getCap(); |
| 97 |
} |
| 98 |
SkScalar miter = -1.f; |
| 99 |
SkPaint::Join join = SkPaint::kDefault_Join; |
| 100 |
|
| 101 |
// Dashing will not insert joins but other path effects may. |
| 102 |
if (!(flags & kNoJoins_KeyFlag) || style.hasNonDashPathEffect()) { |
| 103 |
join = style.strokeRec().getJoin(); |
| 104 |
// Miter limit only affects miter joins |
| 105 |
if (SkPaint::kMiter_Join == join) { |
| 106 |
miter = style.strokeRec().getMiter(); |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
key[i++] = style.strokeRec().getStyle() | |
| 111 |
join << kJoinShift | |
| 112 |
cap << kCapShift; |
| 113 |
|
| 114 |
memcpy(&key[i++], &miter, sizeof(miter)); |
| 115 |
|
| 116 |
SkScalar width = style.strokeRec().getWidth(); |
| 117 |
memcpy(&key[i++], &width, sizeof(width)); |
| 118 |
} |
| 119 |
SkASSERT(KeySize(style, apply) == i); |
| 120 |
} |
| 121 |
|
| 122 |
void GrStyle::initPathEffect(sk_sp<SkPathEffect> pe) { |
| 123 |
SkASSERT(!fPathEffect); |
| 124 |
SkASSERT(DashType::kNone == fDashInfo.fType); |
| 125 |
SkASSERT(0 == fDashInfo.fIntervals.count()); |
| 126 |
if (!pe) { |
| 127 |
return; |
| 128 |
} |
| 129 |
if (auto info = as_PEB(pe)->asADash()) { |
| 130 |
SkStrokeRec::Style recStyle = fStrokeRec.getStyle(); |
| 131 |
if (recStyle != SkStrokeRec::kFill_Style && recStyle != SkStrokeRec::kStrokeAndFill_Style) { |
| 132 |
fDashInfo.fType = DashType::kDash; |
| 133 |
fDashInfo.fIntervals.reset(info->fIntervals.size()); |
| 134 |
memcpy(fDashInfo.fIntervals.data(), info->fIntervals.data(), |
| 135 |
info->fIntervals.size_bytes()); |
| 136 |
fDashInfo.fPhase = info->fPhase; |
| 137 |
fPathEffect = std::move(pe); |
| 138 |
} |
| 139 |
} else { |
| 140 |
fPathEffect = std::move(pe); |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
bool GrStyle::applyPathEffect(SkPath* dst, SkStrokeRec* strokeRec, const SkPath& src) const { |
| 145 |
if (!fPathEffect) { |
| 146 |
return false; |
| 147 |
} |
| 148 |
|
| 149 |
// TODO: [skbug.com/40043046] Plumb CTM callers and pass it to filterPath(). |
| 150 |
SkASSERT(!fPathEffect->needsCTM()); |
| 151 |
|
| 152 |
SkPathBuilder builder; // todo: change our api to take builder |
| 153 |
|
| 154 |
if (DashType::kDash == fDashInfo.fType) { |
| 155 |
// We apply the dash ourselves here rather than using the path effect. This is so that |
| 156 |
// we can control whether the dasher applies the strokeRec for special cases. Our keying |
| 157 |
// depends on the strokeRec being applied separately. |
| 158 |
SkASSERT(!fPathEffect->needsCTM()); // Make sure specified PE doesn't need CTM |
| 159 |
SkScalar phase = fDashInfo.fPhase; |
| 160 |
SkScalar initialLength; |
| 161 |
size_t initialIndex; |
| 162 |
SkScalar intervalLength; |
| 163 |
SkDashPath::CalcDashParameters(phase, fDashInfo.fIntervals, &initialLength, |
| 164 |
&initialIndex, &intervalLength); |
| 165 |
if (!SkDashPath::InternalFilter(&builder, src, strokeRec, |
| 166 |
nullptr, fDashInfo.fIntervals, |
| 167 |
initialLength, initialIndex, intervalLength, phase, |
| 168 |
SkDashPath::StrokeRecApplication::kDisallow)) { |
| 169 |
return false; |
| 170 |
} |
| 171 |
} else if (!fPathEffect->filterPath(&builder, src, strokeRec, nullptr, SkMatrix::I())) { |
| 172 |
return false; |
| 173 |
} |
| 174 |
*dst = builder.detach(); |
| 175 |
dst->setIsVolatile(true); |
| 176 |
return true; |
| 177 |
} |
| 178 |
|
| 179 |
bool GrStyle::applyPathEffectToPath(SkPath *dst, SkStrokeRec *remainingStroke, |
| 180 |
const SkPath &src, SkScalar resScale) const { |
| 181 |
SkASSERT(dst); |
| 182 |
SkStrokeRec strokeRec = fStrokeRec; |
| 183 |
strokeRec.setResScale(resScale); |
| 184 |
if (!this->applyPathEffect(dst, &strokeRec, src)) { |
| 185 |
return false; |
| 186 |
} |
| 187 |
*remainingStroke = strokeRec; |
| 188 |
return true; |
| 189 |
} |
| 190 |
|
| 191 |
bool GrStyle::applyToPath(SkPath* dst, SkStrokeRec::InitStyle* style, const SkPath& src, |
| 192 |
SkScalar resScale) const { |
| 193 |
SkASSERT(style); |
| 194 |
SkASSERT(dst); |
| 195 |
SkStrokeRec strokeRec = fStrokeRec; |
| 196 |
strokeRec.setResScale(resScale); |
| 197 |
const SkPath* pathForStrokeRec = &src; |
| 198 |
if (this->applyPathEffect(dst, &strokeRec, src)) { |
| 199 |
pathForStrokeRec = dst; |
| 200 |
} else if (fPathEffect) { |
| 201 |
return false; |
| 202 |
} |
| 203 |
// todo: change our signature to take pathbuilder, or return an optional<SkPath> |
| 204 |
if (strokeRec.needToApply()) { |
| 205 |
SkPathBuilder builder; |
| 206 |
if (!strokeRec.applyToPath(&builder, *pathForStrokeRec)) { |
| 207 |
return false; |
| 208 |
} |
| 209 |
*dst = builder.detach(); |
| 210 |
dst->setIsVolatile(true); |
| 211 |
*style = SkStrokeRec::kFill_InitStyle; |
| 212 |
} else if (!fPathEffect) { |
| 213 |
// Nothing to do for path effect or stroke, fail. |
| 214 |
return false; |
| 215 |
} else { |
| 216 |
SkASSERT(SkStrokeRec::kFill_Style == strokeRec.getStyle() || |
| 217 |
SkStrokeRec::kHairline_Style == strokeRec.getStyle()); |
| 218 |
*style = strokeRec.getStyle() == SkStrokeRec::kFill_Style |
| 219 |
? SkStrokeRec::kFill_InitStyle |
| 220 |
: SkStrokeRec::kHairline_InitStyle; |
| 221 |
} |
| 222 |
return true; |
| 223 |
} |