paint_worklet_input.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. // Copyright 2018 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_PAINT_WORKLET_INPUT_H_
  5. #define CC_PAINT_PAINT_WORKLET_INPUT_H_
  6. #include <string>
  7. #include <utility>
  8. #include <vector>
  9. #include "base/containers/flat_map.h"
  10. #include "base/memory/ref_counted.h"
  11. #include "cc/paint/element_id.h"
  12. #include "cc/paint/paint_export.h"
  13. #include "cc/paint/paint_image.h"
  14. #include "third_party/abseil-cpp/absl/types/optional.h"
  15. #include "third_party/skia/include/core/SkColor.h"
  16. #include "third_party/skia/include/core/SkRefCnt.h"
  17. #include "ui/gfx/geometry/size_f.h"
  18. namespace cc {
  19. class PaintOpBuffer;
  20. using PaintRecord = PaintOpBuffer;
  21. class CC_PAINT_EXPORT PaintWorkletInput
  22. : public base::RefCountedThreadSafe<PaintWorkletInput> {
  23. public:
  24. enum class NativePropertyType {
  25. kBackgroundColor,
  26. kClipPath,
  27. kInvalid,
  28. };
  29. // Uniquely identifies a property from the animation system, so that a
  30. // PaintWorkletInput can specify the properties it depends on to be painted
  31. // (and for which it must be repainted if their values change).
  32. //
  33. // PropertyKey is designed to support both native and custom properties.
  34. // 1. Custom properties: The same ElementId will be produced for all custom
  35. // properties for a given element. As such we require the custom property
  36. // name as an additional key to uniquely identify custom properties.
  37. // 2. Native properties: When fetching the current value of a native
  38. // property from property tree, we need the ElementId, plus knowing which
  39. // tree to fetch the value from, and that's why we need the
  40. // |native_property_type|.
  41. // One property key should have either |custom_property_name| or
  42. // |native_property_type|, and should never have both or neither.
  43. struct CC_PAINT_EXPORT PropertyKey {
  44. PropertyKey(const std::string& custom_property_name, ElementId element_id);
  45. PropertyKey(NativePropertyType native_property_type, ElementId element_id);
  46. PropertyKey(const PropertyKey&);
  47. ~PropertyKey();
  48. bool operator==(const PropertyKey& other) const;
  49. bool operator!=(const PropertyKey& other) const;
  50. bool operator<(const PropertyKey&) const;
  51. absl::optional<std::string> custom_property_name;
  52. absl::optional<NativePropertyType> native_property_type;
  53. ElementId element_id;
  54. };
  55. // A structure that can hold either a float or color type value, depending
  56. // on the type of custom property. Only one of |float_val| and |color_val|
  57. // should hold value at any time; if neither hold value then we should not use
  58. // this value.
  59. // This structure is needed so that PaintWorkletProxyClient::Paint can handle
  60. // both color and float type custom property values at the same time.
  61. struct CC_PAINT_EXPORT PropertyValue {
  62. PropertyValue();
  63. explicit PropertyValue(float value);
  64. explicit PropertyValue(SkColor4f value);
  65. PropertyValue(const PropertyValue&);
  66. ~PropertyValue();
  67. bool has_value() const;
  68. void reset();
  69. absl::optional<float> float_value;
  70. absl::optional<SkColor4f> color_value;
  71. };
  72. virtual gfx::SizeF GetSize() const = 0;
  73. virtual int WorkletId() const = 0;
  74. // Return the list of properties used as an input by this PaintWorkletInput.
  75. // The values for these properties must be provided when dispatching a worklet
  76. // job for this input.
  77. using PropertyKeys = std::vector<PropertyKey>;
  78. virtual const PropertyKeys& GetPropertyKeys() const = 0;
  79. virtual bool IsCSSPaintWorkletInput() const = 0;
  80. // True if all the animated frames are opaque. Can be false only if animated
  81. // frames are colors.
  82. virtual bool KnownToBeOpaque() const;
  83. protected:
  84. friend class base::RefCountedThreadSafe<PaintWorkletInput>;
  85. virtual ~PaintWorkletInput() = default;
  86. };
  87. // PaintWorkletRecordMap ties the input for a PaintWorklet (PaintWorkletInput)
  88. // to the painted output (a PaintRecord). It also stores the PaintImage::Id for
  89. // the PaintWorklet to enable efficient invalidation of dirty PaintWorklets.
  90. using PaintWorkletRecordMap =
  91. base::flat_map<scoped_refptr<const PaintWorkletInput>,
  92. std::pair<PaintImage::Id, sk_sp<PaintRecord>>>;
  93. } // namespace cc
  94. #endif // CC_PAINT_PAINT_WORKLET_INPUT_H_