blob: 4e1119dfbb45036f2a38cd9135006454364c29e2 [file] [log] [blame]
robertphillips@google.com7fa18762012-09-11 13:02:31 +00001/*
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
kkinnunen070e0102015-05-21 00:37:30 -070010namespace {
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.
13static const int kSimpleVolatilePathVerbLimit = 10;
14
15inline 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 }
bungeman99fe8222015-08-20 07:57:51 -070021 static_assert((sizeof(pts) % sizeof(uint32_t)) == 0 && sizeof(pts) > sizeof(uint32_t),
22 "pts_needs_padding");
kkinnunen070e0102015-05-21 00:37:30 -070023
24 const int kBaseData32Cnt = 1 + sizeof(pts) / sizeof(uint32_t);
kkinnunen50b58e62015-05-18 23:02:07 -070025 int strokeDataCnt = stroke.computeUniqueKeyFragmentData32Cnt();
kkinnunen070e0102015-05-21 00:37:30 -070026 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
36inline 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 }
bungeman99fe8222015-08-20 07:57:51 -070042 static_assert((sizeof(rect) % sizeof(uint32_t)) == 0 && sizeof(rect) > sizeof(uint32_t),
43 "rect_needs_padding");
kkinnunen070e0102015-05-21 00:37:30 -070044
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.
60inline static bool compute_key_for_simple_path(const SkPath& path, const GrStrokeInfo& stroke,
61 GrUniqueKey* key) {
62 if (!path.isVolatile()) {
63 return false;
64 }
kkinnunen070e0102015-05-21 00:37:30 -070065 // 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.
bungeman99fe8222015-08-20 07:57:51 -070072 static_assert(kSimpleVolatilePathVerbLimit <= 100,
73 "big_simple_volatile_path_verb_limit_may_cause_overflow");
kkinnunen070e0102015-05-21 00:37:30 -070074
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
bungeman99fe8222015-08-20 07:57:51 -0700124 static_assert(((sizeof(SkPoint) % sizeof(uint32_t)) == 0) && sizeof(SkPoint) > sizeof(uint32_t),
125 "skpoint_array_needs_padding");
kkinnunen070e0102015-05-21 00:37:30 -0700126
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
138inline 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);
kkinnunen50b58e62015-05-18 23:02:07 -0700144 builder[0] = path.getGenerationID();
145 builder[1] = path.getFillType();
146 if (strokeDataCnt > 0) {
kkinnunen070e0102015-05-21 00:37:30 -0700147 stroke.asUniqueKeyFragment(&builder[kBaseData32Cnt]);
cdaltonb85a0aa2014-07-21 15:32:44 -0700148 }
cdaltonb85a0aa2014-07-21 15:32:44 -0700149}
kkinnunen50b58e62015-05-18 23:02:07 -0700150
kkinnunen070e0102015-05-21 00:37:30 -0700151}
152
153void 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