1/*
2 * Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies)
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#ifndef TiledDrawingAreaProxy_h
27#define TiledDrawingAreaProxy_h
28
29#if ENABLE(TILED_BACKING_STORE)
30
31#include "DrawingAreaProxy.h"
32#include <WebCore/GraphicsContext.h>
33#include <WebCore/IntRect.h>
34#include <wtf/HashSet.h>
35
36#include "RunLoop.h"
37#include "TiledDrawingAreaTile.h"
38
39#if PLATFORM(MAC)
40#include <wtf/RetainPtr.h>
41#ifdef __OBJC__
42@class WKView;
43#else
44class WKView;
45#endif
46#elif PLATFORM(QT)
47class QGraphicsWKView;
48#include <QImage>
49#endif
50
51namespace WebKit {
52
53class UpdateChunk;
54class WebPageProxy;
55
56#if PLATFORM(MAC)
57typedef WKView PlatformWebView;
58#elif PLATFORM(WIN)
59class WebView;
60typedef WebView PlatformWebView;
61#elif PLATFORM(QT)
62typedef QGraphicsWKView PlatformWebView;
63#endif
64
65class TiledDrawingAreaProxy : public DrawingAreaProxy {
66public:
67    static PassOwnPtr<TiledDrawingAreaProxy> create(PlatformWebView* webView, WebPageProxy*);
68
69    TiledDrawingAreaProxy(PlatformWebView*, WebPageProxy*);
70    virtual ~TiledDrawingAreaProxy();
71
72    float contentsScale() const { return m_contentsScale; }
73    void setContentsScale(float);
74
75    void waitUntilUpdatesComplete();
76
77    void takeSnapshot(const WebCore::IntSize& size, const WebCore::IntRect& contentsRect);
78
79#if USE(ACCELERATED_COMPOSITING)
80    virtual void attachCompositingContext(uint32_t /* contextID */) { }
81    virtual void detachCompositingContext() { }
82#endif
83
84    void paint(WebCore::GraphicsContext*, const WebCore::IntRect&);
85
86    WebCore::IntSize tileSize() { return m_tileSize; }
87    void setTileSize(const WebCore::IntSize&);
88
89    double tileCreationDelay() const { return m_tileCreationDelay; }
90    void setTileCreationDelay(double delay);
91
92    // Tiled are dropped outside the keep area, and created for cover area. The values a relative to the viewport size.
93    void getKeepAndCoverAreaMultipliers(WebCore::FloatSize& keepMultiplier, WebCore::FloatSize& coverMultiplier)
94    {
95        keepMultiplier = m_keepAreaMultiplier;
96        coverMultiplier = m_coverAreaMultiplier;
97    }
98    void setKeepAndCoverAreaMultipliers(const WebCore::FloatSize& keepMultiplier, const WebCore::FloatSize& coverMultiplier);
99
100    void tileBufferUpdateComplete();
101
102    WebCore::IntRect mapToContents(const WebCore::IntRect&) const;
103    WebCore::IntRect mapFromContents(const WebCore::IntRect&) const;
104
105    bool hasPendingUpdates() const;
106
107private:
108    WebPageProxy* page();
109    WebCore::IntRect webViewVisibleRect();
110    void updateWebView(const Vector<WebCore::IntRect>& paintedArea);
111
112    void snapshotTaken(UpdateChunk&);
113
114    // DrawingAreaProxy
115    virtual void didReceiveMessage(CoreIPC::Connection*, CoreIPC::MessageID, CoreIPC::ArgumentDecoder*);
116    virtual bool paint(const WebCore::IntRect&, PlatformDrawingContext);
117    virtual void sizeDidChange();
118    virtual void setPageIsVisible(bool isVisible);
119
120    void didSetSize(const WebCore::IntSize&);
121    void invalidate(const WebCore::IntRect& rect);
122    void adjustVisibleRect();
123
124    void requestTileUpdate(int tileID, const WebCore::IntRect& dirtyRect);
125
126    PassRefPtr<TiledDrawingAreaTile> createTile(const TiledDrawingAreaTile::Coordinate&);
127
128    void startTileBufferUpdateTimer();
129    void startTileCreationTimer();
130
131    void tileBufferUpdateTimerFired();
132    void tileCreationTimerFired();
133
134    void updateTileBuffers();
135    void createTiles();
136
137    bool resizeEdgeTiles();
138    void dropTilesOutsideRect(const WebCore::IntRect&);
139
140    PassRefPtr<TiledDrawingAreaTile> tileAt(const TiledDrawingAreaTile::Coordinate&) const;
141    void setTile(const TiledDrawingAreaTile::Coordinate& coordinate, RefPtr<TiledDrawingAreaTile> tile);
142    void removeTile(const TiledDrawingAreaTile::Coordinate& coordinate);
143    void removeAllTiles();
144
145    WebCore::IntRect contentsRect() const;
146
147    WebCore::IntRect calculateKeepRect(const WebCore::IntRect& visibleRect) const;
148    WebCore::IntRect calculateCoverRect(const WebCore::IntRect& visibleRect) const;
149
150    WebCore::IntRect tileRectForCoordinate(const TiledDrawingAreaTile::Coordinate&) const;
151    TiledDrawingAreaTile::Coordinate tileCoordinateForPoint(const WebCore::IntPoint&) const;
152    double tileDistance(const WebCore::IntRect& viewport, const TiledDrawingAreaTile::Coordinate&);
153
154private:
155    bool m_isWaitingForDidSetFrameNotification;
156    bool m_isVisible;
157
158    WebCore::IntSize m_viewSize; // Size of the BackingStore as well.
159    WebCore::IntSize m_lastSetViewSize;
160
161    PlatformWebView* m_webView;
162
163    typedef HashMap<TiledDrawingAreaTile::Coordinate, RefPtr<TiledDrawingAreaTile> > TileMap;
164    TileMap m_tiles;
165
166    WTF::HashMap<int, TiledDrawingAreaTile*> m_tilesByID;
167
168    typedef RunLoop::Timer<TiledDrawingAreaProxy> TileTimer;
169    TileTimer m_tileBufferUpdateTimer;
170    TileTimer m_tileCreationTimer;
171
172    WebCore::IntSize m_tileSize;
173    double m_tileCreationDelay;
174    WebCore::FloatSize m_keepAreaMultiplier;
175    WebCore::FloatSize m_coverAreaMultiplier;
176
177    WebCore::IntRect m_previousVisibleRect;
178    float m_contentsScale;
179
180    friend class TiledDrawingAreaTile;
181};
182
183} // namespace WebKit
184
185#endif // TILED_BACKING_STORE
186
187#endif // TiledDrawingAreaProxy_h
188