Merge branch 'mesa_7_6_branch'
[mesa.git] / src / gallium / state_trackers / vega / vg_tracker.c
1 /**************************************************************************
2 *
3 * Copyright 2009 VMware, Inc. All Rights Reserved.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sub license, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
12 *
13 * The above copyright notice and this permission notice (including the
14 * next paragraph) shall be included in all copies or substantial portions
15 * of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
20 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
21 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
22 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
23 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 *
25 **************************************************************************/
26
27 #include "vg_context.h"
28 #include "vg_tracker.h"
29 #include "mask.h"
30
31 #include "pipe/p_context.h"
32 #include "pipe/p_inlines.h"
33 #include "pipe/p_screen.h"
34 #include "util/u_memory.h"
35 #include "util/u_math.h"
36
37 static struct pipe_texture *
38 create_texture(struct pipe_context *pipe, enum pipe_format format,
39 VGint width, VGint height)
40 {
41 struct pipe_texture templ;
42
43 memset(&templ, 0, sizeof(templ));
44
45 if (format != PIPE_FORMAT_NONE) {
46 templ.format = format;
47 }
48 else {
49 templ.format = PIPE_FORMAT_A8R8G8B8_UNORM;
50 }
51
52 templ.target = PIPE_TEXTURE_2D;
53 pf_get_block(templ.format, &templ.block);
54 templ.width[0] = width;
55 templ.height[0] = height;
56 templ.depth[0] = 1;
57 templ.last_level = 0;
58
59 if (pf_get_component_bits(format, PIPE_FORMAT_COMP_S)) {
60 templ.tex_usage = PIPE_TEXTURE_USAGE_DEPTH_STENCIL;
61 } else {
62 templ.tex_usage = (PIPE_TEXTURE_USAGE_DISPLAY_TARGET |
63 PIPE_TEXTURE_USAGE_RENDER_TARGET |
64 PIPE_TEXTURE_USAGE_SAMPLER);
65 }
66
67 return pipe->screen->texture_create(pipe->screen, &templ);
68 }
69
70 /**
71 * Allocate a renderbuffer for a an on-screen window (not a user-created
72 * renderbuffer). The window system code determines the format.
73 */
74 static struct st_renderbuffer *
75 st_new_renderbuffer_fb(enum pipe_format format)
76 {
77 struct st_renderbuffer *strb;
78
79 strb = CALLOC_STRUCT(st_renderbuffer);
80 if (!strb) {
81 /*_vega_error(NULL, VG_OUT_OF_MEMORY, "creating renderbuffer");*/
82 return NULL;
83 }
84
85 strb->format = format;
86
87 return strb;
88 }
89
90
91 /**
92 * This is called to allocate the original drawing surface, and
93 * during window resize.
94 */
95 static VGboolean
96 st_renderbuffer_alloc_storage(struct vg_context * ctx,
97 struct st_renderbuffer *strb,
98 VGuint width, VGuint height)
99 {
100 struct pipe_context *pipe = ctx->pipe;
101 unsigned surface_usage;
102
103 /* Free the old surface and texture
104 */
105 pipe_surface_reference(&strb->surface, NULL);
106 pipe_texture_reference(&strb->texture, NULL);
107
108
109 /* Probably need dedicated flags for surface usage too:
110 */
111 surface_usage = (PIPE_BUFFER_USAGE_GPU_READ |
112 PIPE_BUFFER_USAGE_GPU_WRITE);
113
114 strb->texture = create_texture(pipe, strb->format,
115 width, height);
116
117 if (!strb->texture)
118 return FALSE;
119
120 strb->surface = pipe->screen->get_tex_surface(pipe->screen,
121 strb->texture,
122 0, 0, 0,
123 surface_usage);
124 strb->width = width;
125 strb->height = height;
126
127 assert(strb->surface->width == width);
128 assert(strb->surface->height == height);
129
130 return strb->surface != NULL;
131 }
132
133 struct vg_context * st_create_context(struct pipe_context *pipe,
134 const void *visual,
135 struct vg_context *share)
136 {
137 struct vg_context *ctx = vg_create_context(pipe, visual, share);
138 /*debug_printf("--------- CREATE CONTEXT %p\n", ctx);*/
139 return ctx;
140 }
141
142 void st_destroy_context(struct vg_context *st)
143 {
144 /*debug_printf("--------- DESTROY CONTEXT %p\n", st);*/
145 vg_destroy_context(st);
146 }
147
148 void st_copy_context_state(struct vg_context *dst, struct vg_context *src,
149 uint mask)
150 {
151 fprintf(stderr, "FIXME: %s\n", __FUNCTION__);
152 }
153
154 void st_get_framebuffer_dimensions(struct st_framebuffer *stfb,
155 uint *width,
156 uint *height)
157 {
158 *width = stfb->strb->width;
159 *height = stfb->strb->height;
160 }
161
162 struct st_framebuffer * st_create_framebuffer(const void *visual,
163 enum pipe_format colorFormat,
164 enum pipe_format depthFormat,
165 enum pipe_format stencilFormat,
166 uint width, uint height,
167 void *privateData)
168 {
169 struct st_framebuffer *stfb = CALLOC_STRUCT(st_framebuffer);
170 if (stfb) {
171 struct st_renderbuffer *rb =
172 st_new_renderbuffer_fb(colorFormat);
173 stfb->strb = rb;
174 #if 0
175 if (doubleBuffer) {
176 struct st_renderbuffer *rb =
177 st_new_renderbuffer_fb(colorFormat);
178 }
179 #endif
180
181 /* we want to combine the depth/stencil */
182 if (stencilFormat == depthFormat)
183 stfb->dsrb = st_new_renderbuffer_fb(stencilFormat);
184 else
185 stfb->dsrb = st_new_renderbuffer_fb(PIPE_FORMAT_S8Z24_UNORM);
186
187 /*### currently we always allocate it but it's possible it's
188 not necessary if EGL_ALPHA_MASK_SIZE was 0
189 */
190 stfb->alpha_mask = 0;
191
192 stfb->init_width = width;
193 stfb->init_height = height;
194 stfb->privateData = privateData;
195 }
196
197 return stfb;
198 }
199
200 static void setup_new_alpha_mask(struct vg_context *ctx,
201 struct st_framebuffer *stfb,
202 uint width, uint height)
203 {
204 struct pipe_context *pipe = ctx->pipe;
205 struct pipe_texture *old_texture = stfb->alpha_mask;
206
207 /*
208 we use PIPE_FORMAT_A8R8G8B8_UNORM because we want to render to
209 this texture and use it as a sampler, so while this wastes some
210 space it makes both of those a lot simpler
211 */
212 stfb->alpha_mask =
213 create_texture(pipe, PIPE_FORMAT_A8R8G8B8_UNORM, width, height);
214
215 if (!stfb->alpha_mask) {
216 if (old_texture)
217 pipe_texture_reference(&old_texture, NULL);
218 return;
219 }
220
221 vg_validate_state(ctx);
222
223 /* alpha mask starts with 1.f alpha */
224 mask_fill(0, 0, width, height, 1.f);
225
226 /* if we had an old surface copy it over */
227 if (old_texture) {
228 struct pipe_surface *surface = pipe->screen->get_tex_surface(
229 pipe->screen,
230 stfb->alpha_mask,
231 0, 0, 0,
232 PIPE_BUFFER_USAGE_GPU_WRITE);
233 struct pipe_surface *old_surface = pipe->screen->get_tex_surface(
234 pipe->screen,
235 old_texture,
236 0, 0, 0,
237 PIPE_BUFFER_USAGE_GPU_READ);
238 if (pipe->surface_copy) {
239 pipe->surface_copy(pipe,
240 surface,
241 0, 0,
242 old_surface,
243 0, 0,
244 MIN2(old_surface->width, width),
245 MIN2(old_surface->height, height));
246 } else {
247 util_surface_copy(pipe, FALSE,
248 surface,
249 0, 0,
250 old_surface,
251 0, 0,
252 MIN2(old_surface->width, width),
253 MIN2(old_surface->height, height));
254 }
255 if (surface)
256 pipe_surface_reference(&surface, NULL);
257 if (old_surface)
258 pipe_surface_reference(&old_surface, NULL);
259 }
260
261 /* Free the old texture
262 */
263 if (old_texture)
264 pipe_texture_reference(&old_texture, NULL);
265 }
266
267 void st_resize_framebuffer(struct st_framebuffer *stfb,
268 uint width, uint height)
269 {
270 struct vg_context *ctx = vg_current_context();
271 struct st_renderbuffer *strb = stfb->strb;
272 struct pipe_framebuffer_state *state;
273
274 if (!ctx)
275 return;
276
277 state = &ctx->state.g3d.fb;
278
279 /* If this is a noop, exit early and don't do the clear, etc below.
280 */
281 if (strb->width == width &&
282 strb->height == height &&
283 state->zsbuf)
284 return;
285
286 if (strb->width != width || strb->height != height)
287 st_renderbuffer_alloc_storage(ctx, strb,
288 width, height);
289
290 if (stfb->dsrb->width != width || stfb->dsrb->height != height)
291 st_renderbuffer_alloc_storage(ctx, stfb->dsrb,
292 width, height);
293
294 {
295 VGuint i;
296
297 memset(state, 0, sizeof(struct pipe_framebuffer_state));
298
299 state->width = width;
300 state->height = height;
301
302 state->nr_cbufs = 1;
303 state->cbufs[0] = strb->surface;
304 for (i = 1; i < PIPE_MAX_COLOR_BUFS; ++i)
305 state->cbufs[i] = 0;
306
307 state->zsbuf = stfb->dsrb->surface;
308
309 cso_set_framebuffer(ctx->cso_context, state);
310 }
311
312 ctx->state.dirty |= VIEWPORT_DIRTY;
313 ctx->state.dirty |= DEPTH_STENCIL_DIRTY;/*to reset the scissors*/
314
315 ctx->pipe->clear(ctx->pipe, PIPE_CLEAR_DEPTHSTENCIL,
316 NULL, 0.0, 0);
317
318 /* we need all the other state already set */
319
320 setup_new_alpha_mask(ctx, stfb, width, height);
321
322 pipe_texture_reference( &stfb->blend_texture, NULL );
323 stfb->blend_texture = create_texture(ctx->pipe, PIPE_FORMAT_A8R8G8B8_UNORM,
324 width, height);
325 }
326
327 void st_set_framebuffer_surface(struct st_framebuffer *stfb,
328 uint surfIndex, struct pipe_surface *surf)
329 {
330 struct st_renderbuffer *rb = stfb->strb;
331
332 /* unreference existing surfaces */
333 pipe_surface_reference( &rb->surface, NULL );
334 pipe_texture_reference( &rb->texture, NULL );
335
336 /* reference new ones */
337 pipe_surface_reference( &rb->surface, surf );
338 pipe_texture_reference( &rb->texture, surf->texture );
339
340 rb->width = surf->width;
341 rb->height = surf->height;
342 }
343
344 int st_get_framebuffer_surface(struct st_framebuffer *stfb,
345 uint surfIndex, struct pipe_surface **surf)
346 {
347 struct st_renderbuffer *rb = stfb->strb;
348 *surf = rb->surface;
349 return VG_TRUE;
350 }
351
352 int st_get_framebuffer_texture(struct st_framebuffer *stfb,
353 uint surfIndex, struct pipe_texture **tex)
354 {
355 struct st_renderbuffer *rb = stfb->strb;
356 *tex = rb->texture;
357 return VG_TRUE;
358 }
359
360 void * st_framebuffer_private(struct st_framebuffer *stfb)
361 {
362 return stfb->privateData;
363 }
364
365 void st_unreference_framebuffer(struct st_framebuffer *stfb)
366 {
367 /* FIXME */
368 }
369
370 void st_make_current(struct vg_context *st,
371 struct st_framebuffer *draw,
372 struct st_framebuffer *read)
373 {
374 vg_set_current_context(st);
375 if (st) {
376 st->draw_buffer = draw;
377 }
378 }
379
380 struct vg_context *st_get_current(void)
381 {
382 return vg_current_context();
383 }
384
385 void st_flush(struct vg_context *st, uint pipeFlushFlags,
386 struct pipe_fence_handle **fence)
387 {
388 st->pipe->flush(st->pipe, pipeFlushFlags, fence);
389 }
390
391 void st_finish(struct vg_context *st)
392 {
393 struct pipe_fence_handle *fence = NULL;
394
395 st_flush(st, PIPE_FLUSH_RENDER_CACHE, &fence);
396
397 st->pipe->screen->fence_finish(st->pipe->screen, fence, 0);
398 st->pipe->screen->fence_reference(st->pipe->screen, &fence, NULL);
399 }
400
401 void st_notify_swapbuffers(struct st_framebuffer *stfb)
402 {
403 struct vg_context *ctx = vg_current_context();
404 if (ctx && ctx->draw_buffer == stfb) {
405 st_flush(ctx,
406 PIPE_FLUSH_RENDER_CACHE |
407 PIPE_FLUSH_SWAPBUFFERS |
408 PIPE_FLUSH_FRAME,
409 NULL);
410 }
411 }
412
413 void st_notify_swapbuffers_complete(struct st_framebuffer *stfb)
414 {
415 }
416
417 int st_bind_texture_surface(struct pipe_surface *ps, int target, int level,
418 enum pipe_format format)
419 {
420 return 0;
421 }
422
423 int st_unbind_texture_surface(struct pipe_surface *ps, int target, int level)
424 {
425 return 0;
426 }