gecko-dev/dom/smil/nsSMILCompositor.h
Nathan Froyd e7b3b3140d Bug 1415980 - make hash keys movable and not copyable; r=erahm
Everything that goes in a PLDHashtable (and its derivatives, like
nsTHashtable) needs to inherit from PLDHashEntryHdr. But through a lack
of enforcement, copy constructors for these derived classes didn't
explicitly invoke the copy constructor for PLDHashEntryHdr (and the
compiler didn't invoke the copy constructor for us). Instead,
PLDHashTable explicitly copied around the bits that the copy constructor
would have.

The current setup has two problems:

1) Derived classes should be using move construction, not copy
   construction, since anything that's shuffling hash table keys/entries
   around will be using move construction.

2) Derived classes should take responsibility for transferring bits of
   superclass state around, and not rely on something else to handle that.

The second point is not a huge problem for PLDHashTable (PLDHashTable
only has to copy PLDHashEntryHdr's bits in a single place), but future
hash table implementations that might move entries around more
aggressively would have to insert compensation code all over the
place. Additionally, if moving entries is implemented via memcpy (which
is quite common), PLDHashTable copying around bits *again* is
inefficient.

Let's fix all these problems in one go, by:

1) Explicitly declaring the set of constructors that PLDHashEntryHdr
   implements (and does not implement). In particular, the copy
   constructor is deleted, so any derived classes that attempt to make
   themselves copyable will be detected at compile time: the compiler
   will complain that the superclass type is not copyable.

This change on its own will result in many compiler errors, so...

2) Change any derived classes to implement move constructors instead of
   copy constructors. Note that some of these move constructors are,
   strictly speaking, unnecessary, since the relevant classes are moved
   via memcpy in nsTHashtable and its derivatives.
2018-09-20 11:20:36 -04:00

127 lines
4.9 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/. */
#ifndef NS_SMILCOMPOSITOR_H_
#define NS_SMILCOMPOSITOR_H_
#include "mozilla/Move.h"
#include "mozilla/UniquePtr.h"
#include "nsTHashtable.h"
#include "nsString.h"
#include "nsSMILAnimationFunction.h"
#include "nsSMILTargetIdentifier.h"
#include "nsSMILCompositorTable.h"
#include "PLDHashTable.h"
//----------------------------------------------------------------------
// nsSMILCompositor
//
// Performs the composition of the animation sandwich by combining the results
// of a series animation functions according to the rules of SMIL composition
// including prioritising animations.
class nsSMILCompositor : public PLDHashEntryHdr
{
public:
typedef nsSMILTargetIdentifier KeyType;
typedef const KeyType& KeyTypeRef;
typedef const KeyType* KeyTypePointer;
explicit nsSMILCompositor(KeyTypePointer aKey)
: mKey(*aKey),
mForceCompositing(false)
{ }
nsSMILCompositor(nsSMILCompositor&& toMove)
: PLDHashEntryHdr(std::move(toMove)),
mKey(std::move(toMove.mKey)),
mAnimationFunctions(std::move(toMove.mAnimationFunctions)),
mForceCompositing(false)
{ }
~nsSMILCompositor() { }
// PLDHashEntryHdr methods
KeyTypeRef GetKey() const { return mKey; }
bool KeyEquals(KeyTypePointer aKey) const;
static KeyTypePointer KeyToPointer(KeyTypeRef aKey) { return &aKey; }
static PLDHashNumber HashKey(KeyTypePointer aKey);
enum { ALLOW_MEMMOVE = false };
// Adds the given animation function to this Compositor's list of functions
void AddAnimationFunction(nsSMILAnimationFunction* aFunc);
// Composes the attribute's current value with the list of animation
// functions, and assigns the resulting value to this compositor's target
// attribute. If a change is made that might produce style updates,
// aMightHavePendingStyleUpdates is set to true. Otherwise it is not modified.
void ComposeAttribute(bool& aMightHavePendingStyleUpdates);
// Clears animation effects on my target attribute
void ClearAnimationEffects();
// Cycle-collection support
void Traverse(nsCycleCollectionTraversalCallback* aCallback);
// Toggles a bit that will force us to composite (bypassing early-return
// optimizations) when we hit ComposeAttribute.
void ToggleForceCompositing() { mForceCompositing = true; }
// Transfers |aOther|'s mCachedBaseValue to |this|
void StealCachedBaseValue(nsSMILCompositor* aOther) {
mCachedBaseValue = std::move(aOther->mCachedBaseValue);
}
private:
// Create a nsISMILAttr for my target, on the heap.
//
// @param aBaseComputedStyle An optional ComputedStyle which, if set, will be
// used when fetching the base style.
mozilla::UniquePtr<nsISMILAttr>
CreateSMILAttr(mozilla::ComputedStyle* aBaseComputedStyle);
// Returns the CSS property this compositor should animate, or
// eCSSProperty_UNKNOWN if this compositor does not animate a CSS property.
nsCSSPropertyID GetCSSPropertyToAnimate() const;
// Returns true if we might need to refer to base styles (i.e. we are
// targeting a CSS property and have one or more animation functions that
// don't just replace the underlying value).
//
// This might return true in some cases where we don't actually need the base
// style since it doesn't build up the animation sandwich to check if the
// functions that appear to need the base style are actually replaced by
// a function further up the stack.
bool MightNeedBaseStyle() const;
// Finds the index of the first function that will affect our animation
// sandwich. Also toggles the 'mForceCompositing' flag if it finds that any
// (used) functions have changed.
uint32_t GetFirstFuncToAffectSandwich();
// If the passed-in base value differs from our cached base value, this
// method updates the cached value (and toggles the 'mForceCompositing' flag)
void UpdateCachedBaseValue(const nsSMILValue& aBaseValue);
// The hash key (tuple of element and attributeName)
KeyType mKey;
// Hash Value: List of animation functions that animate the specified attr
nsTArray<nsSMILAnimationFunction*> mAnimationFunctions;
// Member data for detecting when we need to force-recompose
// ---------------------------------------------------------
// Flag for tracking whether we need to compose. Initialized to false, but
// gets flipped to true if we detect that something has changed.
bool mForceCompositing;
// Cached base value, so we can detect & force-recompose when it changes
// from one sample to the next. (nsSMILAnimationController moves this
// forward from the previous sample's compositor by calling
// StealCachedBaseValue.)
nsSMILValue mCachedBaseValue;
};
#endif // NS_SMILCOMPOSITOR_H_