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