1 /**************************************************************************
3 * Copyright 2009 VMware, Inc. All Rights Reserved.
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:
13 * The above copyright notice and this permission notice (including the
14 * next paragraph) shall be included in all copies or substantial portions
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.
25 **************************************************************************/
27 #include "vg_context.h"
28 #include "vg_tracker.h"
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"
40 /* advertise OpenVG support */
41 PUBLIC const int st_api_OpenVG = 1;
43 static struct pipe_resource *
44 create_texture(struct pipe_context *pipe, enum pipe_format format,
45 VGint width, VGint height)
47 struct pipe_resource templ;
49 memset(&templ, 0, sizeof(templ));
51 if (format != PIPE_FORMAT_NONE) {
52 templ.format = format;
55 templ.format = PIPE_FORMAT_B8G8R8A8_UNORM;
58 templ.target = PIPE_TEXTURE_2D;
60 templ.height0 = height;
64 if (util_format_get_component_bits(format, UTIL_FORMAT_COLORSPACE_ZS, 1)) {
65 templ.bind = PIPE_BIND_DEPTH_STENCIL;
67 templ.bind = (PIPE_BIND_DISPLAY_TARGET |
68 PIPE_BIND_RENDER_TARGET |
69 PIPE_BIND_SAMPLER_VIEW);
72 return pipe->screen->resource_create(pipe->screen, &templ);
75 static struct pipe_sampler_view *
76 create_tex_and_view(struct pipe_context *pipe, enum pipe_format format,
77 VGint width, VGint height)
79 struct pipe_resource *texture;
80 struct pipe_sampler_view view_templ;
81 struct pipe_sampler_view *view;
83 texture = create_texture(pipe, format, width, height);
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);
97 * Allocate a renderbuffer for a an on-screen window (not a user-created
98 * renderbuffer). The window system code determines the format.
100 static struct st_renderbuffer *
101 st_new_renderbuffer_fb(enum pipe_format format)
103 struct st_renderbuffer *strb;
105 strb = CALLOC_STRUCT(st_renderbuffer);
107 /*_vega_error(NULL, VG_OUT_OF_MEMORY, "creating renderbuffer");*/
111 strb->format = format;
118 * This is called to allocate the original drawing surface, and
119 * during window resize.
122 st_renderbuffer_alloc_storage(struct vg_context * ctx,
123 struct st_renderbuffer *strb,
124 VGuint width, VGuint height)
126 struct pipe_context *pipe = ctx->pipe;
127 unsigned surface_usage;
129 /* Free the old surface and texture
131 pipe_surface_reference(&strb->surface, NULL);
132 pipe_resource_reference(&strb->texture, NULL);
135 /* Probably need dedicated flags for surface usage too:
137 surface_usage = (PIPE_BIND_RENDER_TARGET |
138 PIPE_BIND_BLIT_SOURCE |
139 PIPE_BIND_BLIT_DESTINATION);
141 strb->texture = create_texture(pipe, strb->format, width, height);
147 strb->surface = pipe->screen->get_tex_surface(pipe->screen,
152 strb->height = height;
154 assert(strb->surface->width == width);
155 assert(strb->surface->height == height);
157 return strb->surface != NULL;
160 struct vg_context * st_create_context(struct pipe_context *pipe,
162 struct vg_context *share)
164 struct vg_context *ctx = vg_create_context(pipe, visual, share);
165 /*debug_printf("--------- CREATE CONTEXT %p\n", ctx);*/
169 void st_destroy_context(struct vg_context *st)
171 /*debug_printf("--------- DESTROY CONTEXT %p\n", st);*/
172 vg_destroy_context(st);
175 void st_copy_context_state(struct vg_context *dst, struct vg_context *src,
178 fprintf(stderr, "FIXME: %s\n", __FUNCTION__);
181 void st_get_framebuffer_dimensions(struct st_framebuffer *stfb,
185 *width = stfb->strb->width;
186 *height = stfb->strb->height;
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,
196 struct st_framebuffer *stfb = CALLOC_STRUCT(st_framebuffer);
198 struct st_renderbuffer *rb =
199 st_new_renderbuffer_fb(colorFormat);
203 struct st_renderbuffer *rb =
204 st_new_renderbuffer_fb(colorFormat);
208 /* we want to combine the depth/stencil */
209 if (stencilFormat == depthFormat)
210 stfb->dsrb = st_new_renderbuffer_fb(stencilFormat);
212 stfb->dsrb = st_new_renderbuffer_fb(PIPE_FORMAT_Z24_UNORM_S8_USCALED);
214 /*### currently we always allocate it but it's possible it's
215 not necessary if EGL_ALPHA_MASK_SIZE was 0
217 stfb->alpha_mask_view = NULL;
220 stfb->height = height;
221 stfb->privateData = privateData;
227 static void setup_new_alpha_mask(struct vg_context *ctx,
228 struct st_framebuffer *stfb,
229 uint width, uint height)
231 struct pipe_context *pipe = ctx->pipe;
232 struct pipe_sampler_view *old_sampler_view = stfb->alpha_mask_view;
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
239 stfb->alpha_mask_view =
240 create_tex_and_view(pipe, PIPE_FORMAT_B8G8R8A8_UNORM, width, height);
242 if (!stfb->alpha_mask_view) {
243 if (old_sampler_view)
244 pipe_sampler_view_reference(&old_sampler_view, NULL);
248 vg_validate_state(ctx);
250 /* alpha mask starts with 1.f alpha */
251 mask_fill(0, 0, width, height, 1.f);
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(
257 stfb->alpha_mask_view->texture,
259 PIPE_BIND_RENDER_TARGET |
260 PIPE_BIND_BLIT_DESTINATION);
261 struct pipe_surface *old_surface = pipe->screen->get_tex_surface(
263 old_sampler_view->texture,
265 PIPE_BIND_BLIT_SOURCE);
266 if (pipe->surface_copy) {
267 pipe->surface_copy(pipe,
272 MIN2(old_surface->width, width),
273 MIN2(old_surface->height, height));
275 util_surface_copy(pipe, FALSE,
280 MIN2(old_surface->width, width),
281 MIN2(old_surface->height, height));
284 pipe_surface_reference(&surface, NULL);
286 pipe_surface_reference(&old_surface, NULL);
289 /* Free the old texture
291 if (old_sampler_view)
292 pipe_sampler_view_reference(&old_sampler_view, NULL);
295 void st_resize_framebuffer(struct st_framebuffer *stfb,
296 uint width, uint height)
298 struct vg_context *ctx = vg_current_context();
299 struct st_renderbuffer *strb = stfb->strb;
300 struct pipe_framebuffer_state *state;
305 state = &ctx->state.g3d.fb;
307 /* If this is a noop, exit early and don't do the clear, etc below.
309 if (stfb->width == width &&
310 stfb->height == height &&
315 stfb->height = height;
317 if (strb->width != width || strb->height != height)
318 st_renderbuffer_alloc_storage(ctx, strb,
321 if (stfb->dsrb->width != width || stfb->dsrb->height != height)
322 st_renderbuffer_alloc_storage(ctx, stfb->dsrb,
328 memset(state, 0, sizeof(struct pipe_framebuffer_state));
330 state->width = width;
331 state->height = height;
334 state->cbufs[0] = strb->surface;
335 for (i = 1; i < PIPE_MAX_COLOR_BUFS; ++i)
338 state->zsbuf = stfb->dsrb->surface;
340 cso_set_framebuffer(ctx->cso_context, state);
343 ctx->state.dirty |= VIEWPORT_DIRTY;
344 ctx->state.dirty |= DEPTH_STENCIL_DIRTY;/*to reset the scissors*/
346 ctx->pipe->clear(ctx->pipe, PIPE_CLEAR_DEPTHSTENCIL,
349 /* we need all the other state already set */
351 setup_new_alpha_mask(ctx, stfb, width, height);
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,
358 void st_set_framebuffer_surface(struct st_framebuffer *stfb,
359 uint surfIndex, struct pipe_surface *surf)
361 struct st_renderbuffer *rb = stfb->strb;
363 /* unreference existing surfaces */
364 pipe_surface_reference( &rb->surface, NULL );
365 pipe_resource_reference( &rb->texture, NULL );
367 /* reference new ones */
368 pipe_surface_reference( &rb->surface, surf );
369 pipe_resource_reference( &rb->texture, surf->texture );
371 rb->width = surf->width;
372 rb->height = surf->height;
375 int st_get_framebuffer_surface(struct st_framebuffer *stfb,
376 uint surfIndex, struct pipe_surface **surf)
378 struct st_renderbuffer *rb = stfb->strb;
383 int st_get_framebuffer_texture(struct st_framebuffer *stfb,
384 uint surfIndex, struct pipe_resource **tex)
386 struct st_renderbuffer *rb = stfb->strb;
391 void * st_framebuffer_private(struct st_framebuffer *stfb)
393 return stfb->privateData;
396 void st_unreference_framebuffer(struct st_framebuffer *stfb)
401 boolean st_make_current(struct vg_context *st,
402 struct st_framebuffer *draw,
403 struct st_framebuffer *read,
404 void *winsys_drawable_handle)
406 vg_set_current_context(st);
408 st->draw_buffer = draw;
412 struct vg_context *st_get_current(void)
414 return vg_current_context();
417 void st_flush(struct vg_context *st, uint pipeFlushFlags,
418 struct pipe_fence_handle **fence)
420 st->pipe->flush(st->pipe, pipeFlushFlags, fence);
423 void st_finish(struct vg_context *st)
425 struct pipe_fence_handle *fence = NULL;
427 st_flush(st, PIPE_FLUSH_RENDER_CACHE, &fence);
429 st->pipe->screen->fence_finish(st->pipe->screen, fence, 0);
430 st->pipe->screen->fence_reference(st->pipe->screen, &fence, NULL);
433 void st_notify_swapbuffers(struct st_framebuffer *stfb)
435 struct vg_context *ctx = vg_current_context();
436 if (ctx && ctx->draw_buffer == stfb) {
438 PIPE_FLUSH_RENDER_CACHE |
439 PIPE_FLUSH_SWAPBUFFERS |
445 void st_notify_swapbuffers_complete(struct st_framebuffer *stfb)
449 int st_bind_texture_surface(struct pipe_surface *ps, int target, int level,
450 enum pipe_format format)
455 int st_unbind_texture_surface(struct pipe_surface *ps, int target, int level)
460 st_proc st_get_proc_address(const char *procname)