1/*
2 * Copyright (C) 2011 Apple Inc. All rights reserved.
3 * Copyright (C) 2013 Nokia Corporation and/or its subsidiary(-ies).
4 * Copyright (C) 2016-2019 Igalia S.L.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
17 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
19 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
25 * THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28#pragma once
29
30#include "BackingStore.h"
31#include "DrawingAreaProxy.h"
32#include "LayerTreeContext.h"
33#include <wtf/RunLoop.h>
34
35namespace WebCore {
36class Region;
37}
38
39namespace WebKit {
40
41class DrawingAreaProxyCoordinatedGraphics final : public DrawingAreaProxy {
42public:
43 DrawingAreaProxyCoordinatedGraphics(WebPageProxy&, WebProcessProxy&);
44 virtual ~DrawingAreaProxyCoordinatedGraphics();
45
46#if !PLATFORM(WPE)
47 void paint(BackingStore::PlatformGraphicsContext, const WebCore::IntRect&, WebCore::Region& unpaintedRegion);
48#endif
49
50 bool isInAcceleratedCompositingMode() const { return !m_layerTreeContext.isEmpty(); }
51
52private:
53 // DrawingAreaProxy
54 void sizeDidChange() override;
55 void deviceScaleFactorDidChange() override;
56 void waitForBackingStoreUpdateOnNextPaint() override;
57 void setBackingStoreIsDiscardable(bool) override;
58
59 // IPC message handlers
60 void update(uint64_t backingStoreStateID, const UpdateInfo&) override;
61 void didUpdateBackingStoreState(uint64_t backingStoreStateID, const UpdateInfo&, const LayerTreeContext&) override;
62 void enterAcceleratedCompositingMode(uint64_t backingStoreStateID, const LayerTreeContext&) override;
63 void exitAcceleratedCompositingMode(uint64_t backingStoreStateID, const UpdateInfo&) override;
64 void updateAcceleratedCompositingMode(uint64_t backingStoreStateID, const LayerTreeContext&) override;
65
66#if !PLATFORM(WPE)
67 void incorporateUpdate(const UpdateInfo&);
68#endif
69
70 bool alwaysUseCompositing() const;
71 void enterAcceleratedCompositingMode(const LayerTreeContext&);
72 void exitAcceleratedCompositingMode();
73 void updateAcceleratedCompositingMode(const LayerTreeContext&);
74
75 enum RespondImmediatelyOrNot { DoNotRespondImmediately, RespondImmediately };
76 void backingStoreStateDidChange(RespondImmediatelyOrNot);
77 void sendUpdateBackingStoreState(RespondImmediatelyOrNot);
78 void waitForAndDispatchDidUpdateBackingStoreState();
79
80#if !PLATFORM(WPE)
81 void discardBackingStoreSoon();
82 void discardBackingStore();
83#endif
84
85 void dispatchAfterEnsuringDrawing(WTF::Function<void(CallbackBase::Error)>&&) override;
86
87 class DrawingMonitor {
88 WTF_MAKE_NONCOPYABLE(DrawingMonitor); WTF_MAKE_FAST_ALLOCATED;
89 public:
90 DrawingMonitor(WebPageProxy&);
91 ~DrawingMonitor();
92
93 void start(WTF::Function<void(CallbackBase::Error)>&&);
94
95 private:
96 static int webViewDrawCallback(DrawingMonitor*);
97
98 void stop();
99 void didDraw();
100
101 MonotonicTime m_startTime;
102 WTF::Function<void(CallbackBase::Error)> m_callback;
103 RunLoop::Timer<DrawingMonitor> m_timer;
104#if PLATFORM(GTK)
105 WebPageProxy& m_webPage;
106#endif
107 };
108
109 // The state ID corresponding to our current backing store. Updated whenever we allocate
110 // a new backing store. Any messages received that correspond to an earlier state are ignored,
111 // as they don't apply to our current backing store.
112 uint64_t m_currentBackingStoreStateID { 0 };
113
114 // The next backing store state ID we will request the web process update to. Incremented
115 // whenever our state changes in a way that will require a new backing store to be allocated.
116 uint64_t m_nextBackingStoreStateID { 0 };
117
118 // The current layer tree context.
119 LayerTreeContext m_layerTreeContext;
120
121 // Whether we've sent a UpdateBackingStoreState message and are now waiting for a DidUpdateBackingStoreState message.
122 // Used to throttle UpdateBackingStoreState messages so we don't send them faster than the Web process can handle.
123 bool m_isWaitingForDidUpdateBackingStoreState { false };
124
125 // For a new Drawing Area don't draw anything until the WebProcess has sent over the first content.
126 bool m_hasReceivedFirstUpdate { false };
127
128#if !PLATFORM(WPE)
129 bool m_isBackingStoreDiscardable { true };
130 std::unique_ptr<BackingStore> m_backingStore;
131 RunLoop::Timer<DrawingAreaProxyCoordinatedGraphics> m_discardBackingStoreTimer;
132#endif
133 std::unique_ptr<DrawingMonitor> m_drawingMonitor;
134};
135
136} // namespace WebKit
137