1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407
|
/*
* Copyright (C) 2018-2024 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#pragma once
#include "SVGAnimatedPropertyAccessorImpl.h"
#include "SVGAnimatedPropertyPairAccessorImpl.h"
#include "SVGPropertyAccessorImpl.h"
#include "SVGPropertyRegistry.h"
#include <wtf/HashMap.h>
namespace WebCore {
class SVGRectElement;
class SVGCircleElement;
class SVGAttributeAnimator;
class SVGConditionalProcessingAttributes;
template<typename T>
concept HasFastPropertyForAttribute = requires(const T& element, const QualifiedName& name)
{
{ element.propertyForAttribute(name) } -> std::same_as<SVGAnimatedProperty*>;
};
struct SVGAttributeHashTranslator {
static unsigned hash(const QualifiedName& key)
{
if (key.hasPrefix()) {
QualifiedNameComponents components = { nullAtom().impl(), key.localName().impl(), key.namespaceURI().impl() };
return computeHash(components);
}
return DefaultHash<QualifiedName>::hash(key);
}
static bool equal(const QualifiedName& a, const QualifiedName& b) { return a.matches(b); }
static constexpr bool safeToCompareToEmptyOrDeleted = false;
static constexpr bool hasHashInValue = true;
};
template<typename OwnerType, typename... BaseTypes>
class SVGPropertyOwnerRegistry : public SVGPropertyRegistry {
WTF_MAKE_TZONE_ALLOCATED_TEMPLATE(SVGPropertyOwnerRegistry);
public:
SVGPropertyOwnerRegistry(OwnerType& owner)
: m_owner(owner)
{
}
template<const LazyNeverDestroyed<const QualifiedName>& attributeName, Ref<SVGStringList> SVGConditionalProcessingAttributes::*property>
static void registerConditionalProcessingAttributeProperty()
{
registerProperty(attributeName, SVGConditionalProcessingAttributeAccessor<OwnerType>::template singleton<property>());
}
template<const LazyNeverDestroyed<const QualifiedName>& attributeName, Ref<SVGTransformList> OwnerType::*property>
static void registerProperty()
{
registerProperty(attributeName, SVGTransformListAccessor<OwnerType>::template singleton<property>());
}
template<const LazyNeverDestroyed<const QualifiedName>& attributeName, Ref<SVGAnimatedBoolean> OwnerType::*property>
static void registerProperty()
{
registerProperty(attributeName, SVGAnimatedBooleanAccessor<OwnerType>::template singleton<property>());
}
template<const LazyNeverDestroyed<const QualifiedName>& attributeName, typename EnumType, Ref<SVGAnimatedEnumeration> OwnerType::*property>
static void registerProperty()
{
registerProperty(attributeName, SVGAnimatedEnumerationAccessor<OwnerType, EnumType>::template singleton<property>());
}
template<const LazyNeverDestroyed<const QualifiedName>& attributeName, Ref<SVGAnimatedInteger> OwnerType::*property>
static void registerProperty()
{
registerProperty(attributeName, SVGAnimatedIntegerAccessor<OwnerType>::template singleton<property>());
}
template<const LazyNeverDestroyed<const QualifiedName>& attributeName, Ref<SVGAnimatedLength> OwnerType::*property>
static void registerProperty()
{
registerProperty(attributeName, SVGAnimatedLengthAccessor<OwnerType>::template singleton<property>());
}
template<const LazyNeverDestroyed<const QualifiedName>& attributeName, Ref<SVGAnimatedLengthList> OwnerType::*property>
static void registerProperty()
{
registerProperty(attributeName, SVGAnimatedLengthListAccessor<OwnerType>::template singleton<property>());
}
template<const LazyNeverDestroyed<const QualifiedName>& attributeName, Ref<SVGAnimatedNumber> OwnerType::*property>
static void registerProperty()
{
registerProperty(attributeName, SVGAnimatedNumberAccessor<OwnerType>::template singleton<property>());
}
template<const LazyNeverDestroyed<const QualifiedName>& attributeName, Ref<SVGAnimatedNumberList> OwnerType::*property>
static void registerProperty()
{
registerProperty(attributeName, SVGAnimatedNumberListAccessor<OwnerType>::template singleton<property>());
}
template<const LazyNeverDestroyed<const QualifiedName>& attributeName, Ref<SVGAnimatedAngle> OwnerType::*property1, Ref<SVGAnimatedOrientType> OwnerType::*property2>
static void registerProperty()
{
registerProperty(attributeName, SVGAnimatedAngleOrientAccessor<OwnerType>::template singleton<property1, property2>());
}
template<const LazyNeverDestroyed<const QualifiedName>& attributeName, Ref<SVGAnimatedPathSegList> OwnerType::*property>
static void registerProperty()
{
registerProperty(attributeName, SVGAnimatedPathSegListAccessor<OwnerType>::template singleton<property>());
}
template<const LazyNeverDestroyed<const QualifiedName>& attributeName, Ref<SVGAnimatedPointList> OwnerType::*property>
static void registerProperty()
{
registerProperty(attributeName, SVGAnimatedPointListAccessor<OwnerType>::template singleton<property>());
}
template<const LazyNeverDestroyed<const QualifiedName>& attributeName, Ref<SVGAnimatedPreserveAspectRatio> OwnerType::*property>
static void registerProperty()
{
registerProperty(attributeName, SVGAnimatedPreserveAspectRatioAccessor<OwnerType>::template singleton<property>());
}
template<const LazyNeverDestroyed<const QualifiedName>& attributeName, Ref<SVGAnimatedRect> OwnerType::*property>
static void registerProperty()
{
registerProperty(attributeName, SVGAnimatedRectAccessor<OwnerType>::template singleton<property>());
}
template<const LazyNeverDestroyed<const QualifiedName>& attributeName, Ref<SVGAnimatedString> OwnerType::*property>
static void registerProperty()
{
registerProperty(attributeName, SVGAnimatedStringAccessor<OwnerType>::template singleton<property>());
}
template<const LazyNeverDestroyed<const QualifiedName>& attributeName, Ref<SVGAnimatedTransformList> OwnerType::*property>
static void registerProperty()
{
registerProperty(attributeName, SVGAnimatedTransformListAccessor<OwnerType>::template singleton<property>());
}
template<const LazyNeverDestroyed<const QualifiedName>& attributeName, Ref<SVGAnimatedInteger> OwnerType::*property1, Ref<SVGAnimatedInteger> OwnerType::*property2>
static void registerProperty()
{
registerProperty(attributeName, SVGAnimatedIntegerPairAccessor<OwnerType>::template singleton<property1, property2>());
}
template<const LazyNeverDestroyed<const QualifiedName>& attributeName, Ref<SVGAnimatedNumber> OwnerType::*property1, Ref<SVGAnimatedNumber> OwnerType::*property2>
static void registerProperty()
{
registerProperty(attributeName, SVGAnimatedNumberPairAccessor<OwnerType>::template singleton<property1, property2>());
}
// Enumerate all the SVGMemberAccessors recursively. The functor will be called and will
// be given the pair<QualifiedName, SVGMemberAccessor> till the functor returns false.
template<typename Functor>
static bool enumerateRecursively(const Functor& functor)
{
for (const auto& entry : attributeNameToAccessorMap()) {
if (!functor(entry))
return false;
}
return enumerateRecursivelyBaseTypes(functor);
}
template<typename Functor>
static bool lookupRecursivelyAndApply(const QualifiedName& attributeName, const Functor& functor)
{
if (auto* accessor = findAccessor(attributeName)) {
functor(*accessor);
return true;
}
return lookupRecursivelyAndApplyBaseTypes(attributeName, functor);
}
// Returns true if OwnerType owns a property whose name is attributeName.
static bool isKnownAttribute(const QualifiedName& attributeName)
{
return findAccessor(attributeName);
}
// Returns true if OwnerType owns a property whose name is attributeName
// and its type is SVGAnimatedLength.
static bool isAnimatedLengthAttribute(const QualifiedName& attributeName)
{
if (const auto* accessor = findAccessor(attributeName))
return accessor->isAnimatedLength();
return false;
}
QualifiedName propertyAttributeName(const SVGProperty& property) const override
{
QualifiedName attributeName = nullQName();
enumerateRecursively([&](const auto& entry) -> bool {
if (!entry.value->matches(m_owner, property))
return true;
attributeName = entry.key;
return false;
});
return attributeName;
}
QualifiedName animatedPropertyAttributeName(const SVGAnimatedProperty& animatedProperty) const override
{
QualifiedName attributeName = nullQName();
enumerateRecursively([&](const auto& entry) -> bool {
if (!entry.value->matches(m_owner, animatedProperty))
return true;
attributeName = entry.key;
return false;
});
return attributeName;
}
void setAnimatedPropertyDirty(const QualifiedName& attributeName, SVGAnimatedProperty& animatedProperty) const override
{
if (auto* property = fastAnimatedPropertyLookup(m_owner, attributeName)) {
property->setDirty();
return;
}
lookupRecursivelyAndApply(attributeName, [&](auto& accessor) {
accessor.setDirty(m_owner, animatedProperty);
});
}
// Detach all the properties recursively from their OwnerTypes.
void detachAllProperties() const override
{
enumerateRecursively([&](const auto& entry) -> bool {
entry.value->detach(m_owner);
return true;
});
}
static inline SVGAnimatedProperty* fastAnimatedPropertyLookup(OwnerType& owner, const QualifiedName& attributeName)
{
if constexpr (HasFastPropertyForAttribute<OwnerType>)
return owner.propertyForAttribute(attributeName);
else {
static_assert(!std::is_same_v<OwnerType, SVGRectElement> && !std::is_same_v<OwnerType, SVGCircleElement>, "Element should use fast property path");
return nullptr;
}
}
// Finds the property whose name is attributeName and returns the synchronize
// string through the associated SVGMemberAccessor.
std::optional<String> synchronize(const QualifiedName& attributeName) const override
{
if (auto* property = fastAnimatedPropertyLookup(m_owner, attributeName))
return property->synchronize();
std::optional<String> value;
lookupRecursivelyAndApply(attributeName, [&](auto& accessor) {
value = accessor.synchronize(m_owner);
});
return value;
}
// Enumerate recursively the SVGMemberAccessors of the OwnerType and all its BaseTypes.
// Collect all the pairs <AttributeName, String> only for the dirty properties.
UncheckedKeyHashMap<QualifiedName, String> synchronizeAllAttributes() const override
{
UncheckedKeyHashMap<QualifiedName, String> map;
enumerateRecursively([&](const auto& entry) -> bool {
if (auto string = entry.value->synchronize(m_owner))
map.add(entry.key, *string);
return true;
});
return map;
}
bool isAnimatedPropertyAttribute(const QualifiedName& attributeName) const override
{
if (auto* property = fastAnimatedPropertyLookup(m_owner, attributeName))
return true;
bool isAnimatedPropertyAttribute = false;
lookupRecursivelyAndApply(attributeName, [&](auto& accessor) {
isAnimatedPropertyAttribute = accessor.isAnimatedProperty();
});
return isAnimatedPropertyAttribute;
}
bool isAnimatedStylePropertyAttribute(const QualifiedName& attributeName) const override
{
static NeverDestroyed<UncheckedKeyHashSet<QualifiedName::QualifiedNameImpl*>> animatedStyleAttributes = std::initializer_list<QualifiedName::QualifiedNameImpl*> {
SVGNames::cxAttr->impl(),
SVGNames::cyAttr->impl(),
SVGNames::rAttr->impl(),
SVGNames::rxAttr->impl(),
SVGNames::ryAttr->impl(),
SVGNames::heightAttr->impl(),
SVGNames::widthAttr->impl(),
SVGNames::xAttr->impl(),
SVGNames::yAttr->impl()
};
return isAnimatedLengthAttribute(attributeName) && animatedStyleAttributes.get().contains(attributeName.impl());
}
RefPtr<SVGAttributeAnimator> createAnimator(const QualifiedName& attributeName, AnimationMode animationMode, CalcMode calcMode, bool isAccumulated, bool isAdditive) const override
{
RefPtr<SVGAttributeAnimator> animator;
lookupRecursivelyAndApply(attributeName, [&](auto& accessor) {
animator = accessor.createAnimator(m_owner, attributeName, animationMode, calcMode, isAccumulated, isAdditive);
});
return animator;
}
void appendAnimatedInstance(const QualifiedName& attributeName, SVGAttributeAnimator& animator) const override
{
lookupRecursivelyAndApply(attributeName, [&](auto& accessor) {
accessor.appendAnimatedInstance(m_owner, animator);
});
}
private:
// Singleton map for every OwnerType.
using QualifiedNameAccessorHashMap = UncheckedKeyHashMap<QualifiedName, const SVGMemberAccessor<OwnerType>*, SVGAttributeHashTranslator>;
static QualifiedNameAccessorHashMap& attributeNameToAccessorMap()
{
static NeverDestroyed<QualifiedNameAccessorHashMap> attributeNameToAccessorMap;
return attributeNameToAccessorMap;
}
static void registerProperty(const QualifiedName& attributeName, const SVGMemberAccessor<OwnerType>& propertyAccessor)
{
attributeNameToAccessorMap().add(attributeName, &propertyAccessor);
}
// This is a template function with parameter 'I' whose default value = 0. So you can call it without any parameter
// from enumerateRecursively(). It returns true and is enable_if<I == sizeof...(BaseTypes)>. So it is mainly for
// breaking the recursion.
template<typename Functor, size_t I = 0>
static typename std::enable_if<I == sizeof...(BaseTypes), bool>::type enumerateRecursivelyBaseTypes(const Functor&) { return true; }
// This version of animatedTypesBaseTypes() is enable_if<I < sizeof...(BaseTypes)>.
template<typename Functor, size_t I = 0>
static typename std::enable_if<I < sizeof...(BaseTypes), bool>::type enumerateRecursivelyBaseTypes(const Functor& functor)
{
// Get the base type at index 'I' using std::tuple and std::tuple_element.
using BaseType = typename std::tuple_element<I, typename std::tuple<BaseTypes...>>::type;
if (!BaseType::PropertyRegistry::enumerateRecursively(functor))
return false;
// BaseType does not want to break the recursion. So recurse to the next BaseType.
return enumerateRecursivelyBaseTypes<Functor, I + 1>(functor);
}
static const SVGMemberAccessor<OwnerType>* findAccessor(const QualifiedName& attributeName)
{
auto it = attributeNameToAccessorMap().find(attributeName);
return it != attributeNameToAccessorMap().end() ? it->value : nullptr;
}
template<typename Functor, size_t I = 0>
static typename std::enable_if<I == sizeof...(BaseTypes), bool>::type lookupRecursivelyAndApplyBaseTypes(const QualifiedName&, const Functor&) { return false; }
template<typename Functor, size_t I = 0>
static typename std::enable_if<I < sizeof...(BaseTypes), bool>::type lookupRecursivelyAndApplyBaseTypes(const QualifiedName& attributeName, const Functor& functor)
{
// Get the base type at index 'I' using std::tuple and std::tuple_element.
using BaseType = typename std::tuple_element<I, typename std::tuple<BaseTypes...>>::type;
if (BaseType::PropertyRegistry::lookupRecursivelyAndApply(attributeName, functor))
return true;
// BaseType does not want to break the recursion. So recurse to the next BaseType.
return lookupRecursivelyAndApplyBaseTypes<Functor, I + 1>(attributeName, functor);
}
OwnerType& m_owner;
};
#define TZONE_TEMPLATE_PARAMS template<typename OwnerType, typename... BaseTypes>
#define TZONE_TYPE SVGPropertyOwnerRegistry<OwnerType, BaseTypes...>
WTF_MAKE_TZONE_ALLOCATED_TEMPLATE_IMPL_WITH_MULTIPLE_OR_SPECIALIZED_PARAMETERS();
#undef TZONE_TEMPLATE_PARAMS
#undef TZONE_TYPE
} // namespace WebCore
|