1/*
2 * Copyright (C) 2014 Igalia S.L.
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 USE(COORDINATED_GRAPHICS)
29
30#include "CompositingRunLoop.h"
31#include "CoordinatedGraphicsScene.h"
32#include <WebCore/CoordinatedGraphicsState.h>
33#include <WebCore/GLContext.h>
34#include <WebCore/IntSize.h>
35#include <WebCore/TextureMapper.h>
36#include <wtf/Atomics.h>
37#include <wtf/FastMalloc.h>
38#include <wtf/Noncopyable.h>
39#include <wtf/ThreadSafeRefCounted.h>
40
41#if USE(REQUEST_ANIMATION_FRAME_DISPLAY_MONITOR)
42#include "ThreadedDisplayRefreshMonitor.h"
43#endif
44
45namespace WebKit {
46
47class CoordinatedGraphicsScene;
48class CoordinatedGraphicsSceneClient;
49
50class ThreadedCompositor : public CoordinatedGraphicsSceneClient, public ThreadSafeRefCounted<ThreadedCompositor> {
51 WTF_MAKE_NONCOPYABLE(ThreadedCompositor);
52 WTF_MAKE_FAST_ALLOCATED;
53public:
54 class Client {
55 public:
56 virtual uint64_t nativeSurfaceHandleForCompositing() = 0;
57 virtual void didDestroyGLContext() = 0;
58
59 virtual void resize(const WebCore::IntSize&) = 0;
60 virtual void willRenderFrame() = 0;
61 virtual void didRenderFrame() = 0;
62 };
63
64 enum class ShouldDoFrameSync { No, Yes };
65
66 static Ref<ThreadedCompositor> create(Client&, ThreadedDisplayRefreshMonitor::Client&, WebCore::PlatformDisplayID, const WebCore::IntSize&, float scaleFactor, ShouldDoFrameSync = ShouldDoFrameSync::Yes, WebCore::TextureMapper::PaintFlags = 0);
67 virtual ~ThreadedCompositor();
68
69 void setNativeSurfaceHandleForCompositing(uint64_t);
70 void setScaleFactor(float);
71 void setScrollPosition(const WebCore::IntPoint&, float scale);
72 void setViewportSize(const WebCore::IntSize&, float scale);
73
74 void updateSceneState(const WebCore::CoordinatedGraphicsState&);
75
76 void invalidate();
77
78 void forceRepaint();
79
80#if USE(REQUEST_ANIMATION_FRAME_DISPLAY_MONITOR)
81 RefPtr<WebCore::DisplayRefreshMonitor> displayRefreshMonitor(WebCore::PlatformDisplayID);
82#endif
83
84 void frameComplete();
85
86 void suspend();
87 void resume();
88
89private:
90 ThreadedCompositor(Client&, ThreadedDisplayRefreshMonitor::Client&, WebCore::PlatformDisplayID, const WebCore::IntSize&, float scaleFactor, ShouldDoFrameSync, WebCore::TextureMapper::PaintFlags);
91
92 // CoordinatedGraphicsSceneClient
93 void updateViewport() override;
94
95 void renderLayerTree();
96 void sceneUpdateFinished();
97
98 void createGLContext();
99
100 Client& m_client;
101 RefPtr<CoordinatedGraphicsScene> m_scene;
102 std::unique_ptr<WebCore::GLContext> m_context;
103
104 uint64_t m_nativeSurfaceHandle;
105 ShouldDoFrameSync m_doFrameSync;
106 WebCore::TextureMapper::PaintFlags m_paintFlags { 0 };
107 bool m_inForceRepaint { false };
108 unsigned m_suspendedCount { 0 };
109
110 std::unique_ptr<CompositingRunLoop> m_compositingRunLoop;
111
112 struct {
113 Lock lock;
114 WebCore::IntSize viewportSize;
115 WebCore::IntPoint scrollPosition;
116 float scaleFactor { 1 };
117 bool needsResize { false };
118 Vector<WebCore::CoordinatedGraphicsState> states;
119
120 bool clientRendersNextFrame { false };
121 } m_attributes;
122
123#if USE(REQUEST_ANIMATION_FRAME_DISPLAY_MONITOR)
124 Ref<ThreadedDisplayRefreshMonitor> m_displayRefreshMonitor;
125#endif
126};
127
128} // namespace WebKit
129
130#endif // USE(COORDINATED_GRAPHICS)
131
132