~bl4ckb0ne/wxrc

b31849c2211d6b10fe5f4fcd52bcd3a6d8edfee4 — Simon Zeni 1 year, 4 months ago 33555fa
example/xr-buffer: dont init allocator in main_device event handler
3 files changed, 67 insertions(+), 50 deletions(-)

M example/cube.c
M example/xr-buffer.c
M example/xr-buffer.h
M example/cube.c => example/cube.c +3 -2
@@ 319,7 319,8 @@ registry_global(void *data, struct wl_registry *registry, uint32_t name,
	} else if (strcmp(interface, zwp_linux_dmabuf_v1_interface.name) == 0) {
		state->zwp_linux_dmabuf_v1 = wl_registry_bind(registry, name,
			&zwp_linux_dmabuf_v1_interface, 4);
		if (!xr_allocator_init(&state->allocator, state->zwp_linux_dmabuf_v1)) {
		if (!xr_allocator_init(&state->allocator, state->wl_display,
				state->zwp_linux_dmabuf_v1)) {
			state->zwp_linux_dmabuf_v1 = NULL;
		}
	}


@@ 650,7 651,7 @@ main(int argc, char **argv)

	render(&state, 0);

	while (wl_display_dispatch(state.wl_display) != -1);
	while (wl_display_dispatch(state.wl_display) != -1) {}

	TRACE_END;


M example/xr-buffer.c => example/xr-buffer.c +61 -47
@@ 1,3 1,5 @@
#define _POSIX_C_SOURCE 200809L

#include "xr-buffer.h"

#include <assert.h>


@@ 41,54 43,10 @@ feedback_main_device(void *data,
		assert(device->available_nodes & (1 << DRM_NODE_PRIMARY));
		name = device->nodes[DRM_NODE_PRIMARY];
	}
	printf("using DRM node %s\n", name);
	drmFreeDevice(&device);

	/* TODO find a better place to init the allocator */
	allocator->drm_fd = open(name, O_RDWR);
	assert(allocator->drm_fd >= 0);

	allocator->device = gbm_create_device(allocator->drm_fd);
	assert(allocator->device);

	allocator->dpy = eglGetPlatformDisplay(EGL_PLATFORM_GBM_KHR,
		allocator->device, NULL);
	assert(allocator->dpy);

	EGLint major, minor;
	if (!eglInitialize(allocator->dpy, &major, &minor)) {
		fprintf(stderr, "eglInitialize failed\n");
		return;
	}

	EGLint config_attribs[] = {
		EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
		EGL_RED_SIZE, 1,
		EGL_GREEN_SIZE, 1,
		EGL_BLUE_SIZE, 1,
		EGL_ALPHA_SIZE, 1,
		EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
		EGL_NONE,
	};
	EGLint n = 0;
	EGLConfig egl_config;
	eglChooseConfig(allocator->dpy, config_attribs, &egl_config, 1, &n);
	if (n == 0) {
		fprintf(stderr, "No suitable EGL config\n");
		return;
	}

	EGLint context_attribs[] = {
		EGL_CONTEXT_CLIENT_VERSION, 2,
		EGL_NONE,
	};
	allocator->ctx = eglCreateContext(allocator->dpy,
		egl_config, EGL_NO_CONTEXT, context_attribs);
	assert(allocator->ctx != EGL_NO_CONTEXT);
	allocator->drm_render_name = strdup(name);

	eglSwapInterval(allocator->dpy, 0);
	eglMakeCurrent(allocator->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
		allocator->ctx);
	drmFreeDevice(&device);
}

static void


@@ 139,7 97,7 @@ static const struct zwp_linux_dmabuf_feedback_v1_listener feedback_listener = {
};

bool
xr_allocator_init(struct xr_allocator *allocator,
xr_allocator_init(struct xr_allocator *allocator, struct wl_display *display,
		struct zwp_linux_dmabuf_v1 *linux_dmabuf)
{
	if (glEGLImageTargetTexture2DOES == NULL) {


@@ 157,6 115,62 @@ xr_allocator_init(struct xr_allocator *allocator,
	zwp_linux_dmabuf_feedback_v1_add_listener(feedback, &feedback_listener,
		allocator);

	/* Get linux-dmabuf infos */
	wl_display_roundtrip(display);

	if (allocator->drm_render_name == NULL) {
		fprintf(stderr, "no DRM render node available\n");
		return false;
	}

	printf("using DRM node %s\n", allocator->drm_render_name);

	allocator->drm_fd =
		open(allocator->drm_render_name, O_RDWR | O_NONBLOCK | O_CLOEXEC);
	assert(allocator->drm_fd >= 0);

	allocator->device = gbm_create_device(allocator->drm_fd);
	assert(allocator->device);

	allocator->dpy = eglGetPlatformDisplay(EGL_PLATFORM_GBM_KHR,
		allocator->device, NULL);
	assert(allocator->dpy);

	EGLint major, minor;
	if (!eglInitialize(allocator->dpy, &major, &minor)) {
		fprintf(stderr, "eglInitialize failed\n");
		return false;
	}

	EGLint config_attribs[] = {
		EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
		EGL_RED_SIZE, 1,
		EGL_GREEN_SIZE, 1,
		EGL_BLUE_SIZE, 1,
		EGL_ALPHA_SIZE, 1,
		EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
		EGL_NONE,
	};
	EGLint n = 0;
	EGLConfig egl_config;
	eglChooseConfig(allocator->dpy, config_attribs, &egl_config, 1, &n);
	if (n == 0) {
		fprintf(stderr, "No suitable EGL config\n");
		return false;
	}

	EGLint context_attribs[] = {
		EGL_CONTEXT_CLIENT_VERSION, 2,
		EGL_NONE,
	};
	allocator->ctx = eglCreateContext(allocator->dpy,
		egl_config, EGL_NO_CONTEXT, context_attribs);
	assert(allocator->ctx != EGL_NO_CONTEXT);

	eglSwapInterval(allocator->dpy, 0);
	eglMakeCurrent(allocator->dpy, EGL_NO_SURFACE, EGL_NO_SURFACE,
		allocator->ctx);

	return true;
}


M example/xr-buffer.h => example/xr-buffer.h +3 -1
@@ 7,12 7,14 @@
#include <gbm.h>
#include <stdbool.h>
#include <stdint.h>
#include <wayland-client.h>

#define MAX_BUFFER_PLANES 4

struct xr_allocator {
	struct zwp_linux_dmabuf_v1 *linux_dmabuf;

	char *drm_render_name;
	int drm_fd;
	struct gbm_device *device;



@@ 21,7 23,7 @@ struct xr_allocator {
};

bool xr_allocator_init(struct xr_allocator *allocator,
	struct zwp_linux_dmabuf_v1 *linux_dmabuf);
	struct wl_display *display, struct zwp_linux_dmabuf_v1 *linux_dmabuf);

struct xr_buffer {
	struct gbm_bo *bo;