blob: 77f5cf1a0069519d494d9b3b6d812fa2ebd86d11 [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;
Chris Wilson0de19eb2009-02-21 15:22:06 -050051
Chris Wilson0de19eb2009-02-21 15:22:06 -050052 gchar *filename;
53};
54
55static void
56set_source_pixbuf(cairo_t *cr,
57 const GdkPixbuf *pixbuf,
58 double src_x,
59 double src_y,
60 double src_width,
61 double src_height)
62{
63 gint width = gdk_pixbuf_get_width (pixbuf);
64 gint height = gdk_pixbuf_get_height (pixbuf);
65 guchar *gdk_pixels = gdk_pixbuf_get_pixels (pixbuf);
66 int gdk_rowstride = gdk_pixbuf_get_rowstride (pixbuf);
67 int n_channels = gdk_pixbuf_get_n_channels (pixbuf);
68 int cairo_stride;
69 guchar *cairo_pixels;
70 cairo_format_t format;
71 cairo_surface_t *surface;
72 int j;
73
74 if (n_channels == 3)
75 format = CAIRO_FORMAT_RGB24;
76 else
77 format = CAIRO_FORMAT_ARGB32;
78
79 surface = cairo_image_surface_create(format, width, height);
80 if (cairo_surface_status(surface)) {
81 cairo_set_source_surface(cr, surface, 0, 0);
82 return;
83 }
84
85 cairo_stride = cairo_image_surface_get_stride(surface);
86 cairo_pixels = cairo_image_surface_get_data(surface);
87
88 for (j = height; j; j--) {
89 guchar *p = gdk_pixels;
90 guchar *q = cairo_pixels;
91
92 if (n_channels == 3) {
93 guchar *end = p + 3 * width;
94
95 while (p < end) {
96#if G_BYTE_ORDER == G_LITTLE_ENDIAN
97 q[0] = p[2];
98 q[1] = p[1];
99 q[2] = p[0];
100#else
101 q[1] = p[0];
102 q[2] = p[1];
103 q[3] = p[2];
104#endif
105 p += 3;
106 q += 4;
107 }
108 } else {
109 guchar *end = p + 4 * width;
110 guint t1,t2,t3;
111
112#define MULT(d,c,a,t) G_STMT_START { t = c * a + 0x7f; d = ((t >> 8) + t) >> 8; } G_STMT_END
113
114 while (p < end) {
115#if G_BYTE_ORDER == G_LITTLE_ENDIAN
116 MULT(q[0], p[2], p[3], t1);
117 MULT(q[1], p[1], p[3], t2);
118 MULT(q[2], p[0], p[3], t3);
119 q[3] = p[3];
120#else
121 q[0] = p[3];
122 MULT(q[1], p[0], p[3], t1);
123 MULT(q[2], p[1], p[3], t2);
124 MULT(q[3], p[2], p[3], t3);
125#endif
126
127 p += 4;
128 q += 4;
129 }
130#undef MULT
131 }
132
133 gdk_pixels += gdk_rowstride;
134 cairo_pixels += cairo_stride;
135 }
136 cairo_surface_mark_dirty(surface);
137
138 cairo_set_source_surface(cr, surface,
139 src_x + .5 * (src_width - width),
140 src_y + .5 * (src_height - height));
141 cairo_surface_destroy(surface);
142}
143
144static void
145image_draw(struct image *image)
146{
147 struct rectangle rectangle;
148 GdkPixbuf *pb;
149 cairo_t *cr;
Kristian Høgsberg09531622010-06-14 23:22:15 -0400150 cairo_surface_t *wsurface, *surface;
Chris Wilson0de19eb2009-02-21 15:22:06 -0500151
Kristian Høgsberg09531622010-06-14 23:22:15 -0400152 image->redraw_scheduled = 0;
Chris Wilson0de19eb2009-02-21 15:22:06 -0500153
154 window_draw(image->window);
155
156 window_get_child_rectangle(image->window, &rectangle);
157
158 pb = gdk_pixbuf_new_from_file_at_size(image->filename,
159 rectangle.width,
160 rectangle.height,
161 NULL);
162 if (pb == NULL)
163 return;
164
Kristian Høgsberg09531622010-06-14 23:22:15 -0400165 wsurface = window_get_surface(image->window);
166 surface = cairo_surface_create_similar(wsurface,
167 CAIRO_CONTENT_COLOR_ALPHA,
168 rectangle.width,
169 rectangle.height);
Chris Wilson0de19eb2009-02-21 15:22:06 -0500170
Kristian Høgsberg09531622010-06-14 23:22:15 -0400171 cr = cairo_create(surface);
Chris Wilson0de19eb2009-02-21 15:22:06 -0500172 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
173 cairo_set_source_rgba(cr, 0, 0, 0, 1);
174 cairo_paint(cr);
175 set_source_pixbuf(cr, pb,
176 0, 0,
177 rectangle.width, rectangle.height);
178 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
179 cairo_paint(cr);
180 cairo_destroy(cr);
181
182 g_object_unref(pb);
183
Kristian Høgsberg09531622010-06-14 23:22:15 -0400184 window_copy_surface(image->window, &rectangle, surface);
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500185 window_commit(image->window, image->key);
Kristian Høgsberg09531622010-06-14 23:22:15 -0400186 cairo_surface_destroy(surface);
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);
Chris Wilson0de19eb2009-02-21 15:22:06 -0500205 }
206}
207
208static void
209resize_handler(struct window *window, void *data)
210{
211 struct image *image = data;
212
213 image_schedule_redraw(image);
214}
215
216static void
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500217keyboard_focus_handler(struct window *window,
218 struct wl_input_device *device, void *data)
219{
220 struct image *image = data;
221
222 image_schedule_redraw(image);
223}
224
Chris Wilson0de19eb2009-02-21 15:22:06 -0500225static struct image *
226image_create(struct display *display, uint32_t key, const char *filename)
227{
228 struct image *image;
229 gchar *basename;
230 gchar *title;
231
232 image = malloc(sizeof *image);
233 if (image == NULL)
234 return image;
235 memset(image, 0, sizeof *image);
236
237 basename = g_path_get_basename(filename);
238 title = g_strdup_printf("Wayland Image - %s", basename);
239 g_free(basename);
240
241 image->filename = g_strdup(filename);
242
243 image->window = window_create(display, title, 100 * key, 100 * key, 500, 400);
244 image->display = display;
245
246 /* FIXME: Window uses key 1 for moves, need some kind of
247 * allocation scheme here. Or maybe just a real toolkit. */
248 image->key = key + 100;
Chris Wilson0de19eb2009-02-21 15:22:06 -0500249
Chris Wilson0de19eb2009-02-21 15:22:06 -0500250 window_set_resize_handler(image->window, resize_handler, image);
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500251 window_set_keyboard_focus_handler(image->window, keyboard_focus_handler, image);
Chris Wilson0de19eb2009-02-21 15:22:06 -0500252
253 image_draw(image);
254
255 return image;
256}
257
258static const GOptionEntry option_entries[] = {
259 { NULL }
260};
261
262int
263main(int argc, char *argv[])
264{
Chris Wilson0de19eb2009-02-21 15:22:06 -0500265 struct display *d;
Chris Wilson0de19eb2009-02-21 15:22:06 -0500266 int i;
267
Kristian Høgsberg7824d812010-06-08 14:59:44 -0400268 d = display_create(&argc, &argv, option_entries);
Chris Wilson0de19eb2009-02-21 15:22:06 -0500269
270 for (i = 1; i < argc; i++) {
271 struct image *image;
272
273 image = image_create (d, i, argv[i]);
274 }
275
Kristian Høgsberg7824d812010-06-08 14:59:44 -0400276 display_run(d);
Chris Wilson0de19eb2009-02-21 15:22:06 -0500277
278 return 0;
279}