Update content setting for app banners to store more information.
[chromium-blink-merge.git] / cc / base / synced_property.h
blob8b554c62eda7b141196a064585e5ee403cec5cf2
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"
10 namespace cc {
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).
24 template <typename T>
25 class SyncedProperty : public base::RefCounted<SyncedProperty<T>> {
26 public:
27 SyncedProperty() {}
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 {
33 if (is_active_tree)
34 return active_base_.Combine(active_delta_).get();
35 else
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())
47 return false;
49 active_delta_ = delta;
50 return true;
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)
68 return false;
70 pending_base_ = T(main_thread_value);
72 return true;
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())
82 return false;
84 active_base_ = pending_base_;
85 active_delta_ = PendingDelta();
86 sent_delta_ = T::Identity();
88 return true;
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.
94 void AbortCommit() {
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_); }
110 private:
111 // Value last committed to the pending tree.
112 T pending_base_;
113 // Value last committed to the active tree (on the last activation).
114 T active_base_;
115 // The difference between the active_base_ and the user-perceived value.
116 T active_delta_;
117 // The value sent to the main thread (on the last BeginFrame); this is always
118 // identity outside of the BeginFrame-to-activation interval.
119 T sent_delta_;
121 friend class base::RefCounted<SyncedProperty<T>>;
122 ~SyncedProperty() {}
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)
130 template <class V>
131 class AdditionGroup {
132 public:
133 typedef V ValueType;
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_);
149 private:
150 V value_;
153 class ScaleGroup {
154 public:
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_);
171 private:
172 float value_;
175 } // namespace cc
177 #endif // CC_BASE_SYNCED_PROPERTY_H_