blob: 4e4074e0bb9cb6cb0ad2b831367d34a76eb2b71d [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;
Chris Wilson0de19eb2009-02-21 15:22:06 -050048 uint32_t key;
49
50 gboolean redraw_scheduled;
51 gboolean redraw_pending;
52
53 cairo_surface_t *surface;
54 gchar *filename;
55};
56
57static void
58set_source_pixbuf(cairo_t *cr,
59 const GdkPixbuf *pixbuf,
60 double src_x,
61 double src_y,
62 double src_width,
63 double src_height)
64{
65 gint width = gdk_pixbuf_get_width (pixbuf);
66 gint height = gdk_pixbuf_get_height (pixbuf);
67 guchar *gdk_pixels = gdk_pixbuf_get_pixels (pixbuf);
68 int gdk_rowstride = gdk_pixbuf_get_rowstride (pixbuf);
69 int n_channels = gdk_pixbuf_get_n_channels (pixbuf);
70 int cairo_stride;
71 guchar *cairo_pixels;
72 cairo_format_t format;
73 cairo_surface_t *surface;
74 int j;
75
76 if (n_channels == 3)
77 format = CAIRO_FORMAT_RGB24;
78 else
79 format = CAIRO_FORMAT_ARGB32;
80
81 surface = cairo_image_surface_create(format, width, height);
82 if (cairo_surface_status(surface)) {
83 cairo_set_source_surface(cr, surface, 0, 0);
84 return;
85 }
86
87 cairo_stride = cairo_image_surface_get_stride(surface);
88 cairo_pixels = cairo_image_surface_get_data(surface);
89
90 for (j = height; j; j--) {
91 guchar *p = gdk_pixels;
92 guchar *q = cairo_pixels;
93
94 if (n_channels == 3) {
95 guchar *end = p + 3 * width;
96
97 while (p < end) {
98#if G_BYTE_ORDER == G_LITTLE_ENDIAN
99 q[0] = p[2];
100 q[1] = p[1];
101 q[2] = p[0];
102#else
103 q[1] = p[0];
104 q[2] = p[1];
105 q[3] = p[2];
106#endif
107 p += 3;
108 q += 4;
109 }
110 } else {
111 guchar *end = p + 4 * width;
112 guint t1,t2,t3;
113
114#define MULT(d,c,a,t) G_STMT_START { t = c * a + 0x7f; d = ((t >> 8) + t) >> 8; } G_STMT_END
115
116 while (p < end) {
117#if G_BYTE_ORDER == G_LITTLE_ENDIAN
118 MULT(q[0], p[2], p[3], t1);
119 MULT(q[1], p[1], p[3], t2);
120 MULT(q[2], p[0], p[3], t3);
121 q[3] = p[3];
122#else
123 q[0] = p[3];
124 MULT(q[1], p[0], p[3], t1);
125 MULT(q[2], p[1], p[3], t2);
126 MULT(q[3], p[2], p[3], t3);
127#endif
128
129 p += 4;
130 q += 4;
131 }
132#undef MULT
133 }
134
135 gdk_pixels += gdk_rowstride;
136 cairo_pixels += cairo_stride;
137 }
138 cairo_surface_mark_dirty(surface);
139
140 cairo_set_source_surface(cr, surface,
141 src_x + .5 * (src_width - width),
142 src_y + .5 * (src_height - height));
143 cairo_surface_destroy(surface);
144}
145
146static void
147image_draw(struct image *image)
148{
149 struct rectangle rectangle;
150 GdkPixbuf *pb;
151 cairo_t *cr;
152
153 image->redraw_pending = 0;
154
155 window_draw(image->window);
156
157 window_get_child_rectangle(image->window, &rectangle);
158
159 pb = gdk_pixbuf_new_from_file_at_size(image->filename,
160 rectangle.width,
161 rectangle.height,
162 NULL);
163 if (pb == NULL)
164 return;
165
166 image->surface =
167 window_create_surface(image->window, &rectangle);
168
169 cr = cairo_create(image->surface);
170 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
171 cairo_set_source_rgba(cr, 0, 0, 0, 1);
172 cairo_paint(cr);
173 set_source_pixbuf(cr, pb,
174 0, 0,
175 rectangle.width, rectangle.height);
176 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
177 cairo_paint(cr);
178 cairo_destroy(cr);
179
180 g_object_unref(pb);
181
182 window_copy_surface(image->window,
183 &rectangle,
184 image->surface);
185
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500186 window_commit(image->window, image->key);
Chris Wilson0de19eb2009-02-21 15:22:06 -0500187}
188
189static gboolean
190image_idle_redraw(void *data)
191{
192 struct image *image = data;
193
194 image_draw(image);
195
196 return FALSE;
197}
198
199static void
200image_schedule_redraw(struct image *image)
201{
202 if (!image->redraw_scheduled) {
203 image->redraw_scheduled = 1;
204 g_idle_add(image_idle_redraw, image);
205 } else {
206 image->redraw_pending = 1;
207 }
208}
209
210static void
211resize_handler(struct window *window, void *data)
212{
213 struct image *image = data;
214
215 image_schedule_redraw(image);
216}
217
218static void
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500219keyboard_focus_handler(struct window *window,
220 struct wl_input_device *device, void *data)
221{
222 struct image *image = data;
223
224 image_schedule_redraw(image);
225}
226
227static void
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400228acknowledge_handler(struct window *window,
229 uint32_t key, uint32_t frame, void *data)
Chris Wilson0de19eb2009-02-21 15:22:06 -0500230{
231 struct image *image = data;
232
233 if (image->key != key)
234 return;
235
236 cairo_surface_destroy(image->surface);
237 image->redraw_scheduled = 0;
238 if (image->redraw_pending) {
239 image->redraw_pending = 0;
240 image_schedule_redraw(image);
241 }
242}
243
Chris Wilson0de19eb2009-02-21 15:22:06 -0500244static struct image *
245image_create(struct display *display, uint32_t key, const char *filename)
246{
247 struct image *image;
248 gchar *basename;
249 gchar *title;
250
251 image = malloc(sizeof *image);
252 if (image == NULL)
253 return image;
254 memset(image, 0, sizeof *image);
255
256 basename = g_path_get_basename(filename);
257 title = g_strdup_printf("Wayland Image - %s", basename);
258 g_free(basename);
259
260 image->filename = g_strdup(filename);
261
262 image->window = window_create(display, title, 100 * key, 100 * key, 500, 400);
263 image->display = display;
264
265 /* FIXME: Window uses key 1 for moves, need some kind of
266 * allocation scheme here. Or maybe just a real toolkit. */
267 image->key = key + 100;
268 image->redraw_scheduled = 1;
269
Chris Wilson0de19eb2009-02-21 15:22:06 -0500270 window_set_resize_handler(image->window, resize_handler, image);
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500271 window_set_keyboard_focus_handler(image->window, keyboard_focus_handler, image);
Kristian Høgsberg478d9262010-06-08 20:34:11 -0400272 window_set_acknowledge_handler(image->window, acknowledge_handler, image);
Chris Wilson0de19eb2009-02-21 15:22:06 -0500273
274 image_draw(image);
275
276 return image;
277}
278
279static const GOptionEntry option_entries[] = {
280 { NULL }
281};
282
283int
284main(int argc, char *argv[])
285{
Chris Wilson0de19eb2009-02-21 15:22:06 -0500286 struct display *d;
Chris Wilson0de19eb2009-02-21 15:22:06 -0500287 int i;
288
Kristian Høgsberg7824d812010-06-08 14:59:44 -0400289 d = display_create(&argc, &argv, option_entries);
Chris Wilson0de19eb2009-02-21 15:22:06 -0500290
291 for (i = 1; i < argc; i++) {
292 struct image *image;
293
294 image = image_create (d, i, argv[i]);
295 }
296
Kristian Høgsberg7824d812010-06-08 14:59:44 -0400297 display_run(d);
Chris Wilson0de19eb2009-02-21 15:22:06 -0500298
299 return 0;
300}