2010-07-01 Simon Fraser <simon.fraser@apple.com>
[WebKit-https.git] / WebKit2 / WebProcess / WebPage / LayerBackedDrawingArea.cpp
1 /*
2  * Copyright (C) 2010 Apple 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
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23  * THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 #if USE(ACCELERATED_COMPOSITING)
27
28 #include "LayerBackedDrawingArea.h"
29
30 #include "DrawingAreaMessageKinds.h"
31 #include "DrawingAreaProxyMessageKinds.h"
32 #include "MessageID.h"
33 #include "WebCoreArgumentCoders.h"
34 #include "WebPage.h"
35 #include "WebProcess.h"
36 #include <WebCore/GraphicsLayer.h>
37
38 using namespace WebCore;
39
40 namespace WebKit {
41
42 LayerBackedDrawingArea::LayerBackedDrawingArea(WebPage* webPage)
43     : DrawingArea(LayerBackedDrawingAreaType, webPage)
44     , m_syncTimer(WebProcess::shared().runLoop(), this, &LayerBackedDrawingArea::syncCompositingLayers)
45 #if PLATFORM(MAC)
46     , m_remoteLayerRef(0)
47 #endif
48     , m_attached(false)
49     , m_shouldPaint(true)
50 {
51     m_backingLayer = GraphicsLayer::create(this);
52     m_backingLayer->setDrawsContent(true);
53 #ifndef NDEBUG
54     m_backingLayer->setName("DrawingArea backing layer");
55 #endif
56     m_backingLayer->syncCompositingStateForThisLayerOnly();
57     
58     platformInit();
59 }
60
61 LayerBackedDrawingArea::~LayerBackedDrawingArea()
62 {
63     platformClear();
64 }
65
66 void LayerBackedDrawingArea::invalidateWindow(const IntRect& rect, bool immediate)
67 {
68
69 }
70
71 void LayerBackedDrawingArea::invalidateContentsAndWindow(const IntRect& rect, bool immediate)
72 {
73     setNeedsDisplay(rect);
74 }
75
76 void LayerBackedDrawingArea::invalidateContentsForSlowScroll(const IntRect& rect, bool immediate)
77 {
78     setNeedsDisplay(rect);
79 }
80
81 void LayerBackedDrawingArea::scroll(const IntSize& scrollDelta, const IntRect& rectToScroll, const IntRect& clipRect)
82 {
83     // FIXME: Do something much smarter.
84     setNeedsDisplay(rectToScroll);
85 }
86
87 void LayerBackedDrawingArea::setNeedsDisplay(const IntRect& rect)
88 {
89     if (!m_remoteLayerRef)
90         attachCompositingContext(0);
91
92     m_backingLayer->setNeedsDisplayInRect(rect);
93     m_backingLayer->syncCompositingStateForThisLayerOnly();
94
95 #if PLATFORM(MAC)
96     scheduleUpdateLayoutRunLoopObserver();
97 #endif
98 }
99
100 void LayerBackedDrawingArea::display()
101 {
102     // Layout if necessary.
103     m_webPage->layoutIfNeeded();
104 }
105
106 void LayerBackedDrawingArea::scheduleDisplay()
107 {
108 }
109
110 void LayerBackedDrawingArea::setSize(const IntSize& viewSize)
111 {
112     ASSERT(m_shouldPaint);
113     ASSERT_ARG(viewSize, !viewSize.isEmpty());
114
115     m_backingLayer->setSize(viewSize);
116     m_backingLayer->syncCompositingStateForThisLayerOnly();
117     
118     m_webPage->setSize(viewSize);
119
120     // Layout if necessary.
121     m_webPage->layoutIfNeeded();
122
123     WebProcess::shared().connection()->send(DrawingAreaProxyMessage::DidSetSize, m_webPage->pageID(), CoreIPC::In(0));
124 }
125
126 void LayerBackedDrawingArea::suspendPainting()
127 {
128     ASSERT(m_shouldPaint);
129     
130     m_shouldPaint = false;
131 }
132
133 void LayerBackedDrawingArea::resumePainting()
134 {
135     ASSERT(!m_shouldPaint);
136     
137     m_shouldPaint = true;
138     
139     // Display if needed.
140     display();
141 }
142
143 void LayerBackedDrawingArea::didUpdate()
144 {
145
146     // Display if needed.
147     display();
148 }
149
150 void LayerBackedDrawingArea::didReceiveMessage(CoreIPC::Connection*, CoreIPC::MessageID messageID, CoreIPC::ArgumentDecoder& arguments)
151 {
152     switch (messageID.get<DrawingAreaMessage::Kind>()) {
153         case DrawingAreaMessage::SetSize: {
154             IntSize size;
155             if (!arguments.decode(CoreIPC::Out(size)))
156                 return;
157
158             setSize(size);
159             break;
160         }
161         
162         case DrawingAreaMessage::SuspendPainting:
163             suspendPainting();
164             break;
165
166         case DrawingAreaMessage::ResumePainting:
167             resumePainting();
168             break;
169
170         case DrawingAreaMessage::DidUpdate:
171             didUpdate();
172             break;
173
174         default:
175             ASSERT_NOT_REACHED();
176             break;
177     }
178 }
179
180 // GraphicsLayerClient methods
181 void LayerBackedDrawingArea::paintContents(const GraphicsLayer*, GraphicsContext& graphicsContext, GraphicsLayerPaintingPhase, const IntRect& inClip)
182 {
183     m_webPage->drawRect(graphicsContext, inClip);
184 }
185
186 bool LayerBackedDrawingArea::showDebugBorders() const
187 {
188     // FIXME: get from settings;
189     return false;
190 }
191
192 bool LayerBackedDrawingArea::showRepaintCounter() const
193 {
194     // FIXME: get from settings;
195     return false;
196 }
197
198 #if !PLATFORM(MAC)
199 void LayerBackedDrawingArea::attachCompositingContext(GraphicsLayer*)
200 {
201 }
202
203 void LayerBackedDrawingArea::detachCompositingContext()
204 {
205 }
206
207 void LayerBackedDrawingArea::scheduleCompositingLayerSync()
208 {
209 }
210
211 void LayerBackedDrawingArea::syncCompositingLayers()
212 {
213 }
214
215 void LayerBackedDrawingArea::platformInit()
216 {
217 }
218
219 void LayerBackedDrawingArea::platformClear()
220 {
221 }
222 #endif
223
224 } // namespace WebKit
225
226 #endif // USE(ACCELERATED_COMPOSITING)