st/mesa: remove #if FEATURE_GL/ES tests
[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/texobj.h"
32 #include "main/teximage.h"
33 #include "main/texstate.h"
34 #include "main/framebuffer.h"
35 #include "main/fbobject.h"
36 #include "main/renderbuffer.h"
37 #include "main/version.h"
38 #include "st_texture.h"
39
40 #include "st_context.h"
41 #include "st_format.h"
42 #include "st_cb_fbo.h"
43 #include "st_cb_flush.h"
44 #include "st_manager.h"
45
46 #include "state_tracker/st_gl_api.h"
47
48 #include "pipe/p_context.h"
49 #include "pipe/p_screen.h"
50 #include "util/u_format.h"
51 #include "util/u_pointer.h"
52 #include "util/u_inlines.h"
53 #include "util/u_atomic.h"
54 #include "util/u_surface.h"
55
56 /**
57 * Cast wrapper to convert a struct gl_framebuffer to an st_framebuffer.
58 * Return NULL if the struct gl_framebuffer is a user-created framebuffer.
59 * We'll only return non-null for window system framebuffers.
60 * Note that this function may fail.
61 */
62 static INLINE struct st_framebuffer *
63 st_ws_framebuffer(struct gl_framebuffer *fb)
64 {
65 /* FBO cannot be casted. See st_new_framebuffer */
66 if (fb && _mesa_is_winsys_fbo(fb))
67 return (struct st_framebuffer *) fb;
68 return NULL;
69 }
70
71 /**
72 * Map an attachment to a buffer index.
73 */
74 static INLINE gl_buffer_index
75 attachment_to_buffer_index(enum st_attachment_type statt)
76 {
77 gl_buffer_index index;
78
79 switch (statt) {
80 case ST_ATTACHMENT_FRONT_LEFT:
81 index = BUFFER_FRONT_LEFT;
82 break;
83 case ST_ATTACHMENT_BACK_LEFT:
84 index = BUFFER_BACK_LEFT;
85 break;
86 case ST_ATTACHMENT_FRONT_RIGHT:
87 index = BUFFER_FRONT_RIGHT;
88 break;
89 case ST_ATTACHMENT_BACK_RIGHT:
90 index = BUFFER_BACK_RIGHT;
91 break;
92 case ST_ATTACHMENT_DEPTH_STENCIL:
93 index = BUFFER_DEPTH;
94 break;
95 case ST_ATTACHMENT_ACCUM:
96 index = BUFFER_ACCUM;
97 break;
98 case ST_ATTACHMENT_SAMPLE:
99 default:
100 index = BUFFER_COUNT;
101 break;
102 }
103
104 return index;
105 }
106
107 /**
108 * Map a buffer index to an attachment.
109 */
110 static INLINE enum st_attachment_type
111 buffer_index_to_attachment(gl_buffer_index index)
112 {
113 enum st_attachment_type statt;
114
115 switch (index) {
116 case BUFFER_FRONT_LEFT:
117 statt = ST_ATTACHMENT_FRONT_LEFT;
118 break;
119 case BUFFER_BACK_LEFT:
120 statt = ST_ATTACHMENT_BACK_LEFT;
121 break;
122 case BUFFER_FRONT_RIGHT:
123 statt = ST_ATTACHMENT_FRONT_RIGHT;
124 break;
125 case BUFFER_BACK_RIGHT:
126 statt = ST_ATTACHMENT_BACK_RIGHT;
127 break;
128 case BUFFER_DEPTH:
129 statt = ST_ATTACHMENT_DEPTH_STENCIL;
130 break;
131 case BUFFER_ACCUM:
132 statt = ST_ATTACHMENT_ACCUM;
133 break;
134 default:
135 statt = ST_ATTACHMENT_INVALID;
136 break;
137 }
138
139 return statt;
140 }
141
142 /**
143 * Make sure a context picks up the latest cached state of the
144 * drawables it binds to.
145 */
146 static void
147 st_context_validate(struct st_context *st,
148 struct st_framebuffer *stdraw,
149 struct st_framebuffer *stread)
150 {
151 if (stdraw && stdraw->stamp != st->draw_stamp) {
152 st->dirty.st |= ST_NEW_FRAMEBUFFER;
153 _mesa_resize_framebuffer(st->ctx, &stdraw->Base,
154 stdraw->Base.Width,
155 stdraw->Base.Height);
156 st->draw_stamp = stdraw->stamp;
157 }
158
159 if (stread && stread->stamp != st->read_stamp) {
160 if (stread != stdraw) {
161 st->dirty.st |= ST_NEW_FRAMEBUFFER;
162 _mesa_resize_framebuffer(st->ctx, &stread->Base,
163 stread->Base.Width,
164 stread->Base.Height);
165 }
166 st->read_stamp = stread->stamp;
167 }
168 }
169
170 /**
171 * Validate a framebuffer to make sure up-to-date pipe_textures are used.
172 * The context is only used for creating pipe surfaces and for calling
173 * _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 ps = st->pipe->create_surface(st->pipe, textures[i], &surf_tmpl);
227 if (ps) {
228 pipe_surface_reference(&strb->surface, ps);
229 pipe_resource_reference(&strb->texture, ps->texture);
230 /* ownership transfered */
231 pipe_surface_reference(&ps, NULL);
232
233 changed = TRUE;
234
235 strb->Base.Width = strb->surface->width;
236 strb->Base.Height = strb->surface->height;
237
238 width = strb->Base.Width;
239 height = strb->Base.Height;
240 }
241
242 pipe_resource_reference(&textures[i], NULL);
243 }
244
245 if (changed) {
246 ++stfb->stamp;
247 _mesa_resize_framebuffer(st->ctx, &stfb->Base, width, height);
248 }
249 }
250
251 /**
252 * Update the attachments to validate by looping the existing renderbuffers.
253 */
254 static void
255 st_framebuffer_update_attachments(struct st_framebuffer *stfb)
256 {
257 gl_buffer_index idx;
258
259 stfb->num_statts = 0;
260 for (idx = 0; idx < BUFFER_COUNT; idx++) {
261 struct st_renderbuffer *strb;
262 enum st_attachment_type statt;
263
264 strb = st_renderbuffer(stfb->Base.Attachment[idx].Renderbuffer);
265 if (!strb || strb->software)
266 continue;
267
268 statt = buffer_index_to_attachment(idx);
269 if (statt != ST_ATTACHMENT_INVALID &&
270 st_visual_have_buffers(stfb->iface->visual, 1 << statt))
271 stfb->statts[stfb->num_statts++] = statt;
272 }
273 stfb->stamp++;
274 }
275
276 /**
277 * Add a renderbuffer to the framebuffer.
278 */
279 static boolean
280 st_framebuffer_add_renderbuffer(struct st_framebuffer *stfb,
281 gl_buffer_index idx)
282 {
283 struct gl_renderbuffer *rb;
284 enum pipe_format format;
285 boolean sw;
286
287 if (!stfb->iface)
288 return FALSE;
289
290 /* do not distinguish depth/stencil buffers */
291 if (idx == BUFFER_STENCIL)
292 idx = BUFFER_DEPTH;
293
294 switch (idx) {
295 case BUFFER_DEPTH:
296 format = stfb->iface->visual->depth_stencil_format;
297 sw = FALSE;
298 break;
299 case BUFFER_ACCUM:
300 format = stfb->iface->visual->accum_format;
301 sw = TRUE;
302 break;
303 default:
304 format = stfb->iface->visual->color_format;
305 sw = FALSE;
306 break;
307 }
308
309 if (format == PIPE_FORMAT_NONE)
310 return FALSE;
311
312 rb = st_new_renderbuffer_fb(format, stfb->iface->visual->samples, sw);
313 if (!rb)
314 return FALSE;
315
316 if (idx != BUFFER_DEPTH) {
317 _mesa_add_renderbuffer(&stfb->Base, idx, rb);
318 }
319 else {
320 if (util_format_get_component_bits(format, UTIL_FORMAT_COLORSPACE_ZS, 0))
321 _mesa_add_renderbuffer(&stfb->Base, BUFFER_DEPTH, rb);
322 if (util_format_get_component_bits(format, UTIL_FORMAT_COLORSPACE_ZS, 1))
323 _mesa_add_renderbuffer(&stfb->Base, BUFFER_STENCIL, rb);
324 }
325
326 return TRUE;
327 }
328
329 /**
330 * Intialize a struct gl_config from a visual.
331 */
332 static void
333 st_visual_to_context_mode(const struct st_visual *visual,
334 struct gl_config *mode)
335 {
336 memset(mode, 0, sizeof(*mode));
337
338 if (st_visual_have_buffers(visual, ST_ATTACHMENT_BACK_LEFT_MASK))
339 mode->doubleBufferMode = GL_TRUE;
340 if (st_visual_have_buffers(visual,
341 ST_ATTACHMENT_FRONT_RIGHT_MASK | ST_ATTACHMENT_BACK_RIGHT_MASK))
342 mode->stereoMode = GL_TRUE;
343
344 if (visual->color_format != PIPE_FORMAT_NONE) {
345 mode->rgbMode = GL_TRUE;
346
347 mode->redBits =
348 util_format_get_component_bits(visual->color_format,
349 UTIL_FORMAT_COLORSPACE_RGB, 0);
350 mode->greenBits =
351 util_format_get_component_bits(visual->color_format,
352 UTIL_FORMAT_COLORSPACE_RGB, 1);
353 mode->blueBits =
354 util_format_get_component_bits(visual->color_format,
355 UTIL_FORMAT_COLORSPACE_RGB, 2);
356 mode->alphaBits =
357 util_format_get_component_bits(visual->color_format,
358 UTIL_FORMAT_COLORSPACE_RGB, 3);
359
360 mode->rgbBits = mode->redBits +
361 mode->greenBits + mode->blueBits + mode->alphaBits;
362 }
363
364 if (visual->depth_stencil_format != PIPE_FORMAT_NONE) {
365 mode->depthBits =
366 util_format_get_component_bits(visual->depth_stencil_format,
367 UTIL_FORMAT_COLORSPACE_ZS, 0);
368 mode->stencilBits =
369 util_format_get_component_bits(visual->depth_stencil_format,
370 UTIL_FORMAT_COLORSPACE_ZS, 1);
371
372 mode->haveDepthBuffer = mode->depthBits > 0;
373 mode->haveStencilBuffer = mode->stencilBits > 0;
374 }
375
376 if (visual->accum_format != PIPE_FORMAT_NONE) {
377 mode->haveAccumBuffer = GL_TRUE;
378
379 mode->accumRedBits =
380 util_format_get_component_bits(visual->accum_format,
381 UTIL_FORMAT_COLORSPACE_RGB, 0);
382 mode->accumGreenBits =
383 util_format_get_component_bits(visual->accum_format,
384 UTIL_FORMAT_COLORSPACE_RGB, 1);
385 mode->accumBlueBits =
386 util_format_get_component_bits(visual->accum_format,
387 UTIL_FORMAT_COLORSPACE_RGB, 2);
388 mode->accumAlphaBits =
389 util_format_get_component_bits(visual->accum_format,
390 UTIL_FORMAT_COLORSPACE_RGB, 3);
391 }
392
393 if (visual->samples > 1) {
394 mode->sampleBuffers = 1;
395 mode->samples = visual->samples;
396 }
397 }
398
399 /**
400 * Create a framebuffer from a manager interface.
401 */
402 static struct st_framebuffer *
403 st_framebuffer_create(struct st_framebuffer_iface *stfbi)
404 {
405 struct st_framebuffer *stfb;
406 struct gl_config mode;
407 gl_buffer_index idx;
408
409 if (!stfbi)
410 return NULL;
411
412 stfb = CALLOC_STRUCT(st_framebuffer);
413 if (!stfb)
414 return NULL;
415
416 st_visual_to_context_mode(stfbi->visual, &mode);
417 _mesa_initialize_window_framebuffer(&stfb->Base, &mode);
418
419 stfb->iface = stfbi;
420 stfb->iface_stamp = p_atomic_read(&stfbi->stamp) - 1;
421
422 /* add the color buffer */
423 idx = stfb->Base._ColorDrawBufferIndexes[0];
424 if (!st_framebuffer_add_renderbuffer(stfb, idx)) {
425 free(stfb);
426 return NULL;
427 }
428
429 st_framebuffer_add_renderbuffer(stfb, BUFFER_DEPTH);
430 st_framebuffer_add_renderbuffer(stfb, BUFFER_ACCUM);
431
432 stfb->stamp = 0;
433 st_framebuffer_update_attachments(stfb);
434
435 stfb->Base.Initialized = GL_TRUE;
436
437 return stfb;
438 }
439
440 /**
441 * Reference a framebuffer.
442 */
443 static void
444 st_framebuffer_reference(struct st_framebuffer **ptr,
445 struct st_framebuffer *stfb)
446 {
447 struct gl_framebuffer *fb = &stfb->Base;
448 _mesa_reference_framebuffer((struct gl_framebuffer **) ptr, fb);
449 }
450
451 static void
452 st_context_flush(struct st_context_iface *stctxi, unsigned flags,
453 struct pipe_fence_handle **fence)
454 {
455 struct st_context *st = (struct st_context *) stctxi;
456 enum pipe_flush_flags pipe_flags = 0;
457
458 if (flags & ST_FLUSH_END_OF_FRAME) {
459 pipe_flags |= PIPE_FLUSH_END_OF_FRAME;
460 }
461
462 st_flush(st, fence, pipe_flags);
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 tex_type,
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 GLenum target;
483
484 switch (tex_type) {
485 case ST_TEXTURE_1D:
486 target = GL_TEXTURE_1D;
487 break;
488 case ST_TEXTURE_2D:
489 target = GL_TEXTURE_2D;
490 break;
491 case ST_TEXTURE_3D:
492 target = GL_TEXTURE_3D;
493 break;
494 case ST_TEXTURE_RECT:
495 target = GL_TEXTURE_RECTANGLE_ARB;
496 break;
497 default:
498 return FALSE;
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, target, 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_COMPAT;
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 api = API_OPENGL_CORE;
626 break;
627 default:
628 *error = ST_CONTEXT_ERROR_BAD_API;
629 return NULL;
630 break;
631 }
632
633 pipe = smapi->screen->context_create(smapi->screen, NULL);
634 if (!pipe) {
635 *error = ST_CONTEXT_ERROR_NO_MEMORY;
636 return NULL;
637 }
638
639 st_visual_to_context_mode(&attribs->visual, &mode);
640 st = st_create_context(api, pipe, &mode, shared_ctx, &attribs->options);
641 if (!st) {
642 *error = ST_CONTEXT_ERROR_NO_MEMORY;
643 pipe->destroy(pipe);
644 return NULL;
645 }
646
647 if (attribs->flags & ST_CONTEXT_FLAG_DEBUG)
648 st->ctx->Const.ContextFlags |= GL_CONTEXT_FLAG_DEBUG_BIT;
649 if (attribs->flags & ST_CONTEXT_FLAG_FORWARD_COMPATIBLE)
650 st->ctx->Const.ContextFlags |= GL_CONTEXT_FLAG_FORWARD_COMPATIBLE_BIT;
651
652 /* need to perform version check */
653 if (attribs->major > 1 || attribs->minor > 0) {
654 /* Is the actual version less than the requested version?
655 */
656 if (st->ctx->Version < attribs->major * 10 + attribs->minor) {
657 *error = ST_CONTEXT_ERROR_BAD_VERSION;
658 st_destroy_context(st);
659 return NULL;
660 }
661 }
662
663 st->invalidate_on_gl_viewport =
664 smapi->get_param(smapi, ST_MANAGER_BROKEN_INVALIDATE);
665
666 st->iface.destroy = st_context_destroy;
667 st->iface.flush = st_context_flush;
668 st->iface.teximage = st_context_teximage;
669 st->iface.copy = st_context_copy;
670 st->iface.share = st_context_share;
671 st->iface.st_context_private = (void *) smapi;
672 st->iface.cso_context = st->cso_context;
673 st->iface.pipe = st->pipe;
674
675 *error = ST_CONTEXT_SUCCESS;
676 return &st->iface;
677 }
678
679 static struct st_context_iface *
680 st_api_get_current(struct st_api *stapi)
681 {
682 GET_CURRENT_CONTEXT(ctx);
683 struct st_context *st = (ctx) ? ctx->st : NULL;
684
685 return (st) ? &st->iface : NULL;
686 }
687
688 static struct st_framebuffer *
689 st_framebuffer_reuse_or_create(struct gl_framebuffer *fb,
690 struct st_framebuffer_iface *stfbi)
691 {
692 struct st_framebuffer *cur = st_ws_framebuffer(fb), *stfb = NULL;
693
694 /* dummy framebuffers cant be used as st_framebuffer */
695 if (cur && &cur->Base != _mesa_get_incomplete_framebuffer() &&
696 cur->iface == stfbi) {
697 /* reuse the current stfb */
698 st_framebuffer_reference(&stfb, cur);
699 }
700 else {
701 /* create a new one */
702 stfb = st_framebuffer_create(stfbi);
703 }
704
705 return stfb;
706 }
707
708 static boolean
709 st_api_make_current(struct st_api *stapi, struct st_context_iface *stctxi,
710 struct st_framebuffer_iface *stdrawi,
711 struct st_framebuffer_iface *streadi)
712 {
713 struct st_context *st = (struct st_context *) stctxi;
714 struct st_framebuffer *stdraw, *stread;
715 boolean ret;
716
717 _glapi_check_multithread();
718
719 if (st) {
720 /* reuse or create the draw fb */
721 stdraw = st_framebuffer_reuse_or_create(st->ctx->WinSysDrawBuffer,
722 stdrawi);
723 if (streadi != stdrawi) {
724 /* do the same for the read fb */
725 stread = st_framebuffer_reuse_or_create(st->ctx->WinSysReadBuffer,
726 streadi);
727 }
728 else {
729 stread = NULL;
730 /* reuse the draw fb for the read fb */
731 if (stdraw)
732 st_framebuffer_reference(&stread, stdraw);
733 }
734
735 if (stdraw && stread) {
736 st_framebuffer_validate(stdraw, st);
737 if (stread != stdraw)
738 st_framebuffer_validate(stread, st);
739
740 ret = _mesa_make_current(st->ctx, &stdraw->Base, &stread->Base);
741
742 st->draw_stamp = stdraw->stamp - 1;
743 st->read_stamp = stread->stamp - 1;
744 st_context_validate(st, stdraw, stread);
745 }
746 else {
747 struct gl_framebuffer *incomplete = _mesa_get_incomplete_framebuffer();
748 ret = _mesa_make_current(st->ctx, incomplete, incomplete);
749 }
750
751 st_framebuffer_reference(&stdraw, NULL);
752 st_framebuffer_reference(&stread, NULL);
753 }
754 else {
755 ret = _mesa_make_current(NULL, NULL, NULL);
756 }
757
758 return ret;
759 }
760
761 static st_proc_t
762 st_api_get_proc_address(struct st_api *stapi, const char *procname)
763 {
764 return (st_proc_t) _glapi_get_proc_address(procname);
765 }
766
767 static void
768 st_api_destroy(struct st_api *stapi)
769 {
770 }
771
772 /**
773 * Flush the front buffer if the current context renders to the front buffer.
774 */
775 void
776 st_manager_flush_frontbuffer(struct st_context *st)
777 {
778 struct st_framebuffer *stfb = st_ws_framebuffer(st->ctx->DrawBuffer);
779 struct st_renderbuffer *strb = NULL;
780
781 if (stfb)
782 strb = st_renderbuffer(stfb->Base.Attachment[BUFFER_FRONT_LEFT].Renderbuffer);
783 if (!strb)
784 return;
785
786 /* never a dummy fb */
787 assert(&stfb->Base != _mesa_get_incomplete_framebuffer());
788 stfb->iface->flush_front(&st->iface, stfb->iface, ST_ATTACHMENT_FRONT_LEFT);
789 }
790
791 /**
792 * Return the surface of an EGLImage.
793 * FIXME: I think this should operate on resources, not surfaces
794 */
795 struct pipe_surface *
796 st_manager_get_egl_image_surface(struct st_context *st, void *eglimg)
797 {
798 struct st_manager *smapi =
799 (struct st_manager *) st->iface.st_context_private;
800 struct st_egl_image stimg;
801 struct pipe_surface *ps, surf_tmpl;
802
803 if (!smapi || !smapi->get_egl_image)
804 return NULL;
805
806 memset(&stimg, 0, sizeof(stimg));
807 if (!smapi->get_egl_image(smapi, eglimg, &stimg))
808 return NULL;
809
810 u_surface_default_template(&surf_tmpl, stimg.texture);
811 surf_tmpl.u.tex.level = stimg.level;
812 surf_tmpl.u.tex.first_layer = stimg.layer;
813 surf_tmpl.u.tex.last_layer = stimg.layer;
814 ps = st->pipe->create_surface(st->pipe, stimg.texture, &surf_tmpl);
815 pipe_resource_reference(&stimg.texture, NULL);
816
817 return ps;
818 }
819
820 /**
821 * Re-validate the framebuffers.
822 */
823 void
824 st_manager_validate_framebuffers(struct st_context *st)
825 {
826 struct st_framebuffer *stdraw = st_ws_framebuffer(st->ctx->DrawBuffer);
827 struct st_framebuffer *stread = st_ws_framebuffer(st->ctx->ReadBuffer);
828
829 if (stdraw)
830 st_framebuffer_validate(stdraw, st);
831 if (stread && stread != stdraw)
832 st_framebuffer_validate(stread, st);
833
834 st_context_validate(st, stdraw, stread);
835 }
836
837 /**
838 * Add a color renderbuffer on demand.
839 */
840 boolean
841 st_manager_add_color_renderbuffer(struct st_context *st,
842 struct gl_framebuffer *fb,
843 gl_buffer_index idx)
844 {
845 struct st_framebuffer *stfb = st_ws_framebuffer(fb);
846
847 /* FBO */
848 if (!stfb)
849 return FALSE;
850
851 if (stfb->Base.Attachment[idx].Renderbuffer)
852 return TRUE;
853
854 switch (idx) {
855 case BUFFER_FRONT_LEFT:
856 case BUFFER_BACK_LEFT:
857 case BUFFER_FRONT_RIGHT:
858 case BUFFER_BACK_RIGHT:
859 break;
860 default:
861 return FALSE;
862 break;
863 }
864
865 if (!st_framebuffer_add_renderbuffer(stfb, idx))
866 return FALSE;
867
868 st_framebuffer_update_attachments(stfb);
869
870 /*
871 * Force a call to the state tracker manager to validate the
872 * new renderbuffer. It might be that there is a window system
873 * renderbuffer available.
874 */
875 if(stfb->iface)
876 stfb->iface_stamp = p_atomic_read(&stfb->iface->stamp) - 1;
877
878 st_invalidate_state(st->ctx, _NEW_BUFFERS);
879
880 return TRUE;
881 }
882
883 static const struct st_api st_gl_api = {
884 "Mesa " PACKAGE_VERSION,
885 ST_API_OPENGL,
886 ST_PROFILE_DEFAULT_MASK |
887 ST_PROFILE_OPENGL_CORE_MASK |
888 ST_PROFILE_OPENGL_ES1_MASK |
889 ST_PROFILE_OPENGL_ES2_MASK |
890 0,
891 ST_API_FEATURE_MS_VISUALS_MASK,
892 st_api_destroy,
893 st_api_get_proc_address,
894 st_api_create_context,
895 st_api_make_current,
896 st_api_get_current,
897 };
898
899 struct st_api *
900 st_gl_api_create(void)
901 {
902 return (struct st_api *) &st_gl_api;
903 }