• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4 
5 #include "cc/layers/painted_scrollbar_layer_impl.h"
6 
7 #include <algorithm>
8 
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_tracker.h"
16 #include "ui/gfx/rect_conversions.h"
17 
18 namespace cc {
19 
Create(LayerTreeImpl * tree_impl,int id,ScrollbarOrientation orientation)20 scoped_ptr<PaintedScrollbarLayerImpl> PaintedScrollbarLayerImpl::Create(
21     LayerTreeImpl* tree_impl,
22     int id,
23     ScrollbarOrientation orientation) {
24   return make_scoped_ptr(
25       new PaintedScrollbarLayerImpl(tree_impl, id, orientation));
26 }
27 
PaintedScrollbarLayerImpl(LayerTreeImpl * tree_impl,int id,ScrollbarOrientation orientation)28 PaintedScrollbarLayerImpl::PaintedScrollbarLayerImpl(
29     LayerTreeImpl* tree_impl,
30     int id,
31     ScrollbarOrientation orientation)
32     : ScrollbarLayerImplBase(tree_impl, id, orientation, false, false),
33       track_ui_resource_id_(0),
34       thumb_ui_resource_id_(0),
35       thumb_thickness_(0),
36       thumb_length_(0),
37       track_start_(0),
38       track_length_(0),
39       vertical_adjust_(0.f),
40       scroll_layer_id_(Layer::INVALID_ID) {}
41 
~PaintedScrollbarLayerImpl()42 PaintedScrollbarLayerImpl::~PaintedScrollbarLayerImpl() {}
43 
CreateLayerImpl(LayerTreeImpl * tree_impl)44 scoped_ptr<LayerImpl> PaintedScrollbarLayerImpl::CreateLayerImpl(
45     LayerTreeImpl* tree_impl) {
46   return PaintedScrollbarLayerImpl::Create(tree_impl, id(), orientation())
47       .PassAs<LayerImpl>();
48 }
49 
PushPropertiesTo(LayerImpl * layer)50 void PaintedScrollbarLayerImpl::PushPropertiesTo(LayerImpl* layer) {
51   ScrollbarLayerImplBase::PushPropertiesTo(layer);
52 
53   PaintedScrollbarLayerImpl* scrollbar_layer =
54       static_cast<PaintedScrollbarLayerImpl*>(layer);
55 
56   scrollbar_layer->SetThumbThickness(thumb_thickness_);
57   scrollbar_layer->SetThumbLength(thumb_length_);
58   scrollbar_layer->SetTrackStart(track_start_);
59   scrollbar_layer->SetTrackLength(track_length_);
60 
61   scrollbar_layer->set_track_ui_resource_id(track_ui_resource_id_);
62   scrollbar_layer->set_thumb_ui_resource_id(thumb_ui_resource_id_);
63 }
64 
WillDraw(DrawMode draw_mode,ResourceProvider * resource_provider)65 bool PaintedScrollbarLayerImpl::WillDraw(DrawMode draw_mode,
66                                   ResourceProvider* resource_provider) {
67   DCHECK(draw_mode != DRAW_MODE_RESOURCELESS_SOFTWARE);
68   return LayerImpl::WillDraw(draw_mode, resource_provider);
69 }
70 
AppendQuads(RenderPass * render_pass,const OcclusionTracker<LayerImpl> & occlusion_tracker,AppendQuadsData * append_quads_data)71 void PaintedScrollbarLayerImpl::AppendQuads(
72     RenderPass* render_pass,
73     const OcclusionTracker<LayerImpl>& occlusion_tracker,
74     AppendQuadsData* append_quads_data) {
75   bool premultipled_alpha = true;
76   bool flipped = false;
77   gfx::PointF uv_top_left(0.f, 0.f);
78   gfx::PointF uv_bottom_right(1.f, 1.f);
79   gfx::Rect bounds_rect(bounds());
80   gfx::Rect content_bounds_rect(content_bounds());
81 
82   SharedQuadState* shared_quad_state =
83       render_pass->CreateAndAppendSharedQuadState();
84   PopulateSharedQuadState(shared_quad_state);
85 
86   AppendDebugBorderQuad(
87       render_pass, content_bounds(), shared_quad_state, append_quads_data);
88 
89   gfx::Rect thumb_quad_rect = ComputeThumbQuadRect();
90   Occlusion occlusion =
91       occlusion_tracker.GetCurrentOcclusionForLayer(draw_transform());
92   gfx::Rect visible_thumb_quad_rect =
93       occlusion.GetUnoccludedContentRect(thumb_quad_rect);
94 
95   ResourceProvider::ResourceId thumb_resource_id =
96       layer_tree_impl()->ResourceIdForUIResource(thumb_ui_resource_id_);
97   ResourceProvider::ResourceId track_resource_id =
98       layer_tree_impl()->ResourceIdForUIResource(track_ui_resource_id_);
99 
100   if (thumb_resource_id && !visible_thumb_quad_rect.IsEmpty()) {
101     gfx::Rect opaque_rect;
102     const float opacity[] = {1.0f, 1.0f, 1.0f, 1.0f};
103     TextureDrawQuad* quad =
104         render_pass->CreateAndAppendDrawQuad<TextureDrawQuad>();
105     quad->SetNew(shared_quad_state,
106                  thumb_quad_rect,
107                  opaque_rect,
108                  visible_thumb_quad_rect,
109                  thumb_resource_id,
110                  premultipled_alpha,
111                  uv_top_left,
112                  uv_bottom_right,
113                  SK_ColorTRANSPARENT,
114                  opacity,
115                  flipped);
116   }
117 
118   gfx::Rect track_quad_rect = content_bounds_rect;
119   gfx::Rect visible_track_quad_rect =
120       occlusion.GetUnoccludedContentRect(track_quad_rect);
121   if (track_resource_id && !visible_track_quad_rect.IsEmpty()) {
122     gfx::Rect opaque_rect(contents_opaque() ? track_quad_rect : gfx::Rect());
123     const float opacity[] = {1.0f, 1.0f, 1.0f, 1.0f};
124     TextureDrawQuad* quad =
125         render_pass->CreateAndAppendDrawQuad<TextureDrawQuad>();
126     quad->SetNew(shared_quad_state,
127                  track_quad_rect,
128                  opaque_rect,
129                  visible_track_quad_rect,
130                  track_resource_id,
131                  premultipled_alpha,
132                  uv_top_left,
133                  uv_bottom_right,
134                  SK_ColorTRANSPARENT,
135                  opacity,
136                  flipped);
137   }
138 }
139 
SetThumbThickness(int thumb_thickness)140 void PaintedScrollbarLayerImpl::SetThumbThickness(int thumb_thickness) {
141   if (thumb_thickness_ == thumb_thickness)
142     return;
143   thumb_thickness_ = thumb_thickness;
144   NoteLayerPropertyChanged();
145 }
146 
ThumbThickness() const147 int PaintedScrollbarLayerImpl::ThumbThickness() const {
148   return thumb_thickness_;
149 }
150 
SetThumbLength(int thumb_length)151 void PaintedScrollbarLayerImpl::SetThumbLength(int thumb_length) {
152   if (thumb_length_ == thumb_length)
153     return;
154   thumb_length_ = thumb_length;
155   NoteLayerPropertyChanged();
156 }
157 
ThumbLength() const158 int PaintedScrollbarLayerImpl::ThumbLength() const {
159   return thumb_length_;
160 }
161 
SetTrackStart(int track_start)162 void PaintedScrollbarLayerImpl::SetTrackStart(int track_start) {
163   if (track_start_ == track_start)
164     return;
165   track_start_ = track_start;
166   NoteLayerPropertyChanged();
167 }
168 
TrackStart() const169 int PaintedScrollbarLayerImpl::TrackStart() const {
170   return track_start_;
171 }
172 
SetTrackLength(int track_length)173 void PaintedScrollbarLayerImpl::SetTrackLength(int track_length) {
174   if (track_length_ == track_length)
175     return;
176   track_length_ = track_length;
177   NoteLayerPropertyChanged();
178 }
179 
TrackLength() const180 float PaintedScrollbarLayerImpl::TrackLength() const {
181   return track_length_ + (orientation() == VERTICAL ? vertical_adjust() : 0);
182 }
183 
IsThumbResizable() const184 bool PaintedScrollbarLayerImpl::IsThumbResizable() const {
185   return false;
186 }
187 
LayerTypeAsString() const188 const char* PaintedScrollbarLayerImpl::LayerTypeAsString() const {
189   return "cc::PaintedScrollbarLayerImpl";
190 }
191 
192 }  // namespace cc
193