[evince/wip/gpoo/gtk4-rebase-port: 45/96] libview: Port EvPixbufCache to gtk4




commit 4479ae5a4d38df2f2de2f68474e2e4cd094496a2
Author: Qiu Wenbo <qiuwenbo kylinos com cn>
Date:   Wed Dec 8 00:05:42 2021 +0800

    libview: Port EvPixbufCache to gtk4
    
    Signed-off-by: Qiu Wenbo <qiuwenbo kylinos com cn>

 libview/ev-pixbuf-cache.c | 204 +++++++++++++++++++++-------------------------
 libview/ev-pixbuf-cache.h |   6 +-
 2 files changed, 95 insertions(+), 115 deletions(-)
---
diff --git a/libview/ev-pixbuf-cache.c b/libview/ev-pixbuf-cache.c
index 382846594..3383e5e89 100644
--- a/libview/ev-pixbuf-cache.c
+++ b/libview/ev-pixbuf-cache.c
@@ -15,9 +15,7 @@ typedef struct _CacheJobInfo
 
        /* Region of the page that needs to be drawn */
        cairo_region_t  *region;
-
-       /* Data we get from rendering */
-       cairo_surface_t *surface;
+       GdkTexture *texture;
 
        /* Device scale factor of target widget */
        int device_scale;
@@ -29,7 +27,7 @@ typedef struct _CacheJobInfo
        EvSelectionStyle selection_style;
        gboolean         points_set;
 
-       cairo_surface_t *selection;
+       GdkTexture      *selection_texture;
        gdouble          selection_scale;
        EvRectangle      selection_points;
 
@@ -49,7 +47,6 @@ struct _EvPixbufCache
        int start_page;
        int end_page;
         ScrollDirection scroll_direction;
-       gboolean inverted_colors;
 
        gsize max_size;
 
@@ -185,17 +182,19 @@ dispose_cache_job_info (CacheJobInfo *job_info,
        if (job_info->job)
                end_job (job_info, data);
 
-       if (job_info->surface) {
-               cairo_surface_destroy (job_info->surface);
-               job_info->surface = NULL;
+       if (job_info->texture) {
+               g_object_unref (job_info->texture);
+               job_info->texture = NULL;
        }
+
        if (job_info->region) {
                cairo_region_destroy (job_info->region);
                job_info->region = NULL;
        }
-       if (job_info->selection) {
-               cairo_surface_destroy (job_info->selection);
-               job_info->selection = NULL;
+
+       if (job_info->selection_texture) {
+               g_object_unref (job_info->selection_texture);
+               job_info->selection_texture = NULL;
        }
        if (job_info->selection_region) {
                cairo_region_destroy (job_info->selection_region);
@@ -276,25 +275,48 @@ set_device_scale_on_surface (cairo_surface_t *surface,
 #endif
 }
 
+static GdkTexture *
+gdk_texture_new_for_surface(cairo_surface_t *surface)
+{
+       GdkTexture *texture;
+       GBytes *bytes;
+
+       g_return_val_if_fail(surface != NULL, NULL);
+       g_return_val_if_fail(cairo_surface_get_type(surface) == CAIRO_SURFACE_TYPE_IMAGE, NULL);
+       g_return_val_if_fail(cairo_image_surface_get_width(surface) > 0, NULL);
+       g_return_val_if_fail(cairo_image_surface_get_height(surface) > 0, NULL);
+
+       bytes = g_bytes_new_with_free_func(cairo_image_surface_get_data(surface),
+                                          cairo_image_surface_get_height(surface) * 
cairo_image_surface_get_stride(surface),
+                                          (GDestroyNotify)cairo_surface_destroy,
+                                          cairo_surface_reference(surface));
+
+       texture = gdk_memory_texture_new(cairo_image_surface_get_width(surface),
+                                        cairo_image_surface_get_height(surface),
+                                        GDK_MEMORY_DEFAULT,
+                                        bytes,
+                                        cairo_image_surface_get_stride(surface));
+
+       g_bytes_unref(bytes);
+
+       return texture;
+}
+
 static void
 copy_job_to_job_info (EvJobRender   *job_render,
                      CacheJobInfo  *job_info,
                      EvPixbufCache *pixbuf_cache)
 {
-       if (job_info->surface) {
-               cairo_surface_destroy (job_info->surface);
-       }
-       job_info->surface = cairo_surface_reference (job_render->surface);
-       set_device_scale_on_surface (job_info->surface, job_info->device_scale);
-       if (pixbuf_cache->inverted_colors) {
-               ev_document_misc_invert_surface (job_info->surface);
-       }
+       if (job_info->texture)
+               g_object_unref (job_info->texture);
+
+       job_info->texture = g_object_ref (job_render->texture);
 
        job_info->points_set = FALSE;
        if (job_render->include_selection) {
-               if (job_info->selection) {
-                       cairo_surface_destroy (job_info->selection);
-                       job_info->selection = NULL;
+               if (job_info->selection_texture) {
+                       g_object_unref (job_info->selection_texture);
+                       job_info->selection_texture = NULL;
                }
                if (job_info->selection_region) {
                        cairo_region_destroy (job_info->selection_region);
@@ -302,9 +324,6 @@ copy_job_to_job_info (EvJobRender   *job_render,
                }
 
                job_info->selection_points = job_render->selection_points;
-               job_info->selection = cairo_surface_reference (job_render->selection);
-                if (job_info->selection)
-                        set_device_scale_on_surface (job_info->selection, job_info->device_scale);
                job_info->selection_scale = job_render->scale * job_info->device_scale;
                g_assert (job_info->selection_points.x1 >= 0);
 
@@ -312,6 +331,7 @@ copy_job_to_job_info (EvJobRender   *job_render,
                job_info->selection_region = cairo_region_reference (job_render->selection_region);
                job_info->selection_region_scale = job_render->scale;
 
+               job_info->selection_texture = g_object_ref (job_render->selection);
                job_info->points_set = TRUE;
        }
 
@@ -429,7 +449,7 @@ move_one_job (CacheJobInfo  *job_info,
        *target_page = *job_info;
        job_info->job = NULL;
        job_info->region = NULL;
-       job_info->surface = NULL;
+       job_info->texture = NULL;
 
        if (new_priority != priority && target_page->job) {
                ev_job_scheduler_update_job (target_page->job, new_priority);
@@ -650,21 +670,9 @@ ev_pixbuf_cache_clear_job_sizes (EvPixbufCache *pixbuf_cache,
 }
 
 static void
-get_selection_colors (EvView *view, GdkColor *text, GdkColor *base)
+get_selection_colors (EvView *view, GdkRGBA *text, GdkRGBA *base)
 {
-        GdkRGBA fg, bg;
-
-        _ev_view_get_selection_colors (view, &bg, &fg);
-
-        text->pixel = 0;
-        text->red = CLAMP ((guint) (fg.red * 65535), 0, 65535);
-        text->green = CLAMP ((guint) (fg.green * 65535), 0, 65535);
-        text->blue = CLAMP ((guint) (fg.blue * 65535), 0, 65535);
-
-        base->pixel = 0;
-        base->red = CLAMP ((guint) (bg.red * 65535), 0, 65535);
-        base->green = CLAMP ((guint) (bg.green * 65535), 0, 65535);
-        base->blue = CLAMP ((guint) (bg.blue * 65535), 0, 65535);
+        _ev_view_get_selection_colors (view, base, text);
 }
 
 static void
@@ -695,7 +703,7 @@ add_job (EvPixbufCache  *pixbuf_cache,
                                            height * job_info->device_scale);
 
        if (new_selection_surface_needed (pixbuf_cache, job_info, page, scale)) {
-               GdkColor text, base;
+               GdkRGBA text, base;
 
                get_selection_colors (EV_VIEW (pixbuf_cache->view), &text, &base);
                ev_job_render_set_selection_info (EV_JOB_RENDER (job_info->job),
@@ -728,22 +736,22 @@ add_job_if_needed (EvPixbufCache *pixbuf_cache,
                                               page, scale, rotation,
                                               &width, &height);
 
-       if (job_info->surface &&
+       if (job_info->texture &&
            job_info->device_scale == device_scale &&
-           cairo_image_surface_get_width (job_info->surface) == width * device_scale &&
-           cairo_image_surface_get_height (job_info->surface) == height * device_scale)
+           gdk_texture_get_width (job_info->texture) == width * device_scale &&
+           gdk_texture_get_height (job_info->texture) == height * device_scale)
                return;
 
        /* Free old surfaces for non visible pages */
        if (priority == EV_JOB_PRIORITY_LOW) {
-               if (job_info->surface) {
-                       cairo_surface_destroy (job_info->surface);
-                       job_info->surface = NULL;
+               if (job_info->texture) {
+                       g_object_unref (job_info->texture);
+                       job_info->texture = NULL;
                }
 
-               if (job_info->selection) {
-                       cairo_surface_destroy (job_info->selection);
-                       job_info->selection = NULL;
+               if (job_info->selection_texture) {
+                       g_object_unref (job_info->selection_texture);
+                       job_info->selection_texture = NULL;
                }
        }
 
@@ -870,40 +878,8 @@ ev_pixbuf_cache_set_page_range (EvPixbufCache  *pixbuf_cache,
        ev_pixbuf_cache_add_jobs_if_needed (pixbuf_cache, rotation, scale);
 }
 
-void
-ev_pixbuf_cache_set_inverted_colors (EvPixbufCache *pixbuf_cache,
-                                    gboolean       inverted_colors)
-{
-       gint i;
-
-       if (pixbuf_cache->inverted_colors == inverted_colors)
-               return;
-
-       pixbuf_cache->inverted_colors = inverted_colors;
-
-       for (i = 0; i < pixbuf_cache->preload_cache_size; i++) {
-               CacheJobInfo *job_info;
-
-               job_info = pixbuf_cache->prev_job + i;
-               if (job_info && job_info->surface)
-                       ev_document_misc_invert_surface (job_info->surface);
-
-               job_info = pixbuf_cache->next_job + i;
-               if (job_info && job_info->surface)
-                       ev_document_misc_invert_surface (job_info->surface);
-       }
-
-       for (i = 0; i < PAGE_CACHE_LEN (pixbuf_cache); i++) {
-               CacheJobInfo *job_info;
-
-               job_info = pixbuf_cache->job_list + i;
-               if (job_info && job_info->surface)
-                       ev_document_misc_invert_surface (job_info->surface);
-       }
-}
-
-cairo_surface_t *
-ev_pixbuf_cache_get_surface (EvPixbufCache *pixbuf_cache,
+GdkTexture *
+ev_pixbuf_cache_get_texture (EvPixbufCache *pixbuf_cache,
                             gint           page)
 {
        CacheJobInfo *job_info;
@@ -913,7 +889,7 @@ ev_pixbuf_cache_get_surface (EvPixbufCache *pixbuf_cache,
                return NULL;
 
        if (job_info->page_ready)
-               return job_info->surface;
+               return job_info->texture;
 
        /* We don't need to wait for the idle to handle the callback */
        if (job_info->job &&
@@ -922,7 +898,7 @@ ev_pixbuf_cache_get_surface (EvPixbufCache *pixbuf_cache,
                g_signal_emit (pixbuf_cache, signals[JOB_FINISHED], 0, job_info->region);
        }
 
-       return job_info->surface;
+       return job_info->texture;
 }
 
 static gboolean
@@ -931,7 +907,7 @@ new_selection_surface_needed (EvPixbufCache *pixbuf_cache,
                              gint           page,
                              gfloat         scale)
 {
-       if (job_info->selection)
+       if (job_info->selection_texture)
                return job_info->selection_scale != scale;
        return job_info->points_set;
 }
@@ -954,9 +930,9 @@ clear_selection_surface_if_needed (EvPixbufCache *pixbuf_cache,
                                    gfloat         scale)
 {
        if (new_selection_surface_needed (pixbuf_cache, job_info, page, scale)) {
-               if (job_info->selection)
-                       cairo_surface_destroy (job_info->selection);
-               job_info->selection = NULL;
+               if (job_info->selection_texture)
+                       g_object_unref (job_info->selection_texture);
+               job_info->selection_texture = NULL;
                job_info->selection_points.x1 = -1;
        }
 }
@@ -1009,16 +985,18 @@ ev_pixbuf_cache_style_changed (EvPixbufCache *pixbuf_cache)
                CacheJobInfo *job_info;
 
                job_info = pixbuf_cache->prev_job + i;
-               if (job_info->selection) {
-                       cairo_surface_destroy (job_info->selection);
-                       job_info->selection = NULL;
+
+               if (job_info->selection_texture) {
+                       g_object_unref (job_info->selection_texture);
+                       job_info->selection_texture = NULL;
                        job_info->selection_points.x1 = -1;
                }
 
                job_info = pixbuf_cache->next_job + i;
-               if (job_info->selection) {
-                       cairo_surface_destroy (job_info->selection);
-                       job_info->selection = NULL;
+
+               if (job_info->selection_texture) {
+                       g_object_unref (job_info->selection_texture);
+                       job_info->selection_texture = NULL;
                        job_info->selection_points.x1 = -1;
                }
        }
@@ -1027,16 +1005,17 @@ ev_pixbuf_cache_style_changed (EvPixbufCache *pixbuf_cache)
                CacheJobInfo *job_info;
 
                job_info = pixbuf_cache->job_list + i;
-               if (job_info->selection) {
-                       cairo_surface_destroy (job_info->selection);
-                       job_info->selection = NULL;
+
+               if (job_info->selection_texture) {
+                       g_object_unref (job_info->selection_texture);
+                       job_info->selection_texture = NULL;
                        job_info->selection_points.x1 = -1;
                }
        }
 }
 
-cairo_surface_t *
-ev_pixbuf_cache_get_selection_surface (EvPixbufCache   *pixbuf_cache,
+GdkTexture *
+ev_pixbuf_cache_get_selection_texture (EvPixbufCache   *pixbuf_cache,
                                       gint             page,
                                       gfloat           scale)
 {
@@ -1058,7 +1037,7 @@ ev_pixbuf_cache_get_selection_surface (EvPixbufCache   *pixbuf_cache,
         * assumption that it'll be updated later and we can scale it as need
         * be */
        if (job_info->job && EV_JOB_RENDER (job_info->job)->include_selection)
-               return job_info->selection;
+               return job_info->selection_texture;
 
        /* Now, lets see if we need to resize the image.  If we do, we clear the
         * old one. */
@@ -1071,15 +1050,16 @@ ev_pixbuf_cache_get_selection_surface (EvPixbufCache   *pixbuf_cache,
         */
        if (ev_rect_cmp (&(job_info->target_points), &(job_info->selection_points))) {
                EvRectangle *old_points;
-               GdkColor text, base;
+               GdkRGBA text, base;
                EvRenderContext *rc;
                EvPage *ev_page;
                gint width, height;
+               cairo_surface_t *selection = NULL;
 
                /* we need to get a new selection pixbuf */
                ev_document_doc_mutex_lock ();
                if (job_info->selection_points.x1 < 0) {
-                       g_assert (job_info->selection == NULL);
+                       g_assert (job_info->selection_texture == NULL);
                        old_points = NULL;
                } else {
                        old_points = &(job_info->selection_points);
@@ -1097,19 +1077,21 @@ ev_pixbuf_cache_get_selection_surface (EvPixbufCache   *pixbuf_cache,
 
                get_selection_colors (EV_VIEW (pixbuf_cache->view), &text, &base);
                ev_selection_render_selection (EV_SELECTION (pixbuf_cache->document),
-                                              rc, &(job_info->selection),
+                                              rc, &selection,
                                               &(job_info->target_points),
                                               old_points,
                                               job_info->selection_style,
                                               &text, &base);
-                if (job_info->selection)
-                        set_device_scale_on_surface (job_info->selection, job_info->device_scale);
+                if (selection)
+                        set_device_scale_on_surface (selection, job_info->device_scale);
                job_info->selection_points = job_info->target_points;
                job_info->selection_scale = scale * job_info->device_scale;
+               job_info->selection_texture = gdk_texture_new_for_surface (selection);
+               cairo_surface_destroy (selection);
                g_object_unref (rc);
                ev_document_doc_mutex_unlock ();
        }
-       return job_info->selection;
+       return job_info->selection_texture;
 }
 
 cairo_region_t *
@@ -1191,9 +1173,9 @@ clear_job_selection (CacheJobInfo *job_info)
        job_info->points_set = FALSE;
        job_info->selection_points.x1 = -1;
 
-       if (job_info->selection) {
-               cairo_surface_destroy (job_info->selection);
-               job_info->selection = NULL;
+       if (job_info->selection_texture) {
+               g_object_unref (job_info->selection_texture);
+               job_info->selection_texture = NULL;
        }
 
         if (job_info->selection_region) {
diff --git a/libview/ev-pixbuf-cache.h b/libview/ev-pixbuf-cache.h
index b3aed18a8..074a188ae 100644
--- a/libview/ev-pixbuf-cache.h
+++ b/libview/ev-pixbuf-cache.h
@@ -65,7 +65,7 @@ void           ev_pixbuf_cache_set_page_range       (EvPixbufCache *pixbuf_cache
                                                     gint           start_page,
                                                     gint           end_page,
                                                     GList          *selection_list);
-cairo_surface_t *ev_pixbuf_cache_get_surface        (EvPixbufCache *pixbuf_cache,
+GdkTexture *ev_pixbuf_cache_get_texture        (EvPixbufCache *pixbuf_cache,
                                                     gint           page);
 void           ev_pixbuf_cache_clear                (EvPixbufCache *pixbuf_cache);
 void           ev_pixbuf_cache_style_changed        (EvPixbufCache *pixbuf_cache);
@@ -74,10 +74,8 @@ void           ev_pixbuf_cache_reload_page       (EvPixbufCache  *pixbuf_cache,
                                                     gint            page,
                                                     gint            rotation,
                                                     gdouble         scale);
-void           ev_pixbuf_cache_set_inverted_colors  (EvPixbufCache *pixbuf_cache,
-                                                    gboolean       inverted_colors);
 /* Selection */
-cairo_surface_t *ev_pixbuf_cache_get_selection_surface (EvPixbufCache   *pixbuf_cache,
+GdkTexture *ev_pixbuf_cache_get_selection_texture (EvPixbufCache   *pixbuf_cache,
                                                        gint             page,
                                                        gfloat           scale);
 cairo_region_t *ev_pixbuf_cache_get_selection_region (EvPixbufCache *pixbuf_cache,


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]