void CairoGLRenderer::InitDemo(HWND hWnd, HDC hdc) { RECT rect; ::GetClientRect(hWnd, &rect); m_hdc = hdc; m_hglrc = wglCreateContext(hdc); long width = rect.right - rect.left; long height = rect.bottom - rect.top; m_device = cairo_wgl_device_create(m_hglrc); if (cairo_device_status(m_device) != CAIRO_STATUS_SUCCESS) printf("cairo device failed with %s\n", cairo_status_to_string(cairo_device_status(m_device))); m_surface = cairo_gl_surface_create_for_dc(m_device, m_hdc, width, height); if (cairo_surface_status(m_surface) != CAIRO_STATUS_SUCCESS) { const char* error = cairo_status_to_string(cairo_surface_status(m_surface)); fprintf(stderr, "cairo surface failed with %s\n", error); } m_cr = cairo_create (m_surface); if (cairo_status(m_cr) != CAIRO_STATUS_SUCCESS) printf("cairo failed with %s\n", cairo_status_to_string(cairo_status(m_cr))); }
static gboolean gdk_display_init_egl(GdkDisplay *display) { GdkDisplayWayland *display_wayland = GDK_DISPLAY_WAYLAND (display); EGLint major, minor, i; void *p; static const struct { const char *f; unsigned int offset; } extension_functions[] = { { "glEGLImageTargetTexture2DOES", offsetof(GdkDisplayWayland, image_target_texture_2d) }, { "eglCreateImageKHR", offsetof(GdkDisplayWayland, create_image) }, { "eglDestroyImageKHR", offsetof(GdkDisplayWayland, destroy_image) } }; display_wayland->egl_display = eglGetDisplay(display_wayland->wl_display); if (!eglInitialize(display_wayland->egl_display, &major, &minor)) { fprintf(stderr, "failed to initialize display\n"); return FALSE; } eglBindAPI(EGL_OPENGL_API); display_wayland->egl_context = eglCreateContext(display_wayland->egl_display, NULL, EGL_NO_CONTEXT, NULL); if (display_wayland->egl_context == NULL) { fprintf(stderr, "failed to create context\n"); return FALSE; } if (!eglMakeCurrent(display_wayland->egl_display, NULL, NULL, display_wayland->egl_context)) { fprintf(stderr, "faile to make context current\n"); return FALSE; } display_wayland->cairo_device = cairo_egl_device_create(display_wayland->egl_display, display_wayland->egl_context); if (cairo_device_status (display_wayland->cairo_device) != CAIRO_STATUS_SUCCESS) { fprintf(stderr, "failed to get cairo drm device\n"); return FALSE; } for (i = 0; i < G_N_ELEMENTS(extension_functions); i++) { p = eglGetProcAddress(extension_functions[i].f); *(void **) ((char *) display_wayland + extension_functions[i].offset) = p; if (p == NULL) { fprintf(stderr, "failed to look up %s\n", extension_functions[i].f); return FALSE; } } return TRUE; }
static inline void cr_device_check_status (cairo_device_t *device) { rb_cairo_check_status (cairo_device_status (device)); }
static MechEGLConfigPrivate * _create_egl_config (gint renderable_type) { EGLint major, minor, n_configs; MechBackendWayland *backend; MechEGLConfigPrivate *priv; const gchar *extensions; MechEGLConfig *config; GError *error; EGLenum api; const EGLint argb_config_attributes[] = { EGL_SURFACE_TYPE, EGL_WINDOW_BIT, EGL_RED_SIZE, 8, EGL_GREEN_SIZE, 8, EGL_BLUE_SIZE, 8, EGL_ALPHA_SIZE, 8, EGL_DEPTH_SIZE, 24, EGL_RENDERABLE_TYPE, renderable_type, EGL_NONE }; priv = g_new0 (MechEGLConfigPrivate, 1); config = (MechEGLConfig *) priv; if (renderable_type != EGL_OPENGL_BIT && renderable_type != EGL_OPENGL_ES_BIT && renderable_type != EGL_OPENGL_ES2_BIT) { error = g_error_new (mech_surface_egl_error_quark (), 0, "Not a valid renderable type"); goto init_failed; } backend = _mech_backend_wayland_get (); config->egl_display = eglGetDisplay (backend->wl_display); if (eglInitialize (config->egl_display, &major, &minor) == EGL_FALSE) { error = g_error_new (mech_surface_egl_error_quark (), 0, "Could not initialize EGL"); goto init_failed; } api = (renderable_type == EGL_OPENGL_BIT) ? EGL_OPENGL_API : EGL_OPENGL_ES_API; if (eglBindAPI (api) == EGL_FALSE) { error = g_error_new (mech_surface_egl_error_quark (), 0, "Could not bind a rendering API"); goto init_failed; } if (eglChooseConfig (config->egl_display, argb_config_attributes, &config->egl_argb_config, 1, &n_configs) == EGL_FALSE) { error = g_error_new (mech_surface_egl_error_quark (), 0, "Could not find a matching configuration"); goto init_failed; } config->egl_argb_context = eglCreateContext (config->egl_display, config->egl_argb_config, EGL_NO_CONTEXT, NULL); if (config->egl_argb_context == EGL_NO_CONTEXT) { error = g_error_new (mech_surface_egl_error_quark (), 0, "Could not create an EGL context"); goto init_failed; } if (eglMakeCurrent (config->egl_display, EGL_NO_SURFACE, EGL_NO_SURFACE, config->egl_argb_context) == EGL_FALSE) { error = g_error_new (mech_surface_egl_error_quark (), 0, "Could not make EGL context current"); goto init_failed; } config->argb_device = cairo_egl_device_create (config->egl_display, config->egl_argb_context); if (cairo_device_status (config->argb_device) != CAIRO_STATUS_SUCCESS) { error = g_error_new (mech_surface_egl_error_quark (), 0, "Could not create a cairo EGL device"); goto init_failed; } extensions = eglQueryString (config->egl_display, EGL_EXTENSIONS); if (strstr (extensions, "EGL_EXT_buffer_age")) config->has_buffer_age_ext = TRUE; if (strstr (extensions, "EGL_EXT_swap_buffers_with_damage")) config->has_swap_buffers_with_damage_ext = TRUE; return priv; init_failed: if (config->egl_argb_context != EGL_NO_CONTEXT) { eglDestroyContext (config->egl_display, config->egl_argb_context); config->egl_argb_context = EGL_NO_CONTEXT; } if (config->argb_device) { cairo_device_destroy (config->argb_device); config->argb_device = NULL; } priv->error = error; return priv; }