st/mesa: handle texture_from_pixmap and other surface-based textures correctly
[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 unsigned 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 pipe_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 = st_pipe_format_to_mesa_format(pipe_format);
515
516 if (util_format_has_alpha(tex->format))
517 internalFormat = GL_RGBA;
518 else
519 internalFormat = GL_RGB;
520
521 _mesa_init_teximage_fields(ctx, texImage,
522 tex->width0, tex->height0, 1, 0,
523 internalFormat, texFormat);
524
525 width = tex->width0;
526 height = tex->height0;
527 depth = tex->depth0;
528
529 /* grow the image size until we hit level = 0 */
530 while (level > 0) {
531 if (width != 1)
532 width <<= 1;
533 if (height != 1)
534 height <<= 1;
535 if (depth != 1)
536 depth <<= 1;
537 level--;
538 }
539 }
540 else {
541 _mesa_clear_texture_image(ctx, texImage);
542 width = height = depth = 0;
543 }
544
545 pipe_resource_reference(&stImage->pt, tex);
546 stObj->width0 = width;
547 stObj->height0 = height;
548 stObj->depth0 = depth;
549 stObj->surface_format = pipe_format;
550
551 _mesa_dirty_texobj(ctx, texObj, GL_TRUE);
552 _mesa_unlock_texture(ctx, texObj);
553
554 return TRUE;
555 }
556
557 static void
558 st_context_copy(struct st_context_iface *stctxi,
559 struct st_context_iface *stsrci, unsigned mask)
560 {
561 struct st_context *st = (struct st_context *) stctxi;
562 struct st_context *src = (struct st_context *) stsrci;
563
564 _mesa_copy_context(src->ctx, st->ctx, mask);
565 }
566
567 static boolean
568 st_context_share(struct st_context_iface *stctxi,
569 struct st_context_iface *stsrci)
570 {
571 struct st_context *st = (struct st_context *) stctxi;
572 struct st_context *src = (struct st_context *) stsrci;
573
574 return _mesa_share_state(st->ctx, src->ctx);
575 }
576
577 static void
578 st_context_destroy(struct st_context_iface *stctxi)
579 {
580 struct st_context *st = (struct st_context *) stctxi;
581 st_destroy_context(st);
582 }
583
584 static struct st_context_iface *
585 st_api_create_context(struct st_api *stapi, struct st_manager *smapi,
586 const struct st_context_attribs *attribs,
587 enum st_context_error *error,
588 struct st_context_iface *shared_stctxi)
589 {
590 struct st_context *shared_ctx = (struct st_context *) shared_stctxi;
591 struct st_context *st;
592 struct pipe_context *pipe;
593 struct gl_config mode;
594 gl_api api;
595
596 if (!(stapi->profile_mask & (1 << attribs->profile)))
597 return NULL;
598
599 switch (attribs->profile) {
600 case ST_PROFILE_DEFAULT:
601 api = API_OPENGL_COMPAT;
602 break;
603 case ST_PROFILE_OPENGL_ES1:
604 api = API_OPENGLES;
605 break;
606 case ST_PROFILE_OPENGL_ES2:
607 api = API_OPENGLES2;
608 break;
609 case ST_PROFILE_OPENGL_CORE:
610 api = API_OPENGL_CORE;
611 break;
612 default:
613 *error = ST_CONTEXT_ERROR_BAD_API;
614 return NULL;
615 break;
616 }
617
618 pipe = smapi->screen->context_create(smapi->screen, NULL);
619 if (!pipe) {
620 *error = ST_CONTEXT_ERROR_NO_MEMORY;
621 return NULL;
622 }
623
624 st_visual_to_context_mode(&attribs->visual, &mode);
625 st = st_create_context(api, pipe, &mode, shared_ctx, &attribs->options);
626 if (!st) {
627 *error = ST_CONTEXT_ERROR_NO_MEMORY;
628 pipe->destroy(pipe);
629 return NULL;
630 }
631
632 if (attribs->flags & ST_CONTEXT_FLAG_DEBUG)
633 st->ctx->Const.ContextFlags |= GL_CONTEXT_FLAG_DEBUG_BIT;
634 if (attribs->flags & ST_CONTEXT_FLAG_FORWARD_COMPATIBLE)
635 st->ctx->Const.ContextFlags |= GL_CONTEXT_FLAG_FORWARD_COMPATIBLE_BIT;
636
637 /* need to perform version check */
638 if (attribs->major > 1 || attribs->minor > 0) {
639 /* Is the actual version less than the requested version?
640 */
641 if (st->ctx->Version < attribs->major * 10 + attribs->minor) {
642 *error = ST_CONTEXT_ERROR_BAD_VERSION;
643 st_destroy_context(st);
644 return NULL;
645 }
646 }
647
648 st->invalidate_on_gl_viewport =
649 smapi->get_param(smapi, ST_MANAGER_BROKEN_INVALIDATE);
650
651 st->iface.destroy = st_context_destroy;
652 st->iface.flush = st_context_flush;
653 st->iface.teximage = st_context_teximage;
654 st->iface.copy = st_context_copy;
655 st->iface.share = st_context_share;
656 st->iface.st_context_private = (void *) smapi;
657 st->iface.cso_context = st->cso_context;
658 st->iface.pipe = st->pipe;
659
660 *error = ST_CONTEXT_SUCCESS;
661 return &st->iface;
662 }
663
664 static struct st_context_iface *
665 st_api_get_current(struct st_api *stapi)
666 {
667 GET_CURRENT_CONTEXT(ctx);
668 struct st_context *st = (ctx) ? ctx->st : NULL;
669
670 return (st) ? &st->iface : NULL;
671 }
672
673 static struct st_framebuffer *
674 st_framebuffer_reuse_or_create(struct gl_framebuffer *fb,
675 struct st_framebuffer_iface *stfbi)
676 {
677 struct st_framebuffer *cur = st_ws_framebuffer(fb), *stfb = NULL;
678
679 /* dummy framebuffers cant be used as st_framebuffer */
680 if (cur && &cur->Base != _mesa_get_incomplete_framebuffer() &&
681 cur->iface == stfbi) {
682 /* reuse the current stfb */
683 st_framebuffer_reference(&stfb, cur);
684 }
685 else {
686 /* create a new one */
687 stfb = st_framebuffer_create(stfbi);
688 }
689
690 return stfb;
691 }
692
693 static boolean
694 st_api_make_current(struct st_api *stapi, struct st_context_iface *stctxi,
695 struct st_framebuffer_iface *stdrawi,
696 struct st_framebuffer_iface *streadi)
697 {
698 struct st_context *st = (struct st_context *) stctxi;
699 struct st_framebuffer *stdraw, *stread;
700 boolean ret;
701
702 _glapi_check_multithread();
703
704 if (st) {
705 /* reuse or create the draw fb */
706 stdraw = st_framebuffer_reuse_or_create(st->ctx->WinSysDrawBuffer,
707 stdrawi);
708 if (streadi != stdrawi) {
709 /* do the same for the read fb */
710 stread = st_framebuffer_reuse_or_create(st->ctx->WinSysReadBuffer,
711 streadi);
712 }
713 else {
714 stread = NULL;
715 /* reuse the draw fb for the read fb */
716 if (stdraw)
717 st_framebuffer_reference(&stread, stdraw);
718 }
719
720 if (stdraw && stread) {
721 st_framebuffer_validate(stdraw, st);
722 if (stread != stdraw)
723 st_framebuffer_validate(stread, st);
724
725 ret = _mesa_make_current(st->ctx, &stdraw->Base, &stread->Base);
726
727 st->draw_stamp = stdraw->stamp - 1;
728 st->read_stamp = stread->stamp - 1;
729 st_context_validate(st, stdraw, stread);
730 }
731 else {
732 struct gl_framebuffer *incomplete = _mesa_get_incomplete_framebuffer();
733 ret = _mesa_make_current(st->ctx, incomplete, incomplete);
734 }
735
736 st_framebuffer_reference(&stdraw, NULL);
737 st_framebuffer_reference(&stread, NULL);
738 }
739 else {
740 ret = _mesa_make_current(NULL, NULL, NULL);
741 }
742
743 return ret;
744 }
745
746 static st_proc_t
747 st_api_get_proc_address(struct st_api *stapi, const char *procname)
748 {
749 return (st_proc_t) _glapi_get_proc_address(procname);
750 }
751
752 static void
753 st_api_destroy(struct st_api *stapi)
754 {
755 }
756
757 /**
758 * Flush the front buffer if the current context renders to the front buffer.
759 */
760 void
761 st_manager_flush_frontbuffer(struct st_context *st)
762 {
763 struct st_framebuffer *stfb = st_ws_framebuffer(st->ctx->DrawBuffer);
764 struct st_renderbuffer *strb = NULL;
765
766 if (stfb)
767 strb = st_renderbuffer(stfb->Base.Attachment[BUFFER_FRONT_LEFT].Renderbuffer);
768 if (!strb)
769 return;
770
771 /* never a dummy fb */
772 assert(&stfb->Base != _mesa_get_incomplete_framebuffer());
773 stfb->iface->flush_front(&st->iface, stfb->iface, ST_ATTACHMENT_FRONT_LEFT);
774 }
775
776 /**
777 * Return the surface of an EGLImage.
778 * FIXME: I think this should operate on resources, not surfaces
779 */
780 struct pipe_surface *
781 st_manager_get_egl_image_surface(struct st_context *st, void *eglimg)
782 {
783 struct st_manager *smapi =
784 (struct st_manager *) st->iface.st_context_private;
785 struct st_egl_image stimg;
786 struct pipe_surface *ps, surf_tmpl;
787
788 if (!smapi || !smapi->get_egl_image)
789 return NULL;
790
791 memset(&stimg, 0, sizeof(stimg));
792 if (!smapi->get_egl_image(smapi, eglimg, &stimg))
793 return NULL;
794
795 u_surface_default_template(&surf_tmpl, stimg.texture);
796 surf_tmpl.u.tex.level = stimg.level;
797 surf_tmpl.u.tex.first_layer = stimg.layer;
798 surf_tmpl.u.tex.last_layer = stimg.layer;
799 ps = st->pipe->create_surface(st->pipe, stimg.texture, &surf_tmpl);
800 pipe_resource_reference(&stimg.texture, NULL);
801
802 return ps;
803 }
804
805 /**
806 * Re-validate the framebuffers.
807 */
808 void
809 st_manager_validate_framebuffers(struct st_context *st)
810 {
811 struct st_framebuffer *stdraw = st_ws_framebuffer(st->ctx->DrawBuffer);
812 struct st_framebuffer *stread = st_ws_framebuffer(st->ctx->ReadBuffer);
813
814 if (stdraw)
815 st_framebuffer_validate(stdraw, st);
816 if (stread && stread != stdraw)
817 st_framebuffer_validate(stread, st);
818
819 st_context_validate(st, stdraw, stread);
820 }
821
822 /**
823 * Add a color renderbuffer on demand.
824 */
825 boolean
826 st_manager_add_color_renderbuffer(struct st_context *st,
827 struct gl_framebuffer *fb,
828 gl_buffer_index idx)
829 {
830 struct st_framebuffer *stfb = st_ws_framebuffer(fb);
831
832 /* FBO */
833 if (!stfb)
834 return FALSE;
835
836 if (stfb->Base.Attachment[idx].Renderbuffer)
837 return TRUE;
838
839 switch (idx) {
840 case BUFFER_FRONT_LEFT:
841 case BUFFER_BACK_LEFT:
842 case BUFFER_FRONT_RIGHT:
843 case BUFFER_BACK_RIGHT:
844 break;
845 default:
846 return FALSE;
847 break;
848 }
849
850 if (!st_framebuffer_add_renderbuffer(stfb, idx))
851 return FALSE;
852
853 st_framebuffer_update_attachments(stfb);
854
855 /*
856 * Force a call to the state tracker manager to validate the
857 * new renderbuffer. It might be that there is a window system
858 * renderbuffer available.
859 */
860 if(stfb->iface)
861 stfb->iface_stamp = p_atomic_read(&stfb->iface->stamp) - 1;
862
863 st_invalidate_state(st->ctx, _NEW_BUFFERS);
864
865 return TRUE;
866 }
867
868 static const struct st_api st_gl_api = {
869 "Mesa " PACKAGE_VERSION,
870 ST_API_OPENGL,
871 ST_PROFILE_DEFAULT_MASK |
872 ST_PROFILE_OPENGL_CORE_MASK |
873 ST_PROFILE_OPENGL_ES1_MASK |
874 ST_PROFILE_OPENGL_ES2_MASK |
875 0,
876 ST_API_FEATURE_MS_VISUALS_MASK,
877 st_api_destroy,
878 st_api_get_proc_address,
879 st_api_create_context,
880 st_api_make_current,
881 st_api_get_current,
882 };
883
884 struct st_api *
885 st_gl_api_create(void)
886 {
887 return (struct st_api *) &st_gl_api;
888 }