Skip to content

Commit

Permalink
Add a new exportable interface that outputs EGL images
Browse files Browse the repository at this point in the history
This new interface is intended to make the backend easy to use by
EGL-based applications. A single callback 'export_egl_image' will
provide applications with an already constructed, ready to render
EGL image. A side benefit of this is that the actual protocol used
by the backend's nested compositor (wl_surface, linux-dmabuf, etc)
becomes transparent to the application.

This new interface is built conditionally by setting the
-DEXPORTABLE_EGL cmake option. It is ON by default.
  • Loading branch information
elima committed May 31, 2018
1 parent 1138dbf commit 462da6d
Show file tree
Hide file tree
Showing 4 changed files with 218 additions and 0 deletions.
7 changes: 7 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,13 @@ set(WPEBACKEND_FDO_SOURCES
src/ws.cpp
)

option(EXPORTABLE_EGL "Enable the exportable EGL interface" ON)

if (EXPORTABLE_EGL)
list(APPEND WPEBACKEND_FDO_PUBLIC_HEADERS include/wpe-fdo/view-backend-exportable-egl.h)
list(APPEND WPEBACKEND_FDO_SOURCES src/view-backend-exportable-fdo-egl.cpp)
endif ()

add_library(WPEBackend-fdo SHARED ${WPEBACKEND_FDO_SOURCES})
target_include_directories(WPEBackend-fdo PRIVATE ${WPEBACKEND_FDO_INCLUDE_DIRECTORIES})
target_link_libraries(WPEBackend-fdo ${WPEBACKEND_FDO_LIBRARIES})
Expand Down
1 change: 1 addition & 0 deletions include/wpe-fdo/fdo-egl.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#define __WPE_FDO_EGL_H_INSIDE__

#include <wpe/initialize-egl.h>
#include <wpe/view-backend-exportable-egl.h>

#undef __WPE_FDO_EGL_H_INSIDE__

Expand Down
61 changes: 61 additions & 0 deletions include/wpe-fdo/view-backend-exportable-egl.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Copyright (C) 2018 Igalia S.L.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#if !defined(__WPE_FDO_EGL_H_INSIDE__) && !defined(WPE_FDO_COMPILATION)
#error "Only <wpe/fdo-egl.h> can be included directly."
#endif

#ifndef __view_backend_exportable_egl_h__
#define __view_backend_exportable_egl_h__

#ifdef __cplusplus
extern "C" {
#endif

#include <wpe/wpe.h>

typedef void* EGLImageKHR;

struct wpe_view_backend_exportable_fdo;

struct wpe_view_backend_exportable_fdo_egl_client {
void (*export_egl_image)(void* data, EGLImageKHR image);
void (*_wpe_reserved0)(void);
void (*_wpe_reserved1)(void);
void (*_wpe_reserved2)(void);
void (*_wpe_reserved3)(void);
};

struct wpe_view_backend_exportable_fdo*
wpe_view_backend_exportable_fdo_egl_create(struct wpe_view_backend_exportable_fdo_egl_client*, void*, uint32_t width, uint32_t height);

void
wpe_view_backend_exportable_fdo_egl_dispatch_release_image(struct wpe_view_backend_exportable_fdo* exportable, EGLImageKHR image);

#ifdef __cplusplus
}
#endif

#endif /* __view_backend_exportable_egl_h___ */
149 changes: 149 additions & 0 deletions src/view-backend-exportable-fdo-egl.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
/*
* Copyright (C) 2018 Igalia S.L.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#include <assert.h>
#include "ws.h"
#include "view-backend-exportable-fdo.h"
#include "wpe-fdo/view-backend-exportable-egl.h"
#include <EGL/egl.h>
#include <EGL/eglext.h>
#include <list>

namespace {

struct buffer_data {
struct wl_resource *buffer_resource;
EGLImageKHR egl_image;
};

class ClientBundleEGL : public ClientBundleBase {
public:
ClientBundleEGL(struct wpe_view_backend_exportable_fdo_egl_client* _client, void* data,
ViewBackend* viewBackend, uint32_t initialWidth, uint32_t initialHeight)
: ClientBundleBase(data, viewBackend, initialWidth, initialHeight)
, client(_client)
{
m_eglDisplay = WS::Instance::singleton().getEGLDisplay();
assert (m_eglDisplay);

eglCreateImage = (PFNEGLCREATEIMAGEKHRPROC) eglGetProcAddress ("eglCreateImageKHR");
assert (eglCreateImage);

eglDestroyImage = (PFNEGLDESTROYIMAGEKHRPROC)
eglGetProcAddress ("eglDestroyImageKHR");
assert (eglDestroyImage);
}

~ClientBundleEGL()
{
for (auto* buf_data : m_buffers) {
assert(buf_data->egl_image);
eglDestroyImage(m_eglDisplay, buf_data->egl_image);

delete buf_data;
}
m_buffers.clear();
}

void exportBuffer(struct wl_resource *bufferResource) override
{
static const EGLint image_attrs[] = {
EGL_WAYLAND_PLANE_WL, 0,
EGL_NONE
};
EGLImageKHR image = eglCreateImage (m_eglDisplay,
EGL_NO_CONTEXT,
EGL_WAYLAND_BUFFER_WL,
bufferResource,
image_attrs);
if (!image)
return;

auto* buf_data = new struct buffer_data;
buf_data->buffer_resource = bufferResource;
buf_data->egl_image = image;
m_buffers.push_back(buf_data);

client->export_egl_image(data, image);
}

struct buffer_data* releaseImage(EGLImageKHR image)
{
for (auto* buf_data : m_buffers)
if (buf_data->egl_image == image) {
m_buffers.remove(buf_data);
eglDestroyImage(m_eglDisplay, buf_data->egl_image);

return buf_data;
}

return nullptr;
}

struct wpe_view_backend_exportable_fdo_egl_client* client;

private:
EGLDisplay m_eglDisplay;
PFNEGLCREATEIMAGEKHRPROC eglCreateImage;
PFNEGLDESTROYIMAGEKHRPROC eglDestroyImage;

std::list<struct buffer_data*> m_buffers;
};

} // namespace

extern "C" {

__attribute__((visibility("default")))
struct wpe_view_backend_exportable_fdo*
wpe_view_backend_exportable_fdo_egl_create(struct wpe_view_backend_exportable_fdo_egl_client* client, void* data, uint32_t width, uint32_t height)
{
auto* clientBundle = new ClientBundleEGL(client, data, nullptr, width, height);

return wpe_view_backend_exportable_fdo_new(clientBundle);
}

__attribute__((visibility("default")))
void
wpe_view_backend_exportable_fdo_egl_dispatch_release_image(struct wpe_view_backend_exportable_fdo* exportable, EGLImageKHR image)
{
auto* clientBundle = reinterpret_cast<ClientBundleEGL*>
(wpe_view_backend_exportable_fdo_get_client_bundle(exportable));

auto* buffer_data = clientBundle->releaseImage(image);
if (!buffer_data)
return;

/* the EGL image has already been destroyed by ClientBundleEGL */

if (buffer_data->buffer_resource) {
wpe_view_backend_exportable_fdo_dispatch_release_buffer(exportable,
buffer_data->buffer_resource);
}

delete buffer_data;
}

}

0 comments on commit 462da6d

Please sign in to comment.