initial import
[vuplus_webkit] / Source / WebKit / chromium / src / PageOverlay.cpp
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 "GraphicsLayer.h"
33 #include "GraphicsLayerClient.h"
34 #include "Page.h"
35 #include "Settings.h"
36 #include "WebViewClient.h"
37 #include "WebViewImpl.h"
38
39 using namespace WebCore;
40
41 namespace WebKit {
42
43 PassOwnPtr<PageOverlay> PageOverlay::create(WebViewImpl* viewImpl, PageOverlayClient* client)
44 {
45     return adoptPtr(new PageOverlay(viewImpl, client));
46 }
47
48 PageOverlay::PageOverlay(WebViewImpl* viewImpl, PageOverlayClient* client)
49     : m_viewImpl(viewImpl)
50     , m_client(client)
51 {
52 }
53
54 #if USE(ACCELERATED_COMPOSITING)
55 class OverlayGraphicsLayerClientImpl : public WebCore::GraphicsLayerClient {
56 public:
57     static PassOwnPtr<OverlayGraphicsLayerClientImpl*> create(WebViewImpl* webViewImpl, PageOverlay::PageOverlayClient* pageOverlayClient)
58     {
59         return adoptPtr(new OverlayGraphicsLayerClientImpl(webViewImpl, pageOverlayClient));
60     }
61
62     virtual ~OverlayGraphicsLayerClientImpl() { }
63
64     virtual void notifyAnimationStarted(const GraphicsLayer*, double time) { }
65
66     virtual void notifySyncRequired(const GraphicsLayer*) { }
67
68     virtual void paintContents(const GraphicsLayer*, GraphicsContext& context, GraphicsLayerPaintingPhase, const IntRect& inClip)
69     {
70         m_pageOverlayClient->paintPageOverlay(context);
71     }
72
73     virtual bool showDebugBorders() const
74     {
75         return m_webViewImpl->page()->settings()->showDebugBorders();
76     }
77
78     virtual bool showRepaintCounter() const
79     {
80         return m_webViewImpl->page()->settings()->showRepaintCounter();
81     }
82
83 private:
84     explicit OverlayGraphicsLayerClientImpl(WebViewImpl* webViewImpl, PageOverlay::PageOverlayClient* pageOverlayClient)
85         : m_pageOverlayClient(pageOverlayClient)
86         , m_webViewImpl(webViewImpl)
87     {
88     }
89
90     PageOverlay::PageOverlayClient* m_pageOverlayClient;
91     WebViewImpl* m_webViewImpl;
92 };
93 #endif
94
95 void PageOverlay::clear()
96 {
97     invalidateWebFrame();
98
99 #if USE(ACCELERATED_COMPOSITING)
100     if (m_layer) {
101         m_layer->removeFromParent();
102         m_layer = nullptr;
103         m_layerClient = nullptr;
104     }
105 #endif
106 }
107
108 void PageOverlay::update()
109 {
110     invalidateWebFrame();
111
112 #if USE(ACCELERATED_COMPOSITING)
113     if (!m_layer) {
114         m_layerClient = OverlayGraphicsLayerClientImpl::create(m_viewImpl, m_client);
115         m_layer = GraphicsLayer::create(m_layerClient.get());
116         m_layer->setName("WebViewImpl page overlay content");
117         m_layer->setDrawsContent(true);
118         const WebSize& size = m_viewImpl->size();
119         m_layer->setSize(IntSize(size.width, size.height));
120     }
121     m_viewImpl->setOverlayLayer(m_layer.get());
122     m_layer->setNeedsDisplay();
123 #endif
124 }
125
126 void PageOverlay::paintWebFrame(GraphicsContext& gc)
127 {
128     if (!m_viewImpl->isAcceleratedCompositingActive())
129         m_client->paintPageOverlay(gc);
130 }
131
132 void PageOverlay::invalidateWebFrame()
133 {
134     // PageOverlayClient does the actual painting of the overlay.
135     // Here we just make sure to invalidate.
136     if (!m_viewImpl->isAcceleratedCompositingActive()) {
137         // FIXME: able to invalidate a smaller rect.
138         // FIXME: Is it important to just invalidate a smaller rect given that
139         // this is not on a critical codepath? In order to do so, we'd
140         // have to take scrolling into account.
141         const WebSize& size = m_viewImpl->size();
142         WebRect damagedRect(0, 0, size.width, size.height);
143         if (m_viewImpl->client())
144             m_viewImpl->client()->didInvalidateRect(damagedRect);
145     }
146 }
147
148 } // namespace WebKit