Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(29)

Side by Side Diff: cc/base/synced_property.h

Issue 783543003: Update from https://crrev.com/306901 (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: Created 6 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « cc/base/switches.cc ('k') | cc/blink/web_external_texture_layer_impl.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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.
4
5 #ifndef CC_BASE_SYNCED_PROPERTY_H_
6 #define CC_BASE_SYNCED_PROPERTY_H_
7
8 #include "base/memory/ref_counted.h"
9
10 namespace cc {
11
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).
16 //
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).
23
24 template <typename T>
25 class SyncedProperty : public base::RefCounted<SyncedProperty<T>> {
26 public:
27 SyncedProperty() {}
28
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();
37 }
38
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;
48
49 active_delta_ = delta;
50 return true;
51 }
52
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(); }
56
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();
62 }
63
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;
69
70 pending_base_ = T(main_thread_value);
71
72 return true;
73 }
74
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;
83
84 active_base_ = pending_base_;
85 active_delta_ = PendingDelta();
86 sent_delta_ = T::Identity();
87
88 return true;
89 }
90
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();
98 }
99
100 private:
101 // The new delta we would use if we decide to activate now. This delta
102 // excludes the amount that we expect the main thread to reflect back at the
103 // impl thread during the commit.
104 T PendingDelta() const { return active_delta_.InverseCombine(sent_delta_); }
105
106 // Value last committed to the pending tree.
107 T pending_base_;
108 // Value last committed to the active tree (on the last activation).
109 T active_base_;
110 // The difference between the active_base_ and the user-perceived value.
111 T active_delta_;
112 // The value sent to the main thread (on the last BeginFrame); this is always
113 // identity outside of the BeginFrame-to-activation interval.
114 T sent_delta_;
115
116 friend class base::RefCounted<SyncedProperty<T>>;
117 ~SyncedProperty() {}
118 };
119
120 // SyncedProperty's delta-based conflict resolution logic makes sense for any
121 // mathematical group. In practice, there are two that are useful:
122 // 1. Numbers/vectors with addition and identity = 0 (like scroll offsets)
123 // 2. Real numbers with multiplication and identity = 1 (like page scale)
124
125 template <class V>
126 class AdditionGroup {
127 public:
128 typedef V ValueType;
129
130 AdditionGroup() : value_(Identity().get()) {}
131 explicit AdditionGroup(V value) : value_(value) {}
132
133 V& get() { return value_; }
134 const V& get() const { return value_; }
135
136 static AdditionGroup<V> Identity() { return AdditionGroup(V()); } // zero
137 AdditionGroup<V> Combine(AdditionGroup<V> p) const {
138 return AdditionGroup<V>(value_ + p.value_);
139 }
140 AdditionGroup<V> InverseCombine(AdditionGroup<V> p) const {
141 return AdditionGroup<V>(value_ - p.value_);
142 }
143
144 private:
145 V value_;
146 };
147
148 class ScaleGroup {
149 public:
150 typedef float ValueType;
151
152 ScaleGroup() : value_(Identity().get()) {}
153 explicit ScaleGroup(float value) : value_(value) {}
154
155 float& get() { return value_; }
156 const float& get() const { return value_; }
157
158 static ScaleGroup Identity() { return ScaleGroup(1.f); }
159 ScaleGroup Combine(ScaleGroup p) const {
160 return ScaleGroup(value_ * p.value_);
161 }
162 ScaleGroup InverseCombine(ScaleGroup p) const {
163 return ScaleGroup(value_ / p.value_);
164 }
165
166 private:
167 float value_;
168 };
169
170 } // namespace cc
171
172 #endif // CC_BASE_SYNCED_PROPERTY_H_
OLDNEW
« no previous file with comments | « cc/base/switches.cc ('k') | cc/blink/web_external_texture_layer_impl.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698