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 #ifndef GrShape_DEFINED | 8 #ifndef GrShape_DEFINED |
9 #define GrShape_DEFINED | 9 #define GrShape_DEFINED |
10 | 10 |
(...skipping 15 matching lines...) Expand all Loading... |
26 * to geometric information and is included in the new shape's key. When the sam
e style is applied | 26 * to geometric information and is included in the new shape's key. When the sam
e style is applied |
27 * to two shapes that reflect the same underlying geometry the computed keys of
the stylized shapes | 27 * to two shapes that reflect the same underlying geometry the computed keys of
the stylized shapes |
28 * will be the same. | 28 * will be the same. |
29 * | 29 * |
30 * Currently this can only be constructed from a rrect, though it can become a p
ath by applying | 30 * Currently this can only be constructed from a rrect, though it can become a p
ath by applying |
31 * style to the geometry. The idea is to expand this to cover most or all of the
geometries that | 31 * style to the geometry. The idea is to expand this to cover most or all of the
geometries that |
32 * have SkCanvas::draw APIs. | 32 * have SkCanvas::draw APIs. |
33 */ | 33 */ |
34 class GrShape { | 34 class GrShape { |
35 public: | 35 public: |
| 36 GrShape(const SkPath& path) |
| 37 : fType(Type::kPath) |
| 38 , fPath(&path) { |
| 39 this->attemptToReduceFromPath(); |
| 40 } |
| 41 |
36 GrShape() : fType(Type::kEmpty) {} | 42 GrShape() : fType(Type::kEmpty) {} |
37 | 43 |
38 explicit GrShape(const SkRRect& rrect) : fType(Type::kRRect), fRRect(rrect)
{} | 44 explicit GrShape(const SkRRect& rrect) : fType(Type::kRRect), fRRect(rrect)
{} |
39 explicit GrShape(const SkRect& rect) : fType(Type::kRRect), fRRect(SkRRect::
MakeRect(rect)) {} | 45 explicit GrShape(const SkRect& rect) : fType(Type::kRRect), fRRect(SkRRect::
MakeRect(rect)) {} |
40 | 46 |
| 47 GrShape(const SkPath& path, const GrStyle& style) |
| 48 : fType(Type::kPath) |
| 49 , fPath(&path) |
| 50 , fStyle(style) { |
| 51 this->attemptToReduceFromPath(); |
| 52 } |
| 53 |
41 GrShape(const SkRRect& rrect, const GrStyle& style) | 54 GrShape(const SkRRect& rrect, const GrStyle& style) |
42 : fType(Type::kRRect) | 55 : fType(Type::kRRect) |
43 , fRRect(rrect) | 56 , fRRect(rrect) |
44 , fStyle(style) {} | 57 , fStyle(style) {} |
45 | 58 |
46 GrShape(const SkRect& rect, const GrStyle& style) | 59 GrShape(const SkRect& rect, const GrStyle& style) |
47 : fType(Type::kRRect) | 60 : fType(Type::kRRect) |
48 , fRRect(SkRRect::MakeRect(rect)) | 61 , fRRect(SkRRect::MakeRect(rect)) |
49 , fStyle(style) {} | 62 , fStyle(style) {} |
50 | 63 |
| 64 GrShape(const SkPath& path, const SkPaint& paint) |
| 65 : fType(Type::kPath) |
| 66 , fPath(&path) |
| 67 , fStyle(paint) { |
| 68 this->attemptToReduceFromPath(); |
| 69 } |
| 70 |
51 GrShape(const SkRRect& rrect, const SkPaint& paint) | 71 GrShape(const SkRRect& rrect, const SkPaint& paint) |
52 : fType(Type::kRRect) | 72 : fType(Type::kRRect) |
53 , fRRect(rrect) | 73 , fRRect(rrect) |
54 , fStyle(paint) {} | 74 , fStyle(paint) {} |
55 | 75 |
56 GrShape(const SkRect& rect, const SkPaint& paint) | 76 GrShape(const SkRect& rect, const SkPaint& paint) |
57 : fType(Type::kRRect) | 77 : fType(Type::kRRect) |
58 , fRRect(SkRRect::MakeRect(rect)) | 78 , fRRect(SkRRect::MakeRect(rect)) |
59 , fStyle(paint) {} | 79 , fStyle(paint) {} |
60 | 80 |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
115 int unstyledKeySize() const; | 135 int unstyledKeySize() const; |
116 | 136 |
117 /** | 137 /** |
118 * Writes unstyledKeySize() bytes into the provided pointer. Assumes that th
ere is enough | 138 * Writes unstyledKeySize() bytes into the provided pointer. Assumes that th
ere is enough |
119 * space allocated for the key and that unstyledKeySize() does not return a
negative value | 139 * space allocated for the key and that unstyledKeySize() does not return a
negative value |
120 * for this shape. | 140 * for this shape. |
121 */ | 141 */ |
122 void writeUnstyledKey(uint32_t* key) const; | 142 void writeUnstyledKey(uint32_t* key) const; |
123 | 143 |
124 private: | 144 private: |
| 145 enum class Type { |
| 146 kEmpty, |
| 147 kRRect, |
| 148 kPath, |
| 149 }; |
| 150 |
125 /** | 151 /** |
126 * Computes the key length for a GrStyle. The return will be negative if it
cannot be turned | 152 * Computes the key length for a GrStyle. The return will be negative if it
cannot be turned |
127 * into a key. | 153 * into a key. |
128 */ | 154 */ |
129 static int StyleKeySize(const GrStyle& , bool stopAfterPE); | 155 static int StyleKeySize(const GrStyle& , bool stopAfterPE); |
130 | 156 |
131 /** | 157 /** |
132 * Writes a unique key for the style into the provided buffer. This function
assumes the buffer | 158 * Writes a unique key for the style into the provided buffer. This function
assumes the buffer |
133 * has room for at least StyleKeySize() values. It assumes that StyleKeySize
() returns a | 159 * has room for at least StyleKeySize() values. It assumes that StyleKeySize
() returns a |
134 * positive value for the style and stopAfterPE param. This is written so th
at the key for just | 160 * positive value for the style and stopAfterPE param. This is written so th
at the key for just |
135 * dash application followed by the key for the remaining SkStrokeRec is the
same as the | 161 * dash application followed by the key for the remaining SkStrokeRec is the
same as the |
136 * key for applying dashing and SkStrokeRec all at once. | 162 * key for applying dashing and SkStrokeRec all at once. |
137 */ | 163 */ |
138 static void StyleKey(uint32_t*, const GrStyle&, bool stopAfterPE); | 164 static void StyleKey(uint32_t*, const GrStyle&, bool stopAfterPE); |
139 | 165 |
140 /** Constructor used by Apply* functions */ | 166 /** Constructor used by Apply* functions */ |
141 GrShape(const GrShape& parentShape, bool stopAfterPE); | 167 GrShape(const GrShape& parentShape, bool stopAfterPE); |
142 | 168 |
143 /** | 169 /** |
144 * Determines the key we should inherit from the input shape's geometry and
style when | 170 * Determines the key we should inherit from the input shape's geometry and
style when |
145 * we are applying the style to create a new shape. | 171 * we are applying the style to create a new shape. |
146 */ | 172 */ |
147 void setInheritedKey(const GrShape& parentShape, bool stopAfterPE); | 173 void setInheritedKey(const GrShape& parentShape, bool stopAfterPE); |
148 | 174 |
149 enum class Type { | 175 void attemptToReduceFromPath() { |
150 kEmpty, | 176 SkASSERT(Type::kPath == fType); |
151 kRRect, | 177 fType = AttemptToReduceFromPathImpl(*fPath.get(), &fRRect, fStyle.pathEf
fect(), |
152 kPath, | 178 fStyle.strokeRec()); |
153 } fType; | 179 if (Type::kPath != fType) { |
| 180 fPath.reset(); |
| 181 fInheritedKey.reset(0); |
| 182 } |
| 183 } |
154 | 184 |
| 185 static Type AttemptToReduceFromPathImpl(const SkPath& path, SkRRect* rrect, |
| 186 const SkPathEffect* pe, const SkStro
keRec& strokeRec) { |
| 187 if (path.isEmpty()) { |
| 188 return Type::kEmpty; |
| 189 } |
| 190 if (path.isRRect(rrect)) { |
| 191 return Type::kRRect; |
| 192 } |
| 193 SkRect rect; |
| 194 if (path.isOval(&rect)) { |
| 195 rrect->setOval(rect); |
| 196 return Type::kRRect; |
| 197 } |
| 198 bool closed; |
| 199 if (path.isRect(&rect, &closed, nullptr)) { |
| 200 if (closed || (!pe && strokeRec.isFillStyle())) { |
| 201 rrect->setRect(rect); |
| 202 return Type::kRRect; |
| 203 } |
| 204 } |
| 205 return Type::kPath; |
| 206 } |
| 207 |
| 208 Type fType; |
155 SkRRect fRRect; | 209 SkRRect fRRect; |
156 SkTLazy<SkPath> fPath; | 210 SkTLazy<SkPath> fPath; |
157 GrStyle fStyle; | 211 GrStyle fStyle; |
158 SkAutoSTArray<8, uint32_t> fInheritedKey; | 212 SkAutoSTArray<8, uint32_t> fInheritedKey; |
159 }; | 213 }; |
160 #endif | 214 #endif |
OLD | NEW |