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 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
|
/*
* gxr
* Copyright 2018 Collabora Ltd.
* Author: Lubosz Sarnecki <lubosz.sarnecki@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>
#include <vulkan/vulkan.h>
static GdkPixbuf *pixbuf;
static GulkanTexture *gk_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* rgb = gdk_pixbuf_get_pixels (pixbuf);
GulkanClient *client = gxr_context_get_gulkan (context);
gsize size;
int fd;
VkExtent2D extent = { width, height };
VkImageLayout layout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
gk_texture = gulkan_texture_new_export_fd (client, extent,
VK_FORMAT_R8G8B8A8_UNORM,
layout, &size, &fd);
g_print ("Mem size: %lu\n", size);
if (gk_texture == NULL)
{
g_printerr ("Unable to initialize vulkan dmabuf texture.\n");
return;
}
GLint gl_dedicated_mem = GL_TRUE;
GLuint gl_mem_object;
glCreateMemoryObjectsEXT (1, &gl_mem_object);
glMemoryObjectParameterivEXT (
gl_mem_object, GL_DEDICATED_MEMORY_OBJECT_EXT, &gl_dedicated_mem);
glMemoryObjectParameterivEXT (
gl_mem_object, GL_DEDICATED_MEMORY_OBJECT_EXT, &gl_dedicated_mem);
glGetMemoryObjectParameterivEXT (
gl_mem_object, GL_DEDICATED_MEMORY_OBJECT_EXT, &gl_dedicated_mem);
glImportMemoryFdEXT (gl_mem_object, size, GL_HANDLE_TYPE_OPAQUE_FD_EXT, fd);
glGenTextures (1, &gl_texture);
glActiveTexture (GL_TEXTURE0);
glBindTexture (GL_TEXTURE_2D, gl_texture);
glTexStorageMem2DEXT (
GL_TEXTURE_2D, 1, GL_RGBA8, width, height, gl_mem_object, 0);
/* don't need to keep this around */
glDeleteMemoryObjectsEXT (1, &gl_mem_object);
/* this does not seem to be necessary to get everything in linear memory but
* for now we leave it here */
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_TILING_EXT, GL_OPTIMAL_TILING_EXT);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexSubImage2D (GL_TEXTURE_2D, 0 ,0, 0, (GLsizei)width, (GLsizei)height,
GL_RGBA, GL_UNSIGNED_BYTE, (GLvoid*)rgb);
glFinish();
if (!gulkan_texture_transfer_layout (gk_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, gk_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 (gk_texture);
glDeleteTextures (1, &gl_texture);
overlay = NULL;
}
static gboolean
change_callback ()
{
if (!overlay)
return FALSE;
guint width = (guint)gdk_pixbuf_get_width (pixbuf);
guint height = (guint)gdk_pixbuf_get_height (pixbuf);
GdkPixbuf *original = pixbuf;
pixbuf = gdk_pixbuf_flip (pixbuf, TRUE);
g_object_unref (original);
guchar *rgb = gdk_pixbuf_get_pixels (pixbuf);
glTexSubImage2D (GL_TEXTURE_2D, 0 ,0, 0, (GLsizei)width, (GLsizei)height,
GL_RGBA, GL_UNSIGNED_BYTE, (GLvoid*)rgb);
gxr_overlay_submit_texture (overlay, gk_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 ();
g_timeout_add (250, 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);
GSList *instance_ext_list =
gulkan_client_get_external_memory_instance_extensions ();
GSList *device_ext_list =
gulkan_client_get_external_memory_device_extensions ();
context = gxr_context_new_from_vulkan_extensions (GXR_APP_OVERLAY,
instance_ext_list,
device_ext_list,
"Overlay External Memory",
1);
g_slist_free (instance_ext_list);
g_slist_free (device_ext_list);
if (!context)
return -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);
glfwWindowHint (GLFW_VISIBLE, GLFW_FALSE);
GLFWwindow *window = glfwCreateWindow(
640, 480, "GLFW OpenGL & Vulkan", 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 (gk_texture);
glDeleteTextures (1, &gl_texture);
g_object_unref (context);
return 0;
}
|