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(LayerTreeImpl
* treeImpl
, int id
)
19 return make_scoped_ptr(new ScrollbarLayerImpl(treeImpl
, id
));
22 ScrollbarLayerImpl::ScrollbarLayerImpl(LayerTreeImpl
* treeImpl
, int id
)
23 : ScrollbarLayerImplBase(treeImpl
, 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 float ScrollbarLayerImpl::currentPos() const
73 int ScrollbarLayerImpl::totalSize() const
78 int ScrollbarLayerImpl::maximum() const
83 WebKit::WebScrollbar::Orientation
ScrollbarLayerImpl::orientation() const
88 static gfx::RectF
toUVRect(const gfx::Rect
& r
, const gfx::Rect
& bounds
)
90 return gfx::ScaleRect(r
, 1.0 / bounds
.width(), 1.0 / bounds
.height());
93 gfx::Rect
ScrollbarLayerImpl::scrollbarLayerRectToContentRect(const gfx::Rect
& layerRect
) const
95 // Don't intersect with the bounds as in layerRectToContentRect() because
96 // layerRect here might be in coordinates of the containing layer.
97 gfx::RectF contentRect
= gfx::ScaleRect(layerRect
, contentsScaleX(), contentsScaleY());
98 return gfx::ToEnclosingRect(contentRect
);
101 void ScrollbarLayerImpl::appendQuads(QuadSink
& quadSink
, AppendQuadsData
& appendQuadsData
)
103 bool premultipledAlpha
= false;
104 bool flipped
= false;
105 gfx::RectF
uvRect(0, 0, 1, 1);
106 gfx::Rect
boundsRect(gfx::Point(), bounds());
107 gfx::Rect
contentBoundsRect(gfx::Point(), contentBounds());
109 SharedQuadState
* sharedQuadState
= quadSink
.useSharedQuadState(createSharedQuadState());
110 appendDebugBorderQuad(quadSink
, sharedQuadState
, appendQuadsData
);
112 WebRect thumbRect
, backTrackRect
, foreTrackRect
;
113 m_geometry
->splitTrack(&m_scrollbar
, m_geometry
->trackRect(&m_scrollbar
), backTrackRect
, thumbRect
, foreTrackRect
);
114 if (!m_geometry
->hasThumb(&m_scrollbar
))
115 thumbRect
= WebRect();
117 if (m_thumbResourceId
&& !thumbRect
.isEmpty()) {
118 gfx::Rect
quadRect(scrollbarLayerRectToContentRect(thumbRect
));
119 gfx::Rect opaqueRect
;
120 const float opacity
[] = {1.0f
, 1.0f
, 1.0f
, 1.0f
};
121 scoped_ptr
<TextureDrawQuad
> quad
= TextureDrawQuad::Create();
122 quad
->SetNew(sharedQuadState
, quadRect
, opaqueRect
, m_thumbResourceId
, premultipledAlpha
, uvRect
, opacity
, flipped
);
123 quadSink
.append(quad
.PassAs
<DrawQuad
>(), appendQuadsData
);
126 if (!m_backTrackResourceId
)
129 // We only paint the track in two parts if we were given a texture for the forward track part.
130 if (m_foreTrackResourceId
&& !foreTrackRect
.isEmpty()) {
131 gfx::Rect
quadRect(scrollbarLayerRectToContentRect(foreTrackRect
));
132 gfx::Rect
opaqueRect(contentsOpaque() ? quadRect
: gfx::Rect());
133 const float opacity
[] = {1.0f
, 1.0f
, 1.0f
, 1.0f
};
134 scoped_ptr
<TextureDrawQuad
> quad
= TextureDrawQuad::Create();
135 quad
->SetNew(sharedQuadState
, quadRect
, opaqueRect
, m_foreTrackResourceId
, premultipledAlpha
, toUVRect(foreTrackRect
, boundsRect
), opacity
, flipped
);
136 quadSink
.append(quad
.PassAs
<DrawQuad
>(), appendQuadsData
);
139 // Order matters here: since the back track texture is being drawn to the entire contents rect, we must append it after the thumb and
140 // fore track quads. The back track texture contains (and displays) the buttons.
141 if (!contentBoundsRect
.IsEmpty()) {
142 gfx::Rect
quadRect(contentBoundsRect
);
143 gfx::Rect
opaqueRect(contentsOpaque() ? quadRect
: gfx::Rect());
144 const float opacity
[] = {1.0f
, 1.0f
, 1.0f
, 1.0f
};
145 scoped_ptr
<TextureDrawQuad
> quad
= TextureDrawQuad::Create();
146 quad
->SetNew(sharedQuadState
, quadRect
, opaqueRect
, m_backTrackResourceId
, premultipledAlpha
, uvRect
, opacity
, flipped
);
147 quadSink
.append(quad
.PassAs
<DrawQuad
>(), appendQuadsData
);
151 void ScrollbarLayerImpl::didLoseOutputSurface()
153 m_backTrackResourceId
= 0;
154 m_foreTrackResourceId
= 0;
155 m_thumbResourceId
= 0;
158 bool ScrollbarLayerImpl::Scrollbar::isOverlay() const
160 return m_owner
->m_isOverlayScrollbar
;
163 int ScrollbarLayerImpl::Scrollbar::value() const
165 return m_owner
->m_currentPos
;
168 WebKit::WebPoint
ScrollbarLayerImpl::Scrollbar::location() const
170 return WebKit::WebPoint();
173 WebKit::WebSize
ScrollbarLayerImpl::Scrollbar::size() const
175 return WebKit::WebSize(m_owner
->bounds().width(), m_owner
->bounds().height());
178 bool ScrollbarLayerImpl::Scrollbar::enabled() const
180 return m_owner
->m_enabled
;
183 int ScrollbarLayerImpl::Scrollbar::maximum() const
185 return m_owner
->m_maximum
;
188 int ScrollbarLayerImpl::Scrollbar::totalSize() const
190 return m_owner
->m_totalSize
;
193 bool ScrollbarLayerImpl::Scrollbar::isScrollViewScrollbar() const
195 return m_owner
->m_isScrollViewScrollbar
;
198 bool ScrollbarLayerImpl::Scrollbar::isScrollableAreaActive() const
200 return m_owner
->m_isScrollableAreaActive
;
203 void ScrollbarLayerImpl::Scrollbar::getTickmarks(WebKit::WebVector
<WebRect
>& tickmarks
) const
205 tickmarks
= m_owner
->m_tickmarks
;
208 WebScrollbar::ScrollbarControlSize
ScrollbarLayerImpl::Scrollbar::controlSize() const
210 return m_owner
->m_controlSize
;
213 WebScrollbar::ScrollbarPart
ScrollbarLayerImpl::Scrollbar::pressedPart() const
215 return m_owner
->m_pressedPart
;
218 WebScrollbar::ScrollbarPart
ScrollbarLayerImpl::Scrollbar::hoveredPart() const
220 return m_owner
->m_hoveredPart
;
223 WebScrollbar::ScrollbarOverlayStyle
ScrollbarLayerImpl::Scrollbar::scrollbarOverlayStyle() const
225 return m_owner
->m_scrollbarOverlayStyle
;
228 WebScrollbar::Orientation
ScrollbarLayerImpl::Scrollbar::orientation() const
230 return m_owner
->m_orientation
;
233 bool ScrollbarLayerImpl::Scrollbar::isCustomScrollbar() const
235 return m_owner
->m_isCustomScrollbar
;
238 const char* ScrollbarLayerImpl::layerTypeAsString() const
240 return "ScrollbarLayer";