scoped_raster_flags.h 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. #ifndef CC_PAINT_SCOPED_RASTER_FLAGS_H_
  5. #define CC_PAINT_SCOPED_RASTER_FLAGS_H_
  6. #include "base/containers/stack_container.h"
  7. #include "cc/paint/decode_stashing_image_provider.h"
  8. #include "cc/paint/paint_export.h"
  9. #include "cc/paint/paint_flags.h"
  10. #include "third_party/abseil-cpp/absl/types/optional.h"
  11. namespace cc {
  12. // A helper class to modify the flags for raster. This includes alpha folding
  13. // from SaveLayers and decoding images.
  14. class CC_PAINT_EXPORT ScopedRasterFlags {
  15. public:
  16. // |flags| and |image_provider| must outlive this class.
  17. template <class F, class = std::enable_if_t<std::is_same_v<F, float>>>
  18. ScopedRasterFlags(const PaintFlags* flags,
  19. ImageProvider* image_provider,
  20. const SkMatrix& ctm,
  21. int max_texture_size,
  22. F alpha)
  23. : original_flags_(flags) {
  24. if (image_provider) {
  25. decode_stashing_image_provider_.emplace(image_provider);
  26. // We skip the op if any images fail to decode.
  27. DecodeImageShader(ctm);
  28. if (decode_failed_)
  29. return;
  30. DecodeRecordShader(ctm, max_texture_size);
  31. if (decode_failed_)
  32. return;
  33. DecodeFilter();
  34. if (decode_failed_)
  35. return;
  36. }
  37. if (alpha != 1.0f) {
  38. DCHECK(flags->SupportsFoldingAlpha());
  39. MutableFlags()->setAlphaf(flags->getAlphaf() * alpha);
  40. }
  41. AdjustStrokeIfNeeded(ctm);
  42. }
  43. ScopedRasterFlags(const ScopedRasterFlags&) = delete;
  44. ~ScopedRasterFlags();
  45. ScopedRasterFlags& operator=(const ScopedRasterFlags&) = delete;
  46. // The usage of these flags should not extend beyond the lifetime of this
  47. // object.
  48. const PaintFlags* flags() const {
  49. if (decode_failed_)
  50. return nullptr;
  51. return modified_flags_ ? &*modified_flags_ : original_flags_;
  52. }
  53. private:
  54. void DecodeImageShader(const SkMatrix& ctm);
  55. void DecodeRecordShader(const SkMatrix& ctm, int max_texture_size);
  56. void DecodeFilter();
  57. void AdjustStrokeIfNeeded(const SkMatrix& ctm);
  58. PaintFlags* MutableFlags() {
  59. if (!modified_flags_)
  60. modified_flags_.emplace(*original_flags_);
  61. return &*modified_flags_;
  62. }
  63. const PaintFlags* original_flags_;
  64. absl::optional<PaintFlags> modified_flags_;
  65. absl::optional<DecodeStashingImageProvider> decode_stashing_image_provider_;
  66. bool decode_failed_ = false;
  67. };
  68. } // namespace cc
  69. #endif // CC_PAINT_SCOPED_RASTER_FLAGS_H_