2 #include "ev-pixbuf-cache.h"
3 #include "ev-job-scheduler.h"
4 #include "ev-mapping.h"
5 #include "ev-document-forms.h"
6 #include "ev-document-images.h"
7 #include "ev-document-annotations.h"
8 #include "ev-view-private.h"
10 typedef struct _CacheJobInfo
15 /* Region of the page that needs to be drawn */
18 /* Data we get from rendering */
19 cairo_surface_t *surface;
22 * Selection_points are the coordinates encapsulated in selection.
23 * target_points is the target selection size. */
24 EvRectangle selection_points;
25 EvRectangle target_points;
26 EvSelectionStyle selection_style;
29 cairo_surface_t *selection;
30 GdkRegion *selection_region;
37 /* We keep a link to our containing view just for style information. */
40 EvDocumentModel *model;
43 gboolean inverted_colors;
47 /* preload_cache_size is the number of pages prior to the current
48 * visible area that we cache. It's normally 1, but could be 2 in the
51 int preload_cache_size;
54 CacheJobInfo *prev_job;
55 CacheJobInfo *job_list;
56 CacheJobInfo *next_job;
59 struct _EvPixbufCacheClass
61 GObjectClass parent_class;
63 void (* job_finished) (EvPixbufCache *pixbuf_cache);
73 static guint signals[N_SIGNALS] = {0, };
75 static void ev_pixbuf_cache_init (EvPixbufCache *pixbuf_cache);
76 static void ev_pixbuf_cache_class_init (EvPixbufCacheClass *pixbuf_cache);
77 static void ev_pixbuf_cache_finalize (GObject *object);
78 static void ev_pixbuf_cache_dispose (GObject *object);
79 static void job_finished_cb (EvJob *job,
80 EvPixbufCache *pixbuf_cache);
81 static CacheJobInfo *find_job_cache (EvPixbufCache *pixbuf_cache,
83 static gboolean new_selection_surface_needed(EvPixbufCache *pixbuf_cache,
84 CacheJobInfo *job_info,
89 /* These are used for iterating through the prev and next arrays */
90 #define FIRST_VISIBLE_PREV(pixbuf_cache) \
91 (MAX (0, pixbuf_cache->preload_cache_size - pixbuf_cache->start_page))
92 #define VISIBLE_NEXT_LEN(pixbuf_cache) \
93 (MIN(pixbuf_cache->preload_cache_size, ev_document_get_n_pages (pixbuf_cache->document) - (1 + pixbuf_cache->end_page)))
94 #define PAGE_CACHE_LEN(pixbuf_cache) \
95 ((pixbuf_cache->end_page - pixbuf_cache->start_page) + 1)
97 #define MAX_PRELOADED_PAGES 3
99 G_DEFINE_TYPE (EvPixbufCache, ev_pixbuf_cache, G_TYPE_OBJECT)
102 ev_pixbuf_cache_init (EvPixbufCache *pixbuf_cache)
104 pixbuf_cache->start_page = -1;
105 pixbuf_cache->end_page = -1;
109 ev_pixbuf_cache_class_init (EvPixbufCacheClass *class)
111 GObjectClass *object_class;
113 object_class = G_OBJECT_CLASS (class);
115 object_class->finalize = ev_pixbuf_cache_finalize;
116 object_class->dispose = ev_pixbuf_cache_dispose;
118 signals[JOB_FINISHED] =
119 g_signal_new ("job-finished",
120 G_OBJECT_CLASS_TYPE (object_class),
121 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
122 G_STRUCT_OFFSET (EvPixbufCacheClass, job_finished),
124 g_cclosure_marshal_VOID__POINTER,
130 ev_pixbuf_cache_finalize (GObject *object)
132 EvPixbufCache *pixbuf_cache;
134 pixbuf_cache = EV_PIXBUF_CACHE (object);
136 if (pixbuf_cache->job_list) {
137 g_slice_free1 (sizeof (CacheJobInfo) * pixbuf_cache->job_list_len,
138 pixbuf_cache->job_list);
139 pixbuf_cache->job_list = NULL;
141 if (pixbuf_cache->prev_job) {
142 g_slice_free1 (sizeof (CacheJobInfo) * pixbuf_cache->preload_cache_size,
143 pixbuf_cache->prev_job);
144 pixbuf_cache->prev_job = NULL;
146 if (pixbuf_cache->next_job) {
147 g_slice_free1 (sizeof (CacheJobInfo) * pixbuf_cache->preload_cache_size,
148 pixbuf_cache->next_job);
149 pixbuf_cache->next_job = NULL;
152 g_object_unref (pixbuf_cache->model);
154 G_OBJECT_CLASS (ev_pixbuf_cache_parent_class)->finalize (object);
158 dispose_cache_job_info (CacheJobInfo *job_info,
161 if (job_info == NULL)
165 g_signal_handlers_disconnect_by_func (job_info->job,
166 G_CALLBACK (job_finished_cb),
168 ev_job_cancel (job_info->job);
169 g_object_unref (job_info->job);
170 job_info->job = NULL;
172 if (job_info->surface) {
173 cairo_surface_destroy (job_info->surface);
174 job_info->surface = NULL;
176 if (job_info->region) {
177 gdk_region_destroy (job_info->region);
178 job_info->region = NULL;
180 if (job_info->selection) {
181 cairo_surface_destroy (job_info->selection);
182 job_info->selection = NULL;
184 if (job_info->selection_region) {
185 gdk_region_destroy (job_info->selection_region);
186 job_info->selection_region = NULL;
189 job_info->points_set = FALSE;
193 ev_pixbuf_cache_dispose (GObject *object)
195 EvPixbufCache *pixbuf_cache;
198 pixbuf_cache = EV_PIXBUF_CACHE (object);
200 for (i = 0; i < pixbuf_cache->preload_cache_size; i++) {
201 dispose_cache_job_info (pixbuf_cache->prev_job + i, pixbuf_cache);
202 dispose_cache_job_info (pixbuf_cache->next_job + i, pixbuf_cache);
205 for (i = 0; i < PAGE_CACHE_LEN (pixbuf_cache); i++) {
206 dispose_cache_job_info (pixbuf_cache->job_list + i, pixbuf_cache);
209 G_OBJECT_CLASS (ev_pixbuf_cache_parent_class)->dispose (object);
214 ev_pixbuf_cache_new (GtkWidget *view,
215 EvDocumentModel *model,
218 EvPixbufCache *pixbuf_cache;
220 pixbuf_cache = (EvPixbufCache *) g_object_new (EV_TYPE_PIXBUF_CACHE, NULL);
221 /* This is a backlink, so we don't ref this */
222 pixbuf_cache->view = view;
223 pixbuf_cache->model = g_object_ref (model);
224 pixbuf_cache->document = ev_document_model_get_document (model);
225 pixbuf_cache->max_size = max_size;
231 ev_pixbuf_cache_set_max_size (EvPixbufCache *pixbuf_cache,
234 if (pixbuf_cache->max_size == max_size)
237 if (pixbuf_cache->max_size > max_size)
238 ev_pixbuf_cache_clear (pixbuf_cache);
239 pixbuf_cache->max_size = max_size;
243 copy_job_to_job_info (EvJobRender *job_render,
244 CacheJobInfo *job_info,
245 EvPixbufCache *pixbuf_cache)
247 if (job_info->surface) {
248 cairo_surface_destroy (job_info->surface);
250 job_info->surface = cairo_surface_reference (job_render->surface);
251 if (pixbuf_cache->inverted_colors) {
252 ev_document_misc_invert_surface (job_info->surface);
255 job_info->points_set = FALSE;
256 if (job_render->include_selection) {
257 if (job_info->selection) {
258 cairo_surface_destroy (job_info->selection);
259 job_info->selection = NULL;
261 if (job_info->selection_region) {
262 gdk_region_destroy (job_info->selection_region);
263 job_info->selection_region = NULL;
266 job_info->selection_points = job_render->selection_points;
267 job_info->selection_region = gdk_region_copy (job_render->selection_region);
268 job_info->selection = cairo_surface_reference (job_render->selection);
269 g_assert (job_info->selection_points.x1 >= 0);
270 job_info->points_set = TRUE;
274 g_signal_handlers_disconnect_by_func (job_info->job,
275 G_CALLBACK (job_finished_cb),
277 ev_job_cancel (job_info->job);
278 g_object_unref (job_info->job);
279 job_info->job = NULL;
282 job_info->page_ready = TRUE;
286 job_finished_cb (EvJob *job,
287 EvPixbufCache *pixbuf_cache)
289 CacheJobInfo *job_info;
290 EvJobRender *job_render = EV_JOB_RENDER (job);
292 /* If the job is outside of our interest, we silently discard it */
293 if ((job_render->page < (pixbuf_cache->start_page - pixbuf_cache->preload_cache_size)) ||
294 (job_render->page > (pixbuf_cache->end_page + pixbuf_cache->preload_cache_size))) {
295 g_object_unref (job);
299 job_info = find_job_cache (pixbuf_cache, job_render->page);
301 copy_job_to_job_info (job_render, job_info, pixbuf_cache);
302 g_signal_emit (pixbuf_cache, signals[JOB_FINISHED], 0, job_info->region);
305 /* This checks a job to see if the job would generate the right sized pixbuf
306 * given a scale. If it won't, it removes the job and clears it to NULL.
309 check_job_size_and_unref (EvPixbufCache *pixbuf_cache,
310 CacheJobInfo *job_info,
317 if (job_info->job == NULL)
320 _get_page_size_for_scale_and_rotation (job_info->job->document,
321 EV_JOB_RENDER (job_info->job)->page,
323 EV_JOB_RENDER (job_info->job)->rotation,
325 if (width == EV_JOB_RENDER (job_info->job)->target_width &&
326 height == EV_JOB_RENDER (job_info->job)->target_height)
329 g_signal_handlers_disconnect_by_func (job_info->job,
330 G_CALLBACK (job_finished_cb),
332 ev_job_cancel (job_info->job);
333 g_object_unref (job_info->job);
334 job_info->job = NULL;
337 /* Do all function that copies a job from an older cache to it's position in the
338 * new cache. It clears the old job if it doesn't have a place.
341 move_one_job (CacheJobInfo *job_info,
342 EvPixbufCache *pixbuf_cache,
344 CacheJobInfo *new_job_list,
345 CacheJobInfo *new_prev_job,
346 CacheJobInfo *new_next_job,
347 int new_preload_cache_size,
352 CacheJobInfo *target_page = NULL;
356 if (page < (start_page - new_preload_cache_size) ||
357 page > (end_page + new_preload_cache_size)) {
358 dispose_cache_job_info (job_info, pixbuf_cache);
362 /* find the target page to copy it over to. */
363 if (page < start_page) {
364 page_offset = (page - (start_page - new_preload_cache_size));
366 g_assert (page_offset >= 0 &&
367 page_offset < new_preload_cache_size);
368 target_page = new_prev_job + page_offset;
369 new_priority = EV_JOB_PRIORITY_LOW;
370 } else if (page > end_page) {
371 page_offset = (page - (end_page + 1));
373 g_assert (page_offset >= 0 &&
374 page_offset < new_preload_cache_size);
375 target_page = new_next_job + page_offset;
376 new_priority = EV_JOB_PRIORITY_LOW;
378 page_offset = page - start_page;
379 g_assert (page_offset >= 0 &&
380 page_offset <= ((end_page - start_page) + 1));
381 new_priority = EV_JOB_PRIORITY_URGENT;
382 target_page = new_job_list + page_offset;
385 *target_page = *job_info;
386 job_info->job = NULL;
387 job_info->region = NULL;
388 job_info->surface = NULL;
390 if (new_priority != priority && target_page->job) {
391 ev_job_scheduler_update_job (target_page->job, new_priority);
396 ev_pixbuf_cache_get_page_size (EvPixbufCache *pixbuf_cache,
403 _get_page_size_for_scale_and_rotation (pixbuf_cache->document,
404 page_index, scale, rotation,
406 return height * cairo_format_stride_for_width (CAIRO_FORMAT_RGB24, width);
410 ev_pixbuf_cache_get_preload_size (EvPixbufCache *pixbuf_cache,
416 gsize range_size = 0;
417 gint new_preload_cache_size = 0;
419 guint n_pages = ev_document_get_n_pages (pixbuf_cache->document);
421 /* Get the size of the current range */
422 for (i = start_page; i <= end_page; i++) {
423 range_size += ev_pixbuf_cache_get_page_size (pixbuf_cache, i, scale, rotation);
426 if (range_size >= pixbuf_cache->max_size)
427 return new_preload_cache_size;
430 while (((start_page - i > 0) || (end_page + i < n_pages)) &&
431 new_preload_cache_size < MAX_PRELOADED_PAGES) {
433 gboolean updated = FALSE;
435 if (end_page + i < n_pages) {
436 page_size = ev_pixbuf_cache_get_page_size (pixbuf_cache, end_page + i,
438 if (page_size + range_size <= pixbuf_cache->max_size) {
439 range_size += page_size;
440 new_preload_cache_size++;
447 if (start_page - i > 0) {
448 page_size = ev_pixbuf_cache_get_page_size (pixbuf_cache, start_page - i,
450 if (page_size + range_size <= pixbuf_cache->max_size) {
451 range_size += page_size;
453 new_preload_cache_size++;
461 return new_preload_cache_size;
465 ev_pixbuf_cache_update_range (EvPixbufCache *pixbuf_cache,
471 CacheJobInfo *new_job_list;
472 CacheJobInfo *new_prev_job = NULL;
473 CacheJobInfo *new_next_job = NULL;
474 gint new_preload_cache_size;
475 guint new_job_list_len;
478 new_preload_cache_size = ev_pixbuf_cache_get_preload_size (pixbuf_cache,
483 if (pixbuf_cache->start_page == start_page &&
484 pixbuf_cache->end_page == end_page &&
485 pixbuf_cache->preload_cache_size == new_preload_cache_size)
488 new_job_list_len = (end_page - start_page) + 1;
489 new_job_list = g_slice_alloc0 (sizeof (CacheJobInfo) * new_job_list_len);
490 if (new_preload_cache_size > 0) {
491 new_prev_job = g_slice_alloc0 (sizeof (CacheJobInfo) * new_preload_cache_size);
492 new_next_job = g_slice_alloc0 (sizeof (CacheJobInfo) * new_preload_cache_size);
495 /* We go through each job in the old cache and either clear it or move
496 * it to a new location. */
498 /* Start with the prev cache. */
499 page = pixbuf_cache->start_page - pixbuf_cache->preload_cache_size;
500 for (i = 0; i < pixbuf_cache->preload_cache_size; i++) {
502 dispose_cache_job_info (pixbuf_cache->prev_job + i, pixbuf_cache);
504 move_one_job (pixbuf_cache->prev_job + i,
506 new_job_list, new_prev_job, new_next_job,
507 new_preload_cache_size,
508 start_page, end_page, EV_JOB_PRIORITY_LOW);
513 page = pixbuf_cache->start_page;
514 for (i = 0; i < PAGE_CACHE_LEN (pixbuf_cache) && page >= 0; i++) {
515 move_one_job (pixbuf_cache->job_list + i,
517 new_job_list, new_prev_job, new_next_job,
518 new_preload_cache_size,
519 start_page, end_page, EV_JOB_PRIORITY_URGENT);
523 for (i = 0; i < pixbuf_cache->preload_cache_size; i++) {
524 if (page >= ev_document_get_n_pages (pixbuf_cache->document)) {
525 dispose_cache_job_info (pixbuf_cache->next_job + i, pixbuf_cache);
527 move_one_job (pixbuf_cache->next_job + i,
529 new_job_list, new_prev_job, new_next_job,
530 new_preload_cache_size,
531 start_page, end_page, EV_JOB_PRIORITY_LOW);
536 if (pixbuf_cache->job_list) {
537 g_slice_free1 (sizeof (CacheJobInfo) * pixbuf_cache->job_list_len,
538 pixbuf_cache->job_list);
540 if (pixbuf_cache->prev_job) {
541 g_slice_free1 (sizeof (CacheJobInfo) * pixbuf_cache->preload_cache_size,
542 pixbuf_cache->prev_job);
544 if (pixbuf_cache->next_job) {
545 g_slice_free1 (sizeof (CacheJobInfo) * pixbuf_cache->preload_cache_size,
546 pixbuf_cache->next_job);
549 pixbuf_cache->preload_cache_size = new_preload_cache_size;
550 pixbuf_cache->job_list_len = new_job_list_len;
552 pixbuf_cache->job_list = new_job_list;
553 pixbuf_cache->prev_job = new_prev_job;
554 pixbuf_cache->next_job = new_next_job;
556 pixbuf_cache->start_page = start_page;
557 pixbuf_cache->end_page = end_page;
560 static CacheJobInfo *
561 find_job_cache (EvPixbufCache *pixbuf_cache,
566 if (page < (pixbuf_cache->start_page - pixbuf_cache->preload_cache_size) ||
567 page > (pixbuf_cache->end_page + pixbuf_cache->preload_cache_size))
570 if (page < pixbuf_cache->start_page) {
571 page_offset = (page - (pixbuf_cache->start_page - pixbuf_cache->preload_cache_size));
573 g_assert (page_offset >= 0 &&
574 page_offset < pixbuf_cache->preload_cache_size);
575 return pixbuf_cache->prev_job + page_offset;
578 if (page > pixbuf_cache->end_page) {
579 page_offset = (page - (pixbuf_cache->end_page + 1));
581 g_assert (page_offset >= 0 &&
582 page_offset < pixbuf_cache->preload_cache_size);
583 return pixbuf_cache->next_job + page_offset;
586 page_offset = page - pixbuf_cache->start_page;
587 g_assert (page_offset >= 0 &&
588 page_offset <= PAGE_CACHE_LEN(pixbuf_cache));
589 return pixbuf_cache->job_list + page_offset;
593 ev_pixbuf_cache_clear_job_sizes (EvPixbufCache *pixbuf_cache,
598 for (i = 0; i < PAGE_CACHE_LEN (pixbuf_cache); i++) {
599 check_job_size_and_unref (pixbuf_cache, pixbuf_cache->job_list + i, scale);
602 for (i = 0; i < pixbuf_cache->preload_cache_size; i++) {
603 check_job_size_and_unref (pixbuf_cache, pixbuf_cache->prev_job + i, scale);
604 check_job_size_and_unref (pixbuf_cache, pixbuf_cache->next_job + i, scale);
609 get_selection_colors (GtkWidget *widget, GdkColor **text, GdkColor **base)
611 GtkStyle *style = gtk_widget_get_style (widget);
613 if (gtk_widget_has_focus (widget)) {
614 *text = &style->text [GTK_STATE_SELECTED];
615 *base = &style->base [GTK_STATE_SELECTED];
617 *text = &style->text [GTK_STATE_ACTIVE];
618 *base = &style->base [GTK_STATE_ACTIVE];
623 add_job (EvPixbufCache *pixbuf_cache,
624 CacheJobInfo *job_info,
631 EvJobPriority priority)
633 job_info->page_ready = FALSE;
635 if (job_info->region)
636 gdk_region_destroy (job_info->region);
637 job_info->region = region ? gdk_region_copy (region) : NULL;
639 job_info->job = ev_job_render_new (pixbuf_cache->document,
640 page, rotation, scale,
643 if (new_selection_surface_needed (pixbuf_cache, job_info, page, scale)) {
644 GdkColor *text, *base;
646 gtk_widget_ensure_style (pixbuf_cache->view);
647 get_selection_colors (pixbuf_cache->view, &text, &base);
648 ev_job_render_set_selection_info (EV_JOB_RENDER (job_info->job),
649 &(job_info->target_points),
650 job_info->selection_style,
654 g_signal_connect (job_info->job, "finished",
655 G_CALLBACK (job_finished_cb),
657 ev_job_scheduler_push_job (job_info->job, priority);
661 add_job_if_needed (EvPixbufCache *pixbuf_cache,
662 CacheJobInfo *job_info,
666 EvJobPriority priority)
673 _get_page_size_for_scale_and_rotation (pixbuf_cache->document,
674 page, scale, rotation,
677 if (job_info->surface &&
678 cairo_image_surface_get_width (job_info->surface) == width &&
679 cairo_image_surface_get_height (job_info->surface) == height)
682 /* Free old surfaces for non visible pages */
683 if (priority == EV_JOB_PRIORITY_LOW) {
684 if (job_info->surface) {
685 cairo_surface_destroy (job_info->surface);
686 job_info->surface = NULL;
689 if (job_info->selection) {
690 cairo_surface_destroy (job_info->selection);
691 job_info->selection = NULL;
695 add_job (pixbuf_cache, job_info, NULL,
696 width, height, page, rotation, scale,
701 ev_pixbuf_cache_add_jobs_if_needed (EvPixbufCache *pixbuf_cache,
705 CacheJobInfo *job_info;
709 for (i = 0; i < PAGE_CACHE_LEN (pixbuf_cache); i++) {
710 job_info = (pixbuf_cache->job_list + i);
711 page = pixbuf_cache->start_page + i;
713 add_job_if_needed (pixbuf_cache, job_info,
714 page, rotation, scale,
715 EV_JOB_PRIORITY_URGENT);
718 for (i = FIRST_VISIBLE_PREV(pixbuf_cache); i < pixbuf_cache->preload_cache_size; i++) {
719 job_info = (pixbuf_cache->prev_job + i);
720 page = pixbuf_cache->start_page - pixbuf_cache->preload_cache_size + i;
722 add_job_if_needed (pixbuf_cache, job_info,
723 page, rotation, scale,
724 EV_JOB_PRIORITY_LOW);
727 for (i = 0; i < VISIBLE_NEXT_LEN(pixbuf_cache); i++) {
728 job_info = (pixbuf_cache->next_job + i);
729 page = pixbuf_cache->end_page + 1 + i;
731 add_job_if_needed (pixbuf_cache, job_info,
732 page, rotation, scale,
733 EV_JOB_PRIORITY_LOW);
739 ev_pixbuf_cache_set_page_range (EvPixbufCache *pixbuf_cache,
742 GList *selection_list)
744 gdouble scale = ev_document_model_get_scale (pixbuf_cache->model);
745 gint rotation = ev_document_model_get_rotation (pixbuf_cache->model);
747 g_return_if_fail (EV_IS_PIXBUF_CACHE (pixbuf_cache));
749 g_return_if_fail (start_page >= 0 && start_page < ev_document_get_n_pages (pixbuf_cache->document));
750 g_return_if_fail (end_page >= 0 && end_page < ev_document_get_n_pages (pixbuf_cache->document));
751 g_return_if_fail (end_page >= start_page);
753 /* First, resize the page_range as needed. We cull old pages
755 ev_pixbuf_cache_update_range (pixbuf_cache, start_page, end_page, rotation, scale);
757 /* Then, we update the current jobs to see if any of them are the wrong
758 * size, we remove them if we need to. */
759 ev_pixbuf_cache_clear_job_sizes (pixbuf_cache, scale);
761 /* Next, we update the target selection for our pages */
762 ev_pixbuf_cache_set_selection_list (pixbuf_cache, selection_list);
764 /* Finally, we add the new jobs for all the sizes that don't have a
766 ev_pixbuf_cache_add_jobs_if_needed (pixbuf_cache, rotation, scale);
770 ev_pixbuf_cache_set_inverted_colors (EvPixbufCache *pixbuf_cache,
771 gboolean inverted_colors)
775 if (pixbuf_cache->inverted_colors == inverted_colors)
778 pixbuf_cache->inverted_colors = inverted_colors;
780 for (i = 0; i < pixbuf_cache->preload_cache_size; i++) {
781 CacheJobInfo *job_info;
783 job_info = pixbuf_cache->prev_job + i;
784 if (job_info->surface)
785 ev_document_misc_invert_surface (job_info->surface);
787 job_info = pixbuf_cache->next_job + i;
788 if (job_info->surface)
789 ev_document_misc_invert_surface (job_info->surface);
792 for (i = 0; i < PAGE_CACHE_LEN (pixbuf_cache); i++) {
793 CacheJobInfo *job_info;
795 job_info = pixbuf_cache->job_list + i;
796 if (job_info->surface)
797 ev_document_misc_invert_surface (job_info->surface);
802 ev_pixbuf_cache_get_surface (EvPixbufCache *pixbuf_cache,
805 CacheJobInfo *job_info;
807 job_info = find_job_cache (pixbuf_cache, page);
808 if (job_info == NULL)
811 if (job_info->page_ready)
812 return job_info->surface;
814 /* We don't need to wait for the idle to handle the callback */
816 EV_JOB_RENDER (job_info->job)->page_ready) {
817 copy_job_to_job_info (EV_JOB_RENDER (job_info->job), job_info, pixbuf_cache);
818 g_signal_emit (pixbuf_cache, signals[JOB_FINISHED], 0, job_info->region);
821 return job_info->surface;
825 new_selection_surface_needed (EvPixbufCache *pixbuf_cache,
826 CacheJobInfo *job_info,
830 if (job_info->selection) {
832 gint selection_width, selection_height;
834 _get_page_size_for_scale_and_rotation (pixbuf_cache->document,
838 selection_width = cairo_image_surface_get_width (job_info->selection);
839 selection_height = cairo_image_surface_get_height (job_info->selection);
841 if (width != selection_width || height != selection_height)
844 if (job_info->points_set)
852 clear_selection_if_needed (EvPixbufCache *pixbuf_cache,
853 CacheJobInfo *job_info,
857 if (new_selection_surface_needed (pixbuf_cache, job_info, page, scale)) {
858 if (job_info->selection)
859 cairo_surface_destroy (job_info->selection);
860 job_info->selection = NULL;
861 job_info->selection_points.x1 = -1;
865 /* Clears the cache of jobs and pixbufs.
868 ev_pixbuf_cache_clear (EvPixbufCache *pixbuf_cache)
872 if (!pixbuf_cache->job_list)
875 for (i = 0; i < pixbuf_cache->preload_cache_size; i++) {
876 dispose_cache_job_info (pixbuf_cache->prev_job + i, pixbuf_cache);
877 dispose_cache_job_info (pixbuf_cache->next_job + i, pixbuf_cache);
880 for (i = 0; i < PAGE_CACHE_LEN (pixbuf_cache); i++) {
881 dispose_cache_job_info (pixbuf_cache->job_list + i, pixbuf_cache);
887 ev_pixbuf_cache_style_changed (EvPixbufCache *pixbuf_cache)
891 if (!pixbuf_cache->job_list)
894 /* FIXME: doesn't update running jobs. */
895 for (i = 0; i < pixbuf_cache->preload_cache_size; i++) {
896 CacheJobInfo *job_info;
898 job_info = pixbuf_cache->prev_job + i;
899 if (job_info->selection) {
900 cairo_surface_destroy (job_info->selection);
901 job_info->selection = NULL;
904 job_info = pixbuf_cache->next_job + i;
905 if (job_info->selection) {
906 cairo_surface_destroy (job_info->selection);
907 job_info->selection = NULL;
911 for (i = 0; i < PAGE_CACHE_LEN (pixbuf_cache); i++) {
912 CacheJobInfo *job_info;
914 job_info = pixbuf_cache->job_list + i;
915 if (job_info->selection) {
916 cairo_surface_destroy (job_info->selection);
917 job_info->selection = NULL;
923 ev_pixbuf_cache_get_selection_surface (EvPixbufCache *pixbuf_cache,
928 CacheJobInfo *job_info;
930 /* the document does not implement the selection interface */
931 if (!EV_IS_SELECTION (pixbuf_cache->document))
934 job_info = find_job_cache (pixbuf_cache, page);
935 if (job_info == NULL)
938 /* No selection on this page */
939 if (!job_info->points_set)
942 /* If we have a running job, we just return what we have under the
943 * assumption that it'll be updated later and we can scale it as need
945 if (job_info->job && EV_JOB_RENDER (job_info->job)->include_selection)
946 return job_info->selection;
948 /* Now, lets see if we need to resize the image. If we do, we clear the
950 clear_selection_if_needed (pixbuf_cache, job_info, page, scale);
952 /* Finally, we see if the two scales are the same, and get a new pixbuf
953 * if needed. We do this synchronously for now. At some point, we
954 * _should_ be able to get rid of the doc_mutex, so the synchronicity
955 * doesn't kill us. Rendering a few glyphs should really be fast.
957 if (ev_rect_cmp (&(job_info->target_points), &(job_info->selection_points))) {
958 EvRectangle *old_points;
959 GdkColor *text, *base;
963 /* we need to get a new selection pixbuf */
964 ev_document_doc_mutex_lock ();
965 if (job_info->selection_points.x1 < 0) {
966 g_assert (job_info->selection == NULL);
969 g_assert (job_info->selection != NULL);
970 old_points = &(job_info->selection_points);
973 ev_page = ev_document_get_page (pixbuf_cache->document, page);
974 rc = ev_render_context_new (ev_page, 0, scale);
975 g_object_unref (ev_page);
977 if (job_info->selection_region)
978 gdk_region_destroy (job_info->selection_region);
979 job_info->selection_region =
980 ev_selection_get_selection_region (EV_SELECTION (pixbuf_cache->document),
981 rc, job_info->selection_style,
982 &(job_info->target_points));
984 gtk_widget_ensure_style (pixbuf_cache->view);
986 get_selection_colors (pixbuf_cache->view, &text, &base);
988 ev_selection_render_selection (EV_SELECTION (pixbuf_cache->document),
989 rc, &(job_info->selection),
990 &(job_info->target_points),
992 job_info->selection_style,
994 job_info->selection_points = job_info->target_points;
996 ev_document_doc_mutex_unlock ();
999 *region = job_info->selection_region;
1000 return job_info->selection;
1004 update_job_selection (CacheJobInfo *job_info,
1005 EvViewSelection *selection)
1007 job_info->points_set = TRUE;
1008 job_info->target_points = selection->rect;
1009 job_info->selection_style = selection->style;
1013 clear_job_selection (CacheJobInfo *job_info)
1015 job_info->points_set = FALSE;
1016 job_info->selection_points.x1 = -1;
1018 if (job_info->selection) {
1019 cairo_surface_destroy (job_info->selection);
1020 job_info->selection = NULL;
1024 /* This function will reset the selection on pages that no longer have them, and
1025 * will update the target_selection on those that need it. It will _not_ free
1026 * the previous selection_list -- that's up to caller to do.
1029 ev_pixbuf_cache_set_selection_list (EvPixbufCache *pixbuf_cache,
1030 GList *selection_list)
1032 EvViewSelection *selection;
1033 GList *list = selection_list;
1037 g_return_if_fail (EV_IS_PIXBUF_CACHE (pixbuf_cache));
1039 if (!EV_IS_SELECTION (pixbuf_cache->document))
1042 /* We check each area to see what needs updating, and what needs freeing; */
1043 page = pixbuf_cache->start_page - pixbuf_cache->preload_cache_size;
1044 for (i = 0; i < pixbuf_cache->preload_cache_size; i++) {
1052 if (((EvViewSelection *)list->data)->page == page) {
1053 selection = list->data;
1055 } else if (((EvViewSelection *)list->data)->page > page)
1061 update_job_selection (pixbuf_cache->prev_job + i, selection);
1063 clear_job_selection (pixbuf_cache->prev_job + i);
1067 page = pixbuf_cache->start_page;
1068 for (i = 0; i < PAGE_CACHE_LEN (pixbuf_cache); i++) {
1071 if (((EvViewSelection *)list->data)->page == page) {
1072 selection = list->data;
1074 } else if (((EvViewSelection *)list->data)->page > page)
1080 update_job_selection (pixbuf_cache->job_list + i, selection);
1082 clear_job_selection (pixbuf_cache->job_list + i);
1086 for (i = 0; i < pixbuf_cache->preload_cache_size; i++) {
1087 if (page >= ev_document_get_n_pages (pixbuf_cache->document))
1092 if (((EvViewSelection *)list->data)->page == page) {
1093 selection = list->data;
1095 } else if (((EvViewSelection *)list->data)->page > page)
1101 update_job_selection (pixbuf_cache->next_job + i, selection);
1103 clear_job_selection (pixbuf_cache->next_job + i);
1109 /* Returns what the pixbuf cache thinks is */
1112 ev_pixbuf_cache_get_selection_list (EvPixbufCache *pixbuf_cache)
1114 EvViewSelection *selection;
1115 GList *retval = NULL;
1119 g_return_val_if_fail (EV_IS_PIXBUF_CACHE (pixbuf_cache), NULL);
1121 /* We check each area to see what needs updating, and what needs freeing; */
1122 page = pixbuf_cache->start_page - pixbuf_cache->preload_cache_size;
1123 for (i = 0; i < pixbuf_cache->preload_cache_size; i++) {
1129 if (pixbuf_cache->prev_job[i].selection_points.x1 != -1) {
1130 selection = g_new0 (EvViewSelection, 1);
1131 selection->page = page;
1132 selection->rect = pixbuf_cache->prev_job[i].selection_points;
1133 if (pixbuf_cache->prev_job[i].selection_region)
1134 selection->covered_region = gdk_region_copy (pixbuf_cache->prev_job[i].selection_region);
1135 retval = g_list_append (retval, selection);
1141 page = pixbuf_cache->start_page;
1142 for (i = 0; i < PAGE_CACHE_LEN (pixbuf_cache); i++) {
1143 if (pixbuf_cache->job_list[i].selection_points.x1 != -1) {
1144 selection = g_new0 (EvViewSelection, 1);
1145 selection->page = page;
1146 selection->rect = pixbuf_cache->job_list[i].selection_points;
1147 if (pixbuf_cache->job_list[i].selection_region)
1148 selection->covered_region = gdk_region_copy (pixbuf_cache->job_list[i].selection_region);
1149 retval = g_list_append (retval, selection);
1155 for (i = 0; i < pixbuf_cache->preload_cache_size; i++) {
1156 if (page >= ev_document_get_n_pages (pixbuf_cache->document))
1159 if (pixbuf_cache->next_job[i].selection_points.x1 != -1) {
1160 selection = g_new0 (EvViewSelection, 1);
1161 selection->page = page;
1162 selection->rect = pixbuf_cache->next_job[i].selection_points;
1163 if (pixbuf_cache->next_job[i].selection_region)
1164 selection->covered_region = gdk_region_copy (pixbuf_cache->next_job[i].selection_region);
1165 retval = g_list_append (retval, selection);
1175 ev_pixbuf_cache_reload_page (EvPixbufCache *pixbuf_cache,
1181 CacheJobInfo *job_info;
1184 job_info = find_job_cache (pixbuf_cache, page);
1185 if (job_info == NULL)
1188 _get_page_size_for_scale_and_rotation (pixbuf_cache->document,
1189 page, scale, rotation,
1191 add_job (pixbuf_cache, job_info, region,
1192 width, height, page, rotation, scale,
1193 EV_JOB_PRIORITY_URGENT);