[chromium] Turn off painting of scrollbars on compositor thread for unsupported platforms
[WebKit-https.git] / Source / WebCore / page / scrolling / chromium / ScrollingCoordinatorChromium.cpp
1 /*
2  * Copyright (C) 2012 Google Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23  * THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 #include "config.h"
27
28 #include "ScrollingCoordinator.h"
29
30 #include "Frame.h"
31 #include "FrameView.h"
32 #include "LayerChromium.h"
33 #include "Region.h"
34 #include "RenderLayerCompositor.h"
35 #include "RenderView.h"
36 #include "ScrollbarLayerChromium.h"
37 #include "ScrollbarTheme.h"
38 #include "cc/CCProxy.h"
39
40 namespace WebCore {
41
42 class ScrollingCoordinatorPrivate {
43 WTF_MAKE_NONCOPYABLE(ScrollingCoordinatorPrivate);
44 public:
45     ScrollingCoordinatorPrivate() { }
46     ~ScrollingCoordinatorPrivate() { }
47
48     void setScrollLayer(LayerChromium* layer) { m_scrollLayer = layer; }
49     LayerChromium* scrollLayer() const { return m_scrollLayer.get(); }
50
51 private:
52     RefPtr<LayerChromium> m_scrollLayer;
53 };
54
55 PassRefPtr<ScrollingCoordinator> ScrollingCoordinator::create(Page* page)
56 {
57     RefPtr<ScrollingCoordinator> coordinator(adoptRef(new ScrollingCoordinator(page)));
58     coordinator->m_private = new ScrollingCoordinatorPrivate;
59     return coordinator.release();
60 }
61
62 ScrollingCoordinator::~ScrollingCoordinator()
63 {
64     ASSERT(!m_page);
65     delete m_private;
66 }
67
68 static GraphicsLayer* scrollLayerForFrameView(FrameView* frameView)
69 {
70 #if USE(ACCELERATED_COMPOSITING)
71     Frame* frame = frameView->frame();
72     if (!frame)
73         return 0;
74
75     RenderView* renderView = frame->contentRenderer();
76     if (!renderView)
77         return 0;
78     return renderView->compositor()->scrollLayer();
79 #else
80     return 0;
81 #endif
82 }
83
84 static void scrollbarLayerDidChange(Scrollbar* scrollbar, LayerChromium* scrollLayer, GraphicsLayer* scrollbarGraphicsLayer, FrameView* frameView)
85 {
86     ASSERT(scrollbar);
87     ASSERT(scrollbarGraphicsLayer);
88
89     if (!scrollLayer) {
90         // FIXME: sometimes we get called before setScrollLayer, workaround by finding the scroll layout ourselves.
91         scrollLayer = scrollLayerForFrameView(frameView)->platformLayer();
92         ASSERT(scrollLayer);
93     }
94
95     // Root layer non-overlay scrollbars should be marked opaque to disable
96     // blending.
97     bool isOpaqueRootScrollbar = !frameView->parent() && !scrollbar->isOverlayScrollbar();
98     if (!scrollbarGraphicsLayer->contentsOpaque())
99         scrollbarGraphicsLayer->setContentsOpaque(isOpaqueRootScrollbar);
100
101     // Only certain platforms support the way that scrollbars are currently
102     // being painted on the impl thread. For example, Cocoa is not threadsafe.
103     bool platformSupported = false;
104 #if OS(LINUX)
105     platformSupported = true;
106 #endif
107
108     if (scrollbar->isCustomScrollbar() || !CCProxy::hasImplThread() || !platformSupported) {
109         scrollbarGraphicsLayer->setContentsToMedia(0);
110         scrollbarGraphicsLayer->setDrawsContent(true);
111         return;
112     }
113
114     RefPtr<ScrollbarLayerChromium> scrollbarLayer = ScrollbarLayerChromium::create(scrollbar, scrollLayer->id());
115     scrollbarGraphicsLayer->setContentsToMedia(scrollbarLayer.get());
116     scrollbarGraphicsLayer->setDrawsContent(false);
117     scrollbarLayer->setOpaque(scrollbarGraphicsLayer->contentsOpaque());
118 }
119
120 void ScrollingCoordinator::frameViewHorizontalScrollbarLayerDidChange(FrameView* frameView, GraphicsLayer* horizontalScrollbarLayer)
121 {
122     if (!horizontalScrollbarLayer || !coordinatesScrollingForFrameView(frameView))
123         return;
124
125     scrollbarLayerDidChange(frameView->horizontalScrollbar(), m_private->scrollLayer(), horizontalScrollbarLayer, frameView);
126 }
127
128 void ScrollingCoordinator::frameViewVerticalScrollbarLayerDidChange(FrameView* frameView, GraphicsLayer* verticalScrollbarLayer)
129 {
130     if (!verticalScrollbarLayer || !coordinatesScrollingForFrameView(frameView))
131         return;
132
133     scrollbarLayerDidChange(frameView->verticalScrollbar(), m_private->scrollLayer(), verticalScrollbarLayer, frameView);
134 }
135
136 void ScrollingCoordinator::setScrollLayer(GraphicsLayer* scrollLayer)
137 {
138     m_private->setScrollLayer(scrollLayer ? scrollLayer->platformLayer() : 0);
139 }
140
141 void ScrollingCoordinator::setNonFastScrollableRegion(const Region& region)
142 {
143     if (LayerChromium* layer = m_private->scrollLayer())
144         layer->setNonFastScrollableRegion(region);
145 }
146
147 void ScrollingCoordinator::setScrollParameters(ScrollElasticity horizontalScrollElasticity, ScrollElasticity verticalScrollElasticity,
148                                                bool hasEnabledHorizontalScrollbar, bool hasEnabledVerticalScrollbar,
149                                                const IntRect& viewportRect, const IntSize& contentsSize)
150 {
151     // FIXME: Implement!
152 }
153
154 void ScrollingCoordinator::setWheelEventHandlerCount(unsigned wheelEventHandlerCount)
155 {
156     if (LayerChromium* layer = m_private->scrollLayer())
157         layer->setHaveWheelEventHandlers(wheelEventHandlerCount > 0);
158 }
159
160 void ScrollingCoordinator::setShouldUpdateScrollLayerPositionOnMainThread(bool should)
161 {
162     if (LayerChromium* layer = m_private->scrollLayer())
163         layer->setShouldScrollOnMainThread(should);
164 }
165
166 }