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
>> {
27 SyncedProperty() : clobber_active_value_(false) {}
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();
87 clobber_active_value_
= false;
92 // This simulates the consequences of the sent value getting committed and
93 // activated. The value sent to the main thread ends up combined with the
94 // active value, and the sent_delta is subtracted from the delta.
96 active_base_
= active_base_
.Combine(sent_delta_
);
97 active_delta_
= PendingDelta();
98 sent_delta_
= T::Identity();
101 // Values as last pushed to the pending or active tree respectively, with no
102 // impl-thread delta applied.
103 typename
T::ValueType
PendingBase() const { return pending_base_
.get(); }
104 typename
T::ValueType
ActiveBase() const { return active_base_
.get(); }
106 // The new delta we would use if we decide to activate now. This delta
107 // excludes the amount that we expect the main thread to reflect back at the
108 // impl thread during the commit.
109 T
PendingDelta() const {
110 if (clobber_active_value_
)
111 return T::Identity();
112 return active_delta_
.InverseCombine(sent_delta_
);
115 void set_clobber_active_value() { clobber_active_value_
= true; }
118 // Value last committed to the pending tree.
120 // Value last committed to the active tree (on the last activation).
122 // The difference between the active_base_ and the user-perceived value.
124 // The value sent to the main thread (on the last BeginFrame); this is always
125 // identity outside of the BeginFrame-to-activation interval.
127 // When true the pending delta is always identity so that it does not change
128 // and will clobber the active value on push.
129 bool clobber_active_value_
;
131 friend class base::RefCounted
<SyncedProperty
<T
>>;
135 // SyncedProperty's delta-based conflict resolution logic makes sense for any
136 // mathematical group. In practice, there are two that are useful:
137 // 1. Numbers/vectors with addition and identity = 0 (like scroll offsets)
138 // 2. Real numbers with multiplication and identity = 1 (like page scale)
141 class AdditionGroup
{
145 AdditionGroup() : value_(Identity().get()) {}
146 explicit AdditionGroup(V value
) : value_(value
) {}
148 V
& get() { return value_
; }
149 const V
& get() const { return value_
; }
151 static AdditionGroup
<V
> Identity() { return AdditionGroup(V()); } // zero
152 AdditionGroup
<V
> Combine(AdditionGroup
<V
> p
) const {
153 return AdditionGroup
<V
>(value_
+ p
.value_
);
155 AdditionGroup
<V
> InverseCombine(AdditionGroup
<V
> p
) const {
156 return AdditionGroup
<V
>(value_
- p
.value_
);
165 typedef float ValueType
;
167 ScaleGroup() : value_(Identity().get()) {}
168 explicit ScaleGroup(float value
) : value_(value
) {}
170 float& get() { return value_
; }
171 const float& get() const { return value_
; }
173 static ScaleGroup
Identity() { return ScaleGroup(1.f
); }
174 ScaleGroup
Combine(ScaleGroup p
) const {
175 return ScaleGroup(value_
* p
.value_
);
177 ScaleGroup
InverseCombine(ScaleGroup p
) const {
178 return ScaleGroup(value_
/ p
.value_
);
187 #endif // CC_BASE_SYNCED_PROPERTY_H_