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/quad_sink.h"
8 #include "cc/scrollbar_animation_controller.h"
9 #include "cc/texture_draw_quad.h"
10 #include "ui/gfx/rect_conversions.h"
12 using WebKit::WebRect
;
13 using WebKit::WebScrollbar
;
17 scoped_ptr
<ScrollbarLayerImpl
> ScrollbarLayerImpl::create(int id
)
19 return make_scoped_ptr(new ScrollbarLayerImpl(id
));
22 ScrollbarLayerImpl::ScrollbarLayerImpl(int id
)
25 , m_backTrackResourceId(0)
26 , m_foreTrackResourceId(0)
27 , m_thumbResourceId(0)
28 , m_scrollbarOverlayStyle(WebScrollbar::ScrollbarOverlayStyleDefault
)
29 , m_orientation(WebScrollbar::Horizontal
)
30 , m_controlSize(WebScrollbar::RegularScrollbar
)
31 , m_pressedPart(WebScrollbar::NoPart
)
32 , m_hoveredPart(WebScrollbar::NoPart
)
33 , m_isScrollableAreaActive(false)
34 , m_isScrollViewScrollbar(false)
36 , m_isCustomScrollbar(false)
37 , m_isOverlayScrollbar(false)
41 ScrollbarLayerImpl::~ScrollbarLayerImpl()
45 void ScrollbarLayerImpl::setScrollbarGeometry(scoped_ptr
<ScrollbarGeometryFixedThumb
> geometry
)
47 m_geometry
= geometry
.Pass();
50 void ScrollbarLayerImpl::setScrollbarData(WebScrollbar
* scrollbar
)
52 m_scrollbarOverlayStyle
= scrollbar
->scrollbarOverlayStyle();
53 m_orientation
= scrollbar
->orientation();
54 m_controlSize
= scrollbar
->controlSize();
55 m_pressedPart
= scrollbar
->pressedPart();
56 m_hoveredPart
= scrollbar
->hoveredPart();
57 m_isScrollableAreaActive
= scrollbar
->isScrollableAreaActive();
58 m_isScrollViewScrollbar
= scrollbar
->isScrollViewScrollbar();
59 m_enabled
= scrollbar
->enabled();
60 m_isCustomScrollbar
= scrollbar
->isCustomScrollbar();
61 m_isOverlayScrollbar
= scrollbar
->isOverlay();
63 scrollbar
->getTickmarks(m_tickmarks
);
65 m_geometry
->update(scrollbar
);
68 static gfx::RectF
toUVRect(const gfx::Rect
& r
, const gfx::Rect
& bounds
)
70 return gfx::ScaleRect(r
, 1.0 / bounds
.width(), 1.0 / bounds
.height());
73 gfx::Rect
ScrollbarLayerImpl::scrollbarLayerRectToContentRect(const gfx::Rect
& layerRect
) const
75 // Don't intersect with the bounds as in layerRectToContentRect() because
76 // layerRect here might be in coordinates of the containing layer.
77 gfx::RectF contentRect
= gfx::ScaleRect(layerRect
, contentsScaleX(), contentsScaleY());
78 return gfx::ToEnclosingRect(contentRect
);
81 void ScrollbarLayerImpl::appendQuads(QuadSink
& quadSink
, AppendQuadsData
& appendQuadsData
)
83 bool premultipledAlpha
= false;
85 gfx::RectF
uvRect(0, 0, 1, 1);
86 gfx::Rect
boundsRect(gfx::Point(), bounds());
87 gfx::Rect
contentBoundsRect(gfx::Point(), contentBounds());
89 SharedQuadState
* sharedQuadState
= quadSink
.useSharedQuadState(createSharedQuadState());
90 appendDebugBorderQuad(quadSink
, sharedQuadState
, appendQuadsData
);
92 WebRect thumbRect
, backTrackRect
, foreTrackRect
;
93 m_geometry
->splitTrack(&m_scrollbar
, m_geometry
->trackRect(&m_scrollbar
), backTrackRect
, thumbRect
, foreTrackRect
);
94 if (!m_geometry
->hasThumb(&m_scrollbar
))
95 thumbRect
= WebRect();
97 if (m_thumbResourceId
&& !thumbRect
.isEmpty()) {
98 gfx::Rect
quadRect(scrollbarLayerRectToContentRect(thumbRect
));
100 scoped_ptr
<TextureDrawQuad
> quad
= TextureDrawQuad::Create();
101 quad
->SetNew(sharedQuadState
, quadRect
, opaqueRect
, m_thumbResourceId
, premultipledAlpha
, uvRect
, flipped
);
102 quadSink
.append(quad
.PassAs
<DrawQuad
>(), appendQuadsData
);
105 if (!m_backTrackResourceId
)
108 // We only paint the track in two parts if we were given a texture for the forward track part.
109 if (m_foreTrackResourceId
&& !foreTrackRect
.isEmpty()) {
110 gfx::Rect
quadRect(scrollbarLayerRectToContentRect(foreTrackRect
));
111 gfx::Rect
opaqueRect(contentsOpaque() ? quadRect
: gfx::Rect());
112 scoped_ptr
<TextureDrawQuad
> quad
= TextureDrawQuad::Create();
113 quad
->SetNew(sharedQuadState
, quadRect
, opaqueRect
, m_foreTrackResourceId
, premultipledAlpha
, toUVRect(foreTrackRect
, boundsRect
), flipped
);
114 quadSink
.append(quad
.PassAs
<DrawQuad
>(), appendQuadsData
);
117 // Order matters here: since the back track texture is being drawn to the entire contents rect, we must append it after the thumb and
118 // fore track quads. The back track texture contains (and displays) the buttons.
119 if (!contentBoundsRect
.IsEmpty()) {
120 gfx::Rect
quadRect(contentBoundsRect
);
121 gfx::Rect
opaqueRect(contentsOpaque() ? quadRect
: gfx::Rect());
122 scoped_ptr
<TextureDrawQuad
> quad
= TextureDrawQuad::Create();
123 quad
->SetNew(sharedQuadState
, quadRect
, opaqueRect
, m_backTrackResourceId
, premultipledAlpha
, uvRect
, flipped
);
124 quadSink
.append(quad
.PassAs
<DrawQuad
>(), appendQuadsData
);
128 void ScrollbarLayerImpl::didLoseContext()
130 m_backTrackResourceId
= 0;
131 m_foreTrackResourceId
= 0;
132 m_thumbResourceId
= 0;
135 bool ScrollbarLayerImpl::Scrollbar::isOverlay() const
137 return m_owner
->m_isOverlayScrollbar
;
140 int ScrollbarLayerImpl::Scrollbar::value() const
142 return m_owner
->m_currentPos
;
145 WebKit::WebPoint
ScrollbarLayerImpl::Scrollbar::location() const
147 return WebKit::WebPoint();
150 WebKit::WebSize
ScrollbarLayerImpl::Scrollbar::size() const
152 return WebKit::WebSize(m_owner
->bounds().width(), m_owner
->bounds().height());
155 bool ScrollbarLayerImpl::Scrollbar::enabled() const
157 return m_owner
->m_enabled
;
160 int ScrollbarLayerImpl::Scrollbar::maximum() const
162 return m_owner
->m_maximum
;
165 int ScrollbarLayerImpl::Scrollbar::totalSize() const
167 return m_owner
->m_totalSize
;
170 bool ScrollbarLayerImpl::Scrollbar::isScrollViewScrollbar() const
172 return m_owner
->m_isScrollViewScrollbar
;
175 bool ScrollbarLayerImpl::Scrollbar::isScrollableAreaActive() const
177 return m_owner
->m_isScrollableAreaActive
;
180 void ScrollbarLayerImpl::Scrollbar::getTickmarks(WebKit::WebVector
<WebRect
>& tickmarks
) const
182 tickmarks
= m_owner
->m_tickmarks
;
185 WebScrollbar::ScrollbarControlSize
ScrollbarLayerImpl::Scrollbar::controlSize() const
187 return m_owner
->m_controlSize
;
190 WebScrollbar::ScrollbarPart
ScrollbarLayerImpl::Scrollbar::pressedPart() const
192 return m_owner
->m_pressedPart
;
195 WebScrollbar::ScrollbarPart
ScrollbarLayerImpl::Scrollbar::hoveredPart() const
197 return m_owner
->m_hoveredPart
;
200 WebScrollbar::ScrollbarOverlayStyle
ScrollbarLayerImpl::Scrollbar::scrollbarOverlayStyle() const
202 return m_owner
->m_scrollbarOverlayStyle
;
205 WebScrollbar::Orientation
ScrollbarLayerImpl::Scrollbar::orientation() const
207 return m_owner
->m_orientation
;
210 bool ScrollbarLayerImpl::Scrollbar::isCustomScrollbar() const
212 return m_owner
->m_isCustomScrollbar
;
215 const char* ScrollbarLayerImpl::layerTypeAsString() const
217 return "ScrollbarLayer";