Cairo-1.10.2 (32/64 bit)

Headers, Bindings, Libraries for use with FreeBASIC, Please include example of use to help ensure they are tested and usable.
TJF
Posts: 3809
Joined: Dec 06, 2009 22:27
Location: N47°, E15°
Contact:

Cairo-1.10.2 (32/64 bit)

Post by TJF »

Image

Cairo is a library used to provide a vector graphics-based, device-independent API for software developers. It is designed to provide primitives for 2-dimensional drawing across a number of different backends. Cairo is designed to use hardware acceleration when available.

Cairo is free and open-source software subject to the terms of dual licensing, under the GNU Lesser General Public License (LGPL) and the Mozilla Public License (MPL).

This header set is ready for 32/64 bit compiling and can replace the original headers shipped with the fbc packages.
caseih
Posts: 2157
Joined: Feb 26, 2007 5:32

Re: Cairo-1.10.2 (32/64 bit)

Post by caseih »

So when I try to compile and run the cairo examples from the 32-bit FreeBASIC distribution with your headers on my 64-bit system and compiler, I get a seg fault in cairo_paint(). Perhaps something still isn't quite right with the new headers on 64-bit systems?

For example:

Code: Select all

'' Cairo clock. Translated from the C example written by Writser Cleveringa
'' Details: http://cairographics.org/documentation/

#include once "cairo/cairo.bi"
#include once "datetime.bi"

Const SCREEN_W = 400
Const SCREEN_H = 300

Const PI = 3.14159265358979323846#

ScreenRes SCREEN_W, SCREEN_H, 32

''
'' Create a cairo drawing context, using the FB screen as surface.
''
Dim As cairo_surface_t Ptr surface = _
	cairo_image_surface_create_for_data(ScreenPtr(), _
		CAIRO_FORMAT_ARGB32, SCREEN_W, SCREEN_H, _
		SCREEN_W * SizeOf(Integer))

Dim As cairo_t Ptr c = cairo_create(surface)
cairo_scale(c, SCREEN_W, SCREEN_H)

Do
	ScreenLock()

	''
	'' Draw a clock
	''

	'' compute the angles for the indicators of our clock
	Dim As Double minutes = Minute(Now()) * PI / 30
	Dim As Double hours = Hour(Now()) * PI / 6
	Dim As Double seconds = Second(Now()) * PI / 30

	'' Draw the entire context white.
	cairo_set_source_rgba(c, 1, 1, 1, 1)
	cairo_paint(c)

	'' Who doesn't want all those nice line settings :)
	cairo_set_line_cap(c, CAIRO_LINE_CAP_ROUND)
	cairo_set_line_width(c, 0.1)

	'' Translate to the center of the rendering context,
	'' and draw a black clock outline.
	cairo_set_source_rgba(c, 0, 0, 0, 1)
	cairo_translate(c, 0.5, 0.5)
	cairo_arc(c, 0, 0, 0.4, 0, PI * 2)
	cairo_stroke(c)

	'' Draw a white dot on the current second.
	cairo_set_source_rgba(c, 1, 1, 1, 0.6)
	cairo_arc(c, Sin(seconds) * 0.4, -Cos(seconds) * 0.4, 0.05, 0, PI * 2)
	cairo_fill(c)

	'' Draw the minutes indicator
	cairo_set_source_rgba(c, 0.2, 0.2, 1, 0.6)
	cairo_move_to(c, 0, 0)
	cairo_line_to(c, Sin(minutes) * 0.4, -Cos(minutes) * 0.4)
	cairo_stroke(c)

	'' Draw the hours indicator
	cairo_move_to(c, 0, 0)
	cairo_line_to(c, Sin(hours) * 0.2, -Cos(hours) * 0.2)
	cairo_stroke(c)

	ScreenUnlock()

	Sleep 1000
Loop While (Len(Inkey()) = 0)

'' Clean up the cairo context
cairo_destroy(c)
caseih
Posts: 2157
Joined: Feb 26, 2007 5:32

Re: Cairo-1.10.2 (32/64 bit)

Post by caseih »

Found the problem. The example file needs to be modified for 64-bit. When the surface is created, the width is defined in terms of sizeof(Integer), which on 64-bit systems is 8. Because the buffer is RGBA (32-bit), it needs to be in terms of size 4, which on linux is LONG.
TJF
Posts: 3809
Joined: Dec 06, 2009 22:27
Location: N47°, E15°
Contact:

Re: Cairo-1.10.2 (32/64 bit)

Post by TJF »

Could you provide the modified example code here?

Or I can add your code to the header package, if you like.
caseih
Posts: 2157
Joined: Feb 26, 2007 5:32

Re: Cairo-1.10.2 (32/64 bit)

Post by caseih »

Sure. Basically this is the faulty line in clock.bas:

Code: Select all

Dim As cairo_surface_t Ptr surface = _
    cairo_image_surface_create_for_data(ScreenPtr(), _
        CAIRO_FORMAT_ARGB32, SCREEN_W, SCREEN_H, _
        SCREEN_W * SizeOf(Integer))
The buffer is being created with SCREEN_W specifying the width in pixels multiplied by Sizeof(Integer), which works on 32-bit systems, since Integer is 32-bits. On 64-bit Linux (but not necessarily Windows), Integer is 8, hence the surface thinks it's bigger than the actual screen, and so we get a buffer overflow when the surface is rendered to the screen. The solution is to replace SizeOf(Integer) with 4. I hate magic numbers, but in this case, that's appropriate. 4 Bytes per pixel (since it's CAIRO_FORMAT_ARGB32).

There's one other bug in clock.bas too, but I haven't found or fixed it yet. Basically every time the clock is redrawn it moves off the screen a ways.

Hope this makes sense. All the Cairo example files need to have the Sizeof(Integer) stuff replaced with 4.
TJF
Posts: 3809
Joined: Dec 06, 2009 22:27
Location: N47°, E15°
Contact:

Re: Cairo-1.10.2 (32/64 bit)

Post by TJF »

caseih wrote:There's one other bug in clock.bas too, but I haven't found or fixed it yet. Basically every time the clock is redrawn it moves off the screen a ways.
That's an old bug. The function call to cairo_translate() has to get moved before the DO ... LOOP.

I reviewed the examples and included them in the package.
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Cairo-1.10.2 (32/64 bit)

Post by MrSwiss »

Gentlemen,
I'd like to propose a different way to fix the above issue, without any "magic number":
just use SIZEOF(LONG) -- instead of SIZEOF(INTEGER)
since: LONG is the FB-builtin 4-byte INT TYPE (fixed, NOT polymorphic) and therefore
OS independant.
TJF
Posts: 3809
Joined: Dec 06, 2009 22:27
Location: N47°, E15°
Contact:

Re: Cairo-1.10.2 (32/64 bit)

Post by TJF »

This 'magic number' isn't related to the size of any FB type. Instead it's the number of bytes for a pixel. Four bytes are used for RGBA (red, green, blue, alpha).
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Cairo-1.10.2 (32/64 bit)

Post by MrSwiss »

TJF wrote:This 'magic number' isn't related to the size of any FB type. Instead it's the number of bytes for a pixel. Four bytes are used for RGBA (red, green, blue, alpha).
Well you've used SizeOf(Interger) --- used to be 4 Bytes (before polymorphing issues)
Result of SizeOf(Long) returns the needed 4 (Bytes) ... Brauchst nicht zu "schulmeistern".
TJF
Posts: 3809
Joined: Dec 06, 2009 22:27
Location: N47°, E15°
Contact:

Re: Cairo-1.10.2 (32/64 bit)

Post by TJF »

MrSwiss wrote:Well you've used SizeOf(Interger) --- used to be 4 Bytes (before polymorphing issues)
Result of SizeOf(Long) returns the needed 4 (Bytes) ... Brauchst nicht zu "schulmeistern".
I haven't used anything. This example is from Writser Cleveringa, as you can see in the file header ;-) (Later I just fixed the cairo_translate() issue in the code in my package.)
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Cairo-1.10.2 (32/64 bit)

Post by MrSwiss »

OK, after having settled the issue above ;-) , there is something that I personally find
difficult to understand:
- header's (can be got)
- binary's (dito)
what about the import lib's ... e.g. libcairo.dll.a , the only "other way" is compile your-
self, which is difficult without a C-Compiler (to many downloads etc. to just test a library
as far as I'm concerned)
TJF
Posts: 3809
Joined: Dec 06, 2009 22:27
Location: N47°, E15°
Contact:

Re: Cairo-1.10.2 (32/64 bit)

Post by TJF »

That's no issue on LINUX systems. A library gets installed or purged as any other package.

You want it the hard way, so take it ;-)

In case of cairo it's convenient to install the binaries by the GTK+ installers (you'll get Pango and PangoCairo as well):
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Cairo-1.10.2 (32/64 bit)

Post by MrSwiss »

Like I've pointed out before:
I just wanted to test the library, NOT spend my time, resolving dependencies ...
It may be simple on LUNIX, it is NOT on any "other" system.
(reason: the .sh installers are useless)

I'll go looking at something that is a lot less painfull to use like FLTK!
(advantage: statically linked in dependent lib's)

This exercise was for absolutely nothing, just a big waste of time ...
TJF
Posts: 3809
Joined: Dec 06, 2009 22:27
Location: N47°, E15°
Contact:

Re: Cairo-1.10.2 (32/64 bit)

Post by TJF »

Installing libraies isn't a trivial issue, beginners may be overhelmed. That's why I pointed to the bundle installers that do all the work for you. Just
  • Download the matching version (32 / 64 bit).
  • Unpack the archive (you'll get an .exe file installer).
  • Doubleklick the .exe file and answer the questions (or just klick OK in the GUI for default configuration).
  • When compiling, tell the fbc linker where to find the binaries (ie. when you install in C:\opt\ then either add this to your path, or use -l"C:/opt/bin" in the command line, or use #LIBPATH "C:/opt/bin" in your code).
That's all!

Note:

cairo is a grafics library (not a GUI toolkit like FLTK).
dodicat
Posts: 7983
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Cairo-1.10.2 (32/64 bit)

Post by dodicat »

Here are the necessary Cairo dll's and a couple of test files.bas.

32 bit (I don't have 64 bit here, but I will update it when I advance to 64 bit).

Just run the code from inside the folder, no need to blitz your system folders with dll's.

You can make your own code snippets, or projects in that same folder.
I use Win XP here, not sure if Linux or Win 8 will work, maybe you will have to disperse the dll's, but it is worth a try.

I have gone off Cairo personally, the 2d graphics are a very slow compared to FreeBASIC raw graphics, or OpenGL graphics.
Here's the link:
http://www.mediafire.com/download/ui41t ... /Cairo.zip
Post Reply