tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

SVGAnimatedLengthList.cpp (7755B)


      1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
      2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
      3 /* This Source Code Form is subject to the terms of the Mozilla Public
      4 * License, v. 2.0. If a copy of the MPL was not distributed with this
      5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      6 
      7 #include "SVGAnimatedLengthList.h"
      8 
      9 #include <utility>
     10 
     11 #include "DOMSVGAnimatedLengthList.h"
     12 #include "SVGLengthListSMILType.h"
     13 #include "mozilla/SMILValue.h"
     14 #include "mozilla/Try.h"
     15 #include "mozilla/dom/SVGElement.h"
     16 #include "mozilla/dom/SVGLengthBinding.h"
     17 
     18 namespace mozilla {
     19 
     20 using namespace dom;
     21 
     22 nsresult SVGAnimatedLengthList::SetBaseValueString(const nsAString& aValue) {
     23  SVGLengthList newBaseValue;
     24  MOZ_TRY(newBaseValue.SetValueFromString(aValue));
     25 
     26  DOMSVGAnimatedLengthList* domWrapper =
     27      DOMSVGAnimatedLengthList::GetDOMWrapperIfExists(this);
     28  if (domWrapper) {
     29    // We must send this notification *before* changing mBaseVal! If the length
     30    // of our baseVal is being reduced, our baseVal's DOM wrapper list may have
     31    // to remove DOM items from itself, and any removed DOM items need to copy
     32    // their internal counterpart values *before* we change them.
     33    //
     34    domWrapper->InternalBaseValListWillChangeTo(newBaseValue);
     35  }
     36 
     37  // We don't need to call DidChange* here - we're only called by
     38  // SVGElement::ParseAttribute under Element::SetAttr,
     39  // which takes care of notifying.
     40  mBaseVal.SwapWith(newBaseValue);
     41  return NS_OK;
     42 }
     43 
     44 void SVGAnimatedLengthList::ClearBaseValue(uint32_t aAttrEnum) {
     45  DOMSVGAnimatedLengthList* domWrapper =
     46      DOMSVGAnimatedLengthList::GetDOMWrapperIfExists(this);
     47  if (domWrapper) {
     48    // We must send this notification *before* changing mBaseVal! (See above.)
     49    domWrapper->InternalBaseValListWillChangeTo(SVGLengthList());
     50  }
     51  mBaseVal.Clear();
     52  // Caller notifies
     53 }
     54 
     55 nsresult SVGAnimatedLengthList::SetAnimValue(const SVGLengthList& aNewAnimValue,
     56                                             SVGElement* aElement,
     57                                             uint32_t aAttrEnum) {
     58  DOMSVGAnimatedLengthList* domWrapper =
     59      DOMSVGAnimatedLengthList::GetDOMWrapperIfExists(this);
     60  if (domWrapper) {
     61    // A new animation may totally change the number of items in the animVal
     62    // list, replacing what was essentially a mirror of the baseVal list, or
     63    // else replacing and overriding an existing animation. When this happens
     64    // we must try and keep our animVal's DOM wrapper in sync (see the comment
     65    // in DOMSVGAnimatedLengthList::InternalBaseValListWillChangeTo).
     66    //
     67    // It's not possible for us to reliably distinguish between calls to this
     68    // method that are setting a new sample for an existing animation, and
     69    // calls that are setting the first sample of an animation that will
     70    // override an existing animation. Happily it's cheap to just blindly
     71    // notify our animVal's DOM wrapper of its internal counterpart's new value
     72    // each time this method is called, so that's what we do.
     73    //
     74    // Note that we must send this notification *before* setting or changing
     75    // mAnimVal! (See the comment in SetBaseValueString above.)
     76    //
     77    domWrapper->InternalAnimValListWillChangeTo(aNewAnimValue);
     78  }
     79  if (!mAnimVal) {
     80    mAnimVal = MakeUnique<SVGLengthList>();
     81  }
     82  nsresult rv = mAnimVal->CopyFrom(aNewAnimValue);
     83  if (NS_FAILED(rv)) {
     84    // OOM. We clear the animation, and, importantly, ClearAnimValue() ensures
     85    // that mAnimVal and its DOM wrapper (if any) will have the same length!
     86    ClearAnimValue(aElement, aAttrEnum);
     87    return rv;
     88  }
     89  aElement->DidAnimateLengthList(aAttrEnum);
     90  return NS_OK;
     91 }
     92 
     93 void SVGAnimatedLengthList::ClearAnimValue(SVGElement* aElement,
     94                                           uint32_t aAttrEnum) {
     95  DOMSVGAnimatedLengthList* domWrapper =
     96      DOMSVGAnimatedLengthList::GetDOMWrapperIfExists(this);
     97  if (domWrapper) {
     98    // When all animation ends, animVal simply mirrors baseVal, which may have
     99    // a different number of items to the last active animated value. We must
    100    // keep the length of our animVal's DOM wrapper list in sync, and again we
    101    // must do that before touching mAnimVal. See comments above.
    102    //
    103    domWrapper->InternalAnimValListWillChangeTo(mBaseVal);
    104  }
    105  mAnimVal = nullptr;
    106  aElement->DidAnimateLengthList(aAttrEnum);
    107 }
    108 
    109 UniquePtr<SMILAttr> SVGAnimatedLengthList::ToSMILAttr(SVGElement* aSVGElement,
    110                                                      uint8_t aAttrEnum,
    111                                                      uint8_t aAxis,
    112                                                      bool aCanZeroPadList) {
    113  return MakeUnique<SMILAnimatedLengthList>(this, aSVGElement, aAttrEnum, aAxis,
    114                                            aCanZeroPadList);
    115 }
    116 
    117 nsresult SVGAnimatedLengthList::SMILAnimatedLengthList::ValueFromString(
    118    const nsAString& aStr, const dom::SVGAnimationElement* /*aSrcElement*/,
    119    SMILValue& aValue, bool& aPreventCachingOfSandwich) const {
    120  SMILValue val(&SVGLengthListSMILType::sSingleton);
    121  SVGLengthListAndInfo* llai = static_cast<SVGLengthListAndInfo*>(val.mU.mPtr);
    122  nsresult rv = llai->SetValueFromString(aStr);
    123  if (NS_SUCCEEDED(rv)) {
    124    llai->SetInfo(mElement, mAxis, mCanZeroPadList);
    125    aValue = std::move(val);
    126 
    127    // If any of the lengths in the list depend on their context, then we must
    128    // prevent caching of the entire animation sandwich. This is because the
    129    // units of a length at a given index can change from sandwich layer to
    130    // layer, and indeed even be different within a single sandwich layer. If
    131    // any length in the result of an animation sandwich is the result of the
    132    // addition of lengths where one or more of those lengths is context
    133    // dependent, then naturally the resultant length is also context
    134    // dependent, regardless of whether its actual unit is context dependent or
    135    // not. Unfortunately normal invalidation mechanisms won't cause us to
    136    // recalculate the result of the sandwich if the context changes, so we
    137    // take the (substantial) performance hit of preventing caching of the
    138    // sandwich layer, causing the animation sandwich to be recalculated every
    139    // single sample.
    140 
    141    for (uint32_t i = 0; i < llai->Length(); ++i) {
    142      uint8_t unit = (*llai)[i].GetUnit();
    143      if (!SVGLength::IsAbsoluteUnit(unit)) {
    144        aPreventCachingOfSandwich = true;
    145        break;
    146      }
    147    }
    148  }
    149  return rv;
    150 }
    151 
    152 SMILValue SVGAnimatedLengthList::SMILAnimatedLengthList::GetBaseValue() const {
    153  // To benefit from Return Value Optimization and avoid copy constructor calls
    154  // due to our use of return-by-value, we must return the exact same object
    155  // from ALL return points. This function must only return THIS variable:
    156  SMILValue val;
    157 
    158  SMILValue tmp(&SVGLengthListSMILType::sSingleton);
    159  SVGLengthListAndInfo* llai = static_cast<SVGLengthListAndInfo*>(tmp.mU.mPtr);
    160  nsresult rv = llai->CopyFrom(mVal->mBaseVal);
    161  if (NS_SUCCEEDED(rv)) {
    162    llai->SetInfo(mElement, mAxis, mCanZeroPadList);
    163    val = std::move(tmp);
    164  }
    165  return val;
    166 }
    167 
    168 nsresult SVGAnimatedLengthList::SMILAnimatedLengthList::SetAnimValue(
    169    const SMILValue& aValue) {
    170  NS_ASSERTION(aValue.mType == &SVGLengthListSMILType::sSingleton,
    171               "Unexpected type to assign animated value");
    172  if (aValue.mType == &SVGLengthListSMILType::sSingleton) {
    173    mVal->SetAnimValue(*static_cast<SVGLengthListAndInfo*>(aValue.mU.mPtr),
    174                       mElement, mAttrEnum);
    175  }
    176  return NS_OK;
    177 }
    178 
    179 void SVGAnimatedLengthList::SMILAnimatedLengthList::ClearAnimValue() {
    180  if (mVal->mAnimVal) {
    181    mVal->ClearAnimValue(mElement, mAttrEnum);
    182  }
    183 }
    184 
    185 }  // namespace mozilla