[gnome-photos/wip/baedert/gtkimageview: 2/2] Use PhotosGeglImage to draw the image
- From: Timm Bäder <baedert src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-photos/wip/baedert/gtkimageview: 2/2] Use PhotosGeglImage to draw the image
- Date: Thu, 24 Mar 2016 18:38:27 +0000 (UTC)
commit e5d02391920164d2381e7f33d4ddfc2be1a71967
Author: Timm Bäder <mail baedert org>
Date: Thu Mar 24 19:37:49 2016 +0100
Use PhotosGeglImage to draw the image
src/Makefile.am | 2 +
src/photos-gegl-image.c | 156 ++++++++++++++++++++++++++++++++++++++
src/photos-gegl-image.h | 40 ++++++++++
src/photos-image-view.c | 189 +++++++----------------------------------------
src/photos-tool-crop.c | 38 +++++-----
5 files changed, 245 insertions(+), 180 deletions(-)
---
diff --git a/src/Makefile.am b/src/Makefile.am
index 0cb21e6..015f477 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -247,6 +247,8 @@ gnome_photos_SOURCES = \
photos-widget-shader.c \
photos-widget-shader.h \
photos-main.c \
+ photos-gegl-image.c \
+ photos-gegl-image.h \
$(NULL)
BUILT_SOURCES = \
diff --git a/src/photos-gegl-image.c b/src/photos-gegl-image.c
new file mode 100644
index 0000000..c222782
--- /dev/null
+++ b/src/photos-gegl-image.c
@@ -0,0 +1,156 @@
+#include "photos-gegl-image.h"
+
+
+
+G_DEFINE_TYPE(PhotosGeglImage, photos_gegl_image, GTK_TYPE_ABSTRACT_IMAGE)
+
+static int
+__get_width (GtkAbstractImage *_image)
+{
+ return PHOTOS_GEGL_IMAGE (_image)->width;
+}
+
+static int
+__get_height (GtkAbstractImage *_image)
+{
+ return PHOTOS_GEGL_IMAGE (_image)->height;
+}
+
+static int
+__get_scale_factor (GtkAbstractImage *_image)
+{
+ return PHOTOS_GEGL_IMAGE (_image)->scale_factor;
+}
+
+static void
+photos_gegl_image_render_surface (PhotosGeglImage *image)
+{
+ gegl_node_blit (image->node,
+ image->view_scale,
+ &image->roi,
+ image->format,
+ image->buf,
+ GEGL_AUTO_ROWSTRIDE,
+ GEGL_BLIT_CACHE | GEGL_BLIT_DIRTY);
+
+ if (!image->surface)
+ image->surface = cairo_image_surface_create_for_data (image->buf,
+ CAIRO_FORMAT_ARGB32,
+ image->roi.width,
+ image->roi.height,
+ image->stride);
+
+ g_signal_emit_by_name (G_OBJECT (image), "changed", 0);
+}
+
+static void
+__draw (GtkAbstractImage *_image, cairo_t *ct)
+{
+ PhotosGeglImage *image = PHOTOS_GEGL_IMAGE (_image);
+
+ if (image->surface)
+ {
+ cairo_scale (ct, 1.0 / image->view_scale, 1.0 / image->view_scale);
+ cairo_set_source_surface (ct, image->surface, 0, 0);
+ }
+}
+
+static void
+photos_gegl_image_update_bbox (PhotosGeglImage *image)
+{
+ GeglRectangle box;
+
+ if (!image->node)
+ return;
+
+ box = gegl_node_get_bounding_box (image->node);
+
+ if (image->width != box.width || image->height != box.height)
+ {
+ image->width = box.width;
+ image->height = box.height;
+
+ image->roi.x = 0;
+ image->roi.y = 0;
+ image->roi.width = image->width * image->scale_factor;
+ image->roi.height = image->height * image->scale_factor;
+
+ image->stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32, image->roi.width);
+ if (image->buf)
+ g_free (image->buf);
+
+ if (image->surface)
+ {
+ cairo_surface_destroy (image->surface);
+ image->surface = NULL;
+ }
+
+ image->buf = g_malloc (image->stride * image->roi.height);
+ }
+}
+
+static void
+photos_gegl_image_computed (PhotosGeglImage *image)
+{
+ photos_gegl_image_update_bbox (image);
+ photos_gegl_image_render_surface (image);
+}
+
+
+PhotosGeglImage *
+photos_gegl_image_new (GeglNode *node, int scale_factor)
+{
+ PhotosGeglImage *image = (PhotosGeglImage *) g_object_new (PHOTOS_TYPE_GEGL_IMAGE, NULL);
+
+ image->node = node;
+ image->scale_factor = scale_factor;
+ image->format = babl_format ("cairo-ARGB32");
+
+ photos_gegl_image_update_bbox (image);
+ g_signal_connect_object (node, "computed", G_CALLBACK (photos_gegl_image_computed), image,
G_CONNECT_SWAPPED);
+ photos_gegl_image_render_surface (image);
+
+ return image;
+}
+
+static void
+photos_gegl_image_init (PhotosGeglImage *image)
+{
+ image->surface = NULL;
+ image->view_scale = 1.0;
+}
+
+static void
+photos_gegl_image_finalize (GObject *_image)
+{
+ PhotosGeglImage *image = PHOTOS_GEGL_IMAGE (_image);
+
+ if (image->surface)
+ cairo_surface_destroy (image->surface);
+
+ if (image->buf)
+ g_free (image->buf);
+
+ G_OBJECT_CLASS (photos_gegl_image_parent_class)->finalize (_image);
+}
+
+static void
+photos_gegl_image_class_init (PhotosGeglImageClass *klass)
+{
+ GObjectClass *object_class = G_OBJECT_CLASS (klass);
+ GtkAbstractImageClass *image_class = GTK_ABSTRACT_IMAGE_CLASS (klass);
+
+ object_class->finalize = photos_gegl_image_finalize;
+
+ image_class->draw = __draw;
+ image_class->get_width = __get_width;
+ image_class->get_height = __get_height;
+ image_class->get_scale_factor = __get_scale_factor;
+}
+
+void
+photos_gegl_image_set_view_scale (PhotosGeglImage *image, double view_scale)
+{
+ image->view_scale = view_scale;
+ photos_gegl_image_render_surface (image);
+}
diff --git a/src/photos-gegl-image.h b/src/photos-gegl-image.h
new file mode 100644
index 0000000..57c8e05
--- /dev/null
+++ b/src/photos-gegl-image.h
@@ -0,0 +1,40 @@
+
+
+#include <gtk/gtk.h>
+#include <gegl.h>
+
+typedef struct _PhotosGeglImage PhotosGeglImage;
+typedef struct _PhotosGeglImageClass PhotosGeglImageClass;
+
+#define PHOTOS_TYPE_GEGL_IMAGE (photos_gegl_image_get_type ())
+#define PHOTOS_GEGL_IMAGE(obj) (G_TYPE_CHECK_INSTANCE_CAST(obj, PHOTOS_TYPE_GEGL_IMAGE,
PhotosGeglImage))
+#define PHOTOS_GEGL_IMAGE_CLASS(cls) (G_TYPE_CHECK_CLASS_CAST(cls, PHOTOS_TYPE_GEGL_IMAGE,
PhotosGeglImageClass))
+#define PHOTOS_IS_GEGL_IMAGE(obj) (G_TYPE_CHECK_INSTANCE_TYPE(obj, PHOTOS_TYPE_GEGL_IMAGE))
+#define PHOTOS_IS_GEGL_IMAGE_CLASS(cls) (G_TYPE_CHECK_CLASS_TYPE(cls, PHOTOS_TYPE_GEGL_IMAGE))
+#define PHOTOS_GEGL_IMAGE_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS(obj, PHOTOS_TYPE_GEGL_IMAGE,
PhotosGeglImageClass))
+
+struct _PhotosGeglImage
+{
+ GtkAbstractImage parent_instance;
+ GeglNode *node;
+ int width;
+ int height;
+ double view_scale;
+ cairo_surface_t *surface;
+ guchar *buf;
+ const Babl *format;
+ GeglRectangle roi;
+ int stride;
+ int scale_factor;
+};
+
+struct _PhotosGeglImageClass
+{
+ GtkAbstractImageClass parent_class;
+};
+
+GType photos_gegl_image_get_type (void) G_GNUC_CONST;
+
+PhotosGeglImage *photos_gegl_image_new (GeglNode *node, int scale_factor);
+
+void photos_gegl_image_set_view_scale (PhotosGeglImage *image, double view_scale);
diff --git a/src/photos-image-view.c b/src/photos-image-view.c
index f7a1477..48992cc 100644
--- a/src/photos-image-view.c
+++ b/src/photos-image-view.c
@@ -27,18 +27,14 @@
#include "photos-debug.h"
#include "photos-image-view.h"
#include "photos-marshalers.h"
+#include "photos-gegl-image.h"
struct _PhotosImageView
{
GtkImageView parent_instance;
GeglNode *node;
- gfloat x;
- gfloat x_scaled;
- gfloat y;
- gfloat y_scaled;
- gfloat zoom;
- gfloat zoom_scaled;
+ PhotosGeglImage *image;
};
struct _PhotosImageViewClass
@@ -46,7 +42,6 @@ struct _PhotosImageViewClass
GtkImageViewClass parent_class;
/* signals */
- void (*draw_background) (PhotosImageView *self, cairo_t *cr, GdkRectangle *rect);
void (*draw_overlay) (PhotosImageView *self, cairo_t *cr, GdkRectangle *rect);
};
@@ -68,115 +63,8 @@ enum
static guint signals[LAST_SIGNAL] = { 0 };
-
G_DEFINE_TYPE (PhotosImageView, photos_image_view, GTK_TYPE_IMAGE_VIEW);
-
-static void
-photos_image_view_update (PhotosImageView *self)
-{
- GdkRectangle viewport;
- GeglRectangle bbox;
- float zoom_scaled = 1.0;
- gint scale_factor;
- gint viewport_height_real;
- gint viewport_width_real;
-
- if (self->node == NULL)
- return;
-
- gtk_widget_get_allocation (GTK_WIDGET (self), &viewport);
-
- if (viewport.width < 0 || viewport.height < 0)
- return;
-
- bbox = gegl_node_get_bounding_box (self->node);
- if (bbox.width < 0 || bbox.height < 0)
- return;
-
- scale_factor = gtk_widget_get_scale_factor (GTK_WIDGET (self));
- viewport_height_real = viewport.height * scale_factor;
- viewport_width_real = viewport.width * scale_factor;
-
- if (bbox.height > viewport_height_real || bbox.width > viewport_width_real)
- {
- gfloat height_ratio = bbox.height / (gfloat) viewport_height_real;
- gfloat width_ratio = bbox.width / (gfloat) viewport_width_real;
- gfloat max_ratio = MAX (height_ratio, width_ratio);
-
- zoom_scaled = 1.0 / max_ratio;
-
- bbox.width = (gint) (zoom_scaled * bbox.width + 0.5);
- bbox.height = (gint) (zoom_scaled * bbox.height + 0.5);
- bbox.x = (gint) (zoom_scaled * bbox.x + 0.5);
- bbox.y = (gint) (zoom_scaled * bbox.y + 0.5);
- }
-
- self->zoom_scaled = zoom_scaled;
- self->zoom = self->zoom_scaled / (gfloat) scale_factor;
-
- /* At this point, viewport is definitely bigger than bbox. */
- self->x_scaled = (bbox.width - viewport_width_real) / 2.0 + bbox.x;
- self->y_scaled = (bbox.height - viewport_height_real) / 2.0 + bbox.y;
-
- self->x = self->x_scaled / (gfloat) scale_factor;
- self->y = self->y_scaled / (gfloat) scale_factor;
-}
-
-
-static void
-photos_image_view_computed (PhotosImageView *self)
-{
- photos_image_view_update (self);
-}
-
-
-static void
-photos_image_view_draw_node (PhotosImageView *self, cairo_t *cr, GdkRectangle *rect)
-{
- const Babl *format;
- GeglRectangle roi;
- cairo_surface_t *surface = NULL;
- guchar *buf = NULL;
- gint scale_factor;
- gint stride;
- gint64 end;
- gint64 start;
-
- scale_factor = gtk_widget_get_scale_factor (GTK_WIDGET (self));
-
- roi.x = (gint) self->x_scaled + rect->x * scale_factor;
- roi.y = (gint) self->y_scaled + rect->y * scale_factor;
- roi.width = rect->width * scale_factor;
- roi.height = rect->height * scale_factor;
-
- format = babl_format ("cairo-ARGB32");
- stride = cairo_format_stride_for_width (CAIRO_FORMAT_ARGB32, roi.width);
- buf = g_malloc0 (stride * roi.height);
-
- start = g_get_monotonic_time ();
-
- gegl_node_blit (self->node,
- (gdouble) self->zoom_scaled,
- &roi,
- format,
- buf,
- GEGL_AUTO_ROWSTRIDE,
- GEGL_BLIT_CACHE | GEGL_BLIT_DIRTY);
-
- end = g_get_monotonic_time ();
- photos_debug (PHOTOS_DEBUG_GEGL, "PhotosImageView: Node Blit: %" G_GINT64_FORMAT, end - start);
-
- surface = cairo_image_surface_create_for_data (buf, CAIRO_FORMAT_ARGB32, roi.width, roi.height, stride);
- cairo_surface_set_device_scale (surface, (gdouble) scale_factor, (gdouble) scale_factor);
- cairo_set_source_surface (cr, surface, rect->x, rect->y);
- cairo_paint (cr);
-
- cairo_surface_destroy (surface);
- g_free (buf);
-}
-
-
static gboolean
photos_image_view_draw (GtkWidget *widget, cairo_t *cr)
{
@@ -189,13 +77,7 @@ photos_image_view_draw (GtkWidget *widget, cairo_t *cr)
if (!gdk_cairo_get_clip_rectangle (cr, &rect))
goto out;
- cairo_save (cr);
- g_signal_emit (self, signals[DRAW_BACKGROUND], 0, cr, &rect);
- cairo_restore(cr);
-
- cairo_save (cr);
- photos_image_view_draw_node (self, cr, &rect);
- cairo_restore (cr);
+ GTK_WIDGET_CLASS (photos_image_view_parent_class)->draw (widget, cr);
cairo_save (cr);
g_signal_emit (self, signals[DRAW_OVERLAY], 0, cr, &rect);
@@ -205,18 +87,6 @@ photos_image_view_draw (GtkWidget *widget, cairo_t *cr)
return GDK_EVENT_PROPAGATE;
}
-
-static void
-photos_image_view_size_allocate (GtkWidget *widget, GtkAllocation *allocation)
-{
- PhotosImageView *self = PHOTOS_IMAGE_VIEW (widget);
-
- GTK_WIDGET_CLASS (photos_image_view_parent_class)->size_allocate (widget, allocation);
-
- photos_image_view_update (self);
-}
-
-
static void
photos_image_view_dispose (GObject *object)
{
@@ -240,15 +110,15 @@ photos_image_view_get_property (GObject *object, guint prop_id, GValue *value, G
break;
case PROP_X:
- g_value_set_float (value, self->x);
+ g_value_set_float (value, 0);
break;
case PROP_Y:
- g_value_set_float (value, self->y);
+ g_value_set_float (value, 0);
break;
case PROP_ZOOM:
- g_value_set_float (value, self->zoom);
+ g_value_set_float (value, gtk_image_view_get_scale (GTK_IMAGE_VIEW (self)));
break;
default:
@@ -282,6 +152,15 @@ photos_image_view_set_property (GObject *object, guint prop_id, const GValue *va
static void
+view_scale_changed_cb (GObject *source, GParamSpec *spec, gpointer user_data)
+{
+ PhotosImageView *view = PHOTOS_IMAGE_VIEW (source);
+ double scale = gtk_image_view_get_scale (GTK_IMAGE_VIEW (source));
+
+ photos_gegl_image_set_view_scale (view->image, scale);
+}
+
+static void
photos_image_view_init (PhotosImageView *self)
{
GtkStyleContext *context;
@@ -294,6 +173,8 @@ photos_image_view_init (PhotosImageView *self)
context = gtk_widget_get_style_context (GTK_WIDGET (self));
gtk_style_context_add_class (context, GTK_STYLE_CLASS_VIEW);
gtk_style_context_add_class (context, "content-view");
+
+ g_signal_connect (G_OBJECT (self), "notify::scale", G_CALLBACK (view_scale_changed_cb), NULL);
}
@@ -307,7 +188,6 @@ photos_image_view_class_init (PhotosImageViewClass *class)
object_class->get_property = photos_image_view_get_property;
object_class->set_property = photos_image_view_set_property;
widget_class->draw = photos_image_view_draw;
- widget_class->size_allocate = photos_image_view_size_allocate;
g_object_class_install_property (object_class,
PROP_NODE,
@@ -347,18 +227,6 @@ photos_image_view_class_init (PhotosImageViewClass *class)
1.0f,
G_PARAM_READABLE));
- signals[DRAW_BACKGROUND] = g_signal_new ("draw-background",
- G_TYPE_FROM_CLASS (class),
- G_SIGNAL_RUN_LAST,
- G_STRUCT_OFFSET (PhotosImageViewClass, draw_background),
- NULL, /* accumulator */
- NULL, /* accu_data */
- _photos_marshal_VOID__BOXED_BOXED,
- G_TYPE_NONE,
- 2,
- CAIRO_GOBJECT_TYPE_CONTEXT,
- GDK_TYPE_RECTANGLE);
-
signals[DRAW_OVERLAY] = g_signal_new ("draw-overlay",
G_TYPE_FROM_CLASS (class),
G_SIGNAL_RUN_LAST,
@@ -404,7 +272,7 @@ gfloat
photos_image_view_get_x (PhotosImageView *self)
{
g_return_val_if_fail (PHOTOS_IS_IMAGE_VIEW (self), 0.0);
- return self->x;
+ return 0;
}
@@ -412,7 +280,7 @@ gfloat
photos_image_view_get_y (PhotosImageView *self)
{
g_return_val_if_fail (PHOTOS_IS_IMAGE_VIEW (self), 0.0);
- return self->y;
+ return 0;
}
@@ -420,30 +288,29 @@ gfloat
photos_image_view_get_zoom (PhotosImageView *self)
{
g_return_val_if_fail (PHOTOS_IS_IMAGE_VIEW (self), 0.0);
- return self->zoom;
+
+ return gtk_image_view_get_scale (GTK_IMAGE_VIEW (self));
}
void
photos_image_view_set_node (PhotosImageView *self, GeglNode *node)
{
+ int scale_factor;
+
g_return_if_fail (PHOTOS_IS_IMAGE_VIEW (self));
if (self->node == node)
return;
- if (self->node != NULL)
- g_signal_handlers_disconnect_by_func (self->node, photos_image_view_computed, self);
-
g_clear_object (&self->node);
if (node != NULL)
- {
- g_object_ref (node);
- g_signal_connect_object (node, "computed", G_CALLBACK (photos_image_view_computed), self,
G_CONNECT_SWAPPED);
- }
+ g_object_ref (node);
+
+ scale_factor = gtk_widget_get_scale_factor (GTK_WIDGET (self));
self->node = node;
- photos_image_view_update (self);
- gtk_widget_queue_draw (GTK_WIDGET (self));
+ self->image = photos_gegl_image_new (node, scale_factor);
+ gtk_image_view_set_abstract_image (GTK_IMAGE_VIEW (self), GTK_ABSTRACT_IMAGE (self->image));
}
diff --git a/src/photos-tool-crop.c b/src/photos-tool-crop.c
index 8fb3873..d0927c3 100644
--- a/src/photos-tool-crop.c
+++ b/src/photos-tool-crop.c
@@ -196,26 +196,29 @@ photos_tool_crop_find_constraint (PhotosToolCrop *self, gdouble aspect_ratio)
static void
photos_tool_crop_redraw_damaged_area (PhotosToolCrop *self)
{
- cairo_rectangle_int_t area;
- cairo_region_t *region;
- gdouble damage_offset = HANDLE_OFFSET + HANDLE_RADIUS;
- gdouble x;
- gdouble y;
+ /*cairo_rectangle_int_t area;*/
+ /*cairo_region_t *region;*/
+ /*gdouble damage_offset = HANDLE_OFFSET + HANDLE_RADIUS;*/
+ /*gdouble x;*/
+ /*gdouble y;*/
- x = (gdouble) photos_image_view_get_x (PHOTOS_IMAGE_VIEW (self->view));
- x = -x + self->crop_x - damage_offset;
+ /*x = (gdouble) photos_image_view_get_x (PHOTOS_IMAGE_VIEW (self->view));*/
+ /*x = -x + self->crop_x - damage_offset;*/
- y = (gdouble) photos_image_view_get_y (PHOTOS_IMAGE_VIEW (self->view));
- y = -y + self->crop_y - damage_offset;
+ /*y = (gdouble) photos_image_view_get_y (PHOTOS_IMAGE_VIEW (self->view));*/
+ /*y = -y + self->crop_y - damage_offset;*/
- area.height = (gint) (self->crop_height + 2 * damage_offset + 0.5) + 2;
- area.width = (gint) (self->crop_width + 2 * damage_offset + 0.5) + 2;
- area.x = (gint) (x + 0.5) - 1;
- area.y = (gint) (y + 0.5) - 1;
+ /*area.height = (gint) (self->crop_height + 2 * damage_offset + 0.5) + 2;*/
+ /*area.width = (gint) (self->crop_width + 2 * damage_offset + 0.5) + 2;*/
+ /*area.x = (gint) (x + 0.5) - 1;*/
+ /*area.y = (gint) (y + 0.5) - 1;*/
- region = cairo_region_create_rectangle (&area);
- gtk_widget_queue_draw_region (self->view, region);
- cairo_region_destroy (region);
+
+ gtk_widget_queue_draw (self->view);
+
+ /*region = cairo_region_create_rectangle (&area);*/
+ /*gtk_widget_queue_draw_region (self->view, region);*/
+ /*cairo_region_destroy (region);*/
}
@@ -1050,10 +1053,7 @@ photos_tool_crop_draw (PhotosTool *tool, cairo_t *cr, GdkRectangle *rect)
g_return_if_fail (self->view != NULL);
x = (gdouble) photos_image_view_get_x (PHOTOS_IMAGE_VIEW (self->view));
- x = -x;
-
y = (gdouble) photos_image_view_get_y (PHOTOS_IMAGE_VIEW (self->view));
- y = -y;
cairo_save (cr);
cairo_set_source_surface (cr, self->surface, x, y);
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]