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