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
16 /* Region of the page that needs to be drawn */
19 /* Data we get from rendering */
20 cairo_surface_t *surface;
23 * Selection_points are the coordinates encapsulated in selection.
24 * target_points is the target selection size. */
25 EvRectangle selection_points;
26 EvRectangle target_points;
27 EvSelectionStyle selection_style;
30 cairo_surface_t *selection;
31 GdkRegion *selection_region;
38 /* We keep a link to our containing view just for style information. */
43 gboolean inverted_colors;
45 /* preload_cache_size is the number of pages prior to the current
46 * visible area that we cache. It's normally 1, but could be 2 in the
49 int preload_cache_size;
50 CacheJobInfo *prev_job;
51 CacheJobInfo *job_list;
52 CacheJobInfo *next_job;
55 struct _EvPixbufCacheClass
57 GObjectClass parent_class;
59 void (* job_finished) (EvPixbufCache *pixbuf_cache);
69 static guint signals[N_SIGNALS] = {0, };
71 static void ev_pixbuf_cache_init (EvPixbufCache *pixbuf_cache);
72 static void ev_pixbuf_cache_class_init (EvPixbufCacheClass *pixbuf_cache);
73 static void ev_pixbuf_cache_finalize (GObject *object);
74 static void ev_pixbuf_cache_dispose (GObject *object);
75 static void job_finished_cb (EvJob *job,
76 EvPixbufCache *pixbuf_cache);
77 static CacheJobInfo *find_job_cache (EvPixbufCache *pixbuf_cache,
79 static gboolean new_selection_surface_needed(EvPixbufCache *pixbuf_cache,
80 CacheJobInfo *job_info,
85 /* These are used for iterating through the prev and next arrays */
86 #define FIRST_VISIBLE_PREV(pixbuf_cache) \
87 (MAX (0, pixbuf_cache->preload_cache_size - pixbuf_cache->start_page))
88 #define VISIBLE_NEXT_LEN(pixbuf_cache) \
89 (MIN(pixbuf_cache->preload_cache_size, ev_document_get_n_pages (pixbuf_cache->document) - (1 + pixbuf_cache->end_page)))
90 #define PAGE_CACHE_LEN(pixbuf_cache) \
91 ((pixbuf_cache->end_page - pixbuf_cache->start_page) + 1)
93 G_DEFINE_TYPE (EvPixbufCache, ev_pixbuf_cache, G_TYPE_OBJECT)
96 ev_pixbuf_cache_init (EvPixbufCache *pixbuf_cache)
98 pixbuf_cache->start_page = 0;
99 pixbuf_cache->end_page = 0;
100 pixbuf_cache->job_list = g_new0 (CacheJobInfo, PAGE_CACHE_LEN (pixbuf_cache));
102 pixbuf_cache->preload_cache_size = 2;
103 pixbuf_cache->prev_job = g_new0 (CacheJobInfo, pixbuf_cache->preload_cache_size);
104 pixbuf_cache->next_job = g_new0 (CacheJobInfo, pixbuf_cache->preload_cache_size);
108 ev_pixbuf_cache_class_init (EvPixbufCacheClass *class)
110 GObjectClass *object_class;
112 object_class = G_OBJECT_CLASS (class);
114 object_class->finalize = ev_pixbuf_cache_finalize;
115 object_class->dispose = ev_pixbuf_cache_dispose;
117 signals[JOB_FINISHED] =
118 g_signal_new ("job-finished",
119 G_OBJECT_CLASS_TYPE (object_class),
120 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
121 G_STRUCT_OFFSET (EvPixbufCacheClass, job_finished),
123 g_cclosure_marshal_VOID__POINTER,
129 ev_pixbuf_cache_finalize (GObject *object)
131 EvPixbufCache *pixbuf_cache;
133 pixbuf_cache = EV_PIXBUF_CACHE (object);
135 g_free (pixbuf_cache->prev_job);
136 g_free (pixbuf_cache->job_list);
137 g_free (pixbuf_cache->next_job);
139 G_OBJECT_CLASS (ev_pixbuf_cache_parent_class)->finalize (object);
143 dispose_cache_job_info (CacheJobInfo *job_info,
146 if (job_info == NULL)
150 g_signal_handlers_disconnect_by_func (job_info->job,
151 G_CALLBACK (job_finished_cb),
153 ev_job_cancel (job_info->job);
154 g_object_unref (job_info->job);
155 job_info->job = NULL;
157 if (job_info->surface) {
158 cairo_surface_destroy (job_info->surface);
159 job_info->surface = NULL;
161 if (job_info->region) {
162 gdk_region_destroy (job_info->region);
163 job_info->region = NULL;
165 if (job_info->selection) {
166 cairo_surface_destroy (job_info->selection);
167 job_info->selection = NULL;
169 if (job_info->selection_region) {
170 gdk_region_destroy (job_info->selection_region);
171 job_info->selection_region = NULL;
174 g_object_unref (G_OBJECT (job_info->rc));
178 job_info->points_set = FALSE;
182 ev_pixbuf_cache_dispose (GObject *object)
184 EvPixbufCache *pixbuf_cache;
187 pixbuf_cache = EV_PIXBUF_CACHE (object);
189 for (i = 0; i < pixbuf_cache->preload_cache_size; i++) {
190 dispose_cache_job_info (pixbuf_cache->prev_job + i, pixbuf_cache);
191 dispose_cache_job_info (pixbuf_cache->next_job + i, pixbuf_cache);
194 for (i = 0; i < PAGE_CACHE_LEN (pixbuf_cache); i++) {
195 dispose_cache_job_info (pixbuf_cache->job_list + i, pixbuf_cache);
198 G_OBJECT_CLASS (ev_pixbuf_cache_parent_class)->dispose (object);
203 ev_pixbuf_cache_new (GtkWidget *view,
204 EvDocument *document)
206 EvPixbufCache *pixbuf_cache;
208 pixbuf_cache = (EvPixbufCache *) g_object_new (EV_TYPE_PIXBUF_CACHE, NULL);
209 /* This is a backlink, so we don't ref this */
210 pixbuf_cache->view = view;
211 pixbuf_cache->document = document;
217 copy_job_to_job_info (EvJobRender *job_render,
218 CacheJobInfo *job_info,
219 EvPixbufCache *pixbuf_cache)
221 if (job_info->rc == NULL) {
222 job_info->rc = ev_render_context_new (job_render->ev_page,
223 job_render->rotation,
226 ev_render_context_set_page (job_info->rc, job_render->ev_page);
227 ev_render_context_set_rotation (job_info->rc, job_render->rotation);
228 ev_render_context_set_scale (job_info->rc, job_render->scale);
231 if (job_info->surface) {
232 cairo_surface_destroy (job_info->surface);
234 job_info->surface = cairo_surface_reference (job_render->surface);
235 if (pixbuf_cache->inverted_colors) {
236 ev_document_misc_invert_surface (job_info->surface);
239 job_info->points_set = FALSE;
240 if (job_render->include_selection) {
241 if (job_info->selection) {
242 cairo_surface_destroy (job_info->selection);
243 job_info->selection = NULL;
245 if (job_info->selection_region) {
246 gdk_region_destroy (job_info->selection_region);
247 job_info->selection_region = NULL;
250 job_info->selection_points = job_render->selection_points;
251 job_info->selection_region = gdk_region_copy (job_render->selection_region);
252 job_info->selection = cairo_surface_reference (job_render->selection);
253 g_assert (job_info->selection_points.x1 >= 0);
254 job_info->points_set = TRUE;
258 g_signal_handlers_disconnect_by_func (job_info->job,
259 G_CALLBACK (job_finished_cb),
261 ev_job_cancel (job_info->job);
262 g_object_unref (job_info->job);
263 job_info->job = NULL;
266 job_info->page_ready = TRUE;
270 job_finished_cb (EvJob *job,
271 EvPixbufCache *pixbuf_cache)
273 CacheJobInfo *job_info;
274 EvJobRender *job_render = EV_JOB_RENDER (job);
276 /* If the job is outside of our interest, we silently discard it */
277 if ((job_render->page < (pixbuf_cache->start_page - pixbuf_cache->preload_cache_size)) ||
278 (job_render->page > (pixbuf_cache->end_page + pixbuf_cache->preload_cache_size))) {
279 g_object_unref (job);
283 job_info = find_job_cache (pixbuf_cache, job_render->page);
285 copy_job_to_job_info (job_render, job_info, pixbuf_cache);
286 g_signal_emit (pixbuf_cache, signals[JOB_FINISHED], 0, job_info->region);
289 /* This checks a job to see if the job would generate the right sized pixbuf
290 * given a scale. If it won't, it removes the job and clears it to NULL.
293 check_job_size_and_unref (EvPixbufCache *pixbuf_cache,
294 CacheJobInfo *job_info,
301 if (job_info->job == NULL)
304 _get_page_size_for_scale_and_rotation (job_info->job->document,
305 EV_JOB_RENDER (job_info->job)->page,
307 EV_JOB_RENDER (job_info->job)->rotation,
309 if (width == EV_JOB_RENDER (job_info->job)->target_width &&
310 height == EV_JOB_RENDER (job_info->job)->target_height)
313 g_signal_handlers_disconnect_by_func (job_info->job,
314 G_CALLBACK (job_finished_cb),
316 ev_job_cancel (job_info->job);
317 g_object_unref (job_info->job);
318 job_info->job = NULL;
321 /* Do all function that copies a job from an older cache to it's position in the
322 * new cache. It clears the old job if it doesn't have a place.
325 move_one_job (CacheJobInfo *job_info,
326 EvPixbufCache *pixbuf_cache,
328 CacheJobInfo *new_job_list,
329 CacheJobInfo *new_prev_job,
330 CacheJobInfo *new_next_job,
335 CacheJobInfo *target_page = NULL;
339 if (page < (start_page - pixbuf_cache->preload_cache_size) ||
340 page > (end_page + pixbuf_cache->preload_cache_size)) {
341 dispose_cache_job_info (job_info, pixbuf_cache);
345 /* find the target page to copy it over to. */
346 if (page < start_page) {
347 page_offset = (page - (start_page - pixbuf_cache->preload_cache_size));
349 g_assert (page_offset >= 0 &&
350 page_offset < pixbuf_cache->preload_cache_size);
351 target_page = new_prev_job + page_offset;
352 new_priority = EV_JOB_PRIORITY_LOW;
353 } else if (page > end_page) {
354 page_offset = (page - (end_page + 1));
356 g_assert (page_offset >= 0 &&
357 page_offset < pixbuf_cache->preload_cache_size);
358 target_page = new_next_job + page_offset;
359 new_priority = EV_JOB_PRIORITY_LOW;
361 page_offset = page - start_page;
362 g_assert (page_offset >= 0 &&
363 page_offset <= ((end_page - start_page) + 1));
364 new_priority = EV_JOB_PRIORITY_URGENT;
365 target_page = new_job_list + page_offset;
368 *target_page = *job_info;
369 job_info->job = NULL;
370 job_info->region = NULL;
371 job_info->surface = NULL;
373 if (new_priority != priority && target_page->job) {
374 ev_job_scheduler_update_job (target_page->job, new_priority);
379 ev_pixbuf_cache_update_range (EvPixbufCache *pixbuf_cache,
383 CacheJobInfo *new_job_list;
384 CacheJobInfo *new_prev_job;
385 CacheJobInfo *new_next_job;
388 if (pixbuf_cache->start_page == start_page &&
389 pixbuf_cache->end_page == end_page)
392 new_job_list = g_new0 (CacheJobInfo, (end_page - start_page) + 1);
393 new_prev_job = g_new0 (CacheJobInfo, pixbuf_cache->preload_cache_size);
394 new_next_job = g_new0 (CacheJobInfo, pixbuf_cache->preload_cache_size);
396 /* We go through each job in the old cache and either clear it or move
397 * it to a new location. */
399 /* Start with the prev cache. */
400 page = pixbuf_cache->start_page - pixbuf_cache->preload_cache_size;
401 for (i = 0; i < pixbuf_cache->preload_cache_size; i++) {
403 dispose_cache_job_info (pixbuf_cache->prev_job + i, pixbuf_cache);
405 move_one_job (pixbuf_cache->prev_job + i,
407 new_job_list, new_prev_job, new_next_job,
408 start_page, end_page, EV_JOB_PRIORITY_LOW);
413 page = pixbuf_cache->start_page;
414 for (i = 0; i < PAGE_CACHE_LEN (pixbuf_cache); i++) {
415 move_one_job (pixbuf_cache->job_list + i,
417 new_job_list, new_prev_job, new_next_job,
418 start_page, end_page, EV_JOB_PRIORITY_URGENT);
422 for (i = 0; i < pixbuf_cache->preload_cache_size; i++) {
423 if (page >= ev_document_get_n_pages (pixbuf_cache->document)) {
424 dispose_cache_job_info (pixbuf_cache->next_job + i, pixbuf_cache);
426 move_one_job (pixbuf_cache->next_job + i,
428 new_job_list, new_prev_job, new_next_job,
429 start_page, end_page, EV_JOB_PRIORITY_LOW);
434 g_free (pixbuf_cache->job_list);
435 g_free (pixbuf_cache->prev_job);
436 g_free (pixbuf_cache->next_job);
438 pixbuf_cache->job_list = new_job_list;
439 pixbuf_cache->prev_job = new_prev_job;
440 pixbuf_cache->next_job = new_next_job;
442 pixbuf_cache->start_page = start_page;
443 pixbuf_cache->end_page = end_page;
446 static CacheJobInfo *
447 find_job_cache (EvPixbufCache *pixbuf_cache,
452 if (page < (pixbuf_cache->start_page - pixbuf_cache->preload_cache_size) ||
453 page > (pixbuf_cache->end_page + pixbuf_cache->preload_cache_size))
456 if (page < pixbuf_cache->start_page) {
457 page_offset = (page - (pixbuf_cache->start_page - pixbuf_cache->preload_cache_size));
459 g_assert (page_offset >= 0 &&
460 page_offset < pixbuf_cache->preload_cache_size);
461 return pixbuf_cache->prev_job + page_offset;
464 if (page > pixbuf_cache->end_page) {
465 page_offset = (page - (pixbuf_cache->end_page + 1));
467 g_assert (page_offset >= 0 &&
468 page_offset < pixbuf_cache->preload_cache_size);
469 return pixbuf_cache->next_job + page_offset;
472 page_offset = page - pixbuf_cache->start_page;
473 g_assert (page_offset >= 0 &&
474 page_offset <= PAGE_CACHE_LEN(pixbuf_cache));
475 return pixbuf_cache->job_list + page_offset;
479 ev_pixbuf_cache_clear_job_sizes (EvPixbufCache *pixbuf_cache,
484 for (i = 0; i < PAGE_CACHE_LEN (pixbuf_cache); i++) {
485 check_job_size_and_unref (pixbuf_cache, pixbuf_cache->job_list + i, scale);
488 for (i = 0; i < pixbuf_cache->preload_cache_size; i++) {
489 check_job_size_and_unref (pixbuf_cache, pixbuf_cache->prev_job + i, scale);
490 check_job_size_and_unref (pixbuf_cache, pixbuf_cache->next_job + i, scale);
495 get_selection_colors (GtkWidget *widget, GdkColor **text, GdkColor **base)
497 if (GTK_WIDGET_HAS_FOCUS (widget)) {
498 *text = &widget->style->text [GTK_STATE_SELECTED];
499 *base = &widget->style->base [GTK_STATE_SELECTED];
501 *text = &widget->style->text [GTK_STATE_ACTIVE];
502 *base = &widget->style->base [GTK_STATE_ACTIVE];
507 add_job (EvPixbufCache *pixbuf_cache,
508 CacheJobInfo *job_info,
515 EvJobPriority priority)
517 job_info->page_ready = FALSE;
519 if (job_info->region)
520 gdk_region_destroy (job_info->region);
521 job_info->region = region ? gdk_region_copy (region) : NULL;
523 job_info->job = ev_job_render_new (pixbuf_cache->document,
524 page, rotation, scale,
527 if (new_selection_surface_needed (pixbuf_cache, job_info, page, scale)) {
528 GdkColor *text, *base;
530 gtk_widget_ensure_style (pixbuf_cache->view);
531 get_selection_colors (pixbuf_cache->view, &text, &base);
532 ev_job_render_set_selection_info (EV_JOB_RENDER (job_info->job),
533 &(job_info->target_points),
534 job_info->selection_style,
538 g_signal_connect (job_info->job, "finished",
539 G_CALLBACK (job_finished_cb),
541 ev_job_scheduler_push_job (job_info->job, priority);
545 add_job_if_needed (EvPixbufCache *pixbuf_cache,
546 CacheJobInfo *job_info,
550 EvJobPriority priority)
557 _get_page_size_for_scale_and_rotation (pixbuf_cache->document,
558 page, scale, rotation,
561 if (job_info->surface &&
562 cairo_image_surface_get_width (job_info->surface) == width &&
563 cairo_image_surface_get_height (job_info->surface) == height)
566 add_job (pixbuf_cache, job_info, NULL,
567 width, height, page, rotation, scale,
572 ev_pixbuf_cache_add_jobs_if_needed (EvPixbufCache *pixbuf_cache,
576 CacheJobInfo *job_info;
580 for (i = 0; i < PAGE_CACHE_LEN (pixbuf_cache); i++) {
581 job_info = (pixbuf_cache->job_list + i);
582 page = pixbuf_cache->start_page + i;
584 add_job_if_needed (pixbuf_cache, job_info,
585 page, rotation, scale,
586 EV_JOB_PRIORITY_URGENT);
589 for (i = FIRST_VISIBLE_PREV(pixbuf_cache); i < pixbuf_cache->preload_cache_size; i++) {
590 job_info = (pixbuf_cache->prev_job + i);
591 page = pixbuf_cache->start_page - pixbuf_cache->preload_cache_size + i;
593 add_job_if_needed (pixbuf_cache, job_info,
594 page, rotation, scale,
595 EV_JOB_PRIORITY_LOW);
598 for (i = 0; i < VISIBLE_NEXT_LEN(pixbuf_cache); i++) {
599 job_info = (pixbuf_cache->next_job + i);
600 page = pixbuf_cache->end_page + 1 + i;
602 add_job_if_needed (pixbuf_cache, job_info,
603 page, rotation, scale,
604 EV_JOB_PRIORITY_LOW);
610 ev_pixbuf_cache_set_page_range (EvPixbufCache *pixbuf_cache,
615 GList *selection_list)
617 g_return_if_fail (EV_IS_PIXBUF_CACHE (pixbuf_cache));
619 g_return_if_fail (start_page >= 0 && start_page < ev_document_get_n_pages (pixbuf_cache->document));
620 g_return_if_fail (end_page >= 0 && end_page < ev_document_get_n_pages (pixbuf_cache->document));
621 g_return_if_fail (end_page >= start_page);
623 /* First, resize the page_range as needed. We cull old pages
625 ev_pixbuf_cache_update_range (pixbuf_cache, start_page, end_page);
627 /* Then, we update the current jobs to see if any of them are the wrong
628 * size, we remove them if we need to. */
629 ev_pixbuf_cache_clear_job_sizes (pixbuf_cache, scale);
631 /* Next, we update the target selection for our pages */
632 ev_pixbuf_cache_set_selection_list (pixbuf_cache, selection_list);
634 /* Finally, we add the new jobs for all the sizes that don't have a
636 ev_pixbuf_cache_add_jobs_if_needed (pixbuf_cache, rotation, scale);
640 ev_pixbuf_cache_set_inverted_colors (EvPixbufCache *pixbuf_cache,
641 gboolean inverted_colors)
645 if (pixbuf_cache->inverted_colors == inverted_colors)
648 pixbuf_cache->inverted_colors = inverted_colors;
650 for (i = 0; i < pixbuf_cache->preload_cache_size; i++) {
651 CacheJobInfo *job_info;
653 job_info = pixbuf_cache->prev_job + i;
654 if (job_info->surface)
655 ev_document_misc_invert_surface (job_info->surface);
657 job_info = pixbuf_cache->next_job + i;
658 if (job_info->surface)
659 ev_document_misc_invert_surface (job_info->surface);
662 for (i = 0; i < PAGE_CACHE_LEN (pixbuf_cache); i++) {
663 CacheJobInfo *job_info;
665 job_info = pixbuf_cache->job_list + i;
666 if (job_info->surface)
667 ev_document_misc_invert_surface (job_info->surface);
672 ev_pixbuf_cache_get_surface (EvPixbufCache *pixbuf_cache,
675 CacheJobInfo *job_info;
677 job_info = find_job_cache (pixbuf_cache, page);
678 if (job_info == NULL)
681 if (job_info->page_ready)
682 return job_info->surface;
684 /* We don't need to wait for the idle to handle the callback */
686 EV_JOB_RENDER (job_info->job)->page_ready) {
687 copy_job_to_job_info (EV_JOB_RENDER (job_info->job), job_info, pixbuf_cache);
688 g_signal_emit (pixbuf_cache, signals[JOB_FINISHED], 0, job_info->region);
691 return job_info->surface;
695 new_selection_surface_needed (EvPixbufCache *pixbuf_cache,
696 CacheJobInfo *job_info,
700 if (job_info->selection && job_info->rc) {
702 gint selection_width, selection_height;
704 _get_page_size_for_scale_and_rotation (pixbuf_cache->document,
705 page, scale, job_info->rc->rotation,
708 selection_width = cairo_image_surface_get_width (job_info->selection);
709 selection_height = cairo_image_surface_get_height (job_info->selection);
711 if (width != selection_width || height != selection_height)
714 if (job_info->points_set)
722 clear_selection_if_needed (EvPixbufCache *pixbuf_cache,
723 CacheJobInfo *job_info,
727 if (new_selection_surface_needed (pixbuf_cache, job_info, page, scale)) {
728 if (job_info->selection)
729 cairo_surface_destroy (job_info->selection);
730 job_info->selection = NULL;
731 job_info->selection_points.x1 = -1;
735 /* Clears the cache of jobs and pixbufs.
738 ev_pixbuf_cache_clear (EvPixbufCache *pixbuf_cache)
742 for (i = 0; i < pixbuf_cache->preload_cache_size; i++) {
743 dispose_cache_job_info (pixbuf_cache->prev_job + i, pixbuf_cache);
744 dispose_cache_job_info (pixbuf_cache->next_job + i, pixbuf_cache);
747 for (i = 0; i < PAGE_CACHE_LEN (pixbuf_cache); i++) {
748 dispose_cache_job_info (pixbuf_cache->job_list + i, pixbuf_cache);
754 ev_pixbuf_cache_style_changed (EvPixbufCache *pixbuf_cache)
758 /* FIXME: doesn't update running jobs. */
759 for (i = 0; i < pixbuf_cache->preload_cache_size; i++) {
760 CacheJobInfo *job_info;
762 job_info = pixbuf_cache->prev_job + i;
763 if (job_info->selection) {
764 cairo_surface_destroy (job_info->selection);
765 job_info->selection = NULL;
768 job_info = pixbuf_cache->next_job + i;
769 if (job_info->selection) {
770 cairo_surface_destroy (job_info->selection);
771 job_info->selection = NULL;
775 for (i = 0; i < PAGE_CACHE_LEN (pixbuf_cache); i++) {
776 CacheJobInfo *job_info;
778 job_info = pixbuf_cache->job_list + i;
779 if (job_info->selection) {
780 cairo_surface_destroy (job_info->selection);
781 job_info->selection = NULL;
787 ev_pixbuf_cache_get_selection_surface (EvPixbufCache *pixbuf_cache,
792 CacheJobInfo *job_info;
794 /* the document does not implement the selection interface */
795 if (!EV_IS_SELECTION (pixbuf_cache->document))
798 job_info = find_job_cache (pixbuf_cache, page);
799 if (job_info == NULL)
802 /* No selection on this page */
803 if (!job_info->points_set)
806 /* Create new render context if needed (selection + fast scrolling) */
807 if (job_info->rc == NULL) {
809 ev_page = ev_document_get_page (pixbuf_cache->document, page);
810 job_info->rc = ev_render_context_new (ev_page, 0, scale);
811 g_object_unref (ev_page);
815 ev_render_context_set_scale (job_info->rc, scale);
817 /* If we have a running job, we just return what we have under the
818 * assumption that it'll be updated later and we can scale it as need
820 if (job_info->job && EV_JOB_RENDER (job_info->job)->include_selection)
821 return job_info->selection;
823 /* Now, lets see if we need to resize the image. If we do, we clear the
825 clear_selection_if_needed (pixbuf_cache, job_info, page, scale);
827 /* Finally, we see if the two scales are the same, and get a new pixbuf
828 * if needed. We do this synchronously for now. At some point, we
829 * _should_ be able to get rid of the doc_mutex, so the synchronicity
830 * doesn't kill us. Rendering a few glyphs should really be fast.
832 if (ev_rect_cmp (&(job_info->target_points), &(job_info->selection_points))) {
833 EvRectangle *old_points;
834 GdkColor *text, *base;
836 /* we need to get a new selection pixbuf */
837 ev_document_doc_mutex_lock ();
838 if (job_info->selection_points.x1 < 0) {
839 g_assert (job_info->selection == NULL);
842 g_assert (job_info->selection != NULL);
843 old_points = &(job_info->selection_points);
846 if (job_info->selection_region)
847 gdk_region_destroy (job_info->selection_region);
848 job_info->selection_region =
849 ev_selection_get_selection_region (EV_SELECTION (pixbuf_cache->document),
851 job_info->selection_style,
852 &(job_info->target_points));
854 gtk_widget_ensure_style (pixbuf_cache->view);
856 get_selection_colors (pixbuf_cache->view, &text, &base);
858 ev_selection_render_selection (EV_SELECTION (pixbuf_cache->document),
859 job_info->rc, &(job_info->selection),
860 &(job_info->target_points),
862 job_info->selection_style,
864 job_info->selection_points = job_info->target_points;
865 ev_document_doc_mutex_unlock ();
868 *region = job_info->selection_region;
869 return job_info->selection;
873 update_job_selection (CacheJobInfo *job_info,
874 EvViewSelection *selection)
876 job_info->points_set = TRUE;
877 job_info->target_points = selection->rect;
878 job_info->selection_style = selection->style;
882 clear_job_selection (CacheJobInfo *job_info)
884 job_info->points_set = FALSE;
885 job_info->selection_points.x1 = -1;
887 if (job_info->selection) {
888 cairo_surface_destroy (job_info->selection);
889 job_info->selection = NULL;
893 /* This function will reset the selection on pages that no longer have them, and
894 * will update the target_selection on those that need it. It will _not_ free
895 * the previous selection_list -- that's up to caller to do.
898 ev_pixbuf_cache_set_selection_list (EvPixbufCache *pixbuf_cache,
899 GList *selection_list)
901 EvViewSelection *selection;
902 GList *list = selection_list;
906 g_return_if_fail (EV_IS_PIXBUF_CACHE (pixbuf_cache));
908 if (!EV_IS_SELECTION (pixbuf_cache->document))
911 /* We check each area to see what needs updating, and what needs freeing; */
912 page = pixbuf_cache->start_page - pixbuf_cache->preload_cache_size;
913 for (i = 0; i < pixbuf_cache->preload_cache_size; i++) {
921 if (((EvViewSelection *)list->data)->page == page) {
922 selection = list->data;
924 } else if (((EvViewSelection *)list->data)->page > page)
930 update_job_selection (pixbuf_cache->prev_job + i, selection);
932 clear_job_selection (pixbuf_cache->prev_job + i);
936 page = pixbuf_cache->start_page;
937 for (i = 0; i < PAGE_CACHE_LEN (pixbuf_cache); i++) {
940 if (((EvViewSelection *)list->data)->page == page) {
941 selection = list->data;
943 } else if (((EvViewSelection *)list->data)->page > page)
949 update_job_selection (pixbuf_cache->job_list + i, selection);
951 clear_job_selection (pixbuf_cache->job_list + i);
955 for (i = 0; i < pixbuf_cache->preload_cache_size; i++) {
956 if (page >= ev_document_get_n_pages (pixbuf_cache->document))
961 if (((EvViewSelection *)list->data)->page == page) {
962 selection = list->data;
964 } else if (((EvViewSelection *)list->data)->page > page)
970 update_job_selection (pixbuf_cache->next_job + i, selection);
972 clear_job_selection (pixbuf_cache->next_job + i);
978 /* Returns what the pixbuf cache thinks is */
981 ev_pixbuf_cache_get_selection_list (EvPixbufCache *pixbuf_cache)
983 EvViewSelection *selection;
984 GList *retval = NULL;
988 g_return_val_if_fail (EV_IS_PIXBUF_CACHE (pixbuf_cache), NULL);
990 /* We check each area to see what needs updating, and what needs freeing; */
991 page = pixbuf_cache->start_page - pixbuf_cache->preload_cache_size;
992 for (i = 0; i < pixbuf_cache->preload_cache_size; i++) {
998 if (pixbuf_cache->prev_job[i].selection_points.x1 != -1) {
999 selection = g_new0 (EvViewSelection, 1);
1000 selection->page = page;
1001 selection->rect = pixbuf_cache->prev_job[i].selection_points;
1002 if (pixbuf_cache->prev_job[i].selection_region)
1003 selection->covered_region = gdk_region_copy (pixbuf_cache->prev_job[i].selection_region);
1004 retval = g_list_append (retval, selection);
1010 page = pixbuf_cache->start_page;
1011 for (i = 0; i < PAGE_CACHE_LEN (pixbuf_cache); i++) {
1012 if (pixbuf_cache->job_list[i].selection_points.x1 != -1) {
1013 selection = g_new0 (EvViewSelection, 1);
1014 selection->page = page;
1015 selection->rect = pixbuf_cache->job_list[i].selection_points;
1016 if (pixbuf_cache->job_list[i].selection_region)
1017 selection->covered_region = gdk_region_copy (pixbuf_cache->job_list[i].selection_region);
1018 retval = g_list_append (retval, selection);
1024 for (i = 0; i < pixbuf_cache->preload_cache_size; i++) {
1025 if (page >= ev_document_get_n_pages (pixbuf_cache->document))
1028 if (pixbuf_cache->next_job[i].selection_points.x1 != -1) {
1029 selection = g_new0 (EvViewSelection, 1);
1030 selection->page = page;
1031 selection->rect = pixbuf_cache->next_job[i].selection_points;
1032 if (pixbuf_cache->next_job[i].selection_region)
1033 selection->covered_region = gdk_region_copy (pixbuf_cache->next_job[i].selection_region);
1034 retval = g_list_append (retval, selection);
1044 ev_pixbuf_cache_reload_page (EvPixbufCache *pixbuf_cache,
1050 CacheJobInfo *job_info;
1053 job_info = find_job_cache (pixbuf_cache, page);
1054 if (job_info == NULL)
1057 _get_page_size_for_scale_and_rotation (pixbuf_cache->document,
1058 page, scale, rotation,
1060 add_job (pixbuf_cache, job_info, region,
1061 width, height, page, rotation, scale,
1062 EV_JOB_PRIORITY_URGENT);