Merge remote branch 'origin/master' into pipe-video
[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 stfb = CALLOC_STRUCT(st_framebuffer);
430 if (!stfb)
431 return NULL;
432
433 /* for FBO-only context */
434 if (!stfbi) {
435 struct gl_framebuffer *base = _mesa_get_incomplete_framebuffer();
436
437 stfb->Base = *base;
438
439 return stfb;
440 }
441
442 st_visual_to_context_mode(stfbi->visual, &mode);
443 _mesa_initialize_window_framebuffer(&stfb->Base, &mode);
444
445 /* modify the draw/read buffers of the fb */
446 st_visual_to_default_buffer(stfbi->visual, &stfb->Base.ColorDrawBuffer[0],
447 &stfb->Base._ColorDrawBufferIndexes[0]);
448 st_visual_to_default_buffer(stfbi->visual, &stfb->Base.ColorReadBuffer,
449 &stfb->Base._ColorReadBufferIndex);
450
451 stfb->iface = stfbi;
452
453 /* add the color buffer */
454 idx = stfb->Base._ColorDrawBufferIndexes[0];
455 if (!st_framebuffer_add_renderbuffer(stfb, idx)) {
456 FREE(stfb);
457 return NULL;
458 }
459
460 st_framebuffer_add_renderbuffer(stfb, BUFFER_DEPTH);
461 st_framebuffer_add_renderbuffer(stfb, BUFFER_ACCUM);
462
463 st_framebuffer_update_attachments(stfb);
464
465 stfb->Base.Initialized = GL_TRUE;
466
467 return stfb;
468 }
469
470 /**
471 * Reference a framebuffer.
472 */
473 static void
474 st_framebuffer_reference(struct st_framebuffer **ptr,
475 struct st_framebuffer *stfb)
476 {
477 struct gl_framebuffer *fb = &stfb->Base;
478 _mesa_reference_framebuffer((struct gl_framebuffer **) ptr, fb);
479 }
480
481 static void
482 st_context_notify_invalid_framebuffer(struct st_context_iface *stctxi,
483 struct st_framebuffer_iface *stfbi)
484 {
485 struct st_context *st = (struct st_context *) stctxi;
486 struct st_framebuffer *stfb;
487
488 /* either draw or read winsys fb */
489 stfb = st_ws_framebuffer(st->ctx->WinSysDrawBuffer);
490 if (!stfb || stfb->iface != stfbi)
491 stfb = st_ws_framebuffer(st->ctx->WinSysReadBuffer);
492
493 if (stfb && stfb->iface == stfbi) {
494 p_atomic_set(&stfb->revalidate, TRUE);
495 }
496 else {
497 /* This function is probably getting called when we've detected a
498 * change in a window's size but the currently bound context is
499 * not bound to that window.
500 * If the st_framebuffer_iface structure had a pointer to the
501 * corresponding st_framebuffer we'd be able to handle this.
502 */
503 }
504 }
505
506 static void
507 st_context_flush(struct st_context_iface *stctxi, unsigned flags,
508 struct pipe_fence_handle **fence)
509 {
510 struct st_context *st = (struct st_context *) stctxi;
511 st_flush(st, flags, fence);
512 if (flags & PIPE_FLUSH_RENDER_CACHE)
513 st_manager_flush_frontbuffer(st);
514 }
515
516 static boolean
517 st_context_teximage(struct st_context_iface *stctxi,
518 enum st_texture_type target,
519 int level, enum pipe_format internal_format,
520 struct pipe_resource *tex, boolean mipmap)
521 {
522 struct st_context *st = (struct st_context *) stctxi;
523 struct gl_context *ctx = st->ctx;
524 struct gl_texture_unit *texUnit = _mesa_get_current_tex_unit(ctx);
525 struct gl_texture_object *texObj;
526 struct gl_texture_image *texImage;
527 struct st_texture_object *stObj;
528 struct st_texture_image *stImage;
529 GLenum internalFormat;
530 GLuint width, height, depth;
531
532 switch (target) {
533 case ST_TEXTURE_1D:
534 target = GL_TEXTURE_1D;
535 break;
536 case ST_TEXTURE_2D:
537 target = GL_TEXTURE_2D;
538 break;
539 case ST_TEXTURE_3D:
540 target = GL_TEXTURE_3D;
541 break;
542 case ST_TEXTURE_RECT:
543 target = GL_TEXTURE_RECTANGLE_ARB;
544 break;
545 default:
546 return FALSE;
547 break;
548 }
549
550 texObj = _mesa_select_tex_object(ctx, texUnit, target);
551 _mesa_lock_texture(ctx, texObj);
552
553 stObj = st_texture_object(texObj);
554 /* switch to surface based */
555 if (!stObj->surface_based) {
556 _mesa_clear_texture_object(ctx, texObj);
557 stObj->surface_based = GL_TRUE;
558 }
559
560 texImage = _mesa_get_tex_image(ctx, texObj, target, level);
561 stImage = st_texture_image(texImage);
562 if (tex) {
563 gl_format texFormat;
564
565 /*
566 * XXX When internal_format and tex->format differ, st_finalize_texture
567 * needs to allocate a new texture with internal_format and copy the
568 * texture here into the new one. It will result in surface_copy being
569 * called on surfaces whose formats differ.
570 *
571 * To avoid that, internal_format is (wrongly) ignored here. A sane fix
572 * is to use a sampler view.
573 */
574 if (!st_sampler_compat_formats(tex->format, internal_format))
575 internal_format = tex->format;
576
577 if (util_format_get_component_bits(internal_format,
578 UTIL_FORMAT_COLORSPACE_RGB, 3) > 0)
579 internalFormat = GL_RGBA;
580 else
581 internalFormat = GL_RGB;
582
583 texFormat = st_ChooseTextureFormat(ctx, internalFormat,
584 GL_RGBA, GL_UNSIGNED_BYTE);
585
586 _mesa_init_teximage_fields(ctx, target, texImage,
587 tex->width0, tex->height0, 1, 0,
588 internalFormat, texFormat);
589
590 width = tex->width0;
591 height = tex->height0;
592 depth = tex->depth0;
593
594 /* grow the image size until we hit level = 0 */
595 while (level > 0) {
596 if (width != 1)
597 width <<= 1;
598 if (height != 1)
599 height <<= 1;
600 if (depth != 1)
601 depth <<= 1;
602 level--;
603 }
604 }
605 else {
606 _mesa_clear_texture_image(ctx, texImage);
607 width = height = depth = 0;
608 }
609
610 pipe_resource_reference(&stImage->pt, tex);
611 stObj->width0 = width;
612 stObj->height0 = height;
613 stObj->depth0 = depth;
614
615 _mesa_dirty_texobj(ctx, texObj, GL_TRUE);
616 _mesa_unlock_texture(ctx, texObj);
617
618 return TRUE;
619 }
620
621 static void
622 st_context_copy(struct st_context_iface *stctxi,
623 struct st_context_iface *stsrci, unsigned mask)
624 {
625 struct st_context *st = (struct st_context *) stctxi;
626 struct st_context *src = (struct st_context *) stsrci;
627
628 _mesa_copy_context(src->ctx, st->ctx, mask);
629 }
630
631 static boolean
632 st_context_share(struct st_context_iface *stctxi,
633 struct st_context_iface *stsrci)
634 {
635 struct st_context *st = (struct st_context *) stctxi;
636 struct st_context *src = (struct st_context *) stsrci;
637
638 return _mesa_share_state(st->ctx, src->ctx);
639 }
640
641 static void
642 st_context_destroy(struct st_context_iface *stctxi)
643 {
644 struct st_context *st = (struct st_context *) stctxi;
645 st_destroy_context(st);
646 }
647
648 static struct st_context_iface *
649 st_api_create_context(struct st_api *stapi, struct st_manager *smapi,
650 const struct st_context_attribs *attribs,
651 struct st_context_iface *shared_stctxi)
652 {
653 struct st_context *shared_ctx = (struct st_context *) shared_stctxi;
654 struct st_context *st;
655 struct pipe_context *pipe;
656 struct gl_config mode;
657 gl_api api;
658
659 if (!(stapi->profile_mask & (1 << attribs->profile)))
660 return NULL;
661
662 switch (attribs->profile) {
663 case ST_PROFILE_DEFAULT:
664 api = API_OPENGL;
665 break;
666 case ST_PROFILE_OPENGL_ES1:
667 api = API_OPENGLES;
668 break;
669 case ST_PROFILE_OPENGL_ES2:
670 api = API_OPENGLES2;
671 break;
672 case ST_PROFILE_OPENGL_CORE:
673 default:
674 return NULL;
675 break;
676 }
677
678 pipe = smapi->screen->context_create(smapi->screen, NULL);
679 if (!pipe)
680 return NULL;
681
682 st_visual_to_context_mode(&attribs->visual, &mode);
683 st = st_create_context(api, pipe, &mode, shared_ctx);
684 if (!st) {
685 pipe->destroy(pipe);
686 return NULL;
687 }
688
689 /* need to perform version check */
690 if (attribs->major > 1 || attribs->minor > 0) {
691 _mesa_compute_version(st->ctx);
692
693 if (st->ctx->VersionMajor < attribs->major ||
694 st->ctx->VersionMajor < attribs->minor) {
695 st_destroy_context(st);
696 return NULL;
697 }
698 }
699
700 st->invalidate_on_gl_viewport =
701 smapi->get_param(smapi, ST_MANAGER_BROKEN_INVALIDATE);
702
703 st->iface.destroy = st_context_destroy;
704 st->iface.notify_invalid_framebuffer =
705 st_context_notify_invalid_framebuffer;
706 st->iface.flush = st_context_flush;
707 st->iface.teximage = st_context_teximage;
708 st->iface.copy = st_context_copy;
709 st->iface.share = st_context_share;
710 st->iface.st_context_private = (void *) smapi;
711
712 return &st->iface;
713 }
714
715 static boolean
716 st_api_make_current(struct st_api *stapi, struct st_context_iface *stctxi,
717 struct st_framebuffer_iface *stdrawi,
718 struct st_framebuffer_iface *streadi)
719 {
720 struct st_context *st = (struct st_context *) stctxi;
721 struct st_framebuffer *stdraw, *stread, *stfb;
722 boolean ret;
723
724 _glapi_check_multithread();
725
726 if (st) {
727 /* reuse/create the draw fb */
728 stfb = st_ws_framebuffer(st->ctx->WinSysDrawBuffer);
729 if (stfb && stfb->iface == stdrawi) {
730 stdraw = NULL;
731 st_framebuffer_reference(&stdraw, stfb);
732 }
733 else {
734 stdraw = st_framebuffer_create(stdrawi);
735 }
736
737 /* reuse/create the read fb */
738 stfb = st_ws_framebuffer(st->ctx->WinSysReadBuffer);
739 if (!stfb || stfb->iface != streadi)
740 stfb = stdraw;
741 if (stfb && stfb->iface == streadi) {
742 stread = NULL;
743 st_framebuffer_reference(&stread, stfb);
744 }
745 else {
746 stread = st_framebuffer_create(streadi);
747 }
748
749 if (stdraw && stread) {
750 st_framebuffer_validate(stdraw, st);
751 if (stread != stdraw)
752 st_framebuffer_validate(stread, st);
753
754 /* modify the draw/read buffers of the context */
755 if (stdraw->iface) {
756 st_visual_to_default_buffer(stdraw->iface->visual,
757 &st->ctx->Color.DrawBuffer[0], NULL);
758 }
759 if (stread->iface) {
760 st_visual_to_default_buffer(stread->iface->visual,
761 &st->ctx->Pixel.ReadBuffer, NULL);
762 }
763
764 ret = _mesa_make_current(st->ctx, &stdraw->Base, &stread->Base);
765 }
766 else {
767 ret = FALSE;
768 }
769
770 st_framebuffer_reference(&stdraw, NULL);
771 st_framebuffer_reference(&stread, NULL);
772 }
773 else {
774 ret = _mesa_make_current(NULL, NULL, NULL);
775 }
776
777 return ret;
778 }
779
780 static struct st_context_iface *
781 st_api_get_current(struct st_api *stapi)
782 {
783 GET_CURRENT_CONTEXT(ctx);
784 struct st_context *st = (ctx) ? ctx->st : NULL;
785
786 return (st) ? &st->iface : NULL;
787 }
788
789 static st_proc_t
790 st_api_get_proc_address(struct st_api *stapi, const char *procname)
791 {
792 return (st_proc_t) _glapi_get_proc_address(procname);
793 }
794
795 static void
796 st_api_destroy(struct st_api *stapi)
797 {
798 }
799
800 /**
801 * Flush the front buffer if the current context renders to the front buffer.
802 */
803 void
804 st_manager_flush_frontbuffer(struct st_context *st)
805 {
806 struct st_framebuffer *stfb = st_ws_framebuffer(st->ctx->DrawBuffer);
807 struct st_renderbuffer *strb = NULL;
808
809 if (stfb)
810 strb = st_renderbuffer(stfb->Base.Attachment[BUFFER_FRONT_LEFT].Renderbuffer);
811 if (!strb)
812 return;
813
814 /* never a dummy fb */
815 assert(stfb->iface);
816 stfb->iface->flush_front(stfb->iface, ST_ATTACHMENT_FRONT_LEFT);
817 }
818
819 /**
820 * Return the surface of an EGLImage.
821 * FIXME: I think this should operate on resources, not surfaces
822 */
823 struct pipe_surface *
824 st_manager_get_egl_image_surface(struct st_context *st,
825 void *eglimg, unsigned usage)
826 {
827 struct st_manager *smapi =
828 (struct st_manager *) st->iface.st_context_private;
829 struct st_egl_image stimg;
830 struct pipe_surface *ps, surf_tmpl;
831
832 if (!smapi || !smapi->get_egl_image)
833 return NULL;
834
835 memset(&stimg, 0, sizeof(stimg));
836 if (!smapi->get_egl_image(smapi, eglimg, &stimg))
837 return NULL;
838
839 memset(&surf_tmpl, 0, sizeof(surf_tmpl));
840 surf_tmpl.format = stimg.texture->format;
841 surf_tmpl.usage = usage;
842 surf_tmpl.u.tex.level = stimg.level;
843 surf_tmpl.u.tex.first_layer = stimg.layer;
844 surf_tmpl.u.tex.last_layer = stimg.layer;
845 ps = st->pipe->create_surface(st->pipe, stimg.texture, &surf_tmpl);
846 pipe_resource_reference(&stimg.texture, NULL);
847
848 return ps;
849 }
850
851 /**
852 * Re-validate the framebuffers.
853 */
854 void
855 st_manager_validate_framebuffers(struct st_context *st)
856 {
857 struct st_framebuffer *stdraw = st_ws_framebuffer(st->ctx->DrawBuffer);
858 struct st_framebuffer *stread = st_ws_framebuffer(st->ctx->ReadBuffer);
859
860 if (stdraw)
861 st_framebuffer_validate(stdraw, st);
862 if (stread && stread != stdraw)
863 st_framebuffer_validate(stread, st);
864 }
865
866 /**
867 * Add a color renderbuffer on demand.
868 */
869 boolean
870 st_manager_add_color_renderbuffer(struct st_context *st,
871 struct gl_framebuffer *fb,
872 gl_buffer_index idx)
873 {
874 struct st_framebuffer *stfb = st_ws_framebuffer(fb);
875
876 /* FBO */
877 if (!stfb)
878 return FALSE;
879
880 if (stfb->Base.Attachment[idx].Renderbuffer)
881 return TRUE;
882
883 switch (idx) {
884 case BUFFER_FRONT_LEFT:
885 case BUFFER_BACK_LEFT:
886 case BUFFER_FRONT_RIGHT:
887 case BUFFER_BACK_RIGHT:
888 break;
889 default:
890 return FALSE;
891 break;
892 }
893
894 if (!st_framebuffer_add_renderbuffer(stfb, idx))
895 return FALSE;
896
897 st_framebuffer_update_attachments(stfb);
898 st_invalidate_state(st->ctx, _NEW_BUFFERS);
899
900 return TRUE;
901 }
902
903 static const struct st_api st_gl_api = {
904 "Mesa " MESA_VERSION_STRING,
905 ST_API_OPENGL,
906 #if FEATURE_GL
907 ST_PROFILE_DEFAULT_MASK |
908 #endif
909 #if FEATURE_ES1
910 ST_PROFILE_OPENGL_ES1_MASK |
911 #endif
912 #if FEATURE_ES2
913 ST_PROFILE_OPENGL_ES2_MASK |
914 #endif
915 0,
916 st_api_destroy,
917 st_api_get_proc_address,
918 st_api_create_context,
919 st_api_make_current,
920 st_api_get_current,
921 };
922
923 struct st_api *
924 st_gl_api_create(void)
925 {
926 return (struct st_api *) &st_gl_api;
927 }