Merge branch 'master' into gallium-new-formats
[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 "util/u_inlines.h"
33 #include "pipe/p_screen.h"
34 #include "util/u_format.h"
35 #include "util/u_sampler.h"
36 #include "util/u_memory.h"
37 #include "util/u_math.h"
38 #include "util/u_rect.h"
39
40 /* advertise OpenVG support */
41 PUBLIC const int st_api_OpenVG = 1;
42
43 static struct pipe_texture *
44 create_texture(struct pipe_context *pipe, enum pipe_format format,
45 VGint width, VGint height)
46 {
47 struct pipe_texture templ;
48
49 memset(&templ, 0, sizeof(templ));
50
51 if (format != PIPE_FORMAT_NONE) {
52 templ.format = format;
53 }
54 else {
55 templ.format = PIPE_FORMAT_B8G8R8A8_UNORM;
56 }
57
58 templ.target = PIPE_TEXTURE_2D;
59 templ.width0 = width;
60 templ.height0 = height;
61 templ.depth0 = 1;
62 templ.last_level = 0;
63
64 if (util_format_get_component_bits(format, UTIL_FORMAT_COLORSPACE_ZS, 1)) {
65 templ.tex_usage = PIPE_TEXTURE_USAGE_DEPTH_STENCIL;
66 } else {
67 templ.tex_usage = (PIPE_TEXTURE_USAGE_DISPLAY_TARGET |
68 PIPE_TEXTURE_USAGE_RENDER_TARGET |
69 PIPE_TEXTURE_USAGE_SAMPLER);
70 }
71
72 return pipe->screen->texture_create(pipe->screen, &templ);
73 }
74
75 static struct pipe_sampler_view *
76 create_tex_and_view(struct pipe_context *pipe, enum pipe_format format,
77 VGint width, VGint height)
78 {
79 struct pipe_texture *texture;
80 struct pipe_sampler_view view_templ;
81 struct pipe_sampler_view *view;
82
83 texture = create_texture(pipe, format, width, height);
84
85 if (!texture)
86 return NULL;
87
88 u_sampler_view_default_template(&view_templ, texture, texture->format);
89 view = pipe->create_sampler_view(pipe, texture, &view_templ);
90 /* want the texture to go away if the view is freed */
91 pipe_texture_reference(&texture, NULL);
92
93 return view;
94 }
95
96 /**
97 * Allocate a renderbuffer for a an on-screen window (not a user-created
98 * renderbuffer). The window system code determines the format.
99 */
100 static struct st_renderbuffer *
101 st_new_renderbuffer_fb(enum pipe_format format)
102 {
103 struct st_renderbuffer *strb;
104
105 strb = CALLOC_STRUCT(st_renderbuffer);
106 if (!strb) {
107 /*_vega_error(NULL, VG_OUT_OF_MEMORY, "creating renderbuffer");*/
108 return NULL;
109 }
110
111 strb->format = format;
112
113 return strb;
114 }
115
116
117 /**
118 * This is called to allocate the original drawing surface, and
119 * during window resize.
120 */
121 static VGboolean
122 st_renderbuffer_alloc_storage(struct vg_context * ctx,
123 struct st_renderbuffer *strb,
124 VGuint width, VGuint height)
125 {
126 struct pipe_context *pipe = ctx->pipe;
127 unsigned surface_usage;
128
129 /* Free the old surface and texture
130 */
131 pipe_surface_reference(&strb->surface, NULL);
132 pipe_texture_reference(&strb->texture, NULL);
133
134
135 /* Probably need dedicated flags for surface usage too:
136 */
137 surface_usage = (PIPE_BUFFER_USAGE_GPU_READ |
138 PIPE_BUFFER_USAGE_GPU_WRITE);
139
140 strb->texture = create_texture(pipe, strb->format, width, height);
141
142
143 if (!strb->texture)
144 return FALSE;
145
146 strb->surface = pipe->screen->get_tex_surface(pipe->screen,
147 strb->texture,
148 0, 0, 0,
149 surface_usage);
150 strb->width = width;
151 strb->height = height;
152
153 assert(strb->surface->width == width);
154 assert(strb->surface->height == height);
155
156 return strb->surface != NULL;
157 }
158
159 struct vg_context * st_create_context(struct pipe_context *pipe,
160 const void *visual,
161 struct vg_context *share)
162 {
163 struct vg_context *ctx = vg_create_context(pipe, visual, share);
164 /*debug_printf("--------- CREATE CONTEXT %p\n", ctx);*/
165 return ctx;
166 }
167
168 void st_destroy_context(struct vg_context *st)
169 {
170 /*debug_printf("--------- DESTROY CONTEXT %p\n", st);*/
171 vg_destroy_context(st);
172 }
173
174 void st_copy_context_state(struct vg_context *dst, struct vg_context *src,
175 uint mask)
176 {
177 fprintf(stderr, "FIXME: %s\n", __FUNCTION__);
178 }
179
180 void st_get_framebuffer_dimensions(struct st_framebuffer *stfb,
181 uint *width,
182 uint *height)
183 {
184 *width = stfb->strb->width;
185 *height = stfb->strb->height;
186 }
187
188 struct st_framebuffer * st_create_framebuffer(const void *visual,
189 enum pipe_format colorFormat,
190 enum pipe_format depthFormat,
191 enum pipe_format stencilFormat,
192 uint width, uint height,
193 void *privateData)
194 {
195 struct st_framebuffer *stfb = CALLOC_STRUCT(st_framebuffer);
196 if (stfb) {
197 struct st_renderbuffer *rb =
198 st_new_renderbuffer_fb(colorFormat);
199 stfb->strb = rb;
200 #if 0
201 if (doubleBuffer) {
202 struct st_renderbuffer *rb =
203 st_new_renderbuffer_fb(colorFormat);
204 }
205 #endif
206
207 /* we want to combine the depth/stencil */
208 if (stencilFormat == depthFormat)
209 stfb->dsrb = st_new_renderbuffer_fb(stencilFormat);
210 else
211 stfb->dsrb = st_new_renderbuffer_fb(PIPE_FORMAT_Z24_UNORM_S8_USCALED);
212
213 /*### currently we always allocate it but it's possible it's
214 not necessary if EGL_ALPHA_MASK_SIZE was 0
215 */
216 stfb->alpha_mask_view = NULL;
217
218 stfb->width = width;
219 stfb->height = height;
220 stfb->privateData = privateData;
221 }
222
223 return stfb;
224 }
225
226 static void setup_new_alpha_mask(struct vg_context *ctx,
227 struct st_framebuffer *stfb,
228 uint width, uint height)
229 {
230 struct pipe_context *pipe = ctx->pipe;
231 struct pipe_sampler_view *old_sampler_view = stfb->alpha_mask_view;
232
233 /*
234 we use PIPE_FORMAT_B8G8R8A8_UNORM because we want to render to
235 this texture and use it as a sampler, so while this wastes some
236 space it makes both of those a lot simpler
237 */
238 stfb->alpha_mask_view =
239 create_tex_and_view(pipe, PIPE_FORMAT_B8G8R8A8_UNORM, width, height);
240
241 if (!stfb->alpha_mask_view) {
242 if (old_sampler_view)
243 pipe_sampler_view_reference(&old_sampler_view, NULL);
244 return;
245 }
246
247 vg_validate_state(ctx);
248
249 /* alpha mask starts with 1.f alpha */
250 mask_fill(0, 0, width, height, 1.f);
251
252 /* if we had an old surface copy it over */
253 if (old_sampler_view) {
254 struct pipe_surface *surface = pipe->screen->get_tex_surface(
255 pipe->screen,
256 stfb->alpha_mask_view->texture,
257 0, 0, 0,
258 PIPE_BUFFER_USAGE_GPU_WRITE);
259 struct pipe_surface *old_surface = pipe->screen->get_tex_surface(
260 pipe->screen,
261 old_sampler_view->texture,
262 0, 0, 0,
263 PIPE_BUFFER_USAGE_GPU_READ);
264 if (pipe->surface_copy) {
265 pipe->surface_copy(pipe,
266 surface,
267 0, 0,
268 old_surface,
269 0, 0,
270 MIN2(old_surface->width, width),
271 MIN2(old_surface->height, height));
272 } else {
273 util_surface_copy(pipe, FALSE,
274 surface,
275 0, 0,
276 old_surface,
277 0, 0,
278 MIN2(old_surface->width, width),
279 MIN2(old_surface->height, height));
280 }
281 if (surface)
282 pipe_surface_reference(&surface, NULL);
283 if (old_surface)
284 pipe_surface_reference(&old_surface, NULL);
285 }
286
287 /* Free the old texture
288 */
289 if (old_sampler_view)
290 pipe_sampler_view_reference(&old_sampler_view, NULL);
291 }
292
293 void st_resize_framebuffer(struct st_framebuffer *stfb,
294 uint width, uint height)
295 {
296 struct vg_context *ctx = vg_current_context();
297 struct st_renderbuffer *strb = stfb->strb;
298 struct pipe_framebuffer_state *state;
299
300 if (!ctx)
301 return;
302
303 state = &ctx->state.g3d.fb;
304
305 /* If this is a noop, exit early and don't do the clear, etc below.
306 */
307 if (stfb->width == width &&
308 stfb->height == height &&
309 state->zsbuf)
310 return;
311
312 stfb->width = width;
313 stfb->height = height;
314
315 if (strb->width != width || strb->height != height)
316 st_renderbuffer_alloc_storage(ctx, strb,
317 width, height);
318
319 if (stfb->dsrb->width != width || stfb->dsrb->height != height)
320 st_renderbuffer_alloc_storage(ctx, stfb->dsrb,
321 width, height);
322
323 {
324 VGuint i;
325
326 memset(state, 0, sizeof(struct pipe_framebuffer_state));
327
328 state->width = width;
329 state->height = height;
330
331 state->nr_cbufs = 1;
332 state->cbufs[0] = strb->surface;
333 for (i = 1; i < PIPE_MAX_COLOR_BUFS; ++i)
334 state->cbufs[i] = 0;
335
336 state->zsbuf = stfb->dsrb->surface;
337
338 cso_set_framebuffer(ctx->cso_context, state);
339 }
340
341 ctx->state.dirty |= VIEWPORT_DIRTY;
342 ctx->state.dirty |= DEPTH_STENCIL_DIRTY;/*to reset the scissors*/
343
344 ctx->pipe->clear(ctx->pipe, PIPE_CLEAR_DEPTHSTENCIL,
345 NULL, 0.0, 0);
346
347 /* we need all the other state already set */
348
349 setup_new_alpha_mask(ctx, stfb, width, height);
350
351 pipe_sampler_view_reference( &stfb->blend_texture_view, NULL );
352 stfb->blend_texture_view = create_tex_and_view(ctx->pipe, PIPE_FORMAT_B8G8R8A8_UNORM,
353 width, height);
354 }
355
356 void st_set_framebuffer_surface(struct st_framebuffer *stfb,
357 uint surfIndex, struct pipe_surface *surf)
358 {
359 struct st_renderbuffer *rb = stfb->strb;
360
361 /* unreference existing surfaces */
362 pipe_surface_reference( &rb->surface, NULL );
363 pipe_texture_reference( &rb->texture, NULL );
364
365 /* reference new ones */
366 pipe_surface_reference( &rb->surface, surf );
367 pipe_texture_reference( &rb->texture, surf->texture );
368
369 rb->width = surf->width;
370 rb->height = surf->height;
371 }
372
373 int st_get_framebuffer_surface(struct st_framebuffer *stfb,
374 uint surfIndex, struct pipe_surface **surf)
375 {
376 struct st_renderbuffer *rb = stfb->strb;
377 *surf = rb->surface;
378 return VG_TRUE;
379 }
380
381 int st_get_framebuffer_texture(struct st_framebuffer *stfb,
382 uint surfIndex, struct pipe_texture **tex)
383 {
384 struct st_renderbuffer *rb = stfb->strb;
385 *tex = rb->texture;
386 return VG_TRUE;
387 }
388
389 void * st_framebuffer_private(struct st_framebuffer *stfb)
390 {
391 return stfb->privateData;
392 }
393
394 void st_unreference_framebuffer(struct st_framebuffer *stfb)
395 {
396 /* FIXME */
397 }
398
399 boolean st_make_current(struct vg_context *st,
400 struct st_framebuffer *draw,
401 struct st_framebuffer *read,
402 void *winsys_drawable_handle)
403 {
404 vg_set_current_context(st);
405 if (st)
406 st->draw_buffer = draw;
407 return VG_TRUE;
408 }
409
410 struct vg_context *st_get_current(void)
411 {
412 return vg_current_context();
413 }
414
415 void st_flush(struct vg_context *st, uint pipeFlushFlags,
416 struct pipe_fence_handle **fence)
417 {
418 st->pipe->flush(st->pipe, pipeFlushFlags, fence);
419 }
420
421 void st_finish(struct vg_context *st)
422 {
423 struct pipe_fence_handle *fence = NULL;
424
425 st_flush(st, PIPE_FLUSH_RENDER_CACHE, &fence);
426
427 st->pipe->screen->fence_finish(st->pipe->screen, fence, 0);
428 st->pipe->screen->fence_reference(st->pipe->screen, &fence, NULL);
429 }
430
431 void st_notify_swapbuffers(struct st_framebuffer *stfb)
432 {
433 struct vg_context *ctx = vg_current_context();
434 if (ctx && ctx->draw_buffer == stfb) {
435 st_flush(ctx,
436 PIPE_FLUSH_RENDER_CACHE |
437 PIPE_FLUSH_SWAPBUFFERS |
438 PIPE_FLUSH_FRAME,
439 NULL);
440 }
441 }
442
443 void st_notify_swapbuffers_complete(struct st_framebuffer *stfb)
444 {
445 }
446
447 int st_bind_texture_surface(struct pipe_surface *ps, int target, int level,
448 enum pipe_format format)
449 {
450 return 0;
451 }
452
453 int st_unbind_texture_surface(struct pipe_surface *ps, int target, int level)
454 {
455 return 0;
456 }
457
458 st_proc st_get_proc_address(const char *procname)
459 {
460 return NULL;
461 }