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

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

Powered by Google App Engine
This is Rietveld 408576698