Debian Codegraph @

src/egl/drivers/wgl/egl_wgl.c

raw
1 /*
2 * Copyright © Microsoft Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 */
23
24 #include "state_tracker/st_context.h"
25
26 #include <eglcontext.h>
27 #include <eglcurrent.h>
28 #include <egldriver.h>
29 #include <egllog.h>
30 #include <eglsurface.h>
31
32 #include "egl_wgl.h"
33
34 #include <stw_context.h>
35 #include <stw_device.h>
36 #include <stw_ext_interop.h>
37 #include <stw_framebuffer.h>
38 #include <stw_image.h>
39 #include <stw_pixelformat.h>
40 #include <stw_winsys.h>
41
42 #include <GL/wglext.h>
43
44 #include <pipe/p_context.h>
45 #include <pipe/p_screen.h>
46 #include <pipe/p_state.h>
47
48 #include "util/u_call_once.h"
49 #include "mesa/glapi/glapi/glapi.h"
50 #include "dispatch.h"
51
52 #include <GL/mesa_glinterop.h>
53
54 static EGLBoolean
55 wgl_match_config(const _EGLConfig *conf, const _EGLConfig *criteria)
56 {
57 if (_eglCompareConfigs(conf, criteria, NULL, EGL_FALSE) != 0)
58 return EGL_FALSE;
59
60 if (!_eglMatchConfig(conf, criteria))
61 return EGL_FALSE;
62
63 return EGL_TRUE;
64 }
65
66 static struct wgl_egl_config *
67 wgl_add_config(_EGLDisplay *disp, const struct stw_pixelformat_info *stw_config,
68 int id, EGLint surface_type)
69 {
70 struct wgl_egl_config *conf;
71 _EGLConfig base;
72 unsigned int double_buffer;
73 _EGLConfig *matching_config;
74 EGLint num_configs = 0;
75 EGLint config_id;
76
77 _eglInitConfig(&base, disp, id);
78
79 double_buffer = (stw_config->pfd.dwFlags & PFD_DOUBLEBUFFER) != 0;
80
81 if (stw_config->pfd.iPixelType != PFD_TYPE_RGBA)
82 return NULL;
83
84 base.RedSize = stw_config->pfd.cRedBits;
85 base.GreenSize = stw_config->pfd.cGreenBits;
86 base.BlueSize = stw_config->pfd.cBlueBits;
87 base.AlphaSize = stw_config->pfd.cAlphaBits;
88 base.BufferSize = stw_config->pfd.cColorBits;
89
90 if (stw_config->pfd.cAccumBits) {
91 /* Don't expose visuals with the accumulation buffer. */
92 return NULL;
93 }
94
95 base.MaxPbufferWidth = _EGL_MAX_PBUFFER_WIDTH;
96 base.MaxPbufferHeight = _EGL_MAX_PBUFFER_HEIGHT;
97
98 base.DepthSize = stw_config->pfd.cDepthBits;
99 base.StencilSize = stw_config->pfd.cStencilBits;
100 base.Samples = stw_config->stvis.samples;
101 base.SampleBuffers = base.Samples > 1;
102
103 base.NativeRenderable = EGL_TRUE;
104
105 if (surface_type & EGL_PBUFFER_BIT) {
106 base.BindToTextureRGB = stw_config->bindToTextureRGB;
107 if (base.AlphaSize > 0)
108 base.BindToTextureRGBA = stw_config->bindToTextureRGBA;
109 }
110
111 if (double_buffer) {
112 surface_type &= ~EGL_PIXMAP_BIT;
113 }
114
115 if (!(stw_config->pfd.dwFlags & PFD_DRAW_TO_WINDOW)) {
116 surface_type &= ~EGL_WINDOW_BIT;
117 }
118
119 if (!surface_type)
120 return NULL;
121
122 base.SurfaceType = surface_type;
123 base.RenderableType = disp->ClientAPIs;
124 base.Conformant = disp->ClientAPIs;
125
126 base.MinSwapInterval = 0;
127 base.MaxSwapInterval = 4;
128 base.YInvertedNOK = EGL_TRUE;
129
130 if (!_eglValidateConfig(&base, EGL_FALSE)) {
131 _eglLog(_EGL_DEBUG, "wgl: failed to validate config %d", id);
132 return NULL;
133 }
134
135 config_id = base.ConfigID;
136 base.ConfigID = EGL_DONT_CARE;
137 base.SurfaceType = EGL_DONT_CARE;
138 num_configs = _eglFilterArray(disp->Configs, (void **)&matching_config, 1,
139 (_EGLArrayForEach)wgl_match_config, &base);
140
141 if (num_configs == 1) {
142 conf = (struct wgl_egl_config *)matching_config;
143
144 if (!conf->stw_config[double_buffer])
145 conf->stw_config[double_buffer] = stw_config;
146 else
147 /* a similar config type is already added (unlikely) => discard */
148 return NULL;
149 } else if (num_configs == 0) {
150 conf = calloc(1, sizeof(*conf));
151 if (conf == NULL)
152 return NULL;
153
154 conf->stw_config[double_buffer] = stw_config;
155
156 memcpy(&conf->base, &base, sizeof base);
157 conf->base.SurfaceType = 0;
158 conf->base.ConfigID = config_id;
159
160 _eglLinkConfig(&conf->base);
161 } else {
162 UNREACHABLE("duplicates should not be possible");
163 return NULL;
164 }
165
166 conf->base.SurfaceType |= surface_type;
167
168 return conf;
169 }
170
171 static EGLBoolean
172 wgl_add_configs(_EGLDisplay *disp)
173 {
174 unsigned int config_count = 0;
175 unsigned surface_type = EGL_PBUFFER_BIT | EGL_WINDOW_BIT;
176
177 // This is already a filtered set of what the driver supports,
178 // and there's no further filtering needed per-visual
179 for (unsigned i = 1; stw_pixelformat_get_info(i) != NULL; i++) {
180
181 struct wgl_egl_config *wgl_conf = wgl_add_config(
182 disp, stw_pixelformat_get_info(i), config_count + 1, surface_type);
183
184 if (wgl_conf) {
185 if (wgl_conf->base.ConfigID == config_count + 1)
186 config_count++;
187 }
188 }
189
190 return (config_count != 0);
191 }
192
193 static void
194 wgl_display_destroy(_EGLDisplay *disp)
195 {
196 struct wgl_egl_display *wgl_dpy = wgl_egl_display(disp);
197
198 st_screen_destroy(&wgl_dpy->base);
199 free(wgl_dpy);
200 }
201
202 static int
203 wgl_egl_st_get_param(struct pipe_frontend_screen *fscreen,
204 enum st_manager_param param)
205 {
206 /* no-op */
207 return 0;
208 }
209
210 static bool
211 wgl_get_egl_image(struct pipe_frontend_screen *fscreen, void *image,
212 struct st_egl_image *out)
213 {
214 struct wgl_egl_image *wgl_img = (struct wgl_egl_image *)image;
215 stw_translate_image(wgl_img->img, out);
216 return true;
217 }
218
219 static bool
220 wgl_validate_egl_image(struct pipe_frontend_screen *fscreen, void *image)
221 {
222 struct wgl_egl_display *wgl_dpy = (struct wgl_egl_display *)fscreen;
223 _EGLDisplay *disp = _eglLockDisplay(wgl_dpy->parent);
224 _EGLImage *img = _eglLookupImage(image, disp);
225 _eglUnlockDisplay(disp);
226
227 if (img == NULL) {
228 _eglError(EGL_BAD_PARAMETER, "wgl_validate_egl_image");
229 return false;
230 }
231
232 return true;
233 }
234
235 static EGLBoolean
236 wgl_initialize_impl(_EGLDisplay *disp, HDC hdc)
237 {
238 struct wgl_egl_display *wgl_dpy;
239 const char *err;
240
241 wgl_dpy = calloc(1, sizeof(*wgl_dpy));
242 if (!wgl_dpy)
243 return _eglError(EGL_BAD_ALLOC, "eglInitialize");
244
245 disp->DriverData = (void *)wgl_dpy;
246 wgl_dpy->parent = disp;
247
248 if (!stw_init_screen(hdc)) {
249 err = "wgl: failed to initialize screen";
250 goto cleanup;
251 }
252
253 struct stw_device *stw_dev = stw_get_device();
254 wgl_dpy->screen = stw_dev->screen;
255
256 wgl_dpy->base.screen = stw_dev->screen;
257 wgl_dpy->base.get_param = wgl_egl_st_get_param;
258 wgl_dpy->base.get_egl_image = wgl_get_egl_image;
259 wgl_dpy->base.validate_egl_image = wgl_validate_egl_image;
260
261 disp->ClientAPIs = 0;
262 if (_eglIsApiValid(EGL_OPENGL_API))
263 disp->ClientAPIs |= EGL_OPENGL_BIT;
264 if (_eglIsApiValid(EGL_OPENGL_ES_API))
265 disp->ClientAPIs |=
266 EGL_OPENGL_ES_BIT | EGL_OPENGL_ES2_BIT | EGL_OPENGL_ES3_BIT_KHR;
267
268 disp->Extensions.KHR_no_config_context = EGL_TRUE;
269 disp->Extensions.KHR_surfaceless_context = EGL_TRUE;
270 disp->Extensions.MESA_query_driver = EGL_TRUE;
271
272 /* Report back to EGL the bitmask of priorities supported */
273 disp->Extensions.IMG_context_priority = wgl_dpy->screen->caps.context_priority_mask;
274 disp->Extensions.NV_context_priority_realtime =
275 disp->Extensions.IMG_context_priority &
276 (1 << __EGL_CONTEXT_PRIORITY_REALTIME_BIT);
277
278 disp->Extensions.EXT_pixel_format_float = EGL_TRUE;
279
280 if (wgl_dpy->screen->is_format_supported(
281 wgl_dpy->screen, PIPE_FORMAT_B8G8R8A8_SRGB, PIPE_TEXTURE_2D, 0, 0,
282 PIPE_BIND_RENDER_TARGET))
283 disp->Extensions.KHR_gl_colorspace = EGL_TRUE;
284
285 disp->Extensions.KHR_create_context = EGL_TRUE;
286
287 disp->Extensions.KHR_image_base = EGL_TRUE;
288 disp->Extensions.KHR_gl_renderbuffer_image = EGL_TRUE;
289 disp->Extensions.KHR_gl_texture_2D_image = EGL_TRUE;
290 disp->Extensions.KHR_gl_texture_cubemap_image = EGL_TRUE;
291 disp->Extensions.KHR_gl_texture_3D_image = EGL_TRUE;
292
293 disp->Extensions.KHR_fence_sync = EGL_TRUE;
294 disp->Extensions.KHR_reusable_sync = EGL_TRUE;
295 disp->Extensions.KHR_wait_sync = EGL_TRUE;
296
297 if (!wgl_add_configs(disp)) {
298 err = "wgl: failed to add configs";
299 goto cleanup;
300 }
301
302 return EGL_TRUE;
303
304 cleanup:
305 wgl_display_destroy(disp);
306 return _eglError(EGL_NOT_INITIALIZED, err);
307 }
308
309 static EGLBoolean
310 wgl_initialize(_EGLDisplay *disp)
311 {
312 EGLBoolean ret = EGL_FALSE;
313 struct wgl_egl_display *wgl_dpy = wgl_egl_display(disp);
314
315 /* In the case where the application calls eglMakeCurrent(context1),
316 * eglTerminate, then eglInitialize again (without a call to eglReleaseThread
317 * or eglMakeCurrent(NULL) before that), wgl_dpy structure is still
318 * initialized, as we need it to be able to free context1 correctly.
319 *
320 * It would probably be safest to forcibly release the display with
321 * wgl_display_release, to make sure the display is reinitialized correctly.
322 * However, the EGL spec states that we need to keep a reference to the
323 * current context (so we cannot call wgl_make_current(NULL)), and therefore
324 * we would leak context1 as we would be missing the old display connection
325 * to free it up correctly.
326 */
327 if (wgl_dpy) {
328 p_atomic_inc(&wgl_dpy->ref_count);
329 return EGL_TRUE;
330 }
331
332 switch (disp->Platform) {
333 case _EGL_PLATFORM_SURFACELESS:
334 ret = wgl_initialize_impl(disp, NULL);
335 break;
336 case _EGL_PLATFORM_WINDOWS:
337 ret = wgl_initialize_impl(disp, disp->PlatformDisplay);
338 break;
339 default:
340 UNREACHABLE("Callers ensure we cannot get here.");
341 return EGL_FALSE;
342 }
343
344 if (!ret)
345 return EGL_FALSE;
346
347 wgl_dpy = wgl_egl_display(disp);
348 p_atomic_inc(&wgl_dpy->ref_count);
349
350 return EGL_TRUE;
351 }
352
353 /**
354 * Decrement display reference count, and free up display if necessary.
355 */
356 static void
357 wgl_display_release(_EGLDisplay *disp)
358 {
359 struct wgl_egl_display *wgl_dpy;
360
361 if (!disp)
362 return;
363
364 wgl_dpy = wgl_egl_display(disp);
365
366 assert(wgl_dpy->ref_count > 0);
367 if (!p_atomic_dec_zero(&wgl_dpy->ref_count))
368 return;
369
370 _eglCleanupDisplay(disp);
371 wgl_display_destroy(disp);
372 }
373
374 /**
375 * Called via eglTerminate(), drv->Terminate().
376 *
377 * This must be guaranteed to be called exactly once, even if eglTerminate is
378 * called many times (without a eglInitialize in between).
379 */
380 static EGLBoolean
381 wgl_terminate(_EGLDisplay *disp)
382 {
383 /* Release all non-current Context/Surfaces. */
384 _eglReleaseDisplayResources(disp);
385
386 wgl_display_release(disp);
387
388 return EGL_TRUE;
389 }
390
391 /**
392 * Called via eglCreateContext(), drv->CreateContext().
393 */
394 static _EGLContext *
395 wgl_create_context(_EGLDisplay *disp, _EGLConfig *conf, _EGLContext *share_list,
396 const EGLint *attrib_list)
397 {
398 struct wgl_egl_context *wgl_ctx;
399 struct wgl_egl_display *wgl_dpy = wgl_egl_display(disp);
400 struct wgl_egl_context *wgl_ctx_shared = wgl_egl_context(share_list);
401 struct stw_context *shared = wgl_ctx_shared ? wgl_ctx_shared->ctx : NULL;
402 struct wgl_egl_config *wgl_config = wgl_egl_config(conf);
403 const struct stw_pixelformat_info *stw_config;
404
405 wgl_ctx = malloc(sizeof(*wgl_ctx));
406 if (!wgl_ctx) {
407 _eglError(EGL_BAD_ALLOC, "eglCreateContext");
408 return NULL;
409 }
410
411 if (!_eglInitContext(&wgl_ctx->base, disp, conf, share_list, attrib_list))
412 goto cleanup;
413
414 unsigned profile_mask = 0;
415 switch (wgl_ctx->base.ClientAPI) {
416 case EGL_OPENGL_ES_API:
417 profile_mask = WGL_CONTEXT_ES_PROFILE_BIT_EXT;
418 break;
419 case EGL_OPENGL_API:
420 if ((wgl_ctx->base.ClientMajorVersion >= 4 ||
421 (wgl_ctx->base.ClientMajorVersion == 3 &&
422 wgl_ctx->base.ClientMinorVersion >= 2)) &&
423 wgl_ctx->base.Profile == EGL_CONTEXT_OPENGL_CORE_PROFILE_BIT_KHR)
424 profile_mask = WGL_CONTEXT_CORE_PROFILE_BIT_ARB;
425 else if (wgl_ctx->base.ClientMajorVersion == 3 &&
426 wgl_ctx->base.ClientMinorVersion == 1)
427 profile_mask = WGL_CONTEXT_CORE_PROFILE_BIT_ARB;
428 else
429 profile_mask = WGL_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB;
430 break;
431 default:
432 _eglError(EGL_BAD_PARAMETER, "eglCreateContext");
433 free(wgl_ctx);
434 return NULL;
435 }
436
437 if (conf != NULL) {
438 /* The config chosen here isn't necessarily
439 * used for surfaces later.
440 * A pixmap surface will use the single config.
441 * This opportunity depends on disabling the
442 * doubleBufferMode check in
443 * src/mesa/main/context.c:check_compatible()
444 */
445 if (wgl_config->stw_config[1])
446 stw_config = wgl_config->stw_config[1];
447 else
448 stw_config = wgl_config->stw_config[0];
449 } else
450 stw_config = NULL;
451
452 unsigned flags = 0;
453 if (wgl_ctx->base.Flags & EGL_CONTEXT_OPENGL_FORWARD_COMPATIBLE_BIT_KHR)
454 flags |= WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB;
455 if (wgl_ctx->base.Flags & EGL_CONTEXT_OPENGL_DEBUG_BIT_KHR)
456 flags |= WGL_CONTEXT_DEBUG_BIT_ARB;
457 unsigned resetStrategy = WGL_NO_RESET_NOTIFICATION_ARB;
458 if (wgl_ctx->base.ResetNotificationStrategy != EGL_NO_RESET_NOTIFICATION)
459 resetStrategy = WGL_LOSE_CONTEXT_ON_RESET_ARB;
460 wgl_ctx->ctx = stw_create_context_attribs(
461 disp->PlatformDisplay, 0, shared, &wgl_dpy->base,
462 wgl_ctx->base.ClientMajorVersion, wgl_ctx->base.ClientMinorVersion, flags,
463 profile_mask, stw_config, resetStrategy);
464
465 if (!wgl_ctx->ctx)
466 goto cleanup;
467
468 return &wgl_ctx->base;
469
470 cleanup:
471 free(wgl_ctx);
472 return NULL;
473 }
474
475 /**
476 * Called via eglDestroyContext(), drv->DestroyContext().
477 */
478 static EGLBoolean
479 wgl_destroy_context(_EGLDisplay *disp, _EGLContext *ctx)
480 {
481 struct wgl_egl_context *wgl_ctx = wgl_egl_context(ctx);
482
483 if (_eglPutContext(ctx)) {
484 stw_destroy_context(wgl_ctx->ctx);
485 free(wgl_ctx);
486 }
487
488 return EGL_TRUE;
489 }
490
491 static EGLBoolean
492 wgl_destroy_surface(_EGLDisplay *disp, _EGLSurface *surf)
493 {
494 struct wgl_egl_surface *wgl_surf = wgl_egl_surface(surf);
495
496 if (!_eglPutSurface(surf))
497 return EGL_TRUE;
498
499 if (wgl_surf->fb->owner == STW_FRAMEBUFFER_PBUFFER) {
500 DestroyWindow(wgl_surf->fb->hWnd);
501 } else {
502 struct stw_context *ctx = stw_current_context();
503 stw_framebuffer_lock(wgl_surf->fb);
504 stw_framebuffer_release_locked(wgl_surf->fb, ctx ? ctx->st : NULL);
505 }
506 return EGL_TRUE;
507 }
508
509 static void
510 wgl_gl_flush()
511 {
512 CALL_Flush(GET_DISPATCH(), ());
513 }
514
515 /**
516 * Called via eglMakeCurrent(), drv->MakeCurrent().
517 */
518 static EGLBoolean
519 wgl_make_current(_EGLDisplay *disp, _EGLSurface *dsurf, _EGLSurface *rsurf,
520 _EGLContext *ctx)
521 {
522 struct wgl_egl_display *wgl_dpy = wgl_egl_display(disp);
523 struct wgl_egl_context *wgl_ctx = wgl_egl_context(ctx);
524 _EGLDisplay *old_disp = NULL;
525 _EGLContext *old_ctx;
526 _EGLSurface *old_dsurf, *old_rsurf;
527 _EGLSurface *tmp_dsurf, *tmp_rsurf;
528 struct stw_framebuffer *ddraw, *rdraw;
529 struct stw_context *cctx;
530 EGLint egl_error = EGL_SUCCESS;
531
532 if (!wgl_dpy)
533 return _eglError(EGL_NOT_INITIALIZED, "eglMakeCurrent");
534
535 /* make new bindings, set the EGL error otherwise */
536 if (!_eglBindContext(ctx, dsurf, rsurf, &old_ctx, &old_dsurf, &old_rsurf))
537 return EGL_FALSE;
538
539 if (old_ctx) {
540 struct stw_context *old_cctx = wgl_egl_context(old_ctx)->ctx;
541 old_disp = old_ctx->Resource.Display;
542
543 /* flush before context switch */
544 wgl_gl_flush();
545
546 stw_unbind_context(old_cctx);
547 }
548
549 ddraw = (dsurf) ? wgl_egl_surface(dsurf)->fb : NULL;
550 rdraw = (rsurf) ? wgl_egl_surface(rsurf)->fb : NULL;
551 cctx = (wgl_ctx) ? wgl_ctx->ctx : NULL;
552
553 if (cctx || ddraw || rdraw) {
554 if (!stw_make_current(ddraw, rdraw, cctx)) {
555 _EGLContext *tmp_ctx;
556
557 /* stw_make_current failed. We cannot tell for sure why, but
558 * setting the error to EGL_BAD_MATCH is surely better than leaving it
559 * as EGL_SUCCESS.
560 */
561 egl_error = EGL_BAD_MATCH;
562
563 /* undo the previous _eglBindContext */
564 _eglBindContext(old_ctx, old_dsurf, old_rsurf, &ctx, &tmp_dsurf,
565 &tmp_rsurf);
566 assert(&wgl_ctx->base == ctx && tmp_dsurf == dsurf &&
567 tmp_rsurf == rsurf);
568
569 _eglPutSurface(dsurf);
570 _eglPutSurface(rsurf);
571 _eglPutContext(ctx);
572
573 _eglPutSurface(old_dsurf);
574 _eglPutSurface(old_rsurf);
575 _eglPutContext(old_ctx);
576
577 ddraw = (old_dsurf) ? wgl_egl_surface(old_dsurf)->fb : NULL;
578 rdraw = (old_rsurf) ? wgl_egl_surface(old_rsurf)->fb : NULL;
579 cctx = (old_ctx) ? wgl_egl_context(old_ctx)->ctx : NULL;
580
581 /* undo the previous _eglBindContext */
582 if (stw_make_current(ddraw, rdraw, cctx)) {
583 return _eglError(egl_error, "eglMakeCurrent");
584 }
585
586 /* We cannot restore the same state as it was before calling
587 * eglMakeCurrent() and the spec isn't clear about what to do. We
588 * can prevent EGL from calling into the DRI driver with no DRI
589 * context bound.
590 */
591 dsurf = rsurf = NULL;
592 ctx = NULL;
593
594 _eglBindContext(ctx, dsurf, rsurf, &tmp_ctx, &tmp_dsurf, &tmp_rsurf);
595 assert(tmp_ctx == old_ctx && tmp_dsurf == old_dsurf &&
596 tmp_rsurf == old_rsurf);
597
598 _eglLog(_EGL_WARNING, "wgl: failed to rebind the previous context");
599 } else {
600 /* _eglBindContext succeeded, so take a reference on the
601 * wgl_dpy. This prevents wgl_dpy from being reinitialized when a
602 * EGLDisplay is terminated and then initialized again while a
603 * context is still bound. See wgl_initialize() for a more in depth
604 * explanation. */
605 p_atomic_inc(&wgl_dpy->ref_count);
606 }
607 }
608
609 wgl_destroy_surface(disp, old_dsurf);
610 wgl_destroy_surface(disp, old_rsurf);
611
612 if (old_ctx) {
613 wgl_destroy_context(disp, old_ctx);
614 wgl_display_release(old_disp);
615 }
616
617 if (egl_error != EGL_SUCCESS)
618 return _eglError(egl_error, "eglMakeCurrent");
619
620 return EGL_TRUE;
621 }
622
623 static _EGLSurface *
624 wgl_create_window_surface(_EGLDisplay *disp, _EGLConfig *conf,
625 void *native_window, const EGLint *attrib_list)
626 {
627 struct wgl_egl_display *wgl_dpy = wgl_egl_display(disp);
628 struct wgl_egl_config *wgl_conf = wgl_egl_config(conf);
629
630 struct wgl_egl_surface *wgl_surf = calloc(1, sizeof(*wgl_surf));
631 if (!wgl_surf)
632 return NULL;
633
634 if (!_eglInitSurface(&wgl_surf->base, disp, EGL_WINDOW_BIT, conf,
635 attrib_list, native_window)) {
636 free(wgl_surf);
637 return NULL;
638 }
639
640 const struct stw_pixelformat_info *stw_conf = wgl_conf->stw_config[1]
641 ? wgl_conf->stw_config[1]
642 : wgl_conf->stw_config[0];
643 wgl_surf->fb = stw_framebuffer_create(NULL,
644 native_window, stw_conf, STW_FRAMEBUFFER_EGL_WINDOW, &wgl_dpy->base);
645 if (!wgl_surf->fb) {
646 free(wgl_surf);
647 return NULL;
648 }
649
650 wgl_surf->fb->swap_interval = 1;
651 stw_framebuffer_unlock(wgl_surf->fb);
652
653 return &wgl_surf->base;
654 }
655
656 static _EGLSurface *
657 wgl_create_pbuffer_surface(_EGLDisplay *disp, _EGLConfig *conf,
658 const EGLint *attrib_list)
659 {
660 struct wgl_egl_display *wgl_dpy = wgl_egl_display(disp);
661 struct wgl_egl_config *wgl_conf = wgl_egl_config(conf);
662
663 struct wgl_egl_surface *wgl_surf = calloc(1, sizeof(*wgl_surf));
664 if (!wgl_surf)
665 return NULL;
666
667 if (!_eglInitSurface(&wgl_surf->base, disp, EGL_PBUFFER_BIT, conf,
668 attrib_list, NULL)) {
669 free(wgl_surf);
670 return NULL;
671 }
672
673 const struct stw_pixelformat_info *stw_conf = wgl_conf->stw_config[1]
674 ? wgl_conf->stw_config[1]
675 : wgl_conf->stw_config[0];
676 wgl_surf->fb = stw_pbuffer_create(stw_conf, wgl_surf->base.Width,
677 wgl_surf->base.Height, &wgl_dpy->base);
678 if (!wgl_surf->fb) {
679 free(wgl_surf);
680 return NULL;
681 }
682
683 wgl_surf->fb->swap_interval = 1;
684 stw_framebuffer_unlock(wgl_surf->fb);
685
686 return &wgl_surf->base;
687 }
688
689 static EGLBoolean
690 wgl_query_surface(_EGLDisplay *disp, _EGLSurface *surf, EGLint attribute,
691 EGLint *value)
692 {
693 struct wgl_egl_surface *wgl_surf = wgl_egl_surface(surf);
694 RECT client_rect;
695
696 switch (attribute) {
697 case EGL_WIDTH:
698 case EGL_HEIGHT:
699 if (GetClientRect(wgl_surf->fb->hWnd, &client_rect)) {
700 surf->Width = client_rect.right;
701 surf->Height = client_rect.bottom;
702 }
703 break;
704 default:
705 break;
706 }
707 return _eglQuerySurface(disp, surf, attribute, value);
708 }
709
710 static EGLBoolean
711 wgl_bind_tex_image(_EGLDisplay *disp, _EGLSurface *surf, EGLint buffer)
712 {
713 struct wgl_egl_surface *wgl_surf = wgl_egl_surface(surf);
714
715 _EGLContext *ctx = _eglGetCurrentContext();
716 struct wgl_egl_context *wgl_ctx = wgl_egl_context(ctx);
717
718 if (!_eglBindTexImage(disp, surf, buffer))
719 return EGL_FALSE;
720
721 struct pipe_resource *pres = stw_get_framebuffer_resource(
722 wgl_surf->fb->drawable, ST_ATTACHMENT_FRONT_LEFT);
723 enum pipe_format format = pres->format;
724
725 switch (surf->TextureFormat) {
726 case EGL_TEXTURE_RGB:
727 switch (format) {
728 case PIPE_FORMAT_R16G16B16A16_FLOAT:
729 format = PIPE_FORMAT_R16G16B16X16_FLOAT;
730 break;
731 case PIPE_FORMAT_B10G10R10A2_UNORM:
732 format = PIPE_FORMAT_B10G10R10X2_UNORM;
733 break;
734 case PIPE_FORMAT_R10G10B10A2_UNORM:
735 format = PIPE_FORMAT_R10G10B10X2_UNORM;
736 break;
737 case PIPE_FORMAT_BGRA8888_UNORM:
738 format = PIPE_FORMAT_BGRX8888_UNORM;
739 break;
740 case PIPE_FORMAT_ARGB8888_UNORM:
741 format = PIPE_FORMAT_XRGB8888_UNORM;
742 break;
743 default:
744 break;
745 }
746 break;
747 case EGL_TEXTURE_RGBA:
748 break;
749 default:
750 assert(!"Unexpected texture format in wgl_bind_tex_image()");
751 }
752
753 switch (surf->TextureTarget) {
754 case EGL_TEXTURE_2D:
755 break;
756 default:
757 assert(!"Unexpected texture target in wgl_bind_tex_image()");
758 }
759
760 st_context_teximage(wgl_ctx->ctx->st, GL_TEXTURE_2D, 0, format, pres, false);
761
762 return EGL_TRUE;
763 }
764
765 static EGLBoolean
766 wgl_swap_interval(_EGLDisplay *disp, _EGLSurface *surf, EGLint interval)
767 {
768 struct wgl_egl_surface *wgl_surf = wgl_egl_surface(surf);
769 wgl_surf->fb->swap_interval = interval;
770 return EGL_TRUE;
771 }
772
773 static EGLBoolean
774 wgl_swap_buffers(_EGLDisplay *disp, _EGLSurface *draw)
775 {
776 struct wgl_egl_surface *wgl_surf = wgl_egl_surface(draw);
777
778 stw_framebuffer_lock(wgl_surf->fb);
779 HDC hdc = GetDC(wgl_surf->fb->hWnd);
780 BOOL ret = stw_framebuffer_swap_locked(hdc, wgl_surf->fb);
781 ReleaseDC(wgl_surf->fb->hWnd, hdc);
782
783 return ret;
784 }
785
786 static EGLBoolean
787 wgl_wait_client(_EGLDisplay *disp, _EGLContext *ctx)
788 {
789 struct wgl_egl_context *wgl_ctx = wgl_egl_context(ctx);
790 struct pipe_fence_handle *fence = NULL;
791 st_context_flush(wgl_ctx->ctx->st, ST_FLUSH_END_OF_FRAME | ST_FLUSH_WAIT,
792 &fence, NULL, NULL);
793 return EGL_TRUE;
794 }
795
796 static EGLBoolean
797 wgl_wait_native(EGLint engine)
798 {
799 if (engine != EGL_CORE_NATIVE_ENGINE)
800 return _eglError(EGL_BAD_PARAMETER, "eglWaitNative");
801 /* It's unclear what "native" means, but GDI is as good a guess as any */
802 GdiFlush();
803 return EGL_TRUE;
804 }
805
806 static EGLint
807 egl_error_from_stw_image_error(enum stw_image_error err)
808 {
809 switch (err) {
810 case STW_IMAGE_ERROR_SUCCESS:
811 return EGL_SUCCESS;
812 case STW_IMAGE_ERROR_BAD_ALLOC:
813 return EGL_BAD_ALLOC;
814 case STW_IMAGE_ERROR_BAD_MATCH:
815 return EGL_BAD_MATCH;
816 case STW_IMAGE_ERROR_BAD_PARAMETER:
817 return EGL_BAD_PARAMETER;
818 case STW_IMAGE_ERROR_BAD_ACCESS:
819 return EGL_BAD_ACCESS;
820 default:
821 assert(!"unknown stw_image_error code");
822 return EGL_BAD_ALLOC;
823 }
824 }
825
826 static _EGLImage *
827 wgl_create_image_khr_texture(_EGLDisplay *disp, _EGLContext *ctx,
828 EGLenum target, EGLClientBuffer buffer,
829 const EGLint *attr_list)
830 {
831 struct wgl_egl_context *wgl_ctx = wgl_egl_context(ctx);
832 struct wgl_egl_image *wgl_img;
833 GLuint texture = (GLuint)(uintptr_t)buffer;
834 _EGLImageAttribs attrs;
835 GLuint depth;
836 GLenum gl_target;
837 enum stw_image_error error;
838
839 if (texture == 0) {
840 _eglError(EGL_BAD_PARAMETER, "wgl_create_image_khr");
841 return EGL_NO_IMAGE_KHR;
842 }
843
844 if (!_eglParseImageAttribList(&attrs, disp, attr_list))
845 return EGL_NO_IMAGE_KHR;
846
847 switch (target) {
848 case EGL_GL_TEXTURE_2D_KHR:
849 depth = 0;
850 gl_target = GL_TEXTURE_2D;
851 break;
852 case EGL_GL_TEXTURE_3D_KHR:
853 depth = attrs.GLTextureZOffset;
854 gl_target = GL_TEXTURE_3D;
855 break;
856 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_X_KHR:
857 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_X_KHR:
858 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_Y_KHR:
859 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_KHR:
860 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_Z_KHR:
861 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_KHR:
862 depth = target - EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_X_KHR;
863 gl_target = GL_TEXTURE_CUBE_MAP;
864 break;
865 default:
866 UNREACHABLE("Unexpected target in wgl_create_image_khr_texture()");
867 return EGL_NO_IMAGE_KHR;
868 }
869
870 wgl_img = malloc(sizeof *wgl_img);
871 if (!wgl_img) {
872 _eglError(EGL_BAD_ALLOC, "wgl_create_image_khr");
873 return EGL_NO_IMAGE_KHR;
874 }
875
876 _eglInitImage(&wgl_img->base, disp);
877
878 wgl_img->img = stw_create_image_from_texture(
879 wgl_ctx->ctx, gl_target, texture, depth, attrs.GLTextureLevel, &error);
880 assert(!!wgl_img->img == (error == STW_IMAGE_ERROR_SUCCESS));
881
882 if (!wgl_img->img) {
883 free(wgl_img);
884 _eglError(egl_error_from_stw_image_error(error), "wgl_create_image_khr");
885 return EGL_NO_IMAGE_KHR;
886 }
887 return &wgl_img->base;
888 }
889
890 static _EGLImage *
891 wgl_create_image_khr_renderbuffer(_EGLDisplay *disp, _EGLContext *ctx,
892 EGLClientBuffer buffer,
893 const EGLint *attr_list)
894 {
895 struct wgl_egl_context *wgl_ctx = wgl_egl_context(ctx);
896 struct wgl_egl_image *wgl_img;
897 GLuint renderbuffer = (GLuint)(uintptr_t)buffer;
898 enum stw_image_error error;
899
900 if (renderbuffer == 0) {
901 _eglError(EGL_BAD_PARAMETER, "wgl_create_image_khr");
902 return EGL_NO_IMAGE_KHR;
903 }
904
905 wgl_img = malloc(sizeof(*wgl_img));
906 if (!wgl_img) {
907 _eglError(EGL_BAD_ALLOC, "wgl_create_image");
908 return NULL;
909 }
910
911 _eglInitImage(&wgl_img->base, disp);
912
913 wgl_img->img =
914 stw_create_image_from_renderbuffer(wgl_ctx->ctx, renderbuffer, &error);
915 assert(!!wgl_img->img == (error == STW_IMAGE_ERROR_SUCCESS));
916
917 if (!wgl_img->img) {
918 free(wgl_img);
919 _eglError(egl_error_from_stw_image_error(error), "wgl_create_image_khr");
920 return EGL_NO_IMAGE_KHR;
921 }
922
923 return &wgl_img->base;
924 }
925
926 static _EGLImage *
927 wgl_create_image_khr(_EGLDisplay *disp, _EGLContext *ctx, EGLenum target,
928 EGLClientBuffer buffer, const EGLint *attr_list)
929 {
930 switch (target) {
931 case EGL_GL_TEXTURE_2D_KHR:
932 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_X_KHR:
933 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_X_KHR:
934 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_Y_KHR:
935 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_KHR:
936 case EGL_GL_TEXTURE_CUBE_MAP_POSITIVE_Z_KHR:
937 case EGL_GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_KHR:
938 case EGL_GL_TEXTURE_3D_KHR:
939 return wgl_create_image_khr_texture(disp, ctx, target, buffer, attr_list);
940 case EGL_GL_RENDERBUFFER_KHR:
941 return wgl_create_image_khr_renderbuffer(disp, ctx, buffer, attr_list);
942 default:
943 _eglError(EGL_BAD_PARAMETER, "wgl_create_image_khr");
944 return EGL_NO_IMAGE_KHR;
945 }
946 }
947
948 static EGLBoolean
949 wgl_destroy_image_khr(_EGLDisplay *disp, _EGLImage *img)
950 {
951 struct wgl_egl_image *wgl_img = wgl_egl_image(img);
952 stw_destroy_image(wgl_img->img);
953 free(wgl_img);
954 return EGL_TRUE;
955 }
956
957 static _EGLSync *
958 wgl_create_sync_khr(_EGLDisplay *disp, EGLenum type,
959 const EGLAttrib *attrib_list)
960 {
961
962 _EGLContext *ctx = _eglGetCurrentContext();
963 struct wgl_egl_context *wgl_ctx = wgl_egl_context(ctx);
964 struct wgl_egl_sync *wgl_sync;
965
966 struct st_context *st = wgl_ctx ? wgl_ctx->ctx->st : NULL;
967
968 wgl_sync = calloc(1, sizeof(struct wgl_egl_sync));
969 if (!wgl_sync) {
970 _eglError(EGL_BAD_ALLOC, "eglCreateSyncKHR");
971 return NULL;
972 }
973
974 if (!_eglInitSync(&wgl_sync->base, disp, type, attrib_list)) {
975 free(wgl_sync);
976 return NULL;
977 }
978
979 switch (type) {
980 case EGL_SYNC_FENCE_KHR:
981 st_context_flush(st, 0, &wgl_sync->fence, NULL, NULL);
982 if (!wgl_sync->fence) {
983 _eglError(EGL_BAD_ALLOC, "eglCreateSyncKHR");
984 free(wgl_sync);
985 return NULL;
986 }
987 break;
988
989 case EGL_SYNC_REUSABLE_KHR:
990 wgl_sync->event = CreateEvent(NULL, true, false, NULL);
991 if (!wgl_sync->event) {
992 _eglError(EGL_BAD_ALLOC, "eglCreateSyncKHR");
993 free(wgl_sync);
994 return NULL;
995 }
996 }
997
998 wgl_sync->refcount = 1;
999 return &wgl_sync->base;
1000 }
1001
1002 static void
1003 wgl_egl_unref_sync(struct wgl_egl_display *wgl_dpy,
1004 struct wgl_egl_sync *wgl_sync)
1005 {
1006 if (InterlockedDecrement((volatile LONG *)&wgl_sync->refcount) > 0)
1007 return;
1008
1009 if (wgl_sync->fence)
1010 wgl_dpy->screen->fence_reference(wgl_dpy->screen, &wgl_sync->fence, NULL);
1011 if (wgl_sync->event)
1012 CloseHandle(wgl_sync->event);
1013 free(wgl_sync);
1014 }
1015
1016 static EGLBoolean
1017 wgl_destroy_sync_khr(_EGLDisplay *disp, _EGLSync *sync)
1018 {
1019 struct wgl_egl_display *wgl_dpy = wgl_egl_display(disp);
1020 struct wgl_egl_sync *wgl_sync = wgl_egl_sync(sync);
1021 wgl_egl_unref_sync(wgl_dpy, wgl_sync);
1022 return EGL_TRUE;
1023 }
1024
1025 static EGLint
1026 wgl_client_wait_sync_khr(_EGLDisplay *disp, _EGLSync *sync, EGLint flags,
1027 EGLTime timeout)
1028 {
1029 _EGLContext *ctx = _eglGetCurrentContext();
1030 struct wgl_egl_display *wgl_dpy = wgl_egl_display(disp);
1031 struct wgl_egl_context *wgl_ctx = wgl_egl_context(ctx);
1032 struct wgl_egl_sync *wgl_sync = wgl_egl_sync(sync);
1033
1034 EGLint ret = EGL_CONDITION_SATISFIED_KHR;
1035
1036 /* the sync object should take a reference while waiting */
1037 InterlockedIncrement((volatile LONG *)&wgl_sync->refcount);
1038
1039 switch (sync->Type) {
1040 case EGL_SYNC_FENCE_KHR:
1041 if (wgl_dpy->screen->fence_finish(wgl_dpy->screen, NULL, wgl_sync->fence,
1042 timeout))
1043 wgl_sync->base.SyncStatus = EGL_SIGNALED_KHR;
1044 else
1045 ret = EGL_TIMEOUT_EXPIRED_KHR;
1046 break;
1047
1048 case EGL_SYNC_REUSABLE_KHR:
1049 if (wgl_ctx && wgl_sync->base.SyncStatus == EGL_UNSIGNALED_KHR &&
1050 (flags & EGL_SYNC_FLUSH_COMMANDS_BIT_KHR)) {
1051 /* flush context if EGL_SYNC_FLUSH_COMMANDS_BIT_KHR is set */
1052 wgl_gl_flush();
1053 }
1054
1055 DWORD wait_milliseconds = (timeout == EGL_FOREVER_KHR)
1056 ? INFINITE
1057 : (DWORD)(timeout / 1000000ull);
1058 DWORD wait_ret = WaitForSingleObject(wgl_sync->event, wait_milliseconds);
1059 switch (wait_ret) {
1060 case WAIT_OBJECT_0:
1061 assert(wgl_sync->base.SyncStatus == EGL_SIGNALED_KHR);
1062 break;
1063 case WAIT_TIMEOUT:
1064 assert(wgl_sync->base.SyncStatus == EGL_UNSIGNALED_KHR);
1065 ret = EGL_TIMEOUT_EXPIRED_KHR;
1066 break;
1067 default:
1068 _eglError(EGL_BAD_ACCESS, "eglClientWaitSyncKHR");
1069 ret = EGL_FALSE;
1070 break;
1071 }
1072 break;
1073 }
1074 wgl_egl_unref_sync(wgl_dpy, wgl_sync);
1075
1076 return ret;
1077 }
1078
1079 static EGLint
1080 wgl_wait_sync_khr(_EGLDisplay *disp, _EGLSync *sync)
1081 {
1082 _EGLContext *ctx = _eglGetCurrentContext();
1083 struct wgl_egl_context *wgl_ctx = wgl_egl_context(ctx);
1084 struct wgl_egl_sync *wgl_sync = wgl_egl_sync(sync);
1085
1086 if (!wgl_sync->fence)
1087 return EGL_TRUE;
1088
1089 struct pipe_context *pipe = wgl_ctx->ctx->st->pipe;
1090 if (pipe->fence_server_sync)
1091 pipe->fence_server_sync(pipe, wgl_sync->fence, 0);
1092
1093 return EGL_TRUE;
1094 }
1095
1096 static EGLBoolean
1097 wgl_signal_sync_khr(_EGLDisplay *disp, _EGLSync *sync, EGLenum mode)
1098 {
1099 struct wgl_egl_sync *wgl_sync = wgl_egl_sync(sync);
1100
1101 if (sync->Type != EGL_SYNC_REUSABLE_KHR)
1102 return _eglError(EGL_BAD_MATCH, "eglSignalSyncKHR");
1103
1104 if (mode != EGL_SIGNALED_KHR && mode != EGL_UNSIGNALED_KHR)
1105 return _eglError(EGL_BAD_ATTRIBUTE, "eglSignalSyncKHR");
1106
1107 wgl_sync->base.SyncStatus = mode;
1108
1109 if (mode == EGL_SIGNALED_KHR) {
1110 if (!SetEvent(wgl_sync->event))
1111 return _eglError(EGL_BAD_ACCESS, "eglSignalSyncKHR");
1112 } else {
1113 if (!ResetEvent(wgl_sync->event))
1114 return _eglError(EGL_BAD_ACCESS, "eglSignalSyncKHR");
1115 }
1116
1117 return EGL_TRUE;
1118 }
1119
1120 static const char *
1121 wgl_query_driver_name(_EGLDisplay *disp)
1122 {
1123 return stw_get_device()->stw_winsys->get_name();
1124 }
1125
1126 static char *
1127 wgl_query_driver_config(_EGLDisplay *disp)
1128 {
1129 return stw_get_config_xml();
1130 }
1131
1132 static int
1133 wgl_interop_query_device_info(_EGLDisplay *disp, _EGLContext *ctx,
1134 struct mesa_glinterop_device_info *out)
1135 {
1136 struct wgl_egl_context *wgl_ctx = wgl_egl_context(ctx);
1137 return stw_interop_query_device_info(wgl_ctx->ctx, out);
1138 }
1139
1140 static int
1141 wgl_interop_export_object(_EGLDisplay *disp, _EGLContext *ctx,
1142 struct mesa_glinterop_export_in *in,
1143 struct mesa_glinterop_export_out *out)
1144 {
1145 struct wgl_egl_context *wgl_ctx = wgl_egl_context(ctx);
1146 return stw_interop_export_object(wgl_ctx->ctx, in, out);
1147 }
1148
1149 static int
1150 wgl_interop_flush_objects(_EGLDisplay *disp, _EGLContext *ctx, unsigned count,
1151 struct mesa_glinterop_export_in *objects,
1152 struct mesa_glinterop_flush_out *out)
1153 {
1154 struct wgl_egl_context *wgl_ctx = wgl_egl_context(ctx);
1155 return stw_interop_flush_objects(wgl_ctx->ctx, count, objects, out);
1156 }
1157
1158 struct _egl_driver _eglDriver = {
1159 .Initialize = wgl_initialize,
1160 .Terminate = wgl_terminate,
1161 .CreateContext = wgl_create_context,
1162 .DestroyContext = wgl_destroy_context,
1163 .MakeCurrent = wgl_make_current,
1164 .CreateWindowSurface = wgl_create_window_surface,
1165 .CreatePbufferSurface = wgl_create_pbuffer_surface,
1166 .DestroySurface = wgl_destroy_surface,
1167 .QuerySurface = wgl_query_surface,
1168 .BindTexImage = wgl_bind_tex_image,
1169 .ReleaseTexImage = _eglReleaseTexImage,
1170 .SwapInterval = wgl_swap_interval,
1171 .SwapBuffers = wgl_swap_buffers,
1172 .WaitClient = wgl_wait_client,
1173 .WaitNative = wgl_wait_native,
1174 .CreateImageKHR = wgl_create_image_khr,
1175 .DestroyImageKHR = wgl_destroy_image_khr,
1176 .CreateSyncKHR = wgl_create_sync_khr,
1177 .DestroySyncKHR = wgl_destroy_sync_khr,
1178 .ClientWaitSyncKHR = wgl_client_wait_sync_khr,
1179 .WaitSyncKHR = wgl_wait_sync_khr,
1180 .SignalSyncKHR = wgl_signal_sync_khr,
1181 .QueryDriverName = wgl_query_driver_name,
1182 .QueryDriverConfig = wgl_query_driver_config,
1183 .GLInteropQueryDeviceInfo = wgl_interop_query_device_info,
1184 .GLInteropExportObject = wgl_interop_export_object,
1185 .GLInteropFlushObjects = wgl_interop_flush_objects,
1186 };