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