1 /* 2 * Copyright (C) 2011 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 are 6 * met: 7 * 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 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 14 * distribution. 15 * 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. 27 */ 28 29 #include "config.h" 30 #include "PageOverlay.h" 31 32 #include "WebPageOverlay.h" 33 #include "WebViewClient.h" 34 #include "WebViewImpl.h" 35 #include "core/page/Page.h" 36 #include "core/page/Settings.h" 37 #include "core/platform/graphics/GraphicsLayer.h" 38 #include "core/platform/graphics/GraphicsLayerClient.h" 39 #include "public/platform/WebLayer.h" 40 41 using namespace WebCore; 42 43 namespace WebKit { 44 45 namespace { 46 47 WebCanvas* ToWebCanvas(GraphicsContext* gc) 48 { 49 return gc->canvas(); 50 } 51 52 } // namespace 53 54 PassOwnPtr<PageOverlay> PageOverlay::create(WebViewImpl* viewImpl, WebPageOverlay* overlay) 55 { 56 return adoptPtr(new PageOverlay(viewImpl, overlay)); 57 } 58 59 PageOverlay::PageOverlay(WebViewImpl* viewImpl, WebPageOverlay* overlay) 60 : m_viewImpl(viewImpl) 61 , m_overlay(overlay) 62 , m_zOrder(0) 63 { 64 } 65 66 class OverlayGraphicsLayerClientImpl : public WebCore::GraphicsLayerClient { 67 public: 68 static PassOwnPtr<OverlayGraphicsLayerClientImpl*> create(WebPageOverlay* overlay) 69 { 70 return adoptPtr(new OverlayGraphicsLayerClientImpl(overlay)); 71 } 72 73 virtual ~OverlayGraphicsLayerClientImpl() { } 74 75 virtual void notifyAnimationStarted(const GraphicsLayer*, double time) { } 76 77 virtual void paintContents(const GraphicsLayer*, GraphicsContext& gc, GraphicsLayerPaintingPhase, const IntRect& inClip) 78 { 79 gc.save(); 80 m_overlay->paintPageOverlay(ToWebCanvas(&gc)); 81 gc.restore(); 82 } 83 84 private: 85 explicit OverlayGraphicsLayerClientImpl(WebPageOverlay* overlay) 86 : m_overlay(overlay) 87 { 88 } 89 90 WebPageOverlay* m_overlay; 91 }; 92 93 void PageOverlay::clear() 94 { 95 invalidateWebFrame(); 96 97 if (m_layer) { 98 m_layer->removeFromParent(); 99 m_layer = nullptr; 100 m_layerClient = nullptr; 101 } 102 } 103 104 void PageOverlay::update() 105 { 106 invalidateWebFrame(); 107 108 if (!m_layer) { 109 m_layerClient = OverlayGraphicsLayerClientImpl::create(m_overlay); 110 m_layer = GraphicsLayer::create(m_viewImpl->graphicsLayerFactory(), m_layerClient.get()); 111 m_layer->setName("WebViewImpl page overlay content"); 112 m_layer->setDrawsContent(true); 113 } 114 115 FloatSize size(m_viewImpl->size()); 116 if (size != m_layer->size()) { 117 // Triggers re-adding to root layer to ensure that we are on top of 118 // scrollbars. 119 m_layer->removeFromParent(); 120 m_layer->setSize(size); 121 } 122 123 m_viewImpl->setOverlayLayer(m_layer.get()); 124 m_layer->setNeedsDisplay(); 125 126 WebLayer* platformLayer = m_layer->platformLayer(); 127 platformLayer->setShouldScrollOnMainThread(true); 128 } 129 130 void PageOverlay::paintWebFrame(GraphicsContext& gc) 131 { 132 if (!m_viewImpl->isAcceleratedCompositingActive()) { 133 gc.save(); 134 m_overlay->paintPageOverlay(ToWebCanvas(&gc)); 135 gc.restore(); 136 } 137 } 138 139 void PageOverlay::invalidateWebFrame() 140 { 141 // WebPageOverlay does the actual painting of the overlay. 142 // Here we just make sure to invalidate. 143 if (!m_viewImpl->isAcceleratedCompositingActive()) { 144 // FIXME: able to invalidate a smaller rect. 145 // FIXME: Is it important to just invalidate a smaller rect given that 146 // this is not on a critical codepath? In order to do so, we'd 147 // have to take scrolling into account. 148 const WebSize& size = m_viewImpl->size(); 149 WebRect damagedRect(0, 0, size.width, size.height); 150 if (m_viewImpl->client()) 151 m_viewImpl->client()->didInvalidateRect(damagedRect); 152 } 153 } 154 155 } // namespace WebKit 156