dom/smil/nsSMILCompositor.cpp

Tue, 06 Jan 2015 21:39:09 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Tue, 06 Jan 2015 21:39:09 +0100
branch
TOR_BUG_9701
changeset 8
97036ab72558
permissions
-rw-r--r--

Conditionally force memory storage according to privacy.thirdparty.isolate;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

michael@0 1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 #include "nsSMILCompositor.h"
michael@0 7 #include "nsSMILCSSProperty.h"
michael@0 8 #include "nsCSSProps.h"
michael@0 9 #include "nsHashKeys.h"
michael@0 10
michael@0 11 // PLDHashEntryHdr methods
michael@0 12 bool
michael@0 13 nsSMILCompositor::KeyEquals(KeyTypePointer aKey) const
michael@0 14 {
michael@0 15 return aKey && aKey->Equals(mKey);
michael@0 16 }
michael@0 17
michael@0 18 /*static*/ PLDHashNumber
michael@0 19 nsSMILCompositor::HashKey(KeyTypePointer aKey)
michael@0 20 {
michael@0 21 // Combine the 3 values into one numeric value, which will be hashed.
michael@0 22 // NOTE: We right-shift one of the pointers by 2 to get some randomness in
michael@0 23 // its 2 lowest-order bits. (Those shifted-off bits will always be 0 since
michael@0 24 // our pointers will be word-aligned.)
michael@0 25 return (NS_PTR_TO_UINT32(aKey->mElement.get()) >> 2) +
michael@0 26 NS_PTR_TO_UINT32(aKey->mAttributeName.get()) +
michael@0 27 (aKey->mIsCSS ? 1 : 0);
michael@0 28 }
michael@0 29
michael@0 30 // Cycle-collection support
michael@0 31 void
michael@0 32 nsSMILCompositor::Traverse(nsCycleCollectionTraversalCallback* aCallback)
michael@0 33 {
michael@0 34 if (!mKey.mElement)
michael@0 35 return;
michael@0 36
michael@0 37 NS_CYCLE_COLLECTION_NOTE_EDGE_NAME(*aCallback, "Compositor mKey.mElement");
michael@0 38 aCallback->NoteXPCOMChild(mKey.mElement);
michael@0 39 }
michael@0 40
michael@0 41 // Other methods
michael@0 42 void
michael@0 43 nsSMILCompositor::AddAnimationFunction(nsSMILAnimationFunction* aFunc)
michael@0 44 {
michael@0 45 if (aFunc) {
michael@0 46 mAnimationFunctions.AppendElement(aFunc);
michael@0 47 }
michael@0 48 }
michael@0 49
michael@0 50 void
michael@0 51 nsSMILCompositor::ComposeAttribute()
michael@0 52 {
michael@0 53 if (!mKey.mElement)
michael@0 54 return;
michael@0 55
michael@0 56 // FIRST: Get the nsISMILAttr (to grab base value from, and to eventually
michael@0 57 // give animated value to)
michael@0 58 nsAutoPtr<nsISMILAttr> smilAttr(CreateSMILAttr());
michael@0 59 if (!smilAttr) {
michael@0 60 // Target attribute not found (or, out of memory)
michael@0 61 return;
michael@0 62 }
michael@0 63 if (mAnimationFunctions.IsEmpty()) {
michael@0 64 // No active animation functions. (We can still have a nsSMILCompositor in
michael@0 65 // that case if an animation function has *just* become inactive)
michael@0 66 smilAttr->ClearAnimValue();
michael@0 67 return;
michael@0 68 }
michael@0 69
michael@0 70 // SECOND: Sort the animationFunctions, to prepare for compositing.
michael@0 71 nsSMILAnimationFunction::Comparator comparator;
michael@0 72 mAnimationFunctions.Sort(comparator);
michael@0 73
michael@0 74 // THIRD: Step backwards through animation functions to find out
michael@0 75 // which ones we actually care about.
michael@0 76 uint32_t firstFuncToCompose = GetFirstFuncToAffectSandwich();
michael@0 77
michael@0 78 // FOURTH: Get & cache base value
michael@0 79 nsSMILValue sandwichResultValue;
michael@0 80 if (!mAnimationFunctions[firstFuncToCompose]->WillReplace()) {
michael@0 81 sandwichResultValue = smilAttr->GetBaseValue();
michael@0 82 }
michael@0 83 UpdateCachedBaseValue(sandwichResultValue);
michael@0 84
michael@0 85 if (!mForceCompositing) {
michael@0 86 return;
michael@0 87 }
michael@0 88
michael@0 89 // FIFTH: Compose animation functions
michael@0 90 uint32_t length = mAnimationFunctions.Length();
michael@0 91 for (uint32_t i = firstFuncToCompose; i < length; ++i) {
michael@0 92 mAnimationFunctions[i]->ComposeResult(*smilAttr, sandwichResultValue);
michael@0 93 }
michael@0 94 if (sandwichResultValue.IsNull()) {
michael@0 95 smilAttr->ClearAnimValue();
michael@0 96 return;
michael@0 97 }
michael@0 98
michael@0 99 // SIXTH: Set the animated value to the final composited result.
michael@0 100 nsresult rv = smilAttr->SetAnimValue(sandwichResultValue);
michael@0 101 if (NS_FAILED(rv)) {
michael@0 102 NS_WARNING("nsISMILAttr::SetAnimValue failed");
michael@0 103 }
michael@0 104 }
michael@0 105
michael@0 106 void
michael@0 107 nsSMILCompositor::ClearAnimationEffects()
michael@0 108 {
michael@0 109 if (!mKey.mElement || !mKey.mAttributeName)
michael@0 110 return;
michael@0 111
michael@0 112 nsAutoPtr<nsISMILAttr> smilAttr(CreateSMILAttr());
michael@0 113 if (!smilAttr) {
michael@0 114 // Target attribute not found (or, out of memory)
michael@0 115 return;
michael@0 116 }
michael@0 117 smilAttr->ClearAnimValue();
michael@0 118 }
michael@0 119
michael@0 120 // Protected Helper Functions
michael@0 121 // --------------------------
michael@0 122 nsISMILAttr*
michael@0 123 nsSMILCompositor::CreateSMILAttr()
michael@0 124 {
michael@0 125 if (mKey.mIsCSS) {
michael@0 126 nsCSSProperty propId =
michael@0 127 nsCSSProps::LookupProperty(nsDependentAtomString(mKey.mAttributeName),
michael@0 128 nsCSSProps::eEnabledForAllContent);
michael@0 129 if (nsSMILCSSProperty::IsPropertyAnimatable(propId)) {
michael@0 130 return new nsSMILCSSProperty(propId, mKey.mElement.get());
michael@0 131 }
michael@0 132 } else {
michael@0 133 return mKey.mElement->GetAnimatedAttr(mKey.mAttributeNamespaceID,
michael@0 134 mKey.mAttributeName);
michael@0 135 }
michael@0 136 return nullptr;
michael@0 137 }
michael@0 138
michael@0 139 uint32_t
michael@0 140 nsSMILCompositor::GetFirstFuncToAffectSandwich()
michael@0 141 {
michael@0 142 uint32_t i;
michael@0 143 for (i = mAnimationFunctions.Length(); i > 0; --i) {
michael@0 144 nsSMILAnimationFunction* curAnimFunc = mAnimationFunctions[i-1];
michael@0 145 // In the following, the lack of short-circuit behavior of |= means that we
michael@0 146 // will ALWAYS run UpdateCachedTarget (even if mForceCompositing is true)
michael@0 147 // but only call HasChanged and WasSkippedInPrevSample if necessary. This
michael@0 148 // is important since we need UpdateCachedTarget to run in order to detect
michael@0 149 // changes to the target in subsequent samples.
michael@0 150 mForceCompositing |=
michael@0 151 curAnimFunc->UpdateCachedTarget(mKey) ||
michael@0 152 curAnimFunc->HasChanged() ||
michael@0 153 curAnimFunc->WasSkippedInPrevSample();
michael@0 154
michael@0 155 if (curAnimFunc->WillReplace()) {
michael@0 156 --i;
michael@0 157 break;
michael@0 158 }
michael@0 159 }
michael@0 160 // Mark remaining animation functions as having been skipped so if we later
michael@0 161 // use them we'll know to force compositing.
michael@0 162 // Note that we only really need to do this if something has changed
michael@0 163 // (otherwise we would have set the flag on a previous sample) and if
michael@0 164 // something has changed mForceCompositing will be true.
michael@0 165 if (mForceCompositing) {
michael@0 166 for (uint32_t j = i; j > 0; --j) {
michael@0 167 mAnimationFunctions[j-1]->SetWasSkipped();
michael@0 168 }
michael@0 169 }
michael@0 170 return i;
michael@0 171 }
michael@0 172
michael@0 173 void
michael@0 174 nsSMILCompositor::UpdateCachedBaseValue(const nsSMILValue& aBaseValue)
michael@0 175 {
michael@0 176 if (!mCachedBaseValue) {
michael@0 177 // We don't have last sample's base value cached. Assume it's changed.
michael@0 178 mCachedBaseValue = new nsSMILValue(aBaseValue);
michael@0 179 NS_WARN_IF_FALSE(mCachedBaseValue, "failed to cache base value (OOM?)");
michael@0 180 mForceCompositing = true;
michael@0 181 } else if (*mCachedBaseValue != aBaseValue) {
michael@0 182 // Base value has changed since last sample.
michael@0 183 *mCachedBaseValue = aBaseValue;
michael@0 184 mForceCompositing = true;
michael@0 185 }
michael@0 186 }

mercurial