blob: 8f30c4a49fea155c9f984bc8f4a9c0c36de3ac0f [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>
Chris Wilson0de19eb2009-02-21 15:22:06 -050033#include <glib.h>
34#include <gdk-pixbuf/gdk-pixbuf.h>
35
36#include "wayland-client.h"
37#include "wayland-glib.h"
38
39#include "window.h"
40
41static const char gem_device[] = "/dev/dri/card0";
42static const char socket_name[] = "\0wayland";
43
44struct image {
45 struct window *window;
46 struct display *display;
Chris Wilson0de19eb2009-02-21 15:22:06 -050047 uint32_t key;
Chris Wilson0de19eb2009-02-21 15:22:06 -050048 gchar *filename;
49};
50
51static void
52set_source_pixbuf(cairo_t *cr,
53 const GdkPixbuf *pixbuf,
54 double src_x,
55 double src_y,
56 double src_width,
57 double src_height)
58{
59 gint width = gdk_pixbuf_get_width (pixbuf);
60 gint height = gdk_pixbuf_get_height (pixbuf);
61 guchar *gdk_pixels = gdk_pixbuf_get_pixels (pixbuf);
62 int gdk_rowstride = gdk_pixbuf_get_rowstride (pixbuf);
63 int n_channels = gdk_pixbuf_get_n_channels (pixbuf);
64 int cairo_stride;
65 guchar *cairo_pixels;
66 cairo_format_t format;
67 cairo_surface_t *surface;
68 int j;
69
70 if (n_channels == 3)
71 format = CAIRO_FORMAT_RGB24;
72 else
73 format = CAIRO_FORMAT_ARGB32;
74
75 surface = cairo_image_surface_create(format, width, height);
76 if (cairo_surface_status(surface)) {
77 cairo_set_source_surface(cr, surface, 0, 0);
78 return;
79 }
80
81 cairo_stride = cairo_image_surface_get_stride(surface);
82 cairo_pixels = cairo_image_surface_get_data(surface);
83
84 for (j = height; j; j--) {
85 guchar *p = gdk_pixels;
86 guchar *q = cairo_pixels;
87
88 if (n_channels == 3) {
89 guchar *end = p + 3 * width;
90
91 while (p < end) {
92#if G_BYTE_ORDER == G_LITTLE_ENDIAN
93 q[0] = p[2];
94 q[1] = p[1];
95 q[2] = p[0];
96#else
97 q[1] = p[0];
98 q[2] = p[1];
99 q[3] = p[2];
100#endif
101 p += 3;
102 q += 4;
103 }
104 } else {
105 guchar *end = p + 4 * width;
106 guint t1,t2,t3;
107
108#define MULT(d,c,a,t) G_STMT_START { t = c * a + 0x7f; d = ((t >> 8) + t) >> 8; } G_STMT_END
109
110 while (p < end) {
111#if G_BYTE_ORDER == G_LITTLE_ENDIAN
112 MULT(q[0], p[2], p[3], t1);
113 MULT(q[1], p[1], p[3], t2);
114 MULT(q[2], p[0], p[3], t3);
115 q[3] = p[3];
116#else
117 q[0] = p[3];
118 MULT(q[1], p[0], p[3], t1);
119 MULT(q[2], p[1], p[3], t2);
120 MULT(q[3], p[2], p[3], t3);
121#endif
122
123 p += 4;
124 q += 4;
125 }
126#undef MULT
127 }
128
129 gdk_pixels += gdk_rowstride;
130 cairo_pixels += cairo_stride;
131 }
132 cairo_surface_mark_dirty(surface);
133
134 cairo_set_source_surface(cr, surface,
135 src_x + .5 * (src_width - width),
136 src_y + .5 * (src_height - height));
137 cairo_surface_destroy(surface);
138}
139
140static void
141image_draw(struct image *image)
142{
143 struct rectangle rectangle;
144 GdkPixbuf *pb;
145 cairo_t *cr;
Kristian Høgsberg09531622010-06-14 23:22:15 -0400146 cairo_surface_t *wsurface, *surface;
Chris Wilson0de19eb2009-02-21 15:22:06 -0500147
Chris Wilson0de19eb2009-02-21 15:22:06 -0500148 window_draw(image->window);
149
150 window_get_child_rectangle(image->window, &rectangle);
151
152 pb = gdk_pixbuf_new_from_file_at_size(image->filename,
153 rectangle.width,
154 rectangle.height,
155 NULL);
156 if (pb == NULL)
157 return;
158
Kristian Høgsberg09531622010-06-14 23:22:15 -0400159 wsurface = window_get_surface(image->window);
160 surface = cairo_surface_create_similar(wsurface,
161 CAIRO_CONTENT_COLOR_ALPHA,
162 rectangle.width,
163 rectangle.height);
Chris Wilson0de19eb2009-02-21 15:22:06 -0500164
Johan Bilien990854d2010-11-07 09:52:11 -0500165 cairo_surface_destroy(wsurface);
Kristian Høgsberg09531622010-06-14 23:22:15 -0400166 cr = cairo_create(surface);
Chris Wilson0de19eb2009-02-21 15:22:06 -0500167 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
168 cairo_set_source_rgba(cr, 0, 0, 0, 1);
169 cairo_paint(cr);
170 set_source_pixbuf(cr, pb,
171 0, 0,
172 rectangle.width, rectangle.height);
173 cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
174 cairo_paint(cr);
175 cairo_destroy(cr);
176
177 g_object_unref(pb);
178
Kristian Høgsberg09531622010-06-14 23:22:15 -0400179 window_copy_surface(image->window, &rectangle, surface);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -0400180 window_flush(image->window);
Kristian Høgsberg09531622010-06-14 23:22:15 -0400181 cairo_surface_destroy(surface);
Chris Wilson0de19eb2009-02-21 15:22:06 -0500182}
183
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400184static void
185redraw_handler(struct window *window, void *data)
Chris Wilson0de19eb2009-02-21 15:22:06 -0500186{
187 struct image *image = data;
188
189 image_draw(image);
Chris Wilson0de19eb2009-02-21 15:22:06 -0500190}
191
192static void
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500193keyboard_focus_handler(struct window *window,
Kristian Høgsberg43788b12010-07-28 23:50:12 -0400194 struct input *device, void *data)
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500195{
196 struct image *image = data;
197
Kristian Høgsberg80d746f2010-06-14 23:52:50 -0400198 window_schedule_redraw(image->window);
Kristian Høgsberga341fa02010-01-24 18:10:15 -0500199}
200
Chris Wilson0de19eb2009-02-21 15:22:06 -0500201static struct image *
202image_create(struct display *display, uint32_t key, const char *filename)
203{
204 struct image *image;
205 gchar *basename;
206 gchar *title;
207
208 image = malloc(sizeof *image);
209 if (image == NULL)
210 return image;
211 memset(image, 0, sizeof *image);
212
213 basename = g_path_get_basename(filename);
214 title = g_strdup_printf("Wayland Image - %s", basename);
215 g_free(basename);
216
217 image->filename = g_strdup(filename);
218
219 image->window = window_create(display, title, 100 * key, 100 * key, 500, 400);
220 image->display = display;
221
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;
Chris Wilson0de19eb2009-02-21 15:22:06 -0500225
Kristian Høgsbergc8c37342010-06-25 11:19:22 -0400226 window_set_user_data(image->window, image);
227 window_set_redraw_handler(image->window, redraw_handler);
Kristian Høgsberg43788b12010-07-28 23:50:12 -0400228 window_set_keyboard_focus_handler(image->window,
229 keyboard_focus_handler);
Chris Wilson0de19eb2009-02-21 15:22:06 -0500230
231 image_draw(image);
232
233 return image;
234}
235
236static const GOptionEntry option_entries[] = {
237 { NULL }
238};
239
240int
241main(int argc, char *argv[])
242{
Chris Wilson0de19eb2009-02-21 15:22:06 -0500243 struct display *d;
Chris Wilson0de19eb2009-02-21 15:22:06 -0500244 int i;
245
Kristian Høgsberg7824d812010-06-08 14:59:44 -0400246 d = display_create(&argc, &argv, option_entries);
Yuval Fledele9f5e362010-11-22 21:34:19 +0200247 if (d == NULL) {
248 fprintf(stderr, "failed to create display: %m\n");
249 return -1;
250 }
Chris Wilson0de19eb2009-02-21 15:22:06 -0500251
252 for (i = 1; i < argc; i++) {
253 struct image *image;
254
255 image = image_create (d, i, argv[i]);
256 }
257
Kristian Høgsberg7824d812010-06-08 14:59:44 -0400258 display_run(d);
Chris Wilson0de19eb2009-02-21 15:22:06 -0500259
260 return 0;
261}