2 * Copyright © 2008 Kristian Høgsberg
3 * Copyright © 2009 Chris Wilson
5 * Permission to use, copy, modify, distribute, and sell this software and its
6 * documentation for any purpose is hereby granted without fee, provided that
7 * the above copyright notice appear in all copies and that both that copyright
8 * notice and this permission notice appear in supporting documentation, and
9 * that the name of the copyright holders not be used in advertising or
10 * publicity pertaining to distribution of the software without specific,
11 * written prior permission. The copyright holders make no representations
12 * about the suitability of this software for any purpose. It is provided "as
13 * is" without express or implied warranty.
15 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
16 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
17 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
18 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
19 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
20 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
33 #include <cairo-drm.h>
35 #include <gdk-pixbuf/gdk-pixbuf.h>
37 #include "wayland-client.h"
38 #include "wayland-glib.h"
42 static const char gem_device[] = "/dev/dri/card0";
43 static const char socket_name[] = "\0wayland";
46 struct window *window;
47 struct display *display;
53 set_source_pixbuf(cairo_t *cr,
54 const GdkPixbuf *pixbuf,
60 gint width = gdk_pixbuf_get_width (pixbuf);
61 gint height = gdk_pixbuf_get_height (pixbuf);
62 guchar *gdk_pixels = gdk_pixbuf_get_pixels (pixbuf);
63 int gdk_rowstride = gdk_pixbuf_get_rowstride (pixbuf);
64 int n_channels = gdk_pixbuf_get_n_channels (pixbuf);
67 cairo_format_t format;
68 cairo_surface_t *surface;
72 format = CAIRO_FORMAT_RGB24;
74 format = CAIRO_FORMAT_ARGB32;
76 surface = cairo_image_surface_create(format, width, height);
77 if (cairo_surface_status(surface)) {
78 cairo_set_source_surface(cr, surface, 0, 0);
82 cairo_stride = cairo_image_surface_get_stride(surface);
83 cairo_pixels = cairo_image_surface_get_data(surface);
85 for (j = height; j; j--) {
86 guchar *p = gdk_pixels;
87 guchar *q = cairo_pixels;
89 if (n_channels == 3) {
90 guchar *end = p + 3 * width;
93 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
106 guchar *end = p + 4 * width;
109 #define MULT(d,c,a,t) G_STMT_START { t = c * a + 0x7f; d = ((t >> 8) + t) >> 8; } G_STMT_END
112 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
113 MULT(q[0], p[2], p[3], t1);
114 MULT(q[1], p[1], p[3], t2);
115 MULT(q[2], p[0], p[3], t3);
119 MULT(q[1], p[0], p[3], t1);
120 MULT(q[2], p[1], p[3], t2);
121 MULT(q[3], p[2], p[3], t3);
130 gdk_pixels += gdk_rowstride;
131 cairo_pixels += cairo_stride;
133 cairo_surface_mark_dirty(surface);
135 cairo_set_source_surface(cr, surface,
136 src_x + .5 * (src_width - width),
137 src_y + .5 * (src_height - height));
138 cairo_surface_destroy(surface);
142 image_draw(struct image *image)
144 struct rectangle rectangle;
147 cairo_surface_t *wsurface, *surface;
149 window_draw(image->window);
151 window_get_child_rectangle(image->window, &rectangle);
153 pb = gdk_pixbuf_new_from_file_at_size(image->filename,
160 wsurface = window_get_surface(image->window);
161 surface = cairo_surface_create_similar(wsurface,
162 CAIRO_CONTENT_COLOR_ALPHA,
166 cr = cairo_create(surface);
167 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
168 cairo_set_source_rgba(cr, 0, 0, 0, 1);
170 set_source_pixbuf(cr, pb,
172 rectangle.width, rectangle.height);
173 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
179 window_copy_surface(image->window, &rectangle, surface);
180 window_commit(image->window, image->key);
181 cairo_surface_destroy(surface);
185 redraw_handler(struct window *window, void *data)
187 struct image *image = data;
193 keyboard_focus_handler(struct window *window,
194 struct wl_input_device *device, void *data)
196 struct image *image = data;
198 window_schedule_redraw(image->window);
201 static struct image *
202 image_create(struct display *display, uint32_t key, const char *filename)
208 image = malloc(sizeof *image);
211 memset(image, 0, sizeof *image);
213 basename = g_path_get_basename(filename);
214 title = g_strdup_printf("Wayland Image - %s", basename);
217 image->filename = g_strdup(filename);
219 image->window = window_create(display, title, 100 * key, 100 * key, 500, 400);
220 image->display = display;
222 /* FIXME: Window uses key 1 for moves, need some kind of
223 * allocation scheme here. Or maybe just a real toolkit. */
224 image->key = key + 100;
226 window_set_user_data(image->window, image);
227 window_set_redraw_handler(image->window, redraw_handler);
228 window_set_keyboard_focus_handler(image->window, keyboard_focus_handler);
235 static const GOptionEntry option_entries[] = {
240 main(int argc, char *argv[])
245 d = display_create(&argc, &argv, option_entries);
247 for (i = 1; i < argc; i++) {
250 image = image_create (d, i, argv[i]);