REGRESSION (r242687): Flicker when pinch-zooming pages in macOS Safari
[WebKit-https.git] / Source / WebKit / WebProcess / WebPage / mac / TiledCoreAnimationDrawingArea.h
1 /*
2  * Copyright (C) 2011-2018 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 #if PLATFORM(MAC)
29
30 #include "CallbackID.h"
31 #include "DrawingArea.h"
32 #include "LayerTreeContext.h"
33 #include <WebCore/FloatRect.h>
34 #include <WebCore/TransformationMatrix.h>
35 #include <wtf/HashMap.h>
36 #include <wtf/RetainPtr.h>
37 #include <wtf/RunLoop.h>
38
39 OBJC_CLASS CALayer;
40
41 namespace WebCore {
42 class FrameView;
43 class PlatformCALayer;
44 class RunLoopObserver;
45 class TiledBacking;
46 }
47
48 namespace WebKit {
49
50 class LayerHostingContext;
51
52 class TiledCoreAnimationDrawingArea : public DrawingArea, public CanMakeWeakPtr<TiledCoreAnimationDrawingArea> {
53 public:
54     TiledCoreAnimationDrawingArea(WebPage&, const WebPageCreationParameters&);
55     virtual ~TiledCoreAnimationDrawingArea();
56
57 private:
58     // DrawingArea
59     void setNeedsDisplay() override;
60     void setNeedsDisplayInRect(const WebCore::IntRect&) override;
61     void scroll(const WebCore::IntRect& scrollRect, const WebCore::IntSize& scrollDelta) override;
62
63     void forceRepaint() override;
64     bool forceRepaintAsync(CallbackID) override;
65     void setLayerTreeStateIsFrozen(bool) override;
66     bool layerTreeStateIsFrozen() const override;
67     void setRootCompositingLayer(WebCore::GraphicsLayer*) override;
68     void scheduleInitialDeferredPaint() override;
69     void scheduleCompositingLayerFlush() override;
70     void scheduleCompositingLayerFlushImmediately() override;
71
72     void updatePreferences(const WebPreferencesStore&) override;
73     void mainFrameContentSizeChanged(const WebCore::IntSize&) override;
74
75     void setViewExposedRect(Optional<WebCore::FloatRect>) override;
76     Optional<WebCore::FloatRect> viewExposedRect() const override { return m_scrolledViewExposedRect; }
77
78     bool supportsAsyncScrolling() override { return true; }
79
80     void dispatchAfterEnsuringUpdatedScrollPosition(WTF::Function<void ()>&&) override;
81
82     bool shouldUseTiledBackingForFrameView(const WebCore::FrameView&) override;
83
84     void activityStateDidChange(OptionSet<WebCore::ActivityState::Flag> changed, ActivityStateChangeID, const Vector<CallbackID>&) override;
85     void didUpdateActivityStateTimerFired();
86
87     void attachViewOverlayGraphicsLayer(WebCore::GraphicsLayer*) override;
88
89     bool dispatchDidReachLayoutMilestone(OptionSet<WebCore::LayoutMilestone>) override;
90
91     enum class FlushType { Normal, TransientZoom };
92     void flushLayers(FlushType = FlushType::Normal);
93
94     // Message handlers.
95     void updateGeometry(const WebCore::IntSize& viewSize, bool flushSynchronously, const WTF::MachSendRight& fencePort) override;
96     void setDeviceScaleFactor(float) override;
97     void suspendPainting();
98     void resumePainting();
99     void setLayerHostingMode(LayerHostingMode) override;
100     void setColorSpace(const ColorSpaceData&) override;
101     void addFence(const WTF::MachSendRight&) override;
102
103     void addTransactionCallbackID(CallbackID) override;
104     void setShouldScaleViewToFitDocument(bool) override;
105
106     void sendEnterAcceleratedCompositingModeIfNeeded();
107
108     void adjustTransientZoom(double scale, WebCore::FloatPoint origin) override;
109     void commitTransientZoom(double scale, WebCore::FloatPoint origin) override;
110     void applyTransientZoomToPage(double scale, WebCore::FloatPoint origin);
111     WebCore::PlatformCALayer* layerForTransientZoom() const;
112     WebCore::PlatformCALayer* shadowLayerForTransientZoom() const;
113
114     void applyTransientZoomToLayers(double scale, WebCore::FloatPoint origin);
115
116     void updateLayerHostingContext();
117
118     void setRootCompositingLayer(CALayer *);
119     void updateRootLayers();
120
121     WebCore::TiledBacking* mainFrameTiledBacking() const;
122     void updateDebugInfoLayer(bool showLayer);
123
124     void updateIntrinsicContentSizeIfNeeded();
125     void updateScrolledExposedRect();
126     void scaleViewToFitDocumentIfNeeded();
127
128     void sendPendingNewlyReachedLayoutMilestones();
129
130     void layerFlushRunLoopCallback();
131     void invalidateLayerFlushRunLoopObserver();
132     void scheduleLayerFlushRunLoopObserver();
133
134     bool adjustLayerFlushThrottling(WebCore::LayerFlushThrottleState::Flags) override;
135     bool layerFlushThrottlingIsActive() const override;
136
137     void startLayerFlushThrottlingTimer();
138     void layerFlushThrottlingTimerFired();
139
140     std::unique_ptr<LayerHostingContext> m_layerHostingContext;
141
142     RetainPtr<CALayer> m_hostingLayer;
143     RetainPtr<CALayer> m_rootLayer;
144     RetainPtr<CALayer> m_debugInfoLayer;
145     RetainPtr<CALayer> m_pendingRootLayer;
146
147     Optional<WebCore::FloatRect> m_viewExposedRect;
148     Optional<WebCore::FloatRect> m_scrolledViewExposedRect;
149
150     WebCore::IntSize m_lastViewSizeForScaleToFit;
151     WebCore::IntSize m_lastDocumentSizeForScaleToFit;
152
153     WebCore::IntSize m_lastSentIntrinsicContentSize;
154
155     double m_transientZoomScale { 1 };
156     WebCore::FloatPoint m_transientZoomOrigin;
157
158     WebCore::Timer m_layerFlushThrottlingTimer;
159
160     RunLoop::Timer<TiledCoreAnimationDrawingArea> m_sendDidUpdateActivityStateTimer;
161     Vector<CallbackID> m_nextActivityStateChangeCallbackIDs;
162     ActivityStateChangeID m_activityStateChangeID { ActivityStateChangeAsynchronous };
163
164     RefPtr<WebCore::GraphicsLayer> m_viewOverlayRootLayer;
165
166     OptionSet<WebCore::LayoutMilestone> m_pendingNewlyReachedLayoutMilestones;
167     Vector<CallbackID> m_pendingCallbackIDs;
168
169     std::unique_ptr<WebCore::RunLoopObserver> m_layerFlushRunLoopObserver;
170
171     bool m_isPaintingSuspended { false };
172     bool m_inUpdateGeometry { false };
173     bool m_layerTreeStateIsFrozen { false };
174     bool m_shouldScaleViewToFitDocument { false };
175     bool m_isScalingViewToFitDocument { false };
176     bool m_isThrottlingLayerFlushes { false };
177     bool m_isLayerFlushThrottlingTemporarilyDisabledForInteraction { false };
178     bool m_needsSendEnterAcceleratedCompositingMode { true };
179 };
180
181 } // namespace WebKit
182
183 SPECIALIZE_TYPE_TRAITS_DRAWING_AREA(TiledCoreAnimationDrawingArea, DrawingAreaTypeTiledCoreAnimation)
184
185 #endif // PLATFORM(MAC)