summaryrefslogtreecommitdiffstats
path: root/cc/animation/animation_registrar.cc
blob: e498045b56d4d2f3485f5a4b95ee63e93978d0fa (plain)
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
// Copyright 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "cc/animation/animation_registrar.h"

#include <stddef.h>

#include "base/trace_event/trace_event.h"
#include "base/trace_event/trace_event_argument.h"
#include "cc/animation/animation_events.h"
#include "cc/animation/layer_animation_controller.h"

namespace cc {

AnimationRegistrar::AnimationRegistrar() : supports_scroll_animations_(false) {
}

AnimationRegistrar::~AnimationRegistrar() {
  AnimationControllerMap copy = all_animation_controllers_;
  for (AnimationControllerMap::iterator iter = copy.begin();
       iter != copy.end();
       ++iter)
    (*iter).second->SetAnimationRegistrar(nullptr);
}

scoped_refptr<LayerAnimationController>
AnimationRegistrar::GetAnimationControllerForId(int id) {
  scoped_refptr<LayerAnimationController> to_return;
  if (!ContainsKey(all_animation_controllers_, id)) {
    to_return = LayerAnimationController::Create(id);
    to_return->SetAnimationRegistrar(this);
    all_animation_controllers_[id] = to_return.get();
  } else {
    to_return = all_animation_controllers_[id];
  }
  return to_return;
}

void AnimationRegistrar::DidActivateAnimationController(
    LayerAnimationController* controller) {
  active_animation_controllers_[controller->id()] = controller;
}

void AnimationRegistrar::DidDeactivateAnimationController(
    LayerAnimationController* controller) {
  if (ContainsKey(active_animation_controllers_, controller->id()))
    active_animation_controllers_.erase(controller->id());
}

void AnimationRegistrar::RegisterAnimationController(
    LayerAnimationController* controller) {
  all_animation_controllers_[controller->id()] = controller;
}

void AnimationRegistrar::UnregisterAnimationController(
    LayerAnimationController* controller) {
  if (ContainsKey(all_animation_controllers_, controller->id()))
    all_animation_controllers_.erase(controller->id());
  DidDeactivateAnimationController(controller);
}

bool AnimationRegistrar::ActivateAnimations() {
  if (!needs_animate_layers())
    return false;

  TRACE_EVENT0("cc", "AnimationRegistrar::ActivateAnimations");
  AnimationControllerMap active_controllers_copy =
      active_animation_controllers_;
  for (auto& it : active_controllers_copy)
    it.second->ActivateAnimations();

  return true;
}

bool AnimationRegistrar::AnimateLayers(base::TimeTicks monotonic_time) {
  if (!needs_animate_layers())
    return false;

  TRACE_EVENT0("cc", "AnimationRegistrar::AnimateLayers");
  AnimationControllerMap controllers_copy = active_animation_controllers_;
  for (auto& it : controllers_copy)
    it.second->Animate(monotonic_time);

  return true;
}

bool AnimationRegistrar::UpdateAnimationState(bool start_ready_animations,
                                              AnimationEvents* events) {
  if (!needs_animate_layers())
    return false;

  TRACE_EVENT0("cc", "AnimationRegistrar::UpdateAnimationState");
  AnimationControllerMap active_controllers_copy =
      active_animation_controllers_;
  for (auto& it : active_controllers_copy)
    it.second->UpdateState(start_ready_animations, events);

  return true;
}

scoped_ptr<AnimationEvents> AnimationRegistrar::CreateEvents() {
  return make_scoped_ptr(new AnimationEvents());
}

void AnimationRegistrar::SetAnimationEvents(
    scoped_ptr<AnimationEvents> events) {
  for (size_t event_index = 0; event_index < events->events_.size();
       ++event_index) {
    int event_layer_id = events->events_[event_index].layer_id;

    // Use the map of all controllers, not just active ones, since non-active
    // controllers may still receive events for impl-only animations.
    const AnimationRegistrar::AnimationControllerMap& animation_controllers =
        all_animation_controllers_;
    auto iter = animation_controllers.find(event_layer_id);
    if (iter != animation_controllers.end()) {
      switch (events->events_[event_index].type) {
        case AnimationEvent::STARTED:
          (*iter).second->NotifyAnimationStarted(events->events_[event_index]);
          break;

        case AnimationEvent::FINISHED:
          (*iter).second->NotifyAnimationFinished(events->events_[event_index]);
          break;

        case AnimationEvent::ABORTED:
          (*iter).second->NotifyAnimationAborted(events->events_[event_index]);
          break;

        case AnimationEvent::PROPERTY_UPDATE:
          (*iter).second->NotifyAnimationPropertyUpdate(
              events->events_[event_index]);
          break;

        case AnimationEvent::TAKEOVER:
          (*iter).second->NotifyAnimationTakeover(events->events_[event_index]);
          break;
      }
    }
  }
}

}  // namespace cc