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