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
3 // found in the LICENSE file.
5 #include "cc/scrollbar_layer_impl.h"
7 #include "cc/layer_tree_impl.h"
8 #include "cc/quad_sink.h"
9 #include "cc/scrollbar_animation_controller.h"
10 #include "cc/texture_draw_quad.h"
11 #include "ui/gfx/rect_conversions.h"
13 using WebKit::WebRect
;
14 using WebKit::WebScrollbar
;
18 scoped_ptr
<ScrollbarLayerImpl
> ScrollbarLayerImpl::create(LayerTreeImpl
* treeImpl
, int id
, scoped_ptr
<ScrollbarGeometryFixedThumb
> geometry
)
20 return make_scoped_ptr(new ScrollbarLayerImpl(treeImpl
, id
, geometry
.Pass()));
23 ScrollbarLayerImpl::ScrollbarLayerImpl(LayerTreeImpl
* treeImpl
, int id
, scoped_ptr
<ScrollbarGeometryFixedThumb
> geometry
)
24 : ScrollbarLayerImplBase(treeImpl
, id
)
26 , m_backTrackResourceId(0)
27 , m_foreTrackResourceId(0)
28 , m_thumbResourceId(0)
29 , m_geometry(geometry
.Pass())
34 , m_scrollbarOverlayStyle(WebScrollbar::ScrollbarOverlayStyleDefault
)
35 , m_orientation(WebScrollbar::Horizontal
)
36 , m_controlSize(WebScrollbar::RegularScrollbar
)
37 , m_pressedPart(WebScrollbar::NoPart
)
38 , m_hoveredPart(WebScrollbar::NoPart
)
39 , m_isScrollableAreaActive(false)
40 , m_isScrollViewScrollbar(false)
42 , m_isCustomScrollbar(false)
43 , m_isOverlayScrollbar(false)
47 ScrollbarLayerImpl::~ScrollbarLayerImpl()
51 ScrollbarLayerImpl
* ScrollbarLayerImpl::toScrollbarLayer()
56 void ScrollbarLayerImpl::setScrollbarData(WebScrollbar
* scrollbar
)
58 m_scrollbarOverlayStyle
= scrollbar
->scrollbarOverlayStyle();
59 m_orientation
= scrollbar
->orientation();
60 m_controlSize
= scrollbar
->controlSize();
61 m_pressedPart
= scrollbar
->pressedPart();
62 m_hoveredPart
= scrollbar
->hoveredPart();
63 m_isScrollableAreaActive
= scrollbar
->isScrollableAreaActive();
64 m_isScrollViewScrollbar
= scrollbar
->isScrollViewScrollbar();
65 m_enabled
= scrollbar
->enabled();
66 m_isCustomScrollbar
= scrollbar
->isCustomScrollbar();
67 m_isOverlayScrollbar
= scrollbar
->isOverlay();
69 scrollbar
->getTickmarks(m_tickmarks
);
72 void ScrollbarLayerImpl::setThumbSize(gfx::Size size
)
76 // In impl-side painting, the ScrollbarLayerImpl in the pending tree
77 // simply holds properties that are later pushed to the active tree's
78 // layer, but it doesn't hold geometry or append quads.
79 DCHECK(layerTreeImpl()->IsPendingTree());
82 m_geometry
->setThumbSize(size
);
85 float ScrollbarLayerImpl::currentPos() const
90 int ScrollbarLayerImpl::totalSize() const
95 int ScrollbarLayerImpl::maximum() const
100 WebKit::WebScrollbar::Orientation
ScrollbarLayerImpl::orientation() const
102 return m_orientation
;
105 static gfx::RectF
toUVRect(const gfx::Rect
& r
, const gfx::Rect
& bounds
)
107 return gfx::ScaleRect(r
, 1.0 / bounds
.width(), 1.0 / bounds
.height());
110 gfx::Rect
ScrollbarLayerImpl::scrollbarLayerRectToContentRect(const gfx::Rect
& layerRect
) const
112 // Don't intersect with the bounds as in layerRectToContentRect() because
113 // layerRect here might be in coordinates of the containing layer.
114 gfx::RectF contentRect
= gfx::ScaleRect(layerRect
, contentsScaleX(), contentsScaleY());
115 return gfx::ToEnclosingRect(contentRect
);
118 scoped_ptr
<LayerImpl
> ScrollbarLayerImpl::createLayerImpl(LayerTreeImpl
* treeImpl
)
120 return ScrollbarLayerImpl::create(treeImpl
, id(), m_geometry
.Pass()).PassAs
<LayerImpl
>();
123 void ScrollbarLayerImpl::pushPropertiesTo(LayerImpl
* layer
)
125 LayerImpl::pushPropertiesTo(layer
);
127 ScrollbarLayerImpl
* scrollbarLayer
= static_cast<ScrollbarLayerImpl
*>(layer
);
129 scrollbarLayer
->setScrollbarData(&m_scrollbar
);
130 scrollbarLayer
->setThumbSize(m_thumbSize
);
132 scrollbarLayer
->setBackTrackResourceId(m_backTrackResourceId
);
133 scrollbarLayer
->setForeTrackResourceId(m_foreTrackResourceId
);
134 scrollbarLayer
->setThumbResourceId(m_thumbResourceId
);
137 void ScrollbarLayerImpl::appendQuads(QuadSink
& quadSink
, AppendQuadsData
& appendQuadsData
)
139 bool premultipledAlpha
= false;
140 bool flipped
= false;
141 gfx::PointF
uvTopLeft(0.f
, 0.f
);
142 gfx::PointF
uvBottomRight(1.f
, 1.f
);
143 gfx::Rect
boundsRect(gfx::Point(), bounds());
144 gfx::Rect
contentBoundsRect(gfx::Point(), contentBounds());
146 SharedQuadState
* sharedQuadState
= quadSink
.useSharedQuadState(createSharedQuadState());
147 appendDebugBorderQuad(quadSink
, sharedQuadState
, appendQuadsData
);
149 WebRect thumbRect
, backTrackRect
, foreTrackRect
;
150 m_geometry
->splitTrack(&m_scrollbar
, m_geometry
->trackRect(&m_scrollbar
), backTrackRect
, thumbRect
, foreTrackRect
);
151 if (!m_geometry
->hasThumb(&m_scrollbar
))
152 thumbRect
= WebRect();
154 if (m_thumbResourceId
&& !thumbRect
.isEmpty()) {
155 gfx::Rect
quadRect(scrollbarLayerRectToContentRect(thumbRect
));
156 gfx::Rect opaqueRect
;
157 const float opacity
[] = {1.0f
, 1.0f
, 1.0f
, 1.0f
};
158 scoped_ptr
<TextureDrawQuad
> quad
= TextureDrawQuad::Create();
159 quad
->SetNew(sharedQuadState
, quadRect
, opaqueRect
, m_thumbResourceId
, premultipledAlpha
, uvTopLeft
, uvBottomRight
, opacity
, flipped
);
160 quadSink
.append(quad
.PassAs
<DrawQuad
>(), appendQuadsData
);
163 if (!m_backTrackResourceId
)
166 // We only paint the track in two parts if we were given a texture for the forward track part.
167 if (m_foreTrackResourceId
&& !foreTrackRect
.isEmpty()) {
168 gfx::Rect
quadRect(scrollbarLayerRectToContentRect(foreTrackRect
));
169 gfx::Rect
opaqueRect(contentsOpaque() ? quadRect
: gfx::Rect());
170 gfx::RectF
uvRect(toUVRect(foreTrackRect
, boundsRect
));
171 const float opacity
[] = {1.0f
, 1.0f
, 1.0f
, 1.0f
};
172 scoped_ptr
<TextureDrawQuad
> quad
= TextureDrawQuad::Create();
173 quad
->SetNew(sharedQuadState
, quadRect
, opaqueRect
, m_foreTrackResourceId
, premultipledAlpha
, uvRect
.origin(), uvRect
.bottom_right(), opacity
, flipped
);
174 quadSink
.append(quad
.PassAs
<DrawQuad
>(), appendQuadsData
);
177 // Order matters here: since the back track texture is being drawn to the entire contents rect, we must append it after the thumb and
178 // fore track quads. The back track texture contains (and displays) the buttons.
179 if (!contentBoundsRect
.IsEmpty()) {
180 gfx::Rect
quadRect(contentBoundsRect
);
181 gfx::Rect
opaqueRect(contentsOpaque() ? quadRect
: gfx::Rect());
182 const float opacity
[] = {1.0f
, 1.0f
, 1.0f
, 1.0f
};
183 scoped_ptr
<TextureDrawQuad
> quad
= TextureDrawQuad::Create();
184 quad
->SetNew(sharedQuadState
, quadRect
, opaqueRect
, m_backTrackResourceId
, premultipledAlpha
, uvTopLeft
, uvBottomRight
, opacity
, flipped
);
185 quadSink
.append(quad
.PassAs
<DrawQuad
>(), appendQuadsData
);
189 void ScrollbarLayerImpl::didLoseOutputSurface()
191 m_backTrackResourceId
= 0;
192 m_foreTrackResourceId
= 0;
193 m_thumbResourceId
= 0;
196 bool ScrollbarLayerImpl::Scrollbar::isOverlay() const
198 return m_owner
->m_isOverlayScrollbar
;
201 int ScrollbarLayerImpl::Scrollbar::value() const
203 return m_owner
->currentPos();
206 WebKit::WebPoint
ScrollbarLayerImpl::Scrollbar::location() const
208 return WebKit::WebPoint();
211 WebKit::WebSize
ScrollbarLayerImpl::Scrollbar::size() const
213 return WebKit::WebSize(m_owner
->bounds().width(), m_owner
->bounds().height());
216 bool ScrollbarLayerImpl::Scrollbar::enabled() const
218 return m_owner
->m_enabled
;
221 int ScrollbarLayerImpl::Scrollbar::maximum() const
223 return m_owner
->maximum();
226 int ScrollbarLayerImpl::Scrollbar::totalSize() const
228 return m_owner
->totalSize();
231 bool ScrollbarLayerImpl::Scrollbar::isScrollViewScrollbar() const
233 return m_owner
->m_isScrollViewScrollbar
;
236 bool ScrollbarLayerImpl::Scrollbar::isScrollableAreaActive() const
238 return m_owner
->m_isScrollableAreaActive
;
241 void ScrollbarLayerImpl::Scrollbar::getTickmarks(WebKit::WebVector
<WebRect
>& tickmarks
) const
243 tickmarks
= m_owner
->m_tickmarks
;
246 WebScrollbar::ScrollbarControlSize
ScrollbarLayerImpl::Scrollbar::controlSize() const
248 return m_owner
->m_controlSize
;
251 WebScrollbar::ScrollbarPart
ScrollbarLayerImpl::Scrollbar::pressedPart() const
253 return m_owner
->m_pressedPart
;
256 WebScrollbar::ScrollbarPart
ScrollbarLayerImpl::Scrollbar::hoveredPart() const
258 return m_owner
->m_hoveredPart
;
261 WebScrollbar::ScrollbarOverlayStyle
ScrollbarLayerImpl::Scrollbar::scrollbarOverlayStyle() const
263 return m_owner
->m_scrollbarOverlayStyle
;
266 WebScrollbar::Orientation
ScrollbarLayerImpl::Scrollbar::orientation() const
268 return m_owner
->m_orientation
;
271 bool ScrollbarLayerImpl::Scrollbar::isCustomScrollbar() const
273 return m_owner
->m_isCustomScrollbar
;
276 const char* ScrollbarLayerImpl::layerTypeAsString() const
278 return "ScrollbarLayer";