1/*
2 * Copyright (C) 2010-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#include "CallbackID.h"
29#include "DrawingAreaInfo.h"
30#include "LayerTreeContext.h"
31#include "MessageReceiver.h"
32#include "WebPage.h"
33#include <WebCore/ActivityState.h>
34#include <WebCore/FloatRect.h>
35#include <WebCore/IntRect.h>
36#include <WebCore/LayerFlushThrottleState.h>
37#include <WebCore/LayoutMilestone.h>
38#include <WebCore/PlatformScreen.h>
39#include <wtf/Forward.h>
40#include <wtf/Noncopyable.h>
41#include <wtf/TypeCasts.h>
42
43namespace WTF {
44class MachSendRight;
45}
46
47namespace IPC {
48class Connection;
49class Decoder;
50}
51
52namespace WebCore {
53class DisplayRefreshMonitor;
54class Frame;
55class FrameView;
56class GraphicsLayer;
57class GraphicsLayerFactory;
58struct ViewportAttributes;
59}
60
61namespace WebKit {
62
63struct ColorSpaceData;
64class LayerTreeHost;
65struct WebPageCreationParameters;
66struct WebPreferencesStore;
67
68class DrawingArea : public IPC::MessageReceiver {
69 WTF_MAKE_FAST_ALLOCATED;
70 WTF_MAKE_NONCOPYABLE(DrawingArea);
71
72public:
73 static std::unique_ptr<DrawingArea> create(WebPage&, const WebPageCreationParameters&);
74 virtual ~DrawingArea();
75
76 DrawingAreaType type() const { return m_type; }
77 DrawingAreaIdentifier identifier() const { return m_identifier; }
78
79 virtual void setNeedsDisplay() = 0;
80 virtual void setNeedsDisplayInRect(const WebCore::IntRect&) = 0;
81 virtual void scroll(const WebCore::IntRect& scrollRect, const WebCore::IntSize& scrollDelta) = 0;
82
83 // FIXME: These should be pure virtual.
84 virtual void forceRepaint() { }
85 virtual bool forceRepaintAsync(CallbackID) { return false; }
86 virtual void setLayerTreeStateIsFrozen(bool) { }
87 virtual bool layerTreeStateIsFrozen() const { return false; }
88 virtual bool layerFlushThrottlingIsActive() const { return false; }
89
90 virtual void setPaintingEnabled(bool) { }
91 virtual void updatePreferences(const WebPreferencesStore&) { }
92 virtual void mainFrameContentSizeChanged(const WebCore::IntSize&) { }
93
94#if PLATFORM(COCOA)
95 virtual void setViewExposedRect(Optional<WebCore::FloatRect>) = 0;
96 virtual Optional<WebCore::FloatRect> viewExposedRect() const = 0;
97
98 virtual void acceleratedAnimationDidStart(uint64_t /*layerID*/, const String& /*key*/, MonotonicTime /*startTime*/) { }
99 virtual void acceleratedAnimationDidEnd(uint64_t /*layerID*/, const String& /*key*/) { }
100 virtual void addFence(const WTF::MachSendRight&) { }
101#endif
102#if PLATFORM(IOS_FAMILY)
103 virtual WebCore::FloatRect exposedContentRect() const = 0;
104 virtual void setExposedContentRect(const WebCore::FloatRect&) = 0;
105#endif
106 virtual void mainFrameScrollabilityChanged(bool) { }
107
108 virtual bool supportsAsyncScrolling() { return false; }
109
110 virtual bool shouldUseTiledBackingForFrameView(const WebCore::FrameView&) { return false; }
111
112 virtual WebCore::GraphicsLayerFactory* graphicsLayerFactory() { return nullptr; }
113 virtual void setRootCompositingLayer(WebCore::GraphicsLayer*) = 0;
114 virtual void scheduleCompositingLayerFlush() = 0;
115 virtual void scheduleInitialDeferredPaint() = 0;
116 virtual void scheduleCompositingLayerFlushImmediately() = 0;
117
118#if USE(REQUEST_ANIMATION_FRAME_DISPLAY_MONITOR)
119 virtual RefPtr<WebCore::DisplayRefreshMonitor> createDisplayRefreshMonitor(WebCore::PlatformDisplayID);
120#endif
121
122 virtual void dispatchAfterEnsuringUpdatedScrollPosition(WTF::Function<void ()>&&);
123
124 virtual void activityStateDidChange(OptionSet<WebCore::ActivityState::Flag>, ActivityStateChangeID, const Vector<CallbackID>&) { }
125 virtual void setLayerHostingMode(LayerHostingMode) { }
126
127 virtual bool markLayersVolatileImmediatelyIfPossible() { return true; }
128
129 virtual bool adjustLayerFlushThrottling(WebCore::LayerFlushThrottleState::Flags) { return false; }
130
131 virtual void attachViewOverlayGraphicsLayer(WebCore::GraphicsLayer*) { }
132
133 virtual void setShouldScaleViewToFitDocument(bool) { }
134
135 virtual bool addMilestonesToDispatch(OptionSet<WebCore::LayoutMilestone>) { return false; }
136
137#if PLATFORM(COCOA)
138 // Used by TiledCoreAnimationDrawingArea.
139 virtual void updateGeometry(const WebCore::IntSize& viewSize, bool flushSynchronously, const WTF::MachSendRight& fencePort) { }
140#endif
141
142 virtual void layerHostDidFlushLayers() { }
143
144#if USE(COORDINATED_GRAPHICS) || USE(TEXTURE_MAPPER)
145 virtual void didChangeViewportAttributes(WebCore::ViewportAttributes&&) = 0;
146 virtual void deviceOrPageScaleFactorChanged() = 0;
147#endif
148
149 virtual void adoptLayersFromDrawingArea(DrawingArea&) { }
150
151 void removeMessageReceiverIfNeeded();
152
153protected:
154 DrawingArea(DrawingAreaType, DrawingAreaIdentifier, WebPage&);
155
156 template<typename U> bool send(const U& message)
157 {
158 return m_webPage.send(message, m_identifier.toUInt64(), { });
159 }
160
161 DrawingAreaType m_type;
162 DrawingAreaIdentifier m_identifier;
163 WebPage& m_webPage;
164
165private:
166 // IPC::MessageReceiver.
167 void didReceiveMessage(IPC::Connection&, IPC::Decoder&) override;
168
169 // Message handlers.
170 // FIXME: These should be pure virtual.
171 virtual void updateBackingStoreState(uint64_t /*backingStoreStateID*/, bool /*respondImmediately*/, float /*deviceScaleFactor*/, const WebCore::IntSize& /*size*/,
172 const WebCore::IntSize& /*scrollOffset*/) { }
173 virtual void didUpdate() { }
174
175#if PLATFORM(COCOA)
176 // Used by TiledCoreAnimationDrawingArea.
177 virtual void setDeviceScaleFactor(float) { }
178 virtual void setColorSpace(const ColorSpaceData&) { }
179
180 virtual void adjustTransientZoom(double scale, WebCore::FloatPoint origin) { }
181 virtual void commitTransientZoom(double scale, WebCore::FloatPoint origin) { }
182
183 virtual void addTransactionCallbackID(WebKit::CallbackID) { ASSERT_NOT_REACHED(); }
184#endif
185
186 bool m_hasRemovedMessageReceiver { false };
187};
188
189} // namespace WebKit
190
191#define SPECIALIZE_TYPE_TRAITS_DRAWING_AREA(ToValueTypeName, AreaType) \
192SPECIALIZE_TYPE_TRAITS_BEGIN(WebKit::ToValueTypeName) \
193 static bool isType(const WebKit::DrawingArea& area) { return area.type() == WebKit::AreaType; } \
194SPECIALIZE_TYPE_TRAITS_END()
195