gecko-dev/dom/smil/nsSMILCompositor.cpp
Mantaroh Yoshinaga df05b9f9a2 Bug 1062106 part 1 - Remove special handling of attributeType='XML'. r=birtles,longsonr+218550
This patch removes handling of the 'attributeType' attribute so that we behave
as if attributeType is always 'auto'. This means that for CSS properties we
always animate them as CSS properties (i.e. we animate them as part of the
SMIL override stylesheet) rather than mapped attributes.

The one special case is width/height on an outer SVG. Previously we animated
this as a mapped attribute since Web compatibility requires that the
width/height on an outer SVG, when set explicitly, are mapped to style.
However, we can produce the same behavior by animating these as CSS properties
(as opposed to mapped attributes). There is no observable difference in results
returned by the SVG DOM APIs, only the level at which the result is added to
the cascade: the SMIL override stylesheet instead of the attribute animation
presentation hint level.

As part of this patch, we animate width/height on outer SVG elements as CSS
properties as opposed to mapped attributes both for consistency and also so
we can remove the animated mapped attribute code altogether.

MozReview-Commit-ID: Ll1LWWRQ66R

--HG--
extra : rebase_source : bd513e191e3d0ba2a1e982eea4c548392bf5817d
2017-03-21 15:42:17 +09:00

212 lines
7.2 KiB
C++

/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "nsSMILCompositor.h"
#include "nsCSSProps.h"
#include "nsHashKeys.h"
#include "nsSMILCSSProperty.h"
// PLDHashEntryHdr methods
bool
nsSMILCompositor::KeyEquals(KeyTypePointer aKey) const
{
return aKey && aKey->Equals(mKey);
}
/*static*/ PLDHashNumber
nsSMILCompositor::HashKey(KeyTypePointer aKey)
{
// Combine the 3 values into one numeric value, which will be hashed.
// NOTE: We right-shift one of the pointers by 2 to get some randomness in
// its 2 lowest-order bits. (Those shifted-off bits will always be 0 since
// our pointers will be word-aligned.)
return (NS_PTR_TO_UINT32(aKey->mElement.get()) >> 2) +
NS_PTR_TO_UINT32(aKey->mAttributeName.get());
}
// Cycle-collection support
void
nsSMILCompositor::Traverse(nsCycleCollectionTraversalCallback* aCallback)
{
if (!mKey.mElement)
return;
NS_CYCLE_COLLECTION_NOTE_EDGE_NAME(*aCallback, "Compositor mKey.mElement");
aCallback->NoteXPCOMChild(mKey.mElement);
}
// Other methods
void
nsSMILCompositor::AddAnimationFunction(nsSMILAnimationFunction* aFunc)
{
if (aFunc) {
mAnimationFunctions.AppendElement(aFunc);
}
}
void
nsSMILCompositor::ComposeAttribute(bool& aMightHavePendingStyleUpdates)
{
if (!mKey.mElement)
return;
// FIRST: Get the nsISMILAttr (to grab base value from, and to eventually
// give animated value to)
nsAutoPtr<nsISMILAttr> smilAttr(CreateSMILAttr());
if (!smilAttr) {
// Target attribute not found (or, out of memory)
return;
}
if (mAnimationFunctions.IsEmpty()) {
// No active animation functions. (We can still have a nsSMILCompositor in
// that case if an animation function has *just* become inactive)
smilAttr->ClearAnimValue();
// Removing the animation effect may require a style update.
aMightHavePendingStyleUpdates = true;
return;
}
// SECOND: Sort the animationFunctions, to prepare for compositing.
nsSMILAnimationFunction::Comparator comparator;
mAnimationFunctions.Sort(comparator);
// THIRD: Step backwards through animation functions to find out
// which ones we actually care about.
uint32_t firstFuncToCompose = GetFirstFuncToAffectSandwich();
// FOURTH: Get & cache base value
nsSMILValue sandwichResultValue;
if (!mAnimationFunctions[firstFuncToCompose]->WillReplace()) {
sandwichResultValue = smilAttr->GetBaseValue();
}
UpdateCachedBaseValue(sandwichResultValue);
if (!mForceCompositing) {
return;
}
// FIFTH: Compose animation functions
aMightHavePendingStyleUpdates = true;
uint32_t length = mAnimationFunctions.Length();
for (uint32_t i = firstFuncToCompose; i < length; ++i) {
mAnimationFunctions[i]->ComposeResult(*smilAttr, sandwichResultValue);
}
if (sandwichResultValue.IsNull()) {
smilAttr->ClearAnimValue();
return;
}
// SIXTH: Set the animated value to the final composited result.
nsresult rv = smilAttr->SetAnimValue(sandwichResultValue);
if (NS_FAILED(rv)) {
NS_WARNING("nsISMILAttr::SetAnimValue failed");
}
}
void
nsSMILCompositor::ClearAnimationEffects()
{
if (!mKey.mElement || !mKey.mAttributeName)
return;
nsAutoPtr<nsISMILAttr> smilAttr(CreateSMILAttr());
if (!smilAttr) {
// Target attribute not found (or, out of memory)
return;
}
smilAttr->ClearAnimValue();
}
// Protected Helper Functions
// --------------------------
nsISMILAttr*
nsSMILCompositor::CreateSMILAttr()
{
nsCSSPropertyID propID =
nsCSSProps::LookupProperty(nsDependentAtomString(mKey.mAttributeName),
CSSEnabledState::eForAllContent);
if (nsSMILCSSProperty::IsPropertyAnimatable(propID)) {
// If we are animating the 'width' or 'height' of an outer SVG
// element we should animate it as a CSS property, but for other elements
// (e.g. <rect>) we should animate it as a length attribute.
// The easiest way to test for an outer SVG element, is to see if it is an
// SVG-namespace element mapping its width/height attribute to style.
bool animateAsAttr = (mKey.mAttributeName == nsGkAtoms::width ||
mKey.mAttributeName == nsGkAtoms::height) &&
mKey.mElement->GetNameSpaceID() == kNameSpaceID_SVG &&
!mKey.mElement->IsAttributeMapped(mKey.mAttributeName);
if (!animateAsAttr) {
return new nsSMILCSSProperty(propID, mKey.mElement.get());
}
}
return mKey.mElement->GetAnimatedAttr(mKey.mAttributeNamespaceID,
mKey.mAttributeName);
}
uint32_t
nsSMILCompositor::GetFirstFuncToAffectSandwich()
{
// For performance reasons, we throttle most animations on elements in
// display:none subtrees. (We can't throttle animations that target the
// "display" property itself, though -- if we did, display:none elements
// could never be dynamically displayed via animations.)
// To determine whether we're in a display:none subtree, we will check the
// element's primary frame since element in display:none subtree doesn't have
// a primary frame. Before this process, we will construct frame when we
// append an element to subtree. So we will not need to worry about pending
// frame construction in this step.
bool canThrottle = mKey.mAttributeName != nsGkAtoms::display &&
!mKey.mElement->GetPrimaryFrame();
uint32_t i;
for (i = mAnimationFunctions.Length(); i > 0; --i) {
nsSMILAnimationFunction* curAnimFunc = mAnimationFunctions[i-1];
// In the following, the lack of short-circuit behavior of |= means that we
// will ALWAYS run UpdateCachedTarget (even if mForceCompositing is true)
// but only call HasChanged and WasSkippedInPrevSample if necessary. This
// is important since we need UpdateCachedTarget to run in order to detect
// changes to the target in subsequent samples.
mForceCompositing |=
curAnimFunc->UpdateCachedTarget(mKey) ||
(curAnimFunc->HasChanged() && !canThrottle) ||
curAnimFunc->WasSkippedInPrevSample();
if (curAnimFunc->WillReplace()) {
--i;
break;
}
}
// Mark remaining animation functions as having been skipped so if we later
// use them we'll know to force compositing.
// Note that we only really need to do this if something has changed
// (otherwise we would have set the flag on a previous sample) and if
// something has changed mForceCompositing will be true.
if (mForceCompositing) {
for (uint32_t j = i; j > 0; --j) {
mAnimationFunctions[j-1]->SetWasSkipped();
}
}
return i;
}
void
nsSMILCompositor::UpdateCachedBaseValue(const nsSMILValue& aBaseValue)
{
if (!mCachedBaseValue) {
// We don't have last sample's base value cached. Assume it's changed.
mCachedBaseValue = new nsSMILValue(aBaseValue);
NS_WARNING_ASSERTION(mCachedBaseValue, "failed to cache base value (OOM?)");
mForceCompositing = true;
} else if (*mCachedBaseValue != aBaseValue) {
// Base value has changed since last sample.
*mCachedBaseValue = aBaseValue;
mForceCompositing = true;
}
}