| robertphillips@google.com | 7fa1876 | 2012-09-11 13:02:31 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2012 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 "GrPath.h" |
| 9 | |
| kkinnunen | 070e010 | 2015-05-21 00:37:30 -0700 | [diff] [blame] | 10 | namespace { |
| 11 | // Verb count limit for generating path key from content of a volatile path. |
| 12 | // The value should accomodate at least simple rects and rrects. |
| 13 | static const int kSimpleVolatilePathVerbLimit = 10; |
| 14 | |
| 15 | inline static bool compute_key_for_line_path(const SkPath& path, const GrStrokeInfo& stroke, |
| 16 | GrUniqueKey* key) { |
| 17 | SkPoint pts[2]; |
| 18 | if (!path.isLine(pts)) { |
| 19 | return false; |
| 20 | } |
| bungeman | 99fe822 | 2015-08-20 07:57:51 -0700 | [diff] [blame] | 21 | static_assert((sizeof(pts) % sizeof(uint32_t)) == 0 && sizeof(pts) > sizeof(uint32_t), |
| 22 | "pts_needs_padding"); |
| kkinnunen | 070e010 | 2015-05-21 00:37:30 -0700 | [diff] [blame] | 23 | |
| 24 | const int kBaseData32Cnt = 1 + sizeof(pts) / sizeof(uint32_t); |
| kkinnunen | 50b58e6 | 2015-05-18 23:02:07 -0700 | [diff] [blame] | 25 | int strokeDataCnt = stroke.computeUniqueKeyFragmentData32Cnt(); |
| kkinnunen | 070e010 | 2015-05-21 00:37:30 -0700 | [diff] [blame] | 26 | static const GrUniqueKey::Domain kOvalPathDomain = GrUniqueKey::GenerateDomain(); |
| 27 | GrUniqueKey::Builder builder(key, kOvalPathDomain, kBaseData32Cnt + strokeDataCnt); |
| 28 | builder[0] = path.getFillType(); |
| 29 | memcpy(&builder[1], &pts, sizeof(pts)); |
| 30 | if (strokeDataCnt > 0) { |
| 31 | stroke.asUniqueKeyFragment(&builder[kBaseData32Cnt]); |
| 32 | } |
| 33 | return true; |
| 34 | } |
| 35 | |
| 36 | inline static bool compute_key_for_oval_path(const SkPath& path, const GrStrokeInfo& stroke, |
| 37 | GrUniqueKey* key) { |
| 38 | SkRect rect; |
| 39 | if (!path.isOval(&rect)) { |
| 40 | return false; |
| 41 | } |
| bungeman | 99fe822 | 2015-08-20 07:57:51 -0700 | [diff] [blame] | 42 | static_assert((sizeof(rect) % sizeof(uint32_t)) == 0 && sizeof(rect) > sizeof(uint32_t), |
| 43 | "rect_needs_padding"); |
| kkinnunen | 070e010 | 2015-05-21 00:37:30 -0700 | [diff] [blame] | 44 | |
| 45 | const int kBaseData32Cnt = 1 + sizeof(rect) / sizeof(uint32_t); |
| 46 | int strokeDataCnt = stroke.computeUniqueKeyFragmentData32Cnt(); |
| 47 | static const GrUniqueKey::Domain kOvalPathDomain = GrUniqueKey::GenerateDomain(); |
| 48 | GrUniqueKey::Builder builder(key, kOvalPathDomain, kBaseData32Cnt + strokeDataCnt); |
| 49 | builder[0] = path.getFillType(); |
| 50 | memcpy(&builder[1], &rect, sizeof(rect)); |
| 51 | if (strokeDataCnt > 0) { |
| 52 | stroke.asUniqueKeyFragment(&builder[kBaseData32Cnt]); |
| 53 | } |
| 54 | return true; |
| 55 | } |
| 56 | |
| 57 | // Encodes the full path data to the unique key for very small, volatile paths. This is typically |
| 58 | // hit when clipping stencils the clip stack. Intention is that this handles rects too, since |
| 59 | // SkPath::isRect seems to do non-trivial amount of work. |
| 60 | inline static bool compute_key_for_simple_path(const SkPath& path, const GrStrokeInfo& stroke, |
| 61 | GrUniqueKey* key) { |
| 62 | if (!path.isVolatile()) { |
| 63 | return false; |
| 64 | } |
| kkinnunen | 070e010 | 2015-05-21 00:37:30 -0700 | [diff] [blame] | 65 | // The check below should take care of negative values casted positive. |
| 66 | const int verbCnt = path.countVerbs(); |
| 67 | if (verbCnt > kSimpleVolatilePathVerbLimit) { |
| 68 | return false; |
| 69 | } |
| 70 | |
| 71 | // If somebody goes wild with the constant, it might cause an overflow. |
| bungeman | 99fe822 | 2015-08-20 07:57:51 -0700 | [diff] [blame] | 72 | static_assert(kSimpleVolatilePathVerbLimit <= 100, |
| 73 | "big_simple_volatile_path_verb_limit_may_cause_overflow"); |
| kkinnunen | 070e010 | 2015-05-21 00:37:30 -0700 | [diff] [blame] | 74 | |
| 75 | const int pointCnt = path.countPoints(); |
| 76 | if (pointCnt < 0) { |
| 77 | SkASSERT(false); |
| 78 | return false; |
| 79 | } |
| 80 | |
| 81 | // Construct counts that align as uint32_t counts. |
| 82 | #define ARRAY_DATA32_COUNT(array_type, count) \ |
| 83 | static_cast<int>((((count) * sizeof(array_type) + sizeof(uint32_t) - 1) / sizeof(uint32_t))) |
| 84 | |
| 85 | const int verbData32Cnt = ARRAY_DATA32_COUNT(uint8_t, verbCnt); |
| 86 | const int pointData32Cnt = ARRAY_DATA32_COUNT(SkPoint, pointCnt); |
| 87 | |
| 88 | #undef ARRAY_DATA32_COUNT |
| 89 | |
| 90 | // The unique key data is a "message" with following fragments: |
| 91 | // 0) domain, key length, uint32_t for fill type and uint32_t for verbCnt |
| 92 | // (fragment 0, fixed size) |
| 93 | // 1) verb and point data (varying size) |
| 94 | // 2) stroke data (varying size) |
| 95 | |
| 96 | const int baseData32Cnt = 2 + verbData32Cnt + pointData32Cnt; |
| 97 | const int strokeDataCnt = stroke.computeUniqueKeyFragmentData32Cnt(); |
| 98 | static const GrUniqueKey::Domain kSimpleVolatilePathDomain = GrUniqueKey::GenerateDomain(); |
| 99 | GrUniqueKey::Builder builder(key, kSimpleVolatilePathDomain, baseData32Cnt + strokeDataCnt); |
| 100 | int i = 0; |
| 101 | builder[i++] = path.getFillType(); |
| 102 | |
| 103 | // Serialize the verbCnt to make the whole message unambiguous. |
| 104 | // We serialize two variable length fragments to the message: |
| 105 | // * verb and point data (fragment 1) |
| 106 | // * stroke data (fragment 2) |
| 107 | // "Proof:" |
| 108 | // Verb count establishes unambiguous verb data. |
| 109 | // Unambiguous verb data establishes unambiguous point data, making fragment 1 unambiguous. |
| 110 | // Unambiguous fragment 1 establishes unambiguous fragment 2, since the length of the message |
| 111 | // has been established. |
| 112 | |
| 113 | builder[i++] = SkToU32(verbCnt); // The path limit is compile-asserted above, so the cast is ok. |
| 114 | |
| 115 | // Fill the last uint32_t with 0 first, since the last uint8_ts of the uint32_t may be |
| 116 | // uninitialized. This does not produce ambiguous verb data, since we have serialized the exact |
| 117 | // verb count. |
| 118 | if (verbData32Cnt != static_cast<int>((verbCnt * sizeof(uint8_t) / sizeof(uint32_t)))) { |
| 119 | builder[i + verbData32Cnt - 1] = 0; |
| 120 | } |
| 121 | path.getVerbs(reinterpret_cast<uint8_t*>(&builder[i]), verbCnt); |
| 122 | i += verbData32Cnt; |
| 123 | |
| bungeman | 99fe822 | 2015-08-20 07:57:51 -0700 | [diff] [blame] | 124 | static_assert(((sizeof(SkPoint) % sizeof(uint32_t)) == 0) && sizeof(SkPoint) > sizeof(uint32_t), |
| 125 | "skpoint_array_needs_padding"); |
| kkinnunen | 070e010 | 2015-05-21 00:37:30 -0700 | [diff] [blame] | 126 | |
| 127 | // Here we assume getPoints does a memcpy, so that we do not need to worry about the alignment. |
| 128 | path.getPoints(reinterpret_cast<SkPoint*>(&builder[i]), pointCnt); |
| 129 | SkDEBUGCODE(i += pointData32Cnt); |
| 130 | |
| 131 | SkASSERT(i == baseData32Cnt); |
| 132 | if (strokeDataCnt > 0) { |
| 133 | stroke.asUniqueKeyFragment(&builder[baseData32Cnt]); |
| 134 | } |
| 135 | return true; |
| 136 | } |
| 137 | |
| 138 | inline static void compute_key_for_general_path(const SkPath& path, const GrStrokeInfo& stroke, |
| 139 | GrUniqueKey* key) { |
| 140 | const int kBaseData32Cnt = 2; |
| 141 | int strokeDataCnt = stroke.computeUniqueKeyFragmentData32Cnt(); |
| 142 | static const GrUniqueKey::Domain kGeneralPathDomain = GrUniqueKey::GenerateDomain(); |
| 143 | GrUniqueKey::Builder builder(key, kGeneralPathDomain, kBaseData32Cnt + strokeDataCnt); |
| kkinnunen | 50b58e6 | 2015-05-18 23:02:07 -0700 | [diff] [blame] | 144 | builder[0] = path.getGenerationID(); |
| 145 | builder[1] = path.getFillType(); |
| 146 | if (strokeDataCnt > 0) { |
| kkinnunen | 070e010 | 2015-05-21 00:37:30 -0700 | [diff] [blame] | 147 | stroke.asUniqueKeyFragment(&builder[kBaseData32Cnt]); |
| cdalton | b85a0aa | 2014-07-21 15:32:44 -0700 | [diff] [blame] | 148 | } |
| cdalton | b85a0aa | 2014-07-21 15:32:44 -0700 | [diff] [blame] | 149 | } |
| kkinnunen | 50b58e6 | 2015-05-18 23:02:07 -0700 | [diff] [blame] | 150 | |
| kkinnunen | 070e010 | 2015-05-21 00:37:30 -0700 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | void GrPath::ComputeKey(const SkPath& path, const GrStrokeInfo& stroke, GrUniqueKey* key, |
| 154 | bool* outIsVolatile) { |
| 155 | if (compute_key_for_line_path(path, stroke, key)) { |
| 156 | *outIsVolatile = false; |
| 157 | return; |
| 158 | } |
| 159 | |
| 160 | if (compute_key_for_oval_path(path, stroke, key)) { |
| 161 | *outIsVolatile = false; |
| 162 | return; |
| 163 | } |
| 164 | |
| 165 | if (compute_key_for_simple_path(path, stroke, key)) { |
| 166 | *outIsVolatile = false; |
| 167 | return; |
| 168 | } |
| 169 | |
| 170 | compute_key_for_general_path(path, stroke, key); |
| 171 | *outIsVolatile = path.isVolatile(); |
| 172 | } |
| 173 | |