From 78231c8dd0f16e2a64e16691b1137834062e78d9 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Kristian=20H=C3=B8gsberg?= Date: Sat, 8 Nov 2008 15:06:01 -0500 Subject: [PATCH] Implement surface copy request, use it for egl gears. --- egl-compositor.c | 28 ++++++-- gears.c | 13 +++- gears.h | 2 +- wayland.h | 1 - window.c | 192 +++++++++++++++++++++++++++++++++++++------------------ 5 files changed, 162 insertions(+), 74 deletions(-) diff --git a/egl-compositor.c b/egl-compositor.c index dbfd48d..b4c5630 100644 --- a/egl-compositor.c +++ b/egl-compositor.c @@ -149,8 +149,8 @@ notify_surface_attach(struct wl_compositor *compositor, /* FIXME: We need to use a single buffer config without depth * or stencil buffers here to keep egl from creating auxillary * buffers for the pixmap here. */ - sd->surface = eglCreatePixmapForName(ec->display, ec->config, - name, width, height, stride, NULL); + sd->surface = eglCreateSurfaceForName(ec->display, ec->config, + name, width, height, stride, NULL); glBindTexture(GL_TEXTURE_2D, sd->texture); glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); @@ -185,15 +185,29 @@ notify_surface_copy(struct wl_compositor *compositor, uint32_t name, uint32_t stride, int32_t x, int32_t y, int32_t width, int32_t height) { - /* FIXME: Eek, how do we do this... extend the DRI CopyBuffer - * extension and expose it in eagle? Make the surface the - * draw buffer and the named buffer the read buffer and use - * glCopyPixels? */ + struct egl_compositor *ec = (struct egl_compositor *) compositor; + EGLSurface src; + struct surface_data *sd; + + 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 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; diff --git a/gears.c b/gears.c index 9722eef..6179ac7 100644 --- a/gears.c +++ b/gears.c @@ -125,7 +125,8 @@ gear(GLfloat inner_radius, GLfloat outer_radius, GLfloat width, } 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 green[4] = {0.0, 0.8, 0.2, 1.0}; @@ -166,6 +167,14 @@ gears_create(void) glEnable(GL_LIGHT0); 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; } @@ -174,7 +183,7 @@ gears_draw(struct gears *gears, GLfloat angle) { 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(); diff --git a/gears.h b/gears.h index 76b8d63..7c69a35 100644 --- a/gears.h +++ b/gears.h @@ -3,7 +3,7 @@ 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); diff --git a/wayland.h b/wayland.h index c834aca..69128cb 100644 --- a/wayland.h +++ b/wayland.h @@ -139,7 +139,6 @@ struct wl_compositor_interface { struct wl_surface *surface, int32_t x, int32_t y, int32_t width, int32_t height); - }; void wl_display_set_compositor(struct wl_display *display, diff --git a/window.c b/window.c index 45d0306..b423f4c 100644 --- a/window.c +++ b/window.c @@ -27,65 +27,115 @@ static void die(const char *msg) 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_gem_flink flink; - struct drm_i915_gem_pwrite pwrite; - int32_t width, height, stride; - void *data; - width = cairo_image_surface_get_width(surface); - height = cairo_image_surface_get_height(surface); - stride = cairo_image_surface_get_stride(surface); - data = cairo_image_surface_get_data(surface); + 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"); - return 0; - } - - pwrite.handle = create.handle; - 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; + 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; } -#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. */ + 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 = create.handle; + + close.handle = buffer->handle; if (ioctl(fd, DRM_IOCTL_GEM_CLOSE, &close) < 0) { fprintf(stderr, "gem close failed: %m\n"); - return 0; + return -1; + } + + free(buffer); + + return 0; +} + +static int +buffer_data(struct buffer *buffer, int fd, void *data) +{ + 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; } -#endif - return flink.name; + return 0; +} + +static struct buffer * +buffer_create_from_cairo_surface(int fd, cairo_surface_t *surface) +{ + struct buffer *buffer; + int32_t width, height, stride; + void *data; + + width = cairo_image_surface_get_width(surface); + height = cairo_image_surface_get_height(surface); + stride = cairo_image_surface_get_stride(surface); + data = cairo_image_surface_get_data(surface); + + buffer = buffer_create(fd, width, height, stride); + if (buffer == NULL) + return NULL; + + if (buffer_data(buffer, fd, data) < 0) { + buffer_destroy(buffer, fd); + return NULL; + } + + return buffer; } struct window { 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 state; uint32_t name; int fd; int redraw_scheduled; + cairo_pattern_t *background; + + struct buffer *buffer; + struct buffer *egl_buffer; GLfloat gears_angle; struct gears *gears; @@ -104,11 +154,11 @@ draw_window(void *data) int border = 2, radius = 5, h; int margin = (border + 1) / 2; cairo_text_extents_t extents; + struct buffer *buffer; const static char title[] = "Wayland First Post"; surface = cairo_image_surface_create(CAIRO_FORMAT_RGB24, - window->width, - window->height); + window->width, window->height); cr = cairo_create(surface); cairo_set_line_width (cr, border); @@ -122,7 +172,7 @@ draw_window(void *data) window->height - margin); cairo_line_to(cr, margin, window->height - margin); 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_set_source_rgba(cr, 0, 0, 0, 1); cairo_set_font_size(cr, 14); @@ -137,39 +187,27 @@ draw_window(void *data) 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); - wl_surface_attach(window->surface, window->name, - window->width, window->height, window->stride); + wl_surface_attach(window->surface, buffer->name, + buffer->width, buffer->height, buffer->stride); - wl_surface_map(window->surface, - window->x, window->y, - window->width, window->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); + /* FIXME: Free window->buffer when we receive the ack event. */ - 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); + 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); - if (window->gears != NULL) - gears_draw(window->gears, window->gears_angle); + wl_surface_map(window->surface, + window->x, window->y, + buffer->width, buffer->height); window->redraw_scheduled = 0; @@ -215,6 +253,10 @@ void event_handler(struct wl_display *display, case WINDOW_RESIZING_LOWER_RIGHT: window->width = window->drag_x + arg1; window->height = window->drag_y + arg2; + if (window->width < 400) + window->width = 400; + if (window->height < 400) + window->height = 400; if (!window->redraw_scheduled) { window->redraw_scheduled = 1; g_idle_add(draw_window, window); @@ -264,11 +306,14 @@ window_create(struct wl_display *display, int fd) EGLint major, minor, count; EGLConfig configs[64]; struct window *window; + struct buffer *buffer; + const GLfloat red = 0.3, green = 0.3, blue = 0.3, alpha = 0.9; window = malloc(sizeof *window); if (window == NULL) return NULL; + memset(window, 0, sizeof *window); window->surface = wl_display_create_surface(display); window->x = 200; window->y = 200; @@ -276,7 +321,7 @@ window_create(struct wl_display *display, int fd) window->height = 500; window->state = WINDOW_STABLE; window->fd = fd; - window->gears = NULL; + window->background = cairo_pattern_create_rgba (red, green, blue, alpha); window->display = eglCreateDisplayNative("/dev/dri/card0", "i965"); if (window->display == NULL) @@ -293,13 +338,29 @@ window_create(struct wl_display *display, int fd) if (window->context == NULL) 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"); - draw_window(window); + if (!eglMakeCurrent(window->display, + window->egl_surface, window->egl_surface, window->context)) + die("failed to make context current\n"); - window->gears = gears_create(); + glViewport(0, 0, 300, 300); + + window->gears = gears_create(red, green, blue, alpha); window->gears_angle = 0.0; + draw_window(window); + return window; } @@ -307,10 +368,15 @@ static gboolean draw(gpointer data) { struct window *window = data; + struct buffer *buffer; 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; return TRUE; -- 2.7.4