1a6b79957224383dec93c777bbe252861d1200ce
[WebKit-https.git] / Tools / wpe / HeadlessViewBackend / HeadlessViewBackend.cpp
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 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 #include "HeadlessViewBackend.h"
27
28 #include <cassert>
29 #include <fcntl.h>
30 #include <unistd.h>
31 #include <wpe/fdo-egl.h>
32
33 // Manually provide the EGL_CAST C++ definition in case eglplatform.h doesn't provide it.
34 #ifndef EGL_CAST
35 #define EGL_CAST(type, value) (static_cast<type>(value))
36 #endif
37
38 // Keep this in sync with wtf/glib/RunLoopSourcePriority.h.
39 static int kRunLoopSourcePriorityDispatcher = -70;
40
41 static EGLDisplay getEGLDisplay()
42 {
43     static EGLDisplay s_display = EGL_NO_DISPLAY;
44     if (s_display == EGL_NO_DISPLAY) {
45         EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
46         if (display == EGL_NO_DISPLAY)
47             return EGL_NO_DISPLAY;
48
49         if (!eglInitialize(display, nullptr, nullptr))
50             return EGL_NO_DISPLAY;
51
52         if (!eglBindAPI(EGL_OPENGL_ES_API))
53             return EGL_NO_DISPLAY;
54
55         wpe_fdo_initialize_for_egl_display(display);
56         s_display = display;
57     }
58
59     return s_display;
60 }
61
62 HeadlessViewBackend::HeadlessViewBackend()
63 {
64     m_egl.display = getEGLDisplay();
65
66     static const EGLint configAttributes[13] = {
67         EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
68         EGL_RED_SIZE, 1,
69         EGL_GREEN_SIZE, 1,
70         EGL_BLUE_SIZE, 1,
71         EGL_ALPHA_SIZE, 1,
72         EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
73         EGL_NONE
74     };
75
76     EGLint numConfigs;
77     EGLBoolean ret = eglChooseConfig(m_egl.display, configAttributes, &m_egl.config, 1, &numConfigs);
78     if (!ret || !numConfigs)
79         return;
80
81     static const EGLint contextAttributes[3] = {
82         EGL_CONTEXT_CLIENT_VERSION, 2,
83         EGL_NONE
84     };
85
86     m_egl.context = eglCreateContext(m_egl.display, m_egl.config, EGL_NO_CONTEXT, contextAttributes);
87     if (m_egl.context == EGL_NO_CONTEXT)
88         return;
89
90     if (!eglMakeCurrent(m_egl.display, EGL_NO_SURFACE, EGL_NO_SURFACE, m_egl.context))
91         return;
92
93     m_egl.createImage = reinterpret_cast<PFNEGLCREATEIMAGEKHRPROC>(eglGetProcAddress("eglCreateImageKHR"));
94     m_egl.destroyImage = reinterpret_cast<PFNEGLDESTROYIMAGEKHRPROC>(eglGetProcAddress("eglDestroyImageKHR"));
95     m_egl.queryBuffer = reinterpret_cast<PFNEGLQUERYWAYLANDBUFFERWL>(eglGetProcAddress("eglQueryWaylandBufferWL"));
96     m_egl.imageTargetTexture2DOES = reinterpret_cast<PFNGLEGLIMAGETARGETTEXTURE2DOESPROC>(eglGetProcAddress("glEGLImageTargetTexture2DOES"));
97
98     m_exportable = wpe_view_backend_exportable_fdo_create(&s_exportableClient, this, 800, 600);
99
100     m_updateSource = g_timeout_source_new(m_frameRate / 1000);
101     g_source_set_callback(m_updateSource,
102         [](gpointer data) -> gboolean {
103             auto& backend = *static_cast<HeadlessViewBackend*>(data);
104             backend.performUpdate();
105             return TRUE;
106         }, this, nullptr);
107     g_source_set_priority(m_updateSource, kRunLoopSourcePriorityDispatcher);
108     g_source_attach(m_updateSource, g_main_context_default());
109 }
110
111 HeadlessViewBackend::~HeadlessViewBackend()
112 {
113     if (m_updateSource) {
114         g_source_destroy(m_updateSource);
115         g_source_unref(m_updateSource);
116     }
117
118     if (auto image = std::get<0>(m_pendingImage.second))
119         m_egl.destroyImage(m_egl.display, image);
120     if (auto image = std::get<0>(m_lockedImage.second))
121         m_egl.destroyImage(m_egl.display, image);
122
123     if (m_egl.context)
124         eglDestroyContext(m_egl.display, m_egl.context);
125
126     wpe_view_backend_exportable_fdo_destroy(m_exportable);
127 }
128
129 struct wpe_view_backend* HeadlessViewBackend::backend() const
130 {
131     return wpe_view_backend_exportable_fdo_get_view_backend(m_exportable);
132 }
133
134 cairo_surface_t* HeadlessViewBackend::createSnapshot()
135 {
136     performUpdate();
137
138     EGLImageKHR image = std::get<0>(m_lockedImage.second);
139     if (!image)
140         return nullptr;
141
142     uint32_t width = std::get<1>(m_lockedImage.second);
143     uint32_t height = std::get<2>(m_lockedImage.second);
144
145     uint8_t* buffer = new uint8_t[4 * width * height];
146     bool successfulSnapshot = false;
147
148     if (!eglMakeCurrent(m_egl.display, EGL_NO_SURFACE, EGL_NO_SURFACE, m_egl.context))
149         return nullptr;
150
151     GLuint imageTexture;
152     glGenTextures(1, &imageTexture);
153     glBindTexture(GL_TEXTURE_2D, imageTexture);
154     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
155     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
156     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
157     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
158     glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT, width, height, 0, GL_BGRA_EXT, GL_UNSIGNED_BYTE, nullptr);
159
160     m_egl.imageTargetTexture2DOES(GL_TEXTURE_2D, image);
161     glBindTexture(GL_TEXTURE_2D, 0);
162
163     GLuint imageFramebuffer;
164     glGenFramebuffers(1, &imageFramebuffer);
165     glBindFramebuffer(GL_FRAMEBUFFER, imageFramebuffer);
166     glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, imageTexture, 0);
167
168     glFlush();
169     if (glCheckFramebufferStatus(GL_FRAMEBUFFER) == GL_FRAMEBUFFER_COMPLETE) {
170         glReadPixels(0, 0, width, height, GL_BGRA_EXT, GL_UNSIGNED_BYTE, buffer);
171         successfulSnapshot = true;
172     }
173
174     glBindFramebuffer(GL_FRAMEBUFFER, 0);
175     glDeleteFramebuffers(1, &imageFramebuffer);
176     glDeleteTextures(1, &imageTexture);
177
178     if (!successfulSnapshot) {
179         delete[] buffer;
180         return nullptr;
181     }
182
183     cairo_surface_t* imageSurface = cairo_image_surface_create_for_data(buffer,
184         CAIRO_FORMAT_ARGB32, width, height, cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, width));
185     cairo_surface_mark_dirty(imageSurface);
186
187     static cairo_user_data_key_t bufferKey;
188     cairo_surface_set_user_data(imageSurface, &bufferKey, buffer,
189         [](void* data) {
190             auto* buffer = static_cast<uint8_t*>(data);
191             delete[] buffer;
192         });
193
194     return imageSurface;
195 }
196
197 void HeadlessViewBackend::performUpdate()
198 {
199     if (!m_pendingImage.first)
200         return;
201
202     wpe_view_backend_exportable_fdo_dispatch_frame_complete(m_exportable);
203     if (m_lockedImage.first) {
204         wpe_view_backend_exportable_fdo_dispatch_release_buffer(m_exportable, m_lockedImage.first);
205         m_egl.destroyImage(m_egl.display, std::get<0>(m_lockedImage.second));
206     }
207
208     m_lockedImage = m_pendingImage;
209     m_pendingImage = std::pair<struct wl_resource*, std::tuple<EGLImageKHR, uint32_t, uint32_t>> { };
210 }
211
212 struct wpe_view_backend_exportable_fdo_client HeadlessViewBackend::s_exportableClient = {
213     // export_buffer_resource
214     [](void* data, struct wl_resource* bufferResource)
215     {
216         auto& backend = *static_cast<HeadlessViewBackend*>(data);
217         if (backend.m_pendingImage.first)
218             std::abort();
219
220         auto& egl = backend.m_egl;
221
222         EGLint format = 0;
223         if (!egl.queryBuffer(egl.display, bufferResource, EGL_TEXTURE_FORMAT, &format) || format != EGL_TEXTURE_RGBA)
224             return;
225
226         EGLint width, height;
227         if (!egl.queryBuffer(egl.display, bufferResource, EGL_WIDTH, &width)
228             || !egl.queryBuffer(egl.display, bufferResource, EGL_HEIGHT, &height))
229             return;
230
231         EGLint attributes[] = { EGL_WAYLAND_PLANE_WL, 0, EGL_NONE };
232         EGLImageKHR image = egl.createImage(egl.display, EGL_NO_CONTEXT, EGL_WAYLAND_BUFFER_WL, bufferResource, attributes);
233         backend.m_pendingImage = { bufferResource, std::make_tuple(image, width, height) };
234     },
235     // padding
236     nullptr,
237     nullptr,
238     nullptr,
239     nullptr
240 };