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_format.h"
35 #include "util/u_memory.h"
36 #include "util/u_math.h"
37 #include "util/u_rect.h"
39 static struct pipe_texture
*
40 create_texture(struct pipe_context
*pipe
, enum pipe_format format
,
41 VGint width
, VGint height
)
43 struct pipe_texture templ
;
45 memset(&templ
, 0, sizeof(templ
));
47 if (format
!= PIPE_FORMAT_NONE
) {
48 templ
.format
= format
;
51 templ
.format
= PIPE_FORMAT_A8R8G8B8_UNORM
;
54 templ
.target
= PIPE_TEXTURE_2D
;
56 templ
.height0
= height
;
60 if (util_format_get_component_bits(format
, UTIL_FORMAT_COLORSPACE_ZS
, 1)) {
61 templ
.tex_usage
= PIPE_TEXTURE_USAGE_DEPTH_STENCIL
;
63 templ
.tex_usage
= (PIPE_TEXTURE_USAGE_DISPLAY_TARGET
|
64 PIPE_TEXTURE_USAGE_RENDER_TARGET
|
65 PIPE_TEXTURE_USAGE_SAMPLER
);
68 return pipe
->screen
->texture_create(pipe
->screen
, &templ
);
72 * Allocate a renderbuffer for a an on-screen window (not a user-created
73 * renderbuffer). The window system code determines the format.
75 static struct st_renderbuffer
*
76 st_new_renderbuffer_fb(enum pipe_format format
)
78 struct st_renderbuffer
*strb
;
80 strb
= CALLOC_STRUCT(st_renderbuffer
);
82 /*_vega_error(NULL, VG_OUT_OF_MEMORY, "creating renderbuffer");*/
86 strb
->format
= format
;
93 * This is called to allocate the original drawing surface, and
94 * during window resize.
97 st_renderbuffer_alloc_storage(struct vg_context
* ctx
,
98 struct st_renderbuffer
*strb
,
99 VGuint width
, VGuint height
)
101 struct pipe_context
*pipe
= ctx
->pipe
;
102 unsigned surface_usage
;
104 /* Free the old surface and texture
106 pipe_surface_reference(&strb
->surface
, NULL
);
107 pipe_texture_reference(&strb
->texture
, NULL
);
110 /* Probably need dedicated flags for surface usage too:
112 surface_usage
= (PIPE_BUFFER_USAGE_GPU_READ
|
113 PIPE_BUFFER_USAGE_GPU_WRITE
);
115 strb
->texture
= create_texture(pipe
, strb
->format
,
121 strb
->surface
= pipe
->screen
->get_tex_surface(pipe
->screen
,
126 strb
->height
= height
;
128 assert(strb
->surface
->width
== width
);
129 assert(strb
->surface
->height
== height
);
131 return strb
->surface
!= NULL
;
134 struct vg_context
* st_create_context(struct pipe_context
*pipe
,
136 struct vg_context
*share
)
138 struct vg_context
*ctx
= vg_create_context(pipe
, visual
, share
);
139 /*debug_printf("--------- CREATE CONTEXT %p\n", ctx);*/
143 void st_destroy_context(struct vg_context
*st
)
145 /*debug_printf("--------- DESTROY CONTEXT %p\n", st);*/
146 vg_destroy_context(st
);
149 void st_copy_context_state(struct vg_context
*dst
, struct vg_context
*src
,
152 fprintf(stderr
, "FIXME: %s\n", __FUNCTION__
);
155 void st_get_framebuffer_dimensions(struct st_framebuffer
*stfb
,
159 *width
= stfb
->strb
->width
;
160 *height
= stfb
->strb
->height
;
163 struct st_framebuffer
* st_create_framebuffer(const void *visual
,
164 enum pipe_format colorFormat
,
165 enum pipe_format depthFormat
,
166 enum pipe_format stencilFormat
,
167 uint width
, uint height
,
170 struct st_framebuffer
*stfb
= CALLOC_STRUCT(st_framebuffer
);
172 struct st_renderbuffer
*rb
=
173 st_new_renderbuffer_fb(colorFormat
);
177 struct st_renderbuffer
*rb
=
178 st_new_renderbuffer_fb(colorFormat
);
182 /* we want to combine the depth/stencil */
183 if (stencilFormat
== depthFormat
)
184 stfb
->dsrb
= st_new_renderbuffer_fb(stencilFormat
);
186 stfb
->dsrb
= st_new_renderbuffer_fb(PIPE_FORMAT_S8Z24_UNORM
);
188 /*### currently we always allocate it but it's possible it's
189 not necessary if EGL_ALPHA_MASK_SIZE was 0
191 stfb
->alpha_mask
= 0;
193 stfb
->init_width
= width
;
194 stfb
->init_height
= height
;
195 stfb
->privateData
= privateData
;
201 static void setup_new_alpha_mask(struct vg_context
*ctx
,
202 struct st_framebuffer
*stfb
,
203 uint width
, uint height
)
205 struct pipe_context
*pipe
= ctx
->pipe
;
206 struct pipe_texture
*old_texture
= stfb
->alpha_mask
;
209 we use PIPE_FORMAT_A8R8G8B8_UNORM because we want to render to
210 this texture and use it as a sampler, so while this wastes some
211 space it makes both of those a lot simpler
214 create_texture(pipe
, PIPE_FORMAT_A8R8G8B8_UNORM
, width
, height
);
216 if (!stfb
->alpha_mask
) {
218 pipe_texture_reference(&old_texture
, NULL
);
222 vg_validate_state(ctx
);
224 /* alpha mask starts with 1.f alpha */
225 mask_fill(0, 0, width
, height
, 1.f
);
227 /* if we had an old surface copy it over */
229 struct pipe_surface
*surface
= pipe
->screen
->get_tex_surface(
233 PIPE_BUFFER_USAGE_GPU_WRITE
);
234 struct pipe_surface
*old_surface
= pipe
->screen
->get_tex_surface(
238 PIPE_BUFFER_USAGE_GPU_READ
);
239 if (pipe
->surface_copy
) {
240 pipe
->surface_copy(pipe
,
245 MIN2(old_surface
->width
, width
),
246 MIN2(old_surface
->height
, height
));
248 util_surface_copy(pipe
, FALSE
,
253 MIN2(old_surface
->width
, width
),
254 MIN2(old_surface
->height
, height
));
257 pipe_surface_reference(&surface
, NULL
);
259 pipe_surface_reference(&old_surface
, NULL
);
262 /* Free the old texture
265 pipe_texture_reference(&old_texture
, NULL
);
268 void st_resize_framebuffer(struct st_framebuffer
*stfb
,
269 uint width
, uint height
)
271 struct vg_context
*ctx
= vg_current_context();
272 struct st_renderbuffer
*strb
= stfb
->strb
;
273 struct pipe_framebuffer_state
*state
;
278 state
= &ctx
->state
.g3d
.fb
;
280 /* If this is a noop, exit early and don't do the clear, etc below.
282 if (strb
->width
== width
&&
283 strb
->height
== height
&&
287 if (strb
->width
!= width
|| strb
->height
!= height
)
288 st_renderbuffer_alloc_storage(ctx
, strb
,
291 if (stfb
->dsrb
->width
!= width
|| stfb
->dsrb
->height
!= height
)
292 st_renderbuffer_alloc_storage(ctx
, stfb
->dsrb
,
298 memset(state
, 0, sizeof(struct pipe_framebuffer_state
));
300 state
->width
= width
;
301 state
->height
= height
;
304 state
->cbufs
[0] = strb
->surface
;
305 for (i
= 1; i
< PIPE_MAX_COLOR_BUFS
; ++i
)
308 state
->zsbuf
= stfb
->dsrb
->surface
;
310 cso_set_framebuffer(ctx
->cso_context
, state
);
313 ctx
->state
.dirty
|= VIEWPORT_DIRTY
;
314 ctx
->state
.dirty
|= DEPTH_STENCIL_DIRTY
;/*to reset the scissors*/
316 ctx
->pipe
->clear(ctx
->pipe
, PIPE_CLEAR_DEPTHSTENCIL
,
319 /* we need all the other state already set */
321 setup_new_alpha_mask(ctx
, stfb
, width
, height
);
323 pipe_texture_reference( &stfb
->blend_texture
, NULL
);
324 stfb
->blend_texture
= create_texture(ctx
->pipe
, PIPE_FORMAT_A8R8G8B8_UNORM
,
328 void st_set_framebuffer_surface(struct st_framebuffer
*stfb
,
329 uint surfIndex
, struct pipe_surface
*surf
)
331 struct st_renderbuffer
*rb
= stfb
->strb
;
333 /* unreference existing surfaces */
334 pipe_surface_reference( &rb
->surface
, NULL
);
335 pipe_texture_reference( &rb
->texture
, NULL
);
337 /* reference new ones */
338 pipe_surface_reference( &rb
->surface
, surf
);
339 pipe_texture_reference( &rb
->texture
, surf
->texture
);
341 rb
->width
= surf
->width
;
342 rb
->height
= surf
->height
;
345 int st_get_framebuffer_surface(struct st_framebuffer
*stfb
,
346 uint surfIndex
, struct pipe_surface
**surf
)
348 struct st_renderbuffer
*rb
= stfb
->strb
;
353 int st_get_framebuffer_texture(struct st_framebuffer
*stfb
,
354 uint surfIndex
, struct pipe_texture
**tex
)
356 struct st_renderbuffer
*rb
= stfb
->strb
;
361 void * st_framebuffer_private(struct st_framebuffer
*stfb
)
363 return stfb
->privateData
;
366 void st_unreference_framebuffer(struct st_framebuffer
*stfb
)
371 void st_make_current(struct vg_context
*st
,
372 struct st_framebuffer
*draw
,
373 struct st_framebuffer
*read
)
375 vg_set_current_context(st
);
377 st
->draw_buffer
= draw
;
381 struct vg_context
*st_get_current(void)
383 return vg_current_context();
386 void st_flush(struct vg_context
*st
, uint pipeFlushFlags
,
387 struct pipe_fence_handle
**fence
)
389 st
->pipe
->flush(st
->pipe
, pipeFlushFlags
, fence
);
392 void st_finish(struct vg_context
*st
)
394 struct pipe_fence_handle
*fence
= NULL
;
396 st_flush(st
, PIPE_FLUSH_RENDER_CACHE
, &fence
);
398 st
->pipe
->screen
->fence_finish(st
->pipe
->screen
, fence
, 0);
399 st
->pipe
->screen
->fence_reference(st
->pipe
->screen
, &fence
, NULL
);
402 void st_notify_swapbuffers(struct st_framebuffer
*stfb
)
404 struct vg_context
*ctx
= vg_current_context();
405 if (ctx
&& ctx
->draw_buffer
== stfb
) {
407 PIPE_FLUSH_RENDER_CACHE
|
408 PIPE_FLUSH_SWAPBUFFERS
|
414 void st_notify_swapbuffers_complete(struct st_framebuffer
*stfb
)
418 int st_bind_texture_surface(struct pipe_surface
*ps
, int target
, int level
,
419 enum pipe_format format
)
424 int st_unbind_texture_surface(struct pipe_surface
*ps
, int target
, int level
)