Go to most recent revision | Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
4358 | Serge | 1 | /* |
2 | * Mesa 3-D graphics library |
||
3 | * |
||
4 | * Copyright 2009 VMware, Inc. All Rights Reserved. |
||
5 | * Copyright (C) 2010 LunarG Inc. |
||
6 | * |
||
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: |
||
13 | * |
||
14 | * The above copyright notice and this permission notice shall be included |
||
15 | * in all copies or substantial portions of the Software. |
||
16 | * |
||
17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
||
18 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
||
19 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
||
20 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
||
21 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
||
22 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
||
23 | * DEALINGS IN THE SOFTWARE. |
||
24 | * |
||
25 | * Authors: |
||
26 | * Chia-I Wu |
||
27 | */ |
||
28 | |||
29 | #include "state_tracker/st_api.h" |
||
30 | |||
31 | #include "pipe/p_context.h" |
||
32 | #include "pipe/p_screen.h" |
||
33 | #include "util/u_memory.h" |
||
34 | #include "util/u_inlines.h" |
||
35 | #include "util/u_box.h" |
||
36 | #include "util/u_surface.h" |
||
37 | |||
38 | #include "vg_api.h" |
||
39 | #include "vg_manager.h" |
||
40 | #include "vg_context.h" |
||
41 | #include "api.h" |
||
42 | #include "handle.h" |
||
43 | |||
44 | static boolean |
||
45 | vg_context_update_color_rb(struct vg_context *ctx, struct pipe_resource *pt) |
||
46 | { |
||
47 | struct st_renderbuffer *strb = ctx->draw_buffer->strb; |
||
48 | struct pipe_context *pipe = ctx->pipe; |
||
49 | struct pipe_surface surf_tmpl; |
||
50 | |||
51 | if (strb->texture == pt) { |
||
52 | pipe_resource_reference(&pt, NULL); |
||
53 | return FALSE; |
||
54 | } |
||
55 | |||
56 | /* unreference existing ones */ |
||
57 | pipe_surface_reference(&strb->surface, NULL); |
||
58 | pipe_resource_reference(&strb->texture, NULL); |
||
59 | strb->width = strb->height = 0; |
||
60 | |||
61 | strb->texture = pt; |
||
62 | |||
63 | u_surface_default_template(&surf_tmpl, strb->texture); |
||
64 | strb->surface = pipe->create_surface(pipe, strb->texture, &surf_tmpl); |
||
65 | |||
66 | if (!strb->surface) { |
||
67 | pipe_resource_reference(&strb->texture, NULL); |
||
68 | return TRUE; |
||
69 | } |
||
70 | |||
71 | strb->width = pt->width0; |
||
72 | strb->height = pt->height0; |
||
73 | |||
74 | return TRUE; |
||
75 | } |
||
76 | |||
77 | /** |
||
78 | * Flush the front buffer if the current context renders to the front buffer. |
||
79 | */ |
||
80 | void |
||
81 | vg_manager_flush_frontbuffer(struct vg_context *ctx) |
||
82 | { |
||
83 | struct st_framebuffer *stfb = ctx->draw_buffer; |
||
84 | |||
85 | if (!stfb) |
||
86 | return; |
||
87 | |||
88 | switch (stfb->strb_att) { |
||
89 | case ST_ATTACHMENT_FRONT_LEFT: |
||
90 | case ST_ATTACHMENT_FRONT_RIGHT: |
||
91 | stfb->iface->flush_front(&ctx->iface, stfb->iface, stfb->strb_att); |
||
92 | break; |
||
93 | default: |
||
94 | break; |
||
95 | } |
||
96 | } |
||
97 | |||
98 | /** |
||
99 | * Re-validate the framebuffer. |
||
100 | */ |
||
101 | void |
||
102 | vg_manager_validate_framebuffer(struct vg_context *ctx) |
||
103 | { |
||
104 | struct st_framebuffer *stfb = ctx->draw_buffer; |
||
105 | struct pipe_resource *pt; |
||
106 | int32_t new_stamp; |
||
107 | |||
108 | /* no binding surface */ |
||
109 | if (!stfb) |
||
110 | return; |
||
111 | |||
112 | new_stamp = p_atomic_read(&stfb->iface->stamp); |
||
113 | if (stfb->iface_stamp != new_stamp) { |
||
114 | do { |
||
115 | /* validate the fb */ |
||
116 | if (!stfb->iface->validate((struct st_context_iface *)ctx, |
||
117 | stfb->iface, &stfb->strb_att, |
||
118 | 1, &pt) || !pt) |
||
119 | return; |
||
120 | |||
121 | stfb->iface_stamp = new_stamp; |
||
122 | new_stamp = p_atomic_read(&stfb->iface->stamp); |
||
123 | |||
124 | } while (stfb->iface_stamp != new_stamp); |
||
125 | |||
126 | if (vg_context_update_color_rb(ctx, pt) || |
||
127 | stfb->width != pt->width0 || |
||
128 | stfb->height != pt->height0) |
||
129 | ++stfb->stamp; |
||
130 | |||
131 | stfb->width = pt->width0; |
||
132 | stfb->height = pt->height0; |
||
133 | } |
||
134 | |||
135 | if (ctx->draw_stamp != stfb->stamp) { |
||
136 | ctx->state.dirty |= FRAMEBUFFER_DIRTY; |
||
137 | ctx->draw_stamp = stfb->stamp; |
||
138 | } |
||
139 | } |
||
140 | |||
141 | static void |
||
142 | vg_context_flush(struct st_context_iface *stctxi, unsigned flags, |
||
143 | struct pipe_fence_handle **fence) |
||
144 | { |
||
145 | struct vg_context *ctx = (struct vg_context *) stctxi; |
||
146 | unsigned pipe_flags = 0; |
||
147 | |||
148 | if (flags & ST_FLUSH_END_OF_FRAME) { |
||
149 | pipe_flags |= PIPE_FLUSH_END_OF_FRAME; |
||
150 | } |
||
151 | |||
152 | ctx->pipe->flush(ctx->pipe, fence, pipe_flags); |
||
153 | if (flags & ST_FLUSH_FRONT) |
||
154 | vg_manager_flush_frontbuffer(ctx); |
||
155 | } |
||
156 | |||
157 | static void |
||
158 | vg_context_destroy(struct st_context_iface *stctxi) |
||
159 | { |
||
160 | struct vg_context *ctx = (struct vg_context *) stctxi; |
||
161 | struct pipe_context *pipe = ctx->pipe; |
||
162 | |||
163 | vg_destroy_context(ctx); |
||
164 | pipe->destroy(pipe); |
||
165 | } |
||
166 | |||
167 | static struct st_context_iface * |
||
168 | vg_api_create_context(struct st_api *stapi, struct st_manager *smapi, |
||
169 | const struct st_context_attribs *attribs, |
||
170 | enum st_context_error *error, |
||
171 | struct st_context_iface *shared_stctxi) |
||
172 | { |
||
173 | struct vg_context *shared_ctx = (struct vg_context *) shared_stctxi; |
||
174 | struct vg_context *ctx; |
||
175 | struct pipe_context *pipe; |
||
176 | |||
177 | if (!(stapi->profile_mask & (1 << attribs->profile))) { |
||
178 | *error = ST_CONTEXT_ERROR_BAD_API; |
||
179 | return NULL; |
||
180 | } |
||
181 | |||
182 | /* only 1.0 is supported */ |
||
183 | if (attribs->major > 1 || (attribs->major == 1 && attribs->minor > 0)) { |
||
184 | *error = ST_CONTEXT_ERROR_BAD_VERSION; |
||
185 | return NULL; |
||
186 | } |
||
187 | |||
188 | /* for VGHandle / pointer lookups */ |
||
189 | init_handles(); |
||
190 | |||
191 | pipe = smapi->screen->context_create(smapi->screen, NULL); |
||
192 | if (!pipe) { |
||
193 | *error = ST_CONTEXT_ERROR_NO_MEMORY; |
||
194 | return NULL; |
||
195 | } |
||
196 | ctx = vg_create_context(pipe, NULL, shared_ctx); |
||
197 | if (!ctx) { |
||
198 | pipe->destroy(pipe); |
||
199 | *error = ST_CONTEXT_ERROR_NO_MEMORY; |
||
200 | return NULL; |
||
201 | } |
||
202 | |||
203 | ctx->iface.destroy = vg_context_destroy; |
||
204 | |||
205 | ctx->iface.flush = vg_context_flush; |
||
206 | |||
207 | ctx->iface.teximage = NULL; |
||
208 | ctx->iface.copy = NULL; |
||
209 | |||
210 | ctx->iface.st_context_private = (void *) smapi; |
||
211 | |||
212 | return &ctx->iface; |
||
213 | } |
||
214 | |||
215 | static struct st_renderbuffer * |
||
216 | create_renderbuffer(enum pipe_format format) |
||
217 | { |
||
218 | struct st_renderbuffer *strb; |
||
219 | |||
220 | strb = CALLOC_STRUCT(st_renderbuffer); |
||
221 | if (strb) |
||
222 | strb->format = format; |
||
223 | |||
224 | return strb; |
||
225 | } |
||
226 | |||
227 | static void |
||
228 | destroy_renderbuffer(struct st_renderbuffer *strb) |
||
229 | { |
||
230 | pipe_surface_reference(&strb->surface, NULL); |
||
231 | pipe_resource_reference(&strb->texture, NULL); |
||
232 | FREE(strb); |
||
233 | } |
||
234 | |||
235 | /** |
||
236 | * Decide the buffer to render to. |
||
237 | */ |
||
238 | static enum st_attachment_type |
||
239 | choose_attachment(struct st_framebuffer_iface *stfbi) |
||
240 | { |
||
241 | enum st_attachment_type statt; |
||
242 | |||
243 | statt = stfbi->visual->render_buffer; |
||
244 | if (statt != ST_ATTACHMENT_INVALID) { |
||
245 | /* use the buffer given by the visual, unless it is unavailable */ |
||
246 | if (!st_visual_have_buffers(stfbi->visual, 1 << statt)) { |
||
247 | switch (statt) { |
||
248 | case ST_ATTACHMENT_BACK_LEFT: |
||
249 | statt = ST_ATTACHMENT_FRONT_LEFT; |
||
250 | break; |
||
251 | case ST_ATTACHMENT_BACK_RIGHT: |
||
252 | statt = ST_ATTACHMENT_FRONT_RIGHT; |
||
253 | break; |
||
254 | default: |
||
255 | break; |
||
256 | } |
||
257 | |||
258 | if (!st_visual_have_buffers(stfbi->visual, 1 << statt)) |
||
259 | statt = ST_ATTACHMENT_INVALID; |
||
260 | } |
||
261 | } |
||
262 | |||
263 | return statt; |
||
264 | } |
||
265 | |||
266 | /** |
||
267 | * Bind the context to the given framebuffers. |
||
268 | */ |
||
269 | static boolean |
||
270 | vg_context_bind_framebuffers(struct st_context_iface *stctxi, |
||
271 | struct st_framebuffer_iface *stdrawi, |
||
272 | struct st_framebuffer_iface *streadi) |
||
273 | { |
||
274 | struct vg_context *ctx = (struct vg_context *) stctxi; |
||
275 | struct st_framebuffer *stfb; |
||
276 | enum st_attachment_type strb_att; |
||
277 | |||
278 | /* the draw and read framebuffers must be the same */ |
||
279 | if (stdrawi != streadi) |
||
280 | return FALSE; |
||
281 | |||
282 | strb_att = (stdrawi) ? choose_attachment(stdrawi) : ST_ATTACHMENT_INVALID; |
||
283 | |||
284 | if (ctx->draw_buffer) { |
||
285 | stfb = ctx->draw_buffer; |
||
286 | |||
287 | /* free the existing fb */ |
||
288 | if (!stdrawi || |
||
289 | stfb->strb_att != strb_att || |
||
290 | stfb->strb->format != stdrawi->visual->color_format) { |
||
291 | destroy_renderbuffer(stfb->strb); |
||
292 | destroy_renderbuffer(stfb->dsrb); |
||
293 | FREE(stfb); |
||
294 | |||
295 | ctx->draw_buffer = NULL; |
||
296 | } |
||
297 | } |
||
298 | |||
299 | if (!stdrawi) |
||
300 | return TRUE; |
||
301 | |||
302 | if (strb_att == ST_ATTACHMENT_INVALID) |
||
303 | return FALSE; |
||
304 | |||
305 | /* create a new fb */ |
||
306 | if (!ctx->draw_buffer) { |
||
307 | stfb = CALLOC_STRUCT(st_framebuffer); |
||
308 | if (!stfb) |
||
309 | return FALSE; |
||
310 | |||
311 | stfb->strb = create_renderbuffer(stdrawi->visual->color_format); |
||
312 | if (!stfb->strb) { |
||
313 | FREE(stfb); |
||
314 | return FALSE; |
||
315 | } |
||
316 | |||
317 | stfb->dsrb = create_renderbuffer(ctx->ds_format); |
||
318 | if (!stfb->dsrb) { |
||
319 | FREE(stfb->strb); |
||
320 | FREE(stfb); |
||
321 | return FALSE; |
||
322 | } |
||
323 | |||
324 | stfb->width = 0; |
||
325 | stfb->height = 0; |
||
326 | stfb->strb_att = strb_att; |
||
327 | stfb->stamp = 1; |
||
328 | stfb->iface_stamp = p_atomic_read(&stdrawi->stamp) - 1; |
||
329 | |||
330 | ctx->draw_buffer = stfb; |
||
331 | } |
||
332 | |||
333 | ctx->draw_buffer->iface = stdrawi; |
||
334 | ctx->draw_stamp = ctx->draw_buffer->stamp - 1; |
||
335 | |||
336 | return TRUE; |
||
337 | } |
||
338 | |||
339 | static boolean |
||
340 | vg_api_make_current(struct st_api *stapi, struct st_context_iface *stctxi, |
||
341 | struct st_framebuffer_iface *stdrawi, |
||
342 | struct st_framebuffer_iface *streadi) |
||
343 | { |
||
344 | struct vg_context *ctx = (struct vg_context *) stctxi; |
||
345 | |||
346 | if (stctxi) |
||
347 | vg_context_bind_framebuffers(stctxi, stdrawi, streadi); |
||
348 | vg_set_current_context(ctx); |
||
349 | |||
350 | return TRUE; |
||
351 | } |
||
352 | |||
353 | static struct st_context_iface * |
||
354 | vg_api_get_current(struct st_api *stapi) |
||
355 | { |
||
356 | struct vg_context *ctx = vg_current_context(); |
||
357 | |||
358 | return (ctx) ? &ctx->iface : NULL; |
||
359 | } |
||
360 | |||
361 | static st_proc_t |
||
362 | vg_api_get_proc_address(struct st_api *stapi, const char *procname) |
||
363 | { |
||
364 | return api_get_proc_address(procname); |
||
365 | } |
||
366 | |||
367 | static void |
||
368 | vg_api_destroy(struct st_api *stapi) |
||
369 | { |
||
370 | } |
||
371 | |||
372 | static const struct st_api vg_api = { |
||
373 | "Vega " PACKAGE_VERSION, |
||
374 | ST_API_OPENVG, |
||
375 | ST_PROFILE_DEFAULT_MASK, |
||
376 | 0, |
||
377 | vg_api_destroy, |
||
378 | vg_api_get_proc_address, |
||
379 | vg_api_create_context, |
||
380 | vg_api_make_current, |
||
381 | vg_api_get_current, |
||
382 | }; |
||
383 | |||
384 | const struct st_api * |
||
385 | vg_api_get(void) |
||
386 | { |
||
387 | return &vg_api; |
||
388 | }><>><>><> |