Home | History | Annotate | Download | only in web
      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 "web/PageOverlay.h"
     31 
     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"
     41 
     42 using namespace WebCore;
     43 
     44 namespace blink {
     45 
     46 namespace {
     47 
     48 WebCanvas* ToWebCanvas(GraphicsContext* gc)
     49 {
     50     return gc->canvas();
     51 }
     52 
     53 } // namespace
     54 
     55 PassOwnPtr<PageOverlay> PageOverlay::create(WebViewImpl* viewImpl, WebPageOverlay* overlay)
     56 {
     57     return adoptPtr(new PageOverlay(viewImpl, overlay));
     58 }
     59 
     60 PageOverlay::PageOverlay(WebViewImpl* viewImpl, WebPageOverlay* overlay)
     61     : m_viewImpl(viewImpl)
     62     , m_overlay(overlay)
     63     , m_zOrder(0)
     64 {
     65 }
     66 
     67 class OverlayGraphicsLayerClientImpl : public WebCore::GraphicsLayerClient {
     68 public:
     69     static PassOwnPtr<OverlayGraphicsLayerClientImpl> create(WebPageOverlay* overlay)
     70     {
     71         return adoptPtr(new OverlayGraphicsLayerClientImpl(overlay));
     72     }
     73 
     74     virtual ~OverlayGraphicsLayerClientImpl() { }
     75 
     76     virtual void notifyAnimationStarted(const GraphicsLayer*, double monotonicTime) OVERRIDE { }
     77 
     78     virtual void paintContents(const GraphicsLayer*, GraphicsContext& gc, GraphicsLayerPaintingPhase, const IntRect& inClip)
     79     {
     80         if (gc.paintingDisabled())
     81             return;
     82         gc.save();
     83         m_overlay->paintPageOverlay(ToWebCanvas(&gc));
     84         gc.restore();
     85     }
     86 
     87     virtual String debugName(const GraphicsLayer* graphicsLayer) OVERRIDE
     88     {
     89         return String("WebViewImpl Page Overlay Content Layer");
     90     }
     91 
     92 private:
     93     explicit OverlayGraphicsLayerClientImpl(WebPageOverlay* overlay)
     94         : m_overlay(overlay)
     95     {
     96     }
     97 
     98     WebPageOverlay* m_overlay;
     99 };
    100 
    101 void PageOverlay::clear()
    102 {
    103     invalidateWebFrame();
    104 
    105     if (m_layer) {
    106         m_layer->removeFromParent();
    107         if (WebCore::Page* page = m_viewImpl->page())
    108             page->inspectorController().didRemovePageOverlay(m_layer.get());
    109         m_layer = nullptr;
    110         m_layerClient = nullptr;
    111     }
    112 }
    113 
    114 void PageOverlay::update()
    115 {
    116     invalidateWebFrame();
    117 
    118     if (!m_layer) {
    119         m_layerClient = OverlayGraphicsLayerClientImpl::create(m_overlay);
    120         m_layer = GraphicsLayer::create(m_viewImpl->graphicsLayerFactory(), m_layerClient.get());
    121         m_layer->setDrawsContent(true);
    122 
    123         if (WebCore::Page* page = m_viewImpl->page())
    124             page->inspectorController().willAddPageOverlay(m_layer.get());
    125 
    126         // This is required for contents of overlay to stay in sync with the page while scrolling.
    127         WebLayer* platformLayer = m_layer->platformLayer();
    128         platformLayer->setShouldScrollOnMainThread(true);
    129     }
    130 
    131     FloatSize size(m_viewImpl->size());
    132     if (size != m_layer->size()) {
    133         // Triggers re-adding to root layer to ensure that we are on top of
    134         // scrollbars.
    135         m_layer->removeFromParent();
    136         m_layer->setSize(size);
    137     }
    138 
    139     m_viewImpl->setOverlayLayer(m_layer.get());
    140     m_layer->setNeedsDisplay();
    141 }
    142 
    143 void PageOverlay::paintWebFrame(GraphicsContext& gc)
    144 {
    145     if (!m_viewImpl->isAcceleratedCompositingActive() && !gc.paintingDisabled()) {
    146         gc.save();
    147         m_overlay->paintPageOverlay(ToWebCanvas(&gc));
    148         gc.restore();
    149     }
    150 }
    151 
    152 void PageOverlay::invalidateWebFrame()
    153 {
    154     // WebPageOverlay does the actual painting of the overlay.
    155     // Here we just make sure to invalidate.
    156     if (!m_viewImpl->isAcceleratedCompositingActive()) {
    157         // FIXME: able to invalidate a smaller rect.
    158         // FIXME: Is it important to just invalidate a smaller rect given that
    159         // this is not on a critical codepath? In order to do so, we'd
    160         // have to take scrolling into account.
    161         const WebSize& size = m_viewImpl->size();
    162         WebRect damagedRect(0, 0, size.width, size.height);
    163         if (m_viewImpl->client())
    164             m_viewImpl->client()->didInvalidateRect(damagedRect);
    165     }
    166 }
    167 
    168 } // namespace blink
    169