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/mfeatures.h"
32 #include "main/texobj.h"
33 #include "main/teximage.h"
34 #include "main/texstate.h"
35 #include "main/framebuffer.h"
36 #include "main/fbobject.h"
37 #include "main/renderbuffer.h"
38 #include "main/version.h"
39 #include "st_texture.h"
40
41 #include "st_context.h"
42 #include "st_format.h"
43 #include "st_cb_fbo.h"
44 #include "st_cb_flush.h"
45 #include "st_manager.h"
46
47 #include "state_tracker/st_gl_api.h"
48
49 #include "pipe/p_context.h"
50 #include "pipe/p_screen.h"
51 #include "util/u_format.h"
52 #include "util/u_pointer.h"
53 #include "util/u_inlines.h"
54 #include "util/u_atomic.h"
55 #include "util/u_surface.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 *
st_ws_framebuffer(struct gl_framebuffer * fb)64 st_ws_framebuffer(struct gl_framebuffer *fb)
65 {
66 /* FBO cannot be casted. See st_new_framebuffer */
67 if (fb && _mesa_is_winsys_fbo(fb))
68 return (struct st_framebuffer *) fb;
69 return NULL;
70 }
71
72 /**
73 * Map an attachment to a buffer index.
74 */
75 static INLINE gl_buffer_index
attachment_to_buffer_index(enum st_attachment_type statt)76 attachment_to_buffer_index(enum st_attachment_type statt)
77 {
78 gl_buffer_index index;
79
80 switch (statt) {
81 case ST_ATTACHMENT_FRONT_LEFT:
82 index = BUFFER_FRONT_LEFT;
83 break;
84 case ST_ATTACHMENT_BACK_LEFT:
85 index = BUFFER_BACK_LEFT;
86 break;
87 case ST_ATTACHMENT_FRONT_RIGHT:
88 index = BUFFER_FRONT_RIGHT;
89 break;
90 case ST_ATTACHMENT_BACK_RIGHT:
91 index = BUFFER_BACK_RIGHT;
92 break;
93 case ST_ATTACHMENT_DEPTH_STENCIL:
94 index = BUFFER_DEPTH;
95 break;
96 case ST_ATTACHMENT_ACCUM:
97 index = BUFFER_ACCUM;
98 break;
99 case ST_ATTACHMENT_SAMPLE:
100 default:
101 index = BUFFER_COUNT;
102 break;
103 }
104
105 return index;
106 }
107
108 /**
109 * Map a buffer index to an attachment.
110 */
111 static INLINE enum st_attachment_type
buffer_index_to_attachment(gl_buffer_index index)112 buffer_index_to_attachment(gl_buffer_index index)
113 {
114 enum st_attachment_type statt;
115
116 switch (index) {
117 case BUFFER_FRONT_LEFT:
118 statt = ST_ATTACHMENT_FRONT_LEFT;
119 break;
120 case BUFFER_BACK_LEFT:
121 statt = ST_ATTACHMENT_BACK_LEFT;
122 break;
123 case BUFFER_FRONT_RIGHT:
124 statt = ST_ATTACHMENT_FRONT_RIGHT;
125 break;
126 case BUFFER_BACK_RIGHT:
127 statt = ST_ATTACHMENT_BACK_RIGHT;
128 break;
129 case BUFFER_DEPTH:
130 statt = ST_ATTACHMENT_DEPTH_STENCIL;
131 break;
132 case BUFFER_ACCUM:
133 statt = ST_ATTACHMENT_ACCUM;
134 break;
135 default:
136 statt = ST_ATTACHMENT_INVALID;
137 break;
138 }
139
140 return statt;
141 }
142
143 /**
144 * Make sure a context picks up the latest cached state of the
145 * drawables it binds to.
146 */
147 static void
st_context_validate(struct st_context * st,struct st_framebuffer * stdraw,struct st_framebuffer * stread)148 st_context_validate(struct st_context *st,
149 struct st_framebuffer *stdraw,
150 struct st_framebuffer *stread)
151 {
152 if (stdraw && stdraw->stamp != st->draw_stamp) {
153 st->dirty.st |= ST_NEW_FRAMEBUFFER;
154 _mesa_resize_framebuffer(st->ctx, &stdraw->Base,
155 stdraw->Base.Width,
156 stdraw->Base.Height);
157 st->draw_stamp = stdraw->stamp;
158 }
159
160 if (stread && stread->stamp != st->read_stamp) {
161 if (stread != stdraw) {
162 st->dirty.st |= ST_NEW_FRAMEBUFFER;
163 _mesa_resize_framebuffer(st->ctx, &stread->Base,
164 stread->Base.Width,
165 stread->Base.Height);
166 }
167 st->read_stamp = stread->stamp;
168 }
169 }
170
171 /**
172 * Validate a framebuffer to make sure up-to-date pipe_textures are used.
173 * The context we need to pass in is s dummy context needed only to be
174 * able to get a pipe context to create pipe surfaces, and to have a
175 * context to call _mesa_resize_framebuffer():
176 * (That should probably be rethought, since those surfaces become
177 * drawable state, not context state, and can be freed by another pipe
178 * context).
179 */
180 static void
st_framebuffer_validate(struct st_framebuffer * stfb,struct st_context * st)181 st_framebuffer_validate(struct st_framebuffer *stfb,
182 struct st_context *st)
183 {
184 struct pipe_resource *textures[ST_ATTACHMENT_COUNT];
185 uint width, height;
186 unsigned i;
187 boolean changed = FALSE;
188 int32_t new_stamp = p_atomic_read(&stfb->iface->stamp);
189
190 if (stfb->iface_stamp == new_stamp)
191 return;
192
193 /* validate the fb */
194 do {
195 if (!stfb->iface->validate(stfb->iface, stfb->statts,
196 stfb->num_statts, textures))
197 return;
198
199 stfb->iface_stamp = new_stamp;
200 new_stamp = p_atomic_read(&stfb->iface->stamp);
201 } while(stfb->iface_stamp != new_stamp);
202
203 width = stfb->Base.Width;
204 height = stfb->Base.Height;
205
206 for (i = 0; i < stfb->num_statts; i++) {
207 struct st_renderbuffer *strb;
208 struct pipe_surface *ps, surf_tmpl;
209 gl_buffer_index idx;
210
211 if (!textures[i])
212 continue;
213
214 idx = attachment_to_buffer_index(stfb->statts[i]);
215 if (idx >= BUFFER_COUNT) {
216 pipe_resource_reference(&textures[i], NULL);
217 continue;
218 }
219
220 strb = st_renderbuffer(stfb->Base.Attachment[idx].Renderbuffer);
221 assert(strb);
222 if (strb->texture == textures[i]) {
223 pipe_resource_reference(&textures[i], NULL);
224 continue;
225 }
226
227 u_surface_default_template(&surf_tmpl, textures[i],
228 PIPE_BIND_RENDER_TARGET);
229 ps = st->pipe->create_surface(st->pipe, textures[i], &surf_tmpl);
230 if (ps) {
231 pipe_surface_reference(&strb->surface, ps);
232 pipe_resource_reference(&strb->texture, ps->texture);
233 /* ownership transfered */
234 pipe_surface_reference(&ps, NULL);
235
236 changed = TRUE;
237
238 strb->Base.Width = strb->surface->width;
239 strb->Base.Height = strb->surface->height;
240
241 width = strb->Base.Width;
242 height = strb->Base.Height;
243 }
244
245 pipe_resource_reference(&textures[i], NULL);
246 }
247
248 if (changed) {
249 ++stfb->stamp;
250 _mesa_resize_framebuffer(st->ctx, &stfb->Base, width, height);
251 }
252 }
253
254 /**
255 * Update the attachments to validate by looping the existing renderbuffers.
256 */
257 static void
st_framebuffer_update_attachments(struct st_framebuffer * stfb)258 st_framebuffer_update_attachments(struct st_framebuffer *stfb)
259 {
260 gl_buffer_index idx;
261
262 stfb->num_statts = 0;
263 for (idx = 0; idx < BUFFER_COUNT; idx++) {
264 struct st_renderbuffer *strb;
265 enum st_attachment_type statt;
266
267 strb = st_renderbuffer(stfb->Base.Attachment[idx].Renderbuffer);
268 if (!strb || strb->software)
269 continue;
270
271 statt = buffer_index_to_attachment(idx);
272 if (statt != ST_ATTACHMENT_INVALID &&
273 st_visual_have_buffers(stfb->iface->visual, 1 << statt))
274 stfb->statts[stfb->num_statts++] = statt;
275 }
276 stfb->stamp++;
277 }
278
279 /**
280 * Add a renderbuffer to the framebuffer.
281 */
282 static boolean
st_framebuffer_add_renderbuffer(struct st_framebuffer * stfb,gl_buffer_index idx)283 st_framebuffer_add_renderbuffer(struct st_framebuffer *stfb,
284 gl_buffer_index idx)
285 {
286 struct gl_renderbuffer *rb;
287 enum pipe_format format;
288 int samples;
289 boolean sw;
290
291 if (!stfb->iface)
292 return FALSE;
293
294 /* do not distinguish depth/stencil buffers */
295 if (idx == BUFFER_STENCIL)
296 idx = BUFFER_DEPTH;
297
298 switch (idx) {
299 case BUFFER_DEPTH:
300 format = stfb->iface->visual->depth_stencil_format;
301 sw = FALSE;
302 break;
303 case BUFFER_ACCUM:
304 format = stfb->iface->visual->accum_format;
305 sw = TRUE;
306 break;
307 default:
308 format = stfb->iface->visual->color_format;
309 sw = FALSE;
310 break;
311 }
312
313 if (format == PIPE_FORMAT_NONE)
314 return FALSE;
315
316 samples = stfb->iface->visual->samples;
317 if (!samples)
318 samples = st_get_msaa();
319
320 rb = st_new_renderbuffer_fb(format, samples, sw);
321 if (!rb)
322 return FALSE;
323
324 if (idx != BUFFER_DEPTH) {
325 _mesa_add_renderbuffer(&stfb->Base, idx, rb);
326 }
327 else {
328 if (util_format_get_component_bits(format, UTIL_FORMAT_COLORSPACE_ZS, 0))
329 _mesa_add_renderbuffer(&stfb->Base, BUFFER_DEPTH, rb);
330 if (util_format_get_component_bits(format, UTIL_FORMAT_COLORSPACE_ZS, 1))
331 _mesa_add_renderbuffer(&stfb->Base, BUFFER_STENCIL, rb);
332 }
333
334 return TRUE;
335 }
336
337 /**
338 * Intialize a struct gl_config from a visual.
339 */
340 static void
st_visual_to_context_mode(const struct st_visual * visual,struct gl_config * mode)341 st_visual_to_context_mode(const struct st_visual *visual,
342 struct gl_config *mode)
343 {
344 memset(mode, 0, sizeof(*mode));
345
346 if (st_visual_have_buffers(visual, ST_ATTACHMENT_BACK_LEFT_MASK))
347 mode->doubleBufferMode = GL_TRUE;
348 if (st_visual_have_buffers(visual,
349 ST_ATTACHMENT_FRONT_RIGHT_MASK | ST_ATTACHMENT_BACK_RIGHT_MASK))
350 mode->stereoMode = GL_TRUE;
351
352 if (visual->color_format != PIPE_FORMAT_NONE) {
353 mode->rgbMode = GL_TRUE;
354
355 mode->redBits =
356 util_format_get_component_bits(visual->color_format,
357 UTIL_FORMAT_COLORSPACE_RGB, 0);
358 mode->greenBits =
359 util_format_get_component_bits(visual->color_format,
360 UTIL_FORMAT_COLORSPACE_RGB, 1);
361 mode->blueBits =
362 util_format_get_component_bits(visual->color_format,
363 UTIL_FORMAT_COLORSPACE_RGB, 2);
364 mode->alphaBits =
365 util_format_get_component_bits(visual->color_format,
366 UTIL_FORMAT_COLORSPACE_RGB, 3);
367
368 mode->rgbBits = mode->redBits +
369 mode->greenBits + mode->blueBits + mode->alphaBits;
370 }
371
372 if (visual->depth_stencil_format != PIPE_FORMAT_NONE) {
373 mode->depthBits =
374 util_format_get_component_bits(visual->depth_stencil_format,
375 UTIL_FORMAT_COLORSPACE_ZS, 0);
376 mode->stencilBits =
377 util_format_get_component_bits(visual->depth_stencil_format,
378 UTIL_FORMAT_COLORSPACE_ZS, 1);
379
380 mode->haveDepthBuffer = mode->depthBits > 0;
381 mode->haveStencilBuffer = mode->stencilBits > 0;
382 }
383
384 if (visual->accum_format != PIPE_FORMAT_NONE) {
385 mode->haveAccumBuffer = GL_TRUE;
386
387 mode->accumRedBits =
388 util_format_get_component_bits(visual->accum_format,
389 UTIL_FORMAT_COLORSPACE_RGB, 0);
390 mode->accumGreenBits =
391 util_format_get_component_bits(visual->accum_format,
392 UTIL_FORMAT_COLORSPACE_RGB, 1);
393 mode->accumBlueBits =
394 util_format_get_component_bits(visual->accum_format,
395 UTIL_FORMAT_COLORSPACE_RGB, 2);
396 mode->accumAlphaBits =
397 util_format_get_component_bits(visual->accum_format,
398 UTIL_FORMAT_COLORSPACE_RGB, 3);
399 }
400
401 if (visual->samples) {
402 mode->sampleBuffers = 1;
403 mode->samples = visual->samples;
404 }
405 }
406
407 /**
408 * Create a framebuffer from a manager interface.
409 */
410 static struct st_framebuffer *
st_framebuffer_create(struct st_framebuffer_iface * stfbi)411 st_framebuffer_create(struct st_framebuffer_iface *stfbi)
412 {
413 struct st_framebuffer *stfb;
414 struct gl_config mode;
415 gl_buffer_index idx;
416
417 if (!stfbi)
418 return NULL;
419
420 stfb = CALLOC_STRUCT(st_framebuffer);
421 if (!stfb)
422 return NULL;
423
424 st_visual_to_context_mode(stfbi->visual, &mode);
425 _mesa_initialize_window_framebuffer(&stfb->Base, &mode);
426
427 stfb->iface = stfbi;
428 stfb->iface_stamp = p_atomic_read(&stfbi->stamp) - 1;
429
430 /* add the color buffer */
431 idx = stfb->Base._ColorDrawBufferIndexes[0];
432 if (!st_framebuffer_add_renderbuffer(stfb, idx)) {
433 FREE(stfb);
434 return NULL;
435 }
436
437 st_framebuffer_add_renderbuffer(stfb, BUFFER_DEPTH);
438 st_framebuffer_add_renderbuffer(stfb, BUFFER_ACCUM);
439
440 stfb->stamp = 0;
441 st_framebuffer_update_attachments(stfb);
442
443 stfb->Base.Initialized = GL_TRUE;
444
445 return stfb;
446 }
447
448 /**
449 * Reference a framebuffer.
450 */
451 static void
st_framebuffer_reference(struct st_framebuffer ** ptr,struct st_framebuffer * stfb)452 st_framebuffer_reference(struct st_framebuffer **ptr,
453 struct st_framebuffer *stfb)
454 {
455 struct gl_framebuffer *fb = &stfb->Base;
456 _mesa_reference_framebuffer((struct gl_framebuffer **) ptr, fb);
457 }
458
459 static void
st_context_flush(struct st_context_iface * stctxi,unsigned flags,struct pipe_fence_handle ** fence)460 st_context_flush(struct st_context_iface *stctxi, unsigned flags,
461 struct pipe_fence_handle **fence)
462 {
463 struct st_context *st = (struct st_context *) stctxi;
464 st_flush(st, fence);
465 if (flags & ST_FLUSH_FRONT)
466 st_manager_flush_frontbuffer(st);
467 }
468
469 static boolean
st_context_teximage(struct st_context_iface * stctxi,enum st_texture_type tex_type,int level,enum pipe_format internal_format,struct pipe_resource * tex,boolean mipmap)470 st_context_teximage(struct st_context_iface *stctxi,
471 enum st_texture_type tex_type,
472 int level, enum pipe_format internal_format,
473 struct pipe_resource *tex, boolean mipmap)
474 {
475 struct st_context *st = (struct st_context *) stctxi;
476 struct gl_context *ctx = st->ctx;
477 struct gl_texture_unit *texUnit = _mesa_get_current_tex_unit(ctx);
478 struct gl_texture_object *texObj;
479 struct gl_texture_image *texImage;
480 struct st_texture_object *stObj;
481 struct st_texture_image *stImage;
482 GLenum internalFormat;
483 GLuint width, height, depth;
484 GLenum target;
485
486 switch (tex_type) {
487 case ST_TEXTURE_1D:
488 target = GL_TEXTURE_1D;
489 break;
490 case ST_TEXTURE_2D:
491 target = GL_TEXTURE_2D;
492 break;
493 case ST_TEXTURE_3D:
494 target = GL_TEXTURE_3D;
495 break;
496 case ST_TEXTURE_RECT:
497 target = GL_TEXTURE_RECTANGLE_ARB;
498 break;
499 default:
500 return FALSE;
501 }
502
503 texObj = _mesa_select_tex_object(ctx, texUnit, target);
504 _mesa_lock_texture(ctx, texObj);
505
506 stObj = st_texture_object(texObj);
507 /* switch to surface based */
508 if (!stObj->surface_based) {
509 _mesa_clear_texture_object(ctx, texObj);
510 stObj->surface_based = GL_TRUE;
511 }
512
513 texImage = _mesa_get_tex_image(ctx, texObj, target, level);
514 stImage = st_texture_image(texImage);
515 if (tex) {
516 gl_format texFormat;
517
518 /*
519 * XXX When internal_format and tex->format differ, st_finalize_texture
520 * needs to allocate a new texture with internal_format and copy the
521 * texture here into the new one. It will result in surface_copy being
522 * called on surfaces whose formats differ.
523 *
524 * To avoid that, internal_format is (wrongly) ignored here. A sane fix
525 * is to use a sampler view.
526 */
527 if (!st_sampler_compat_formats(tex->format, internal_format))
528 internal_format = tex->format;
529
530 if (util_format_get_component_bits(internal_format,
531 UTIL_FORMAT_COLORSPACE_RGB, 3) > 0)
532 internalFormat = GL_RGBA;
533 else
534 internalFormat = GL_RGB;
535
536 texFormat = st_ChooseTextureFormat(ctx, target, internalFormat,
537 GL_BGRA, GL_UNSIGNED_BYTE);
538
539 _mesa_init_teximage_fields(ctx, texImage,
540 tex->width0, tex->height0, 1, 0,
541 internalFormat, texFormat);
542
543 width = tex->width0;
544 height = tex->height0;
545 depth = tex->depth0;
546
547 /* grow the image size until we hit level = 0 */
548 while (level > 0) {
549 if (width != 1)
550 width <<= 1;
551 if (height != 1)
552 height <<= 1;
553 if (depth != 1)
554 depth <<= 1;
555 level--;
556 }
557 }
558 else {
559 _mesa_clear_texture_image(ctx, texImage);
560 width = height = depth = 0;
561 }
562
563 pipe_resource_reference(&stImage->pt, tex);
564 stObj->width0 = width;
565 stObj->height0 = height;
566 stObj->depth0 = depth;
567
568 _mesa_dirty_texobj(ctx, texObj, GL_TRUE);
569 _mesa_unlock_texture(ctx, texObj);
570
571 return TRUE;
572 }
573
574 static void
st_context_copy(struct st_context_iface * stctxi,struct st_context_iface * stsrci,unsigned mask)575 st_context_copy(struct st_context_iface *stctxi,
576 struct st_context_iface *stsrci, unsigned mask)
577 {
578 struct st_context *st = (struct st_context *) stctxi;
579 struct st_context *src = (struct st_context *) stsrci;
580
581 _mesa_copy_context(src->ctx, st->ctx, mask);
582 }
583
584 static boolean
st_context_share(struct st_context_iface * stctxi,struct st_context_iface * stsrci)585 st_context_share(struct st_context_iface *stctxi,
586 struct st_context_iface *stsrci)
587 {
588 struct st_context *st = (struct st_context *) stctxi;
589 struct st_context *src = (struct st_context *) stsrci;
590
591 return _mesa_share_state(st->ctx, src->ctx);
592 }
593
594 static void
st_context_destroy(struct st_context_iface * stctxi)595 st_context_destroy(struct st_context_iface *stctxi)
596 {
597 struct st_context *st = (struct st_context *) stctxi;
598 st_destroy_context(st);
599 }
600
601 static struct st_context_iface *
st_api_create_context(struct st_api * stapi,struct st_manager * smapi,const struct st_context_attribs * attribs,enum st_context_error * error,struct st_context_iface * shared_stctxi)602 st_api_create_context(struct st_api *stapi, struct st_manager *smapi,
603 const struct st_context_attribs *attribs,
604 enum st_context_error *error,
605 struct st_context_iface *shared_stctxi)
606 {
607 struct st_context *shared_ctx = (struct st_context *) shared_stctxi;
608 struct st_context *st;
609 struct pipe_context *pipe;
610 struct gl_config mode;
611 gl_api api;
612
613 if (!(stapi->profile_mask & (1 << attribs->profile)))
614 return NULL;
615
616 switch (attribs->profile) {
617 case ST_PROFILE_DEFAULT:
618 api = API_OPENGL;
619 break;
620 case ST_PROFILE_OPENGL_ES1:
621 api = API_OPENGLES;
622 break;
623 case ST_PROFILE_OPENGL_ES2:
624 api = API_OPENGLES2;
625 break;
626 case ST_PROFILE_OPENGL_CORE:
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 /* need to perform version check */
648 if (attribs->major > 1 || attribs->minor > 0) {
649 _mesa_compute_version(st->ctx);
650
651 /* Is the actual version less than the requested version? Mesa can't
652 * yet enforce the added restrictions of a forward-looking context, so
653 * fail that too.
654 */
655 if (st->ctx->Version < attribs->major * 10 + attribs->minor
656 || (attribs->flags & ~ST_CONTEXT_FLAG_DEBUG) != 0) {
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
673 *error = ST_CONTEXT_SUCCESS;
674 return &st->iface;
675 }
676
677 static struct st_context_iface *
st_api_get_current(struct st_api * stapi)678 st_api_get_current(struct st_api *stapi)
679 {
680 GET_CURRENT_CONTEXT(ctx);
681 struct st_context *st = (ctx) ? ctx->st : NULL;
682
683 return (st) ? &st->iface : NULL;
684 }
685
686 static struct st_framebuffer *
st_framebuffer_reuse_or_create(struct gl_framebuffer * fb,struct st_framebuffer_iface * stfbi)687 st_framebuffer_reuse_or_create(struct gl_framebuffer *fb,
688 struct st_framebuffer_iface *stfbi)
689 {
690 struct st_framebuffer *cur = st_ws_framebuffer(fb), *stfb = NULL;
691
692 /* dummy framebuffers cant be used as st_framebuffer */
693 if (cur && &cur->Base != _mesa_get_incomplete_framebuffer() &&
694 cur->iface == stfbi) {
695 /* reuse the current stfb */
696 st_framebuffer_reference(&stfb, cur);
697 }
698 else {
699 /* create a new one */
700 stfb = st_framebuffer_create(stfbi);
701 }
702
703 return stfb;
704 }
705
706 static boolean
st_api_make_current(struct st_api * stapi,struct st_context_iface * stctxi,struct st_framebuffer_iface * stdrawi,struct st_framebuffer_iface * streadi)707 st_api_make_current(struct st_api *stapi, struct st_context_iface *stctxi,
708 struct st_framebuffer_iface *stdrawi,
709 struct st_framebuffer_iface *streadi)
710 {
711 struct st_context *st = (struct st_context *) stctxi;
712 struct st_framebuffer *stdraw, *stread;
713 boolean ret;
714
715 _glapi_check_multithread();
716
717 if (st) {
718 /* reuse or create the draw fb */
719 stdraw = st_framebuffer_reuse_or_create(st->ctx->WinSysDrawBuffer,
720 stdrawi);
721 if (streadi != stdrawi) {
722 /* do the same for the read fb */
723 stread = st_framebuffer_reuse_or_create(st->ctx->WinSysReadBuffer,
724 streadi);
725 }
726 else {
727 stread = NULL;
728 /* reuse the draw fb for the read fb */
729 if (stdraw)
730 st_framebuffer_reference(&stread, stdraw);
731 }
732
733 if (stdraw && stread) {
734 st_framebuffer_validate(stdraw, st);
735 if (stread != stdraw)
736 st_framebuffer_validate(stread, st);
737
738 ret = _mesa_make_current(st->ctx, &stdraw->Base, &stread->Base);
739
740 st->draw_stamp = stdraw->stamp - 1;
741 st->read_stamp = stread->stamp - 1;
742 st_context_validate(st, stdraw, stread);
743 }
744 else {
745 struct gl_framebuffer *incomplete = _mesa_get_incomplete_framebuffer();
746 ret = _mesa_make_current(st->ctx, incomplete, incomplete);
747 }
748
749 st_framebuffer_reference(&stdraw, NULL);
750 st_framebuffer_reference(&stread, NULL);
751 }
752 else {
753 ret = _mesa_make_current(NULL, NULL, NULL);
754 }
755
756 return ret;
757 }
758
759 static st_proc_t
st_api_get_proc_address(struct st_api * stapi,const char * procname)760 st_api_get_proc_address(struct st_api *stapi, const char *procname)
761 {
762 return (st_proc_t) _glapi_get_proc_address(procname);
763 }
764
765 static void
st_api_destroy(struct st_api * stapi)766 st_api_destroy(struct st_api *stapi)
767 {
768 }
769
770 /**
771 * Flush the front buffer if the current context renders to the front buffer.
772 */
773 void
st_manager_flush_frontbuffer(struct st_context * st)774 st_manager_flush_frontbuffer(struct st_context *st)
775 {
776 struct st_framebuffer *stfb = st_ws_framebuffer(st->ctx->DrawBuffer);
777 struct st_renderbuffer *strb = NULL;
778
779 if (stfb)
780 strb = st_renderbuffer(stfb->Base.Attachment[BUFFER_FRONT_LEFT].Renderbuffer);
781 if (!strb)
782 return;
783
784 /* never a dummy fb */
785 assert(&stfb->Base != _mesa_get_incomplete_framebuffer());
786 stfb->iface->flush_front(stfb->iface, ST_ATTACHMENT_FRONT_LEFT);
787 }
788
789 /**
790 * Return the surface of an EGLImage.
791 * FIXME: I think this should operate on resources, not surfaces
792 */
793 struct pipe_surface *
st_manager_get_egl_image_surface(struct st_context * st,void * eglimg,unsigned usage)794 st_manager_get_egl_image_surface(struct st_context *st,
795 void *eglimg, unsigned usage)
796 {
797 struct st_manager *smapi =
798 (struct st_manager *) st->iface.st_context_private;
799 struct st_egl_image stimg;
800 struct pipe_surface *ps, surf_tmpl;
801
802 if (!smapi || !smapi->get_egl_image)
803 return NULL;
804
805 memset(&stimg, 0, sizeof(stimg));
806 if (!smapi->get_egl_image(smapi, eglimg, &stimg))
807 return NULL;
808
809 u_surface_default_template(&surf_tmpl, stimg.texture, usage);
810 surf_tmpl.u.tex.level = stimg.level;
811 surf_tmpl.u.tex.first_layer = stimg.layer;
812 surf_tmpl.u.tex.last_layer = stimg.layer;
813 ps = st->pipe->create_surface(st->pipe, stimg.texture, &surf_tmpl);
814 pipe_resource_reference(&stimg.texture, NULL);
815
816 return ps;
817 }
818
819 /**
820 * Re-validate the framebuffers.
821 */
822 void
st_manager_validate_framebuffers(struct st_context * st)823 st_manager_validate_framebuffers(struct st_context *st)
824 {
825 struct st_framebuffer *stdraw = st_ws_framebuffer(st->ctx->DrawBuffer);
826 struct st_framebuffer *stread = st_ws_framebuffer(st->ctx->ReadBuffer);
827
828 if (stdraw)
829 st_framebuffer_validate(stdraw, st);
830 if (stread && stread != stdraw)
831 st_framebuffer_validate(stread, st);
832
833 st_context_validate(st, stdraw, stread);
834 }
835
836 /**
837 * Add a color renderbuffer on demand.
838 */
839 boolean
st_manager_add_color_renderbuffer(struct st_context * st,struct gl_framebuffer * fb,gl_buffer_index idx)840 st_manager_add_color_renderbuffer(struct st_context *st,
841 struct gl_framebuffer *fb,
842 gl_buffer_index idx)
843 {
844 struct st_framebuffer *stfb = st_ws_framebuffer(fb);
845
846 /* FBO */
847 if (!stfb)
848 return FALSE;
849
850 if (stfb->Base.Attachment[idx].Renderbuffer)
851 return TRUE;
852
853 switch (idx) {
854 case BUFFER_FRONT_LEFT:
855 case BUFFER_BACK_LEFT:
856 case BUFFER_FRONT_RIGHT:
857 case BUFFER_BACK_RIGHT:
858 break;
859 default:
860 return FALSE;
861 break;
862 }
863
864 if (!st_framebuffer_add_renderbuffer(stfb, idx))
865 return FALSE;
866
867 st_framebuffer_update_attachments(stfb);
868
869 /*
870 * Force a call to the state tracker manager to validate the
871 * new renderbuffer. It might be that there is a window system
872 * renderbuffer available.
873 */
874 if(stfb->iface)
875 stfb->iface_stamp = p_atomic_read(&stfb->iface->stamp) - 1;
876
877 st_invalidate_state(st->ctx, _NEW_BUFFERS);
878
879 return TRUE;
880 }
881
882 static const struct st_api st_gl_api = {
883 "Mesa " MESA_VERSION_STRING,
884 ST_API_OPENGL,
885 #if FEATURE_GL
886 ST_PROFILE_DEFAULT_MASK |
887 #endif
888 #if FEATURE_ES1
889 ST_PROFILE_OPENGL_ES1_MASK |
890 #endif
891 #if FEATURE_ES2
892 ST_PROFILE_OPENGL_ES2_MASK |
893 #endif
894 0,
895 0,
896 st_api_destroy,
897 st_api_get_proc_address,
898 st_api_create_context,
899 st_api_make_current,
900 st_api_get_current,
901 };
902
903 struct st_api *
st_gl_api_create(void)904 st_gl_api_create(void)
905 {
906 return (struct st_api *) &st_gl_api;
907 }
908