blob: 2d23b5e9fa295db4d1aa6996702e8bdeb1cb51c4 [file] [log] [blame]
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05001/*
2 * Copyright © 2008 Kristian Høgsberg
3 *
4 * Permission to use, copy, modify, distribute, and sell this software and its
5 * documentation for any purpose is hereby granted without fee, provided that
6 * the above copyright notice appear in all copies and that both that copyright
7 * notice and this permission notice appear in supporting documentation, and
8 * that the name of the copyright holders not be used in advertising or
9 * publicity pertaining to distribution of the software without specific,
10 * written prior permission. The copyright holders make no representations
11 * about the suitability of this software for any purpose. It is provided "as
12 * is" without express or implied warranty.
13 *
14 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
20 * OF THIS SOFTWARE.
21 */
22
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -050023#include <stdint.h>
24#include <stdlib.h>
25#include <string.h>
26#include <stdio.h>
27#include <i915_drm.h>
28#include <sys/ioctl.h>
29#include <math.h>
30#include <cairo.h>
31#include "cairo-util.h"
32
33struct buffer *
34buffer_create(int fd, int width, int height, int stride)
35{
36 struct buffer *buffer;
37 struct drm_i915_gem_create create;
38 struct drm_gem_flink flink;
39
40 buffer = malloc(sizeof *buffer);
41 buffer->width = width;
42 buffer->height = height;
43 buffer->stride = stride;
44
45 memset(&create, 0, sizeof(create));
46 create.size = height * stride;
47
48 if (ioctl(fd, DRM_IOCTL_I915_GEM_CREATE, &create) != 0) {
49 fprintf(stderr, "gem create failed: %m\n");
50 free(buffer);
51 return NULL;
52 }
53
54 flink.handle = create.handle;
55 if (ioctl(fd, DRM_IOCTL_GEM_FLINK, &flink) != 0) {
56 fprintf(stderr, "gem flink failed: %m\n");
57 free(buffer);
58 return 0;
59 }
60
61 buffer->handle = flink.handle;
62 buffer->name = flink.name;
63
64 return buffer;
65}
66
67int
68buffer_destroy(struct buffer *buffer, int fd)
69{
70 struct drm_gem_close close;
71
72 close.handle = buffer->handle;
73 if (ioctl(fd, DRM_IOCTL_GEM_CLOSE, &close) < 0) {
74 fprintf(stderr, "gem close failed: %m\n");
75 return -1;
76 }
77
78 free(buffer);
79
80 return 0;
81}
82
83int
84buffer_data(struct buffer *buffer, int fd, void *data)
85{
86 struct drm_i915_gem_pwrite pwrite;
87
88 pwrite.handle = buffer->handle;
89 pwrite.offset = 0;
90 pwrite.size = buffer->height * buffer->stride;
91 pwrite.data_ptr = (uint64_t) (uintptr_t) data;
92
93 if (ioctl(fd, DRM_IOCTL_I915_GEM_PWRITE, &pwrite) < 0) {
94 fprintf(stderr, "gem pwrite failed: %m\n");
95 return -1;
96 }
97
98 return 0;
99}
100
101struct buffer *
102buffer_create_from_cairo_surface(int fd, cairo_surface_t *surface)
103{
104 struct buffer *buffer;
105 int32_t width, height, stride;
106 void *data;
107
108 width = cairo_image_surface_get_width(surface);
109 height = cairo_image_surface_get_height(surface);
110 stride = cairo_image_surface_get_stride(surface);
111 data = cairo_image_surface_get_data(surface);
112
113 buffer = buffer_create(fd, width, height, stride);
114 if (buffer == NULL)
115 return NULL;
116
117 if (buffer_data(buffer, fd, data) < 0) {
118 buffer_destroy(buffer, fd);
119 return NULL;
120 }
121
122 return buffer;
123}
124
Kristian Høgsberg87330262008-11-17 22:23:55 -0500125#define ARRAY_LENGTH(a) (sizeof (a) / sizeof (a)[0])
126
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -0500127void
Kristian Høgsberg10bdd292008-11-08 23:27:27 -0500128blur_surface(cairo_surface_t *surface, int margin)
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -0500129{
130 cairo_surface_t *tmp;
131 int32_t width, height, stride, x, y, z, w;
132 uint8_t *src, *dst;
133 uint32_t *s, *d, a, p;
Kristian Høgsberg87330262008-11-17 22:23:55 -0500134 int i, j, k, size, half;
Kristian Høgsberge9d550b2008-11-19 00:49:39 -0500135 uint8_t kernel[17];
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -0500136 double f;
137
Kristian Høgsberg87330262008-11-17 22:23:55 -0500138 size = ARRAY_LENGTH(kernel);
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -0500139 width = cairo_image_surface_get_width(surface);
140 height = cairo_image_surface_get_height(surface);
141 stride = cairo_image_surface_get_stride(surface);
142 src = cairo_image_surface_get_data(surface);
143
144 tmp = cairo_image_surface_create(CAIRO_FORMAT_RGB24, width, height);
145 dst = cairo_image_surface_get_data(tmp);
146
147 half = size / 2;
148 a = 0;
149 for (i = 0; i < size; i++) {
150 f = (i - half);
151 kernel[i] = exp(- f * f / 30.0) * 80;
152 a += kernel[i];
153 }
154
155 for (i = 0; i < height; i++) {
156 s = (uint32_t *) (src + i * stride);
157 d = (uint32_t *) (dst + i * stride);
158 for (j = 0; j < width; j++) {
Kristian Høgsberg87330262008-11-17 22:23:55 -0500159 if (margin < j && j < width - margin) {
160 d[j] = s[j];
Kristian Høgsberg10bdd292008-11-08 23:27:27 -0500161 continue;
Kristian Høgsberg87330262008-11-17 22:23:55 -0500162 }
163
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -0500164 x = 0;
165 y = 0;
166 z = 0;
167 w = 0;
168 for (k = 0; k < size; k++) {
169 if (j - half + k < 0 || j - half + k >= width)
170 continue;
171 p = s[j - half + k];
172
173 x += (p >> 24) * kernel[k];
174 y += ((p >> 16) & 0xff) * kernel[k];
175 z += ((p >> 8) & 0xff) * kernel[k];
176 w += (p & 0xff) * kernel[k];
177 }
178 d[j] = (x / a << 24) | (y / a << 16) | (z / a << 8) | w / a;
179 }
180 }
181
182 for (i = 0; i < height; i++) {
183 s = (uint32_t *) (dst + i * stride);
184 d = (uint32_t *) (src + i * stride);
185 for (j = 0; j < width; j++) {
Kristian Høgsberg87330262008-11-17 22:23:55 -0500186 if (margin <= i && i < height - margin) {
187 d[j] = s[j];
Kristian Høgsberg10bdd292008-11-08 23:27:27 -0500188 continue;
Kristian Høgsberg87330262008-11-17 22:23:55 -0500189 }
190
Kristian Høgsberg2f2cfae2008-11-08 22:46:30 -0500191 x = 0;
192 y = 0;
193 z = 0;
194 w = 0;
195 for (k = 0; k < size; k++) {
196 if (i - half + k < 0 || i - half + k >= height)
197 continue;
198 s = (uint32_t *) (dst + (i - half + k) * stride);
199 p = s[j];
200
201 x += (p >> 24) * kernel[k];
202 y += ((p >> 16) & 0xff) * kernel[k];
203 z += ((p >> 8) & 0xff) * kernel[k];
204 w += (p & 0xff) * kernel[k];
205 }
206 d[j] = (x / a << 24) | (y / a << 16) | (z / a << 8) | w / a;
207 }
208 }
209
210 cairo_surface_destroy(tmp);
211}