#include "cc/trees/animated_paint_worklet_tracker.h"
#include <string>
#include <utility>
#include <vector>
#include "base/memory/raw_ptr.h"
#include "cc/layers/picture_layer_impl.h"
namespace cc {
AnimatedPaintWorkletTracker::AnimatedPaintWorkletTracker() = default;
AnimatedPaintWorkletTracker::~AnimatedPaintWorkletTracker() = default;
AnimatedPaintWorkletTracker::PropertyState::PropertyState(
PaintWorkletInput::PropertyValue value,
base::flat_set<raw_ptr<PictureLayerImpl, CtnExperimental>> layers)
: animation_value(value), associated_layers(std::move(layers)) {}
AnimatedPaintWorkletTracker::PropertyState::PropertyState() = default;
AnimatedPaintWorkletTracker::PropertyState::PropertyState(
const PropertyState& other) = default;
AnimatedPaintWorkletTracker::PropertyState::~PropertyState() = default;
void AnimatedPaintWorkletTracker::OnCustomPropertyMutated(
PaintWorkletInput::PropertyKey property_key,
PaintWorkletInput::PropertyValue property_value) {
auto iter = input_properties_.find(property_key);
if (iter == input_properties_.end()) {
return;
}
iter->second.animation_value = std::move(property_value);
input_properties_animated_on_impl_.insert(property_key);
}
bool AnimatedPaintWorkletTracker::InvalidatePaintWorkletsOnPendingTree() {
for (const auto& prop_key : input_properties_animated_on_impl_) {
auto it = input_properties_.find(prop_key);
if (it == input_properties_.end()) {
continue;
}
PropertyState& state = it->second;
for (PictureLayerImpl* layer : state.associated_layers) {
layer->InvalidatePaintWorklets(prop_key, state.animation_value,
state.last_animation_value);
}
state.last_animation_value = state.animation_value;
}
bool return_value = !input_properties_animated_on_impl_.empty();
input_properties_animated_on_impl_.clear();
return return_value;
}
PaintWorkletInput::PropertyValue
AnimatedPaintWorkletTracker::GetPropertyAnimationValue(
const PaintWorkletInput::PropertyKey& key) const {
return input_properties_.find(key)->second.animation_value;
}
void AnimatedPaintWorkletTracker::UpdatePaintWorkletInputProperties(
const std::vector<DiscardableImageMap::PaintWorkletInputWithImageId>&
inputs,
PictureLayerImpl* layer) {
std::vector<PaintWorkletInput::PropertyKey> all_input_properties_vector;
for (const auto& input : inputs) {
all_input_properties_vector.insert(
all_input_properties_vector.end(),
std::begin(input.first->GetPropertyKeys()),
std::end(input.first->GetPropertyKeys()));
}
base::flat_set<PaintWorkletInput::PropertyKey> all_input_properties(
std::move(all_input_properties_vector));
for (auto& entry : input_properties_) {
if (all_input_properties.contains(entry.first))
entry.second.associated_layers.insert(layer);
else
entry.second.associated_layers.erase(layer);
}
for (const auto& prop_key : all_input_properties) {
if (!input_properties_.contains(prop_key))
input_properties_[prop_key].associated_layers.insert(layer);
}
}
void AnimatedPaintWorkletTracker::ClearUnusedInputProperties() {
base::EraseIf(
input_properties_,
[](const std::pair<PaintWorkletInput::PropertyKey, PropertyState>&
entry) { return entry.second.associated_layers.empty(); });
for (auto& entry : input_properties_)
entry.second.animation_value.reset();
}
bool AnimatedPaintWorkletTracker::HasInputPropertiesAnimatedOnImpl() const {
return !input_properties_animated_on_impl_.empty();
}
}