kkinnunen | 1899651 | 2015-04-26 23:18:49 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 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 "GrStrokeInfo.h" |
kkinnunen | 50b58e6 | 2015-05-18 23:02:07 -0700 | [diff] [blame] | 9 | #include "GrResourceKey.h" |
kkinnunen | 1899651 | 2015-04-26 23:18:49 -0700 | [diff] [blame] | 10 | #include "SkDashPathPriv.h" |
| 11 | |
Tom Hudson | 2880df2 | 2015-10-29 09:55:42 -0400 | [diff] [blame] | 12 | bool all_dash_intervals_zero(const SkScalar* intervals, int count) { |
| 13 | for (int i = 0 ; i < count; ++i) { |
| 14 | if (intervals[i] != 0) { |
| 15 | return false; |
| 16 | } |
| 17 | } |
| 18 | return true; |
| 19 | } |
| 20 | |
kkinnunen | d156d36 | 2015-05-18 22:23:54 -0700 | [diff] [blame] | 21 | bool GrStrokeInfo::applyDashToPath(SkPath* dst, GrStrokeInfo* dstStrokeInfo, |
| 22 | const SkPath& src) const { |
kkinnunen | 1899651 | 2015-04-26 23:18:49 -0700 | [diff] [blame] | 23 | if (this->isDashed()) { |
kkinnunen | 261694c | 2015-05-05 08:00:10 -0700 | [diff] [blame] | 24 | SkPathEffect::DashInfo info; |
| 25 | info.fIntervals = fIntervals.get(); |
| 26 | info.fCount = fIntervals.count(); |
| 27 | info.fPhase = fDashPhase; |
kkinnunen | d156d36 | 2015-05-18 22:23:54 -0700 | [diff] [blame] | 28 | GrStrokeInfo filteredStroke(*this, false); |
Tom Hudson | 2880df2 | 2015-10-29 09:55:42 -0400 | [diff] [blame] | 29 | // Handle the case where all intervals are 0 and we simply drop the dash effect |
| 30 | if (all_dash_intervals_zero(fIntervals.get(), fIntervals.count())) { |
| 31 | *dstStrokeInfo = filteredStroke; |
| 32 | *dst = src; |
| 33 | return true; |
| 34 | } |
| 35 | // See if we can filter the dash into a path on cpu |
| 36 | if (SkDashPath::FilterDashPath(dst, src, &filteredStroke, nullptr, info)) { |
kkinnunen | d156d36 | 2015-05-18 22:23:54 -0700 | [diff] [blame] | 37 | *dstStrokeInfo = filteredStroke; |
kkinnunen | 1899651 | 2015-04-26 23:18:49 -0700 | [diff] [blame] | 38 | return true; |
| 39 | } |
| 40 | } |
| 41 | return false; |
| 42 | } |
kkinnunen | 50b58e6 | 2015-05-18 23:02:07 -0700 | [diff] [blame] | 43 | |
| 44 | void GrStrokeInfo::asUniqueKeyFragment(uint32_t* data) const { |
| 45 | const int kSkScalarData32Cnt = sizeof(SkScalar) / sizeof(uint32_t); |
| 46 | enum { |
| 47 | kStyleBits = 2, |
| 48 | kJoinBits = 2, |
| 49 | kCapBits = 32 - kStyleBits - kJoinBits, |
| 50 | |
| 51 | kJoinShift = kStyleBits, |
| 52 | kCapShift = kJoinShift + kJoinBits, |
| 53 | }; |
| 54 | |
Tom Hudson | 2880df2 | 2015-10-29 09:55:42 -0400 | [diff] [blame] | 55 | static_assert(SkStrokeRec::kStyleCount <= (1 << kStyleBits), "style_shift_will_be_wrong"); |
| 56 | static_assert(SkPaint::kJoinCount <= (1 << kJoinBits), "cap_shift_will_be_wrong"); |
| 57 | static_assert(SkPaint::kCapCount <= (1 << kCapBits), "cap_does_not_fit"); |
kkinnunen | 50b58e6 | 2015-05-18 23:02:07 -0700 | [diff] [blame] | 58 | uint32_t styleKey = this->getStyle(); |
| 59 | if (this->needToApply()) { |
| 60 | styleKey |= this->getJoin() << kJoinShift; |
| 61 | styleKey |= this->getCap() << kCapShift; |
| 62 | } |
| 63 | int i = 0; |
| 64 | data[i++] = styleKey; |
| 65 | |
| 66 | // Memcpy the scalar fields. Does not "reinterpret_cast<SkScalar&>(data[i]) = ..." due to |
| 67 | // scalars having more strict alignment requirements than what data can guarantee. The |
| 68 | // compiler should optimize memcpys to assignments. |
| 69 | SkScalar scalar; |
| 70 | scalar = this->getMiter(); |
| 71 | memcpy(&data[i], &scalar, sizeof(scalar)); |
| 72 | i += kSkScalarData32Cnt; |
| 73 | |
| 74 | scalar = this->getWidth(); |
| 75 | memcpy(&data[i], &scalar, sizeof(scalar)); |
| 76 | i += kSkScalarData32Cnt; |
| 77 | |
| 78 | if (this->isDashed()) { |
| 79 | SkScalar phase = this->getDashPhase(); |
| 80 | memcpy(&data[i], &phase, sizeof(phase)); |
| 81 | i += kSkScalarData32Cnt; |
| 82 | |
| 83 | int32_t count = this->getDashCount() & static_cast<int32_t>(~1); |
| 84 | SkASSERT(count == this->getDashCount()); |
| 85 | const SkScalar* intervals = this->getDashIntervals(); |
| 86 | int intervalByteCnt = count * sizeof(SkScalar); |
| 87 | memcpy(&data[i], intervals, intervalByteCnt); |
| 88 | // Enable the line below if fields are added after dashing. |
| 89 | SkDEBUGCODE(i += kSkScalarData32Cnt * count); |
| 90 | } |
| 91 | |
| 92 | SkASSERT(this->computeUniqueKeyFragmentData32Cnt() == i); |
| 93 | } |
| 94 | |