diff options
author | 2018-03-28 15:25:33 -0400 | |
---|---|---|
committer | 2018-03-28 15:25:33 -0400 | |
commit | 9070950eecded7bfa64e7bca3bb76b150ccc8b72 (patch) | |
tree | 509a9c669bf2679085e27a1ff1b0c95526abf14c /wayland/cairo.c | |
parent | Merge pull request #1641 from swaywm/transformed-events (diff) | |
parent | Address review comments (diff) | |
download | sway-9070950eecded7bfa64e7bca3bb76b150ccc8b72.tar.gz sway-9070950eecded7bfa64e7bca3bb76b150ccc8b72.tar.zst sway-9070950eecded7bfa64e7bca3bb76b150ccc8b72.zip |
Merge pull request #1638 from swaywm/swaybg-layers
Reimplement swaybg using surface layers
Diffstat (limited to 'wayland/cairo.c')
-rw-r--r-- | wayland/cairo.c | 134 |
1 files changed, 0 insertions, 134 deletions
diff --git a/wayland/cairo.c b/wayland/cairo.c deleted file mode 100644 index 193205b1..00000000 --- a/wayland/cairo.c +++ /dev/null | |||
@@ -1,134 +0,0 @@ | |||
1 | #include "client/cairo.h" | ||
2 | |||
3 | void cairo_set_source_u32(cairo_t *cairo, uint32_t color) { | ||
4 | cairo_set_source_rgba(cairo, | ||
5 | (color >> (3*8) & 0xFF) / 255.0, | ||
6 | (color >> (2*8) & 0xFF) / 255.0, | ||
7 | (color >> (1*8) & 0xFF) / 255.0, | ||
8 | (color >> (0*8) & 0xFF) / 255.0); | ||
9 | } | ||
10 | |||
11 | cairo_surface_t *cairo_image_surface_scale(cairo_surface_t *image, int width, int height) { | ||
12 | int image_width = cairo_image_surface_get_width(image); | ||
13 | int image_height = cairo_image_surface_get_height(image); | ||
14 | |||
15 | cairo_surface_t *new = | ||
16 | cairo_image_surface_create(CAIRO_FORMAT_ARGB32, width, height); | ||
17 | |||
18 | cairo_t *cairo = cairo_create(new); | ||
19 | |||
20 | cairo_scale(cairo, (double) width / image_width, (double) height / image_height); | ||
21 | |||
22 | cairo_set_source_surface(cairo, image, 0, 0); | ||
23 | cairo_paint(cairo); | ||
24 | |||
25 | cairo_destroy(cairo); | ||
26 | |||
27 | return new; | ||
28 | } | ||
29 | |||
30 | #ifdef WITH_GDK_PIXBUF | ||
31 | #include <gdk-pixbuf/gdk-pixbuf.h> | ||
32 | |||
33 | #ifndef GDK_PIXBUF_CHECK_VERSION | ||
34 | #define GDK_PIXBUF_CHECK_VERSION(major,minor,micro) \ | ||
35 | (GDK_PIXBUF_MAJOR > (major) || \ | ||
36 | (GDK_PIXBUF_MAJOR == (major) && GDK_PIXBUF_MINOR > (minor)) || \ | ||
37 | (GDK_PIXBUF_MAJOR == (major) && GDK_PIXBUF_MINOR == (minor) && \ | ||
38 | GDK_PIXBUF_MICRO >= (micro))) | ||
39 | #endif | ||
40 | |||
41 | cairo_surface_t* gdk_cairo_image_surface_create_from_pixbuf(const GdkPixbuf *gdkbuf) { | ||
42 | int chan = gdk_pixbuf_get_n_channels(gdkbuf); | ||
43 | if (chan < 3) return NULL; | ||
44 | |||
45 | #if GDK_PIXBUF_CHECK_VERSION(2,32,0) | ||
46 | const guint8* gdkpix = gdk_pixbuf_read_pixels(gdkbuf); | ||
47 | #else | ||
48 | const guint8* gdkpix = gdk_pixbuf_get_pixels(gdkbuf); | ||
49 | #endif | ||
50 | if (!gdkpix) { | ||
51 | return NULL; | ||
52 | } | ||
53 | gint w = gdk_pixbuf_get_width(gdkbuf); | ||
54 | gint h = gdk_pixbuf_get_height(gdkbuf); | ||
55 | int stride = gdk_pixbuf_get_rowstride(gdkbuf); | ||
56 | |||
57 | cairo_format_t fmt = (chan == 3) ? CAIRO_FORMAT_RGB24 : CAIRO_FORMAT_ARGB32; | ||
58 | cairo_surface_t * cs = cairo_image_surface_create (fmt, w, h); | ||
59 | cairo_surface_flush (cs); | ||
60 | if ( !cs || cairo_surface_status(cs) != CAIRO_STATUS_SUCCESS) { | ||
61 | return NULL; | ||
62 | } | ||
63 | |||
64 | int cstride = cairo_image_surface_get_stride(cs); | ||
65 | unsigned char * cpix = cairo_image_surface_get_data(cs); | ||
66 | |||
67 | if (chan == 3) { | ||
68 | int i; | ||
69 | for (i = h; i; --i) { | ||
70 | const guint8 *gp = gdkpix; | ||
71 | unsigned char *cp = cpix; | ||
72 | const guint8* end = gp + 3*w; | ||
73 | while (gp < end) { | ||
74 | #if G_BYTE_ORDER == G_LITTLE_ENDIAN | ||
75 | cp[0] = gp[2]; | ||
76 | cp[1] = gp[1]; | ||
77 | cp[2] = gp[0]; | ||
78 | #else | ||
79 | cp[1] = gp[0]; | ||
80 | cp[2] = gp[1]; | ||
81 | cp[3] = gp[2]; | ||
82 | #endif | ||
83 | gp += 3; | ||
84 | cp += 4; | ||
85 | } | ||
86 | gdkpix += stride; | ||
87 | cpix += cstride; | ||
88 | } | ||
89 | } else { | ||
90 | /* premul-color = alpha/255 * color/255 * 255 = (alpha*color)/255 | ||
91 | * (z/255) = z/256 * 256/255 = z/256 (1 + 1/255) | ||
92 | * = z/256 + (z/256)/255 = (z + z/255)/256 | ||
93 | * # recurse once | ||
94 | * = (z + (z + z/255)/256)/256 | ||
95 | * = (z + z/256 + z/256/255) / 256 | ||
96 | * # only use 16bit uint operations, loose some precision, | ||
97 | * # result is floored. | ||
98 | * -> (z + z>>8)>>8 | ||
99 | * # add 0x80/255 = 0.5 to convert floor to round | ||
100 | * => (z+0x80 + (z+0x80)>>8 ) >> 8 | ||
101 | * ------ | ||
102 | * tested as equal to lround(z/255.0) for uint z in [0..0xfe02] | ||
103 | */ | ||
104 | #define PREMUL_ALPHA(x,a,b,z) G_STMT_START { z = a * b + 0x80; x = (z + (z >> 8)) >> 8; } G_STMT_END | ||
105 | int i; | ||
106 | for (i = h; i; --i) { | ||
107 | const guint8 *gp = gdkpix; | ||
108 | unsigned char *cp = cpix; | ||
109 | const guint8* end = gp + 4*w; | ||
110 | guint z1, z2, z3; | ||
111 | while (gp < end) { | ||
112 | #if G_BYTE_ORDER == G_LITTLE_ENDIAN | ||
113 | PREMUL_ALPHA(cp[0], gp[2], gp[3], z1); | ||
114 | PREMUL_ALPHA(cp[1], gp[1], gp[3], z2); | ||
115 | PREMUL_ALPHA(cp[2], gp[0], gp[3], z3); | ||
116 | cp[3] = gp[3]; | ||
117 | #else | ||
118 | PREMUL_ALPHA(cp[1], gp[0], gp[3], z1); | ||
119 | PREMUL_ALPHA(cp[2], gp[1], gp[3], z2); | ||
120 | PREMUL_ALPHA(cp[3], gp[2], gp[3], z3); | ||
121 | cp[0] = gp[3]; | ||
122 | #endif | ||
123 | gp += 4; | ||
124 | cp += 4; | ||
125 | } | ||
126 | gdkpix += stride; | ||
127 | cpix += cstride; | ||
128 | } | ||
129 | #undef PREMUL_ALPHA | ||
130 | } | ||
131 | cairo_surface_mark_dirty(cs); | ||
132 | return cs; | ||
133 | } | ||
134 | #endif //WITH_GDK_PIXBUF | ||