2 * Copyright (C) 2011 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above
12 * copyright notice, this list of conditions and the following disclaimer
13 * in the documentation and/or other materials provided with the
16 * THIS SOFTWARE IS PROVIDED BY GOOGLE INC. AND ITS CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GOOGLE INC.
20 * OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #include "web/PageOverlay.h"
32 #include "core/frame/Settings.h"
33 #include "core/page/Page.h"
34 #include "platform/graphics/GraphicsContext.h"
35 #include "platform/graphics/GraphicsLayer.h"
36 #include "platform/graphics/GraphicsLayerClient.h"
37 #include "public/platform/WebLayer.h"
38 #include "public/web/WebPageOverlay.h"
39 #include "public/web/WebViewClient.h"
40 #include "web/WebViewImpl.h"
46 WebCanvas* ToWebCanvas(GraphicsContext* gc)
53 PassOwnPtr<PageOverlay> PageOverlay::create(WebViewImpl* viewImpl, WebPageOverlay* overlay)
55 return adoptPtr(new PageOverlay(viewImpl, overlay));
58 PageOverlay::PageOverlay(WebViewImpl* viewImpl, WebPageOverlay* overlay)
59 : m_viewImpl(viewImpl)
65 class OverlayGraphicsLayerClientImpl : public GraphicsLayerClient {
67 static PassOwnPtr<OverlayGraphicsLayerClientImpl> create(WebPageOverlay* overlay)
69 return adoptPtr(new OverlayGraphicsLayerClientImpl(overlay));
72 virtual ~OverlayGraphicsLayerClientImpl() { }
74 virtual void paintContents(const GraphicsLayer*, GraphicsContext& gc, GraphicsLayerPaintingPhase, const IntRect& inClip)
77 m_overlay->paintPageOverlay(ToWebCanvas(&gc));
81 virtual String debugName(const GraphicsLayer* graphicsLayer) override
83 return String("WebViewImpl Page Overlay Content Layer");
87 explicit OverlayGraphicsLayerClientImpl(WebPageOverlay* overlay)
92 WebPageOverlay* m_overlay;
95 void PageOverlay::clear()
100 m_layer->removeFromParent();
101 if (Page* page = m_viewImpl->page())
102 page->inspectorController().didRemovePageOverlay(m_layer.get());
104 m_layerClient = nullptr;
108 void PageOverlay::update()
110 invalidateWebFrame();
113 m_layerClient = OverlayGraphicsLayerClientImpl::create(m_overlay);
114 m_layer = GraphicsLayer::create(m_viewImpl->graphicsLayerFactory(), m_layerClient.get());
115 m_layer->setDrawsContent(true);
117 if (Page* page = m_viewImpl->page())
118 page->inspectorController().willAddPageOverlay(m_layer.get());
120 // This is required for contents of overlay to stay in sync with the page while scrolling.
121 WebLayer* platformLayer = m_layer->platformLayer();
122 platformLayer->setShouldScrollOnMainThread(true);
125 FloatSize size(m_viewImpl->size());
126 if (size != m_layer->size()) {
127 // Triggers re-adding to root layer to ensure that we are on top of
129 m_layer->removeFromParent();
130 m_layer->setSize(size);
133 m_viewImpl->setOverlayLayer(m_layer.get());
134 m_layer->setNeedsDisplay();
137 void PageOverlay::paintWebFrame(GraphicsContext& gc)
139 if (!m_viewImpl->isAcceleratedCompositingActive()) {
141 m_overlay->paintPageOverlay(ToWebCanvas(&gc));
146 void PageOverlay::invalidateWebFrame()
148 // WebPageOverlay does the actual painting of the overlay.
149 // Here we just make sure to invalidate.
150 if (!m_viewImpl->isAcceleratedCompositingActive()) {
151 // FIXME: able to invalidate a smaller rect.
152 // FIXME: Is it important to just invalidate a smaller rect given that
153 // this is not on a critical codepath? In order to do so, we'd
154 // have to take scrolling into account.
155 const WebSize& size = m_viewImpl->size();
156 WebRect damagedRect(0, 0, size.width, size.height);
157 if (m_viewImpl->client())
158 m_viewImpl->client()->didInvalidateRect(damagedRect);