drivers: compute version and then initialize exec table
[mesa.git] / src / mesa / state_tracker / st_manager.c
1 /*
2 * Mesa 3-D graphics library
3 * Version: 7.9
4 *
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 <olv@lunarg.com>
27 */
28
29 #include "main/mtypes.h"
30 #include "main/context.h"
31 #include "main/mfeatures.h"
32 #include "main/texobj.h"
33 #include "main/teximage.h"
34 #include "main/texstate.h"
35 #include "main/framebuffer.h"
36 #include "main/fbobject.h"
37 #include "main/renderbuffer.h"
38 #include "main/version.h"
39 #include "st_texture.h"
40
41 #include "st_context.h"
42 #include "st_format.h"
43 #include "st_cb_fbo.h"
44 #include "st_cb_flush.h"
45 #include "st_manager.h"
46
47 #include "state_tracker/st_gl_api.h"
48
49 #include "pipe/p_context.h"
50 #include "pipe/p_screen.h"
51 #include "util/u_format.h"
52 #include "util/u_pointer.h"
53 #include "util/u_inlines.h"
54 #include "util/u_atomic.h"
55 #include "util/u_surface.h"
56
57 /**
58 * Cast wrapper to convert a struct gl_framebuffer to an st_framebuffer.
59 * Return NULL if the struct gl_framebuffer is a user-created framebuffer.
60 * We'll only return non-null for window system framebuffers.
61 * Note that this function may fail.
62 */
63 static INLINE struct st_framebuffer *
64 st_ws_framebuffer(struct gl_framebuffer *fb)
65 {
66 /* FBO cannot be casted. See st_new_framebuffer */
67 if (fb && _mesa_is_winsys_fbo(fb))
68 return (struct st_framebuffer *) fb;
69 return NULL;
70 }
71
72 /**
73 * Map an attachment to a buffer index.
74 */
75 static INLINE gl_buffer_index
76 attachment_to_buffer_index(enum st_attachment_type statt)
77 {
78 gl_buffer_index index;
79
80 switch (statt) {
81 case ST_ATTACHMENT_FRONT_LEFT:
82 index = BUFFER_FRONT_LEFT;
83 break;
84 case ST_ATTACHMENT_BACK_LEFT:
85 index = BUFFER_BACK_LEFT;
86 break;
87 case ST_ATTACHMENT_FRONT_RIGHT:
88 index = BUFFER_FRONT_RIGHT;
89 break;
90 case ST_ATTACHMENT_BACK_RIGHT:
91 index = BUFFER_BACK_RIGHT;
92 break;
93 case ST_ATTACHMENT_DEPTH_STENCIL:
94 index = BUFFER_DEPTH;
95 break;
96 case ST_ATTACHMENT_ACCUM:
97 index = BUFFER_ACCUM;
98 break;
99 case ST_ATTACHMENT_SAMPLE:
100 default:
101 index = BUFFER_COUNT;
102 break;
103 }
104
105 return index;
106 }
107
108 /**
109 * Map a buffer index to an attachment.
110 */
111 static INLINE enum st_attachment_type
112 buffer_index_to_attachment(gl_buffer_index index)
113 {
114 enum st_attachment_type statt;
115
116 switch (index) {
117 case BUFFER_FRONT_LEFT:
118 statt = ST_ATTACHMENT_FRONT_LEFT;
119 break;
120 case BUFFER_BACK_LEFT:
121 statt = ST_ATTACHMENT_BACK_LEFT;
122 break;
123 case BUFFER_FRONT_RIGHT:
124 statt = ST_ATTACHMENT_FRONT_RIGHT;
125 break;
126 case BUFFER_BACK_RIGHT:
127 statt = ST_ATTACHMENT_BACK_RIGHT;
128 break;
129 case BUFFER_DEPTH:
130 statt = ST_ATTACHMENT_DEPTH_STENCIL;
131 break;
132 case BUFFER_ACCUM:
133 statt = ST_ATTACHMENT_ACCUM;
134 break;
135 default:
136 statt = ST_ATTACHMENT_INVALID;
137 break;
138 }
139
140 return statt;
141 }
142
143 /**
144 * Make sure a context picks up the latest cached state of the
145 * drawables it binds to.
146 */
147 static void
148 st_context_validate(struct st_context *st,
149 struct st_framebuffer *stdraw,
150 struct st_framebuffer *stread)
151 {
152 if (stdraw && stdraw->stamp != st->draw_stamp) {
153 st->dirty.st |= ST_NEW_FRAMEBUFFER;
154 _mesa_resize_framebuffer(st->ctx, &stdraw->Base,
155 stdraw->Base.Width,
156 stdraw->Base.Height);
157 st->draw_stamp = stdraw->stamp;
158 }
159
160 if (stread && stread->stamp != st->read_stamp) {
161 if (stread != stdraw) {
162 st->dirty.st |= ST_NEW_FRAMEBUFFER;
163 _mesa_resize_framebuffer(st->ctx, &stread->Base,
164 stread->Base.Width,
165 stread->Base.Height);
166 }
167 st->read_stamp = stread->stamp;
168 }
169 }
170
171 /**
172 * Validate a framebuffer to make sure up-to-date pipe_textures are used.
173 * The context we need to pass in is s dummy context needed only to be
174 * able to get a pipe context to create pipe surfaces, and to have a
175 * context to call _mesa_resize_framebuffer():
176 * (That should probably be rethought, since those surfaces become
177 * drawable state, not context state, and can be freed by another pipe
178 * context).
179 */
180 static void
181 st_framebuffer_validate(struct st_framebuffer *stfb,
182 struct st_context *st)
183 {
184 struct pipe_resource *textures[ST_ATTACHMENT_COUNT];
185 uint width, height;
186 unsigned i;
187 boolean changed = FALSE;
188 int32_t new_stamp = p_atomic_read(&stfb->iface->stamp);
189
190 if (stfb->iface_stamp == new_stamp)
191 return;
192
193 /* validate the fb */
194 do {
195 if (!stfb->iface->validate(stfb->iface, stfb->statts,
196 stfb->num_statts, textures))
197 return;
198
199 stfb->iface_stamp = new_stamp;
200 new_stamp = p_atomic_read(&stfb->iface->stamp);
201 } while(stfb->iface_stamp != new_stamp);
202
203 width = stfb->Base.Width;
204 height = stfb->Base.Height;
205
206 for (i = 0; i < stfb->num_statts; i++) {
207 struct st_renderbuffer *strb;
208 struct pipe_surface *ps, surf_tmpl;
209 gl_buffer_index idx;
210
211 if (!textures[i])
212 continue;
213
214 idx = attachment_to_buffer_index(stfb->statts[i]);
215 if (idx >= BUFFER_COUNT) {
216 pipe_resource_reference(&textures[i], NULL);
217 continue;
218 }
219
220 strb = st_renderbuffer(stfb->Base.Attachment[idx].Renderbuffer);
221 assert(strb);
222 if (strb->texture == textures[i]) {
223 pipe_resource_reference(&textures[i], NULL);
224 continue;
225 }
226
227 u_surface_default_template(&surf_tmpl, textures[i]);
228 ps = st->pipe->create_surface(st->pipe, textures[i], &surf_tmpl);
229 if (ps) {
230 pipe_surface_reference(&strb->surface, ps);
231 pipe_resource_reference(&strb->texture, ps->texture);
232 /* ownership transfered */
233 pipe_surface_reference(&ps, NULL);
234
235 changed = TRUE;
236
237 strb->Base.Width = strb->surface->width;
238 strb->Base.Height = strb->surface->height;
239
240 width = strb->Base.Width;
241 height = strb->Base.Height;
242 }
243
244 pipe_resource_reference(&textures[i], NULL);
245 }
246
247 if (changed) {
248 ++stfb->stamp;
249 _mesa_resize_framebuffer(st->ctx, &stfb->Base, width, height);
250 }
251 }
252
253 /**
254 * Update the attachments to validate by looping the existing renderbuffers.
255 */
256 static void
257 st_framebuffer_update_attachments(struct st_framebuffer *stfb)
258 {
259 gl_buffer_index idx;
260
261 stfb->num_statts = 0;
262 for (idx = 0; idx < BUFFER_COUNT; idx++) {
263 struct st_renderbuffer *strb;
264 enum st_attachment_type statt;
265
266 strb = st_renderbuffer(stfb->Base.Attachment[idx].Renderbuffer);
267 if (!strb || strb->software)
268 continue;
269
270 statt = buffer_index_to_attachment(idx);
271 if (statt != ST_ATTACHMENT_INVALID &&
272 st_visual_have_buffers(stfb->iface->visual, 1 << statt))
273 stfb->statts[stfb->num_statts++] = statt;
274 }
275 stfb->stamp++;
276 }
277
278 /**
279 * Add a renderbuffer to the framebuffer.
280 */
281 static boolean
282 st_framebuffer_add_renderbuffer(struct st_framebuffer *stfb,
283 gl_buffer_index idx)
284 {
285 struct gl_renderbuffer *rb;
286 enum pipe_format format;
287 boolean sw;
288
289 if (!stfb->iface)
290 return FALSE;
291
292 /* do not distinguish depth/stencil buffers */
293 if (idx == BUFFER_STENCIL)
294 idx = BUFFER_DEPTH;
295
296 switch (idx) {
297 case BUFFER_DEPTH:
298 format = stfb->iface->visual->depth_stencil_format;
299 sw = FALSE;
300 break;
301 case BUFFER_ACCUM:
302 format = stfb->iface->visual->accum_format;
303 sw = TRUE;
304 break;
305 default:
306 format = stfb->iface->visual->color_format;
307 sw = FALSE;
308 break;
309 }
310
311 if (format == PIPE_FORMAT_NONE)
312 return FALSE;
313
314 rb = st_new_renderbuffer_fb(format, stfb->iface->visual->samples, sw);
315 if (!rb)
316 return FALSE;
317
318 if (idx != BUFFER_DEPTH) {
319 _mesa_add_renderbuffer(&stfb->Base, idx, rb);
320 }
321 else {
322 if (util_format_get_component_bits(format, UTIL_FORMAT_COLORSPACE_ZS, 0))
323 _mesa_add_renderbuffer(&stfb->Base, BUFFER_DEPTH, rb);
324 if (util_format_get_component_bits(format, UTIL_FORMAT_COLORSPACE_ZS, 1))
325 _mesa_add_renderbuffer(&stfb->Base, BUFFER_STENCIL, rb);
326 }
327
328 return TRUE;
329 }
330
331 /**
332 * Intialize a struct gl_config from a visual.
333 */
334 static void
335 st_visual_to_context_mode(const struct st_visual *visual,
336 struct gl_config *mode)
337 {
338 memset(mode, 0, sizeof(*mode));
339
340 if (st_visual_have_buffers(visual, ST_ATTACHMENT_BACK_LEFT_MASK))
341 mode->doubleBufferMode = GL_TRUE;
342 if (st_visual_have_buffers(visual,
343 ST_ATTACHMENT_FRONT_RIGHT_MASK | ST_ATTACHMENT_BACK_RIGHT_MASK))
344 mode->stereoMode = GL_TRUE;
345
346 if (visual->color_format != PIPE_FORMAT_NONE) {
347 mode->rgbMode = GL_TRUE;
348
349 mode->redBits =
350 util_format_get_component_bits(visual->color_format,
351 UTIL_FORMAT_COLORSPACE_RGB, 0);
352 mode->greenBits =
353 util_format_get_component_bits(visual->color_format,
354 UTIL_FORMAT_COLORSPACE_RGB, 1);
355 mode->blueBits =
356 util_format_get_component_bits(visual->color_format,
357 UTIL_FORMAT_COLORSPACE_RGB, 2);
358 mode->alphaBits =
359 util_format_get_component_bits(visual->color_format,
360 UTIL_FORMAT_COLORSPACE_RGB, 3);
361
362 mode->rgbBits = mode->redBits +
363 mode->greenBits + mode->blueBits + mode->alphaBits;
364 }
365
366 if (visual->depth_stencil_format != PIPE_FORMAT_NONE) {
367 mode->depthBits =
368 util_format_get_component_bits(visual->depth_stencil_format,
369 UTIL_FORMAT_COLORSPACE_ZS, 0);
370 mode->stencilBits =
371 util_format_get_component_bits(visual->depth_stencil_format,
372 UTIL_FORMAT_COLORSPACE_ZS, 1);
373
374 mode->haveDepthBuffer = mode->depthBits > 0;
375 mode->haveStencilBuffer = mode->stencilBits > 0;
376 }
377
378 if (visual->accum_format != PIPE_FORMAT_NONE) {
379 mode->haveAccumBuffer = GL_TRUE;
380
381 mode->accumRedBits =
382 util_format_get_component_bits(visual->accum_format,
383 UTIL_FORMAT_COLORSPACE_RGB, 0);
384 mode->accumGreenBits =
385 util_format_get_component_bits(visual->accum_format,
386 UTIL_FORMAT_COLORSPACE_RGB, 1);
387 mode->accumBlueBits =
388 util_format_get_component_bits(visual->accum_format,
389 UTIL_FORMAT_COLORSPACE_RGB, 2);
390 mode->accumAlphaBits =
391 util_format_get_component_bits(visual->accum_format,
392 UTIL_FORMAT_COLORSPACE_RGB, 3);
393 }
394
395 if (visual->samples > 1) {
396 mode->sampleBuffers = 1;
397 mode->samples = visual->samples;
398 }
399 }
400
401 /**
402 * Create a framebuffer from a manager interface.
403 */
404 static struct st_framebuffer *
405 st_framebuffer_create(struct st_framebuffer_iface *stfbi)
406 {
407 struct st_framebuffer *stfb;
408 struct gl_config mode;
409 gl_buffer_index idx;
410
411 if (!stfbi)
412 return NULL;
413
414 stfb = CALLOC_STRUCT(st_framebuffer);
415 if (!stfb)
416 return NULL;
417
418 st_visual_to_context_mode(stfbi->visual, &mode);
419 _mesa_initialize_window_framebuffer(&stfb->Base, &mode);
420
421 stfb->iface = stfbi;
422 stfb->iface_stamp = p_atomic_read(&stfbi->stamp) - 1;
423
424 /* add the color buffer */
425 idx = stfb->Base._ColorDrawBufferIndexes[0];
426 if (!st_framebuffer_add_renderbuffer(stfb, idx)) {
427 free(stfb);
428 return NULL;
429 }
430
431 st_framebuffer_add_renderbuffer(stfb, BUFFER_DEPTH);
432 st_framebuffer_add_renderbuffer(stfb, BUFFER_ACCUM);
433
434 stfb->stamp = 0;
435 st_framebuffer_update_attachments(stfb);
436
437 stfb->Base.Initialized = GL_TRUE;
438
439 return stfb;
440 }
441
442 /**
443 * Reference a framebuffer.
444 */
445 static void
446 st_framebuffer_reference(struct st_framebuffer **ptr,
447 struct st_framebuffer *stfb)
448 {
449 struct gl_framebuffer *fb = &stfb->Base;
450 _mesa_reference_framebuffer((struct gl_framebuffer **) ptr, fb);
451 }
452
453 static void
454 st_context_flush(struct st_context_iface *stctxi, unsigned flags,
455 struct pipe_fence_handle **fence)
456 {
457 struct st_context *st = (struct st_context *) stctxi;
458 st_flush(st, fence);
459 if (flags & ST_FLUSH_FRONT)
460 st_manager_flush_frontbuffer(st);
461 }
462
463 static boolean
464 st_context_teximage(struct st_context_iface *stctxi,
465 enum st_texture_type tex_type,
466 int level, enum pipe_format internal_format,
467 struct pipe_resource *tex, boolean mipmap)
468 {
469 struct st_context *st = (struct st_context *) stctxi;
470 struct gl_context *ctx = st->ctx;
471 struct gl_texture_unit *texUnit = _mesa_get_current_tex_unit(ctx);
472 struct gl_texture_object *texObj;
473 struct gl_texture_image *texImage;
474 struct st_texture_object *stObj;
475 struct st_texture_image *stImage;
476 GLenum internalFormat;
477 GLuint width, height, depth;
478 GLenum target;
479
480 switch (tex_type) {
481 case ST_TEXTURE_1D:
482 target = GL_TEXTURE_1D;
483 break;
484 case ST_TEXTURE_2D:
485 target = GL_TEXTURE_2D;
486 break;
487 case ST_TEXTURE_3D:
488 target = GL_TEXTURE_3D;
489 break;
490 case ST_TEXTURE_RECT:
491 target = GL_TEXTURE_RECTANGLE_ARB;
492 break;
493 default:
494 return FALSE;
495 }
496
497 texObj = _mesa_select_tex_object(ctx, texUnit, target);
498 _mesa_lock_texture(ctx, texObj);
499
500 stObj = st_texture_object(texObj);
501 /* switch to surface based */
502 if (!stObj->surface_based) {
503 _mesa_clear_texture_object(ctx, texObj);
504 stObj->surface_based = GL_TRUE;
505 }
506
507 texImage = _mesa_get_tex_image(ctx, texObj, target, level);
508 stImage = st_texture_image(texImage);
509 if (tex) {
510 gl_format texFormat;
511
512 /*
513 * XXX When internal_format and tex->format differ, st_finalize_texture
514 * needs to allocate a new texture with internal_format and copy the
515 * texture here into the new one. It will result in surface_copy being
516 * called on surfaces whose formats differ.
517 *
518 * To avoid that, internal_format is (wrongly) ignored here. A sane fix
519 * is to use a sampler view.
520 */
521 if (!st_sampler_compat_formats(tex->format, internal_format))
522 internal_format = tex->format;
523
524 if (util_format_get_component_bits(internal_format,
525 UTIL_FORMAT_COLORSPACE_RGB, 3) > 0)
526 internalFormat = GL_RGBA;
527 else
528 internalFormat = GL_RGB;
529
530 texFormat = st_ChooseTextureFormat(ctx, target, internalFormat,
531 GL_BGRA, GL_UNSIGNED_BYTE);
532
533 _mesa_init_teximage_fields(ctx, texImage,
534 tex->width0, tex->height0, 1, 0,
535 internalFormat, texFormat);
536
537 width = tex->width0;
538 height = tex->height0;
539 depth = tex->depth0;
540
541 /* grow the image size until we hit level = 0 */
542 while (level > 0) {
543 if (width != 1)
544 width <<= 1;
545 if (height != 1)
546 height <<= 1;
547 if (depth != 1)
548 depth <<= 1;
549 level--;
550 }
551 }
552 else {
553 _mesa_clear_texture_image(ctx, texImage);
554 width = height = depth = 0;
555 }
556
557 pipe_resource_reference(&stImage->pt, tex);
558 stObj->width0 = width;
559 stObj->height0 = height;
560 stObj->depth0 = depth;
561
562 _mesa_dirty_texobj(ctx, texObj, GL_TRUE);
563 _mesa_unlock_texture(ctx, texObj);
564
565 return TRUE;
566 }
567
568 static void
569 st_context_copy(struct st_context_iface *stctxi,
570 struct st_context_iface *stsrci, unsigned mask)
571 {
572 struct st_context *st = (struct st_context *) stctxi;
573 struct st_context *src = (struct st_context *) stsrci;
574
575 _mesa_copy_context(src->ctx, st->ctx, mask);
576 }
577
578 static boolean
579 st_context_share(struct st_context_iface *stctxi,
580 struct st_context_iface *stsrci)
581 {
582 struct st_context *st = (struct st_context *) stctxi;
583 struct st_context *src = (struct st_context *) stsrci;
584
585 return _mesa_share_state(st->ctx, src->ctx);
586 }
587
588 static void
589 st_context_destroy(struct st_context_iface *stctxi)
590 {
591 struct st_context *st = (struct st_context *) stctxi;
592 st_destroy_context(st);
593 }
594
595 static struct st_context_iface *
596 st_api_create_context(struct st_api *stapi, struct st_manager *smapi,
597 const struct st_context_attribs *attribs,
598 enum st_context_error *error,
599 struct st_context_iface *shared_stctxi)
600 {
601 struct st_context *shared_ctx = (struct st_context *) shared_stctxi;
602 struct st_context *st;
603 struct pipe_context *pipe;
604 struct gl_config mode;
605 gl_api api;
606
607 if (!(stapi->profile_mask & (1 << attribs->profile)))
608 return NULL;
609
610 switch (attribs->profile) {
611 case ST_PROFILE_DEFAULT:
612 api = API_OPENGL_COMPAT;
613 break;
614 case ST_PROFILE_OPENGL_ES1:
615 api = API_OPENGLES;
616 break;
617 case ST_PROFILE_OPENGL_ES2:
618 api = API_OPENGLES2;
619 break;
620 case ST_PROFILE_OPENGL_CORE:
621 api = API_OPENGL_CORE;
622 break;
623 default:
624 *error = ST_CONTEXT_ERROR_BAD_API;
625 return NULL;
626 break;
627 }
628
629 pipe = smapi->screen->context_create(smapi->screen, NULL);
630 if (!pipe) {
631 *error = ST_CONTEXT_ERROR_NO_MEMORY;
632 return NULL;
633 }
634
635 st_visual_to_context_mode(&attribs->visual, &mode);
636 st = st_create_context(api, pipe, &mode, shared_ctx, &attribs->options);
637 if (!st) {
638 *error = ST_CONTEXT_ERROR_NO_MEMORY;
639 pipe->destroy(pipe);
640 return NULL;
641 }
642
643 if (attribs->flags & ST_CONTEXT_FLAG_DEBUG)
644 st->ctx->Const.ContextFlags |= GL_CONTEXT_FLAG_DEBUG_BIT;
645 if (attribs->flags & ST_CONTEXT_FLAG_FORWARD_COMPATIBLE)
646 st->ctx->Const.ContextFlags |= GL_CONTEXT_FLAG_FORWARD_COMPATIBLE_BIT;
647
648 /* need to perform version check */
649 if (attribs->major > 1 || attribs->minor > 0) {
650 /* Is the actual version less than the requested version?
651 */
652 if (st->ctx->Version < attribs->major * 10 + attribs->minor) {
653 *error = ST_CONTEXT_ERROR_BAD_VERSION;
654 st_destroy_context(st);
655 return NULL;
656 }
657 }
658
659 st->invalidate_on_gl_viewport =
660 smapi->get_param(smapi, ST_MANAGER_BROKEN_INVALIDATE);
661
662 st->iface.destroy = st_context_destroy;
663 st->iface.flush = st_context_flush;
664 st->iface.teximage = st_context_teximage;
665 st->iface.copy = st_context_copy;
666 st->iface.share = st_context_share;
667 st->iface.st_context_private = (void *) smapi;
668 st->iface.cso_context = st->cso_context;
669 st->iface.pipe = st->pipe;
670
671 *error = ST_CONTEXT_SUCCESS;
672 return &st->iface;
673 }
674
675 static struct st_context_iface *
676 st_api_get_current(struct st_api *stapi)
677 {
678 GET_CURRENT_CONTEXT(ctx);
679 struct st_context *st = (ctx) ? ctx->st : NULL;
680
681 return (st) ? &st->iface : NULL;
682 }
683
684 static struct st_framebuffer *
685 st_framebuffer_reuse_or_create(struct gl_framebuffer *fb,
686 struct st_framebuffer_iface *stfbi)
687 {
688 struct st_framebuffer *cur = st_ws_framebuffer(fb), *stfb = NULL;
689
690 /* dummy framebuffers cant be used as st_framebuffer */
691 if (cur && &cur->Base != _mesa_get_incomplete_framebuffer() &&
692 cur->iface == stfbi) {
693 /* reuse the current stfb */
694 st_framebuffer_reference(&stfb, cur);
695 }
696 else {
697 /* create a new one */
698 stfb = st_framebuffer_create(stfbi);
699 }
700
701 return stfb;
702 }
703
704 static boolean
705 st_api_make_current(struct st_api *stapi, struct st_context_iface *stctxi,
706 struct st_framebuffer_iface *stdrawi,
707 struct st_framebuffer_iface *streadi)
708 {
709 struct st_context *st = (struct st_context *) stctxi;
710 struct st_framebuffer *stdraw, *stread;
711 boolean ret;
712
713 _glapi_check_multithread();
714
715 if (st) {
716 /* reuse or create the draw fb */
717 stdraw = st_framebuffer_reuse_or_create(st->ctx->WinSysDrawBuffer,
718 stdrawi);
719 if (streadi != stdrawi) {
720 /* do the same for the read fb */
721 stread = st_framebuffer_reuse_or_create(st->ctx->WinSysReadBuffer,
722 streadi);
723 }
724 else {
725 stread = NULL;
726 /* reuse the draw fb for the read fb */
727 if (stdraw)
728 st_framebuffer_reference(&stread, stdraw);
729 }
730
731 if (stdraw && stread) {
732 st_framebuffer_validate(stdraw, st);
733 if (stread != stdraw)
734 st_framebuffer_validate(stread, st);
735
736 ret = _mesa_make_current(st->ctx, &stdraw->Base, &stread->Base);
737
738 st->draw_stamp = stdraw->stamp - 1;
739 st->read_stamp = stread->stamp - 1;
740 st_context_validate(st, stdraw, stread);
741 }
742 else {
743 struct gl_framebuffer *incomplete = _mesa_get_incomplete_framebuffer();
744 ret = _mesa_make_current(st->ctx, incomplete, incomplete);
745 }
746
747 st_framebuffer_reference(&stdraw, NULL);
748 st_framebuffer_reference(&stread, NULL);
749 }
750 else {
751 ret = _mesa_make_current(NULL, NULL, NULL);
752 }
753
754 return ret;
755 }
756
757 static st_proc_t
758 st_api_get_proc_address(struct st_api *stapi, const char *procname)
759 {
760 return (st_proc_t) _glapi_get_proc_address(procname);
761 }
762
763 static void
764 st_api_destroy(struct st_api *stapi)
765 {
766 }
767
768 /**
769 * Flush the front buffer if the current context renders to the front buffer.
770 */
771 void
772 st_manager_flush_frontbuffer(struct st_context *st)
773 {
774 struct st_framebuffer *stfb = st_ws_framebuffer(st->ctx->DrawBuffer);
775 struct st_renderbuffer *strb = NULL;
776
777 if (stfb)
778 strb = st_renderbuffer(stfb->Base.Attachment[BUFFER_FRONT_LEFT].Renderbuffer);
779 if (!strb)
780 return;
781
782 /* never a dummy fb */
783 assert(&stfb->Base != _mesa_get_incomplete_framebuffer());
784 stfb->iface->flush_front(&st->iface, stfb->iface, ST_ATTACHMENT_FRONT_LEFT);
785 }
786
787 /**
788 * Return the surface of an EGLImage.
789 * FIXME: I think this should operate on resources, not surfaces
790 */
791 struct pipe_surface *
792 st_manager_get_egl_image_surface(struct st_context *st, void *eglimg)
793 {
794 struct st_manager *smapi =
795 (struct st_manager *) st->iface.st_context_private;
796 struct st_egl_image stimg;
797 struct pipe_surface *ps, surf_tmpl;
798
799 if (!smapi || !smapi->get_egl_image)
800 return NULL;
801
802 memset(&stimg, 0, sizeof(stimg));
803 if (!smapi->get_egl_image(smapi, eglimg, &stimg))
804 return NULL;
805
806 u_surface_default_template(&surf_tmpl, stimg.texture);
807 surf_tmpl.u.tex.level = stimg.level;
808 surf_tmpl.u.tex.first_layer = stimg.layer;
809 surf_tmpl.u.tex.last_layer = stimg.layer;
810 ps = st->pipe->create_surface(st->pipe, stimg.texture, &surf_tmpl);
811 pipe_resource_reference(&stimg.texture, NULL);
812
813 return ps;
814 }
815
816 /**
817 * Re-validate the framebuffers.
818 */
819 void
820 st_manager_validate_framebuffers(struct st_context *st)
821 {
822 struct st_framebuffer *stdraw = st_ws_framebuffer(st->ctx->DrawBuffer);
823 struct st_framebuffer *stread = st_ws_framebuffer(st->ctx->ReadBuffer);
824
825 if (stdraw)
826 st_framebuffer_validate(stdraw, st);
827 if (stread && stread != stdraw)
828 st_framebuffer_validate(stread, st);
829
830 st_context_validate(st, stdraw, stread);
831 }
832
833 /**
834 * Add a color renderbuffer on demand.
835 */
836 boolean
837 st_manager_add_color_renderbuffer(struct st_context *st,
838 struct gl_framebuffer *fb,
839 gl_buffer_index idx)
840 {
841 struct st_framebuffer *stfb = st_ws_framebuffer(fb);
842
843 /* FBO */
844 if (!stfb)
845 return FALSE;
846
847 if (stfb->Base.Attachment[idx].Renderbuffer)
848 return TRUE;
849
850 switch (idx) {
851 case BUFFER_FRONT_LEFT:
852 case BUFFER_BACK_LEFT:
853 case BUFFER_FRONT_RIGHT:
854 case BUFFER_BACK_RIGHT:
855 break;
856 default:
857 return FALSE;
858 break;
859 }
860
861 if (!st_framebuffer_add_renderbuffer(stfb, idx))
862 return FALSE;
863
864 st_framebuffer_update_attachments(stfb);
865
866 /*
867 * Force a call to the state tracker manager to validate the
868 * new renderbuffer. It might be that there is a window system
869 * renderbuffer available.
870 */
871 if(stfb->iface)
872 stfb->iface_stamp = p_atomic_read(&stfb->iface->stamp) - 1;
873
874 st_invalidate_state(st->ctx, _NEW_BUFFERS);
875
876 return TRUE;
877 }
878
879 static const struct st_api st_gl_api = {
880 "Mesa " MESA_VERSION_STRING,
881 ST_API_OPENGL,
882 #if FEATURE_GL
883 ST_PROFILE_DEFAULT_MASK |
884 ST_PROFILE_OPENGL_CORE_MASK |
885 #endif
886 #if FEATURE_ES1
887 ST_PROFILE_OPENGL_ES1_MASK |
888 #endif
889 #if FEATURE_ES2
890 ST_PROFILE_OPENGL_ES2_MASK |
891 #endif
892 0,
893 ST_API_FEATURE_MS_VISUALS_MASK,
894 st_api_destroy,
895 st_api_get_proc_address,
896 st_api_create_context,
897 st_api_make_current,
898 st_api_get_current,
899 };
900
901 struct st_api *
902 st_gl_api_create(void)
903 {
904 return (struct st_api *) &st_gl_api;
905 }