mirror of
https://gitlab.freedesktop.org/wayland/wayland.git
synced 2025-11-03 09:01:42 -05:00
Implement surface copy request, use it for egl gears.
This commit is contained in:
parent
1cbaa6abac
commit
78231c8dd0
5 changed files with 172 additions and 84 deletions
|
|
@ -149,7 +149,7 @@ notify_surface_attach(struct wl_compositor *compositor,
|
||||||
/* FIXME: We need to use a single buffer config without depth
|
/* FIXME: We need to use a single buffer config without depth
|
||||||
* or stencil buffers here to keep egl from creating auxillary
|
* or stencil buffers here to keep egl from creating auxillary
|
||||||
* buffers for the pixmap here. */
|
* buffers for the pixmap here. */
|
||||||
sd->surface = eglCreatePixmapForName(ec->display, ec->config,
|
sd->surface = eglCreateSurfaceForName(ec->display, ec->config,
|
||||||
name, width, height, stride, NULL);
|
name, width, height, stride, NULL);
|
||||||
|
|
||||||
glBindTexture(GL_TEXTURE_2D, sd->texture);
|
glBindTexture(GL_TEXTURE_2D, sd->texture);
|
||||||
|
|
@ -185,15 +185,29 @@ notify_surface_copy(struct wl_compositor *compositor,
|
||||||
uint32_t name, uint32_t stride,
|
uint32_t name, uint32_t stride,
|
||||||
int32_t x, int32_t y, int32_t width, int32_t height)
|
int32_t x, int32_t y, int32_t width, int32_t height)
|
||||||
{
|
{
|
||||||
/* FIXME: Eek, how do we do this... extend the DRI CopyBuffer
|
struct egl_compositor *ec = (struct egl_compositor *) compositor;
|
||||||
* extension and expose it in eagle? Make the surface the
|
EGLSurface src;
|
||||||
* draw buffer and the named buffer the read buffer and use
|
struct surface_data *sd;
|
||||||
* glCopyPixels? */
|
|
||||||
|
sd = wl_surface_get_data(surface);
|
||||||
|
|
||||||
|
/* FIXME: glCopyPixels should work, but then we'll have to
|
||||||
|
* call eglMakeCurrent to set up the src and dest surfaces
|
||||||
|
* first. This seems cheaper, but maybe there's a better way
|
||||||
|
* to accomplish this. */
|
||||||
|
|
||||||
|
src = eglCreateSurfaceForName(ec->display, ec->config,
|
||||||
|
name, x + width, y + height, stride, NULL);
|
||||||
|
|
||||||
|
eglCopyNativeBuffers(ec->display, sd->surface, GL_FRONT_LEFT, dst_x, dst_y,
|
||||||
|
src, GL_FRONT_LEFT, x, y, width, height);
|
||||||
|
schedule_repaint(ec);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
notify_surface_damage(struct wl_compositor *compositor,
|
notify_surface_damage(struct wl_compositor *compositor,
|
||||||
struct wl_surface *surface)
|
struct wl_surface *surface,
|
||||||
|
int32_t x, int32_t y, int32_t width, int32_t height)
|
||||||
{
|
{
|
||||||
struct egl_compositor *ec = (struct egl_compositor *) compositor;
|
struct egl_compositor *ec = (struct egl_compositor *) compositor;
|
||||||
|
|
||||||
|
|
|
||||||
13
gears.c
13
gears.c
|
|
@ -125,7 +125,8 @@ gear(GLfloat inner_radius, GLfloat outer_radius, GLfloat width,
|
||||||
}
|
}
|
||||||
|
|
||||||
struct gears *
|
struct gears *
|
||||||
gears_create(void)
|
gears_create(GLfloat clear_red, GLfloat clear_green,
|
||||||
|
GLfloat clear_blue, GLfloat clear_alpha)
|
||||||
{
|
{
|
||||||
static GLfloat red[4] = {0.8, 0.1, 0.0, 1.0};
|
static GLfloat red[4] = {0.8, 0.1, 0.0, 1.0};
|
||||||
static GLfloat green[4] = {0.0, 0.8, 0.2, 1.0};
|
static GLfloat green[4] = {0.0, 0.8, 0.2, 1.0};
|
||||||
|
|
@ -166,6 +167,14 @@ gears_create(void)
|
||||||
glEnable(GL_LIGHT0);
|
glEnable(GL_LIGHT0);
|
||||||
glEnable(GL_DEPTH_TEST);
|
glEnable(GL_DEPTH_TEST);
|
||||||
|
|
||||||
|
/* We're generating premultiplied alpha so multiply in the
|
||||||
|
* alpha. The gears are solid color and doesn't have
|
||||||
|
* anti-aliased edges, they're ok. */
|
||||||
|
glClearColor(clear_red * clear_alpha,
|
||||||
|
clear_green * clear_alpha,
|
||||||
|
clear_blue * clear_alpha,
|
||||||
|
clear_alpha);
|
||||||
|
|
||||||
return gears;
|
return gears;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -174,7 +183,7 @@ gears_draw(struct gears *gears, GLfloat angle)
|
||||||
{
|
{
|
||||||
GLfloat view_rotx = 20.0, view_roty = 30.0, view_rotz = 0.0;
|
GLfloat view_rotx = 20.0, view_roty = 30.0, view_rotz = 0.0;
|
||||||
|
|
||||||
glClear(GL_DEPTH_BUFFER_BIT);
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||||
|
|
||||||
glPushMatrix();
|
glPushMatrix();
|
||||||
|
|
||||||
|
|
|
||||||
2
gears.h
2
gears.h
|
|
@ -3,7 +3,7 @@
|
||||||
|
|
||||||
struct gears;
|
struct gears;
|
||||||
|
|
||||||
struct gears *gears_create(void);
|
struct gears *gears_create(GLfloat red, GLfloat green, GLfloat blue, GLfloat alpha);
|
||||||
|
|
||||||
void gears_draw(struct gears *gears, GLfloat angle);
|
void gears_draw(struct gears *gears, GLfloat angle);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -139,7 +139,6 @@ struct wl_compositor_interface {
|
||||||
struct wl_surface *surface,
|
struct wl_surface *surface,
|
||||||
int32_t x, int32_t y,
|
int32_t x, int32_t y,
|
||||||
int32_t width, int32_t height);
|
int32_t width, int32_t height);
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
void wl_display_set_compositor(struct wl_display *display,
|
void wl_display_set_compositor(struct wl_display *display,
|
||||||
|
|
|
||||||
210
window.c
210
window.c
|
|
@ -27,11 +27,83 @@ static void die(const char *msg)
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
|
||||||
static uint32_t name_cairo_surface(int fd, cairo_surface_t *surface)
|
struct buffer {
|
||||||
|
int width, height, stride;
|
||||||
|
uint32_t name, handle;
|
||||||
|
};
|
||||||
|
|
||||||
|
static struct buffer *
|
||||||
|
buffer_create(int fd, int width, int height, int stride)
|
||||||
{
|
{
|
||||||
|
struct buffer *buffer;
|
||||||
struct drm_i915_gem_create create;
|
struct drm_i915_gem_create create;
|
||||||
struct drm_gem_flink flink;
|
struct drm_gem_flink flink;
|
||||||
|
|
||||||
|
buffer = malloc(sizeof *buffer);
|
||||||
|
buffer->width = width;
|
||||||
|
buffer->height = height;
|
||||||
|
buffer->stride = stride;
|
||||||
|
|
||||||
|
memset(&create, 0, sizeof(create));
|
||||||
|
create.size = height * stride;
|
||||||
|
|
||||||
|
if (ioctl(fd, DRM_IOCTL_I915_GEM_CREATE, &create) != 0) {
|
||||||
|
fprintf(stderr, "gem create failed: %m\n");
|
||||||
|
free(buffer);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
flink.handle = create.handle;
|
||||||
|
if (ioctl(fd, DRM_IOCTL_GEM_FLINK, &flink) != 0) {
|
||||||
|
fprintf(stderr, "gem flink failed: %m\n");
|
||||||
|
free(buffer);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
buffer->handle = flink.handle;
|
||||||
|
buffer->name = flink.name;
|
||||||
|
|
||||||
|
return buffer;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int
|
||||||
|
buffer_destroy(struct buffer *buffer, int fd)
|
||||||
|
{
|
||||||
|
struct drm_gem_close close;
|
||||||
|
|
||||||
|
close.handle = buffer->handle;
|
||||||
|
if (ioctl(fd, DRM_IOCTL_GEM_CLOSE, &close) < 0) {
|
||||||
|
fprintf(stderr, "gem close failed: %m\n");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
free(buffer);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int
|
||||||
|
buffer_data(struct buffer *buffer, int fd, void *data)
|
||||||
|
{
|
||||||
struct drm_i915_gem_pwrite pwrite;
|
struct drm_i915_gem_pwrite pwrite;
|
||||||
|
|
||||||
|
pwrite.handle = buffer->handle;
|
||||||
|
pwrite.offset = 0;
|
||||||
|
pwrite.size = buffer->height * buffer->stride;
|
||||||
|
pwrite.data_ptr = (uint64_t) (uintptr_t) data;
|
||||||
|
|
||||||
|
if (ioctl(fd, DRM_IOCTL_I915_GEM_PWRITE, &pwrite) < 0) {
|
||||||
|
fprintf(stderr, "gem pwrite failed: %m\n");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static struct buffer *
|
||||||
|
buffer_create_from_cairo_surface(int fd, cairo_surface_t *surface)
|
||||||
|
{
|
||||||
|
struct buffer *buffer;
|
||||||
int32_t width, height, stride;
|
int32_t width, height, stride;
|
||||||
void *data;
|
void *data;
|
||||||
|
|
||||||
|
|
@ -40,52 +112,30 @@ static uint32_t name_cairo_surface(int fd, cairo_surface_t *surface)
|
||||||
stride = cairo_image_surface_get_stride(surface);
|
stride = cairo_image_surface_get_stride(surface);
|
||||||
data = cairo_image_surface_get_data(surface);
|
data = cairo_image_surface_get_data(surface);
|
||||||
|
|
||||||
memset(&create, 0, sizeof(create));
|
buffer = buffer_create(fd, width, height, stride);
|
||||||
create.size = height * stride;
|
if (buffer == NULL)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
if (ioctl(fd, DRM_IOCTL_I915_GEM_CREATE, &create) != 0) {
|
if (buffer_data(buffer, fd, data) < 0) {
|
||||||
fprintf(stderr, "gem create failed: %m\n");
|
buffer_destroy(buffer, fd);
|
||||||
return 0;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
pwrite.handle = create.handle;
|
return buffer;
|
||||||
pwrite.offset = 0;
|
|
||||||
pwrite.size = height * stride;
|
|
||||||
pwrite.data_ptr = (uint64_t) (uintptr_t) data;
|
|
||||||
if (ioctl(fd, DRM_IOCTL_I915_GEM_PWRITE, &pwrite) < 0) {
|
|
||||||
fprintf(stderr, "gem pwrite failed: %m\n");
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
flink.handle = create.handle;
|
|
||||||
if (ioctl(fd, DRM_IOCTL_GEM_FLINK, &flink) != 0) {
|
|
||||||
fprintf(stderr, "gem flink failed: %m\n");
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
#if 0
|
|
||||||
/* We need to hold on to the handle until the server has received
|
|
||||||
* the attach request... we probably need a confirmation event.
|
|
||||||
* I guess the breadcrumb idea will suffice. */
|
|
||||||
struct drm_gem_close close;
|
|
||||||
close.handle = create.handle;
|
|
||||||
if (ioctl(fd, DRM_IOCTL_GEM_CLOSE, &close) < 0) {
|
|
||||||
fprintf(stderr, "gem close failed: %m\n");
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
return flink.name;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
struct window {
|
struct window {
|
||||||
struct wl_surface *surface;
|
struct wl_surface *surface;
|
||||||
int x, y, width, height, stride;
|
int x, y, width, height;
|
||||||
int drag_x, drag_y, last_x, last_y;
|
int drag_x, drag_y, last_x, last_y;
|
||||||
int state;
|
int state;
|
||||||
uint32_t name;
|
uint32_t name;
|
||||||
int fd;
|
int fd;
|
||||||
int redraw_scheduled;
|
int redraw_scheduled;
|
||||||
|
cairo_pattern_t *background;
|
||||||
|
|
||||||
|
struct buffer *buffer;
|
||||||
|
struct buffer *egl_buffer;
|
||||||
|
|
||||||
GLfloat gears_angle;
|
GLfloat gears_angle;
|
||||||
struct gears *gears;
|
struct gears *gears;
|
||||||
|
|
@ -104,11 +154,11 @@ draw_window(void *data)
|
||||||
int border = 2, radius = 5, h;
|
int border = 2, radius = 5, h;
|
||||||
int margin = (border + 1) / 2;
|
int margin = (border + 1) / 2;
|
||||||
cairo_text_extents_t extents;
|
cairo_text_extents_t extents;
|
||||||
|
struct buffer *buffer;
|
||||||
const static char title[] = "Wayland First Post";
|
const static char title[] = "Wayland First Post";
|
||||||
|
|
||||||
surface = cairo_image_surface_create(CAIRO_FORMAT_RGB24,
|
surface = cairo_image_surface_create(CAIRO_FORMAT_RGB24,
|
||||||
window->width,
|
window->width, window->height);
|
||||||
window->height);
|
|
||||||
|
|
||||||
cr = cairo_create(surface);
|
cr = cairo_create(surface);
|
||||||
cairo_set_line_width (cr, border);
|
cairo_set_line_width (cr, border);
|
||||||
|
|
@ -122,7 +172,7 @@ draw_window(void *data)
|
||||||
window->height - margin);
|
window->height - margin);
|
||||||
cairo_line_to(cr, margin, window->height - margin);
|
cairo_line_to(cr, margin, window->height - margin);
|
||||||
cairo_close_path(cr);
|
cairo_close_path(cr);
|
||||||
cairo_set_source_rgba(cr, 0.2, 0.2, 0.2, 0.9);
|
cairo_set_source(cr, window->background);
|
||||||
cairo_fill_preserve(cr);
|
cairo_fill_preserve(cr);
|
||||||
cairo_set_source_rgba(cr, 0, 0, 0, 1);
|
cairo_set_source_rgba(cr, 0, 0, 0, 1);
|
||||||
cairo_set_font_size(cr, 14);
|
cairo_set_font_size(cr, 14);
|
||||||
|
|
@ -137,39 +187,27 @@ draw_window(void *data)
|
||||||
|
|
||||||
cairo_destroy(cr);
|
cairo_destroy(cr);
|
||||||
|
|
||||||
window->stride = cairo_image_surface_get_stride(surface);
|
if (window->buffer != NULL)
|
||||||
|
buffer_destroy(window->buffer, window->fd);
|
||||||
|
buffer = buffer_create_from_cairo_surface(window->fd, surface);
|
||||||
|
window->buffer = buffer;
|
||||||
|
|
||||||
window->name = name_cairo_surface(window->fd, surface);
|
|
||||||
cairo_surface_destroy(surface);
|
cairo_surface_destroy(surface);
|
||||||
|
|
||||||
wl_surface_attach(window->surface, window->name,
|
wl_surface_attach(window->surface, buffer->name,
|
||||||
window->width, window->height, window->stride);
|
buffer->width, buffer->height, buffer->stride);
|
||||||
|
|
||||||
|
/* FIXME: Free window->buffer when we receive the ack event. */
|
||||||
|
|
||||||
|
buffer = window->egl_buffer;
|
||||||
|
gears_draw(window->gears, window->gears_angle);
|
||||||
|
wl_surface_copy(window->surface, 20, 50,
|
||||||
|
buffer->name, buffer->stride,
|
||||||
|
0, 0, buffer->width, buffer->height);
|
||||||
|
|
||||||
wl_surface_map(window->surface,
|
wl_surface_map(window->surface,
|
||||||
window->x, window->y,
|
window->x, window->y,
|
||||||
window->width, window->height);
|
buffer->width, buffer->height);
|
||||||
|
|
||||||
if (window->egl_surface != EGL_NO_SURFACE)
|
|
||||||
eglDestroySurface(window->display, window->egl_surface);
|
|
||||||
|
|
||||||
/* FIXME: We need to get the stride right here in a chipset
|
|
||||||
* independent way. Maybe do it in name_cairo_surface(). */
|
|
||||||
window->egl_surface = eglCreatePixmapForName(window->display,
|
|
||||||
window->config, window->name,
|
|
||||||
window->width, window->height,
|
|
||||||
window->stride, NULL);
|
|
||||||
|
|
||||||
if (surface == NULL)
|
|
||||||
die("failed to create surface\n");
|
|
||||||
|
|
||||||
if (!eglMakeCurrent(window->display,
|
|
||||||
window->egl_surface, window->egl_surface, window->context))
|
|
||||||
die("failed to make context current\n");
|
|
||||||
|
|
||||||
glViewport(border, window->height - h - margin - 300, 300, 300);
|
|
||||||
|
|
||||||
if (window->gears != NULL)
|
|
||||||
gears_draw(window->gears, window->gears_angle);
|
|
||||||
|
|
||||||
window->redraw_scheduled = 0;
|
window->redraw_scheduled = 0;
|
||||||
|
|
||||||
|
|
@ -215,6 +253,10 @@ void event_handler(struct wl_display *display,
|
||||||
case WINDOW_RESIZING_LOWER_RIGHT:
|
case WINDOW_RESIZING_LOWER_RIGHT:
|
||||||
window->width = window->drag_x + arg1;
|
window->width = window->drag_x + arg1;
|
||||||
window->height = window->drag_y + arg2;
|
window->height = window->drag_y + arg2;
|
||||||
|
if (window->width < 400)
|
||||||
|
window->width = 400;
|
||||||
|
if (window->height < 400)
|
||||||
|
window->height = 400;
|
||||||
if (!window->redraw_scheduled) {
|
if (!window->redraw_scheduled) {
|
||||||
window->redraw_scheduled = 1;
|
window->redraw_scheduled = 1;
|
||||||
g_idle_add(draw_window, window);
|
g_idle_add(draw_window, window);
|
||||||
|
|
@ -264,11 +306,14 @@ window_create(struct wl_display *display, int fd)
|
||||||
EGLint major, minor, count;
|
EGLint major, minor, count;
|
||||||
EGLConfig configs[64];
|
EGLConfig configs[64];
|
||||||
struct window *window;
|
struct window *window;
|
||||||
|
struct buffer *buffer;
|
||||||
|
const GLfloat red = 0.3, green = 0.3, blue = 0.3, alpha = 0.9;
|
||||||
|
|
||||||
window = malloc(sizeof *window);
|
window = malloc(sizeof *window);
|
||||||
if (window == NULL)
|
if (window == NULL)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
|
memset(window, 0, sizeof *window);
|
||||||
window->surface = wl_display_create_surface(display);
|
window->surface = wl_display_create_surface(display);
|
||||||
window->x = 200;
|
window->x = 200;
|
||||||
window->y = 200;
|
window->y = 200;
|
||||||
|
|
@ -276,7 +321,7 @@ window_create(struct wl_display *display, int fd)
|
||||||
window->height = 500;
|
window->height = 500;
|
||||||
window->state = WINDOW_STABLE;
|
window->state = WINDOW_STABLE;
|
||||||
window->fd = fd;
|
window->fd = fd;
|
||||||
window->gears = NULL;
|
window->background = cairo_pattern_create_rgba (red, green, blue, alpha);
|
||||||
|
|
||||||
window->display = eglCreateDisplayNative("/dev/dri/card0", "i965");
|
window->display = eglCreateDisplayNative("/dev/dri/card0", "i965");
|
||||||
if (window->display == NULL)
|
if (window->display == NULL)
|
||||||
|
|
@ -293,13 +338,29 @@ window_create(struct wl_display *display, int fd)
|
||||||
if (window->context == NULL)
|
if (window->context == NULL)
|
||||||
die("failed to create context\n");
|
die("failed to create context\n");
|
||||||
|
|
||||||
window->egl_surface = EGL_NO_SURFACE;
|
/* FIXME: We need to get the stride right here in a chipset
|
||||||
|
* independent way. Maybe do it in name_cairo_surface(). */
|
||||||
|
buffer = buffer_create(window->fd, 300, 300, (300 * 4 + 15) & ~15);
|
||||||
|
window->egl_buffer = buffer;
|
||||||
|
window->egl_surface = eglCreateSurfaceForName(window->display,
|
||||||
|
window->config, buffer->name,
|
||||||
|
buffer->width, buffer->height,
|
||||||
|
buffer->stride, NULL);
|
||||||
|
|
||||||
|
if (window->egl_surface == NULL)
|
||||||
|
die("failed to create egl surface\n");
|
||||||
|
|
||||||
|
if (!eglMakeCurrent(window->display,
|
||||||
|
window->egl_surface, window->egl_surface, window->context))
|
||||||
|
die("failed to make context current\n");
|
||||||
|
|
||||||
|
glViewport(0, 0, 300, 300);
|
||||||
|
|
||||||
|
window->gears = gears_create(red, green, blue, alpha);
|
||||||
|
window->gears_angle = 0.0;
|
||||||
|
|
||||||
draw_window(window);
|
draw_window(window);
|
||||||
|
|
||||||
window->gears = gears_create();
|
|
||||||
window->gears_angle = 0.0;
|
|
||||||
|
|
||||||
return window;
|
return window;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -307,10 +368,15 @@ static gboolean
|
||||||
draw(gpointer data)
|
draw(gpointer data)
|
||||||
{
|
{
|
||||||
struct window *window = data;
|
struct window *window = data;
|
||||||
|
struct buffer *buffer;
|
||||||
|
|
||||||
gears_draw(window->gears, window->gears_angle);
|
gears_draw(window->gears, window->gears_angle);
|
||||||
wl_surface_damage(window->surface, 0, 0,
|
|
||||||
window->width, window->height);
|
buffer = window->egl_buffer;
|
||||||
|
wl_surface_copy(window->surface, 20, 50,
|
||||||
|
buffer->name, buffer->stride,
|
||||||
|
0, 0, buffer->width, buffer->height);
|
||||||
|
|
||||||
window->gears_angle += 1;
|
window->gears_angle += 1;
|
||||||
|
|
||||||
return TRUE;
|
return TRUE;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue