2 * Copyright (C) 2016 Igalia S.L.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
27 #include "AcceleratedBackingStoreWayland.h"
31 #include "WaylandCompositor.h"
32 #include "WebPageProxy.h"
33 #include <WebCore/CairoUtilities.h>
34 #include <WebCore/RefPtrCairo.h>
37 #include <GLES2/gl2.h>
39 #include <WebCore/OpenGLShims.h>
42 using namespace WebCore;
46 std::unique_ptr<AcceleratedBackingStoreWayland> AcceleratedBackingStoreWayland::create(WebPageProxy& webPage)
48 if (!WaylandCompositor::singleton().isRunning())
50 return std::unique_ptr<AcceleratedBackingStoreWayland>(new AcceleratedBackingStoreWayland(webPage));
53 AcceleratedBackingStoreWayland::AcceleratedBackingStoreWayland(WebPageProxy& webPage)
54 : AcceleratedBackingStore(webPage)
56 WaylandCompositor::singleton().registerWebPage(m_webPage);
59 AcceleratedBackingStoreWayland::~AcceleratedBackingStoreWayland()
61 WaylandCompositor::singleton().unregisterWebPage(m_webPage);
64 #if GTK_CHECK_VERSION(3, 16, 0)
65 bool AcceleratedBackingStoreWayland::canGdkUseGL() const
67 static bool initialized = false;
68 static bool canCreateGLContext = false;
71 return canCreateGLContext;
75 GUniqueOutPtr<GError> error;
76 GdkWindow* gdkWindow = gtk_widget_get_window(m_webPage.viewWidget());
77 GRefPtr<GdkGLContext> gdkContext(gdk_window_create_gl_context(gdkWindow, &error.outPtr()));
79 g_warning("GDK is not able to create a GL context, falling back to glReadPixels (slow!): %s", error->message);
83 canCreateGLContext = true;
89 bool AcceleratedBackingStoreWayland::paint(cairo_t* cr, const IntRect& clipRect)
93 if (!WaylandCompositor::singleton().getTexture(m_webPage, texture, textureSize))
97 AcceleratedBackingStore::paint(cr, clipRect);
99 #if GTK_CHECK_VERSION(3, 16, 0)
101 gdk_cairo_draw_from_gl(cr, gtk_widget_get_window(m_webPage.viewWidget()), texture, GL_TEXTURE, m_webPage.deviceScaleFactor(), 0, 0, textureSize.width(), textureSize.height());
107 if (!m_surface || cairo_image_surface_get_width(m_surface.get()) != textureSize.width() || cairo_image_surface_get_height(m_surface.get()) != textureSize.height())
108 m_surface = adoptRef(cairo_image_surface_create(CAIRO_FORMAT_ARGB32, textureSize.width(), textureSize.height()));
110 cairoSurfaceSetDeviceScale(m_surface.get(), m_webPage.deviceScaleFactor(), m_webPage.deviceScaleFactor());
113 glGenFramebuffers(1, &fb);
114 glBindFramebuffer(GL_FRAMEBUFFER, fb);
115 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture, 0);
117 glPixelStorei(GL_PACK_ALIGNMENT, 4);
120 unsigned char* data = cairo_image_surface_get_data(m_surface.get());
121 if (cairo_image_surface_get_stride(m_surface.get()) == textureSize.width() * 4)
122 glReadPixels(0, 0, textureSize.width(), textureSize.height(), GL_RGBA, GL_UNSIGNED_BYTE, data);
124 int strideBytes = cairo_image_surface_get_stride(m_surface.get());
125 for (int i = 0; i < textureSize.height(); i++) {
126 unsigned char* dataOffset = data + i * strideBytes;
127 glReadPixels(0, i, textureSize.width(), 1, GL_RGBA, GL_UNSIGNED_BYTE, dataOffset);
132 int totalBytes = textureSize.width() * textureSize.height() * 4;
133 for (int i = 0; i < totalBytes; i += 4)
134 std::swap(data[i], data[i + 2]);
136 glPixelStorei(GL_PACK_ROW_LENGTH, cairo_image_surface_get_stride(m_surface.get()) / 4);
137 glReadPixels(0, 0, textureSize.width(), textureSize.height(), GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, cairo_image_surface_get_data(m_surface.get()));
138 glPixelStorei(GL_PACK_ROW_LENGTH, 0);
141 glBindFramebuffer(GL_FRAMEBUFFER, 0);
142 glDeleteFramebuffers(1, &fb);
144 // The surface can be modified by the web process at any time, so we mark it
145 // as dirty to ensure we always render the updated contents as soon as possible.
146 cairo_surface_mark_dirty(m_surface.get());
148 // The compositor renders the texture flipped for gdk_cairo_draw_from_gl, fix that here.
149 cairo_matrix_t transform;
150 cairo_matrix_init(&transform, 1, 0, 0, -1, 0, textureSize.height() / m_webPage.deviceScaleFactor());
151 cairo_transform(cr, &transform);
153 cairo_rectangle(cr, clipRect.x(), clipRect.y(), clipRect.width(), clipRect.height());
154 cairo_set_source_surface(cr, m_surface.get(), 0, 0);
155 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
163 } // namespace WebKit
165 #endif // PLATFORM(WAYLAND)