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

Side by Side Diff: cc/layers/scrollbar_layer_impl.cc

Issue 18341009: Refactor cc scrollbar layers to separate solid-color vs desktop. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Address Enne's comments. Created 7 years, 4 months 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 | Annotate | Revision Log
OLDNEW
1 // Copyright 2012 The Chromium Authors. All rights reserved. 1 // Copyright 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "cc/layers/scrollbar_layer_impl.h" 5 #include "cc/layers/scrollbar_layer_impl.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 8
9 #include "cc/animation/scrollbar_animation_controller.h" 9 #include "cc/animation/scrollbar_animation_controller.h"
10 #include "cc/layers/layer.h" 10 #include "cc/layers/layer.h"
(...skipping 12 matching lines...) Expand all
23 ScrollbarOrientation orientation) { 23 ScrollbarOrientation orientation) {
24 return make_scoped_ptr(new ScrollbarLayerImpl(tree_impl, 24 return make_scoped_ptr(new ScrollbarLayerImpl(tree_impl,
25 id, 25 id,
26 orientation)); 26 orientation));
27 } 27 }
28 28
29 ScrollbarLayerImpl::ScrollbarLayerImpl( 29 ScrollbarLayerImpl::ScrollbarLayerImpl(
30 LayerTreeImpl* tree_impl, 30 LayerTreeImpl* tree_impl,
31 int id, 31 int id,
32 ScrollbarOrientation orientation) 32 ScrollbarOrientation orientation)
33 : LayerImpl(tree_impl, id), 33 : ScrollbarLayerImplBase(tree_impl, id, orientation),
34 track_resource_id_(0), 34 track_resource_id_(0),
35 thumb_resource_id_(0), 35 thumb_resource_id_(0),
36 current_pos_(0.f),
37 maximum_(0),
38 thumb_thickness_(0), 36 thumb_thickness_(0),
39 thumb_length_(0), 37 thumb_length_(0),
40 track_start_(0), 38 track_start_(0),
41 track_length_(0), 39 track_length_(0),
42 orientation_(orientation), 40 orientation_(orientation),
43 vertical_adjust_(0.f), 41 vertical_adjust_(0.f),
44 visible_to_total_length_ratio_(1.f), 42 visible_to_total_length_ratio_(1.f),
45 scroll_layer_id_(Layer::INVALID_ID), 43 scroll_layer_id_(Layer::INVALID_ID) {}
46 is_overlay_scrollbar_(false) {}
47 44
48 ScrollbarLayerImpl::~ScrollbarLayerImpl() {} 45 ScrollbarLayerImpl::~ScrollbarLayerImpl() {}
49 46
50 ScrollbarLayerImpl* ScrollbarLayerImpl::ToScrollbarLayer() {
51 return this;
52 }
53
54 scoped_ptr<LayerImpl> ScrollbarLayerImpl::CreateLayerImpl( 47 scoped_ptr<LayerImpl> ScrollbarLayerImpl::CreateLayerImpl(
55 LayerTreeImpl* tree_impl) { 48 LayerTreeImpl* tree_impl) {
56 return ScrollbarLayerImpl::Create(tree_impl, 49 return ScrollbarLayerImpl::Create(tree_impl,
57 id(), 50 id(),
58 orientation_).PassAs<LayerImpl>(); 51 orientation()).PassAs<LayerImpl>();
59 } 52 }
60 53
61 void ScrollbarLayerImpl::PushPropertiesTo(LayerImpl* layer) { 54 void ScrollbarLayerImpl::PushPropertiesTo(LayerImpl* layer) {
62 LayerImpl::PushPropertiesTo(layer); 55 ScrollbarLayerImplBase::PushPropertiesTo(layer);
63 56
64 ScrollbarLayerImpl* scrollbar_layer = static_cast<ScrollbarLayerImpl*>(layer); 57 ScrollbarLayerImpl* scrollbar_layer = static_cast<ScrollbarLayerImpl*>(layer);
65 58
66 scrollbar_layer->set_thumb_thickness(thumb_thickness_); 59 scrollbar_layer->set_thumb_thickness(thumb_thickness_);
67 scrollbar_layer->set_thumb_length(thumb_length_); 60 scrollbar_layer->set_thumb_length(thumb_length_);
68 scrollbar_layer->set_track_start(track_start_); 61 scrollbar_layer->set_track_start(track_start_);
69 scrollbar_layer->set_track_length(track_length_); 62 scrollbar_layer->set_track_length(track_length_);
70 scrollbar_layer->set_is_overlay_scrollbar(is_overlay_scrollbar_);
71 63
72 scrollbar_layer->set_track_resource_id(track_resource_id_); 64 scrollbar_layer->set_track_resource_id(track_resource_id_);
73 scrollbar_layer->set_thumb_resource_id(thumb_resource_id_); 65 scrollbar_layer->set_thumb_resource_id(thumb_resource_id_);
74 } 66 }
75 67
76 bool ScrollbarLayerImpl::WillDraw(DrawMode draw_mode, 68 bool ScrollbarLayerImpl::WillDraw(DrawMode draw_mode,
77 ResourceProvider* resource_provider) { 69 ResourceProvider* resource_provider) {
78 if (draw_mode == DRAW_MODE_RESOURCELESS_SOFTWARE && 70 if (draw_mode == DRAW_MODE_RESOURCELESS_SOFTWARE)
aelias_OOO_until_Jul13 2013/08/14 21:26:08 This "resourceless software" mode only arises in A
danakj 2013/08/14 21:31:30 Maybe DCHECK it?
wjmaclean 2013/08/15 20:09:40 Done.
wjmaclean 2013/08/15 20:09:40 Done.
79 !layer_tree_impl()->settings().solid_color_scrollbars)
80 return false; 71 return false;
81 return LayerImpl::WillDraw(draw_mode, resource_provider); 72 return LayerImpl::WillDraw(draw_mode, resource_provider);
82 } 73 }
83 74
84 void ScrollbarLayerImpl::AppendQuads(QuadSink* quad_sink, 75 void ScrollbarLayerImpl::AppendQuads(QuadSink* quad_sink,
85 AppendQuadsData* append_quads_data) { 76 AppendQuadsData* append_quads_data) {
86 bool premultipled_alpha = true; 77 bool premultipled_alpha = true;
87 bool flipped = false; 78 bool flipped = false;
88 gfx::PointF uv_top_left(0.f, 0.f); 79 gfx::PointF uv_top_left(0.f, 0.f);
89 gfx::PointF uv_bottom_right(1.f, 1.f); 80 gfx::PointF uv_bottom_right(1.f, 1.f);
90 gfx::Rect bounds_rect(bounds()); 81 gfx::Rect bounds_rect(bounds());
91 gfx::Rect content_bounds_rect(content_bounds()); 82 gfx::Rect content_bounds_rect(content_bounds());
92 83
93 SharedQuadState* shared_quad_state = 84 SharedQuadState* shared_quad_state =
94 quad_sink->UseSharedQuadState(CreateSharedQuadState()); 85 quad_sink->UseSharedQuadState(CreateSharedQuadState());
95 AppendDebugBorderQuad(quad_sink, shared_quad_state, append_quads_data); 86 AppendDebugBorderQuad(quad_sink, shared_quad_state, append_quads_data);
96 87
97 gfx::Rect thumb_quad_rect = ComputeThumbQuadRect(); 88 gfx::Rect thumb_quad_rect = ComputeThumbQuadRect();
98 89
99 if (layer_tree_impl()->settings().solid_color_scrollbars) {
100 scoped_ptr<SolidColorDrawQuad> quad = SolidColorDrawQuad::Create();
101 quad->SetNew(shared_quad_state,
102 thumb_quad_rect,
103 layer_tree_impl()->settings().solid_color_scrollbar_color,
104 false);
105 quad_sink->Append(quad.PassAs<DrawQuad>(), append_quads_data);
106 return;
107 }
108
109 if (thumb_resource_id_ && !thumb_quad_rect.IsEmpty()) { 90 if (thumb_resource_id_ && !thumb_quad_rect.IsEmpty()) {
110 gfx::Rect opaque_rect; 91 gfx::Rect opaque_rect;
111 const float opacity[] = {1.0f, 1.0f, 1.0f, 1.0f}; 92 const float opacity[] = {1.0f, 1.0f, 1.0f, 1.0f};
112 scoped_ptr<TextureDrawQuad> quad = TextureDrawQuad::Create(); 93 scoped_ptr<TextureDrawQuad> quad = TextureDrawQuad::Create();
113 quad->SetNew(shared_quad_state, 94 quad->SetNew(shared_quad_state,
114 thumb_quad_rect, 95 thumb_quad_rect,
115 opaque_rect, 96 opaque_rect,
116 thumb_resource_id_, 97 thumb_resource_id_,
117 premultipled_alpha, 98 premultipled_alpha,
118 uv_top_left, 99 uv_top_left,
(...skipping 22 matching lines...) Expand all
141 premultipled_alpha, 122 premultipled_alpha,
142 uv_top_left, 123 uv_top_left,
143 uv_bottom_right, 124 uv_bottom_right,
144 SK_ColorTRANSPARENT, 125 SK_ColorTRANSPARENT,
145 opacity, 126 opacity,
146 flipped); 127 flipped);
147 quad_sink->Append(quad.PassAs<DrawQuad>(), append_quads_data); 128 quad_sink->Append(quad.PassAs<DrawQuad>(), append_quads_data);
148 } 129 }
149 } 130 }
150 131
151 ScrollbarOrientation ScrollbarLayerImpl::Orientation() const {
152 return orientation_;
153 }
154
155 float ScrollbarLayerImpl::CurrentPos() const {
156 return current_pos_;
157 }
158
159 int ScrollbarLayerImpl::Maximum() const {
160 return maximum_;
161 }
162
163 gfx::Rect ScrollbarLayerImpl::ScrollbarLayerRectToContentRect(
164 gfx::RectF layer_rect) const {
165 // Don't intersect with the bounds as in layerRectToContentRect() because
166 // layer_rect here might be in coordinates of the containing layer.
167 gfx::RectF content_rect = gfx::ScaleRect(layer_rect,
168 contents_scale_x(),
169 contents_scale_y());
170 return gfx::ToEnclosingRect(content_rect);
171 }
172
173 gfx::Rect ScrollbarLayerImpl::ComputeThumbQuadRect() const {
174 // Thumb extent is the length of the thumb in the scrolling direction, thumb
175 // thickness is in the perpendicular direction. Here's an example of a
176 // horizontal scrollbar - inputs are above the scrollbar, computed values
177 // below:
178 //
179 // |<------------------- track_length_ ------------------->|
180 //
181 // |--| <-- start_offset
182 //
183 // +--+----------------------------+------------------+-------+--+
184 // |<|| |##################| ||>|
185 // +--+----------------------------+------------------+-------+--+
186 //
187 // |<- thumb_length ->|
188 //
189 // |<------- thumb_offset -------->|
190 //
191 // For painted, scrollbars, the length is fixed. For solid color scrollbars we
192 // have to compute it. The ratio of the thumb's length to the track's length
193 // is the same as that of the visible viewport to the total viewport, unless
194 // that would make the thumb's length less than its thickness.
195 //
196 // vertical_adjust_ is used when the layer geometry from the main thread is
197 // not in sync with what the user sees. For instance on Android scrolling the
198 // top bar controls out of view reveals more of the page content. We want the
199 // root layer scrollbars to reflect what the user sees even if we haven't
200 // received new layer geometry from the main thread. If the user has scrolled
201 // down by 50px and the initial viewport size was 950px the geometry would
202 // look something like this:
203 //
204 // vertical_adjust_ = 50, scroll position 0, visible ratios 99%
205 // Layer geometry: Desired thumb positions:
206 // +--------------------+-+ +----------------------+ <-- 0px
207 // | |v| | #|
208 // | |e| | #|
209 // | |r| | #|
210 // | |t| | #|
211 // | |i| | #|
212 // | |c| | #|
213 // | |a| | #|
214 // | |l| | #|
215 // | | | | #|
216 // | |l| | #|
217 // | |a| | #|
218 // | |y| | #|
219 // | |e| | #|
220 // | |r| | #|
221 // +--------------------+-+ | #|
222 // | horizontal layer | | | #|
223 // +--------------------+-+ | #| <-- 950px
224 // | | | #|
225 // | | |##################### |
226 // +----------------------+ +----------------------+ <-- 1000px
227 //
228 // The layer geometry is set up for a 950px tall viewport, but the user can
229 // actually see down to 1000px. Thus we have to move the quad for the
230 // horizontal scrollbar down by the vertical_adjust_ factor and lay the
231 // vertical thumb out on a track lengthed by the vertical_adjust_ factor. This
232 // means the quads may extend outside the layer's bounds.
233
234 int thumb_length = thumb_length_;
235 float track_length = track_length_;
236 if (orientation_ == VERTICAL)
237 track_length += vertical_adjust_;
238
239 if (layer_tree_impl()->settings().solid_color_scrollbars) {
240 thumb_length = std::max(
241 static_cast<int>(visible_to_total_length_ratio_ * track_length),
242 thumb_thickness_);
243 }
244
245 // With the length known, we can compute the thumb's position.
246 float clamped_current_pos =
247 std::min(std::max(current_pos_, 0.f), static_cast<float>(maximum_));
248 float ratio = clamped_current_pos / maximum_;
249 float max_offset = track_length - thumb_length;
250 int thumb_offset = static_cast<int>(ratio * max_offset) + track_start_;
251
252 gfx::RectF thumb_rect;
253 if (orientation_ == HORIZONTAL) {
254 thumb_rect = gfx::RectF(thumb_offset, vertical_adjust_,
255 thumb_length, thumb_thickness_);
256 } else {
257 thumb_rect = gfx::RectF(0.f, thumb_offset,
258 thumb_thickness_, thumb_length);
259 }
260
261 return ScrollbarLayerRectToContentRect(thumb_rect);
262 }
263
264 void ScrollbarLayerImpl::DidLoseOutputSurface() { 132 void ScrollbarLayerImpl::DidLoseOutputSurface() {
265 track_resource_id_ = 0; 133 track_resource_id_ = 0;
266 thumb_resource_id_ = 0; 134 thumb_resource_id_ = 0;
267 } 135 }
268 136
269 const char* ScrollbarLayerImpl::LayerTypeAsString() const { 137 const char* ScrollbarLayerImpl::LayerTypeAsString() const {
270 return "cc::ScrollbarLayerImpl"; 138 return "cc::ScrollbarLayerImpl";
271 } 139 }
272 140
141 int ScrollbarLayerImpl::ThumbThickness() const {
142 return thumb_thickness_;
143 }
144
145 int ScrollbarLayerImpl::ThumbLength() const {
146 return thumb_length_;
147 }
148
149 float ScrollbarLayerImpl::TrackLength() const {
150 return track_length_ + (orientation() == VERTICAL ? vertical_adjust() : 0);
151 }
152
153 int ScrollbarLayerImpl::TrackStart() const {
154 return track_start_;
155 }
156
273 } // namespace cc 157 } // namespace cc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698