summaryrefslogtreecommitdiff
path: root/src/cairo-xcb-connection-core.c
blob: e01dc1a8361940bd39a29cc30c704f7752466384 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
/* cairo - a vector graphics library with display and print output
 *
 * Copyright © 2009 Intel Corporation
 *
 * This library is free software; you can redistribute it and/or
 * modify it either under the terms of the GNU Lesser General Public
 * License version 2.1 as published by the Free Software Foundation
 * (the "LGPL") or, at your option, under the terms of the Mozilla
 * Public License Version 1.1 (the "MPL"). If you do not alter this
 * notice, a recipient may use your version of this file under either
 * the MPL or the LGPL.
 *
 * You should have received a copy of the LGPL along with this library
 * in the file COPYING-LGPL-2.1; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA
 * You should have received a copy of the MPL along with this library
 * in the file COPYING-MPL-1.1
 *
 * The contents of this file are subject to the Mozilla Public License
 * Version 1.1 (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
 * OF ANY KIND, either express or implied. See the LGPL or the MPL for
 * the specific language governing rights and limitations.
 *
 * Contributor(s):
 *	Chris Wilson <chris@chris-wilson.co.uk>
 */

#include "cairoint.h"

#include "cairo-xcb-private.h"

#include <xcb/xcbext.h>

xcb_pixmap_t
_cairo_xcb_connection_create_pixmap (cairo_xcb_connection_t *connection,
				     uint8_t depth,
				     xcb_drawable_t drawable,
				     uint16_t width,
				     uint16_t height)
{
    xcb_pixmap_t pixmap = _cairo_xcb_connection_get_xid (connection);

    assert (width > 0);
    assert (height > 0);
    xcb_create_pixmap (connection->xcb_connection,
		       depth, pixmap, drawable,
		       width, height);
    return pixmap;
}

void
_cairo_xcb_connection_free_pixmap (cairo_xcb_connection_t *connection,
				   xcb_pixmap_t pixmap)
{
    xcb_free_pixmap (connection->xcb_connection, pixmap);
    _cairo_xcb_connection_put_xid (connection, pixmap);
}

xcb_gcontext_t
_cairo_xcb_connection_create_gc (cairo_xcb_connection_t *connection,
				 xcb_drawable_t drawable,
				 uint32_t value_mask,
				 uint32_t *values)
{
    xcb_gcontext_t gc = _cairo_xcb_connection_get_xid (connection);
    xcb_create_gc (connection->xcb_connection, gc, drawable,
		   value_mask, values);
    return gc;
}

void
_cairo_xcb_connection_free_gc (cairo_xcb_connection_t *connection,
			       xcb_gcontext_t gc)
{
    xcb_free_gc (connection->xcb_connection, gc);
    _cairo_xcb_connection_put_xid (connection, gc);
}

void
_cairo_xcb_connection_change_gc (cairo_xcb_connection_t *connection,
				 xcb_gcontext_t gc,
				 uint32_t value_mask,
				 uint32_t *values)
{
    xcb_change_gc (connection->xcb_connection, gc,
		   value_mask, values);
}

void
_cairo_xcb_connection_copy_area (cairo_xcb_connection_t *connection,
				 xcb_drawable_t src,
				 xcb_drawable_t dst,
				 xcb_gcontext_t gc,
				 int16_t src_x,
				 int16_t src_y,
				 int16_t dst_x,
				 int16_t dst_y,
				 uint16_t width,
				 uint16_t height)
{
    xcb_copy_area (connection->xcb_connection, src, dst, gc,
		   src_x, src_y, dst_x, dst_y, width, height);
}

void
_cairo_xcb_connection_poly_fill_rectangle (cairo_xcb_connection_t *connection,
					   xcb_drawable_t dst,
					   xcb_gcontext_t gc,
					   uint32_t num_rectangles,
					   xcb_rectangle_t *rectangles)
{
    xcb_poly_fill_rectangle (connection->xcb_connection, dst, gc,
			     num_rectangles, rectangles);
}

void
_cairo_xcb_connection_put_image (cairo_xcb_connection_t *connection,
				 xcb_drawable_t dst,
				 xcb_gcontext_t gc,
				 uint16_t width,
				 uint16_t height,
				 int16_t dst_x,
				 int16_t dst_y,
				 uint8_t depth,
				 uint32_t stride,
				 void *data)
{
    const uint32_t req_size = 18;
    uint32_t length = height * stride;
    uint32_t len = (req_size + length) >> 2;

    if (len < connection->maximum_request_length) {
	xcb_put_image (connection->xcb_connection, XCB_IMAGE_FORMAT_Z_PIXMAP,
		       dst, gc, width, height, dst_x, dst_y, 0, depth,
		       length, data);
    } else {
	int rows = (connection->maximum_request_length - req_size - 4) / stride;
	if (rows > 0) {
	    do {
		if (rows > height)
		    rows = height;

		length = rows * stride;

		xcb_put_image (connection->xcb_connection, XCB_IMAGE_FORMAT_Z_PIXMAP,
			       dst, gc, width, rows, dst_x, dst_y, 0, depth, length, data);

		height -= rows;
		dst_y += rows;
		data = (char *) data + length;
	    } while (height);
	} else {
	    ASSERT_NOT_REACHED;
	}
    }
}

static void
_cairo_xcb_connection_do_put_subimage (cairo_xcb_connection_t *connection,
				       xcb_drawable_t dst,
				       xcb_gcontext_t gc,
				       int16_t src_x,
				       int16_t src_y,
				       uint16_t width,
				       uint16_t height,
				       uint16_t cpp,
				       int stride,
				       int16_t dst_x,
				       int16_t dst_y,
				       uint8_t depth,
				       void *_data)
{
    xcb_protocol_request_t xcb_req = {
	0 /* count */,
	0 /* ext */,
	XCB_PUT_IMAGE /* opcode */,
	1 /* isvoid (doesn't cause a reply) */
    };
    xcb_put_image_request_t req;
    struct iovec vec_stack[CAIRO_STACK_ARRAY_LENGTH (struct iovec)];
    struct iovec *vec = vec_stack;
    uint32_t len = 0;
    uint8_t *data = _data;
    int n = 3;
    /* Two extra entries are needed for xcb, two for us */
    int entries_needed = height + 2 + 2;

    req.format = XCB_IMAGE_FORMAT_Z_PIXMAP;
    req.drawable = dst;
    req.gc = gc;
    req.width = width;
    req.height = height;
    req.dst_x = dst_x;
    req.dst_y = dst_y;
    req.left_pad = 0;
    req.depth = depth;
    req.pad0[0] = 0;
    req.pad0[1] = 0;

    if (entries_needed > ARRAY_LENGTH (vec_stack)) {
	vec = _cairo_malloc_ab (entries_needed, sizeof (struct iovec));
	if (unlikely (vec == NULL)) {
	    /* XXX loop over ARRAY_LENGTH (vec_stack) */
	    return;
	}
    }

    data += src_y * stride + src_x * cpp;
    /* vec[1] will be used in XCB if it has to use BigRequests or insert a sync,
     * vec[0] is used if the internal queue needs to be flushed. */
    vec[2].iov_base = (char *) &req;
    vec[2].iov_len = sizeof (req);

    /* Now comes the actual data */
    while (height--) {
	vec[n].iov_base = data;
	vec[n].iov_len = cpp * width;
	len += cpp * width;
	data += stride;
	n++;
    }

    /* And again some padding */
    vec[n].iov_base = 0;
    vec[n].iov_len = -len & 3;
    n++;

    /* For efficiency reasons, this functions writes the request "directly" to
     * the xcb connection to avoid having to copy the data around. */
    assert (n == entries_needed);
    xcb_req.count = n - 2;
    xcb_send_request (connection->xcb_connection, 0, &vec[2], &xcb_req);

    if (vec != vec_stack)
	free (vec);
}

void
_cairo_xcb_connection_put_subimage (cairo_xcb_connection_t *connection,
				    xcb_drawable_t dst,
				    xcb_gcontext_t gc,
				    int16_t src_x,
				    int16_t src_y,
				    uint16_t width,
				    uint16_t height,
				    uint16_t cpp,
				    int stride,
				    int16_t dst_x,
				    int16_t dst_y,
				    uint8_t depth,
				    void *_data)
{
    const uint32_t req_size = sizeof(xcb_put_image_request_t);
    uint32_t length = height * cpp * width;
    uint32_t len = (req_size + length) >> 2;

    if (len < connection->maximum_request_length) {
	_cairo_xcb_connection_do_put_subimage (connection, dst, gc, src_x, src_y,
			width, height, cpp, stride, dst_x, dst_y, depth, _data);
    } else {
	int rows = (connection->maximum_request_length - req_size - 4) / (cpp * width);
	if (rows > 0) {
	    do {
		if (rows > height)
		    rows = height;

		_cairo_xcb_connection_do_put_subimage (connection, dst, gc, src_x, src_y,
			width, rows, cpp, stride, dst_x, dst_y, depth, _data);

		height -= rows;
		dst_y += rows;
		_data = (char *) _data + stride * rows;
	    } while (height);
	} else {
	    ASSERT_NOT_REACHED;
	}
    }
}

xcb_get_image_reply_t *
_cairo_xcb_connection_get_image (cairo_xcb_connection_t *connection,
				 xcb_drawable_t src,
				 int16_t src_x,
				 int16_t src_y,
				 uint16_t width,
				 uint16_t height)
{
    return xcb_get_image_reply (connection->xcb_connection,
				xcb_get_image (connection->xcb_connection,
					       XCB_IMAGE_FORMAT_Z_PIXMAP,
					       src,
					       src_x, src_y,
					       width, height,
					       (uint32_t) -1),
				NULL);
}