Squashed commit of the following:
[profile/ivi/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_resource *
44 create_texture(struct pipe_context *pipe, enum pipe_format format,
45                     VGint width, VGint height)
46 {
47    struct pipe_resource 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.bind = PIPE_BIND_DEPTH_STENCIL;
66    } else {
67       templ.bind = (PIPE_BIND_DISPLAY_TARGET |
68                     PIPE_BIND_RENDER_TARGET |
69                     PIPE_BIND_SAMPLER_VIEW);
70    }
71
72    return pipe->screen->resource_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_resource *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_resource_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_resource_reference(&strb->texture, NULL);
133
134
135    /* Probably need dedicated flags for surface usage too:
136     */
137    surface_usage = (PIPE_BIND_RENDER_TARGET  |
138                     PIPE_BIND_BLIT_SOURCE |
139                     PIPE_BIND_BLIT_DESTINATION);
140
141    strb->texture = create_texture(pipe, strb->format, width, height);
142
143
144    if (!strb->texture)
145       return FALSE;
146
147    strb->surface = pipe->screen->get_tex_surface(pipe->screen,
148                                                  strb->texture,
149                                                  0, 0, 0,
150                                                  surface_usage);
151    strb->width = width;
152    strb->height = height;
153
154    assert(strb->surface->width == width);
155    assert(strb->surface->height == height);
156
157    return strb->surface != NULL;
158 }
159
160 struct vg_context * st_create_context(struct pipe_context *pipe,
161                                       const void *visual,
162                                       struct vg_context *share)
163 {
164    struct vg_context *ctx = vg_create_context(pipe, visual, share);
165    /*debug_printf("--------- CREATE CONTEXT %p\n", ctx);*/
166    return ctx;
167 }
168
169 void st_destroy_context(struct vg_context *st)
170 {
171    /*debug_printf("--------- DESTROY CONTEXT %p\n", st);*/
172    vg_destroy_context(st);
173 }
174
175 void st_copy_context_state(struct vg_context *dst, struct vg_context *src,
176                            uint mask)
177 {
178    fprintf(stderr, "FIXME: %s\n", __FUNCTION__);
179 }
180
181 void st_get_framebuffer_dimensions(struct st_framebuffer *stfb,
182                                    uint *width,
183                                    uint *height)
184 {
185    *width = stfb->strb->width;
186    *height = stfb->strb->height;
187 }
188
189 struct st_framebuffer * st_create_framebuffer(const void *visual,
190                                               enum pipe_format colorFormat,
191                                               enum pipe_format depthFormat,
192                                               enum pipe_format stencilFormat,
193                                               uint width, uint height,
194                                               void *privateData)
195 {
196    struct st_framebuffer *stfb = CALLOC_STRUCT(st_framebuffer);
197    if (stfb) {
198       struct st_renderbuffer *rb =
199          st_new_renderbuffer_fb(colorFormat);
200       stfb->strb = rb;
201 #if 0
202       if (doubleBuffer) {
203          struct st_renderbuffer *rb =
204             st_new_renderbuffer_fb(colorFormat);
205       }
206 #endif
207
208       /* we want to combine the depth/stencil */
209       if (stencilFormat == depthFormat)
210          stfb->dsrb = st_new_renderbuffer_fb(stencilFormat);
211       else
212          stfb->dsrb = st_new_renderbuffer_fb(PIPE_FORMAT_Z24_UNORM_S8_USCALED);
213
214       /*### currently we always allocate it but it's possible it's
215         not necessary if EGL_ALPHA_MASK_SIZE was 0
216       */
217       stfb->alpha_mask_view = NULL;
218
219       stfb->width = width;
220       stfb->height = height;
221       stfb->privateData = privateData;
222    }
223
224    return stfb;
225 }
226
227 static void setup_new_alpha_mask(struct vg_context *ctx,
228                                  struct st_framebuffer *stfb,
229                                  uint width, uint height)
230 {
231    struct pipe_context *pipe = ctx->pipe;
232    struct pipe_sampler_view *old_sampler_view = stfb->alpha_mask_view;
233
234    /*
235      we use PIPE_FORMAT_B8G8R8A8_UNORM because we want to render to
236      this texture and use it as a sampler, so while this wastes some
237      space it makes both of those a lot simpler
238    */
239    stfb->alpha_mask_view =
240       create_tex_and_view(pipe, PIPE_FORMAT_B8G8R8A8_UNORM, width, height);
241
242    if (!stfb->alpha_mask_view) {
243       if (old_sampler_view)
244          pipe_sampler_view_reference(&old_sampler_view, NULL);
245       return;
246    }
247
248    vg_validate_state(ctx);
249
250    /* alpha mask starts with 1.f alpha */
251    mask_fill(0, 0, width, height, 1.f);
252
253    /* if we had an old surface copy it over */
254    if (old_sampler_view) {
255       struct pipe_surface *surface = pipe->screen->get_tex_surface(
256          pipe->screen,
257          stfb->alpha_mask_view->texture,
258          0, 0, 0,
259          PIPE_BIND_RENDER_TARGET |
260          PIPE_BIND_BLIT_DESTINATION);
261       struct pipe_surface *old_surface = pipe->screen->get_tex_surface(
262          pipe->screen,
263          old_sampler_view->texture,
264          0, 0, 0,
265          PIPE_BIND_BLIT_SOURCE);
266       if (pipe->surface_copy) {
267          pipe->surface_copy(pipe,
268                             surface,
269                             0, 0,
270                             old_surface,
271                             0, 0,
272                             MIN2(old_surface->width, width),
273                             MIN2(old_surface->height, height));
274       } else {
275          util_surface_copy(pipe, FALSE,
276                            surface,
277                            0, 0,
278                            old_surface,
279                            0, 0,
280                            MIN2(old_surface->width, width),
281                            MIN2(old_surface->height, height));
282       }
283       if (surface)
284          pipe_surface_reference(&surface, NULL);
285       if (old_surface)
286          pipe_surface_reference(&old_surface, NULL);
287    }
288
289    /* Free the old texture
290     */
291    if (old_sampler_view)
292       pipe_sampler_view_reference(&old_sampler_view, NULL);
293 }
294
295 void st_resize_framebuffer(struct st_framebuffer *stfb,
296                            uint width, uint height)
297 {
298    struct vg_context *ctx = vg_current_context();
299    struct st_renderbuffer *strb = stfb->strb;
300    struct pipe_framebuffer_state *state;
301
302    if (!ctx)
303       return;
304
305    state = &ctx->state.g3d.fb;
306
307    /* If this is a noop, exit early and don't do the clear, etc below.
308     */
309    if (stfb->width == width &&
310        stfb->height == height &&
311        state->zsbuf)
312       return;
313
314    stfb->width = width;
315    stfb->height = height;
316
317    if (strb->width != width || strb->height != height)
318       st_renderbuffer_alloc_storage(ctx, strb,
319                                  width, height);
320
321    if (stfb->dsrb->width != width || stfb->dsrb->height != height)
322       st_renderbuffer_alloc_storage(ctx, stfb->dsrb,
323                                  width, height);
324
325    {
326       VGuint i;
327
328       memset(state, 0, sizeof(struct pipe_framebuffer_state));
329
330       state->width  = width;
331       state->height = height;
332
333       state->nr_cbufs = 1;
334       state->cbufs[0] = strb->surface;
335       for (i = 1; i < PIPE_MAX_COLOR_BUFS; ++i)
336          state->cbufs[i] = 0;
337
338       state->zsbuf = stfb->dsrb->surface;
339
340       cso_set_framebuffer(ctx->cso_context, state);
341    }
342
343    ctx->state.dirty |= VIEWPORT_DIRTY;
344    ctx->state.dirty |= DEPTH_STENCIL_DIRTY;/*to reset the scissors*/
345
346    ctx->pipe->clear(ctx->pipe, PIPE_CLEAR_DEPTHSTENCIL,
347                     NULL, 0.0, 0);
348
349    /* we need all the other state already set */
350
351    setup_new_alpha_mask(ctx, stfb, width, height);
352
353    pipe_sampler_view_reference( &stfb->blend_texture_view, NULL );
354    stfb->blend_texture_view = create_tex_and_view(ctx->pipe, PIPE_FORMAT_B8G8R8A8_UNORM,
355                                                   width, height);
356 }
357
358 void st_set_framebuffer_surface(struct st_framebuffer *stfb,
359                                 uint surfIndex, struct pipe_surface *surf)
360 {
361    struct st_renderbuffer *rb = stfb->strb;
362
363    /* unreference existing surfaces */
364    pipe_surface_reference( &rb->surface, NULL );
365    pipe_resource_reference( &rb->texture, NULL );
366
367    /* reference new ones */
368    pipe_surface_reference( &rb->surface, surf );
369    pipe_resource_reference( &rb->texture, surf->texture );
370
371    rb->width  = surf->width;
372    rb->height = surf->height;
373 }
374
375 int st_get_framebuffer_surface(struct st_framebuffer *stfb,
376                                uint surfIndex, struct pipe_surface **surf)
377 {
378    struct st_renderbuffer *rb = stfb->strb;
379    *surf = rb->surface;
380    return VG_TRUE;
381 }
382
383 int st_get_framebuffer_texture(struct st_framebuffer *stfb,
384                                uint surfIndex, struct pipe_resource **tex)
385 {
386    struct st_renderbuffer *rb = stfb->strb;
387    *tex = rb->texture;
388    return VG_TRUE;
389 }
390
391 void * st_framebuffer_private(struct st_framebuffer *stfb)
392 {
393    return stfb->privateData;
394 }
395
396 void st_unreference_framebuffer(struct st_framebuffer *stfb)
397 {
398    /* FIXME */
399 }
400
401 boolean st_make_current(struct vg_context *st,
402                         struct st_framebuffer *draw,
403                         struct st_framebuffer *read,
404                         void *winsys_drawable_handle)
405 {
406    vg_set_current_context(st);
407    if (st)
408       st->draw_buffer = draw;
409    return VG_TRUE;
410 }
411
412 struct vg_context *st_get_current(void)
413 {
414    return vg_current_context();
415 }
416
417 void st_flush(struct vg_context *st, uint pipeFlushFlags,
418               struct pipe_fence_handle **fence)
419 {
420    st->pipe->flush(st->pipe, pipeFlushFlags, fence);
421 }
422
423 void st_finish(struct vg_context *st)
424 {
425    struct pipe_fence_handle *fence = NULL;
426
427    st_flush(st, PIPE_FLUSH_RENDER_CACHE, &fence);
428
429    st->pipe->screen->fence_finish(st->pipe->screen, fence, 0);
430    st->pipe->screen->fence_reference(st->pipe->screen, &fence, NULL);
431 }
432
433 void st_notify_swapbuffers(struct st_framebuffer *stfb)
434 {
435    struct vg_context *ctx = vg_current_context();
436    if (ctx && ctx->draw_buffer == stfb) {
437       st_flush(ctx,
438                PIPE_FLUSH_RENDER_CACHE | 
439                PIPE_FLUSH_SWAPBUFFERS |
440                PIPE_FLUSH_FRAME,
441                NULL);
442    }
443 }
444
445 void st_notify_swapbuffers_complete(struct st_framebuffer *stfb)
446 {
447 }
448
449 int st_bind_texture_surface(struct pipe_surface *ps, int target, int level,
450                             enum pipe_format format)
451 {
452    return 0;
453 }
454
455 int st_unbind_texture_surface(struct pipe_surface *ps, int target, int level)
456 {
457    return 0;
458 }
459
460 st_proc st_get_proc_address(const char *procname)
461 {
462    return NULL;
463 }