1/*
2 * Copyright (C) 2012-2014 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#ifndef RemoteLayerTreeDrawingArea_h
27#define RemoteLayerTreeDrawingArea_h
28
29#include "DrawingArea.h"
30#include "GraphicsLayerCARemote.h"
31#include "RemoteLayerTreeTransaction.h"
32#include <WebCore/GraphicsLayerClient.h>
33#include <WebCore/Timer.h>
34#include <atomic>
35#include <dispatch/dispatch.h>
36#include <wtf/HashMap.h>
37
38namespace WebCore {
39class PlatformCALayer;
40}
41
42namespace IPC {
43class MessageEncoder;
44}
45
46namespace WebKit {
47
48class RemoteLayerTreeContext;
49class RemoteLayerTreeDisplayRefreshMonitor;
50
51class RemoteLayerTreeDrawingArea : public DrawingArea, public WebCore::GraphicsLayerClient {
52    friend class RemoteLayerTreeDisplayRefreshMonitor;
53public:
54    RemoteLayerTreeDrawingArea(WebPage&, const WebPageCreationParameters&);
55    virtual ~RemoteLayerTreeDrawingArea();
56
57    uint64_t nextTransactionID() const { return m_currentTransactionID + 1; }
58
59private:
60    // DrawingArea
61    virtual void setNeedsDisplay() override;
62    virtual void setNeedsDisplayInRect(const WebCore::IntRect&) override;
63    virtual void scroll(const WebCore::IntRect& scrollRect, const WebCore::IntSize& scrollDelta) override;
64    virtual void updateGeometry(const WebCore::IntSize& viewSize, const WebCore::IntSize& layerPosition) override;
65
66    virtual WebCore::GraphicsLayerFactory* graphicsLayerFactory() override;
67    virtual void setRootCompositingLayer(WebCore::GraphicsLayer*) override;
68    virtual void scheduleCompositingLayerFlush() override;
69    virtual void scheduleCompositingLayerFlushImmediately() override;
70
71    virtual void addTransactionCallbackID(uint64_t callbackID) override;
72
73    virtual PassRefPtr<WebCore::DisplayRefreshMonitor> createDisplayRefreshMonitor(PlatformDisplayID) override;
74    void willDestroyDisplayRefreshMonitor(WebCore::DisplayRefreshMonitor*);
75
76    virtual bool shouldUseTiledBackingForFrameView(const WebCore::FrameView*) override;
77
78    virtual void updatePreferences(const WebPreferencesStore&) override;
79
80    virtual bool supportsAsyncScrolling() override { return true; }
81
82    virtual void setLayerTreeStateIsFrozen(bool) override;
83
84    virtual void forceRepaint() override;
85    virtual bool forceRepaintAsync(uint64_t) override { return false; }
86
87    virtual void setExposedRect(const WebCore::FloatRect&) override;
88    virtual WebCore::FloatRect exposedRect() const override { return m_scrolledExposedRect; }
89
90    virtual void acceleratedAnimationDidStart(uint64_t layerID, const String& key, double startTime) override;
91
92#if PLATFORM(IOS)
93    virtual void setExposedContentRect(const WebCore::FloatRect&) override;
94#endif
95
96    virtual void didUpdate() override;
97
98#if PLATFORM(IOS)
99    virtual void setDeviceScaleFactor(float) override;
100#endif
101
102    virtual void mainFrameContentSizeChanged(const WebCore::IntSize&) override;
103
104    virtual void viewStateDidChange(WebCore::ViewState::Flags changed, bool wantsDidUpdateViewState) override;
105
106    virtual bool adjustLayerFlushThrottling(WebCore::LayerFlushThrottleState::Flags) override;
107
108    // GraphicsLayerClient
109    virtual void notifyAnimationStarted(const WebCore::GraphicsLayer*, double time) override { }
110    virtual void notifyFlushRequired(const WebCore::GraphicsLayer*) override { }
111    virtual void paintContents(const WebCore::GraphicsLayer*, WebCore::GraphicsContext&, WebCore::GraphicsLayerPaintingPhase, const WebCore::FloatRect& inClip) override { }
112
113    void updateScrolledExposedRect();
114
115    void layerFlushTimerFired(WebCore::Timer<RemoteLayerTreeDrawingArea>*);
116    void flushLayers();
117
118    WebCore::TiledBacking* mainFrameTiledBacking() const;
119
120    uint64_t takeNextTransactionID() { return ++m_currentTransactionID; }
121
122    virtual bool markLayersVolatileImmediatelyIfPossible() override;
123
124    class BackingStoreFlusher : public ThreadSafeRefCounted<BackingStoreFlusher> {
125    public:
126        static PassRefPtr<BackingStoreFlusher> create(IPC::Connection*, std::unique_ptr<IPC::MessageEncoder>, Vector<RetainPtr<CGContextRef>>);
127
128        void flush();
129        bool hasFlushed() const { return m_hasFlushed; }
130
131    private:
132        BackingStoreFlusher(IPC::Connection*, std::unique_ptr<IPC::MessageEncoder>, Vector<RetainPtr<CGContextRef>>);
133
134        RefPtr<IPC::Connection> m_connection;
135        std::unique_ptr<IPC::MessageEncoder> m_commitEncoder;
136        Vector<RetainPtr<CGContextRef>> m_contextsToFlush;
137
138        std::atomic<bool> m_hasFlushed;
139    };
140
141    std::unique_ptr<RemoteLayerTreeContext> m_remoteLayerTreeContext;
142    std::unique_ptr<WebCore::GraphicsLayer> m_rootLayer;
143
144    WebCore::IntSize m_viewSize;
145
146    WebCore::FloatRect m_exposedRect;
147    WebCore::FloatRect m_scrolledExposedRect;
148
149    WebCore::Timer<RemoteLayerTreeDrawingArea> m_layerFlushTimer;
150    bool m_isFlushingSuspended;
151    bool m_hasDeferredFlush;
152    bool m_isThrottlingLayerFlushes;
153    bool m_isLayerFlushThrottlingTemporarilyDisabledForInteraction;
154    bool m_isInitialThrottledLayerFlush;
155
156    bool m_waitingForBackingStoreSwap;
157    bool m_hadFlushDeferredWhileWaitingForBackingStoreSwap;
158
159    dispatch_queue_t m_commitQueue;
160    RefPtr<BackingStoreFlusher> m_pendingBackingStoreFlusher;
161
162    HashSet<RemoteLayerTreeDisplayRefreshMonitor*> m_displayRefreshMonitors;
163    HashSet<RemoteLayerTreeDisplayRefreshMonitor*>* m_displayRefreshMonitorsToNotify;
164
165    uint64_t m_currentTransactionID;
166    Vector<RemoteLayerTreeTransaction::TransactionCallbackID> m_pendingCallbackIDs;
167};
168
169DRAWING_AREA_TYPE_CASTS(RemoteLayerTreeDrawingArea, type() == DrawingAreaTypeRemoteLayerTree);
170
171} // namespace WebKit
172
173#endif // RemoteLayerTreeDrawingArea_h
174