WeakPtrFactory should populate m_ref lazily.
[WebKit-https.git] / Source / WebKit / WebProcess / WebPage / RemoteLayerTree / RemoteLayerTreeDrawingArea.h
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 #pragma once
27
28 #include "CallbackID.h"
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 #include <wtf/WeakPtr.h>
38
39 namespace WebCore {
40 class PlatformCALayer;
41 }
42
43 namespace WebKit {
44
45 class RemoteLayerTreeContext;
46 class RemoteLayerTreeDisplayRefreshMonitor;
47
48 class RemoteLayerTreeDrawingArea : public DrawingArea, public WebCore::GraphicsLayerClient {
49     friend class RemoteLayerTreeDisplayRefreshMonitor;
50 public:
51     RemoteLayerTreeDrawingArea(WebPage&, const WebPageCreationParameters&);
52     virtual ~RemoteLayerTreeDrawingArea();
53
54     uint64_t nextTransactionID() const { return m_currentTransactionID + 1; }
55     uint64_t lastCommittedTransactionID() const { return m_currentTransactionID; }
56
57     WeakPtr<RemoteLayerTreeDrawingArea> createWeakPtr() { return m_weakPtrFactory.createWeakPtr(*this); }
58
59 private:
60     // DrawingArea
61     void setNeedsDisplay() override;
62     void setNeedsDisplayInRect(const WebCore::IntRect&) override;
63     void scroll(const WebCore::IntRect& scrollRect, const WebCore::IntSize& scrollDelta) override;
64     void updateGeometry(const WebCore::IntSize& viewSize, const WebCore::IntSize& layerPosition, bool flushSynchronously, const WebCore::MachSendRight& fencePort) override;
65
66     WebCore::GraphicsLayerFactory* graphicsLayerFactory() override;
67     void setRootCompositingLayer(WebCore::GraphicsLayer*) override;
68     void scheduleCompositingLayerFlush() override;
69     void scheduleCompositingLayerFlushImmediately() override;
70     void attachViewOverlayGraphicsLayer(WebCore::Frame*, WebCore::GraphicsLayer*) override;
71
72     void addTransactionCallbackID(CallbackID) override;
73
74     RefPtr<WebCore::DisplayRefreshMonitor> createDisplayRefreshMonitor(WebCore::PlatformDisplayID) override;
75     void willDestroyDisplayRefreshMonitor(WebCore::DisplayRefreshMonitor*);
76
77     bool shouldUseTiledBackingForFrameView(const WebCore::FrameView&) override;
78
79     void updatePreferences(const WebPreferencesStore&) override;
80
81     bool supportsAsyncScrolling() override { return true; }
82
83     void setLayerTreeStateIsFrozen(bool) override;
84
85     void forceRepaint() override;
86     bool forceRepaintAsync(CallbackID) override { return false; }
87
88     void setViewExposedRect(std::optional<WebCore::FloatRect>) override;
89     std::optional<WebCore::FloatRect> viewExposedRect() const override { return m_scrolledViewExposedRect; }
90
91     void acceleratedAnimationDidStart(uint64_t layerID, const String& key, double startTime) override;
92     void acceleratedAnimationDidEnd(uint64_t layerID, const String& key) override;
93
94 #if PLATFORM(IOS)
95     WebCore::FloatRect exposedContentRect() const override;
96     void setExposedContentRect(const WebCore::FloatRect&) override;
97 #endif
98
99     void didUpdate() override;
100
101 #if PLATFORM(IOS)
102     void setDeviceScaleFactor(float) override;
103 #endif
104
105     void mainFrameContentSizeChanged(const WebCore::IntSize&) override;
106
107     void activityStateDidChange(WebCore::ActivityState::Flags changed, bool wantsDidUpdateActivityState, const Vector<CallbackID>& callbackIDs) override;
108
109     bool adjustLayerFlushThrottling(WebCore::LayerFlushThrottleState::Flags) override;
110
111     bool dispatchDidReachLayoutMilestone(WebCore::LayoutMilestones) override;
112
113     void updateScrolledExposedRect();
114     void updateRootLayers();
115
116     void flushLayers();
117
118     WebCore::TiledBacking* mainFrameTiledBacking() const;
119
120     uint64_t takeNextTransactionID() { return ++m_currentTransactionID; }
121
122     bool markLayersVolatileImmediatelyIfPossible() override;
123
124     class BackingStoreFlusher : public ThreadSafeRefCounted<BackingStoreFlusher> {
125     public:
126         static Ref<BackingStoreFlusher> create(IPC::Connection*, std::unique_ptr<IPC::Encoder>, 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::Encoder>, Vector<RetainPtr<CGContextRef>>);
133
134         RefPtr<IPC::Connection> m_connection;
135         std::unique_ptr<IPC::Encoder> 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     std::optional<WebCore::FloatRect> m_viewExposedRect;
147     std::optional<WebCore::FloatRect> m_scrolledViewExposedRect;
148
149     WebCore::Timer m_layerFlushTimer;
150     bool m_isFlushingSuspended { false };
151     bool m_hasDeferredFlush { false };
152     bool m_isThrottlingLayerFlushes { false };
153     bool m_isLayerFlushThrottlingTemporarilyDisabledForInteraction { false };
154     bool m_isInitialThrottledLayerFlush { false };
155
156     bool m_waitingForBackingStoreSwap { false };
157     bool m_hadFlushDeferredWhileWaitingForBackingStoreSwap { false };
158     bool m_nextFlushIsForImmediatePaint { false };
159
160     dispatch_queue_t m_commitQueue;
161     RefPtr<BackingStoreFlusher> m_pendingBackingStoreFlusher;
162
163     HashSet<RemoteLayerTreeDisplayRefreshMonitor*> m_displayRefreshMonitors;
164     HashSet<RemoteLayerTreeDisplayRefreshMonitor*>* m_displayRefreshMonitorsToNotify { nullptr };
165
166     uint64_t m_currentTransactionID { 0 };
167     Vector<RemoteLayerTreeTransaction::TransactionCallbackID> m_pendingCallbackIDs;
168
169     WebCore::LayoutMilestones m_pendingNewlyReachedLayoutMilestones { 0 };
170
171     WebCore::GraphicsLayer* m_contentLayer { nullptr };
172     WebCore::GraphicsLayer* m_viewOverlayRootLayer { nullptr };
173     
174     WeakPtrFactory<RemoteLayerTreeDrawingArea> m_weakPtrFactory;
175 };
176
177 } // namespace WebKit
178
179 SPECIALIZE_TYPE_TRAITS_DRAWING_AREA(RemoteLayerTreeDrawingArea, DrawingAreaTypeRemoteLayerTree)