2 * Mesa 3-D graphics library
5 * Copyright (C) 2010 LunarG Inc.
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 * Chia-I Wu <olv@lunarg.com>
28 #include "state_tracker/st_api.h"
30 #include "pipe/p_context.h"
31 #include "pipe/p_screen.h"
32 #include "util/u_memory.h"
33 #include "util/u_inlines.h"
35 #include "vg_manager.h"
36 #include "vg_context.h"
37 #include "vg_tracker.h" /* for st_resize_framebuffer */
41 * Flush the front buffer if the current context renders to the front buffer.
44 vg_manager_flush_frontbuffer(struct vg_context
*ctx
)
46 struct st_framebuffer
*stfb
= ctx
->draw_buffer
;
51 /* st_public.h is used */
53 struct pipe_screen
*screen
= ctx
->pipe
->screen
;
54 if (screen
->flush_frontbuffer
) {
55 screen
->flush_frontbuffer(screen
,
56 stfb
->strb
->surface
, ctx
->pipe
->priv
);
61 switch (stfb
->strb_att
) {
62 case ST_ATTACHMENT_FRONT_LEFT
:
63 case ST_ATTACHMENT_FRONT_RIGHT
:
64 stfb
->iface
->flush_front(stfb
->iface
, stfb
->strb_att
);
72 * Re-validate the framebuffer.
75 vg_manager_validate_framebuffer(struct vg_context
*ctx
)
77 struct pipe_screen
*screen
= ctx
->pipe
->screen
;
78 struct st_framebuffer
*stfb
= ctx
->draw_buffer
;
79 struct st_renderbuffer
*rb
;
80 struct pipe_texture
*pt
;
82 /* no binding surface */
86 /* st_public.h is used */
88 struct pipe_screen
*screen
= ctx
->pipe
->screen
;
89 if (screen
->update_buffer
)
90 screen
->update_buffer(screen
, ctx
->pipe
->priv
);
94 if (!p_atomic_read(&ctx
->draw_buffer_invalid
))
98 if (!stfb
->iface
->validate(stfb
->iface
, &stfb
->strb_att
, 1, &pt
) || !pt
)
102 if (rb
->texture
== pt
) {
103 pipe_texture_reference(&pt
, NULL
);
107 /* unreference existing ones */
108 pipe_surface_reference(&rb
->surface
, NULL
);
109 pipe_texture_reference(&rb
->texture
, NULL
);
112 rb
->surface
= screen
->get_tex_surface(screen
, rb
->texture
, 0, 0, 0,
113 PIPE_BUFFER_USAGE_GPU_READ
| PIPE_BUFFER_USAGE_GPU_WRITE
);
115 rb
->width
= rb
->surface
->width
;
116 rb
->height
= rb
->surface
->height
;
118 st_resize_framebuffer(stfb
, rb
->width
, rb
->height
);
120 p_atomic_set(&ctx
->draw_buffer_invalid
, FALSE
);
125 vg_context_notify_invalid_framebuffer(struct st_context_iface
*stctxi
,
126 struct st_framebuffer_iface
*stfbi
)
128 struct vg_context
*ctx
= (struct vg_context
*) stctxi
;
129 p_atomic_set(&ctx
->draw_buffer_invalid
, TRUE
);
133 vg_context_flush(struct st_context_iface
*stctxi
, unsigned flags
,
134 struct pipe_fence_handle
**fence
)
136 struct vg_context
*ctx
= (struct vg_context
*) stctxi
;
137 ctx
->pipe
->flush(ctx
->pipe
, flags
, fence
);
138 if (flags
& PIPE_FLUSH_RENDER_CACHE
)
139 vg_manager_flush_frontbuffer(ctx
);
143 vg_context_destroy(struct st_context_iface
*stctxi
)
145 struct vg_context
*ctx
= (struct vg_context
*) stctxi
;
146 vg_destroy_context(ctx
);
149 static struct st_context_iface
*
150 vg_api_create_context(struct st_api
*stapi
, struct st_manager
*smapi
,
151 const struct st_visual
*visual
,
152 struct st_context_iface
*shared_stctxi
)
154 struct vg_context
*shared_ctx
= (struct vg_context
*) shared_stctxi
;
155 struct vg_context
*ctx
;
156 struct pipe_context
*pipe
;
158 pipe
= smapi
->screen
->context_create(smapi
->screen
, NULL
);
161 ctx
= vg_create_context(pipe
, NULL
, shared_ctx
);
167 ctx
->iface
.destroy
= vg_context_destroy
;
169 ctx
->iface
.notify_invalid_framebuffer
=
170 vg_context_notify_invalid_framebuffer
;
171 ctx
->iface
.flush
= vg_context_flush
;
173 ctx
->iface
.teximage
= NULL
;
174 ctx
->iface
.copy
= NULL
;
176 ctx
->iface
.st_context_private
= (void *) smapi
;
181 static struct st_renderbuffer
*
182 create_renderbuffer(enum pipe_format format
)
184 struct st_renderbuffer
*strb
;
186 strb
= CALLOC_STRUCT(st_renderbuffer
);
188 strb
->format
= format
;
194 destroy_renderbuffer(struct st_renderbuffer
*strb
)
196 pipe_surface_reference(&strb
->surface
, NULL
);
197 pipe_texture_reference(&strb
->texture
, NULL
);
202 * Decide the buffer to render to.
204 static enum st_attachment_type
205 choose_attachment(struct st_framebuffer_iface
*stfbi
)
207 enum st_attachment_type statt
;
209 statt
= stfbi
->visual
->render_buffer
;
210 if (statt
!= ST_ATTACHMENT_INVALID
) {
211 /* use the buffer given by the visual, unless it is unavailable */
212 if (!st_visual_have_buffers(stfbi
->visual
, 1 << statt
)) {
214 case ST_ATTACHMENT_BACK_LEFT
:
215 statt
= ST_ATTACHMENT_FRONT_LEFT
;
217 case ST_ATTACHMENT_BACK_RIGHT
:
218 statt
= ST_ATTACHMENT_FRONT_RIGHT
;
224 if (!st_visual_have_buffers(stfbi
->visual
, 1 << statt
))
225 statt
= ST_ATTACHMENT_INVALID
;
233 * Bind the context to the given framebuffers.
236 vg_context_bind_framebuffers(struct st_context_iface
*stctxi
,
237 struct st_framebuffer_iface
*stdrawi
,
238 struct st_framebuffer_iface
*streadi
)
240 struct vg_context
*ctx
= (struct vg_context
*) stctxi
;
241 struct st_framebuffer
*stfb
;
242 enum st_attachment_type strb_att
;
244 /* the draw and read framebuffers must be the same */
245 if (stdrawi
!= streadi
)
248 p_atomic_set(&ctx
->draw_buffer_invalid
, TRUE
);
250 strb_att
= (stdrawi
) ? choose_attachment(stdrawi
) : ST_ATTACHMENT_INVALID
;
252 if (ctx
->draw_buffer
) {
253 stfb
= ctx
->draw_buffer
;
255 /* free the existing fb */
257 stfb
->strb_att
!= strb_att
||
258 stfb
->strb
->format
!= stdrawi
->visual
->color_format
||
259 stfb
->dsrb
->format
!= stdrawi
->visual
->depth_stencil_format
) {
260 destroy_renderbuffer(stfb
->strb
);
261 destroy_renderbuffer(stfb
->dsrb
);
264 ctx
->draw_buffer
= NULL
;
271 if (strb_att
== ST_ATTACHMENT_INVALID
)
274 /* create a new fb */
275 if (!ctx
->draw_buffer
) {
276 stfb
= CALLOC_STRUCT(st_framebuffer
);
280 stfb
->strb
= create_renderbuffer(stdrawi
->visual
->color_format
);
286 stfb
->dsrb
= create_renderbuffer(stdrawi
->visual
->depth_stencil_format
);
295 stfb
->strb_att
= strb_att
;
297 ctx
->draw_buffer
= stfb
;
300 ctx
->draw_buffer
->iface
= stdrawi
;
306 vg_api_make_current(struct st_api
*stapi
, struct st_context_iface
*stctxi
,
307 struct st_framebuffer_iface
*stdrawi
,
308 struct st_framebuffer_iface
*streadi
)
310 struct vg_context
*ctx
= (struct vg_context
*) stctxi
;
313 vg_context_bind_framebuffers(stctxi
, stdrawi
, streadi
);
314 vg_set_current_context(ctx
);
319 static struct st_context_iface
*
320 vg_api_get_current(struct st_api
*stapi
)
322 struct vg_context
*ctx
= vg_current_context();
324 return (ctx
) ? &ctx
->iface
: NULL
;
328 vg_api_is_visual_supported(struct st_api
*stapi
,
329 const struct st_visual
*visual
)
331 /* the impl requires a depth/stencil buffer */
332 if (visual
->depth_stencil_format
== PIPE_FORMAT_NONE
)
339 vg_api_get_proc_address(struct st_api
*stapi
, const char *procname
)
342 return (st_proc_t
) NULL
;
346 vg_api_destroy(struct st_api
*stapi
)
351 static struct st_api
*
352 vg_module_create_api(void)
354 struct st_api
*stapi
;
356 stapi
= CALLOC_STRUCT(st_api
);
358 stapi
->destroy
= vg_api_destroy
;
359 stapi
->get_proc_address
= vg_api_get_proc_address
;
360 stapi
->is_visual_supported
= vg_api_is_visual_supported
;
362 stapi
->create_context
= vg_api_create_context
;
363 stapi
->make_current
= vg_api_make_current
;
364 stapi
->get_current
= vg_api_get_current
;
370 PUBLIC
const struct st_module st_module_OpenVG
= {
371 .api
= ST_API_OPENVG
,
372 .create_api
= vg_module_create_api
,