2015-05-03 22:32:37 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2012-05-21 15:12:37 +04:00
|
|
|
/* 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/. */
|
2009-01-15 07:38:07 +03:00
|
|
|
|
2019-01-01 12:16:21 +03:00
|
|
|
#include "SMILCompositor.h"
|
2017-02-02 09:23:22 +03:00
|
|
|
|
2019-05-16 16:21:20 +03:00
|
|
|
#include "mozilla/dom/SVGSVGElement.h"
|
2017-04-03 10:49:10 +03:00
|
|
|
#include "nsComputedDOMStyle.h"
|
2009-10-03 01:37:25 +04:00
|
|
|
#include "nsCSSProps.h"
|
2009-01-15 07:38:07 +03:00
|
|
|
#include "nsHashKeys.h"
|
2019-01-08 10:55:14 +03:00
|
|
|
#include "SMILCSSProperty.h"
|
2009-01-15 07:38:07 +03:00
|
|
|
|
2019-01-01 12:16:21 +03:00
|
|
|
namespace mozilla {
|
|
|
|
|
2009-01-15 07:38:07 +03:00
|
|
|
// PLDHashEntryHdr methods
|
2019-01-01 12:16:21 +03:00
|
|
|
bool SMILCompositor::KeyEquals(KeyTypePointer aKey) const {
|
2009-01-15 07:38:07 +03:00
|
|
|
return aKey && aKey->Equals(mKey);
|
|
|
|
}
|
|
|
|
|
2019-02-26 01:05:29 +03:00
|
|
|
/*static*/
|
|
|
|
PLDHashNumber SMILCompositor::HashKey(KeyTypePointer aKey) {
|
2010-03-12 09:03:49 +03:00
|
|
|
// 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) +
|
2017-03-21 09:42:17 +03:00
|
|
|
NS_PTR_TO_UINT32(aKey->mAttributeName.get());
|
2009-01-15 07:38:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Cycle-collection support
|
2019-01-01 12:16:21 +03:00
|
|
|
void SMILCompositor::Traverse(nsCycleCollectionTraversalCallback* aCallback) {
|
2009-01-15 07:38:07 +03:00
|
|
|
if (!mKey.mElement) return;
|
|
|
|
|
|
|
|
NS_CYCLE_COLLECTION_NOTE_EDGE_NAME(*aCallback, "Compositor mKey.mElement");
|
|
|
|
aCallback->NoteXPCOMChild(mKey.mElement);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Other methods
|
2019-01-01 12:16:21 +03:00
|
|
|
void SMILCompositor::AddAnimationFunction(SMILAnimationFunction* aFunc) {
|
2009-01-15 07:38:07 +03:00
|
|
|
if (aFunc) {
|
|
|
|
mAnimationFunctions.AppendElement(aFunc);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-01 12:16:21 +03:00
|
|
|
void SMILCompositor::ComposeAttribute(bool& aMightHavePendingStyleUpdates) {
|
2009-07-14 23:33:29 +04:00
|
|
|
if (!mKey.mElement) return;
|
|
|
|
|
2018-03-23 16:49:21 +03:00
|
|
|
// If we might need to resolve base styles, grab a suitable ComputedStyle
|
2019-01-25 06:24:01 +03:00
|
|
|
// for initializing our SMILAttr with.
|
2018-03-22 21:20:41 +03:00
|
|
|
RefPtr<ComputedStyle> baseComputedStyle;
|
2017-04-03 10:49:10 +03:00
|
|
|
if (MightNeedBaseStyle()) {
|
2018-03-22 21:20:41 +03:00
|
|
|
baseComputedStyle = nsComputedDOMStyle::GetUnanimatedComputedStyleNoFlush(
|
2021-08-07 12:47:15 +03:00
|
|
|
mKey.mElement, PseudoStyleType::NotPseudo);
|
2017-04-03 10:49:10 +03:00
|
|
|
}
|
|
|
|
|
2019-01-25 06:24:01 +03:00
|
|
|
// FIRST: Get the SMILAttr (to grab base value from, and to eventually
|
2009-07-14 23:33:29 +04:00
|
|
|
// give animated value to)
|
2019-01-25 06:24:01 +03:00
|
|
|
UniquePtr<SMILAttr> smilAttr = CreateSMILAttr(baseComputedStyle);
|
2009-01-15 07:38:07 +03:00
|
|
|
if (!smilAttr) {
|
2009-10-03 01:37:25 +04:00
|
|
|
// Target attribute not found (or, out of memory)
|
2009-01-15 07:38:07 +03:00
|
|
|
return;
|
|
|
|
}
|
2010-02-21 00:13:11 +03:00
|
|
|
if (mAnimationFunctions.IsEmpty()) {
|
2019-01-01 12:16:21 +03:00
|
|
|
// No active animation functions. (We can still have a SMILCompositor in
|
2010-02-21 00:13:11 +03:00
|
|
|
// that case if an animation function has *just* become inactive)
|
|
|
|
smilAttr->ClearAnimValue();
|
2016-05-23 04:47:14 +03:00
|
|
|
// Removing the animation effect may require a style update.
|
|
|
|
aMightHavePendingStyleUpdates = true;
|
2010-02-21 00:13:11 +03:00
|
|
|
return;
|
|
|
|
}
|
2009-01-15 07:38:07 +03:00
|
|
|
|
|
|
|
// SECOND: Sort the animationFunctions, to prepare for compositing.
|
2018-12-31 14:54:22 +03:00
|
|
|
SMILAnimationFunction::Comparator comparator;
|
2009-01-15 07:38:07 +03:00
|
|
|
mAnimationFunctions.Sort(comparator);
|
|
|
|
|
|
|
|
// THIRD: Step backwards through animation functions to find out
|
|
|
|
// which ones we actually care about.
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t firstFuncToCompose = GetFirstFuncToAffectSandwich();
|
2009-01-15 07:38:07 +03:00
|
|
|
|
2010-02-21 00:13:11 +03:00
|
|
|
// FOURTH: Get & cache base value
|
2019-01-23 16:48:08 +03:00
|
|
|
SMILValue sandwichResultValue;
|
2010-11-10 02:21:04 +03:00
|
|
|
if (!mAnimationFunctions[firstFuncToCompose]->WillReplace()) {
|
|
|
|
sandwichResultValue = smilAttr->GetBaseValue();
|
2009-01-15 07:38:07 +03:00
|
|
|
}
|
2010-02-21 00:13:11 +03:00
|
|
|
UpdateCachedBaseValue(sandwichResultValue);
|
|
|
|
|
2010-02-21 00:13:11 +03:00
|
|
|
if (!mForceCompositing) {
|
|
|
|
return;
|
|
|
|
}
|
2010-02-21 00:13:11 +03:00
|
|
|
|
|
|
|
// FIFTH: Compose animation functions
|
2016-05-23 04:47:14 +03:00
|
|
|
aMightHavePendingStyleUpdates = true;
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t length = mAnimationFunctions.Length();
|
|
|
|
for (uint32_t i = firstFuncToCompose; i < length; ++i) {
|
2010-02-21 00:13:11 +03:00
|
|
|
mAnimationFunctions[i]->ComposeResult(*smilAttr, sandwichResultValue);
|
2009-01-15 07:38:07 +03:00
|
|
|
}
|
2010-11-10 02:21:04 +03:00
|
|
|
if (sandwichResultValue.IsNull()) {
|
|
|
|
smilAttr->ClearAnimValue();
|
|
|
|
return;
|
|
|
|
}
|
2009-01-15 07:38:07 +03:00
|
|
|
|
2010-02-21 00:13:11 +03:00
|
|
|
// SIXTH: Set the animated value to the final composited result.
|
|
|
|
nsresult rv = smilAttr->SetAnimValue(sandwichResultValue);
|
2009-01-15 07:38:07 +03:00
|
|
|
if (NS_FAILED(rv)) {
|
2019-01-25 06:24:01 +03:00
|
|
|
NS_WARNING("SMILAttr::SetAnimValue failed");
|
2009-07-15 22:33:31 +04:00
|
|
|
}
|
2009-01-15 07:38:07 +03:00
|
|
|
}
|
|
|
|
|
2019-01-01 12:16:21 +03:00
|
|
|
void SMILCompositor::ClearAnimationEffects() {
|
2009-07-14 23:33:29 +04:00
|
|
|
if (!mKey.mElement || !mKey.mAttributeName) return;
|
2009-01-15 07:38:07 +03:00
|
|
|
|
2019-01-25 06:24:01 +03:00
|
|
|
UniquePtr<SMILAttr> smilAttr = CreateSMILAttr(nullptr);
|
2009-07-14 23:33:29 +04:00
|
|
|
if (!smilAttr) {
|
|
|
|
// Target attribute not found (or, out of memory)
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
smilAttr->ClearAnimValue();
|
2009-01-15 07:38:07 +03:00
|
|
|
}
|
2009-07-14 23:33:29 +04:00
|
|
|
|
2010-02-21 00:13:11 +03:00
|
|
|
// Protected Helper Functions
|
|
|
|
// --------------------------
|
2019-01-25 06:24:01 +03:00
|
|
|
UniquePtr<SMILAttr> SMILCompositor::CreateSMILAttr(
|
2018-03-22 21:20:41 +03:00
|
|
|
ComputedStyle* aBaseComputedStyle) {
|
2017-03-30 07:10:08 +03:00
|
|
|
nsCSSPropertyID propID = GetCSSPropertyToAnimate();
|
|
|
|
|
|
|
|
if (propID != eCSSProperty_UNKNOWN) {
|
2019-01-08 10:55:14 +03:00
|
|
|
return MakeUnique<SMILCSSProperty>(propID, mKey.mElement.get(),
|
|
|
|
aBaseComputedStyle);
|
2017-03-30 07:10:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return mKey.mElement->GetAnimatedAttr(mKey.mAttributeNamespaceID,
|
|
|
|
mKey.mAttributeName);
|
|
|
|
}
|
|
|
|
|
2019-01-01 12:16:21 +03:00
|
|
|
nsCSSPropertyID SMILCompositor::GetCSSPropertyToAnimate() const {
|
2017-03-30 10:13:02 +03:00
|
|
|
if (mKey.mAttributeNamespaceID != kNameSpaceID_None) {
|
|
|
|
return eCSSProperty_UNKNOWN;
|
|
|
|
}
|
|
|
|
|
2017-03-21 09:42:17 +03:00
|
|
|
nsCSSPropertyID propID =
|
2020-01-08 04:21:30 +03:00
|
|
|
nsCSSProps::LookupProperty(nsAtomCString(mKey.mAttributeName));
|
2017-03-30 07:10:08 +03:00
|
|
|
|
2019-01-08 10:55:14 +03:00
|
|
|
if (!SMILCSSProperty::IsPropertyAnimatable(propID)) {
|
2017-03-30 07:10:08 +03:00
|
|
|
return eCSSProperty_UNKNOWN;
|
2010-02-21 00:13:11 +03:00
|
|
|
}
|
2017-03-21 09:42:17 +03:00
|
|
|
|
2017-03-30 07:10:08 +03:00
|
|
|
// 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
|
2019-05-16 16:21:20 +03:00
|
|
|
// in SVG namespace (e.g. <rect>) we should animate it as a length attribute.
|
2017-04-04 08:56:52 +03:00
|
|
|
if ((mKey.mAttributeName == nsGkAtoms::width ||
|
|
|
|
mKey.mAttributeName == nsGkAtoms::height) &&
|
2019-05-16 16:21:20 +03:00
|
|
|
mKey.mElement->GetNameSpaceID() == kNameSpaceID_SVG) {
|
|
|
|
// Not an <svg> element.
|
|
|
|
if (!mKey.mElement->IsSVGElement(nsGkAtoms::svg)) {
|
|
|
|
return eCSSProperty_UNKNOWN;
|
|
|
|
}
|
|
|
|
|
|
|
|
// An inner <svg> element
|
|
|
|
if (static_cast<dom::SVGSVGElement const&>(*mKey.mElement).IsInner()) {
|
|
|
|
return eCSSProperty_UNKNOWN;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Indeed an outer <svg> element, fall through.
|
2017-04-04 08:56:52 +03:00
|
|
|
}
|
2017-03-30 07:10:08 +03:00
|
|
|
|
2017-04-04 08:56:52 +03:00
|
|
|
return propID;
|
2010-02-21 00:13:11 +03:00
|
|
|
}
|
|
|
|
|
2019-01-01 12:16:21 +03:00
|
|
|
bool SMILCompositor::MightNeedBaseStyle() const {
|
2017-04-03 10:49:09 +03:00
|
|
|
if (GetCSSPropertyToAnimate() == eCSSProperty_UNKNOWN) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We should return true if at least one animation function might build on
|
|
|
|
// the base value.
|
2018-12-31 14:54:22 +03:00
|
|
|
for (const SMILAnimationFunction* func : mAnimationFunctions) {
|
2017-04-03 10:49:09 +03:00
|
|
|
if (!func->WillReplace()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-01-01 12:16:21 +03:00
|
|
|
uint32_t SMILCompositor::GetFirstFuncToAffectSandwich() {
|
2017-02-02 09:23:22 +03:00
|
|
|
// 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.
|
2016-05-23 04:45:14 +03:00
|
|
|
bool canThrottle = mKey.mAttributeName != nsGkAtoms::display &&
|
2017-02-02 09:23:22 +03:00
|
|
|
!mKey.mElement->GetPrimaryFrame();
|
2016-05-23 04:45:14 +03:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t i;
|
2010-02-21 00:13:11 +03:00
|
|
|
for (i = mAnimationFunctions.Length(); i > 0; --i) {
|
2018-12-31 14:54:22 +03:00
|
|
|
SMILAnimationFunction* curAnimFunc = mAnimationFunctions[i - 1];
|
2012-02-24 04:45:40 +04:00
|
|
|
// 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) ||
|
2016-05-23 04:45:14 +03:00
|
|
|
(curAnimFunc->HasChanged() && !canThrottle) ||
|
2012-02-24 04:45:40 +04:00
|
|
|
curAnimFunc->WasSkippedInPrevSample();
|
2010-02-21 00:13:11 +03:00
|
|
|
|
|
|
|
if (curAnimFunc->WillReplace()) {
|
|
|
|
--i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2016-05-23 04:45:14 +03:00
|
|
|
|
2012-02-24 04:45:40 +04:00
|
|
|
// 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) {
|
2012-08-22 19:56:38 +04:00
|
|
|
for (uint32_t j = i; j > 0; --j) {
|
2012-02-24 04:45:40 +04:00
|
|
|
mAnimationFunctions[j - 1]->SetWasSkipped();
|
|
|
|
}
|
|
|
|
}
|
2010-02-21 00:13:11 +03:00
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
2019-01-23 16:48:08 +03:00
|
|
|
void SMILCompositor::UpdateCachedBaseValue(const SMILValue& aBaseValue) {
|
Bug 1353208 - Don't allocate separate heap memory for nsSMILCompositor::mCachedBaseValue; r=dholbert
nsSMILCompositor::mCachedBaseValue is an nsAutoPtr<nsSMILValue> that we allocate
on the heap. It looks like we did that back in bug 533291 presumably because it
makes transferring these cached values between nsSMILCompositor objects cheaper.
One drawback of this, however, is that mCachedBaseValue has two null states:
the mCachedBaseValue pointer can be null, and the pointed-to nsSMILValue can be
a null value (i.e. IsNull() returns true).
Now that we have move ctors and operators defined for nsSMILValue we can
transfer these objects between compositors cheaply without requiring the object
to be allocated as separate heap object. This patch makes mCachedBaseValue just
a regular nsSMILValue class member (i.e. drops the nsAutoPtr).
There's a subtle difference in behavior with regards to the first sample.
Previously we would compare the (initially) null mCachedBaseValue pointer with
the passed-in nsSMILValue and set mForceCompositing to true. With this patch,
however, we will only set mForceCompositing to true if the passed-in
mCachedBaseValue is not null.
I believe this is correct, however, since if we don't call GetBaseValue in
ComposeAttribute we should not be setting mForceCompositing to true (something
else should ensure that gets set to true), and if we do call GetBaseValue the
result should not be a null nsSMILValue (except in some OOM cases where we don't
really care if we miss a sample). This patch adds an assertion to check that
GetBaseValue does, in fact, return a non-null value. (I checked the code and
this appears to be the case. Even in error cases we typically return an empty
nsSMILValue of a non-null type. For example, the early return in
nsSMILCSSProperty::GetBaseValue() does this.)
MozReview-Commit-ID: BRJFa4xMdxz
--HG--
extra : rebase_source : f3e3ca1e01e73610523bde7583e2a002d2473184
2017-03-30 07:10:07 +03:00
|
|
|
if (mCachedBaseValue != aBaseValue) {
|
2010-02-21 00:13:11 +03:00
|
|
|
// Base value has changed since last sample.
|
Bug 1353208 - Don't allocate separate heap memory for nsSMILCompositor::mCachedBaseValue; r=dholbert
nsSMILCompositor::mCachedBaseValue is an nsAutoPtr<nsSMILValue> that we allocate
on the heap. It looks like we did that back in bug 533291 presumably because it
makes transferring these cached values between nsSMILCompositor objects cheaper.
One drawback of this, however, is that mCachedBaseValue has two null states:
the mCachedBaseValue pointer can be null, and the pointed-to nsSMILValue can be
a null value (i.e. IsNull() returns true).
Now that we have move ctors and operators defined for nsSMILValue we can
transfer these objects between compositors cheaply without requiring the object
to be allocated as separate heap object. This patch makes mCachedBaseValue just
a regular nsSMILValue class member (i.e. drops the nsAutoPtr).
There's a subtle difference in behavior with regards to the first sample.
Previously we would compare the (initially) null mCachedBaseValue pointer with
the passed-in nsSMILValue and set mForceCompositing to true. With this patch,
however, we will only set mForceCompositing to true if the passed-in
mCachedBaseValue is not null.
I believe this is correct, however, since if we don't call GetBaseValue in
ComposeAttribute we should not be setting mForceCompositing to true (something
else should ensure that gets set to true), and if we do call GetBaseValue the
result should not be a null nsSMILValue (except in some OOM cases where we don't
really care if we miss a sample). This patch adds an assertion to check that
GetBaseValue does, in fact, return a non-null value. (I checked the code and
this appears to be the case. Even in error cases we typically return an empty
nsSMILValue of a non-null type. For example, the early return in
nsSMILCSSProperty::GetBaseValue() does this.)
MozReview-Commit-ID: BRJFa4xMdxz
--HG--
extra : rebase_source : f3e3ca1e01e73610523bde7583e2a002d2473184
2017-03-30 07:10:07 +03:00
|
|
|
mCachedBaseValue = aBaseValue;
|
2011-10-17 18:59:28 +04:00
|
|
|
mForceCompositing = true;
|
2010-02-21 00:13:11 +03:00
|
|
|
}
|
|
|
|
}
|
2019-01-01 12:16:21 +03:00
|
|
|
|
|
|
|
} // namespace mozilla
|