Majid Valipour | e9855f7 | 2017-10-20 02:08:58 | [diff] [blame] | 1 | // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "cc/trees/layer_tree_mutator.h" |
| 6 | |
Majid Valipour | c035b5fc | 2018-07-04 15:17:21 | [diff] [blame] | 7 | #include <algorithm> |
| 8 | |
Majid Valipour | e9855f7 | 2017-10-20 02:08:58 | [diff] [blame] | 9 | namespace cc { |
| 10 | |
Majid Valipour | c035b5fc | 2018-07-04 15:17:21 | [diff] [blame] | 11 | AnimationWorkletInput::AddAndUpdateState::AddAndUpdateState( |
| 12 | WorkletAnimationId worklet_animation_id, |
Yi Gu | a5a3bb6c | 2018-06-01 22:16:07 | [diff] [blame] | 13 | std::string name, |
| 14 | double current_time, |
Yi Gu | 859ff15 | 2018-10-16 19:17:09 | [diff] [blame] | 15 | std::unique_ptr<AnimationOptions> options, |
Jordan Taylor | fc0cdda0 | 2019-05-20 18:01:25 | [diff] [blame] | 16 | std::unique_ptr<AnimationEffectTimings> effect_timings) |
Majid Valipour | c035b5fc | 2018-07-04 15:17:21 | [diff] [blame] | 17 | : worklet_animation_id(worklet_animation_id), |
Yi Gu | a5a3bb6c | 2018-06-01 22:16:07 | [diff] [blame] | 18 | name(name), |
| 19 | current_time(current_time), |
Yi Gu | 859ff15 | 2018-10-16 19:17:09 | [diff] [blame] | 20 | options(std::move(options)), |
Jordan Taylor | fc0cdda0 | 2019-05-20 18:01:25 | [diff] [blame] | 21 | effect_timings(std::move(effect_timings)) {} |
Majid Valipour | c035b5fc | 2018-07-04 15:17:21 | [diff] [blame] | 22 | AnimationWorkletInput::AddAndUpdateState::AddAndUpdateState( |
| 23 | AddAndUpdateState&&) = default; |
| 24 | AnimationWorkletInput::AddAndUpdateState::~AddAndUpdateState() = default; |
| 25 | |
| 26 | #if DCHECK_IS_ON() |
Kevin Ellis | 3080186c | 2019-01-17 22:26:48 | [diff] [blame] | 27 | bool AnimationWorkletInput::ValidateId(int worklet_id) const { |
Majid Valipour | c035b5fc | 2018-07-04 15:17:21 | [diff] [blame] | 28 | return std::all_of(added_and_updated_animations.cbegin(), |
| 29 | added_and_updated_animations.cend(), |
Kevin Ellis | 3080186c | 2019-01-17 22:26:48 | [diff] [blame] | 30 | [worklet_id](auto& it) { |
| 31 | return it.worklet_animation_id.worklet_id == worklet_id; |
Majid Valipour | c035b5fc | 2018-07-04 15:17:21 | [diff] [blame] | 32 | }) && |
| 33 | std::all_of(updated_animations.cbegin(), updated_animations.cend(), |
Kevin Ellis | 3080186c | 2019-01-17 22:26:48 | [diff] [blame] | 34 | [worklet_id](auto& it) { |
| 35 | return it.worklet_animation_id.worklet_id == worklet_id; |
Majid Valipour | c035b5fc | 2018-07-04 15:17:21 | [diff] [blame] | 36 | }) && |
Yi Gu | feb2f7e | 2018-09-13 03:27:40 | [diff] [blame] | 37 | std::all_of( |
| 38 | removed_animations.cbegin(), removed_animations.cend(), |
Kevin Ellis | 3080186c | 2019-01-17 22:26:48 | [diff] [blame] | 39 | [worklet_id](auto& it) { return it.worklet_id == worklet_id; }); |
Majid Valipour | c035b5fc | 2018-07-04 15:17:21 | [diff] [blame] | 40 | } |
| 41 | #endif |
| 42 | |
| 43 | AnimationWorkletInput::AnimationWorkletInput() = default; |
| 44 | AnimationWorkletInput::~AnimationWorkletInput() = default; |
Yi Gu | a5a3bb6c | 2018-06-01 22:16:07 | [diff] [blame] | 45 | |
Majid Valipour | e9855f7 | 2017-10-20 02:08:58 | [diff] [blame] | 46 | MutatorInputState::MutatorInputState() = default; |
| 47 | MutatorInputState::~MutatorInputState() = default; |
| 48 | |
Majid Valipour | c035b5fc | 2018-07-04 15:17:21 | [diff] [blame] | 49 | bool MutatorInputState::IsEmpty() const { |
| 50 | // If there is an AnimationWorkletInput entry in the map then that entry is |
| 51 | // guranteed to be non-empty. So checking |inputs_| map emptiness is |
| 52 | // sufficient. |
| 53 | return inputs_.empty(); |
| 54 | } |
| 55 | |
| 56 | AnimationWorkletInput& MutatorInputState::EnsureWorkletEntry(int id) { |
| 57 | auto it = inputs_.find(id); |
| 58 | if (it == inputs_.end()) |
| 59 | it = inputs_.emplace_hint(it, id, new AnimationWorkletInput); |
| 60 | |
| 61 | return *it->second; |
| 62 | } |
| 63 | |
| 64 | void MutatorInputState::Add(AnimationWorkletInput::AddAndUpdateState&& state) { |
| 65 | AnimationWorkletInput& worklet_input = |
Kevin Ellis | 3080186c | 2019-01-17 22:26:48 | [diff] [blame] | 66 | EnsureWorkletEntry(state.worklet_animation_id.worklet_id); |
Majid Valipour | c035b5fc | 2018-07-04 15:17:21 | [diff] [blame] | 67 | worklet_input.added_and_updated_animations.push_back(std::move(state)); |
| 68 | } |
| 69 | void MutatorInputState::Update(AnimationWorkletInput::UpdateState&& state) { |
| 70 | AnimationWorkletInput& worklet_input = |
Kevin Ellis | 3080186c | 2019-01-17 22:26:48 | [diff] [blame] | 71 | EnsureWorkletEntry(state.worklet_animation_id.worklet_id); |
Majid Valipour | c035b5fc | 2018-07-04 15:17:21 | [diff] [blame] | 72 | worklet_input.updated_animations.push_back(std::move(state)); |
| 73 | } |
| 74 | void MutatorInputState::Remove(WorkletAnimationId worklet_animation_id) { |
| 75 | AnimationWorkletInput& worklet_input = |
Kevin Ellis | 3080186c | 2019-01-17 22:26:48 | [diff] [blame] | 76 | EnsureWorkletEntry(worklet_animation_id.worklet_id); |
Majid Valipour | c035b5fc | 2018-07-04 15:17:21 | [diff] [blame] | 77 | worklet_input.removed_animations.push_back(worklet_animation_id); |
| 78 | } |
| 79 | |
| 80 | std::unique_ptr<AnimationWorkletInput> MutatorInputState::TakeWorkletState( |
Kevin Ellis | 3080186c | 2019-01-17 22:26:48 | [diff] [blame] | 81 | int worklet_id) { |
| 82 | auto it = inputs_.find(worklet_id); |
Majid Valipour | c035b5fc | 2018-07-04 15:17:21 | [diff] [blame] | 83 | if (it == inputs_.end()) |
| 84 | return nullptr; |
| 85 | |
| 86 | std::unique_ptr<AnimationWorkletInput> result = std::move(it->second); |
| 87 | inputs_.erase(it); |
| 88 | return result; |
| 89 | } |
| 90 | |
| 91 | AnimationWorkletOutput::AnimationWorkletOutput() = default; |
| 92 | AnimationWorkletOutput::~AnimationWorkletOutput() = default; |
Majid Valipour | e9855f7 | 2017-10-20 02:08:58 | [diff] [blame] | 93 | |
Yi Gu | 859ff15 | 2018-10-16 19:17:09 | [diff] [blame] | 94 | AnimationWorkletOutput::AnimationState::AnimationState(WorkletAnimationId id) |
| 95 | : worklet_animation_id(id) {} |
Majid Valipour | 82b3d45 | 2018-08-24 15:30:01 | [diff] [blame] | 96 | AnimationWorkletOutput::AnimationState::AnimationState(const AnimationState&) = |
| 97 | default; |
Yi Gu | 859ff15 | 2018-10-16 19:17:09 | [diff] [blame] | 98 | AnimationWorkletOutput::AnimationState::~AnimationState() = default; |
Majid Valipour | 82b3d45 | 2018-08-24 15:30:01 | [diff] [blame] | 99 | |
Majid Valipour | e9855f7 | 2017-10-20 02:08:58 | [diff] [blame] | 100 | } // namespace cc |