385fa857b561270b353cfdec36844767732af361
[mesa.git] / src / gallium / winsys / drm / radeon / core / radeon_buffer.c
1 /*
2 * Copyright © 2008 Jérôme Glisse
3 * 2009 Corbin Simpson
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining
7 * a copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
16 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17 * NON-INFRINGEMENT. IN NO EVENT SHALL THE COPYRIGHT HOLDERS, AUTHORS
18 * AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21 * USE OR OTHER DEALINGS IN THE SOFTWARE.
22 *
23 * The above copyright notice and this permission notice (including the
24 * next paragraph) shall be included in all copies or substantial portions
25 * of the Software.
26 */
27 /*
28 * Authors:
29 * Jérôme Glisse <glisse@freedesktop.org>
30 * Corbin Simpson <MostAwesomeDude@gmail.com>
31 */
32
33 #include "radeon_buffer.h"
34
35 #include "radeon_bo_gem.h"
36 #include "softpipe/sp_texture.h"
37 #include "r300_context.h"
38 #include "util/u_format.h"
39 #include "util/u_math.h"
40 #include <X11/Xutil.h>
41
42 struct radeon_vl_context
43 {
44 Display *display;
45 int screen;
46 Drawable drawable;
47 };
48
49 static const char *radeon_get_name(struct pipe_winsys *ws)
50 {
51 return "Radeon/GEM+KMS";
52 }
53
54 static struct pipe_buffer *radeon_buffer_create(struct pipe_winsys *ws,
55 unsigned alignment,
56 unsigned usage,
57 unsigned size)
58 {
59 struct radeon_winsys *radeon_ws = (struct radeon_winsys *)ws;
60 struct radeon_pipe_buffer *radeon_buffer;
61 uint32_t domain;
62
63 radeon_buffer = CALLOC_STRUCT(radeon_pipe_buffer);
64 if (radeon_buffer == NULL) {
65 return NULL;
66 }
67
68 pipe_reference_init(&radeon_buffer->base.reference, 1);
69 radeon_buffer->base.alignment = alignment;
70 radeon_buffer->base.usage = usage;
71 radeon_buffer->base.size = size;
72
73 domain = 0;
74
75 if (usage & PIPE_BUFFER_USAGE_PIXEL) {
76 domain |= RADEON_GEM_DOMAIN_VRAM;
77 }
78 if (usage & PIPE_BUFFER_USAGE_VERTEX) {
79 domain |= RADEON_GEM_DOMAIN_GTT;
80 }
81 if (usage & PIPE_BUFFER_USAGE_INDEX) {
82 domain |= RADEON_GEM_DOMAIN_GTT;
83 }
84
85 radeon_buffer->bo = radeon_bo_open(radeon_ws->priv->bom, 0, size,
86 alignment, domain, 0);
87 if (radeon_buffer->bo == NULL) {
88 FREE(radeon_buffer);
89 return NULL;
90 }
91 return &radeon_buffer->base;
92 }
93
94 static struct pipe_buffer *radeon_buffer_user_create(struct pipe_winsys *ws,
95 void *ptr,
96 unsigned bytes)
97 {
98 struct radeon_pipe_buffer *radeon_buffer;
99
100 radeon_buffer =
101 (struct radeon_pipe_buffer*)radeon_buffer_create(ws, 0, 0, bytes);
102 if (radeon_buffer == NULL) {
103 return NULL;
104 }
105 radeon_bo_map(radeon_buffer->bo, 1);
106 memcpy(radeon_buffer->bo->ptr, ptr, bytes);
107 radeon_bo_unmap(radeon_buffer->bo);
108 return &radeon_buffer->base;
109 }
110
111 static struct pipe_buffer *radeon_surface_buffer_create(struct pipe_winsys *ws,
112 unsigned width,
113 unsigned height,
114 enum pipe_format format,
115 unsigned usage,
116 unsigned tex_usage,
117 unsigned *stride)
118 {
119 /* Radeons enjoy things in multiples of 32. */
120 /* XXX this can be 32 when POT */
121 const unsigned alignment = 64;
122 unsigned nblocksy, size;
123
124 nblocksy = util_format_get_nblocksy(format, height);
125 *stride = align(util_format_get_stride(format, width), alignment);
126 size = *stride * nblocksy;
127
128 return radeon_buffer_create(ws, 64, usage, size);
129 }
130
131 static void radeon_buffer_del(struct pipe_buffer *buffer)
132 {
133 struct radeon_pipe_buffer *radeon_buffer =
134 (struct radeon_pipe_buffer*)buffer;
135
136 radeon_bo_unref(radeon_buffer->bo);
137 free(radeon_buffer);
138 }
139
140 static void *radeon_buffer_map(struct pipe_winsys *ws,
141 struct pipe_buffer *buffer,
142 unsigned flags)
143 {
144 struct radeon_winsys_priv *priv = ((struct radeon_winsys *)ws)->priv;
145 struct radeon_pipe_buffer *radeon_buffer =
146 (struct radeon_pipe_buffer*)buffer;
147 int write = 0;
148
149 if (flags & PIPE_BUFFER_USAGE_DONTBLOCK) {
150 uint32_t domain;
151
152 if (radeon_bo_is_busy(radeon_buffer->bo, &domain))
153 return NULL;
154 }
155
156 if (radeon_bo_is_referenced_by_cs(radeon_buffer->bo, priv->cs)) {
157 priv->flush_cb(priv->flush_data);
158 }
159
160 if (flags & PIPE_BUFFER_USAGE_CPU_WRITE) {
161 write = 1;
162 }
163
164 if (radeon_bo_map(radeon_buffer->bo, write)) {
165 return NULL;
166 }
167
168 return radeon_buffer->bo->ptr;
169 }
170
171 static void radeon_buffer_unmap(struct pipe_winsys *ws,
172 struct pipe_buffer *buffer)
173 {
174 struct radeon_pipe_buffer *radeon_buffer =
175 (struct radeon_pipe_buffer*)buffer;
176
177 radeon_bo_unmap(radeon_buffer->bo);
178 }
179
180 static void radeon_fence_reference(struct pipe_winsys *ws,
181 struct pipe_fence_handle **ptr,
182 struct pipe_fence_handle *pfence)
183 {
184 }
185
186 static int radeon_fence_signalled(struct pipe_winsys *ws,
187 struct pipe_fence_handle *pfence,
188 unsigned flag)
189 {
190 return 1;
191 }
192
193 static int radeon_fence_finish(struct pipe_winsys *ws,
194 struct pipe_fence_handle *pfence,
195 unsigned flag)
196 {
197 return 0;
198 }
199
200 static void radeon_display_surface(struct pipe_winsys *pws,
201 struct pipe_surface *psurf,
202 struct radeon_vl_context *rvl_ctx)
203 {
204 struct r300_texture *r300tex = (struct r300_texture *)(psurf->texture);
205 XImage *ximage;
206 void *data;
207
208 ximage = XCreateImage(rvl_ctx->display,
209 XDefaultVisual(rvl_ctx->display, rvl_ctx->screen),
210 XDefaultDepth(rvl_ctx->display, rvl_ctx->screen),
211 ZPixmap, 0, /* format, offset */
212 NULL, /* data */
213 0, 0, /* size */
214 32, /* bitmap_pad */
215 0); /* bytes_per_line */
216
217 assert(ximage->format);
218 assert(ximage->bitmap_unit);
219
220 data = pws->buffer_map(pws, r300tex->buffer, 0);
221
222 /* update XImage's fields */
223 ximage->data = data;
224 ximage->width = psurf->width;
225 ximage->height = psurf->height;
226 ximage->bytes_per_line = psurf->width * (ximage->bits_per_pixel >> 3);
227
228 XPutImage(rvl_ctx->display, rvl_ctx->drawable,
229 XDefaultGC(rvl_ctx->display, rvl_ctx->screen),
230 ximage, 0, 0, 0, 0, psurf->width, psurf->height);
231
232 XSync(rvl_ctx->display, 0);
233
234 ximage->data = NULL;
235 XDestroyImage(ximage);
236
237 pws->buffer_unmap(pws, r300tex->buffer);
238 }
239
240 static void radeon_flush_frontbuffer(struct pipe_winsys *pipe_winsys,
241 struct pipe_surface *pipe_surface,
242 void *context_private)
243 {
244 struct radeon_vl_context *rvl_ctx;
245 rvl_ctx = (struct radeon_vl_context *) context_private;
246 radeon_display_surface(pipe_winsys, pipe_surface, rvl_ctx);
247 }
248
249 struct radeon_winsys* radeon_pipe_winsys(int fd)
250 {
251 struct radeon_winsys* radeon_ws;
252
253 radeon_ws = CALLOC_STRUCT(radeon_winsys);
254 if (radeon_ws == NULL) {
255 return NULL;
256 }
257
258 radeon_ws->priv = CALLOC_STRUCT(radeon_winsys_priv);
259 if (radeon_ws->priv == NULL) {
260 FREE(radeon_ws);
261 return NULL;
262 }
263
264 radeon_ws->priv->fd = fd;
265 radeon_ws->priv->bom = radeon_bo_manager_gem_ctor(fd);
266
267 radeon_ws->base.flush_frontbuffer = radeon_flush_frontbuffer;
268
269 radeon_ws->base.buffer_create = radeon_buffer_create;
270 radeon_ws->base.user_buffer_create = radeon_buffer_user_create;
271 radeon_ws->base.surface_buffer_create = radeon_surface_buffer_create;
272 radeon_ws->base.buffer_map = radeon_buffer_map;
273 radeon_ws->base.buffer_unmap = radeon_buffer_unmap;
274 radeon_ws->base.buffer_destroy = radeon_buffer_del;
275
276 radeon_ws->base.fence_reference = radeon_fence_reference;
277 radeon_ws->base.fence_signalled = radeon_fence_signalled;
278 radeon_ws->base.fence_finish = radeon_fence_finish;
279
280 radeon_ws->base.get_name = radeon_get_name;
281
282 return radeon_ws;
283 }