blob: e38da0ffd5db61e65c03151a1a48cb9aa695366f [file] [log] [blame]
Chris Wilson0de19eb2009-02-21 15:22:06 -05001/*
2 * Copyright © 2008 Kristian Høgsberg
3 * Copyright © 2009 Chris Wilson
4 *
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.
14 *
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
21 * OF THIS SOFTWARE.
22 */
23
24#include <stdint.h>
25#include <stdio.h>
26#include <stdlib.h>
27#include <string.h>
28#include <fcntl.h>
29#include <unistd.h>
30#include <math.h>
31#include <time.h>
32#include <cairo.h>
33#include <cairo-drm.h>
34#include <glib.h>
35#include <gdk-pixbuf/gdk-pixbuf.h>
36
37#include "wayland-client.h"
38#include "wayland-glib.h"
39
40#include "window.h"
41
42static const char gem_device[] = "/dev/dri/card0";
43static const char socket_name[] = "\0wayland";
44
45struct image {
46 struct window *window;
47 struct display *display;
48 struct wl_compositor *compositor;
49 uint32_t key;
50
51 gboolean redraw_scheduled;
52 gboolean redraw_pending;
53
54 cairo_surface_t *surface;
55 gchar *filename;
56};
57
58static void
59set_source_pixbuf(cairo_t *cr,
60 const GdkPixbuf *pixbuf,
61 double src_x,
62 double src_y,
63 double src_width,
64 double src_height)
65{
66 gint width = gdk_pixbuf_get_width (pixbuf);
67 gint height = gdk_pixbuf_get_height (pixbuf);
68 guchar *gdk_pixels = gdk_pixbuf_get_pixels (pixbuf);
69 int gdk_rowstride = gdk_pixbuf_get_rowstride (pixbuf);
70 int n_channels = gdk_pixbuf_get_n_channels (pixbuf);
71 int cairo_stride;
72 guchar *cairo_pixels;
73 cairo_format_t format;
74 cairo_surface_t *surface;
75 int j;
76
77 if (n_channels == 3)
78 format = CAIRO_FORMAT_RGB24;
79 else
80 format = CAIRO_FORMAT_ARGB32;
81
82 surface = cairo_image_surface_create(format, width, height);
83 if (cairo_surface_status(surface)) {
84 cairo_set_source_surface(cr, surface, 0, 0);
85 return;
86 }
87
88 cairo_stride = cairo_image_surface_get_stride(surface);
89 cairo_pixels = cairo_image_surface_get_data(surface);
90
91 for (j = height; j; j--) {
92 guchar *p = gdk_pixels;
93 guchar *q = cairo_pixels;
94
95 if (n_channels == 3) {
96 guchar *end = p + 3 * width;
97
98 while (p < end) {
99#if G_BYTE_ORDER == G_LITTLE_ENDIAN
100 q[0] = p[2];
101 q[1] = p[1];
102 q[2] = p[0];
103#else
104 q[1] = p[0];
105 q[2] = p[1];
106 q[3] = p[2];
107#endif
108 p += 3;
109 q += 4;
110 }
111 } else {
112 guchar *end = p + 4 * width;
113 guint t1,t2,t3;
114
115#define MULT(d,c,a,t) G_STMT_START { t = c * a + 0x7f; d = ((t >> 8) + t) >> 8; } G_STMT_END
116
117 while (p < end) {
118#if G_BYTE_ORDER == G_LITTLE_ENDIAN
119 MULT(q[0], p[2], p[3], t1);
120 MULT(q[1], p[1], p[3], t2);
121 MULT(q[2], p[0], p[3], t3);
122 q[3] = p[3];
123#else
124 q[0] = p[3];
125 MULT(q[1], p[0], p[3], t1);
126 MULT(q[2], p[1], p[3], t2);
127 MULT(q[3], p[2], p[3], t3);
128#endif
129
130 p += 4;
131 q += 4;
132 }
133#undef MULT
134 }
135
136 gdk_pixels += gdk_rowstride;
137 cairo_pixels += cairo_stride;
138 }
139 cairo_surface_mark_dirty(surface);
140
141 cairo_set_source_surface(cr, surface,
142 src_x + .5 * (src_width - width),
143 src_y + .5 * (src_height - height));
144 cairo_surface_destroy(surface);
145}
146
147static void
148image_draw(struct image *image)
149{
150 struct rectangle rectangle;
151 GdkPixbuf *pb;
152 cairo_t *cr;
153
154 image->redraw_pending = 0;
155
156 window_draw(image->window);
157
158 window_get_child_rectangle(image->window, &rectangle);
159
160 pb = gdk_pixbuf_new_from_file_at_size(image->filename,
161 rectangle.width,
162 rectangle.height,
163 NULL);
164 if (pb == NULL)
165 return;
166
167 image->surface =
168 window_create_surface(image->window, &rectangle);
169
170 cr = cairo_create(image->surface);
171 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
172 cairo_set_source_rgba(cr, 0, 0, 0, 1);
173 cairo_paint(cr);
174 set_source_pixbuf(cr, pb,
175 0, 0,
176 rectangle.width, rectangle.height);
177 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
178 cairo_paint(cr);
179 cairo_destroy(cr);
180
181 g_object_unref(pb);
182
183 window_copy_surface(image->window,
184 &rectangle,
185 image->surface);
186
187 wl_compositor_commit(image->compositor, image->key);
188}
189
190static gboolean
191image_idle_redraw(void *data)
192{
193 struct image *image = data;
194
195 image_draw(image);
196
197 return FALSE;
198}
199
200static void
201image_schedule_redraw(struct image *image)
202{
203 if (!image->redraw_scheduled) {
204 image->redraw_scheduled = 1;
205 g_idle_add(image_idle_redraw, image);
206 } else {
207 image->redraw_pending = 1;
208 }
209}
210
211static void
212resize_handler(struct window *window, void *data)
213{
214 struct image *image = data;
215
216 image_schedule_redraw(image);
217}
218
219static void
220handle_acknowledge(void *data,
221 struct wl_compositor *compositor,
222 uint32_t key, uint32_t frame)
223{
224 struct image *image = data;
225
226 if (image->key != key)
227 return;
228
229 cairo_surface_destroy(image->surface);
230 image->redraw_scheduled = 0;
231 if (image->redraw_pending) {
232 image->redraw_pending = 0;
233 image_schedule_redraw(image);
234 }
235}
236
237static void
238handle_frame(void *data,
239 struct wl_compositor *compositor,
240 uint32_t frame, uint32_t timestamp)
241{
242}
243
244static const struct wl_compositor_listener compositor_listener = {
245 handle_acknowledge,
246 handle_frame,
247};
248
249static struct image *
250image_create(struct display *display, uint32_t key, const char *filename)
251{
252 struct image *image;
253 gchar *basename;
254 gchar *title;
255
256 image = malloc(sizeof *image);
257 if (image == NULL)
258 return image;
259 memset(image, 0, sizeof *image);
260
261 basename = g_path_get_basename(filename);
262 title = g_strdup_printf("Wayland Image - %s", basename);
263 g_free(basename);
264
265 image->filename = g_strdup(filename);
266
267 image->window = window_create(display, title, 100 * key, 100 * key, 500, 400);
268 image->display = display;
269
270 /* FIXME: Window uses key 1 for moves, need some kind of
271 * allocation scheme here. Or maybe just a real toolkit. */
272 image->key = key + 100;
273 image->redraw_scheduled = 1;
274
275 image->compositor = display_get_compositor(display);
276 window_set_resize_handler(image->window, resize_handler, image);
277
278 wl_compositor_add_listener(image->compositor, &compositor_listener, image);
279
280 image_draw(image);
281
282 return image;
283}
284
285static const GOptionEntry option_entries[] = {
286 { NULL }
287};
288
289int
290main(int argc, char *argv[])
291{
292 struct wl_display *display;
293 int fd;
294 GMainLoop *loop;
295 GSource *source;
296 struct display *d;
297 GOptionContext *context;
298 GError *error;
299 int i;
300
301 context = g_option_context_new(NULL);
302 g_option_context_add_main_entries(context, option_entries, "Wayland Image");
303 if (!g_option_context_parse(context, &argc, &argv, &error)) {
304 fprintf(stderr, "option parsing failed: %s\n", error->message);
305 exit(EXIT_FAILURE);
306 }
307
308 fd = open(gem_device, O_RDWR);
309 if (fd < 0) {
310 fprintf(stderr, "drm open failed: %m\n");
311 return -1;
312 }
313
314 display = wl_display_create(socket_name, sizeof socket_name);
315 if (display == NULL) {
316 fprintf(stderr, "failed to create display: %m\n");
317 return -1;
318 }
319
320 d = display_create(display, fd);
321 loop = g_main_loop_new(NULL, FALSE);
322 source = wl_glib_source_new(display);
323 g_source_attach(source, NULL);
324
325 g_type_init ();
326
327 for (i = 1; i < argc; i++) {
328 struct image *image;
329
330 image = image_create (d, i, argv[i]);
331 }
332
333 g_main_loop_run(loop);
334
335 return 0;
336}