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/frame/Settings.h"
37#include "platform/graphics/GraphicsLayer.h"
38#include "platform/graphics/GraphicsLayerClient.h"
39#include "public/platform/WebLayer.h"
40
41using namespace WebCore;
42
43namespace blink {
44
45namespace {
46
47WebCanvas* ToWebCanvas(GraphicsContext* gc)
48{
49    return gc->canvas();
50}
51
52} // namespace
53
54PassOwnPtr<PageOverlay> PageOverlay::create(WebViewImpl* viewImpl, WebPageOverlay* overlay)
55{
56    return adoptPtr(new PageOverlay(viewImpl, overlay));
57}
58
59PageOverlay::PageOverlay(WebViewImpl* viewImpl, WebPageOverlay* overlay)
60    : m_viewImpl(viewImpl)
61    , m_overlay(overlay)
62    , m_zOrder(0)
63{
64}
65
66class OverlayGraphicsLayerClientImpl : public WebCore::GraphicsLayerClient {
67public:
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 wallClockTime, double monotonicTime) OVERRIDE { }
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    virtual String debugName(const GraphicsLayer* graphicsLayer) OVERRIDE
85    {
86        return String("WebViewImpl Page Overlay Content Layer");
87    }
88
89private:
90    explicit OverlayGraphicsLayerClientImpl(WebPageOverlay* overlay)
91        : m_overlay(overlay)
92    {
93    }
94
95    WebPageOverlay* m_overlay;
96};
97
98void PageOverlay::clear()
99{
100    invalidateWebFrame();
101
102    if (m_layer) {
103        m_layer->removeFromParent();
104        m_layer = nullptr;
105        m_layerClient = nullptr;
106    }
107}
108
109void PageOverlay::update()
110{
111    invalidateWebFrame();
112
113    if (!m_layer) {
114        m_layerClient = OverlayGraphicsLayerClientImpl::create(m_overlay);
115        m_layer = GraphicsLayer::create(m_viewImpl->graphicsLayerFactory(), m_layerClient.get());
116        m_layer->setDrawsContent(true);
117
118        // Compositor hit-testing does not know how to deal with layers that may be
119        // transparent to events (see http://crbug.com/269598). So require
120        // scrolling and touches on this layer to go to the main thread.
121        WebLayer* platformLayer = m_layer->platformLayer();
122        platformLayer->setShouldScrollOnMainThread(true);
123        WebVector<WebRect> webRects(static_cast<size_t>(1));
124        webRects[0] = WebRect(0, 0, INT_MAX, INT_MAX);
125        platformLayer->setTouchEventHandlerRegion(webRects);
126    }
127
128    FloatSize size(m_viewImpl->size());
129    if (size != m_layer->size()) {
130        // Triggers re-adding to root layer to ensure that we are on top of
131        // scrollbars.
132        m_layer->removeFromParent();
133        m_layer->setSize(size);
134    }
135
136    m_viewImpl->setOverlayLayer(m_layer.get());
137    m_layer->setNeedsDisplay();
138}
139
140void PageOverlay::paintWebFrame(GraphicsContext& gc)
141{
142    if (!m_viewImpl->isAcceleratedCompositingActive()) {
143        gc.save();
144        m_overlay->paintPageOverlay(ToWebCanvas(&gc));
145        gc.restore();
146    }
147}
148
149void PageOverlay::invalidateWebFrame()
150{
151    // WebPageOverlay does the actual painting of the overlay.
152    // Here we just make sure to invalidate.
153    if (!m_viewImpl->isAcceleratedCompositingActive()) {
154        // FIXME: able to invalidate a smaller rect.
155        // FIXME: Is it important to just invalidate a smaller rect given that
156        // this is not on a critical codepath? In order to do so, we'd
157        // have to take scrolling into account.
158        const WebSize& size = m_viewImpl->size();
159        WebRect damagedRect(0, 0, size.width, size.height);
160        if (m_viewImpl->client())
161            m_viewImpl->client()->didInvalidateRect(damagedRect);
162    }
163}
164
165} // namespace blink
166