Unreviewed build fix for Qt.
[WebKit-https.git] / Source / WebKit2 / WebProcess / WebPage / qt / LayerTreeHostQt.cpp
1 /*
2  * Copyright (C) 2011 Apple Inc. All rights reserved.
3  * Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
15  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
16  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
18  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
24  * THE POSSIBILITY OF SUCH DAMAGE.
25  */
26
27 #include "config.h"
28
29 #if USE(ACCELERATED_COMPOSITING)
30
31 #include "LayerTreeHostQt.h"
32
33 #include "DrawingAreaImpl.h"
34 #include "GraphicsContext.h"
35 #include "LayerTreeHostProxyMessages.h"
36 #include "MessageID.h"
37 #include "WebGraphicsLayer.h"
38 #include "WebPage.h"
39 #include <WebCore/Frame.h>
40 #include <WebCore/FrameView.h>
41 #include <WebCore/Page.h>
42 #include <WebCore/Settings.h>
43
44 using namespace WebCore;
45
46 namespace WebKit {
47
48 PassRefPtr<LayerTreeHostQt> LayerTreeHostQt::create(WebPage* webPage)
49 {
50     return adoptRef(new LayerTreeHostQt(webPage));
51 }
52
53 LayerTreeHostQt::~LayerTreeHostQt()
54 {
55     if (m_rootLayer)
56         toWebGraphicsLayer(m_rootLayer.get())->setLayerTreeTileClient(0);
57 }
58
59 LayerTreeHostQt::LayerTreeHostQt(WebPage* webPage)
60     : LayerTreeHost(webPage)
61     , m_notifyAfterScheduledLayerFlush(false)
62     , m_isValid(true)
63 #if USE(TILED_BACKING_STORE)
64     , m_waitingForUIProcess(false)
65     , m_isSuspended(false)
66 #endif
67     , m_shouldSyncFrame(false)
68     , m_shouldSyncRootLayer(true)
69     , m_layerFlushTimer(this, &LayerTreeHostQt::layerFlushTimerFired)
70     , m_layerFlushSchedulingEnabled(true)
71     , m_shouldRecreateBackingStore(false)
72 {
73     // Create a root layer.
74     m_rootLayer = GraphicsLayer::create(this);
75     WebGraphicsLayer* webRootLayer = toWebGraphicsLayer(m_rootLayer.get());
76     webRootLayer->setRootLayer(true);
77 #ifndef NDEBUG
78     m_rootLayer->setName("LayerTreeHostQt root layer");
79 #endif
80     m_rootLayer->setDrawsContent(false);
81     m_rootLayer->setSize(m_webPage->size());
82     m_layerTreeContext.webLayerID = toWebGraphicsLayer(webRootLayer)->id();
83
84     m_nonCompositedContentLayer = GraphicsLayer::create(this);
85 #if USE(TILED_BACKING_STORE)
86     toWebGraphicsLayer(m_rootLayer.get())->setLayerTreeTileClient(this);
87 #endif
88 #ifndef NDEBUG
89     m_nonCompositedContentLayer->setName("LayerTreeHostQt non-composited content");
90 #endif
91     m_nonCompositedContentLayer->setDrawsContent(true);
92     m_nonCompositedContentLayer->setContentsOpaque(m_webPage->drawsBackground() && !m_webPage->drawsTransparentBackground());
93     m_nonCompositedContentLayer->setSize(m_webPage->size());
94
95     m_rootLayer->addChild(m_nonCompositedContentLayer.get());
96
97     if (m_webPage->hasPageOverlay())
98         createPageOverlayLayer();
99
100     scheduleLayerFlush();
101 }
102
103 void LayerTreeHostQt::setLayerFlushSchedulingEnabled(bool layerFlushingEnabled)
104 {
105     if (m_layerFlushSchedulingEnabled == layerFlushingEnabled)
106         return;
107
108     m_layerFlushSchedulingEnabled = layerFlushingEnabled;
109
110     if (m_layerFlushSchedulingEnabled) {
111         scheduleLayerFlush();
112         return;
113     }
114
115     cancelPendingLayerFlush();
116 }
117
118 void LayerTreeHostQt::scheduleLayerFlush()
119 {
120     if (!m_layerFlushSchedulingEnabled)
121         return;
122
123     if (!m_layerFlushTimer.isActive())
124         m_layerFlushTimer.startOneShot(0);
125 }
126
127 void LayerTreeHostQt::cancelPendingLayerFlush()
128 {
129     m_layerFlushTimer.stop();
130 }
131
132 void LayerTreeHostQt::setShouldNotifyAfterNextScheduledLayerFlush(bool notifyAfterScheduledLayerFlush)
133 {
134     m_notifyAfterScheduledLayerFlush = notifyAfterScheduledLayerFlush;
135 }
136
137 void LayerTreeHostQt::setRootCompositingLayer(WebCore::GraphicsLayer* graphicsLayer)
138 {
139     m_nonCompositedContentLayer->removeAllChildren();
140
141     // Add the accelerated layer tree hierarchy.
142     if (graphicsLayer)
143         m_nonCompositedContentLayer->addChild(graphicsLayer);
144 }
145
146 void LayerTreeHostQt::invalidate()
147 {
148     cancelPendingLayerFlush();
149
150     ASSERT(m_isValid);
151     m_rootLayer = nullptr;
152     m_isValid = false;
153 }
154
155 void LayerTreeHostQt::setNonCompositedContentsNeedDisplay(const WebCore::IntRect& rect)
156 {
157     m_nonCompositedContentLayer->setNeedsDisplayInRect(rect);
158     if (m_pageOverlayLayer)
159         m_pageOverlayLayer->setNeedsDisplayInRect(rect);
160
161     scheduleLayerFlush();
162 }
163
164 void LayerTreeHostQt::scrollNonCompositedContents(const WebCore::IntRect& scrollRect, const WebCore::IntSize& scrollOffset)
165 {
166     setNonCompositedContentsNeedDisplay(scrollRect);
167 }
168
169 void LayerTreeHostQt::forceRepaint()
170 {
171     scheduleLayerFlush();
172 }
173
174 void LayerTreeHostQt::sizeDidChange(const WebCore::IntSize& newSize)
175 {
176     m_rootLayer->setSize(newSize);
177
178     // If the newSize exposes new areas of the non-composited content a setNeedsDisplay is needed
179     // for those newly exposed areas.
180     FloatSize oldSize = m_nonCompositedContentLayer->size();
181     m_nonCompositedContentLayer->setSize(newSize);
182
183     if (newSize.width() > oldSize.width()) {
184         float height = std::min(static_cast<float>(newSize.height()), oldSize.height());
185         m_nonCompositedContentLayer->setNeedsDisplayInRect(FloatRect(oldSize.width(), 0, newSize.width() - oldSize.width(), height));
186     }
187
188     if (newSize.height() > oldSize.height())
189         m_nonCompositedContentLayer->setNeedsDisplayInRect(FloatRect(0, oldSize.height(), newSize.width(), newSize.height() - oldSize.height()));
190
191     if (m_pageOverlayLayer)
192         m_pageOverlayLayer->setSize(newSize);
193
194     scheduleLayerFlush();
195 }
196
197 void LayerTreeHostQt::didInstallPageOverlay()
198 {
199     createPageOverlayLayer();
200     scheduleLayerFlush();
201 }
202
203 void LayerTreeHostQt::didUninstallPageOverlay()
204 {
205     destroyPageOverlayLayer();
206     scheduleLayerFlush();
207 }
208
209 void LayerTreeHostQt::setPageOverlayNeedsDisplay(const WebCore::IntRect& rect)
210 {
211     ASSERT(m_pageOverlayLayer);
212     m_pageOverlayLayer->setNeedsDisplayInRect(rect);
213     scheduleLayerFlush();
214 }
215
216 bool LayerTreeHostQt::flushPendingLayerChanges()
217 {
218     recreateBackingStoreIfNeeded();
219
220     bool didSync = m_webPage->corePage()->mainFrame()->view()->syncCompositingStateIncludingSubframes();
221     m_nonCompositedContentLayer->syncCompositingStateForThisLayerOnly();
222     if (m_pageOverlayLayer)
223         m_pageOverlayLayer->syncCompositingStateForThisLayerOnly();
224
225     m_rootLayer->syncCompositingStateForThisLayerOnly();
226     return didSync;
227 }
228
229 void LayerTreeHostQt::didSyncCompositingStateForLayer(const WebLayerInfo& info)
230 {
231     m_shouldSyncFrame = true;
232     m_webPage->send(Messages::LayerTreeHostProxy::SyncCompositingLayerState(info));
233 }
234
235 void LayerTreeHostQt::didDeleteLayer(WebLayerID id)
236 {
237     m_shouldSyncFrame = true;
238     m_webPage->send(Messages::LayerTreeHostProxy::DeleteCompositingLayer(id));
239 }
240
241 void LayerTreeHostQt::performScheduledLayerFlush()
242 {
243 #if USE(TILED_BACKING_STORE)
244     if (m_isSuspended || m_waitingForUIProcess)
245         return;
246 #endif
247
248     m_webPage->layoutIfNeeded();
249
250     if (!m_isValid)
251         return;
252
253     m_shouldSyncFrame = false;
254     flushPendingLayerChanges();
255     if (!m_shouldSyncFrame)
256         return;
257
258     if (m_shouldSyncRootLayer) {
259         m_webPage->send(Messages::LayerTreeHostProxy::SetRootCompositingLayer(toWebGraphicsLayer(m_rootLayer.get())->id()));
260         m_shouldSyncRootLayer = false;
261     }
262
263     m_webPage->send(Messages::LayerTreeHostProxy::DidRenderFrame());
264     m_waitingForUIProcess = true;
265
266     if (!m_notifyAfterScheduledLayerFlush)
267         return;
268
269     // Let the drawing area know that we've done a flush of the layer changes.
270     static_cast<DrawingAreaImpl*>(m_webPage->drawingArea())->layerHostDidFlushLayers();
271     m_notifyAfterScheduledLayerFlush = false;
272 }
273
274 void LayerTreeHostQt::layerFlushTimerFired(Timer<LayerTreeHostQt>*)
275 {
276     performScheduledLayerFlush();
277 }
278
279 void LayerTreeHostQt::createPageOverlayLayer()
280 {
281     ASSERT(!m_pageOverlayLayer);
282
283     m_pageOverlayLayer = GraphicsLayer::create(this);
284 #ifndef NDEBUG
285     m_pageOverlayLayer->setName("LayerTreeHostQt page overlay content");
286 #endif
287
288     m_pageOverlayLayer->setDrawsContent(true);
289     m_pageOverlayLayer->setSize(m_webPage->size());
290
291     m_rootLayer->addChild(m_pageOverlayLayer.get());
292 }
293
294 void LayerTreeHostQt::destroyPageOverlayLayer()
295 {
296     ASSERT(m_pageOverlayLayer);
297     m_pageOverlayLayer->removeFromParent();
298     m_pageOverlayLayer = nullptr;
299 }
300
301 int64_t LayerTreeHostQt::adoptImageBackingStore(Image* image)
302 {
303     if (!image)
304         return InvalidWebLayerID;
305     QPixmap* pixmap = image->nativeImageForCurrentFrame();
306
307     if (!pixmap)
308         return InvalidWebLayerID;
309
310     int64_t key = pixmap->cacheKey();
311     HashMap<int64_t, int>::iterator it = m_directlyCompositedImageRefCounts.find(key);
312
313     if (it != m_directlyCompositedImageRefCounts.end()) {
314         ++(it->second);
315         return key;
316     }
317
318     RefPtr<ShareableBitmap> bitmap = ShareableBitmap::createShareable(image->size(), image->currentFrameHasAlpha() ? ShareableBitmap::SupportsAlpha : 0);
319     {
320         OwnPtr<WebCore::GraphicsContext> graphicsContext = bitmap->createGraphicsContext();
321         graphicsContext->drawImage(image, ColorSpaceDeviceRGB, IntPoint::zero());
322     }
323
324     // Qt uses BGRA internally, we swizzle to RGBA for OpenGL.
325     bitmap->swizzleRGB();
326     ShareableBitmap::Handle handle;
327     bitmap->createHandle(handle);
328     m_webPage->send(Messages::LayerTreeHostProxy::CreateDirectlyCompositedImage(key, handle));
329     m_directlyCompositedImageRefCounts.add(key, 1);
330     return key;
331 }
332
333 void LayerTreeHostQt::releaseImageBackingStore(int64_t key)
334 {
335     if (!key)
336         return;
337     HashMap<int64_t, int>::iterator it = m_directlyCompositedImageRefCounts.find(key);
338     if (it == m_directlyCompositedImageRefCounts.end())
339         return;
340
341     it->second--;
342
343     if (it->second)
344         return;
345
346     m_directlyCompositedImageRefCounts.remove(it);
347     m_webPage->send(Messages::LayerTreeHostProxy::DestroyDirectlyCompositedImage(key));
348 }
349
350
351 void LayerTreeHostQt::notifyAnimationStarted(const WebCore::GraphicsLayer*, double time)
352 {
353 }
354
355 void LayerTreeHostQt::notifySyncRequired(const WebCore::GraphicsLayer*)
356 {
357 }
358
359 void LayerTreeHostQt::paintContents(const WebCore::GraphicsLayer* graphicsLayer, WebCore::GraphicsContext& graphicsContext, WebCore::GraphicsLayerPaintingPhase, const WebCore::IntRect& clipRect)
360 {
361     if (graphicsLayer == m_nonCompositedContentLayer) {
362         m_webPage->drawRect(graphicsContext, clipRect);
363         return;
364     }
365
366     if (graphicsLayer == m_pageOverlayLayer) {
367         // Overlays contain transparent contents and won't clear the context as part of their rendering, so we do it here.
368         graphicsContext.clearRect(clipRect);
369         m_webPage->drawPageOverlay(graphicsContext, clipRect);
370         return;
371     }
372 }
373
374 bool LayerTreeHostQt::showDebugBorders(const WebCore::GraphicsLayer*) const
375 {
376     return m_webPage->corePage()->settings()->showDebugBorders();
377 }
378
379 bool LayerTreeHostQt::showRepaintCounter(const WebCore::GraphicsLayer*) const
380 {
381     return m_webPage->corePage()->settings()->showRepaintCounter();
382 }
383
384 bool LayerTreeHost::supportsAcceleratedCompositing()
385 {
386     return true;
387 }
388
389 #if USE(TILED_BACKING_STORE)
390 void LayerTreeHostQt::createTile(WebLayerID layerID, int tileID, const UpdateInfo& updateInfo)
391 {
392     m_webPage->send(Messages::LayerTreeHostProxy::CreateTileForLayer(layerID, tileID, updateInfo));
393 }
394
395 void LayerTreeHostQt::updateTile(WebLayerID layerID, int tileID, const UpdateInfo& updateInfo)
396 {
397     m_webPage->send(Messages::LayerTreeHostProxy::UpdateTileForLayer(layerID, tileID, updateInfo));
398 }
399 void LayerTreeHostQt::removeTile(WebLayerID layerID, int tileID)
400 {
401     m_webPage->send(Messages::LayerTreeHostProxy::RemoveTileForLayer(layerID, tileID));
402 }
403
404 void LayerTreeHostQt::setVisibleContentRectAndScale(const IntRect& rect, float scale)
405 {
406     if (m_rootLayer)
407         toWebGraphicsLayer(m_rootLayer.get())->setVisibleContentRectAndScale(rect, scale);
408 }
409
410 void LayerTreeHostQt::setVisibleContentRectTrajectoryVector(const FloatPoint& trajectoryVector)
411 {
412     toWebGraphicsLayer(m_nonCompositedContentLayer.get())->setVisibleContentRectTrajectoryVector(trajectoryVector);
413 }
414
415 void LayerTreeHostQt::renderNextFrame()
416 {
417     m_waitingForUIProcess = false;
418     scheduleLayerFlush();
419 }
420
421 bool LayerTreeHostQt::layerTreeTileUpdatesAllowed() const
422 {
423     return !m_isSuspended && !m_waitingForUIProcess;
424 }
425
426 void LayerTreeHostQt::purgeBackingStores()
427 {
428     m_shouldRecreateBackingStore = true;
429     WebGraphicsLayer* webRootLayer = toWebGraphicsLayer(m_rootLayer.get());
430     webRootLayer->purgeBackingStores();
431
432     ASSERT(!m_directlyCompositedImageRefCounts.size());
433 }
434
435 void LayerTreeHostQt::recreateBackingStoreIfNeeded()
436 {
437     if (!m_shouldRecreateBackingStore)
438         return;
439
440     m_shouldRecreateBackingStore = false;
441     WebGraphicsLayer* webRootLayer = toWebGraphicsLayer(m_rootLayer.get());
442     webRootLayer->recreateBackingStoreIfNeeded();
443 }
444 #endif
445
446 } // namespace WebKit
447 #else
448 #include "LayerTreeHost.h"
449
450 using namespace WebCore;
451
452 namespace WebKit {
453
454 bool LayerTreeHost::supportsAcceleratedCompositing()
455 {
456     return false;
457 }
458
459 } // namespace WebKit
460 #endif