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_texture
*
44 create_texture(struct pipe_context
*pipe
, enum pipe_format format
,
45 VGint width
, VGint height
)
47 struct pipe_texture 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
.tex_usage
= PIPE_TEXTURE_USAGE_DEPTH_STENCIL
;
67 templ
.tex_usage
= (PIPE_TEXTURE_USAGE_DISPLAY_TARGET
|
68 PIPE_TEXTURE_USAGE_RENDER_TARGET
|
69 PIPE_TEXTURE_USAGE_SAMPLER
);
72 return pipe
->screen
->texture_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_texture
*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_texture_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_texture_reference(&strb
->texture
, NULL
);
135 /* Probably need dedicated flags for surface usage too:
137 surface_usage
= (PIPE_BUFFER_USAGE_GPU_READ
|
138 PIPE_BUFFER_USAGE_GPU_WRITE
);
140 strb
->texture
= create_texture(pipe
, strb
->format
, width
, height
);
146 strb
->surface
= pipe
->screen
->get_tex_surface(pipe
->screen
,
151 strb
->height
= height
;
153 assert(strb
->surface
->width
== width
);
154 assert(strb
->surface
->height
== height
);
156 return strb
->surface
!= NULL
;
159 struct vg_context
* st_create_context(struct pipe_context
*pipe
,
161 struct vg_context
*share
)
163 struct vg_context
*ctx
= vg_create_context(pipe
, visual
, share
);
164 /*debug_printf("--------- CREATE CONTEXT %p\n", ctx);*/
168 void st_destroy_context(struct vg_context
*st
)
170 /*debug_printf("--------- DESTROY CONTEXT %p\n", st);*/
171 vg_destroy_context(st
);
174 void st_copy_context_state(struct vg_context
*dst
, struct vg_context
*src
,
177 fprintf(stderr
, "FIXME: %s\n", __FUNCTION__
);
180 void st_get_framebuffer_dimensions(struct st_framebuffer
*stfb
,
184 *width
= stfb
->strb
->width
;
185 *height
= stfb
->strb
->height
;
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
,
195 struct st_framebuffer
*stfb
= CALLOC_STRUCT(st_framebuffer
);
197 struct st_renderbuffer
*rb
=
198 st_new_renderbuffer_fb(colorFormat
);
202 struct st_renderbuffer
*rb
=
203 st_new_renderbuffer_fb(colorFormat
);
207 /* we want to combine the depth/stencil */
208 if (stencilFormat
== depthFormat
)
209 stfb
->dsrb
= st_new_renderbuffer_fb(stencilFormat
);
211 stfb
->dsrb
= st_new_renderbuffer_fb(PIPE_FORMAT_Z24_UNORM_S8_USCALED
);
213 /*### currently we always allocate it but it's possible it's
214 not necessary if EGL_ALPHA_MASK_SIZE was 0
216 stfb
->alpha_mask_view
= NULL
;
219 stfb
->height
= height
;
220 stfb
->privateData
= privateData
;
226 static void setup_new_alpha_mask(struct vg_context
*ctx
,
227 struct st_framebuffer
*stfb
,
228 uint width
, uint height
)
230 struct pipe_context
*pipe
= ctx
->pipe
;
231 struct pipe_sampler_view
*old_sampler_view
= stfb
->alpha_mask_view
;
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
238 stfb
->alpha_mask_view
=
239 create_tex_and_view(pipe
, PIPE_FORMAT_B8G8R8A8_UNORM
, width
, height
);
241 if (!stfb
->alpha_mask_view
) {
242 if (old_sampler_view
)
243 pipe_sampler_view_reference(&old_sampler_view
, NULL
);
247 vg_validate_state(ctx
);
249 /* alpha mask starts with 1.f alpha */
250 mask_fill(0, 0, width
, height
, 1.f
);
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(
256 stfb
->alpha_mask_view
->texture
,
258 PIPE_BUFFER_USAGE_GPU_WRITE
);
259 struct pipe_surface
*old_surface
= pipe
->screen
->get_tex_surface(
261 old_sampler_view
->texture
,
263 PIPE_BUFFER_USAGE_GPU_READ
);
264 if (pipe
->surface_copy
) {
265 pipe
->surface_copy(pipe
,
270 MIN2(old_surface
->width
, width
),
271 MIN2(old_surface
->height
, height
));
273 util_surface_copy(pipe
, FALSE
,
278 MIN2(old_surface
->width
, width
),
279 MIN2(old_surface
->height
, height
));
282 pipe_surface_reference(&surface
, NULL
);
284 pipe_surface_reference(&old_surface
, NULL
);
287 /* Free the old texture
289 if (old_sampler_view
)
290 pipe_sampler_view_reference(&old_sampler_view
, NULL
);
293 void st_resize_framebuffer(struct st_framebuffer
*stfb
,
294 uint width
, uint height
)
296 struct vg_context
*ctx
= vg_current_context();
297 struct st_renderbuffer
*strb
= stfb
->strb
;
298 struct pipe_framebuffer_state
*state
;
303 state
= &ctx
->state
.g3d
.fb
;
305 /* If this is a noop, exit early and don't do the clear, etc below.
307 if (stfb
->width
== width
&&
308 stfb
->height
== height
&&
313 stfb
->height
= height
;
315 if (strb
->width
!= width
|| strb
->height
!= height
)
316 st_renderbuffer_alloc_storage(ctx
, strb
,
319 if (stfb
->dsrb
->width
!= width
|| stfb
->dsrb
->height
!= height
)
320 st_renderbuffer_alloc_storage(ctx
, stfb
->dsrb
,
326 memset(state
, 0, sizeof(struct pipe_framebuffer_state
));
328 state
->width
= width
;
329 state
->height
= height
;
332 state
->cbufs
[0] = strb
->surface
;
333 for (i
= 1; i
< PIPE_MAX_COLOR_BUFS
; ++i
)
336 state
->zsbuf
= stfb
->dsrb
->surface
;
338 cso_set_framebuffer(ctx
->cso_context
, state
);
341 ctx
->state
.dirty
|= VIEWPORT_DIRTY
;
342 ctx
->state
.dirty
|= DEPTH_STENCIL_DIRTY
;/*to reset the scissors*/
344 ctx
->pipe
->clear(ctx
->pipe
, PIPE_CLEAR_DEPTHSTENCIL
,
347 /* we need all the other state already set */
349 setup_new_alpha_mask(ctx
, stfb
, width
, height
);
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
,
356 void st_set_framebuffer_surface(struct st_framebuffer
*stfb
,
357 uint surfIndex
, struct pipe_surface
*surf
)
359 struct st_renderbuffer
*rb
= stfb
->strb
;
361 /* unreference existing surfaces */
362 pipe_surface_reference( &rb
->surface
, NULL
);
363 pipe_texture_reference( &rb
->texture
, NULL
);
365 /* reference new ones */
366 pipe_surface_reference( &rb
->surface
, surf
);
367 pipe_texture_reference( &rb
->texture
, surf
->texture
);
369 rb
->width
= surf
->width
;
370 rb
->height
= surf
->height
;
373 int st_get_framebuffer_surface(struct st_framebuffer
*stfb
,
374 uint surfIndex
, struct pipe_surface
**surf
)
376 struct st_renderbuffer
*rb
= stfb
->strb
;
381 int st_get_framebuffer_texture(struct st_framebuffer
*stfb
,
382 uint surfIndex
, struct pipe_texture
**tex
)
384 struct st_renderbuffer
*rb
= stfb
->strb
;
389 void * st_framebuffer_private(struct st_framebuffer
*stfb
)
391 return stfb
->privateData
;
394 void st_unreference_framebuffer(struct st_framebuffer
*stfb
)
399 boolean
st_make_current(struct vg_context
*st
,
400 struct st_framebuffer
*draw
,
401 struct st_framebuffer
*read
,
402 void *winsys_drawable_handle
)
404 vg_set_current_context(st
);
406 st
->draw_buffer
= draw
;
410 struct vg_context
*st_get_current(void)
412 return vg_current_context();
415 void st_flush(struct vg_context
*st
, uint pipeFlushFlags
,
416 struct pipe_fence_handle
**fence
)
418 st
->pipe
->flush(st
->pipe
, pipeFlushFlags
, fence
);
421 void st_finish(struct vg_context
*st
)
423 struct pipe_fence_handle
*fence
= NULL
;
425 st_flush(st
, PIPE_FLUSH_RENDER_CACHE
, &fence
);
427 st
->pipe
->screen
->fence_finish(st
->pipe
->screen
, fence
, 0);
428 st
->pipe
->screen
->fence_reference(st
->pipe
->screen
, &fence
, NULL
);
431 void st_notify_swapbuffers(struct st_framebuffer
*stfb
)
433 struct vg_context
*ctx
= vg_current_context();
434 if (ctx
&& ctx
->draw_buffer
== stfb
) {
436 PIPE_FLUSH_RENDER_CACHE
|
437 PIPE_FLUSH_SWAPBUFFERS
|
443 void st_notify_swapbuffers_complete(struct st_framebuffer
*stfb
)
447 int st_bind_texture_surface(struct pipe_surface
*ps
, int target
, int level
,
448 enum pipe_format format
)
453 int st_unbind_texture_surface(struct pipe_surface
*ps
, int target
, int level
)
458 st_proc
st_get_proc_address(const char *procname
)