summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPietro Gagliardi <[email protected]>2014-04-12 13:48:59 -0400
committerPietro Gagliardi <[email protected]>2014-04-12 13:48:59 -0400
commitae4950bbd8a4dcae995da86a8b637ff13e8a90f2 (patch)
tree6c6481c5e3d6f68cefcd3f9bd26c22ea102b1ea4
parente3fdc76c5bf6b77ce53c8573f05f2d0ae8e33778 (diff)
Switched to ARGB drawing in Areas on GTK+.
-rw-r--r--area.go6
-rw-r--r--area_unix.go31
2 files changed, 20 insertions, 17 deletions
diff --git a/area.go b/area.go
index 2314837..c9734d5 100644
--- a/area.go
+++ b/area.go
@@ -340,8 +340,8 @@ func pixelData(img *image.RGBA) *uint8 {
// some platforms require pixels in ARGB order in their native endianness (because they treat the pixel array as an array of uint32s)
// this does the conversion
-// you need to convert somewhere; this memory is assumed to have a stride equal to the pixels per scanline (Windows gives us memory to use; other platforms we'll see)
-func toARGB(i *image.RGBA, memory uintptr) {
+// you need to convert somewhere (Windows and cairo give us memory to use; Windows has stride==width but cairo might not)
+func toARGB(i *image.RGBA, memory uintptr, memstride int) {
var realbits []byte
rbs := (*reflect.SliceHeader)(unsafe.Pointer(&realbits))
@@ -352,6 +352,7 @@ func toARGB(i *image.RGBA, memory uintptr) {
q := 0
for y := i.Rect.Min.Y; y < i.Rect.Max.Y; y++ {
nextp := p + i.Stride
+ nextq := q + memstride
for x := i.Rect.Min.X; x < i.Rect.Max.X; x++ {
argb := uint32(i.Pix[p + 3]) << 24 // A
argb |= uint32(i.Pix[p + 0]) << 16 // R
@@ -367,5 +368,6 @@ func toARGB(i *image.RGBA, memory uintptr) {
q += 4
}
p = nextp
+ q = nextq
}
}
diff --git a/area_unix.go b/area_unix.go
index fe67641..71a9edb 100644
--- a/area_unix.go
+++ b/area_unix.go
@@ -57,26 +57,27 @@ func our_area_draw_callback(widget *C.GtkWidget, cr *C.cairo_t, data C.gpointer)
return C.FALSE // signals handled without stopping the event chain (thanks to desrt again)
}
i := s.handler.Paint(cliprect)
- // pixel order is [R G B A] (see Example 1 on https://developer.gnome.org/gdk-pixbuf/2.26/gdk-pixbuf-The-GdkPixbuf-Structure.html) so we don't have to convert anything
- // gdk-pixbuf is not alpha-premultiplied (thanks to desrt in irc.gimp.net/#gtk+)
- pixbuf := C.gdk_pixbuf_new_from_data(
- (*C.guchar)(unsafe.Pointer(pixelData(i))),
- C.GDK_COLORSPACE_RGB,
- C.TRUE, // has alpha channel
- 8, // bits per sample
+ surface := C.cairo_image_surface_create(
+ C.CAIRO_FORMAT_ARGB32, // alpha-premultiplied; native byte order
C.int(i.Rect.Dx()),
- C.int(i.Rect.Dy()),
- C.int(i.Stride),
- nil, nil) // do not free data
- C.gdk_cairo_set_source_pixbuf(cr,
- pixbuf,
- C.gdouble(cliprect.Min.X),
- C.gdouble(cliprect.Min.Y))
+ C.int(i.Rect.Dy()))
+ if status := C.cairo_surface_status(surface); status != C.CAIRO_STATUS_SUCCESS {
+ panic(fmt.Errorf("cairo_create_image_surface() failed: %s\n",
+ C.GoString(C.cairo_status_to_string(status))))
+ }
+ // the flush and mark_dirty calls are required; see the cairo docs and https://git.gnome.org/browse/gtk+/tree/gdk/gdkcairo.c#n232 (thanks desrt in irc.gimp.net/#gtk+)
+ C.cairo_surface_flush(surface)
+ toARGB(i, uintptr(unsafe.Pointer(C.cairo_image_surface_get_data(surface))),
+ int(C.cairo_image_surface_get_stride(surface)))
+ C.cairo_surface_mark_dirty(surface)
+ C.cairo_set_source_surface(cr,
+ surface,
+ 0, 0) // origin of the surface
// that just set the brush that cairo uses: we have to actually draw now
// (via https://developer.gnome.org/gtkmm-tutorial/stable/sec-draw-images.html.en)
C.cairo_rectangle(cr, x, y, w, h) // breaking the nrom here since we have the double data already
C.cairo_fill(cr)
- C.g_object_unref((C.gpointer)(unsafe.Pointer(pixbuf))) // free pixbuf
+ C.cairo_surface_destroy(surface) // free surface
return C.FALSE // signals handled without stopping the event chain (thanks to desrt again)
}