1 | /* |
2 | * Copyright (C) 2016 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 COMPUTER, INC. ``AS IS'' AND ANY |
14 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
15 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
16 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR |
17 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
18 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
19 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
20 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
21 | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
22 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
23 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
24 | */ |
25 | |
26 | #include "config.h" |
27 | #include "AcceleratedBackingStoreX11.h" |
28 | |
29 | #if PLATFORM(X11) |
30 | |
31 | #include "DrawingAreaProxyCoordinatedGraphics.h" |
32 | #include "LayerTreeContext.h" |
33 | #include "WebPageProxy.h" |
34 | #include <WebCore/CairoUtilities.h> |
35 | #include <WebCore/PlatformDisplayX11.h> |
36 | #include <WebCore/XErrorTrapper.h> |
37 | #include <X11/Xlib.h> |
38 | #include <X11/extensions/Xdamage.h> |
39 | #include <cairo-xlib.h> |
40 | #include <gdk/gdkx.h> |
41 | #include <gtk/gtk.h> |
42 | #include <wtf/HashMap.h> |
43 | #include <wtf/NeverDestroyed.h> |
44 | |
45 | namespace WebKit { |
46 | using namespace WebCore; |
47 | |
48 | static Optional<int> s_damageEventBase; |
49 | static Optional<int> s_damageErrorBase; |
50 | |
51 | class XDamageNotifier { |
52 | WTF_MAKE_NONCOPYABLE(XDamageNotifier); |
53 | friend NeverDestroyed<XDamageNotifier>; |
54 | public: |
55 | static XDamageNotifier& singleton() |
56 | { |
57 | static NeverDestroyed<XDamageNotifier> notifier; |
58 | return notifier; |
59 | } |
60 | |
61 | void add(Damage damage, WTF::Function<void()>&& notifyFunction) |
62 | { |
63 | if (m_notifyFunctions.isEmpty()) |
64 | gdk_window_add_filter(nullptr, reinterpret_cast<GdkFilterFunc>(&filterXDamageEvent), this); |
65 | m_notifyFunctions.add(damage, WTFMove(notifyFunction)); |
66 | } |
67 | |
68 | void remove(Damage damage) |
69 | { |
70 | m_notifyFunctions.remove(damage); |
71 | if (m_notifyFunctions.isEmpty()) |
72 | gdk_window_remove_filter(nullptr, reinterpret_cast<GdkFilterFunc>(&filterXDamageEvent), this); |
73 | } |
74 | |
75 | private: |
76 | XDamageNotifier() = default; |
77 | |
78 | static GdkFilterReturn filterXDamageEvent(GdkXEvent* event, GdkEvent*, XDamageNotifier* notifier) |
79 | { |
80 | auto* xEvent = static_cast<XEvent*>(event); |
81 | if (xEvent->type != s_damageEventBase.value() + XDamageNotify) |
82 | return GDK_FILTER_CONTINUE; |
83 | |
84 | auto* damageEvent = reinterpret_cast<XDamageNotifyEvent*>(xEvent); |
85 | if (notifier->notify(damageEvent->damage)) { |
86 | XDamageSubtract(xEvent->xany.display, damageEvent->damage, None, None); |
87 | return GDK_FILTER_REMOVE; |
88 | } |
89 | |
90 | return GDK_FILTER_CONTINUE; |
91 | } |
92 | |
93 | bool notify(Damage damage) const |
94 | { |
95 | auto it = m_notifyFunctions.find(damage); |
96 | if (it != m_notifyFunctions.end()) { |
97 | ((*it).value)(); |
98 | return true; |
99 | } |
100 | return false; |
101 | } |
102 | |
103 | HashMap<Damage, WTF::Function<void()>> m_notifyFunctions; |
104 | }; |
105 | |
106 | std::unique_ptr<AcceleratedBackingStoreX11> AcceleratedBackingStoreX11::create(WebPageProxy& webPage) |
107 | { |
108 | auto& display = downcast<PlatformDisplayX11>(PlatformDisplay::sharedDisplay()); |
109 | if (!display.supportsXComposite() || !display.supportsXDamage(s_damageEventBase, s_damageErrorBase)) |
110 | return nullptr; |
111 | return std::unique_ptr<AcceleratedBackingStoreX11>(new AcceleratedBackingStoreX11(webPage)); |
112 | } |
113 | |
114 | AcceleratedBackingStoreX11::AcceleratedBackingStoreX11(WebPageProxy& webPage) |
115 | : AcceleratedBackingStore(webPage) |
116 | { |
117 | } |
118 | |
119 | static inline unsigned char xDamageErrorCode(unsigned char errorCode) |
120 | { |
121 | ASSERT(s_damageErrorBase); |
122 | return static_cast<unsigned>(s_damageErrorBase.value()) + errorCode; |
123 | } |
124 | |
125 | AcceleratedBackingStoreX11::~AcceleratedBackingStoreX11() |
126 | { |
127 | if (!m_surface && !m_damage) |
128 | return; |
129 | |
130 | Display* display = downcast<PlatformDisplayX11>(PlatformDisplay::sharedDisplay()).native(); |
131 | XErrorTrapper trapper(display, XErrorTrapper::Policy::Crash, { BadDrawable, xDamageErrorCode(BadDamage) }); |
132 | if (m_damage) { |
133 | XDamageNotifier::singleton().remove(m_damage.get()); |
134 | m_damage.reset(); |
135 | XSync(display, False); |
136 | } |
137 | } |
138 | |
139 | void AcceleratedBackingStoreX11::update(const LayerTreeContext& layerTreeContext) |
140 | { |
141 | Pixmap pixmap = layerTreeContext.contextID; |
142 | if (m_surface && cairo_xlib_surface_get_drawable(m_surface.get()) == pixmap) |
143 | return; |
144 | |
145 | Display* display = downcast<PlatformDisplayX11>(PlatformDisplay::sharedDisplay()).native(); |
146 | |
147 | if (m_surface) { |
148 | XErrorTrapper trapper(display, XErrorTrapper::Policy::Crash, { BadDrawable, xDamageErrorCode(BadDamage) }); |
149 | if (m_damage) { |
150 | XDamageNotifier::singleton().remove(m_damage.get()); |
151 | m_damage.reset(); |
152 | XSync(display, False); |
153 | } |
154 | m_surface = nullptr; |
155 | } |
156 | |
157 | if (!pixmap) |
158 | return; |
159 | |
160 | auto* drawingArea = static_cast<DrawingAreaProxyCoordinatedGraphics*>(m_webPage.drawingArea()); |
161 | if (!drawingArea) |
162 | return; |
163 | |
164 | IntSize size = drawingArea->size(); |
165 | float deviceScaleFactor = m_webPage.deviceScaleFactor(); |
166 | size.scale(deviceScaleFactor); |
167 | |
168 | XErrorTrapper trapper(display, XErrorTrapper::Policy::Crash, { BadDrawable, xDamageErrorCode(BadDamage) }); |
169 | ASSERT(downcast<PlatformDisplayX11>(PlatformDisplay::sharedDisplay()).native() == GDK_DISPLAY_XDISPLAY(gdk_display_get_default())); |
170 | GdkVisual* visual = gdk_screen_get_rgba_visual(gdk_screen_get_default()); |
171 | if (!visual) |
172 | visual = gdk_screen_get_system_visual(gdk_screen_get_default()); |
173 | m_surface = adoptRef(cairo_xlib_surface_create(display, pixmap, GDK_VISUAL_XVISUAL(visual), size.width(), size.height())); |
174 | cairoSurfaceSetDeviceScale(m_surface.get(), deviceScaleFactor, deviceScaleFactor); |
175 | m_damage = XDamageCreate(display, pixmap, XDamageReportNonEmpty); |
176 | XDamageNotifier::singleton().add(m_damage.get(), [this] { |
177 | if (m_webPage.isViewVisible()) |
178 | gtk_widget_queue_draw(m_webPage.viewWidget()); |
179 | }); |
180 | XSync(display, False); |
181 | } |
182 | |
183 | bool AcceleratedBackingStoreX11::paint(cairo_t* cr, const IntRect& clipRect) |
184 | { |
185 | if (!m_surface) |
186 | return false; |
187 | |
188 | cairo_save(cr); |
189 | |
190 | // The surface can be modified by the web process at any time, so we mark it |
191 | // as dirty to ensure we always render the updated contents as soon as possible. |
192 | cairo_surface_mark_dirty(m_surface.get()); |
193 | cairo_rectangle(cr, clipRect.x(), clipRect.y(), clipRect.width(), clipRect.height()); |
194 | cairo_set_source_surface(cr, m_surface.get(), 0, 0); |
195 | cairo_set_operator(cr, CAIRO_OPERATOR_OVER); |
196 | cairo_fill(cr); |
197 | |
198 | cairo_restore(cr); |
199 | |
200 | return true; |
201 | } |
202 | |
203 | } // namespace WebKit |
204 | |
205 | #endif // PLATFORM(X11) |
206 | |