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