summaryrefslogtreecommitdiffstats
path: root/src/gallium/winsys/g3dvl/xsp_winsys.c
blob: c660c6e0186edaffcfcb8ff8d06d23bb6ca6d09c (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
#include "xsp_winsys.h"
#include <X11/Xutil.h>
#include <pipe/p_winsys.h>
#include <pipe/p_state.h>
#include <pipe/p_util.h>
#include <pipe/p_inlines.h>
#include <softpipe/sp_winsys.h>

/* pipe_winsys implementation */

struct xsp_pipe_winsys
{
	struct pipe_winsys	base;
	Display			*display;
	XImage			fbimage;
};

struct xsp_buffer
{
	struct pipe_buffer	base;
	boolean			is_user_buffer;
	void			*data;
	void			*mapped_data;
};

static struct pipe_buffer* xsp_buffer_create(struct pipe_winsys *pws, unsigned alignment, unsigned usage, unsigned size)
{
	struct xsp_buffer *buffer;
	
	assert(pws);
	
	buffer = calloc(1, sizeof(struct xsp_buffer));
	buffer->base.refcount = 1;
	buffer->base.alignment = alignment;
	buffer->base.usage = usage;
	buffer->base.size = size;
	buffer->data = align_malloc(size, alignment);
	
	return (struct pipe_buffer*)buffer;
}

static struct pipe_buffer* xsp_user_buffer_create(struct pipe_winsys *pws, void *data, unsigned size)
{
	struct xsp_buffer *buffer;
	
	assert(pws);
	
	buffer = calloc(1, sizeof(struct xsp_buffer));
	buffer->base.refcount = 1;
	buffer->base.size = size;
	buffer->is_user_buffer = TRUE;
	buffer->data = data;
	
	return (struct pipe_buffer*)buffer;
}

static void* xsp_buffer_map(struct pipe_winsys *pws, struct pipe_buffer *buffer, unsigned flags)
{
	struct xsp_buffer *xsp_buf = (struct xsp_buffer*)buffer;
	
	assert(pws);
	assert(buffer);
	
	xsp_buf->mapped_data = xsp_buf->data;
	
	return xsp_buf->mapped_data;
}

static void xsp_buffer_unmap(struct pipe_winsys *pws, struct pipe_buffer *buffer)
{
	struct xsp_buffer *xsp_buf = (struct xsp_buffer*)buffer;
	
	assert(pws);
	assert(buffer);
	
	xsp_buf->mapped_data = NULL;
}

static void xsp_buffer_destroy(struct pipe_winsys *pws, struct pipe_buffer *buffer)
{
	struct xsp_buffer *xsp_buf = (struct xsp_buffer*)buffer;
	
	assert(pws);
	assert(buffer);
	
	if (!xsp_buf->is_user_buffer)
		align_free(xsp_buf->data);
	
	free(xsp_buf);
}

static struct pipe_surface* xsp_surface_alloc(struct pipe_winsys *pws)
{
	struct pipe_surface *surface;
	
	assert(pws);
	
	surface = calloc(1, sizeof(struct pipe_surface));
	surface->refcount = 1;
	surface->winsys = pws;
	
	return surface;
}

/* Borrowed from Mesa's xm_winsys */
static unsigned int round_up(unsigned n, unsigned multiple)
{
   return (n + multiple - 1) & ~(multiple - 1);
}

static int xsp_surface_alloc_storage
(
	struct pipe_winsys *pws,
	struct pipe_surface *surface,
	unsigned width,
	unsigned height,
	enum pipe_format format,
	unsigned flags,
	unsigned tex_usage
)
{
	const unsigned int ALIGNMENT = 1;
	
	assert(pws);
	assert(surface);
	
	surface->width = width;
	surface->height = height;
	surface->format = format;
	pf_get_block(format, &surface->block);
	surface->nblocksx = pf_get_nblocksx(&surface->block, width);
	surface->nblocksy = pf_get_nblocksy(&surface->block, height);
	surface->stride = round_up(surface->nblocksx * surface->block.size, ALIGNMENT);
	surface->usage = flags;
	surface->buffer = pws->buffer_create(pws, ALIGNMENT, PIPE_BUFFER_USAGE_PIXEL, surface->stride * surface->nblocksy);
	
	return 0;
}

static void xsp_surface_release(struct pipe_winsys *pws, struct pipe_surface **surface)
{
	struct pipe_surface *s;
	
	assert(pws);
	assert(surface);
	assert(*surface);
	
	s = *surface;
	
	s->refcount--;
	
	if (s->refcount == 0)
	{
		pipe_buffer_reference(pws, &s->buffer, NULL);
		free(s);
	}
	
	*surface = NULL;
}

static void xsp_fence_reference(struct pipe_winsys *pws, struct pipe_fence_handle **ptr, struct pipe_fence_handle *fence)
{
	assert(pws);
	assert(ptr);
	assert(fence);
}

static int xsp_fence_signalled(struct pipe_winsys *pws, struct pipe_fence_handle *fence, unsigned flag)
{
	assert(pws);
	assert(fence);
	
	return 0;
}

static int xsp_fence_finish(struct pipe_winsys *pws, struct pipe_fence_handle *fence, unsigned flag)
{
	assert(pws);
	assert(fence);
	
	return 0;
}

static void xsp_flush_frontbuffer(struct pipe_winsys *pws, struct pipe_surface *surface, void *context_private)
{	
	struct xsp_pipe_winsys *xsp_winsys;
	
	assert(pws);
	assert(surface);
	assert(context_private);
	
	xsp_winsys = (struct xsp_pipe_winsys*)pws;
	
	xsp_winsys->fbimage.width = surface->width;
	xsp_winsys->fbimage.height = surface->height;
	xsp_winsys->fbimage.bytes_per_line = surface->width * (xsp_winsys->fbimage.bits_per_pixel >> 3);
	xsp_winsys->fbimage.data = pipe_surface_map(surface, 0);
	
	XPutImage
	(
		xsp_winsys->display,
		*(Drawable*)context_private,
		XDefaultGC(xsp_winsys->display, XDefaultScreen(xsp_winsys->display)),
		&xsp_winsys->fbimage,
		0,
		0,
		0,
		0,
		surface->width,
		surface->height
	);
	XFlush(xsp_winsys->display);
	pipe_surface_unmap(surface);
}

static const char* xsp_get_name(struct pipe_winsys *pws)
{
	assert(pws);
	return "X11 SoftPipe";
}

/* Show starts here */

struct pipe_context* create_pipe_context(Display *display)
{
	struct xsp_pipe_winsys	*xsp_winsys;
	struct pipe_screen	*screen;
	struct pipe_context	*pipe;
	
	assert(display);
	
	xsp_winsys = calloc(1, sizeof(struct xsp_pipe_winsys));
	xsp_winsys->base.buffer_create = xsp_buffer_create;
	xsp_winsys->base.user_buffer_create = xsp_user_buffer_create;
	xsp_winsys->base.buffer_map = xsp_buffer_map;
	xsp_winsys->base.buffer_unmap = xsp_buffer_unmap;
	xsp_winsys->base.buffer_destroy = xsp_buffer_destroy;
	xsp_winsys->base.surface_alloc = xsp_surface_alloc;
	xsp_winsys->base.surface_alloc_storage = xsp_surface_alloc_storage;
	xsp_winsys->base.surface_release = xsp_surface_release;
	xsp_winsys->base.fence_reference = xsp_fence_reference;
	xsp_winsys->base.fence_signalled = xsp_fence_signalled;
	xsp_winsys->base.fence_finish = xsp_fence_finish;
	xsp_winsys->base.flush_frontbuffer = xsp_flush_frontbuffer;
	xsp_winsys->base.get_name = xsp_get_name;
	xsp_winsys->display = display;
	
	{
		/* XXX: Can't use the returned XImage* directly,
		since we don't have control over winsys destruction
		and we wouldn't be able to free it */
		XImage *template = XCreateImage
		(
			display,
			XDefaultVisual(display, XDefaultScreen(display)),
			XDefaultDepth(display, XDefaultScreen(display)),
			ZPixmap,
			0,
			NULL,
			0,	/* Don't know the width and height until flush_frontbuffer */
			0,
			32,
			0
		);
		
		memcpy(&xsp_winsys->fbimage, template, sizeof(XImage));
		XInitImage(&xsp_winsys->fbimage);
		
		XDestroyImage(template);
	}
	
	screen = softpipe_create_screen((struct pipe_winsys*)xsp_winsys);
	pipe = softpipe_create(screen, (struct pipe_winsys*)xsp_winsys, NULL);
	
	return pipe;
}

int destroy_pipe_context(struct pipe_context *pipe)
{
	struct pipe_screen *screen;
	struct pipe_winsys *winsys;
	
	assert(pipe);
	
	screen = pipe->screen;
	winsys = pipe->winsys;
	pipe->destroy(pipe);
	screen->destroy(screen);
	free(winsys);
	
	return 0;
}