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 "pipe/p_inlines.h"
33 #include "pipe/p_screen.h"
34 #include "util/u_memory.h"
35 #include "util/u_math.h"
37 static struct pipe_texture
*
38 create_texture(struct pipe_context
*pipe
, enum pipe_format format
,
39 VGint width
, VGint height
)
41 struct pipe_texture templ
;
43 memset(&templ
, 0, sizeof(templ
));
45 if (format
!= PIPE_FORMAT_NONE
) {
46 templ
.format
= format
;
49 templ
.format
= PIPE_FORMAT_A8R8G8B8_UNORM
;
52 templ
.target
= PIPE_TEXTURE_2D
;
53 pf_get_block(templ
.format
, &templ
.block
);
54 templ
.width
[0] = width
;
55 templ
.height
[0] = height
;
59 if (pf_get_component_bits(format
, PIPE_FORMAT_COMP_S
)) {
60 templ
.tex_usage
= PIPE_TEXTURE_USAGE_DEPTH_STENCIL
;
62 templ
.tex_usage
= (PIPE_TEXTURE_USAGE_DISPLAY_TARGET
|
63 PIPE_TEXTURE_USAGE_RENDER_TARGET
|
64 PIPE_TEXTURE_USAGE_SAMPLER
);
67 return pipe
->screen
->texture_create(pipe
->screen
, &templ
);
71 * Allocate a renderbuffer for a an on-screen window (not a user-created
72 * renderbuffer). The window system code determines the format.
74 static struct st_renderbuffer
*
75 st_new_renderbuffer_fb(enum pipe_format format
)
77 struct st_renderbuffer
*strb
;
79 strb
= CALLOC_STRUCT(st_renderbuffer
);
81 /*_vega_error(NULL, VG_OUT_OF_MEMORY, "creating renderbuffer");*/
85 strb
->format
= format
;
92 * This is called to allocate the original drawing surface, and
93 * during window resize.
96 st_renderbuffer_alloc_storage(struct vg_context
* ctx
,
97 struct st_renderbuffer
*strb
,
98 VGuint width
, VGuint height
)
100 struct pipe_context
*pipe
= ctx
->pipe
;
101 unsigned surface_usage
;
103 /* Free the old surface and texture
105 pipe_surface_reference(&strb
->surface
, NULL
);
106 pipe_texture_reference(&strb
->texture
, NULL
);
109 /* Probably need dedicated flags for surface usage too:
111 surface_usage
= (PIPE_BUFFER_USAGE_GPU_READ
|
112 PIPE_BUFFER_USAGE_GPU_WRITE
);
114 strb
->texture
= create_texture(pipe
, strb
->format
,
120 strb
->surface
= pipe
->screen
->get_tex_surface(pipe
->screen
,
125 strb
->height
= height
;
127 assert(strb
->surface
->width
== width
);
128 assert(strb
->surface
->height
== height
);
130 return strb
->surface
!= NULL
;
133 struct vg_context
* st_create_context(struct pipe_context
*pipe
,
135 struct vg_context
*share
)
137 struct vg_context
*ctx
= vg_create_context(pipe
, visual
, share
);
138 /*debug_printf("--------- CREATE CONTEXT %p\n", ctx);*/
142 void st_destroy_context(struct vg_context
*st
)
144 /*debug_printf("--------- DESTROY CONTEXT %p\n", st);*/
145 vg_destroy_context(st
);
148 void st_copy_context_state(struct vg_context
*dst
, struct vg_context
*src
,
151 fprintf(stderr
, "FIXME: %s\n", __FUNCTION__
);
154 void st_get_framebuffer_dimensions(struct st_framebuffer
*stfb
,
158 *width
= stfb
->strb
->width
;
159 *height
= stfb
->strb
->height
;
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
,
169 struct st_framebuffer
*stfb
= CALLOC_STRUCT(st_framebuffer
);
171 struct st_renderbuffer
*rb
=
172 st_new_renderbuffer_fb(colorFormat
);
176 struct st_renderbuffer
*rb
=
177 st_new_renderbuffer_fb(colorFormat
);
181 /* we want to combine the depth/stencil */
182 if (stencilFormat
== depthFormat
)
183 stfb
->dsrb
= st_new_renderbuffer_fb(stencilFormat
);
185 stfb
->dsrb
= st_new_renderbuffer_fb(PIPE_FORMAT_S8Z24_UNORM
);
187 /*### currently we always allocate it but it's possible it's
188 not necessary if EGL_ALPHA_MASK_SIZE was 0
190 stfb
->alpha_mask
= 0;
192 stfb
->init_width
= width
;
193 stfb
->init_height
= height
;
194 stfb
->privateData
= privateData
;
200 static void setup_new_alpha_mask(struct vg_context
*ctx
,
201 struct st_framebuffer
*stfb
,
202 uint width
, uint height
)
204 struct pipe_context
*pipe
= ctx
->pipe
;
205 struct pipe_texture
*old_texture
= stfb
->alpha_mask
;
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
213 create_texture(pipe
, PIPE_FORMAT_A8R8G8B8_UNORM
, width
, height
);
215 if (!stfb
->alpha_mask
) {
217 pipe_texture_reference(&old_texture
, NULL
);
221 vg_validate_state(ctx
);
223 /* alpha mask starts with 1.f alpha */
224 mask_fill(0, 0, width
, height
, 1.f
);
226 /* if we had an old surface copy it over */
228 struct pipe_surface
*surface
= pipe
->screen
->get_tex_surface(
232 PIPE_BUFFER_USAGE_GPU_WRITE
);
233 struct pipe_surface
*old_surface
= pipe
->screen
->get_tex_surface(
237 PIPE_BUFFER_USAGE_GPU_READ
);
238 pipe
->surface_copy(pipe
,
243 MIN2(old_surface
->width
, width
),
244 MIN2(old_surface
->height
, height
));
246 pipe_surface_reference(&surface
, NULL
);
248 pipe_surface_reference(&old_surface
, NULL
);
251 /* Free the old texture
254 pipe_texture_reference(&old_texture
, NULL
);
257 void st_resize_framebuffer(struct st_framebuffer
*stfb
,
258 uint width
, uint height
)
260 struct vg_context
*ctx
= vg_current_context();
261 struct st_renderbuffer
*strb
= stfb
->strb
;
262 struct pipe_framebuffer_state
*state
;
267 state
= &ctx
->state
.g3d
.fb
;
269 /* If this is a noop, exit early and don't do the clear, etc below.
271 if (strb
->width
== width
&&
272 strb
->height
== height
&&
276 if (strb
->width
!= width
|| strb
->height
!= height
)
277 st_renderbuffer_alloc_storage(ctx
, strb
,
280 if (stfb
->dsrb
->width
!= width
|| stfb
->dsrb
->height
!= height
)
281 st_renderbuffer_alloc_storage(ctx
, stfb
->dsrb
,
287 memset(state
, 0, sizeof(struct pipe_framebuffer_state
));
289 state
->width
= width
;
290 state
->height
= height
;
293 state
->cbufs
[0] = strb
->surface
;
294 for (i
= 1; i
< PIPE_MAX_COLOR_BUFS
; ++i
)
297 state
->zsbuf
= stfb
->dsrb
->surface
;
299 cso_set_framebuffer(ctx
->cso_context
, state
);
302 ctx
->state
.dirty
|= VIEWPORT_DIRTY
;
303 ctx
->state
.dirty
|= DEPTH_STENCIL_DIRTY
;/*to reset the scissors*/
305 ctx
->pipe
->clear(ctx
->pipe
, PIPE_CLEAR_DEPTHSTENCIL
,
308 /* we need all the other state already set */
310 setup_new_alpha_mask(ctx
, stfb
, width
, height
);
312 pipe_texture_reference( &stfb
->blend_texture
, NULL
);
313 stfb
->blend_texture
= create_texture(ctx
->pipe
, PIPE_FORMAT_A8R8G8B8_UNORM
,
317 void st_set_framebuffer_surface(struct st_framebuffer
*stfb
,
318 uint surfIndex
, struct pipe_surface
*surf
)
320 struct st_renderbuffer
*rb
= stfb
->strb
;
322 /* unreference existing surfaces */
323 pipe_surface_reference( &rb
->surface
, NULL
);
324 pipe_texture_reference( &rb
->texture
, NULL
);
326 /* reference new ones */
327 pipe_surface_reference( &rb
->surface
, surf
);
328 pipe_texture_reference( &rb
->texture
, surf
->texture
);
330 rb
->width
= surf
->width
;
331 rb
->height
= surf
->height
;
334 int st_get_framebuffer_surface(struct st_framebuffer
*stfb
,
335 uint surfIndex
, struct pipe_surface
**surf
)
337 struct st_renderbuffer
*rb
= stfb
->strb
;
342 int st_get_framebuffer_texture(struct st_framebuffer
*stfb
,
343 uint surfIndex
, struct pipe_texture
**tex
)
345 struct st_renderbuffer
*rb
= stfb
->strb
;
350 void * st_framebuffer_private(struct st_framebuffer
*stfb
)
352 return stfb
->privateData
;
355 void st_unreference_framebuffer(struct st_framebuffer
*stfb
)
360 void st_make_current(struct vg_context
*st
,
361 struct st_framebuffer
*draw
,
362 struct st_framebuffer
*read
)
364 vg_set_current_context(st
);
366 st
->draw_buffer
= draw
;
370 struct vg_context
*st_get_current(void)
372 return vg_current_context();
375 void st_flush(struct vg_context
*st
, uint pipeFlushFlags
,
376 struct pipe_fence_handle
**fence
)
378 st
->pipe
->flush(st
->pipe
, pipeFlushFlags
, fence
);
381 void st_finish(struct vg_context
*st
)
383 struct pipe_fence_handle
*fence
= NULL
;
385 st_flush(st
, PIPE_FLUSH_RENDER_CACHE
, &fence
);
387 st
->pipe
->screen
->fence_finish(st
->pipe
->screen
, fence
, 0);
388 st
->pipe
->screen
->fence_reference(st
->pipe
->screen
, &fence
, NULL
);
391 void st_notify_swapbuffers(struct st_framebuffer
*stfb
)
393 struct vg_context
*ctx
= vg_current_context();
394 if (ctx
&& ctx
->draw_buffer
== stfb
) {
396 PIPE_FLUSH_RENDER_CACHE
|
397 PIPE_FLUSH_SWAPBUFFERS
|
403 void st_notify_swapbuffers_complete(struct st_framebuffer
*stfb
)
407 int st_bind_texture_surface(struct pipe_surface
*ps
, int target
, int level
,
408 enum pipe_format format
)
413 int st_unbind_texture_surface(struct pipe_surface
*ps
, int target
, int level
)