Changes.
[cairo/gpu.git] / src / gpu / cairo-gpu-impl-space-gallium-softpipe.h
blobd2f06e8a3271adddd7051e389a5a1c40ffebfa9d
1 // adapted copy of state_trackers/python/st_softpipe_winsys.c
3 struct st_softpipe_buffer
5 struct pipe_buffer base;
6 boolean userBuffer; /** Is this a user-space buffer? */
7 void *data;
8 void *mapped;
9 };
12 /** Cast wrapper */
13 static inline struct st_softpipe_buffer *
14 st_softpipe_buffer( struct pipe_buffer *buf )
16 return (struct st_softpipe_buffer *)buf;
20 static void *
21 st_softpipe_buffer_map(struct pipe_winsys *winsys,
22 struct pipe_buffer *buf,
23 unsigned flags)
25 struct st_softpipe_buffer *st_softpipe_buf = st_softpipe_buffer(buf);
26 st_softpipe_buf->mapped = st_softpipe_buf->data;
27 return st_softpipe_buf->mapped;
31 static void
32 st_softpipe_buffer_unmap(struct pipe_winsys *winsys,
33 struct pipe_buffer *buf)
35 struct st_softpipe_buffer *st_softpipe_buf = st_softpipe_buffer(buf);
36 st_softpipe_buf->mapped = NULL;
40 static void
41 st_softpipe_buffer_destroy(struct pipe_buffer *buf)
43 struct st_softpipe_buffer *oldBuf = st_softpipe_buffer(buf);
45 if (oldBuf->data) {
46 if (!oldBuf->userBuffer)
47 free(oldBuf->data);
49 oldBuf->data = NULL;
52 free(oldBuf);
56 static void
57 st_softpipe_flush_frontbuffer(struct pipe_winsys *winsys,
58 struct pipe_surface *surf,
59 void *context_private)
65 static const char *
66 st_softpipe_get_name(struct pipe_winsys *winsys)
68 return "softpipe";
71 #include <malloc.h>
73 static struct pipe_buffer *
74 st_softpipe_buffer_create(struct pipe_winsys *winsys,
75 unsigned alignment,
76 unsigned usage,
77 unsigned size)
79 struct st_softpipe_buffer *buffer = calloc(sizeof(struct st_softpipe_buffer), 1);
81 pipe_reference_init(&buffer->base.reference, 1);
82 buffer->base.alignment = alignment;
83 buffer->base.usage = usage;
84 buffer->base.size = size;
86 //if(posix_memalign(&buffer->data, alignment, size))
87 if(!(buffer->data = memalign(alignment, size)))
89 free(buffer);
90 return 0;
93 return &buffer->base;
97 /**
98 * Create buffer which wraps user-space data.
100 static struct pipe_buffer *
101 st_softpipe_user_buffer_create(struct pipe_winsys *winsys,
102 void *ptr,
103 unsigned bytes)
105 struct st_softpipe_buffer *buffer;
107 buffer = calloc(sizeof(struct st_softpipe_buffer), 1);
108 if(!buffer)
109 return NULL;
111 pipe_reference_init(&buffer->base.reference, 1);
112 buffer->base.size = bytes;
113 buffer->userBuffer = TRUE;
114 buffer->data = ptr;
116 return &buffer->base;
121 * Round n up to next multiple.
123 static inline unsigned
124 round_up(unsigned n, unsigned multiple)
126 return (n + multiple - 1) & ~(multiple - 1);
130 static struct pipe_buffer *
131 st_softpipe_surface_buffer_create(struct pipe_winsys *winsys,
132 unsigned width, unsigned height,
133 enum pipe_format format,
134 unsigned usage,
135 unsigned *stride)
137 const unsigned alignment = 64;
138 struct pipe_format_block block;
139 unsigned nblocksx, nblocksy;
141 pf_get_block(format, &block);
142 nblocksx = pf_get_nblocksx(&block, width);
143 nblocksy = pf_get_nblocksy(&block, height);
144 *stride = round_up(nblocksx * block.size, alignment);
146 return winsys->buffer_create(winsys, alignment,
147 usage,
148 *stride * nblocksy);
152 static void
153 st_softpipe_fence_reference(struct pipe_winsys *winsys,
154 struct pipe_fence_handle **ptr,
155 struct pipe_fence_handle *fence)
160 static int
161 st_softpipe_fence_signalled(struct pipe_winsys *winsys,
162 struct pipe_fence_handle *fence,
163 unsigned flag)
165 return 0;
169 static int
170 st_softpipe_fence_finish(struct pipe_winsys *winsys,
171 struct pipe_fence_handle *fence,
172 unsigned flag)
174 return 0;
178 static void
179 st_softpipe_destroy(struct pipe_winsys *winsys)
181 free(winsys);
185 static struct pipe_screen *
186 st_softpipe_screen_create(void)
188 static struct pipe_winsys *winsys;
189 struct pipe_screen *screen;
191 winsys = calloc(sizeof(struct pipe_winsys), 1);
192 if(!winsys)
193 return NULL;
195 winsys->destroy = st_softpipe_destroy;
197 winsys->buffer_create = st_softpipe_buffer_create;
198 winsys->user_buffer_create = st_softpipe_user_buffer_create;
199 winsys->buffer_map = st_softpipe_buffer_map;
200 winsys->buffer_unmap = st_softpipe_buffer_unmap;
201 winsys->buffer_destroy = st_softpipe_buffer_destroy;
203 winsys->surface_buffer_create = st_softpipe_surface_buffer_create;
205 winsys->fence_reference = st_softpipe_fence_reference;
206 winsys->fence_signalled = st_softpipe_fence_signalled;
207 winsys->fence_finish = st_softpipe_fence_finish;
209 winsys->flush_frontbuffer = st_softpipe_flush_frontbuffer;
210 winsys->get_name = st_softpipe_get_name;
212 screen = softpipe_create_screen(winsys);
213 if(!screen)
214 st_softpipe_destroy(winsys);
216 return screen;
219 cairo_space_t *
220 cairo_gallium_softpipe_space_create(unsigned flags)
222 cairo_gpu_space_t *space;
223 space = _cairo_gpu_space__begin_create();
224 if(!space)
225 return 0;
227 space->api = API_SOFTPIPE;
228 space->screen = st_softpipe_screen_create();
229 space->owns_screen = 1;
231 _cairo_gpu_space__finish_create(space, flags);
232 return &space->base;