Beispiel #1
0
static gboolean
_cogl_winsys_display_setup (CoglDisplay *display,
                            GError **error)
{
  CoglDisplayEGL *egl_display;
  CoglRenderer *renderer = display->renderer;
  CoglRendererEGL *egl_renderer = renderer->winsys;

  _COGL_RETURN_VAL_IF_FAIL (display->winsys == NULL, FALSE);

  egl_display = g_slice_new0 (CoglDisplayEGL);
  display->winsys = egl_display;

#ifdef COGL_HAS_WAYLAND_EGL_SERVER_SUPPORT
  if (display->wayland_compositor_display)
    {
      struct wl_display *wayland_display = display->wayland_compositor_display;
      CoglRendererEGL *egl_renderer = display->renderer->winsys;

      egl_renderer->pf_eglBindWaylandDisplay (egl_renderer->edpy,
                                              wayland_display);
    }
#endif

  if (egl_renderer->platform_vtable->display_setup &&
      !egl_renderer->platform_vtable->display_setup (display, error))
    goto error;

  if (!create_context (display, error))
    goto error;

  egl_display->found_egl_config = TRUE;

  return TRUE;

error:
  _cogl_winsys_display_destroy (display);
  return FALSE;
}
static CoglBool
_cogl_winsys_display_setup (CoglDisplay *display,
                            CoglError **error)
{
  CoglDisplaySdl2 *sdl_display;
  const char * (* get_string_func) (GLenum name);
  const char *gl_version;

  _COGL_RETURN_VAL_IF_FAIL (display->winsys == NULL, FALSE);

  sdl_display = g_slice_new0 (CoglDisplaySdl2);
  display->winsys = sdl_display;

  set_gl_attribs_from_framebuffer_config (&display->onscreen_template->config);

  if (display->renderer->driver == COGL_DRIVER_GLES1)
    SDL_GL_SetAttribute (SDL_GL_CONTEXT_MAJOR_VERSION, 1);
  else if (display->renderer->driver == COGL_DRIVER_GLES2)
    SDL_GL_SetAttribute (SDL_GL_CONTEXT_MAJOR_VERSION, 2);
  else if (display->renderer->driver == COGL_DRIVER_GL3)
    {
      SDL_GL_SetAttribute (SDL_GL_CONTEXT_MAJOR_VERSION, 3);
      SDL_GL_SetAttribute (SDL_GL_CONTEXT_PROFILE_MASK,
                           SDL_GL_CONTEXT_PROFILE_CORE);
      SDL_GL_SetAttribute (SDL_GL_CONTEXT_FLAGS,
                           SDL_GL_CONTEXT_FORWARD_COMPATIBLE_FLAG);
    }

  /* Create a dummy 1x1 window that never gets display so that we can
   * create a GL context */
  sdl_display->dummy_window = SDL_CreateWindow ("",
                                                0, 0, /* x/y */
                                                1, 1, /* w/h */
                                                SDL_WINDOW_OPENGL |
                                                SDL_WINDOW_HIDDEN);
  if (sdl_display->dummy_window == NULL)
    {
      _cogl_set_error (error, COGL_WINSYS_ERROR,
                       COGL_WINSYS_ERROR_INIT,
                       "SDL_CreateWindow failed: %s",
                       SDL_GetError ());
      goto error;
    }

  sdl_display->context = SDL_GL_CreateContext (sdl_display->dummy_window);

  if (sdl_display->context == NULL)
    {
      _cogl_set_error (error, COGL_WINSYS_ERROR,
                       COGL_WINSYS_ERROR_INIT,
                       "SDL_GL_CreateContext failed: %s",
                       SDL_GetError ());
      goto error;
    }

  /* SDL doesn't seem to provide a way to select between GL and GLES
   * and instead it will just pick one itself. We can at least try to
   * verify that it picked the one we were expecting by looking at the
   * GL version string */
  get_string_func = SDL_GL_GetProcAddress ("glGetString");
  gl_version = get_string_func (GL_VERSION);

  switch (display->renderer->driver)
    {
    case COGL_DRIVER_GL:
    case COGL_DRIVER_GL3:
      /* The first character of the version string will be a digit if
       * it's normal GL */
      if (!g_ascii_isdigit (gl_version[0]))
        {
          _cogl_set_error (error, COGL_WINSYS_ERROR,
                           COGL_WINSYS_ERROR_INIT,
                           "The GL driver was requested but SDL is using GLES");
          goto error;
        }

      if (display->renderer->driver == COGL_DRIVER_GL3 &&
          gl_version[0] < '3')
        {
          _cogl_set_error (error, COGL_WINSYS_ERROR,
                           COGL_WINSYS_ERROR_INIT,
                           "The GL3 driver was requested but SDL is using "
                           "GL %c", gl_version[0]);
          goto error;
        }
      break;

    case COGL_DRIVER_GLES2:
      if (!g_str_has_prefix (gl_version, "OpenGL ES 2"))
        {
          _cogl_set_error (error, COGL_WINSYS_ERROR,
                           COGL_WINSYS_ERROR_INIT,
                           "The GLES2 driver was requested but SDL is "
                           "not using GLES2");
          goto error;
        }
      break;

    case COGL_DRIVER_GLES1:
      if (!g_str_has_prefix (gl_version, "OpenGL ES 1"))
        {
          _cogl_set_error (error, COGL_WINSYS_ERROR,
                           COGL_WINSYS_ERROR_INIT,
                           "The GLES1 driver was requested but SDL is "
                           "not using GLES1");
          goto error;
        }
      break;

    default:
      g_assert_not_reached ();
    }

  return TRUE;

error:
  _cogl_winsys_display_destroy (display);
  return FALSE;
}