Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a cairo-gl surface under Gtk

Tags:

cairo

gtk3

I want to create a window using GTK 3 under linux (fedora 12) and draw on it a simple rectangle using cairo-gl backend, for this I want to create a cairo-gl surface. How can I do this, can anybody help me with a sample code.

Thanks in advance.

RC7

like image 629
RC7 Avatar asked May 24 '11 05:05

RC7


People also ask

Does Cairo use OpenGL?

The Cairo GL backend allows hardware-accelerated rendering by targeting the OpenGL® ES API.

Does Cairo use GPU?

Cairo is a high-level canvas drawing model for laying out pages and other views. The base elements are resolution independent paths and patterns. Cairo can translate that canvas model into GPU primitives using OpenGL (among others), but the canvas model does not often translate efficiently to the GPU.


1 Answers

Well, first of all you need to make sure your cairo version has gl support. I'm not sure the version in Fedora 12 has it. You may have to compile your own cairo.

Then you need to look at cairo-gl.h: http://cgit.freedesktop.org/cairo/tree/src/cairo-gl.h

This file has a nice comment explaining that first you need to create a cairo_device using your gl context and then pass it to cairo_gl_surface_create().

This file from the cairo test suite contains an example of a gl surface: http://cgit.freedesktop.org/cairo/tree/test/gl-surface-source.c

Notice that it is a .c file that includes another .c file: http://cgit.freedesktop.org/cairo/tree/test/set-source.c

The test suite is full of cairo programming examples. Use it! :)

I'm not sure how you do GL using GTK3, but in the worst case, you can use GDK to get X11's Display and Window IDs, then you use standard glx calls: http://developer.gnome.org/gdk3/stable/gdk3-X-Window-System-Interaction.html

And to use cairo with GTK3, you need to look at this: http://developer.gnome.org/gdk3/stable/gdk3-Cairo-Interaction.html And this: http://zetcode.com/tutorials/cairographicstutorial/

I gave you a bunch of Lego bricks. Now you need to assemble them. (Or maybe someone with more time will do it and write a better answer here?)

like image 190
pzanoni Avatar answered Oct 01 '22 20:10

pzanoni