OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef CC_PAINT_PAINT_OP_BUFFER_H_ | |
6 #define CC_PAINT_PAINT_OP_BUFFER_H_ | |
7 | |
8 #include <stdint.h> | |
9 | |
10 #include "base/logging.h" | |
11 #include "base/memory/aligned_memory.h" | |
12 #include "cc/paint/paint_canvas.h" | |
13 #include "cc/paint/paint_export.h" | |
14 #include "cc/paint/paint_flags.h" | |
15 #include "third_party/skia/include/core/SkPicture.h" | |
16 #include "third_party/skia/include/core/SkRect.h" | |
17 #include "third_party/skia/include/core/SkTextBlob.h" | |
18 | |
19 // PaintOpBuffer is a reimplementation of SkLiteDL. | |
20 // See: third_party/skia/src/core/SkLiteDL.h. | |
21 | |
22 namespace cc { | |
23 | |
24 class DisplayItemList; | |
25 | |
26 class CC_PAINT_EXPORT ThreadsafeMatrix : public SkMatrix { | |
27 public: | |
28 explicit ThreadsafeMatrix(const SkMatrix& matrix) : SkMatrix(matrix) { | |
29 (void)getType(); | |
30 } | |
31 }; | |
32 | |
33 class CC_PAINT_EXPORT ThreadsafePath : public SkPath { | |
34 public: | |
35 explicit ThreadsafePath(const SkPath& path) : SkPath(path) { | |
36 updateBoundsCache(); | |
37 } | |
38 }; | |
39 | |
40 enum class PaintOpType : uint8_t { | |
41 Annotate, | |
42 ClipPath, | |
43 ClipRect, | |
44 ClipRRect, | |
45 Concat, | |
46 DrawArc, | |
47 DrawCircle, | |
48 DrawColor, | |
49 DrawDisplayItemList, | |
50 DrawDRRect, | |
51 DrawImage, | |
52 DrawImageRect, | |
53 DrawIRect, | |
54 DrawLine, | |
55 DrawOval, | |
56 DrawPath, | |
57 DrawPosText, | |
58 DrawRecord, | |
59 DrawRect, | |
60 DrawRRect, | |
61 DrawText, | |
62 DrawTextBlob, | |
63 Noop, | |
64 Restore, | |
65 Rotate, | |
66 Save, | |
67 SaveLayer, | |
68 SaveLayerAlpha, | |
69 Scale, | |
70 SetMatrix, | |
71 Translate, | |
72 LastPaintOpType = Translate, | |
73 }; | |
74 | |
75 struct CC_PAINT_EXPORT PaintOp { | |
76 uint32_t type : 8; | |
77 uint32_t skip : 24; | |
78 | |
79 PaintOpType GetType() const { return static_cast<PaintOpType>(type); } | |
80 | |
81 void Raster(SkCanvas* canvas, const SkMatrix& original_ctm) const; | |
82 bool IsDrawOp() const; | |
83 | |
84 // Only valid for draw ops. | |
85 void RasterWithAlpha(SkCanvas* canvas, uint8_t alpha) const; | |
86 | |
87 int CountSlowPaths() const { return 0; } | |
88 | |
89 // Returns the number of bytes used by this op in referenced sub records | |
90 // and display lists. This doesn't count other objects like paths or blobs. | |
91 size_t AdditionalBytesUsed() const { return 0; } | |
92 | |
93 static constexpr bool kIsDrawOp = false; | |
94 // If an op has |kHasPaintFlags| set to true, it must: | |
95 // (1) Provide a PaintFlags member called |flags| | |
96 // (2) Provide a RasterWithFlags function instead of a Raster function. | |
97 static constexpr bool kHasPaintFlags = false; | |
98 static SkRect kUnsetRect; | |
99 }; | |
100 | |
101 struct CC_PAINT_EXPORT PaintOpWithData : PaintOp { | |
102 // Having data is just a helper for ops that have a varying amount of data and | |
103 // want a way to store that inline. This is for ops that pass in a | |
104 // void* and a length. | |
105 explicit PaintOpWithData(size_t bytes) : bytes(bytes) {} | |
106 | |
107 // Get data out by calling paint_op_data. This can't be part of the class | |
108 // because it needs to know the size of the derived type. | |
109 size_t bytes; | |
110 }; | |
111 | |
112 template <typename T> | |
113 const void* paint_op_data(const T* op) { | |
114 static_assert(std::is_convertible<T, PaintOpWithData>::value, | |
115 "T is not a PaintOpWithData"); | |
116 // Arbitrary data for a PaintOp is stored after the PaintOp itself | |
117 // in the PaintOpBuffer. Therefore, to access this data, it's | |
118 // pointer math to increment past the size of T. Accessing the | |
119 // next op in the buffer is ((char*)op) + op->skip, with the data | |
120 // fitting between. | |
121 return op + 1; | |
122 } | |
123 | |
124 template <typename T> | |
125 void* paint_op_data(T* op) { | |
126 static_assert(std::is_convertible<T, PaintOpWithData>::value, | |
127 "T is not a PaintOpWithData"); | |
128 return op + 1; | |
129 } | |
130 | |
131 struct CC_PAINT_EXPORT PaintOpWithDataArrayBase : PaintOpWithData { | |
132 // Helper class for static asserts in push functions. | |
133 using PaintOpWithData::PaintOpWithData; | |
134 }; | |
135 | |
136 template <typename T> | |
137 struct CC_PAINT_EXPORT PaintOpWithDataArray : PaintOpWithDataArrayBase { | |
138 // Paint op that has a T[count] and a char[bytes]. | |
139 PaintOpWithDataArray(size_t bytes, size_t count) | |
140 : PaintOpWithDataArrayBase(bytes), count(count) {} | |
141 // Use paint_op_array to get array data. | |
142 | |
143 size_t count; | |
144 }; | |
145 | |
146 template <typename M, typename T> | |
147 const M* paint_op_array(const T* op) { | |
148 static_assert(std::is_convertible<T, PaintOpWithDataArrayBase>::value, | |
149 "T is not a PaintOpWithDataArray"); | |
150 // See comment in paint_op_data. Array data is stored after | |
151 // any void* data. Memory layout here is: |op|data|array data|next op| | |
152 return SkTAddOffset<const M>(op + 1, op->bytes); | |
153 } | |
154 template <typename M, typename T> | |
155 M* paint_op_array(T* op) { | |
156 static_assert(std::is_convertible<T, PaintOpWithDataArrayBase>::value, | |
157 "T is not a PaintOpWithDataArray"); | |
158 return SkTAddOffset<M>(op + 1, op->bytes); | |
159 } | |
160 | |
161 struct CC_PAINT_EXPORT AnnotateOp final : PaintOp { | |
162 enum class AnnotationType { | |
163 URL, | |
164 LinkToDestination, | |
165 NamedDestination, | |
166 }; | |
167 | |
168 static constexpr PaintOpType kType = PaintOpType::Annotate; | |
169 AnnotateOp(PaintCanvas::AnnotationType annotation_type, | |
170 const SkRect& rect, | |
171 sk_sp<SkData> data); | |
172 ~AnnotateOp(); | |
173 void Raster(SkCanvas* canvas) const; | |
174 | |
175 PaintCanvas::AnnotationType annotation_type; | |
176 SkRect rect; | |
177 sk_sp<SkData> data; | |
178 }; | |
179 | |
180 struct CC_PAINT_EXPORT ClipPathOp final : PaintOp { | |
181 static constexpr PaintOpType kType = PaintOpType::ClipPath; | |
182 ClipPathOp(SkPath path, SkClipOp op, bool antialias) | |
183 : path(path), op(op), antialias(antialias) {} | |
184 void Raster(SkCanvas* canvas) const; | |
185 int CountSlowPaths() const; | |
186 | |
187 ThreadsafePath path; | |
188 SkClipOp op; | |
189 bool antialias; | |
190 }; | |
191 | |
192 struct CC_PAINT_EXPORT ClipRectOp final : PaintOp { | |
193 static constexpr PaintOpType kType = PaintOpType::ClipRect; | |
194 ClipRectOp(const SkRect& rect, SkClipOp op, bool antialias) | |
195 : rect(rect), op(op), antialias(antialias) {} | |
196 void Raster(SkCanvas* canvas) const; | |
197 | |
198 SkRect rect; | |
199 SkClipOp op; | |
200 bool antialias; | |
201 }; | |
202 | |
203 struct CC_PAINT_EXPORT ClipRRectOp final : PaintOp { | |
204 static constexpr PaintOpType kType = PaintOpType::ClipRRect; | |
205 ClipRRectOp(const SkRRect& rrect, SkClipOp op, bool antialias) | |
206 : rrect(rrect), op(op), antialias(antialias) {} | |
207 void Raster(SkCanvas* canvas) const; | |
208 | |
209 SkRRect rrect; | |
210 SkClipOp op; | |
211 bool antialias; | |
212 }; | |
213 | |
214 struct CC_PAINT_EXPORT ConcatOp final : PaintOp { | |
215 static constexpr PaintOpType kType = PaintOpType::Concat; | |
216 explicit ConcatOp(const SkMatrix& matrix) : matrix(matrix) {} | |
217 void Raster(SkCanvas* canvas) const; | |
218 | |
219 ThreadsafeMatrix matrix; | |
220 }; | |
221 | |
222 struct CC_PAINT_EXPORT DrawArcOp final : PaintOp { | |
223 static constexpr PaintOpType kType = PaintOpType::DrawArc; | |
224 static constexpr bool kIsDrawOp = true; | |
225 static constexpr bool kHasPaintFlags = true; | |
226 DrawArcOp(const SkRect& oval, | |
227 SkScalar start_angle, | |
228 SkScalar sweep_angle, | |
229 bool use_center, | |
230 const PaintFlags& flags) | |
231 : oval(oval), | |
232 start_angle(start_angle), | |
233 sweep_angle(sweep_angle), | |
234 use_center(use_center), | |
235 flags(flags) {} | |
236 void RasterWithFlags(SkCanvas* canvas, const PaintFlags& flags) const; | |
237 | |
238 SkRect oval; | |
239 SkScalar start_angle; | |
240 SkScalar sweep_angle; | |
241 bool use_center; | |
242 PaintFlags flags; | |
243 }; | |
244 | |
245 struct CC_PAINT_EXPORT DrawCircleOp final : PaintOp { | |
246 static constexpr PaintOpType kType = PaintOpType::DrawCircle; | |
247 static constexpr bool kIsDrawOp = true; | |
248 static constexpr bool kHasPaintFlags = true; | |
249 DrawCircleOp(SkScalar cx, | |
250 SkScalar cy, | |
251 SkScalar radius, | |
252 const PaintFlags& flags) | |
253 : cx(cx), cy(cy), radius(radius), flags(flags) {} | |
254 void RasterWithFlags(SkCanvas* canvas, const PaintFlags& flags) const; | |
255 | |
256 SkScalar cx; | |
257 SkScalar cy; | |
258 SkScalar radius; | |
259 PaintFlags flags; | |
260 }; | |
261 | |
262 struct CC_PAINT_EXPORT DrawColorOp final : PaintOp { | |
263 static constexpr PaintOpType kType = PaintOpType::DrawColor; | |
264 static constexpr bool kIsDrawOp = true; | |
265 DrawColorOp(SkColor color, SkBlendMode mode) : color(color), mode(mode) {} | |
266 void Raster(SkCanvas* canvas) const; | |
267 | |
268 SkColor color; | |
269 SkBlendMode mode; | |
270 }; | |
271 | |
272 struct CC_PAINT_EXPORT DrawDisplayItemListOp final : PaintOp { | |
273 static constexpr PaintOpType kType = PaintOpType::DrawDisplayItemList; | |
274 static constexpr bool kIsDrawOp = true; | |
275 explicit DrawDisplayItemListOp(scoped_refptr<DisplayItemList> list); | |
276 // Windows wants to generate these when types are exported, so | |
277 // provide them here explicitly so that DisplayItemList doesn't have | |
278 // to be defined in this header. | |
279 DrawDisplayItemListOp(const DrawDisplayItemListOp& op); | |
280 DrawDisplayItemListOp& operator=(const DrawDisplayItemListOp& op); | |
281 ~DrawDisplayItemListOp(); | |
282 void Raster(SkCanvas* canvas) const; | |
283 size_t AdditionalBytesUsed() const; | |
284 // TODO(enne): DisplayItemList should know number of slow paths. | |
285 | |
286 scoped_refptr<DisplayItemList> list; | |
287 }; | |
288 | |
289 struct CC_PAINT_EXPORT DrawDRRectOp final : PaintOp { | |
290 static constexpr PaintOpType kType = PaintOpType::DrawDRRect; | |
291 static constexpr bool kIsDrawOp = true; | |
292 static constexpr bool kHasPaintFlags = true; | |
293 DrawDRRectOp(const SkRRect& outer, | |
294 const SkRRect& inner, | |
295 const PaintFlags& flags) | |
296 : outer(outer), inner(inner), flags(flags) {} | |
297 void RasterWithFlags(SkCanvas* canvas, const PaintFlags& flags) const; | |
298 | |
299 SkRRect outer; | |
300 SkRRect inner; | |
301 PaintFlags flags; | |
302 }; | |
303 | |
304 struct CC_PAINT_EXPORT DrawImageOp final : PaintOp { | |
305 static constexpr PaintOpType kType = PaintOpType::DrawImage; | |
306 static constexpr bool kIsDrawOp = true; | |
307 static constexpr bool kHasPaintFlags = true; | |
308 DrawImageOp(const PaintImage& image, | |
309 SkScalar left, | |
310 SkScalar top, | |
311 const PaintFlags* flags); | |
312 ~DrawImageOp(); | |
313 void RasterWithFlags(SkCanvas* canvas, const PaintFlags& flags) const; | |
314 | |
315 PaintImage image; | |
316 SkScalar left; | |
317 SkScalar top; | |
318 PaintFlags flags; | |
319 }; | |
320 | |
321 struct CC_PAINT_EXPORT DrawImageRectOp final : PaintOp { | |
322 static constexpr PaintOpType kType = PaintOpType::DrawImageRect; | |
323 static constexpr bool kIsDrawOp = true; | |
324 static constexpr bool kHasPaintFlags = true; | |
325 DrawImageRectOp(const PaintImage& image, | |
326 const SkRect& src, | |
327 const SkRect& dst, | |
328 const PaintFlags* flags, | |
329 PaintCanvas::SrcRectConstraint constraint); | |
330 ~DrawImageRectOp(); | |
331 void RasterWithFlags(SkCanvas* canvas, const PaintFlags& flags) const; | |
332 | |
333 PaintImage image; | |
334 PaintFlags flags; | |
335 SkRect src; | |
336 SkRect dst; | |
337 PaintCanvas::SrcRectConstraint constraint; | |
338 }; | |
339 | |
340 struct CC_PAINT_EXPORT DrawIRectOp final : PaintOp { | |
341 static constexpr PaintOpType kType = PaintOpType::DrawIRect; | |
342 static constexpr bool kIsDrawOp = true; | |
343 static constexpr bool kHasPaintFlags = true; | |
344 DrawIRectOp(const SkIRect& rect, const PaintFlags& flags) | |
345 : rect(rect), flags(flags) {} | |
346 void RasterWithFlags(SkCanvas* canvas, const PaintFlags& flags) const; | |
347 | |
348 SkIRect rect; | |
349 PaintFlags flags; | |
350 }; | |
351 | |
352 struct CC_PAINT_EXPORT DrawLineOp final : PaintOp { | |
353 static constexpr PaintOpType kType = PaintOpType::DrawLine; | |
354 static constexpr bool kIsDrawOp = true; | |
355 static constexpr bool kHasPaintFlags = true; | |
356 DrawLineOp(SkScalar x0, | |
357 SkScalar y0, | |
358 SkScalar x1, | |
359 SkScalar y1, | |
360 const PaintFlags& flags) | |
361 : x0(x0), y0(y0), x1(x1), y1(y1), flags(flags) {} | |
362 void RasterWithFlags(SkCanvas* canvas, const PaintFlags& flags) const; | |
363 int CountSlowPaths() const; | |
364 | |
365 SkScalar x0; | |
366 SkScalar y0; | |
367 SkScalar x1; | |
368 SkScalar y1; | |
369 PaintFlags flags; | |
370 }; | |
371 | |
372 struct CC_PAINT_EXPORT DrawOvalOp final : PaintOp { | |
373 static constexpr PaintOpType kType = PaintOpType::DrawOval; | |
374 static constexpr bool kIsDrawOp = true; | |
375 static constexpr bool kHasPaintFlags = true; | |
376 DrawOvalOp(const SkRect& oval, const PaintFlags& flags) | |
377 : oval(oval), flags(flags) {} | |
378 void RasterWithFlags(SkCanvas* canvas, const PaintFlags& flags) const; | |
379 | |
380 SkRect oval; | |
381 PaintFlags flags; | |
382 }; | |
383 | |
384 struct CC_PAINT_EXPORT DrawPathOp final : PaintOp { | |
385 static constexpr PaintOpType kType = PaintOpType::DrawPath; | |
386 static constexpr bool kIsDrawOp = true; | |
387 static constexpr bool kHasPaintFlags = true; | |
388 DrawPathOp(const SkPath& path, const PaintFlags& flags) | |
389 : path(path), flags(flags) {} | |
390 void RasterWithFlags(SkCanvas* canvas, const PaintFlags& flags) const; | |
391 int CountSlowPaths() const; | |
392 | |
393 ThreadsafePath path; | |
394 PaintFlags flags; | |
395 }; | |
396 | |
397 struct CC_PAINT_EXPORT DrawPosTextOp final : PaintOpWithDataArray<SkPoint> { | |
398 static constexpr PaintOpType kType = PaintOpType::DrawPosText; | |
399 static constexpr bool kIsDrawOp = true; | |
400 static constexpr bool kHasPaintFlags = true; | |
401 DrawPosTextOp(size_t bytes, size_t count, const PaintFlags& flags); | |
402 ~DrawPosTextOp(); | |
403 void RasterWithFlags(SkCanvas* canvas, const PaintFlags& flags) const; | |
404 | |
405 PaintFlags flags; | |
406 }; | |
407 | |
408 struct CC_PAINT_EXPORT DrawRecordOp final : PaintOp { | |
409 static constexpr PaintOpType kType = PaintOpType::DrawRecord; | |
410 static constexpr bool kIsDrawOp = true; | |
411 explicit DrawRecordOp(sk_sp<const PaintRecord> record); | |
412 ~DrawRecordOp(); | |
413 void Raster(SkCanvas* canvas) const; | |
414 size_t AdditionalBytesUsed() const; | |
415 | |
416 sk_sp<const PaintRecord> record; | |
417 }; | |
418 | |
419 struct CC_PAINT_EXPORT DrawRectOp final : PaintOp { | |
420 static constexpr PaintOpType kType = PaintOpType::DrawRect; | |
421 static constexpr bool kIsDrawOp = true; | |
422 static constexpr bool kHasPaintFlags = true; | |
423 DrawRectOp(const SkRect& rect, const PaintFlags& flags) | |
424 : rect(rect), flags(flags) {} | |
425 void RasterWithFlags(SkCanvas* canvas, const PaintFlags& flags) const; | |
426 | |
427 SkRect rect; | |
428 PaintFlags flags; | |
429 }; | |
430 | |
431 struct CC_PAINT_EXPORT DrawRRectOp final : PaintOp { | |
432 static constexpr PaintOpType kType = PaintOpType::DrawRRect; | |
433 static constexpr bool kIsDrawOp = true; | |
434 static constexpr bool kHasPaintFlags = true; | |
435 DrawRRectOp(const SkRRect& rrect, const PaintFlags& flags) | |
436 : rrect(rrect), flags(flags) {} | |
437 void RasterWithFlags(SkCanvas* canvas, const PaintFlags& flags) const; | |
438 | |
439 SkRRect rrect; | |
440 PaintFlags flags; | |
441 }; | |
442 | |
443 struct CC_PAINT_EXPORT DrawTextOp final : PaintOpWithData { | |
444 static constexpr PaintOpType kType = PaintOpType::DrawText; | |
445 static constexpr bool kIsDrawOp = true; | |
446 static constexpr bool kHasPaintFlags = true; | |
447 DrawTextOp(size_t bytes, SkScalar x, SkScalar y, const PaintFlags& flags) | |
448 : PaintOpWithData(bytes), x(x), y(y), flags(flags) {} | |
449 void RasterWithFlags(SkCanvas* canvas, const PaintFlags& flags) const; | |
450 | |
451 SkScalar x; | |
452 SkScalar y; | |
453 PaintFlags flags; | |
454 }; | |
455 | |
456 struct CC_PAINT_EXPORT DrawTextBlobOp final : PaintOp { | |
457 static constexpr PaintOpType kType = PaintOpType::DrawTextBlob; | |
458 static constexpr bool kIsDrawOp = true; | |
459 static constexpr bool kHasPaintFlags = true; | |
460 DrawTextBlobOp(sk_sp<SkTextBlob> blob, | |
461 SkScalar x, | |
462 SkScalar y, | |
463 const PaintFlags& flags); | |
464 ~DrawTextBlobOp(); | |
465 void RasterWithFlags(SkCanvas* canvas, const PaintFlags& flags) const; | |
466 | |
467 sk_sp<SkTextBlob> blob; | |
468 SkScalar x; | |
469 SkScalar y; | |
470 PaintFlags flags; | |
471 }; | |
472 | |
473 struct CC_PAINT_EXPORT NoopOp final : PaintOp { | |
474 static constexpr PaintOpType kType = PaintOpType::Noop; | |
475 void Raster(SkCanvas* canvas) const {} | |
476 }; | |
477 | |
478 struct CC_PAINT_EXPORT RestoreOp final : PaintOp { | |
479 static constexpr PaintOpType kType = PaintOpType::Restore; | |
480 void Raster(SkCanvas* canvas) const; | |
481 }; | |
482 | |
483 struct CC_PAINT_EXPORT RotateOp final : PaintOp { | |
484 static constexpr PaintOpType kType = PaintOpType::Rotate; | |
485 explicit RotateOp(SkScalar degrees) : degrees(degrees) {} | |
486 void Raster(SkCanvas* canvas) const; | |
487 | |
488 SkScalar degrees; | |
489 }; | |
490 | |
491 struct CC_PAINT_EXPORT SaveOp final : PaintOp { | |
492 static constexpr PaintOpType kType = PaintOpType::Save; | |
493 void Raster(SkCanvas* canvas) const; | |
494 }; | |
495 | |
496 struct CC_PAINT_EXPORT SaveLayerOp final : PaintOp { | |
497 static constexpr PaintOpType kType = PaintOpType::SaveLayer; | |
498 static constexpr bool kHasPaintFlags = true; | |
499 SaveLayerOp(const SkRect* bounds, const PaintFlags* flags) | |
500 : bounds(bounds ? *bounds : kUnsetRect) { | |
501 if (flags) | |
502 this->flags = *flags; | |
503 } | |
504 void RasterWithFlags(SkCanvas* canvas, const PaintFlags& flags) const; | |
505 | |
506 SkRect bounds; | |
507 PaintFlags flags; | |
508 }; | |
509 | |
510 struct CC_PAINT_EXPORT SaveLayerAlphaOp final : PaintOp { | |
511 static constexpr PaintOpType kType = PaintOpType::SaveLayerAlpha; | |
512 SaveLayerAlphaOp(const SkRect* bounds, uint8_t alpha) | |
513 : bounds(bounds ? *bounds : kUnsetRect), alpha(alpha) {} | |
514 void Raster(SkCanvas* canvas) const; | |
515 | |
516 SkRect bounds; | |
517 uint8_t alpha; | |
518 }; | |
519 | |
520 struct CC_PAINT_EXPORT ScaleOp final : PaintOp { | |
521 static constexpr PaintOpType kType = PaintOpType::Scale; | |
522 ScaleOp(SkScalar sx, SkScalar sy) : sx(sx), sy(sy) {} | |
523 void Raster(SkCanvas* canvas) const; | |
524 | |
525 SkScalar sx; | |
526 SkScalar sy; | |
527 }; | |
528 | |
529 struct CC_PAINT_EXPORT SetMatrixOp final : PaintOp { | |
530 static constexpr PaintOpType kType = PaintOpType::SetMatrix; | |
531 explicit SetMatrixOp(const SkMatrix& matrix) : matrix(matrix) {} | |
532 // This is the only op that needs the original ctm of the SkCanvas | |
533 // used for raster (since SetMatrix is relative to the recording origin and | |
534 // shouldn't clobber the SkCanvas raster origin). | |
535 // | |
536 // TODO(enne): Find some cleaner way to do this, possibly by making | |
537 // all SetMatrix calls Concat?? | |
538 void Raster(SkCanvas* canvas, const SkMatrix& original_ctm) const; | |
539 | |
540 ThreadsafeMatrix matrix; | |
541 }; | |
542 | |
543 struct CC_PAINT_EXPORT TranslateOp final : PaintOp { | |
544 static constexpr PaintOpType kType = PaintOpType::Translate; | |
545 TranslateOp(SkScalar dx, SkScalar dy) : dx(dx), dy(dy) {} | |
546 void Raster(SkCanvas* canvas) const; | |
547 | |
548 SkScalar dx; | |
549 SkScalar dy; | |
550 }; | |
551 | |
552 using LargestPaintOp = DrawDRRectOp; | |
553 | |
554 class CC_PAINT_EXPORT PaintOpBuffer : public SkRefCnt { | |
555 public: | |
556 enum { kInitialBufferSize = 4096 }; | |
557 | |
558 PaintOpBuffer(); | |
559 explicit PaintOpBuffer(const SkRect& cull_rect); | |
560 ~PaintOpBuffer() override; | |
561 | |
562 void Reset(); | |
563 | |
564 void playback(SkCanvas* canvas) const; | |
565 void playback(SkCanvas* canvas, SkPicture::AbortCallback* callback) const; | |
566 | |
567 // TODO(enne): These are no longer approximate. Rename these. | |
568 int approximateOpCount() const { return op_count_; } | |
569 size_t approximateBytesUsed() const { | |
570 return sizeof(*this) + reserved_ + subrecord_bytes_used_; | |
571 } | |
572 int numSlowPaths() const { return num_slow_paths_; } | |
573 | |
574 // Resize the PaintOpBuffer to exactly fit the current amount of used space. | |
575 void ShrinkToFit(); | |
576 | |
577 const SkRect& cullRect() const { return cull_rect_; } | |
578 | |
579 PaintOp* GetFirstOp() const { | |
580 return const_cast<PaintOp*>(first_op_.data_as<PaintOp>()); | |
581 } | |
582 | |
583 template <typename T, typename... Args> | |
584 void push(Args&&... args) { | |
585 static_assert(std::is_convertible<T, PaintOp>::value, "T not a PaintOp."); | |
586 static_assert(!std::is_convertible<T, PaintOpWithData>::value, | |
587 "Type needs to use push_with_data"); | |
588 push_internal<T>(0, std::forward<Args>(args)...); | |
589 } | |
590 | |
591 template <typename T, typename... Args> | |
592 void push_with_data(const void* data, size_t bytes, Args&&... args) { | |
593 static_assert(std::is_convertible<T, PaintOpWithData>::value, | |
594 "T is not a PaintOpWithData"); | |
595 #if !defined(OS_CHROMEOS) | |
596 // TODO(enne): non-linux chromeos builds think that DrawTextOp | |
597 // can be converted to a PaintOpWithDataArrayBase. OOPS. | |
598 static_assert(!std::is_convertible<T, PaintOpWithDataArrayBase>::value, | |
599 "Type needs to use push_with_data_array"); | |
600 #endif | |
601 DCHECK_GE(bytes, 0u); | |
602 T* op = push_internal<T>(bytes, bytes, std::forward<Args>(args)...); | |
603 memcpy(paint_op_data(op), data, bytes); | |
604 | |
605 #if DCHECK_IS_ON() | |
606 // Double check the data fits between op and next op and doesn't clobber. | |
607 char* op_start = reinterpret_cast<char*>(op); | |
608 char* op_end = op_start + sizeof(T); | |
609 char* next_op = op_start + op->skip; | |
610 char* data_start = reinterpret_cast<char*>(paint_op_data(op)); | |
611 char* data_end = data_start + bytes; | |
612 DCHECK_GE(data_start, op_end); | |
613 DCHECK_LT(data_start, next_op); | |
614 DCHECK_LE(data_end, next_op); | |
615 #endif | |
616 } | |
617 | |
618 template <typename T, typename M, typename... Args> | |
619 void push_with_data_array(const void* data, | |
620 size_t bytes, | |
621 const M* array, | |
622 size_t count, | |
623 Args&&... args) { | |
624 static_assert(std::is_convertible<T, PaintOpWithDataArray<M>>::value, | |
625 "T is not a PaintOpWithDataArray"); | |
626 DCHECK_GE(bytes, 0u); | |
627 DCHECK_GE(count, 0u); | |
628 size_t array_size = sizeof(M) * count; | |
629 size_t total_size = bytes + array_size; | |
630 T* op = | |
631 push_internal<T>(total_size, bytes, count, std::forward<Args>(args)...); | |
632 memcpy(paint_op_data(op), data, bytes); | |
633 memcpy(paint_op_array<M>(op), array, array_size); | |
634 | |
635 #if DCHECK_IS_ON() | |
636 // Double check data and array don't clobber op, next op, or each other | |
637 char* op_start = reinterpret_cast<char*>(op); | |
638 char* op_end = op_start + sizeof(T); | |
639 char* next_op = op_start + op->skip; | |
640 char* data_start = reinterpret_cast<char*>(paint_op_data(op)); | |
641 char* data_end = data_start + bytes; | |
642 char* array_start = reinterpret_cast<char*>(paint_op_array<M>(op)); | |
643 char* array_end = array_start + array_size; | |
644 DCHECK_GE(data_start, op_end); | |
645 DCHECK_LE(data_start, array_start); | |
646 DCHECK_GE(array_start, data_end); | |
647 DCHECK_LE(array_end, next_op); | |
648 #endif | |
649 } | |
650 | |
651 class Iterator { | |
652 public: | |
653 explicit Iterator(const PaintOpBuffer* buffer) | |
654 : buffer_(buffer), ptr_(buffer_->data_.get()) {} | |
655 | |
656 PaintOp* operator->() const { | |
657 return op_idx_ ? reinterpret_cast<PaintOp*>(ptr_) : buffer_->GetFirstOp(); | |
658 } | |
659 PaintOp* operator*() const { return operator->(); } | |
660 Iterator begin() { return Iterator(buffer_, buffer_->data_.get(), 0); } | |
661 Iterator end() { | |
662 return Iterator(buffer_, buffer_->data_.get() + buffer_->used_, | |
663 buffer_->approximateOpCount()); | |
664 } | |
665 bool operator!=(const Iterator& other) { | |
666 // Not valid to compare iterators on different buffers. | |
667 DCHECK_EQ(other.buffer_, buffer_); | |
668 return other.op_idx_ != op_idx_; | |
669 } | |
670 Iterator& operator++() { | |
671 if (!op_idx_++) | |
672 return *this; | |
673 PaintOp* op = **this; | |
674 uint32_t type = op->type; | |
675 CHECK_LE(type, static_cast<uint32_t>(PaintOpType::LastPaintOpType)); | |
676 ptr_ += op->skip; | |
677 return *this; | |
678 } | |
679 operator bool() const { return op_idx_ < buffer_->approximateOpCount(); } | |
680 | |
681 int op_idx() const { return op_idx_; } | |
682 | |
683 // Return the next op without advancing the iterator, or nullptr if none. | |
684 PaintOp* peek1() const { | |
685 if (op_idx_ + 1 >= buffer_->approximateOpCount()) | |
686 return nullptr; | |
687 if (!op_idx_) | |
688 return reinterpret_cast<PaintOp*>(ptr_); | |
689 return reinterpret_cast<PaintOp*>(ptr_ + (*this)->skip); | |
690 } | |
691 | |
692 // Return the op two ops ahead without advancing the iterator, or nullptr if | |
693 // none. | |
694 PaintOp* peek2() const { | |
695 if (op_idx_ + 2 >= buffer_->approximateOpCount()) | |
696 return nullptr; | |
697 char* next = ptr_ + reinterpret_cast<PaintOp*>(ptr_)->skip; | |
698 PaintOp* next_op = reinterpret_cast<PaintOp*>(next); | |
699 if (!op_idx_) | |
700 return next_op; | |
701 return reinterpret_cast<PaintOp*>(next + next_op->skip); | |
702 } | |
703 | |
704 private: | |
705 Iterator(const PaintOpBuffer* buffer, char* ptr, int op_idx) | |
706 : buffer_(buffer), ptr_(ptr), op_idx_(op_idx) {} | |
707 | |
708 const PaintOpBuffer* buffer_ = nullptr; | |
709 char* ptr_ = nullptr; | |
710 int op_idx_ = 0; | |
711 }; | |
712 | |
713 private: | |
714 template <typename T, bool HasFlags> | |
715 struct CountSlowPathsFromFlags { | |
716 static int Count(const T* op) { return 0; } | |
717 }; | |
718 | |
719 template <typename T> | |
720 struct CountSlowPathsFromFlags<T, true> { | |
721 static int Count(const T* op) { return op->flags.getPathEffect() ? 1 : 0; } | |
722 }; | |
723 | |
724 template <typename T, typename... Args> | |
725 T* push_internal(size_t bytes, Args&&... args) { | |
726 size_t skip = SkAlignPtr(sizeof(T) + bytes); | |
727 DCHECK_LT(skip, static_cast<size_t>(1) << 24); | |
728 if (used_ + skip > reserved_ || !op_count_) { | |
729 if (!op_count_) { | |
730 if (bytes) { | |
731 // Internal first_op buffer doesn't have room for extra data. | |
732 // If the op wants extra bytes, then we'll just store a Noop | |
733 // in the first_op and proceed from there. This seems unlikely | |
734 // to be a common case. | |
735 push<NoopOp>(); | |
736 } else { | |
737 // |first_op_| is aligned to LargestPaintOp. If T needs a smaller | |
738 // alignment, this is okay because it will be a factor of the actual | |
739 // alignment being used (as they are always a power of 2). If T needs | |
740 // a larger alignment, that is bad and we should use T to choose the | |
741 // alignment of |first_op_| instead. | |
742 static_assert(ALIGNOF(T) <= ALIGNOF(LargestPaintOp), ""); | |
743 auto* op = reinterpret_cast<T*>(first_op_.data_as<T>()); | |
744 new (op) T{std::forward<Args>(args)...}; | |
745 op->type = static_cast<uint32_t>(T::kType); | |
746 op->skip = 0; | |
747 AnalyzeAddedOp(op); | |
enne (OOO)
2017/04/25 19:47:14
Added khushal's AnalyzeAddedOp function from https
| |
748 op_count_++; | |
749 return op; | |
750 } | |
751 } | |
752 | |
753 // Start reserved_ at kInitialBufferSize and then double. | |
754 // ShrinkToFit can make this smaller afterwards. | |
755 while (used_ + skip > reserved_) | |
756 reserved_ = reserved_ ? reserved_ * 2 : kInitialBufferSize; | |
757 data_.realloc(reserved_); | |
758 } | |
759 DCHECK_LE(used_ + skip, reserved_); | |
760 | |
761 T* op = reinterpret_cast<T*>(data_.get() + used_); | |
762 used_ += skip; | |
763 new (op) T(std::forward<Args>(args)...); | |
764 op->type = static_cast<uint32_t>(T::kType); | |
765 op->skip = skip; | |
766 AnalyzeAddedOp(op); | |
767 op_count_++; | |
768 return op; | |
769 } | |
770 | |
771 template <typename T> | |
772 void AnalyzeAddedOp(const T* op) { | |
773 num_slow_paths_ += CountSlowPathsFromFlags<T, T::kHasPaintFlags>::Count(op); | |
774 num_slow_paths_ += op->CountSlowPaths(); | |
775 | |
776 subrecord_bytes_used_ += op->AdditionalBytesUsed(); | |
777 } | |
778 | |
779 // As a performance optimization because n=1 is an extremely common case just | |
780 // store the first op in the PaintOpBuffer itself to avoid an extra alloc. | |
781 base::AlignedMemory<sizeof(LargestPaintOp), ALIGNOF(LargestPaintOp)> | |
782 first_op_; | |
783 SkAutoTMalloc<char> data_; | |
784 size_t used_ = 0; | |
785 size_t reserved_ = 0; | |
786 int op_count_ = 0; | |
787 | |
788 // Record paths for veto-to-msaa for gpu raster. | |
789 int num_slow_paths_ = 0; | |
790 // Record additional bytes used by referenced sub-records and display lists. | |
791 size_t subrecord_bytes_used_ = 0; | |
792 SkRect cull_rect_; | |
793 | |
794 DISALLOW_COPY_AND_ASSIGN(PaintOpBuffer); | |
795 }; | |
796 | |
797 } // namespace cc | |
798 | |
799 #endif // CC_PAINT_PAINT_OP_BUFFER_H_ | |
OLD | NEW |