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