1 // Copyright 2014 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.
5 #ifndef CC_BASE_SYNCED_PROPERTY_H_
6 #define CC_BASE_SYNCED_PROPERTY_H_
8 #include "base/memory/ref_counted.h"
12 // This class is the basic primitive used for impl-thread scrolling. Its job is
13 // to sanely resolve the case where both the main and impl thread are
14 // concurrently updating the same value (for example, when Javascript sets the
15 // scroll offset during an ongoing impl-side scroll).
17 // There are three trees (main, pending, and active) and therefore also three
18 // places with their own idea of the scroll offsets (and analogous properties
19 // like page scale). Objects of this class are meant to be held on the Impl
20 // side, and contain the canonical reference for the pending and active trees,
21 // as well as keeping track of the latest delta sent to the main thread (which
22 // is necessary for conflict resolution).
25 class SyncedProperty
: public base::RefCounted
<SyncedProperty
<T
>> {
29 // Returns the canonical value for the specified tree, including the sum of
30 // all deltas. The pending tree should use this for activation purposes and
31 // the active tree should use this for drawing.
32 typename
T::ValueType
Current(bool is_active_tree
) const {
34 return active_base_
.Combine(active_delta_
).get();
36 return pending_base_
.Combine(PendingDelta()).get();
39 // Sets the value on the impl thread, due to an impl-thread-originating
40 // action. Returns true if this had any effect. This will remain
41 // impl-thread-only information at first, and will get pulled back to the main
42 // thread on the next call of PullDeltaToMainThread (which happens right
43 // before the commit).
44 bool SetCurrent(typename
T::ValueType current
) {
45 T delta
= T(current
).InverseCombine(active_base_
);
46 if (active_delta_
.get() == delta
.get())
49 active_delta_
= delta
;
53 // Returns the difference between the last value that was committed and
54 // activated from the main thread, and the current total value.
55 typename
T::ValueType
Delta() const { return active_delta_
.get(); }
57 // Returns the latest active tree delta and also makes a note that this value
58 // was sent to the main thread.
59 typename
T::ValueType
PullDeltaForMainThread() {
60 sent_delta_
= active_delta_
;
61 return active_delta_
.get();
64 // Push the latest value from the main thread onto pending tree-associated
65 // state. Returns true if this had any effect.
66 bool PushFromMainThread(typename
T::ValueType main_thread_value
) {
67 if (pending_base_
.get() == main_thread_value
)
70 pending_base_
= T(main_thread_value
);
75 // Push the value associated with the pending tree to be the active base
76 // value. As part of this, subtract the last sent value from the active tree
77 // delta (which will make the delta zero at steady state, or make it contain
78 // only the difference since the last send).
79 bool PushPendingToActive() {
80 if (active_base_
.get() == pending_base_
.get() &&
81 sent_delta_
.get() == T::Identity().get())
84 active_base_
= pending_base_
;
85 active_delta_
= PendingDelta();
86 sent_delta_
= T::Identity();
91 // This simulates the consequences of the sent value getting committed and
92 // activated. The value sent to the main thread ends up combined with the
93 // active value, and the sent_delta is subtracted from the delta.
95 active_base_
= active_base_
.Combine(sent_delta_
);
96 active_delta_
= PendingDelta();
97 sent_delta_
= T::Identity();
100 // Values as last pushed to the pending or active tree respectively, with no
101 // impl-thread delta applied.
102 typename
T::ValueType
PendingBase() const { return pending_base_
.get(); }
103 typename
T::ValueType
ActiveBase() const { return active_base_
.get(); }
105 // The new delta we would use if we decide to activate now. This delta
106 // excludes the amount that we expect the main thread to reflect back at the
107 // impl thread during the commit.
108 T
PendingDelta() const { return active_delta_
.InverseCombine(sent_delta_
); }
111 // Value last committed to the pending tree.
113 // Value last committed to the active tree (on the last activation).
115 // The difference between the active_base_ and the user-perceived value.
117 // The value sent to the main thread (on the last BeginFrame); this is always
118 // identity outside of the BeginFrame-to-activation interval.
121 friend class base::RefCounted
<SyncedProperty
<T
>>;
125 // SyncedProperty's delta-based conflict resolution logic makes sense for any
126 // mathematical group. In practice, there are two that are useful:
127 // 1. Numbers/vectors with addition and identity = 0 (like scroll offsets)
128 // 2. Real numbers with multiplication and identity = 1 (like page scale)
131 class AdditionGroup
{
135 AdditionGroup() : value_(Identity().get()) {}
136 explicit AdditionGroup(V value
) : value_(value
) {}
138 V
& get() { return value_
; }
139 const V
& get() const { return value_
; }
141 static AdditionGroup
<V
> Identity() { return AdditionGroup(V()); } // zero
142 AdditionGroup
<V
> Combine(AdditionGroup
<V
> p
) const {
143 return AdditionGroup
<V
>(value_
+ p
.value_
);
145 AdditionGroup
<V
> InverseCombine(AdditionGroup
<V
> p
) const {
146 return AdditionGroup
<V
>(value_
- p
.value_
);
155 typedef float ValueType
;
157 ScaleGroup() : value_(Identity().get()) {}
158 explicit ScaleGroup(float value
) : value_(value
) {}
160 float& get() { return value_
; }
161 const float& get() const { return value_
; }
163 static ScaleGroup
Identity() { return ScaleGroup(1.f
); }
164 ScaleGroup
Combine(ScaleGroup p
) const {
165 return ScaleGroup(value_
* p
.value_
);
167 ScaleGroup
InverseCombine(ScaleGroup p
) const {
168 return ScaleGroup(value_
/ p
.value_
);
177 #endif // CC_BASE_SYNCED_PROPERTY_H_