This repository has been archived by the owner on Aug 4, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathnsSMILTimedElement.cpp
2345 lines (2029 loc) · 75.1 KB
/
nsSMILTimedElement.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* 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 "mozilla/DebugOnly.h"
#include "nsSMILTimedElement.h"
#include "nsAttrValueInlines.h"
#include "nsSMILAnimationFunction.h"
#include "nsSMILTimeValue.h"
#include "nsSMILTimeValueSpec.h"
#include "nsSMILInstanceTime.h"
#include "nsSMILParserUtils.h"
#include "nsSMILTimeContainer.h"
#include "nsGkAtoms.h"
#include "nsGUIEvent.h"
#include "nsEventDispatcher.h"
#include "nsReadableUtils.h"
#include "nsMathUtils.h"
#include "nsThreadUtils.h"
#include "nsIPresShell.h"
#include "prdtoa.h"
#include "plstr.h"
#include "prtime.h"
#include "nsString.h"
#include "mozilla/AutoRestore.h"
#include "nsCharSeparatedTokenizer.h"
#include <algorithm>
using namespace mozilla;
//----------------------------------------------------------------------
// Helper class: InstanceTimeComparator
// Upon inserting an instance time into one of our instance time lists we assign
// it a serial number. This allows us to sort the instance times in such a way
// that where we have several equal instance times, the ones added later will
// sort later. This means that when we call UpdateCurrentInterval during the
// waiting state we won't unnecessarily change the begin instance.
//
// The serial number also means that every instance time has an unambiguous
// position in the array so we can use RemoveElementSorted and the like.
bool
nsSMILTimedElement::InstanceTimeComparator::Equals(
const nsSMILInstanceTime* aElem1,
const nsSMILInstanceTime* aElem2) const
{
NS_ABORT_IF_FALSE(aElem1 && aElem2,
"Trying to compare null instance time pointers");
NS_ABORT_IF_FALSE(aElem1->Serial() && aElem2->Serial(),
"Instance times have not been assigned serial numbers");
NS_ABORT_IF_FALSE(aElem1 == aElem2 || aElem1->Serial() != aElem2->Serial(),
"Serial numbers are not unique");
return aElem1->Serial() == aElem2->Serial();
}
bool
nsSMILTimedElement::InstanceTimeComparator::LessThan(
const nsSMILInstanceTime* aElem1,
const nsSMILInstanceTime* aElem2) const
{
NS_ABORT_IF_FALSE(aElem1 && aElem2,
"Trying to compare null instance time pointers");
NS_ABORT_IF_FALSE(aElem1->Serial() && aElem2->Serial(),
"Instance times have not been assigned serial numbers");
int8_t cmp = aElem1->Time().CompareTo(aElem2->Time());
return cmp == 0 ? aElem1->Serial() < aElem2->Serial() : cmp < 0;
}
//----------------------------------------------------------------------
// Helper class: AsyncTimeEventRunner
namespace
{
class AsyncTimeEventRunner : public nsRunnable
{
protected:
nsRefPtr<nsIContent> mTarget;
uint32_t mMsg;
int32_t mDetail;
public:
AsyncTimeEventRunner(nsIContent* aTarget, uint32_t aMsg, int32_t aDetail)
: mTarget(aTarget), mMsg(aMsg), mDetail(aDetail)
{
}
NS_IMETHOD Run()
{
nsUIEvent event(true, mMsg, mDetail);
event.eventStructType = NS_SMIL_TIME_EVENT;
nsPresContext* context = nullptr;
nsIDocument* doc = mTarget->GetCurrentDoc();
if (doc) {
nsCOMPtr<nsIPresShell> shell = doc->GetShell();
if (shell) {
context = shell->GetPresContext();
}
}
return nsEventDispatcher::Dispatch(mTarget, context, &event);
}
};
}
//----------------------------------------------------------------------
// Helper class: AutoIntervalUpdateBatcher
// RAII helper to set the mDeferIntervalUpdates flag on an nsSMILTimedElement
// and perform the UpdateCurrentInterval when the object is destroyed.
//
// If several of these objects are allocated on the stack, the update will not
// be performed until the last object for a given nsSMILTimedElement is
// destroyed.
class NS_STACK_CLASS nsSMILTimedElement::AutoIntervalUpdateBatcher
{
public:
AutoIntervalUpdateBatcher(nsSMILTimedElement& aTimedElement)
: mTimedElement(aTimedElement),
mDidSetFlag(!aTimedElement.mDeferIntervalUpdates)
{
mTimedElement.mDeferIntervalUpdates = true;
}
~AutoIntervalUpdateBatcher()
{
if (!mDidSetFlag)
return;
mTimedElement.mDeferIntervalUpdates = false;
if (mTimedElement.mDoDeferredUpdate) {
mTimedElement.mDoDeferredUpdate = false;
mTimedElement.UpdateCurrentInterval();
}
}
private:
nsSMILTimedElement& mTimedElement;
bool mDidSetFlag;
};
//----------------------------------------------------------------------
// Templated helper functions
// Selectively remove elements from an array of type
// nsTArray<nsRefPtr<nsSMILInstanceTime> > with O(n) performance.
template <class TestFunctor>
void
nsSMILTimedElement::RemoveInstanceTimes(InstanceTimeList& aArray,
TestFunctor& aTest)
{
InstanceTimeList newArray;
for (uint32_t i = 0; i < aArray.Length(); ++i) {
nsSMILInstanceTime* item = aArray[i].get();
if (aTest(item, i)) {
// As per bugs 665334 and 669225 we should be careful not to remove the
// instance time that corresponds to the previous interval's end time.
//
// Most functors supplied here fulfil this condition by checking if the
// instance time is marked as "ShouldPreserve" and if so, not deleting it.
//
// However, when filtering instance times, we sometimes need to drop even
// instance times marked as "ShouldPreserve". In that case we take special
// care not to delete the end instance time of the previous interval.
NS_ABORT_IF_FALSE(!GetPreviousInterval() ||
item != GetPreviousInterval()->End(),
"Removing end instance time of previous interval");
item->Unlink();
} else {
newArray.AppendElement(item);
}
}
aArray.Clear();
aArray.SwapElements(newArray);
}
//----------------------------------------------------------------------
// Static members
nsAttrValue::EnumTable nsSMILTimedElement::sFillModeTable[] = {
{"remove", FILL_REMOVE},
{"freeze", FILL_FREEZE},
{nullptr, 0}
};
nsAttrValue::EnumTable nsSMILTimedElement::sRestartModeTable[] = {
{"always", RESTART_ALWAYS},
{"whenNotActive", RESTART_WHENNOTACTIVE},
{"never", RESTART_NEVER},
{nullptr, 0}
};
const nsSMILMilestone nsSMILTimedElement::sMaxMilestone(INT64_MAX, false);
// The thresholds at which point we start filtering intervals and instance times
// indiscriminately.
// See FilterIntervals and FilterInstanceTimes.
const uint8_t nsSMILTimedElement::sMaxNumIntervals = 20;
const uint8_t nsSMILTimedElement::sMaxNumInstanceTimes = 100;
// Detect if we arrive in some sort of undetected recursive syncbase dependency
// relationship
const uint8_t nsSMILTimedElement::sMaxUpdateIntervalRecursionDepth = 20;
//----------------------------------------------------------------------
// Ctor, dtor
nsSMILTimedElement::nsSMILTimedElement()
:
mAnimationElement(nullptr),
mFillMode(FILL_REMOVE),
mRestartMode(RESTART_ALWAYS),
mInstanceSerialIndex(0),
mClient(nullptr),
mCurrentInterval(nullptr),
mCurrentRepeatIteration(0),
mPrevRegisteredMilestone(sMaxMilestone),
mElementState(STATE_STARTUP),
mSeekState(SEEK_NOT_SEEKING),
mDeferIntervalUpdates(false),
mDoDeferredUpdate(false),
mDeleteCount(0),
mUpdateIntervalRecursionDepth(0)
{
mSimpleDur.SetIndefinite();
mMin.SetMillis(0L);
mMax.SetIndefinite();
mTimeDependents.Init();
}
nsSMILTimedElement::~nsSMILTimedElement()
{
// Unlink all instance times from dependent intervals
for (uint32_t i = 0; i < mBeginInstances.Length(); ++i) {
mBeginInstances[i]->Unlink();
}
mBeginInstances.Clear();
for (uint32_t i = 0; i < mEndInstances.Length(); ++i) {
mEndInstances[i]->Unlink();
}
mEndInstances.Clear();
// Notify anyone listening to our intervals that they're gone
// (We shouldn't get any callbacks from this because all our instance times
// are now disassociated with any intervals)
ClearIntervals();
// The following assertions are important in their own right (for checking
// correct behavior) but also because AutoIntervalUpdateBatcher holds pointers
// to class so if they fail there's the possibility we might have dangling
// pointers.
NS_ABORT_IF_FALSE(!mDeferIntervalUpdates,
"Interval updates should no longer be blocked when an nsSMILTimedElement "
"disappears");
NS_ABORT_IF_FALSE(!mDoDeferredUpdate,
"There should no longer be any pending updates when an "
"nsSMILTimedElement disappears");
}
void
nsSMILTimedElement::SetAnimationElement(nsISMILAnimationElement* aElement)
{
NS_ABORT_IF_FALSE(aElement, "NULL owner element");
NS_ABORT_IF_FALSE(!mAnimationElement, "Re-setting owner");
mAnimationElement = aElement;
}
nsSMILTimeContainer*
nsSMILTimedElement::GetTimeContainer()
{
return mAnimationElement ? mAnimationElement->GetTimeContainer() : nullptr;
}
//----------------------------------------------------------------------
// nsIDOMElementTimeControl methods
//
// The definition of the ElementTimeControl interface differs between SMIL
// Animation and SVG 1.1. In SMIL Animation all methods have a void return
// type and the new instance time is simply added to the list and restart
// semantics are applied as with any other instance time. In the SVG definition
// the methods return a bool depending on the restart mode.
//
// This inconsistency has now been addressed by an erratum in SVG 1.1:
//
// http://www.w3.org/2003/01/REC-SVG11-20030114-errata#elementtimecontrol-interface
//
// which favours the definition in SMIL, i.e. instance times are just added
// without first checking the restart mode.
nsresult
nsSMILTimedElement::BeginElementAt(double aOffsetSeconds)
{
nsSMILTimeContainer* container = GetTimeContainer();
if (!container)
return NS_ERROR_FAILURE;
nsSMILTime currentTime = container->GetCurrentTime();
return AddInstanceTimeFromCurrentTime(currentTime, aOffsetSeconds, true);
}
nsresult
nsSMILTimedElement::EndElementAt(double aOffsetSeconds)
{
nsSMILTimeContainer* container = GetTimeContainer();
if (!container)
return NS_ERROR_FAILURE;
nsSMILTime currentTime = container->GetCurrentTime();
return AddInstanceTimeFromCurrentTime(currentTime, aOffsetSeconds, false);
}
//----------------------------------------------------------------------
// nsSVGAnimationElement methods
nsSMILTimeValue
nsSMILTimedElement::GetStartTime() const
{
return mElementState == STATE_WAITING || mElementState == STATE_ACTIVE
? mCurrentInterval->Begin()->Time()
: nsSMILTimeValue();
}
//----------------------------------------------------------------------
// Hyperlinking support
nsSMILTimeValue
nsSMILTimedElement::GetHyperlinkTime() const
{
nsSMILTimeValue hyperlinkTime; // Default ctor creates unresolved time
if (mElementState == STATE_ACTIVE) {
hyperlinkTime = mCurrentInterval->Begin()->Time();
} else if (!mBeginInstances.IsEmpty()) {
hyperlinkTime = mBeginInstances[0]->Time();
}
return hyperlinkTime;
}
//----------------------------------------------------------------------
// nsSMILTimedElement
void
nsSMILTimedElement::AddInstanceTime(nsSMILInstanceTime* aInstanceTime,
bool aIsBegin)
{
NS_ABORT_IF_FALSE(aInstanceTime, "Attempting to add null instance time");
// Event-sensitivity: If an element is not active (but the parent time
// container is), then events are only handled for begin specifications.
if (mElementState != STATE_ACTIVE && !aIsBegin &&
aInstanceTime->IsDynamic())
{
// No need to call Unlink here--dynamic instance times shouldn't be linked
// to anything that's going to miss them
NS_ABORT_IF_FALSE(!aInstanceTime->GetBaseInterval(),
"Dynamic instance time has a base interval--we probably need to unlink"
" it if we're not going to use it");
return;
}
aInstanceTime->SetSerial(++mInstanceSerialIndex);
InstanceTimeList& instanceList = aIsBegin ? mBeginInstances : mEndInstances;
nsRefPtr<nsSMILInstanceTime>* inserted =
instanceList.InsertElementSorted(aInstanceTime, InstanceTimeComparator());
if (!inserted) {
NS_WARNING("Insufficient memory to insert instance time");
return;
}
UpdateCurrentInterval();
}
void
nsSMILTimedElement::UpdateInstanceTime(nsSMILInstanceTime* aInstanceTime,
nsSMILTimeValue& aUpdatedTime,
bool aIsBegin)
{
NS_ABORT_IF_FALSE(aInstanceTime, "Attempting to update null instance time");
// The reason we update the time here and not in the nsSMILTimeValueSpec is
// that it means we *could* re-sort more efficiently by doing a sorted remove
// and insert but currently this doesn't seem to be necessary given how
// infrequently we get these change notices.
aInstanceTime->DependentUpdate(aUpdatedTime);
InstanceTimeList& instanceList = aIsBegin ? mBeginInstances : mEndInstances;
instanceList.Sort(InstanceTimeComparator());
// Generally speaking, UpdateCurrentInterval makes changes to the current
// interval and sends changes notices itself. However, in this case because
// instance times are shared between the instance time list and the intervals
// we are effectively changing the current interval outside
// UpdateCurrentInterval so we need to explicitly signal that we've made
// a change.
//
// This wouldn't be necessary if we cloned instance times on adding them to
// the current interval but this introduces other complications (particularly
// detecting which instance time is being used to define the begin of the
// current interval when doing a Reset).
bool changedCurrentInterval = mCurrentInterval &&
(mCurrentInterval->Begin() == aInstanceTime ||
mCurrentInterval->End() == aInstanceTime);
UpdateCurrentInterval(changedCurrentInterval);
}
void
nsSMILTimedElement::RemoveInstanceTime(nsSMILInstanceTime* aInstanceTime,
bool aIsBegin)
{
NS_ABORT_IF_FALSE(aInstanceTime, "Attempting to remove null instance time");
// If the instance time should be kept (because it is or was the fixed end
// point of an interval) then just disassociate it from the creator.
if (aInstanceTime->ShouldPreserve()) {
aInstanceTime->Unlink();
return;
}
InstanceTimeList& instanceList = aIsBegin ? mBeginInstances : mEndInstances;
mozilla::DebugOnly<bool> found =
instanceList.RemoveElementSorted(aInstanceTime, InstanceTimeComparator());
NS_ABORT_IF_FALSE(found, "Couldn't find instance time to delete");
UpdateCurrentInterval();
}
namespace
{
class NS_STACK_CLASS RemoveByCreator
{
public:
RemoveByCreator(const nsSMILTimeValueSpec* aCreator) : mCreator(aCreator)
{ }
bool operator()(nsSMILInstanceTime* aInstanceTime, uint32_t /*aIndex*/)
{
if (aInstanceTime->GetCreator() != mCreator)
return false;
// If the instance time should be kept (because it is or was the fixed end
// point of an interval) then just disassociate it from the creator.
if (aInstanceTime->ShouldPreserve()) {
aInstanceTime->Unlink();
return false;
}
return true;
}
private:
const nsSMILTimeValueSpec* mCreator;
};
}
void
nsSMILTimedElement::RemoveInstanceTimesForCreator(
const nsSMILTimeValueSpec* aCreator, bool aIsBegin)
{
NS_ABORT_IF_FALSE(aCreator, "Creator not set");
InstanceTimeList& instances = aIsBegin ? mBeginInstances : mEndInstances;
RemoveByCreator removeByCreator(aCreator);
RemoveInstanceTimes(instances, removeByCreator);
UpdateCurrentInterval();
}
void
nsSMILTimedElement::SetTimeClient(nsSMILAnimationFunction* aClient)
{
//
// No need to check for NULL. A NULL parameter simply means to remove the
// previous client which we do by setting to NULL anyway.
//
mClient = aClient;
}
void
nsSMILTimedElement::SampleAt(nsSMILTime aContainerTime)
{
// Milestones are cleared before a sample
mPrevRegisteredMilestone = sMaxMilestone;
DoSampleAt(aContainerTime, false);
}
void
nsSMILTimedElement::SampleEndAt(nsSMILTime aContainerTime)
{
// Milestones are cleared before a sample
mPrevRegisteredMilestone = sMaxMilestone;
// If the current interval changes, we don't bother trying to remove any old
// milestones we'd registered. So it's possible to get a call here to end an
// interval at a time that no longer reflects the end of the current interval.
//
// For now we just check that we're actually in an interval but note that the
// initial sample we use to initialise the model is an end sample. This is
// because we want to resolve all the instance times before committing to an
// initial interval. Therefore an end sample from the startup state is also
// acceptable.
if (mElementState == STATE_ACTIVE || mElementState == STATE_STARTUP) {
DoSampleAt(aContainerTime, true); // End sample
} else {
// Even if this was an unnecessary milestone sample we want to be sure that
// our next real milestone is registered.
RegisterMilestone();
}
}
void
nsSMILTimedElement::DoSampleAt(nsSMILTime aContainerTime, bool aEndOnly)
{
NS_ABORT_IF_FALSE(mAnimationElement,
"Got sample before being registered with an animation element");
NS_ABORT_IF_FALSE(GetTimeContainer(),
"Got sample without being registered with a time container");
// This could probably happen if we later implement externalResourcesRequired
// (bug 277955) and whilst waiting for those resources (and the animation to
// start) we transfer a node from another document fragment that has already
// started. In such a case we might receive milestone samples registered with
// the already active container.
if (GetTimeContainer()->IsPausedByType(nsSMILTimeContainer::PAUSE_BEGIN))
return;
// We use an end-sample to start animation since an end-sample lets us
// tentatively create an interval without committing to it (by transitioning
// to the ACTIVE state) and this is necessary because we might have
// dependencies on other animations that are yet to start. After these
// other animations start, it may be necessary to revise our initial interval.
//
// However, sometimes instead of an end-sample we can get a regular sample
// during STARTUP state. This can happen, for example, if we register
// a milestone before time t=0 and are then re-bound to the tree (which sends
// us back to the STARTUP state). In such a case we should just ignore the
// sample and wait for our real initial sample which will be an end-sample.
if (mElementState == STATE_STARTUP && !aEndOnly)
return;
bool finishedSeek = false;
if (GetTimeContainer()->IsSeeking() && mSeekState == SEEK_NOT_SEEKING) {
mSeekState = mElementState == STATE_ACTIVE ?
SEEK_FORWARD_FROM_ACTIVE :
SEEK_FORWARD_FROM_INACTIVE;
} else if (mSeekState != SEEK_NOT_SEEKING &&
!GetTimeContainer()->IsSeeking()) {
finishedSeek = true;
}
bool stateChanged;
nsSMILTimeValue sampleTime(aContainerTime);
do {
#ifdef DEBUG
// Check invariant
if (mElementState == STATE_STARTUP || mElementState == STATE_POSTACTIVE) {
NS_ABORT_IF_FALSE(!mCurrentInterval,
"Shouldn't have current interval in startup or postactive states");
} else {
NS_ABORT_IF_FALSE(mCurrentInterval,
"Should have current interval in waiting and active states");
}
#endif
stateChanged = false;
switch (mElementState)
{
case STATE_STARTUP:
{
nsSMILInterval firstInterval;
mElementState = GetNextInterval(nullptr, nullptr, nullptr, firstInterval)
? STATE_WAITING
: STATE_POSTACTIVE;
stateChanged = true;
if (mElementState == STATE_WAITING) {
mCurrentInterval = new nsSMILInterval(firstInterval);
NotifyNewInterval();
}
}
break;
case STATE_WAITING:
{
if (mCurrentInterval->Begin()->Time() <= sampleTime) {
mElementState = STATE_ACTIVE;
mCurrentInterval->FixBegin();
if (mClient) {
mClient->Activate(mCurrentInterval->Begin()->Time().GetMillis());
}
if (mSeekState == SEEK_NOT_SEEKING) {
FireTimeEventAsync(NS_SMIL_BEGIN, 0);
}
if (HasPlayed()) {
Reset(); // Apply restart behaviour
// The call to Reset() may mean that the end point of our current
// interval should be changed and so we should update the interval
// now. However, calling UpdateCurrentInterval could result in the
// interval getting deleted (perhaps through some web of syncbase
// dependencies) therefore we make updating the interval the last
// thing we do. There is no guarantee that mCurrentInterval is set
// after this.
UpdateCurrentInterval();
}
stateChanged = true;
}
}
break;
case STATE_ACTIVE:
{
// Ending early will change the interval but we don't notify dependents
// of the change until we have closed off the current interval (since we
// don't want dependencies to un-end our early end).
bool didApplyEarlyEnd = ApplyEarlyEnd(sampleTime);
if (mCurrentInterval->End()->Time() <= sampleTime) {
nsSMILInterval newInterval;
mElementState =
GetNextInterval(mCurrentInterval, nullptr, nullptr, newInterval)
? STATE_WAITING
: STATE_POSTACTIVE;
if (mClient) {
mClient->Inactivate(mFillMode == FILL_FREEZE);
}
mCurrentInterval->FixEnd();
if (mSeekState == SEEK_NOT_SEEKING) {
FireTimeEventAsync(NS_SMIL_END, 0);
}
mCurrentRepeatIteration = 0;
mOldIntervals.AppendElement(mCurrentInterval.forget());
SampleFillValue();
if (mElementState == STATE_WAITING) {
mCurrentInterval = new nsSMILInterval(newInterval);
}
// We are now in a consistent state to dispatch notifications
if (didApplyEarlyEnd) {
NotifyChangedInterval(
mOldIntervals[mOldIntervals.Length() - 1], false, true);
}
if (mElementState == STATE_WAITING) {
NotifyNewInterval();
}
FilterHistory();
stateChanged = true;
} else {
NS_ABORT_IF_FALSE(!didApplyEarlyEnd,
"We got an early end, but didn't end");
nsSMILTime beginTime = mCurrentInterval->Begin()->Time().GetMillis();
NS_ASSERTION(aContainerTime >= beginTime,
"Sample time should not precede current interval");
nsSMILTime activeTime = aContainerTime - beginTime;
SampleSimpleTime(activeTime);
// We register our repeat times as milestones (except when we're
// seeking) so we should get a sample at exactly the time we repeat.
// (And even when we are seeking we want to update
// mCurrentRepeatIteration so we do that first before testing the seek
// state.)
uint32_t prevRepeatIteration = mCurrentRepeatIteration;
if (ActiveTimeToSimpleTime(activeTime, mCurrentRepeatIteration)==0 &&
mCurrentRepeatIteration != prevRepeatIteration &&
mCurrentRepeatIteration &&
mSeekState == SEEK_NOT_SEEKING) {
FireTimeEventAsync(NS_SMIL_REPEAT,
static_cast<int32_t>(mCurrentRepeatIteration));
}
}
}
break;
case STATE_POSTACTIVE:
break;
}
// Generally we continue driving the state machine so long as we have changed
// state. However, for end samples we only drive the state machine as far as
// the waiting or postactive state because we don't want to commit to any new
// interval (by transitioning to the active state) until all the end samples
// have finished and we then have complete information about the available
// instance times upon which to base our next interval.
} while (stateChanged && (!aEndOnly || (mElementState != STATE_WAITING &&
mElementState != STATE_POSTACTIVE)));
if (finishedSeek) {
DoPostSeek();
}
RegisterMilestone();
}
void
nsSMILTimedElement::HandleContainerTimeChange()
{
// In future we could possibly introduce a separate change notice for time
// container changes and only notify those dependents who live in other time
// containers. For now we don't bother because when we re-resolve the time in
// the nsSMILTimeValueSpec we'll check if anything has changed and if not, we
// won't go any further.
if (mElementState == STATE_WAITING || mElementState == STATE_ACTIVE) {
NotifyChangedInterval(mCurrentInterval, false, false);
}
}
namespace
{
bool
RemoveNonDynamic(nsSMILInstanceTime* aInstanceTime)
{
// Generally dynamically-generated instance times (DOM calls, event-based
// times) are not associated with their creator nsSMILTimeValueSpec since
// they may outlive them.
NS_ABORT_IF_FALSE(!aInstanceTime->IsDynamic() ||
!aInstanceTime->GetCreator(),
"Dynamic instance time should be unlinked from its creator");
return !aInstanceTime->IsDynamic() && !aInstanceTime->ShouldPreserve();
}
}
void
nsSMILTimedElement::Rewind()
{
NS_ABORT_IF_FALSE(mAnimationElement,
"Got rewind request before being attached to an animation element");
// It's possible to get a rewind request whilst we're already in the middle of
// a backwards seek. This can happen when we're performing tree surgery and
// seeking containers at the same time because we can end up requesting
// a local rewind on an element after binding it to a new container and then
// performing a rewind on that container as a whole without sampling in
// between.
//
// However, it should currently be impossible to get a rewind in the middle of
// a forwards seek since forwards seeks are detected and processed within the
// same (re)sample.
if (mSeekState == SEEK_NOT_SEEKING) {
mSeekState = mElementState == STATE_ACTIVE ?
SEEK_BACKWARD_FROM_ACTIVE :
SEEK_BACKWARD_FROM_INACTIVE;
}
NS_ABORT_IF_FALSE(mSeekState == SEEK_BACKWARD_FROM_INACTIVE ||
mSeekState == SEEK_BACKWARD_FROM_ACTIVE,
"Rewind in the middle of a forwards seek?");
// Putting us in the startup state will ensure we skip doing any interval
// updates
mElementState = STATE_STARTUP;
ClearIntervals();
UnsetBeginSpec(RemoveNonDynamic);
UnsetEndSpec(RemoveNonDynamic);
if (mClient) {
mClient->Inactivate(false);
}
if (mAnimationElement->HasAnimAttr(nsGkAtoms::begin)) {
nsAutoString attValue;
mAnimationElement->GetAnimAttr(nsGkAtoms::begin, attValue);
SetBeginSpec(attValue, &mAnimationElement->AsElement(), RemoveNonDynamic);
}
if (mAnimationElement->HasAnimAttr(nsGkAtoms::end)) {
nsAutoString attValue;
mAnimationElement->GetAnimAttr(nsGkAtoms::end, attValue);
SetEndSpec(attValue, &mAnimationElement->AsElement(), RemoveNonDynamic);
}
mPrevRegisteredMilestone = sMaxMilestone;
RegisterMilestone();
NS_ABORT_IF_FALSE(!mCurrentInterval,
"Current interval is set at end of rewind");
}
namespace
{
bool
RemoveNonDOM(nsSMILInstanceTime* aInstanceTime)
{
return !aInstanceTime->FromDOM() && !aInstanceTime->ShouldPreserve();
}
}
bool
nsSMILTimedElement::SetAttr(nsIAtom* aAttribute, const nsAString& aValue,
nsAttrValue& aResult,
Element* aContextNode,
nsresult* aParseResult)
{
bool foundMatch = true;
nsresult parseResult = NS_OK;
if (aAttribute == nsGkAtoms::begin) {
parseResult = SetBeginSpec(aValue, aContextNode, RemoveNonDOM);
} else if (aAttribute == nsGkAtoms::dur) {
parseResult = SetSimpleDuration(aValue);
} else if (aAttribute == nsGkAtoms::end) {
parseResult = SetEndSpec(aValue, aContextNode, RemoveNonDOM);
} else if (aAttribute == nsGkAtoms::fill) {
parseResult = SetFillMode(aValue);
} else if (aAttribute == nsGkAtoms::max) {
parseResult = SetMax(aValue);
} else if (aAttribute == nsGkAtoms::min) {
parseResult = SetMin(aValue);
} else if (aAttribute == nsGkAtoms::repeatCount) {
parseResult = SetRepeatCount(aValue);
} else if (aAttribute == nsGkAtoms::repeatDur) {
parseResult = SetRepeatDur(aValue);
} else if (aAttribute == nsGkAtoms::restart) {
parseResult = SetRestart(aValue);
} else {
foundMatch = false;
}
if (foundMatch) {
aResult.SetTo(aValue);
if (aParseResult) {
*aParseResult = parseResult;
}
}
return foundMatch;
}
bool
nsSMILTimedElement::UnsetAttr(nsIAtom* aAttribute)
{
bool foundMatch = true;
if (aAttribute == nsGkAtoms::begin) {
UnsetBeginSpec(RemoveNonDOM);
} else if (aAttribute == nsGkAtoms::dur) {
UnsetSimpleDuration();
} else if (aAttribute == nsGkAtoms::end) {
UnsetEndSpec(RemoveNonDOM);
} else if (aAttribute == nsGkAtoms::fill) {
UnsetFillMode();
} else if (aAttribute == nsGkAtoms::max) {
UnsetMax();
} else if (aAttribute == nsGkAtoms::min) {
UnsetMin();
} else if (aAttribute == nsGkAtoms::repeatCount) {
UnsetRepeatCount();
} else if (aAttribute == nsGkAtoms::repeatDur) {
UnsetRepeatDur();
} else if (aAttribute == nsGkAtoms::restart) {
UnsetRestart();
} else {
foundMatch = false;
}
return foundMatch;
}
//----------------------------------------------------------------------
// Setters and unsetters
nsresult
nsSMILTimedElement::SetBeginSpec(const nsAString& aBeginSpec,
Element* aContextNode,
RemovalTestFunction aRemove)
{
return SetBeginOrEndSpec(aBeginSpec, aContextNode, true /*isBegin*/,
aRemove);
}
void
nsSMILTimedElement::UnsetBeginSpec(RemovalTestFunction aRemove)
{
ClearSpecs(mBeginSpecs, mBeginInstances, aRemove);
UpdateCurrentInterval();
}
nsresult
nsSMILTimedElement::SetEndSpec(const nsAString& aEndSpec,
Element* aContextNode,
RemovalTestFunction aRemove)
{
return SetBeginOrEndSpec(aEndSpec, aContextNode, false /*!isBegin*/,
aRemove);
}
void
nsSMILTimedElement::UnsetEndSpec(RemovalTestFunction aRemove)
{
ClearSpecs(mEndSpecs, mEndInstances, aRemove);
UpdateCurrentInterval();
}
nsresult
nsSMILTimedElement::SetSimpleDuration(const nsAString& aDurSpec)
{
nsSMILTimeValue duration;
bool isMedia;
nsresult rv;
rv = nsSMILParserUtils::ParseClockValue(aDurSpec, &duration,
nsSMILParserUtils::kClockValueAllowIndefinite, &isMedia);
if (NS_FAILED(rv)) {
mSimpleDur.SetIndefinite();
return NS_ERROR_FAILURE;
}
if (duration.IsDefinite() && duration.GetMillis() == 0L) {
mSimpleDur.SetIndefinite();
return NS_ERROR_FAILURE;
}
//
// SVG-specific: "For SVG's animation elements, if "media" is specified, the
// attribute will be ignored." (SVG 1.1, section 19.2.6)
//
if (isMedia)
duration.SetIndefinite();
// mSimpleDur should never be unresolved. ParseClockValue will either set
// duration to resolved/indefinite/media or will return a failure code.
NS_ABORT_IF_FALSE(duration.IsResolved(),
"Setting unresolved simple duration");
mSimpleDur = duration;
UpdateCurrentInterval();
return NS_OK;
}
void
nsSMILTimedElement::UnsetSimpleDuration()
{
mSimpleDur.SetIndefinite();
UpdateCurrentInterval();
}
nsresult
nsSMILTimedElement::SetMin(const nsAString& aMinSpec)
{
nsSMILTimeValue duration;
bool isMedia;
nsresult rv;
rv = nsSMILParserUtils::ParseClockValue(aMinSpec, &duration, 0, &isMedia);
if (isMedia) {
duration.SetMillis(0L);
}
if (NS_FAILED(rv) || !duration.IsDefinite()) {
mMin.SetMillis(0L);
return NS_ERROR_FAILURE;
}
if (duration.GetMillis() < 0L) {
mMin.SetMillis(0L);
return NS_ERROR_FAILURE;
}
mMin = duration;
UpdateCurrentInterval();
return NS_OK;
}
void
nsSMILTimedElement::UnsetMin()
{
mMin.SetMillis(0L);
UpdateCurrentInterval();
}
nsresult
nsSMILTimedElement::SetMax(const nsAString& aMaxSpec)
{
nsSMILTimeValue duration;
bool isMedia;
nsresult rv;
rv = nsSMILParserUtils::ParseClockValue(aMaxSpec, &duration,
nsSMILParserUtils::kClockValueAllowIndefinite, &isMedia);
if (isMedia)
duration.SetIndefinite();
if (NS_FAILED(rv) || !duration.IsResolved()) {
mMax.SetIndefinite();
return NS_ERROR_FAILURE;
}
if (duration.IsDefinite() && duration.GetMillis() <= 0L) {
mMax.SetIndefinite();
return NS_ERROR_FAILURE;
}
mMax = duration;