1 // Copyright 2013 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 #include "cc/layers/painted_scrollbar_layer_impl.h"
9 #include "cc/animation/scrollbar_animation_controller.h"
10 #include "cc/layers/layer.h"
11 #include "cc/quads/solid_color_draw_quad.h"
12 #include "cc/quads/texture_draw_quad.h"
13 #include "cc/trees/layer_tree_impl.h"
14 #include "cc/trees/layer_tree_settings.h"
15 #include "cc/trees/occlusion.h"
16 #include "ui/gfx/geometry/rect_conversions.h"
20 scoped_ptr
<PaintedScrollbarLayerImpl
> PaintedScrollbarLayerImpl::Create(
21 LayerTreeImpl
* tree_impl
,
23 ScrollbarOrientation orientation
) {
24 return make_scoped_ptr(
25 new PaintedScrollbarLayerImpl(tree_impl
, id
, orientation
));
28 PaintedScrollbarLayerImpl::PaintedScrollbarLayerImpl(
29 LayerTreeImpl
* tree_impl
,
31 ScrollbarOrientation orientation
)
32 : ScrollbarLayerImplBase(tree_impl
, id
, orientation
, false, false),
33 track_ui_resource_id_(0),
34 thumb_ui_resource_id_(0),
35 internal_contents_scale_(1.f
),
42 PaintedScrollbarLayerImpl::~PaintedScrollbarLayerImpl() {}
44 scoped_ptr
<LayerImpl
> PaintedScrollbarLayerImpl::CreateLayerImpl(
45 LayerTreeImpl
* tree_impl
) {
46 return PaintedScrollbarLayerImpl::Create(tree_impl
, id(), orientation());
49 void PaintedScrollbarLayerImpl::PushPropertiesTo(LayerImpl
* layer
) {
50 ScrollbarLayerImplBase::PushPropertiesTo(layer
);
52 PaintedScrollbarLayerImpl
* scrollbar_layer
=
53 static_cast<PaintedScrollbarLayerImpl
*>(layer
);
55 scrollbar_layer
->set_internal_contents_scale_and_bounds(
56 internal_contents_scale_
, internal_content_bounds_
);
58 scrollbar_layer
->SetThumbThickness(thumb_thickness_
);
59 scrollbar_layer
->SetThumbLength(thumb_length_
);
60 scrollbar_layer
->SetTrackStart(track_start_
);
61 scrollbar_layer
->SetTrackLength(track_length_
);
63 scrollbar_layer
->set_track_ui_resource_id(track_ui_resource_id_
);
64 scrollbar_layer
->set_thumb_ui_resource_id(thumb_ui_resource_id_
);
67 bool PaintedScrollbarLayerImpl::WillDraw(DrawMode draw_mode
,
68 ResourceProvider
* resource_provider
) {
69 DCHECK(draw_mode
!= DRAW_MODE_RESOURCELESS_SOFTWARE
);
70 return LayerImpl::WillDraw(draw_mode
, resource_provider
);
73 void PaintedScrollbarLayerImpl::AppendQuads(
74 RenderPass
* render_pass
,
75 AppendQuadsData
* append_quads_data
) {
76 bool premultipled_alpha
= true;
78 bool nearest_neighbor
= false;
79 gfx::PointF
uv_top_left(0.f
, 0.f
);
80 gfx::PointF
uv_bottom_right(1.f
, 1.f
);
82 SharedQuadState
* shared_quad_state
=
83 render_pass
->CreateAndAppendSharedQuadState();
84 PopulateScaledSharedQuadState(shared_quad_state
, internal_contents_scale_
);
86 AppendDebugBorderQuad(render_pass
, internal_content_bounds_
,
87 shared_quad_state
, append_quads_data
);
89 gfx::Rect thumb_quad_rect
= ComputeThumbQuadRect();
90 gfx::Rect scaled_thumb_quad_rect
=
91 gfx::ScaleToEnclosingRect(thumb_quad_rect
, internal_contents_scale_
);
92 gfx::Rect visible_thumb_quad_rect
=
93 draw_properties().occlusion_in_content_space
.GetUnoccludedContentRect(
95 gfx::Rect scaled_visible_thumb_quad_rect
= gfx::ScaleToEnclosingRect(
96 visible_thumb_quad_rect
, internal_contents_scale_
);
98 ResourceProvider::ResourceId thumb_resource_id
=
99 layer_tree_impl()->ResourceIdForUIResource(thumb_ui_resource_id_
);
100 ResourceProvider::ResourceId track_resource_id
=
101 layer_tree_impl()->ResourceIdForUIResource(track_ui_resource_id_
);
103 if (thumb_resource_id
&& !visible_thumb_quad_rect
.IsEmpty()) {
104 gfx::Rect opaque_rect
;
105 const float opacity
[] = {1.0f
, 1.0f
, 1.0f
, 1.0f
};
106 // TODO(danakj): crbug.com/455931
107 layer_tree_impl()->resource_provider()->ValidateResource(thumb_resource_id
);
108 TextureDrawQuad
* quad
=
109 render_pass
->CreateAndAppendDrawQuad
<TextureDrawQuad
>();
110 quad
->SetNew(shared_quad_state
, scaled_thumb_quad_rect
, opaque_rect
,
111 scaled_visible_thumb_quad_rect
, thumb_resource_id
,
112 premultipled_alpha
, uv_top_left
, uv_bottom_right
,
113 SK_ColorTRANSPARENT
, opacity
, flipped
, nearest_neighbor
);
116 gfx::Rect
track_quad_rect(bounds());
117 gfx::Rect
scaled_track_quad_rect(internal_content_bounds_
);
118 gfx::Rect visible_track_quad_rect
=
119 draw_properties().occlusion_in_content_space
.GetUnoccludedContentRect(
121 gfx::Rect scaled_visible_track_quad_rect
= gfx::ScaleToEnclosingRect(
122 visible_track_quad_rect
, internal_contents_scale_
);
123 if (track_resource_id
&& !visible_track_quad_rect
.IsEmpty()) {
124 gfx::Rect
opaque_rect(contents_opaque() ? scaled_track_quad_rect
126 const float opacity
[] = {1.0f
, 1.0f
, 1.0f
, 1.0f
};
127 // TODO(danakj): crbug.com/455931
128 layer_tree_impl()->resource_provider()->ValidateResource(track_resource_id
);
129 TextureDrawQuad
* quad
=
130 render_pass
->CreateAndAppendDrawQuad
<TextureDrawQuad
>();
131 quad
->SetNew(shared_quad_state
, scaled_track_quad_rect
, opaque_rect
,
132 scaled_visible_track_quad_rect
, track_resource_id
,
133 premultipled_alpha
, uv_top_left
, uv_bottom_right
,
134 SK_ColorTRANSPARENT
, opacity
, flipped
, nearest_neighbor
);
138 gfx::Rect
PaintedScrollbarLayerImpl::GetEnclosingRectInTargetSpace() const {
139 return GetScaledEnclosingRectInTargetSpace(internal_contents_scale_
);
142 void PaintedScrollbarLayerImpl::SetThumbThickness(int thumb_thickness
) {
143 if (thumb_thickness_
== thumb_thickness
)
145 thumb_thickness_
= thumb_thickness
;
146 NoteLayerPropertyChanged();
149 int PaintedScrollbarLayerImpl::ThumbThickness() const {
150 return thumb_thickness_
;
153 void PaintedScrollbarLayerImpl::SetThumbLength(int thumb_length
) {
154 if (thumb_length_
== thumb_length
)
156 thumb_length_
= thumb_length
;
157 NoteLayerPropertyChanged();
160 int PaintedScrollbarLayerImpl::ThumbLength() const {
161 return thumb_length_
;
164 void PaintedScrollbarLayerImpl::SetTrackStart(int track_start
) {
165 if (track_start_
== track_start
)
167 track_start_
= track_start
;
168 NoteLayerPropertyChanged();
171 int PaintedScrollbarLayerImpl::TrackStart() const {
175 void PaintedScrollbarLayerImpl::SetTrackLength(int track_length
) {
176 if (track_length_
== track_length
)
178 track_length_
= track_length
;
179 NoteLayerPropertyChanged();
182 float PaintedScrollbarLayerImpl::TrackLength() const {
183 return track_length_
+ (orientation() == VERTICAL
? vertical_adjust() : 0);
186 bool PaintedScrollbarLayerImpl::IsThumbResizable() const {
190 const char* PaintedScrollbarLayerImpl::LayerTypeAsString() const {
191 return "cc::PaintedScrollbarLayerImpl";