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