Bug 53121 - My program runs successfully with Cairo-1.10.2, but getting SIGSEGV with 1.12.2.
My program runs successfully with Cairo-1.10.2, but getting SIGSEGV with 1.12.2.
Status: RESOLVED FIXED
Product: cairo
Classification: Unclassified
Component: win32 backend
1.12.2
x86 (IA32) Windows (All)
: medium blocker
Assigned To: cairo-bugs mailing list
cairo-bugs mailing list
:
Depends on:
Blocks: cairo-1.14
  Show dependency treegraph
 
Reported: 2012-08-04 14:08 UTC by Hakki Dogusan
Modified: 2015-02-03 00:44 UTC (History)
0 users

See Also:
i915 platform:
i915 features:


Attachments
quick hack (1.49 KB, patch)
2013-09-12 04:27 UTC, Massimo
Details | Splinter Review

Note You need to log in before you can comment on or make changes to this bug.
Description Hakki Dogusan 2012-08-04 14:08:28 UTC
My environment: (WinXP, MinGW (gcc-4.6.2), Cairo-1.12.2, Pixman-0.26.0)

Test program follows:

#include <windows.h>
#include <math.h>
#include <cairo.h>
//#if CAIRO_HAS_XLIB_SURFACE
//    #include <X11/Xlib.h>
//    #include <gtk/gtk.h>
//    #include <gdk/gdk.h>
//#endif
#if CAIRO_HAS_SVG_SURFACE
    #include <cairo-svg.h>
#endif
#if CAIRO_HAS_PDF_SURFACE
    #include <cairo-pdf.h>
#endif
#if CAIRO_HAS_PS_SURFACE
    #include <cairo-ps.h>
#endif
#if CAIRO_HAS_XCB_SURFACE
    #include <cairo-xcb.h>
#endif
#if CAIRO_HAS_XLIB_SURFACE
    #include <cairo-xlib.h>
#endif
#if CAIRO_HAS_WIN32_SURFACE
    #include <cairo-win32.h>
#endif
#if CAIRO_HAS_BEOS_SURFACE
    #include <cairo-beos.h>
#endif
#if CAIRO_HAS_DIRECTFB_SURFACE
    #include <cairo-directfb.h>
#endif
#if CAIRO_HAS_OS2_SURFACE
    #include <cairo-os2.h>
#endif
#if CAIRO_HAS_GLITZ_SURFACE
    #include <cairo-glitz.h>
#endif
#if CAIRO_HAS_QUARTZ_SURFACE
    #include <cairo-quartz.h>
#endif
#if CAIRO_HAS_ATSUI_FONT
    #include <cairo-atsui.h>
#endif
#if CAIRO_HAS_FT_FONT
    #include <cairo-ft.h>
#endif

/*  Declare Windows procedure  */
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);

/*  Make the class name into a global variable  */
const wchar_t* szClassName = L"CairoTest";

int WINAPI WinMain (HINSTANCE hThisInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR lpszArgument,
                     int nCmdShow)
{
    HWND hwnd;               /* This is the handle for our window */
    MSG messages;            /* Here messages to the application are saved */
    WNDCLASSEX wincl;        /* Data structure for the windowclass */

    /* The Window structure */
    wincl.hInstance = hThisInstance;
    wincl.lpszClassName = szClassName;
    wincl.lpfnWndProc = WindowProcedure;      /* This function is called by windows */
    wincl.style = CS_DBLCLKS;                 /* Catch double-clicks */
    wincl.cbSize = sizeof (WNDCLASSEX);

    /* Use default icon and mouse-pointer */
    wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
    wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
    wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
    wincl.lpszMenuName = NULL;                 /* No menu */
    wincl.cbClsExtra = 0;                      /* No extra bytes after the window class */
    wincl.cbWndExtra = 0;                      /* structure or the window instance */
    /* Use Windows's default colour as the background of the window */
    wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;

    /* Register the window class, and if it fails quit the program */
    if (!RegisterClassEx (&wincl))
        return 0;

    /* The class is registered, let's create the program*/
    hwnd = CreateWindowEx (
           0,                   /* Extended possibilites for variation */
           szClassName,         /* Classname */
           L"CairoTest",        /* Title Text */
           WS_OVERLAPPEDWINDOW, /* default window */
           CW_USEDEFAULT,       /* Windows decides the position */
           CW_USEDEFAULT,       /* where the window ends up on the screen */
           544,                 /* The programs width */
           375,                 /* and height in pixels */
           HWND_DESKTOP,        /* The window is a child-window to desktop */
           NULL,                /* No menu */
           hThisInstance,       /* Program Instance handler */
           NULL                 /* No Window Creation data */
           );

    /* Make the window visible on the screen */
    ShowWindow (hwnd, nCmdShow);

    /* Run the message loop. It will run until GetMessage() returns 0 */
    while (GetMessage (&messages, NULL, 0, 0))
    {
        /* Translate virtual-key messages into character messages */
        TranslateMessage(&messages);
        /* Send message to WindowProcedure */
        DispatchMessage(&messages);
    }

    /* The program return-value is 0 - The value that PostQuitMessage() gave */
    return messages.wParam;
}




static void
snap_point (cairo_t *cr, double *x, double *y, bool odd_integer_line_width = true)
{
    cairo_user_to_device (cr, x, y);
    if (odd_integer_line_width)
    {
        // round to nearest half integer
        *x = round (*x) + .5;
        *y = round (*y) + .5;
    }
    else
    {
        // round to nearest integer
        *x = round (*x);
        *y = round (*y);
    }
    cairo_device_to_user (cr, x, y);
}

static double
snapx(cairo_t* cr, double ax)
{
    double x = ax;
    double y = 0;
    snap_point(cr, &x, &y);
    return x;
}

static double
snapy(cairo_t* cr, double ay)
{
    double x = 0;
    double y = ay;
    snap_point(cr, &x, &y);
    return y;
}

static void
draw_grid_lines(cairo_t* cr, int maxx, int maxy, int step, double linewidth)
{
    for (int x = 0; x < maxx; x += step)
    {
        cairo_move_to (cr, snapx(cr,x), snapy(cr,0));
        cairo_line_to (cr, snapx(cr,x), snapy(cr,maxy));
        cairo_close_path(cr);
    }

    for (int y = 0; y < maxy; y += step)
    {
        cairo_move_to (cr, snapx(cr,0),    snapy(cr,y));
        cairo_line_to (cr, snapx(cr,maxx), snapy(cr,y));
        cairo_close_path(cr);
    }

  //cairo_set_source_rgb(cr, 0.84, 0.84, 0.84); //paper color: Dim Grey
    cairo_set_source_rgb(cr, 0.64, 0.64, 0.64); //?
    cairo_save(cr);
    {
        cairo_identity_matrix (cr);
        cairo_set_line_width (cr, linewidth);
        cairo_stroke (cr);
    }
    cairo_restore(cr);
}


static void
draw_grid(cairo_t* cr, int pw, int ph, int zoom)
{
    cairo_save(cr);
    {
        // line positions in mm
        if (0 < zoom && zoom <= 100)
        {
            draw_grid_lines(cr, pw, ph, 1000, .2);
        }
        else if (100 < zoom && zoom <= 250)
        {
            draw_grid_lines(cr, pw, ph, 1000, .2);
        }
        else if (250 < zoom)
        {
            draw_grid_lines(cr, pw, ph,  500, .2);
            draw_grid_lines(cr, pw, ph, 1000, .4);
        }
    }
    cairo_restore(cr);
}

static void
draw_paper(cairo_t* cr, double sf, int pw, int ph)
{
    double shadow = 3.0 / sf;

    cairo_save(cr);
    {
        cairo_rectangle (cr, shadow, shadow, pw, ph);
        cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
        cairo_fill(cr);
        cairo_rectangle (cr, 0, 0, pw, ph);
        cairo_set_source_rgb(cr, 0.84, 0.84, 0.84); //Dim Grey
        cairo_fill(cr);

    }
    cairo_restore(cr);
}


PAINTSTRUCT  ps;
HDC          hdc;

void dopainting(HDC hdc)
{
    cairo_t *cr = cairo_create(cairo_win32_surface_create( hdc ));
    // dark gray background
    cairo_set_source_rgb(cr, 0.5, 0.5, 0.5);
    cairo_paint(cr);

        draw_paper(cr, 1.0, 500, 500);
        draw_grid(cr, 500, 500, 100);

    cairo_destroy(cr);
}

/*  This function is called by the Windows function DispatchMessage()  */

LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)                  /* handle the messages */
    {
        case WM_DESTROY:
            PostQuitMessage (0);       /* send a WM_QUIT to the message queue */
            break;
        case WM_PAINT:
            hdc = BeginPaint(hwnd, &ps);
            dopainting(hdc);
            EndPaint(hwnd, &ps);
            ValidateRect(hwnd, NULL);
            break;
        default:                      /* for messages that we don't deal with */
            return DefWindowProc (hwnd, message, wParam, lParam);
    }

    return 0;
}
Comment 1 Massimo 2013-09-12 04:27:24 UTC
Created attachment 85688 [details] [review]
quick hack

This is a quick hack that apparently fixes the issue
in this example and in the snippet code described
in the thread related to

http://lists.cairographics.org/archives/cairo/2013-September/024626.html

Basically it enlarges the fallback surface created of the amount
necessary not to write past the end of the DIB. It assumes that
Clip applied to an HDC are clamped to (0,0,width,height) of the HDC.

HTH
Comment 2 LRN 2014-04-06 13:23:08 UTC
The patch (attachment 85688 [details] [review]) appears to be fixing my problems with RGBA windows ( https://bugzilla.gnome.org/show_bug.cgi?id=727316 and http://lists.cairographics.org/archives/cairo/2014-April/025147.html ).
Comment 3 Matthew Flatt 2014-09-25 22:25:49 UTC
The patch (attachment 85688 [details] [review]) seems to work for me, too. We have incorporated the patch into our build of Cairo to be distributed with the next release of Racket.
Comment 4 Bryce Harrington 2015-02-03 00:44:52 UTC
Thanks, patch applied:
   cca8b19..91f128b  master -> master