1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
|
/*
* gxr
* Copyright 2018 Collabora Ltd.
* Author: Lubosz Sarnecki <lubosz.sarnecki@collabora.com>
* Author: Christoph Haag <christoph.haag@collabora.com>
* SPDX-License-Identifier: MIT
*/
#include <glib.h>
#include <gdk/gdk.h>
#include <fcntl.h>
#include "gxr.h"
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#define EGL_EGLEXT_PROTOTYPES
#include <EGL/egl.h>
#include <EGL/eglext.h>
static GdkPixbuf *pixbuf;
static GulkanTexture *texture;
static GxrOverlay *overlay;
static GLuint gl_texture;
static GxrContext *context;
static void
create_overlay ()
{
guint width = (guint)gdk_pixbuf_get_width (pixbuf);
guint height = (guint) gdk_pixbuf_get_height (pixbuf);
guchar* rgba = gdk_pixbuf_get_pixels (pixbuf);
GulkanClient *client = gxr_context_get_gulkan (context);
glGenTextures (1, &gl_texture);
glActiveTexture (GL_TEXTURE0);
glBindTexture (GL_TEXTURE_2D, gl_texture);
/* TODO: investigate why this does not make the buffer storage linear.
* Workaround: R600_DEBUG=notiling */
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_TILING_EXT, GL_LINEAR_TILING_EXT);
glTexImage2D (GL_TEXTURE_2D, 0, GL_RGBA8, (GLsizei)width, (GLsizei)height, 0,
GL_RGBA, GL_UNSIGNED_BYTE, (GLvoid*)rgba);
PFNEGLEXPORTDRMIMAGEMESAPROC _eglExportDMABUFImageMESA =
(PFNEGLEXPORTDMABUFIMAGEMESAPROC)
eglGetProcAddress ("eglExportDMABUFImageMESA");
if (!_eglExportDMABUFImageMESA)
{
g_printerr ("Unable to load function eglExportDMABUFImageMESA.");
return;
}
EGLDisplay eglDisplay = eglGetCurrentDisplay ();
EGLContext eglContext = eglGetCurrentContext ();
EGLImage egl_image = eglCreateImage (eglDisplay,
eglContext,
EGL_GL_TEXTURE_2D,
(EGLClientBuffer)(gulong)gl_texture,
NULL);
int fd;
if (!_eglExportDMABUFImageMESA (eglDisplay, egl_image, &fd, 0, 0)) {
g_printerr ("Unable to export DMABUF\n");
return;
}
eglDestroyImage (eglDisplay, egl_image);
VkExtent2D extent = { width, height };
texture = gulkan_texture_new_from_dmabuf (client, fd, extent,
VK_FORMAT_R8G8B8A8_UNORM);
if (texture == NULL)
{
g_printerr ("Unable to initialize vulkan dmabuf texture.\n");
return;
}
if (!gulkan_texture_transfer_layout (texture,
VK_IMAGE_LAYOUT_UNDEFINED,
VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL))
{
g_printerr ("Unable to transfer layout.\n");
}
overlay = gxr_overlay_new_width (context, "vulkan.dmabuf", 2.0);
if (overlay == NULL)
{
g_printerr ("Overlay unavailable.\n");
return;
}
graphene_matrix_t transform;
graphene_point3d_t pos =
{
.x = 0,
.y = 1,
.z = -2
};
graphene_matrix_init_translate (&transform, &pos);
gxr_overlay_set_transform_absolute (overlay, &transform);
gint64 start = g_get_monotonic_time ();
gxr_overlay_submit_texture (overlay, texture);
gint64 end = g_get_monotonic_time ();
g_print ("Submit frame took %f ms\n",
(double) (end - start) / (1000.));
gxr_overlay_show (overlay);
}
static void
destroy_overlay ()
{
g_object_unref (overlay);
g_object_unref (texture);
glDeleteTextures (1, &gl_texture);
overlay = NULL;
}
static gboolean
overlay_change_callback ()
{
if (!overlay)
return FALSE;
gxr_overlay_submit_texture (overlay, texture);
return TRUE;
}
static gboolean
timeout_callback ()
{
if (overlay)
{
gint64 start = g_get_monotonic_time ();
destroy_overlay ();
gint64 end = g_get_monotonic_time ();
g_print ("Destroy overlay took %f ms\n",
(double) (end - start) / (1000.));
}
else
{
gint64 start = g_get_monotonic_time ();
create_overlay ();
/* submit the same gulkan texture periodically every 100 ms
* possibly after having written some different data into its memory */
g_timeout_add (100, overlay_change_callback, NULL);
gint64 end = g_get_monotonic_time ();
g_print ("Create overlay took %f ms\n",
(double) (end - start) / (1000.));
}
return TRUE;
}
static GdkPixbuf *
load_gdk_pixbuf ()
{
GError *error = NULL;
GdkPixbuf * pixbuf_unflipped =
gdk_pixbuf_new_from_resource ("/res/cat.jpg", &error);
if (error != NULL) {
g_printerr ("Unable to read file: %s\n", error->message);
g_error_free (error);
return NULL;
} else {
GdkPixbuf *pb = gdk_pixbuf_add_alpha (pixbuf_unflipped, FALSE, 0, 0, 0);
g_object_unref (pixbuf_unflipped);
return pb;
}
}
int
main ()
{
GMainLoop *loop = g_main_loop_new (NULL, FALSE);
context = gxr_context_new (GXR_APP_OVERLAY, "Overlay Mesa Dma-Buf", 1);
pixbuf = load_gdk_pixbuf ();
if (pixbuf == NULL)
return -1;
if (!glfwInit ()) {
g_printerr ("Unable to initialize GLFW3\n");
return 1;
}
glfwWindowHint (GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint (GLFW_CONTEXT_VERSION_MINOR, 2);
glfwWindowHint (GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
/* eglExportDMABUFImageMESA requires EGL GL context */
glfwWindowHint (GLFW_CONTEXT_CREATION_API, GLFW_EGL_CONTEXT_API);
/* window doesn't do anything, just for OpenGL context */
glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE);
GLFWwindow *window = glfwCreateWindow (640, 480, "EGL window", NULL, NULL);
if (!window) {
g_printerr ("Unable to create window with GLFW3\n");
glfwTerminate ();
return 1;
}
glfwMakeContextCurrent (window);
glewInit ();
const GLubyte* renderer = glGetString (GL_RENDERER);
const GLubyte* version = glGetString (GL_VERSION);
g_print ("OpengL Renderer: %s\n", renderer);
g_print ("OpenGL version : %s\n", version);
create_overlay ();
g_timeout_add (1000, timeout_callback, NULL);
g_main_loop_run (loop);
g_main_loop_unref (loop);
g_print ("bye\n");
g_object_unref (overlay);
g_object_unref (texture);
g_object_unref (context);
return 0;
}
|