• 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/layers/quad_sink.h"
12 #include "cc/quads/solid_color_draw_quad.h"
13 #include "cc/quads/texture_draw_quad.h"
14 #include "cc/trees/layer_tree_impl.h"
15 #include "cc/trees/layer_tree_settings.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),
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(QuadSink * quad_sink,AppendQuadsData * append_quads_data)71 void PaintedScrollbarLayerImpl::AppendQuads(
72     QuadSink* quad_sink,
73     AppendQuadsData* append_quads_data) {
74   bool premultipled_alpha = true;
75   bool flipped = false;
76   gfx::PointF uv_top_left(0.f, 0.f);
77   gfx::PointF uv_bottom_right(1.f, 1.f);
78   gfx::Rect bounds_rect(bounds());
79   gfx::Rect content_bounds_rect(content_bounds());
80 
81   SharedQuadState* shared_quad_state =
82       quad_sink->UseSharedQuadState(CreateSharedQuadState());
83   AppendDebugBorderQuad(quad_sink, shared_quad_state, append_quads_data);
84 
85   gfx::Rect thumb_quad_rect = ComputeThumbQuadRect();
86 
87   ResourceProvider::ResourceId thumb_resource_id =
88       layer_tree_impl()->ResourceIdForUIResource(thumb_ui_resource_id_);
89   ResourceProvider::ResourceId track_resource_id =
90       layer_tree_impl()->ResourceIdForUIResource(track_ui_resource_id_);
91 
92   if (thumb_resource_id && !thumb_quad_rect.IsEmpty()) {
93     gfx::Rect opaque_rect;
94     const float opacity[] = {1.0f, 1.0f, 1.0f, 1.0f};
95     scoped_ptr<TextureDrawQuad> quad = TextureDrawQuad::Create();
96     quad->SetNew(shared_quad_state,
97                  thumb_quad_rect,
98                  opaque_rect,
99                  thumb_resource_id,
100                  premultipled_alpha,
101                  uv_top_left,
102                  uv_bottom_right,
103                  SK_ColorTRANSPARENT,
104                  opacity,
105                  flipped);
106     quad_sink->Append(quad.PassAs<DrawQuad>(), append_quads_data);
107   }
108 
109   gfx::Rect track_quad_rect = content_bounds_rect;
110   if (track_resource_id && !track_quad_rect.IsEmpty()) {
111     gfx::Rect opaque_rect(contents_opaque() ? track_quad_rect : gfx::Rect());
112     const float opacity[] = {1.0f, 1.0f, 1.0f, 1.0f};
113     scoped_ptr<TextureDrawQuad> quad = TextureDrawQuad::Create();
114     quad->SetNew(shared_quad_state,
115                  track_quad_rect,
116                  opaque_rect,
117                  track_resource_id,
118                  premultipled_alpha,
119                  uv_top_left,
120                  uv_bottom_right,
121                  SK_ColorTRANSPARENT,
122                  opacity,
123                  flipped);
124     quad_sink->Append(quad.PassAs<DrawQuad>(), append_quads_data);
125   }
126 }
127 
SetThumbThickness(int thumb_thickness)128 void PaintedScrollbarLayerImpl::SetThumbThickness(int thumb_thickness) {
129   if (thumb_thickness_ == thumb_thickness)
130     return;
131   thumb_thickness_ = thumb_thickness;
132   NoteLayerPropertyChanged();
133 }
134 
ThumbThickness() const135 int PaintedScrollbarLayerImpl::ThumbThickness() const {
136   return thumb_thickness_;
137 }
138 
SetThumbLength(int thumb_length)139 void PaintedScrollbarLayerImpl::SetThumbLength(int thumb_length) {
140   if (thumb_length_ == thumb_length)
141     return;
142   thumb_length_ = thumb_length;
143   NoteLayerPropertyChanged();
144 }
145 
ThumbLength() const146 int PaintedScrollbarLayerImpl::ThumbLength() const {
147   return thumb_length_;
148 }
149 
SetTrackStart(int track_start)150 void PaintedScrollbarLayerImpl::SetTrackStart(int track_start) {
151   if (track_start_ == track_start)
152     return;
153   track_start_ = track_start;
154   NoteLayerPropertyChanged();
155 }
156 
TrackStart() const157 int PaintedScrollbarLayerImpl::TrackStart() const {
158   return track_start_;
159 }
160 
SetTrackLength(int track_length)161 void PaintedScrollbarLayerImpl::SetTrackLength(int track_length) {
162   if (track_length_ == track_length)
163     return;
164   track_length_ = track_length;
165   NoteLayerPropertyChanged();
166 }
167 
TrackLength() const168 float PaintedScrollbarLayerImpl::TrackLength() const {
169   return track_length_ + (orientation() == VERTICAL ? vertical_adjust() : 0);
170 }
171 
LayerTypeAsString() const172 const char* PaintedScrollbarLayerImpl::LayerTypeAsString() const {
173   return "cc::PaintedScrollbarLayerImpl";
174 }
175 
176 }  // namespace cc
177