Remote Layer Tree: Force repaint
[WebKit-https.git] / Source / WebKit2 / WebProcess / WebPage / mac / RemoteLayerTreeDrawingArea.mm
1 /*
2  * Copyright (C) 2012 Apple 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 #import "config.h"
27 #import "RemoteLayerTreeDrawingArea.h"
28
29 #import "DrawingAreaProxyMessages.h"
30 #import "GraphicsLayerCARemote.h"
31 #import "RemoteLayerTreeContext.h"
32 #import "WebPage.h"
33 #import <WebCore/Frame.h>
34 #import <WebCore/FrameView.h>
35 #import <WebCore/MainFrame.h>
36 #import <WebCore/Settings.h>
37
38 using namespace WebCore;
39
40 namespace WebKit {
41
42 RemoteLayerTreeDrawingArea::RemoteLayerTreeDrawingArea(WebPage* webPage, const WebPageCreationParameters&)
43     : DrawingArea(DrawingAreaTypeRemoteLayerTree, webPage)
44     , m_remoteLayerTreeContext(std::make_unique<RemoteLayerTreeContext>(webPage))
45 {
46     webPage->corePage()->settings().setForceCompositingMode(true);
47 #if PLATFORM(IOS)
48     webPage->corePage()->settings().setDelegatesPageScaling(true);
49 #endif
50 }
51
52 RemoteLayerTreeDrawingArea::~RemoteLayerTreeDrawingArea()
53 {
54 }
55
56 void RemoteLayerTreeDrawingArea::setNeedsDisplay()
57 {
58 }
59
60 void RemoteLayerTreeDrawingArea::setNeedsDisplayInRect(const IntRect&)
61 {
62 }
63
64 void RemoteLayerTreeDrawingArea::scroll(const IntRect& scrollRect, const IntSize& scrollDelta)
65 {
66 }
67
68 GraphicsLayerFactory* RemoteLayerTreeDrawingArea::graphicsLayerFactory()
69 {
70     return m_remoteLayerTreeContext.get();
71 }
72
73 void RemoteLayerTreeDrawingArea::setRootCompositingLayer(GraphicsLayer* rootLayer)
74 {
75     m_rootLayer = rootLayer ? static_cast<GraphicsLayerCARemote*>(rootLayer)->platformCALayer() : nullptr;
76
77     m_remoteLayerTreeContext->setRootLayer(rootLayer);
78 }
79
80 void RemoteLayerTreeDrawingArea::scheduleCompositingLayerFlush()
81 {
82     m_remoteLayerTreeContext->scheduleLayerFlush();
83 }
84
85 void RemoteLayerTreeDrawingArea::updateGeometry(const IntSize& viewSize, const IntSize& layerPosition)
86 {
87     m_viewSize = viewSize;
88     m_webPage->setSize(viewSize);
89
90     for (const auto& overlayAndLayer : m_pageOverlayLayers) {
91         GraphicsLayer* layer = overlayAndLayer.value.get();
92         if (layer->drawsContent())
93             layer->setSize(viewSize);
94     }
95
96     scheduleCompositingLayerFlush();
97
98     m_webPage->send(Messages::DrawingAreaProxy::DidUpdateGeometry());
99 }
100
101 bool RemoteLayerTreeDrawingArea::shouldUseTiledBackingForFrameView(const FrameView* frameView)
102 {
103     return frameView && frameView->frame().isMainFrame();
104 }
105
106 void RemoteLayerTreeDrawingArea::updatePreferences(const WebPreferencesStore&)
107 {
108     Settings& settings = m_webPage->corePage()->settings();
109
110     // Fixed position elements need to be composited and create stacking contexts
111     // in order to be scrolled by the ScrollingCoordinator.
112     settings.setAcceleratedCompositingForFixedPositionEnabled(true);
113     settings.setFixedPositionCreatesStackingContext(true);
114
115     for (const auto& overlayAndLayer : m_pageOverlayLayers) {
116         overlayAndLayer.value->setAcceleratesDrawing(settings.acceleratedDrawingEnabled());
117         overlayAndLayer.value->setShowDebugBorder(settings.showDebugBorders());
118         overlayAndLayer.value->setShowRepaintCounter(settings.showRepaintCounter());
119     }
120 }
121
122 void RemoteLayerTreeDrawingArea::didInstallPageOverlay(PageOverlay* pageOverlay)
123 {
124     std::unique_ptr<GraphicsLayerCARemote> layer(static_cast<GraphicsLayerCARemote*>(GraphicsLayer::create(graphicsLayerFactory(), this).release()));
125 #ifndef NDEBUG
126     layer->setName("page overlay content");
127 #endif
128
129     layer->setAcceleratesDrawing(m_webPage->corePage()->settings().acceleratedDrawingEnabled());
130     layer->setShowDebugBorder(m_webPage->corePage()->settings().showDebugBorders());
131     layer->setShowRepaintCounter(m_webPage->corePage()->settings().showRepaintCounter());
132
133     m_rootLayer->appendSublayer(layer->platformCALayer());
134     m_remoteLayerTreeContext->outOfTreeLayerWasAdded(layer.get());
135
136     m_pageOverlayLayers.add(pageOverlay, std::move(layer));
137     scheduleCompositingLayerFlush();
138 }
139
140 void RemoteLayerTreeDrawingArea::didUninstallPageOverlay(PageOverlay* pageOverlay)
141 {
142     std::unique_ptr<GraphicsLayerCARemote> layer = m_pageOverlayLayers.take(pageOverlay);
143     ASSERT(layer);
144
145     m_remoteLayerTreeContext->outOfTreeLayerWillBeRemoved(layer.get());
146     layer->platformCALayer()->removeFromSuperlayer();
147
148     scheduleCompositingLayerFlush();
149 }
150
151 void RemoteLayerTreeDrawingArea::setPageOverlayNeedsDisplay(PageOverlay* pageOverlay, const IntRect& rect)
152 {
153     GraphicsLayerCARemote* layer = m_pageOverlayLayers.get(pageOverlay);
154
155     if (!layer)
156         return;
157
158     if (!layer->drawsContent()) {
159         layer->setDrawsContent(true);
160         layer->setSize(m_viewSize);
161     }
162
163     layer->setNeedsDisplayInRect(rect);
164     scheduleCompositingLayerFlush();
165 }
166
167 void RemoteLayerTreeDrawingArea::setPageOverlayOpacity(PageOverlay* pageOverlay, float opacity)
168 {
169     GraphicsLayerCARemote* layer = m_pageOverlayLayers.get(pageOverlay);
170     
171     if (!layer)
172         return;
173     
174     layer->setOpacity(opacity);
175     scheduleCompositingLayerFlush();
176 }
177
178 void RemoteLayerTreeDrawingArea::paintContents(const GraphicsLayer* graphicsLayer, GraphicsContext& graphicsContext, GraphicsLayerPaintingPhase, const IntRect& clipRect)
179 {
180     for (const auto& overlayAndLayer : m_pageOverlayLayers) {
181         if (overlayAndLayer.value.get() == graphicsLayer) {
182             m_webPage->drawPageOverlay(overlayAndLayer.key, graphicsContext, clipRect);
183             break;
184         }
185     }
186 }
187
188 float RemoteLayerTreeDrawingArea::deviceScaleFactor() const
189 {
190     return m_webPage->corePage()->deviceScaleFactor();
191 }
192
193 #if PLATFORM(IOS)
194 void RemoteLayerTreeDrawingArea::setDeviceScaleFactor(float deviceScaleFactor)
195 {
196     m_webPage->setDeviceScaleFactor(deviceScaleFactor);
197 }
198 #endif
199
200 void RemoteLayerTreeDrawingArea::setLayerTreeStateIsFrozen(bool isFrozen)
201 {
202     m_remoteLayerTreeContext->setIsFlushingSuspended(isFrozen);
203 }
204
205 void RemoteLayerTreeDrawingArea::forceRepaint()
206 {
207     m_remoteLayerTreeContext->forceRepaint();
208 }
209
210 } // namespace WebKit