2 #include "ev-pixbuf-cache.h"
3 #include "ev-job-scheduler.h"
4 #include "ev-page-cache.h"
5 #include "ev-document-images.h"
6 #include "ev-document-forms.h"
7 #include "ev-document-links.h"
8 #include "ev-document-annotations.h"
10 #include "ev-form-field.h"
11 #include "ev-annotation.h"
13 typedef struct _CacheJobInfo
19 /* Region of the page that needs to be drawn */
22 /* Data we get from rendering */
23 cairo_surface_t *surface;
26 GList *form_field_mapping;
27 GList *annots_mapping;
28 GdkRegion *text_mapping;
31 * Selection_points are the coordinates encapsulated in selection.
32 * target_points is the target selection size. */
33 EvRectangle selection_points;
34 EvRectangle target_points;
35 EvSelectionStyle selection_style;
38 cairo_surface_t *selection;
39 GdkRegion *selection_region;
46 /* We keep a link to our containing view just for style information. */
52 /* preload_cache_size is the number of pages prior to the current
53 * visible area that we cache. It's normally 1, but could be 2 in the
56 int preload_cache_size;
57 CacheJobInfo *prev_job;
58 CacheJobInfo *job_list;
59 CacheJobInfo *next_job;
62 struct _EvPixbufCacheClass
64 GObjectClass parent_class;
66 void (* job_finished) (EvPixbufCache *pixbuf_cache);
76 static guint signals[N_SIGNALS] = {0, };
78 static void ev_pixbuf_cache_init (EvPixbufCache *pixbuf_cache);
79 static void ev_pixbuf_cache_class_init (EvPixbufCacheClass *pixbuf_cache);
80 static void ev_pixbuf_cache_finalize (GObject *object);
81 static void ev_pixbuf_cache_dispose (GObject *object);
82 static void job_page_ready_cb (EvJob *job,
83 EvPixbufCache *pixbuf_cache);
84 static void job_finished_cb (EvJob *job,
85 EvPixbufCache *pixbuf_cache);
86 static CacheJobInfo *find_job_cache (EvPixbufCache *pixbuf_cache,
88 static void copy_job_to_job_info (EvJobRender *job_render,
89 CacheJobInfo *job_info,
90 EvPixbufCache *pixbuf_cache);
91 static void copy_job_page_and_selection_to_job_info (EvJobRender *job_render,
92 CacheJobInfo *job_info,
93 EvPixbufCache *pixbuf_cache);
94 static gboolean new_selection_surface_needed(EvPixbufCache *pixbuf_cache,
95 CacheJobInfo *job_info,
100 /* These are used for iterating through the prev and next arrays */
101 #define FIRST_VISIBLE_PREV(pixbuf_cache) \
102 (MAX (0, pixbuf_cache->preload_cache_size - pixbuf_cache->start_page))
103 #define VISIBLE_NEXT_LEN(pixbuf_cache, page_cache) \
104 (MIN(pixbuf_cache->preload_cache_size, ev_page_cache_get_n_pages (page_cache) - (1 + pixbuf_cache->end_page)))
105 #define PAGE_CACHE_LEN(pixbuf_cache) \
106 ((pixbuf_cache->end_page - pixbuf_cache->start_page) + 1)
108 G_DEFINE_TYPE (EvPixbufCache, ev_pixbuf_cache, G_TYPE_OBJECT)
111 ev_pixbuf_cache_init (EvPixbufCache *pixbuf_cache)
113 pixbuf_cache->start_page = 0;
114 pixbuf_cache->end_page = 0;
115 pixbuf_cache->job_list = g_new0 (CacheJobInfo, PAGE_CACHE_LEN (pixbuf_cache));
117 pixbuf_cache->preload_cache_size = 2;
118 pixbuf_cache->prev_job = g_new0 (CacheJobInfo, pixbuf_cache->preload_cache_size);
119 pixbuf_cache->next_job = g_new0 (CacheJobInfo, pixbuf_cache->preload_cache_size);
123 ev_pixbuf_cache_class_init (EvPixbufCacheClass *class)
125 GObjectClass *object_class;
127 object_class = G_OBJECT_CLASS (class);
129 object_class->finalize = ev_pixbuf_cache_finalize;
130 object_class->dispose = ev_pixbuf_cache_dispose;
132 signals[JOB_FINISHED] =
133 g_signal_new ("job-finished",
134 G_OBJECT_CLASS_TYPE (object_class),
135 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
136 G_STRUCT_OFFSET (EvPixbufCacheClass, job_finished),
138 g_cclosure_marshal_VOID__POINTER,
144 ev_pixbuf_cache_finalize (GObject *object)
146 EvPixbufCache *pixbuf_cache;
148 pixbuf_cache = EV_PIXBUF_CACHE (object);
150 g_free (pixbuf_cache->prev_job);
151 g_free (pixbuf_cache->job_list);
152 g_free (pixbuf_cache->next_job);
154 G_OBJECT_CLASS (ev_pixbuf_cache_parent_class)->finalize (object);
158 dispose_cache_job_info (CacheJobInfo *job_info,
161 if (job_info == NULL)
164 g_signal_handlers_disconnect_by_func (job_info->job,
165 G_CALLBACK (job_page_ready_cb),
167 g_signal_handlers_disconnect_by_func (job_info->job,
168 G_CALLBACK (job_finished_cb),
170 ev_job_cancel (job_info->job);
171 g_object_unref (job_info->job);
172 job_info->job = NULL;
174 if (job_info->surface) {
175 cairo_surface_destroy (job_info->surface);
176 job_info->surface = NULL;
178 if (job_info->region) {
179 gdk_region_destroy (job_info->region);
180 job_info->region = NULL;
182 if (job_info->link_mapping) {
183 ev_link_mapping_free (job_info->link_mapping);
184 job_info->link_mapping = NULL;
186 if (job_info->image_mapping) {
187 ev_image_mapping_free (job_info->image_mapping);
188 job_info->image_mapping = NULL;
190 if (job_info->form_field_mapping) {
191 ev_form_field_mapping_free (job_info->form_field_mapping);
192 job_info->form_field_mapping = NULL;
194 if (job_info->annots_mapping) {
195 ev_annotation_mapping_free (job_info->annots_mapping);
196 job_info->annots_mapping = NULL;
198 if (job_info->text_mapping) {
199 gdk_region_destroy (job_info->text_mapping);
200 job_info->text_mapping = NULL;
202 if (job_info->selection) {
203 cairo_surface_destroy (job_info->selection);
204 job_info->selection = NULL;
206 if (job_info->selection_region) {
207 gdk_region_destroy (job_info->selection_region);
208 job_info->selection_region = NULL;
211 g_object_unref (G_OBJECT (job_info->rc));
215 job_info->points_set = FALSE;
219 ev_pixbuf_cache_dispose (GObject *object)
221 EvPixbufCache *pixbuf_cache;
224 pixbuf_cache = EV_PIXBUF_CACHE (object);
226 for (i = 0; i < pixbuf_cache->preload_cache_size; i++) {
227 dispose_cache_job_info (pixbuf_cache->prev_job + i, pixbuf_cache);
228 dispose_cache_job_info (pixbuf_cache->next_job + i, pixbuf_cache);
231 for (i = 0; i < PAGE_CACHE_LEN (pixbuf_cache); i++) {
232 dispose_cache_job_info (pixbuf_cache->job_list + i, pixbuf_cache);
235 G_OBJECT_CLASS (ev_pixbuf_cache_parent_class)->dispose (object);
240 ev_pixbuf_cache_new (GtkWidget *view,
241 EvDocument *document)
243 EvPixbufCache *pixbuf_cache;
245 pixbuf_cache = (EvPixbufCache *) g_object_new (EV_TYPE_PIXBUF_CACHE, NULL);
246 /* This is a backlink, so we don't ref this */
247 pixbuf_cache->view = view;
248 pixbuf_cache->document = document;
254 job_page_ready_cb (EvJob *job,
255 EvPixbufCache *pixbuf_cache)
257 CacheJobInfo *job_info;
258 EvJobRender *job_render = EV_JOB_RENDER (job);
260 /* If the job is outside of our interest, we silently discard it */
261 if ((job_render->page < (pixbuf_cache->start_page - pixbuf_cache->preload_cache_size)) ||
262 (job_render->page > (pixbuf_cache->end_page + pixbuf_cache->preload_cache_size))) {
263 g_object_unref (job);
267 job_info = find_job_cache (pixbuf_cache, job_render->page);
269 copy_job_page_and_selection_to_job_info (job_render, job_info, pixbuf_cache);
270 g_signal_emit (pixbuf_cache, signals[JOB_FINISHED], 0, job_info->region);
274 job_finished_cb (EvJob *job,
275 EvPixbufCache *pixbuf_cache)
277 CacheJobInfo *job_info;
278 EvJobRender *job_render = EV_JOB_RENDER (job);
280 /* If the job is outside of our interest, we silently discard it */
281 if ((job_render->page < (pixbuf_cache->start_page - pixbuf_cache->preload_cache_size)) ||
282 (job_render->page > (pixbuf_cache->end_page + pixbuf_cache->preload_cache_size))) {
283 g_object_unref (job);
287 job_info = find_job_cache (pixbuf_cache, job_render->page);
288 copy_job_to_job_info (job_render, job_info, pixbuf_cache);
291 /* This checks a job to see if the job would generate the right sized pixbuf
292 * given a scale. If it won't, it removes the job and clears it to NULL.
295 check_job_size_and_unref (EvPixbufCache *pixbuf_cache,
296 CacheJobInfo *job_info,
297 EvPageCache *page_cache,
305 if (job_info->job == NULL)
308 ev_page_cache_get_size (page_cache,
309 EV_JOB_RENDER (job_info->job)->page,
310 EV_JOB_RENDER (job_info->job)->rotation,
314 if (width == EV_JOB_RENDER (job_info->job)->target_width &&
315 height == EV_JOB_RENDER (job_info->job)->target_height)
318 g_signal_handlers_disconnect_by_func (job_info->job,
319 G_CALLBACK (job_page_ready_cb),
321 g_signal_handlers_disconnect_by_func (job_info->job,
322 G_CALLBACK (job_finished_cb),
324 ev_job_cancel (job_info->job);
325 g_object_unref (job_info->job);
326 job_info->job = NULL;
329 /* Do all function that copies a job from an older cache to it's position in the
330 * new cache. It clears the old job if it doesn't have a place.
333 move_one_job (CacheJobInfo *job_info,
334 EvPixbufCache *pixbuf_cache,
336 CacheJobInfo *new_job_list,
337 CacheJobInfo *new_prev_job,
338 CacheJobInfo *new_next_job,
343 CacheJobInfo *target_page = NULL;
347 if (page < (start_page - pixbuf_cache->preload_cache_size) ||
348 page > (end_page + pixbuf_cache->preload_cache_size)) {
349 dispose_cache_job_info (job_info, pixbuf_cache);
353 /* find the target page to copy it over to. */
354 if (page < start_page) {
355 page_offset = (page - (start_page - pixbuf_cache->preload_cache_size));
357 g_assert (page_offset >= 0 &&
358 page_offset < pixbuf_cache->preload_cache_size);
359 target_page = new_prev_job + page_offset;
360 new_priority = EV_JOB_PRIORITY_LOW;
361 } else if (page > end_page) {
362 page_offset = (page - (end_page + 1));
364 g_assert (page_offset >= 0 &&
365 page_offset < pixbuf_cache->preload_cache_size);
366 target_page = new_next_job + page_offset;
367 new_priority = EV_JOB_PRIORITY_LOW;
369 page_offset = page - start_page;
370 g_assert (page_offset >= 0 &&
371 page_offset <= ((end_page - start_page) + 1));
372 new_priority = EV_JOB_PRIORITY_URGENT;
373 target_page = new_job_list + page_offset;
376 *target_page = *job_info;
377 job_info->job = NULL;
378 job_info->region = NULL;
379 job_info->surface = NULL;
380 job_info->link_mapping = NULL;
381 job_info->image_mapping = NULL;
382 job_info->form_field_mapping = NULL;
383 job_info->annots_mapping = NULL;
385 if (new_priority != priority && target_page->job) {
386 ev_job_scheduler_update_job (target_page->job, new_priority);
391 ev_pixbuf_cache_update_range (EvPixbufCache *pixbuf_cache,
395 CacheJobInfo *new_job_list;
396 CacheJobInfo *new_prev_job;
397 CacheJobInfo *new_next_job;
398 EvPageCache *page_cache;
401 if (pixbuf_cache->start_page == start_page &&
402 pixbuf_cache->end_page == end_page)
405 page_cache = ev_page_cache_get (pixbuf_cache->document);
407 new_job_list = g_new0 (CacheJobInfo, (end_page - start_page) + 1);
408 new_prev_job = g_new0 (CacheJobInfo, pixbuf_cache->preload_cache_size);
409 new_next_job = g_new0 (CacheJobInfo, pixbuf_cache->preload_cache_size);
411 /* We go through each job in the old cache and either clear it or move
412 * it to a new location. */
414 /* Start with the prev cache. */
415 page = pixbuf_cache->start_page - pixbuf_cache->preload_cache_size;
416 for (i = 0; i < pixbuf_cache->preload_cache_size; i++) {
418 dispose_cache_job_info (pixbuf_cache->prev_job + i, pixbuf_cache);
420 move_one_job (pixbuf_cache->prev_job + i,
422 new_job_list, new_prev_job, new_next_job,
423 start_page, end_page, EV_JOB_PRIORITY_LOW);
428 page = pixbuf_cache->start_page;
429 for (i = 0; i < PAGE_CACHE_LEN (pixbuf_cache); i++) {
430 move_one_job (pixbuf_cache->job_list + i,
432 new_job_list, new_prev_job, new_next_job,
433 start_page, end_page, EV_JOB_PRIORITY_URGENT);
437 for (i = 0; i < pixbuf_cache->preload_cache_size; i++) {
438 if (page >= ev_page_cache_get_n_pages (page_cache)) {
439 dispose_cache_job_info (pixbuf_cache->next_job + i, pixbuf_cache);
441 move_one_job (pixbuf_cache->next_job + i,
443 new_job_list, new_prev_job, new_next_job,
444 start_page, end_page, EV_JOB_PRIORITY_LOW);
449 g_free (pixbuf_cache->job_list);
450 g_free (pixbuf_cache->prev_job);
451 g_free (pixbuf_cache->next_job);
453 pixbuf_cache->job_list = new_job_list;
454 pixbuf_cache->prev_job = new_prev_job;
455 pixbuf_cache->next_job = new_next_job;
457 pixbuf_cache->start_page = start_page;
458 pixbuf_cache->end_page = end_page;
462 copy_job_page_and_selection_to_job_info (EvJobRender *job_render,
463 CacheJobInfo *job_info,
464 EvPixbufCache *pixbuf_cache)
466 if (job_info->rc == NULL) {
467 job_info->rc = ev_render_context_new (job_render->ev_page,
468 job_render->rotation,
471 ev_render_context_set_page (job_info->rc, job_render->ev_page);
472 ev_render_context_set_rotation (job_info->rc, job_render->rotation);
473 ev_render_context_set_scale (job_info->rc, job_render->scale);
476 if (job_info->surface) {
477 cairo_surface_destroy (job_info->surface);
479 job_info->surface = cairo_surface_reference (job_render->surface);
481 job_info->points_set = FALSE;
482 if (job_render->flags & EV_RENDER_INCLUDE_SELECTION) {
483 if (job_info->selection) {
484 cairo_surface_destroy (job_info->selection);
485 job_info->selection = NULL;
487 if (job_info->selection_region) {
488 gdk_region_destroy (job_info->selection_region);
489 job_info->selection_region = NULL;
492 job_info->selection_points = job_render->selection_points;
493 job_info->selection_region = gdk_region_copy (job_render->selection_region);
494 job_info->selection = cairo_surface_reference (job_render->selection);
495 g_assert (job_info->selection_points.x1 >= 0);
496 job_info->points_set = TRUE;
500 g_signal_handlers_disconnect_by_func (job_info->job,
501 G_CALLBACK (job_page_ready_cb),
505 job_info->page_ready = TRUE;
509 copy_job_to_job_info (EvJobRender *job_render,
510 CacheJobInfo *job_info,
511 EvPixbufCache *pixbuf_cache)
513 if (!job_info->page_ready) {
514 g_signal_emit (pixbuf_cache, signals[JOB_FINISHED], 0, job_info->region);
515 copy_job_page_and_selection_to_job_info (job_render,
520 if (job_render->flags & EV_RENDER_INCLUDE_LINKS) {
521 if (job_info->link_mapping)
522 ev_link_mapping_free (job_info->link_mapping);
523 job_info->link_mapping = job_render->link_mapping;
526 if (job_render->flags & EV_RENDER_INCLUDE_IMAGES) {
527 if (job_info->image_mapping)
528 ev_image_mapping_free (job_info->image_mapping);
529 job_info->image_mapping = job_render->image_mapping;
532 if (job_render->flags & EV_RENDER_INCLUDE_FORMS) {
533 if (job_info->form_field_mapping)
534 ev_form_field_mapping_free (job_info->form_field_mapping);
535 job_info->form_field_mapping = job_render->form_field_mapping;
538 if (job_render->flags & EV_RENDER_INCLUDE_ANNOTS) {
539 if (job_info->annots_mapping)
540 ev_annotation_mapping_free (job_info->annots_mapping);
541 job_info->annots_mapping = job_render->annots_mapping;
544 if (job_render->flags & EV_RENDER_INCLUDE_TEXT) {
545 if (job_info->text_mapping)
546 gdk_region_destroy (job_info->text_mapping);
547 job_info->text_mapping = job_render->text_mapping;
551 g_signal_handlers_disconnect_by_func (job_info->job,
552 G_CALLBACK (job_finished_cb),
554 ev_job_cancel (job_info->job);
555 g_object_unref (job_info->job);
556 job_info->job = NULL;
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,
596 EvPageCache *page_cache;
599 page_cache = ev_page_cache_get (pixbuf_cache->document);
601 for (i = 0; i < PAGE_CACHE_LEN (pixbuf_cache); i++) {
602 check_job_size_and_unref (pixbuf_cache, pixbuf_cache->job_list + i, page_cache, scale);
605 for (i = 0; i < pixbuf_cache->preload_cache_size; i++) {
606 check_job_size_and_unref (pixbuf_cache, pixbuf_cache->prev_job + i, page_cache, scale);
607 check_job_size_and_unref (pixbuf_cache, pixbuf_cache->next_job + i, page_cache, scale);
612 get_selection_colors (GtkWidget *widget, GdkColor **text, GdkColor **base)
614 if (GTK_WIDGET_HAS_FOCUS (widget)) {
615 *text = &widget->style->text [GTK_STATE_SELECTED];
616 *base = &widget->style->base [GTK_STATE_SELECTED];
618 *text = &widget->style->text [GTK_STATE_ACTIVE];
619 *base = &widget->style->base [GTK_STATE_ACTIVE];
624 add_job (EvPixbufCache *pixbuf_cache,
625 CacheJobInfo *job_info,
626 EvPageCache *page_cache,
633 EvJobPriority priority)
635 EvRenderFlags flags = 0;
637 job_info->page_ready = FALSE;
639 if (job_info->region)
640 gdk_region_destroy (job_info->region);
641 job_info->region = region ? gdk_region_copy (region) : NULL;
643 /* Figure out what else we need for this job */
644 if (job_info->link_mapping == NULL)
645 flags |= EV_RENDER_INCLUDE_LINKS;
646 if (job_info->image_mapping == NULL)
647 flags |= EV_RENDER_INCLUDE_IMAGES;
648 if (job_info->form_field_mapping == NULL)
649 flags |= EV_RENDER_INCLUDE_FORMS;
650 if (job_info->annots_mapping == NULL)
651 flags |= EV_RENDER_INCLUDE_ANNOTS;
652 if (job_info->text_mapping == NULL)
653 flags |= EV_RENDER_INCLUDE_TEXT;
655 job_info->job = ev_job_render_new (pixbuf_cache->document,
656 page, rotation, scale,
660 if (new_selection_surface_needed (pixbuf_cache, job_info, page, scale)) {
661 GdkColor *text, *base;
663 gtk_widget_ensure_style (pixbuf_cache->view);
664 get_selection_colors (pixbuf_cache->view, &text, &base);
665 ev_job_render_set_selection_info (EV_JOB_RENDER (job_info->job),
666 &(job_info->target_points),
667 job_info->selection_style,
671 g_signal_connect (job_info->job, "page-ready",
672 G_CALLBACK (job_page_ready_cb),
674 g_signal_connect (job_info->job, "finished",
675 G_CALLBACK (job_finished_cb),
677 ev_job_scheduler_push_job (job_info->job, priority);
681 add_job_if_needed (EvPixbufCache *pixbuf_cache,
682 CacheJobInfo *job_info,
683 EvPageCache *page_cache,
687 EvJobPriority priority)
694 ev_page_cache_get_size (page_cache, page, rotation,
695 scale, &width, &height);
697 if (job_info->surface &&
698 cairo_image_surface_get_width (job_info->surface) == width &&
699 cairo_image_surface_get_height (job_info->surface) == height)
702 add_job (pixbuf_cache, job_info, page_cache, NULL,
703 width, height, page, rotation, scale,
708 ev_pixbuf_cache_add_jobs_if_needed (EvPixbufCache *pixbuf_cache,
712 EvPageCache *page_cache;
713 CacheJobInfo *job_info;
717 page_cache = ev_page_cache_get (pixbuf_cache->document);
719 for (i = 0; i < PAGE_CACHE_LEN (pixbuf_cache); i++) {
720 job_info = (pixbuf_cache->job_list + i);
721 page = pixbuf_cache->start_page + i;
723 add_job_if_needed (pixbuf_cache, job_info,
724 page_cache, page, rotation, scale,
725 EV_JOB_PRIORITY_URGENT);
728 for (i = FIRST_VISIBLE_PREV(pixbuf_cache); i < pixbuf_cache->preload_cache_size; i++) {
729 job_info = (pixbuf_cache->prev_job + i);
730 page = pixbuf_cache->start_page - pixbuf_cache->preload_cache_size + i;
732 add_job_if_needed (pixbuf_cache, job_info,
733 page_cache, page, rotation, scale,
734 EV_JOB_PRIORITY_LOW);
737 for (i = 0; i < VISIBLE_NEXT_LEN(pixbuf_cache, page_cache); i++) {
738 job_info = (pixbuf_cache->next_job + i);
739 page = pixbuf_cache->end_page + 1 + i;
741 add_job_if_needed (pixbuf_cache, job_info,
742 page_cache, page, rotation, scale,
743 EV_JOB_PRIORITY_LOW);
749 ev_pixbuf_cache_set_page_range (EvPixbufCache *pixbuf_cache,
754 GList *selection_list)
756 EvPageCache *page_cache;
758 g_return_if_fail (EV_IS_PIXBUF_CACHE (pixbuf_cache));
760 page_cache = ev_page_cache_get (pixbuf_cache->document);
762 g_return_if_fail (start_page >= 0 && start_page < ev_page_cache_get_n_pages (page_cache));
763 g_return_if_fail (end_page >= 0 && end_page < ev_page_cache_get_n_pages (page_cache));
764 g_return_if_fail (end_page >= start_page);
766 /* First, resize the page_range as needed. We cull old pages
768 ev_pixbuf_cache_update_range (pixbuf_cache, start_page, end_page);
770 /* Then, we update the current jobs to see if any of them are the wrong
771 * size, we remove them if we need to. */
772 ev_pixbuf_cache_clear_job_sizes (pixbuf_cache, scale);
774 /* Next, we update the target selection for our pages */
775 ev_pixbuf_cache_set_selection_list (pixbuf_cache, selection_list);
777 /* Finally, we add the new jobs for all the sizes that don't have a
779 ev_pixbuf_cache_add_jobs_if_needed (pixbuf_cache, rotation, scale);
783 ev_pixbuf_cache_get_surface (EvPixbufCache *pixbuf_cache,
786 CacheJobInfo *job_info;
788 job_info = find_job_cache (pixbuf_cache, page);
789 if (job_info == NULL)
792 if (job_info->page_ready)
793 return job_info->surface;
795 /* We don't need to wait for the idle to handle the callback */
797 EV_JOB_RENDER (job_info->job)->page_ready) {
798 copy_job_page_and_selection_to_job_info (EV_JOB_RENDER (job_info->job), job_info, pixbuf_cache);
799 g_signal_emit (pixbuf_cache, signals[JOB_FINISHED], 0, job_info->region);
802 return job_info->surface;
806 ev_pixbuf_cache_get_link_mapping (EvPixbufCache *pixbuf_cache,
809 CacheJobInfo *job_info;
811 job_info = find_job_cache (pixbuf_cache, page);
812 if (job_info == NULL)
815 /* We don't need to wait for the idle to handle the callback */
817 EV_JOB (job_info->job)->finished) {
818 copy_job_to_job_info (EV_JOB_RENDER (job_info->job), job_info, pixbuf_cache);
821 return job_info->link_mapping;
825 ev_pixbuf_cache_get_image_mapping (EvPixbufCache *pixbuf_cache,
828 CacheJobInfo *job_info;
830 if (!EV_IS_DOCUMENT_IMAGES (pixbuf_cache->document))
833 job_info = find_job_cache (pixbuf_cache, page);
834 if (job_info == NULL)
837 /* We don't need to wait for the idle to handle the callback */
839 EV_JOB (job_info->job)->finished) {
840 copy_job_to_job_info (EV_JOB_RENDER (job_info->job), job_info, pixbuf_cache);
843 return job_info->image_mapping;
847 ev_pixbuf_cache_get_form_field_mapping (EvPixbufCache *pixbuf_cache,
850 CacheJobInfo *job_info;
852 if (!EV_IS_DOCUMENT_FORMS (pixbuf_cache->document))
855 job_info = find_job_cache (pixbuf_cache, page);
856 if (job_info == NULL)
859 /* We don't need to wait for the idle to handle the callback */
861 EV_JOB (job_info->job)->finished) {
862 copy_job_to_job_info (EV_JOB_RENDER(job_info->job), job_info, pixbuf_cache);
865 return job_info->form_field_mapping;
869 ev_pixbuf_cache_get_annots_mapping (EvPixbufCache *pixbuf_cache,
872 CacheJobInfo *job_info;
874 if (!EV_IS_DOCUMENT_ANNOTATIONS (pixbuf_cache->document))
877 job_info = find_job_cache (pixbuf_cache, page);
878 if (job_info == NULL)
881 /* We don't need to wait for the idle to handle the callback */
883 EV_JOB (job_info->job)->finished) {
884 copy_job_to_job_info (EV_JOB_RENDER (job_info->job), job_info, pixbuf_cache);
887 return job_info->annots_mapping;
891 new_selection_surface_needed (EvPixbufCache *pixbuf_cache,
892 CacheJobInfo *job_info,
896 EvPageCache *page_cache;
898 if (job_info->selection && job_info->rc) {
900 gint selection_width, selection_height;
902 page_cache = ev_page_cache_get (pixbuf_cache->document);
903 ev_page_cache_get_size (page_cache, page,
904 job_info->rc->rotation,
905 scale, &width, &height);
907 selection_width = cairo_image_surface_get_width (job_info->selection);
908 selection_height = cairo_image_surface_get_height (job_info->selection);
910 if (width != selection_width || height != selection_height)
913 if (job_info->points_set)
921 clear_selection_if_needed (EvPixbufCache *pixbuf_cache,
922 CacheJobInfo *job_info,
926 if (new_selection_surface_needed (pixbuf_cache, job_info, page, scale)) {
927 if (job_info->selection)
928 cairo_surface_destroy (job_info->selection);
929 job_info->selection = NULL;
930 job_info->selection_points.x1 = -1;
935 ev_pixbuf_cache_get_text_mapping (EvPixbufCache *pixbuf_cache,
938 CacheJobInfo *job_info;
940 job_info = find_job_cache (pixbuf_cache, page);
941 if (job_info == NULL)
944 /* We don't need to wait for the idle to handle the callback */
946 EV_JOB (job_info->job)->finished) {
947 copy_job_to_job_info (EV_JOB_RENDER (job_info->job), job_info, pixbuf_cache);
950 return job_info->text_mapping;
953 /* Clears the cache of jobs and pixbufs.
956 ev_pixbuf_cache_clear (EvPixbufCache *pixbuf_cache)
960 for (i = 0; i < pixbuf_cache->preload_cache_size; i++) {
961 dispose_cache_job_info (pixbuf_cache->prev_job + i, pixbuf_cache);
962 dispose_cache_job_info (pixbuf_cache->next_job + i, pixbuf_cache);
965 for (i = 0; i < PAGE_CACHE_LEN (pixbuf_cache); i++) {
966 dispose_cache_job_info (pixbuf_cache->job_list + i, pixbuf_cache);
972 ev_pixbuf_cache_style_changed (EvPixbufCache *pixbuf_cache)
976 /* FIXME: doesn't update running jobs. */
977 for (i = 0; i < pixbuf_cache->preload_cache_size; i++) {
978 CacheJobInfo *job_info;
980 job_info = pixbuf_cache->prev_job + i;
981 if (job_info->selection) {
982 cairo_surface_destroy (job_info->selection);
983 job_info->selection = NULL;
986 job_info = pixbuf_cache->next_job + i;
987 if (job_info->selection) {
988 cairo_surface_destroy (job_info->selection);
989 job_info->selection = NULL;
993 for (i = 0; i < PAGE_CACHE_LEN (pixbuf_cache); i++) {
994 CacheJobInfo *job_info;
996 job_info = pixbuf_cache->job_list + i;
997 if (job_info->selection) {
998 cairo_surface_destroy (job_info->selection);
999 job_info->selection = NULL;
1005 ev_pixbuf_cache_get_selection_surface (EvPixbufCache *pixbuf_cache,
1010 CacheJobInfo *job_info;
1012 /* the document does not implement the selection interface */
1013 if (!EV_IS_SELECTION (pixbuf_cache->document))
1016 job_info = find_job_cache (pixbuf_cache, page);
1017 if (job_info == NULL)
1020 /* No selection on this page */
1021 if (!job_info->points_set)
1024 /* Create new render context if needed (selection + fast scrolling) */
1025 if (job_info->rc == NULL) {
1027 ev_page = ev_document_get_page (pixbuf_cache->document, page);
1028 job_info->rc = ev_render_context_new (ev_page, 0, scale);
1029 g_object_unref (ev_page);
1033 ev_render_context_set_scale (job_info->rc, scale);
1035 /* If we have a running job, we just return what we have under the
1036 * assumption that it'll be updated later and we can scale it as need
1038 if (job_info->job && (EV_JOB_RENDER (job_info->job)->flags & EV_RENDER_INCLUDE_SELECTION))
1039 return job_info->selection;
1041 /* Now, lets see if we need to resize the image. If we do, we clear the
1043 clear_selection_if_needed (pixbuf_cache, job_info, page, scale);
1045 /* Finally, we see if the two scales are the same, and get a new pixbuf
1046 * if needed. We do this synchronously for now. At some point, we
1047 * _should_ be able to get rid of the doc_mutex, so the synchronicity
1048 * doesn't kill us. Rendering a few glyphs should really be fast.
1050 if (ev_rect_cmp (&(job_info->target_points), &(job_info->selection_points))) {
1051 EvRectangle *old_points;
1052 GdkColor *text, *base;
1054 /* we need to get a new selection pixbuf */
1055 ev_document_doc_mutex_lock ();
1056 if (job_info->selection_points.x1 < 0) {
1057 g_assert (job_info->selection == NULL);
1060 g_assert (job_info->selection != NULL);
1061 old_points = &(job_info->selection_points);
1064 if (job_info->selection_region)
1065 gdk_region_destroy (job_info->selection_region);
1066 job_info->selection_region =
1067 ev_selection_get_selection_region (EV_SELECTION (pixbuf_cache->document),
1069 job_info->selection_style,
1070 &(job_info->target_points));
1072 gtk_widget_ensure_style (pixbuf_cache->view);
1074 get_selection_colors (pixbuf_cache->view, &text, &base);
1076 ev_selection_render_selection (EV_SELECTION (pixbuf_cache->document),
1077 job_info->rc, &(job_info->selection),
1078 &(job_info->target_points),
1080 job_info->selection_style,
1082 job_info->selection_points = job_info->target_points;
1083 ev_document_doc_mutex_unlock ();
1086 *region = job_info->selection_region;
1087 return job_info->selection;
1091 update_job_selection (CacheJobInfo *job_info,
1092 EvViewSelection *selection)
1094 job_info->points_set = TRUE;
1095 job_info->target_points = selection->rect;
1096 job_info->selection_style = selection->style;
1100 clear_job_selection (CacheJobInfo *job_info)
1102 job_info->points_set = FALSE;
1103 job_info->selection_points.x1 = -1;
1105 if (job_info->selection) {
1106 cairo_surface_destroy (job_info->selection);
1107 job_info->selection = NULL;
1111 /* This function will reset the selection on pages that no longer have them, and
1112 * will update the target_selection on those that need it. It will _not_ free
1113 * the previous selection_list -- that's up to caller to do.
1116 ev_pixbuf_cache_set_selection_list (EvPixbufCache *pixbuf_cache,
1117 GList *selection_list)
1119 EvPageCache *page_cache;
1120 EvViewSelection *selection;
1121 GList *list = selection_list;
1125 g_return_if_fail (EV_IS_PIXBUF_CACHE (pixbuf_cache));
1127 if (!EV_IS_SELECTION (pixbuf_cache->document))
1130 page_cache = ev_page_cache_get (pixbuf_cache->document);
1132 /* We check each area to see what needs updating, and what needs freeing; */
1133 page = pixbuf_cache->start_page - pixbuf_cache->preload_cache_size;
1134 for (i = 0; i < pixbuf_cache->preload_cache_size; i++) {
1142 if (((EvViewSelection *)list->data)->page == page) {
1143 selection = list->data;
1145 } else if (((EvViewSelection *)list->data)->page > page)
1151 update_job_selection (pixbuf_cache->prev_job + i, selection);
1153 clear_job_selection (pixbuf_cache->prev_job + i);
1157 page = pixbuf_cache->start_page;
1158 for (i = 0; i < PAGE_CACHE_LEN (pixbuf_cache); i++) {
1161 if (((EvViewSelection *)list->data)->page == page) {
1162 selection = list->data;
1164 } else if (((EvViewSelection *)list->data)->page > page)
1170 update_job_selection (pixbuf_cache->job_list + i, selection);
1172 clear_job_selection (pixbuf_cache->job_list + i);
1176 for (i = 0; i < pixbuf_cache->preload_cache_size; i++) {
1177 if (page >= ev_page_cache_get_n_pages (page_cache))
1182 if (((EvViewSelection *)list->data)->page == page) {
1183 selection = list->data;
1185 } else if (((EvViewSelection *)list->data)->page > page)
1191 update_job_selection (pixbuf_cache->next_job + i, selection);
1193 clear_job_selection (pixbuf_cache->next_job + i);
1199 /* Returns what the pixbuf cache thinks is */
1202 ev_pixbuf_cache_get_selection_list (EvPixbufCache *pixbuf_cache)
1204 EvPageCache *page_cache;
1205 EvViewSelection *selection;
1206 GList *retval = NULL;
1210 g_return_val_if_fail (EV_IS_PIXBUF_CACHE (pixbuf_cache), NULL);
1212 page_cache = ev_page_cache_get (pixbuf_cache->document);
1214 /* We check each area to see what needs updating, and what needs freeing; */
1215 page = pixbuf_cache->start_page - pixbuf_cache->preload_cache_size;
1216 for (i = 0; i < pixbuf_cache->preload_cache_size; i++) {
1222 if (pixbuf_cache->prev_job[i].selection_points.x1 != -1) {
1223 selection = g_new0 (EvViewSelection, 1);
1224 selection->page = page;
1225 selection->rect = pixbuf_cache->prev_job[i].selection_points;
1226 if (pixbuf_cache->prev_job[i].selection_region)
1227 selection->covered_region = gdk_region_copy (pixbuf_cache->prev_job[i].selection_region);
1228 retval = g_list_append (retval, selection);
1234 page = pixbuf_cache->start_page;
1235 for (i = 0; i < PAGE_CACHE_LEN (pixbuf_cache); i++) {
1236 if (pixbuf_cache->job_list[i].selection_points.x1 != -1) {
1237 selection = g_new0 (EvViewSelection, 1);
1238 selection->page = page;
1239 selection->rect = pixbuf_cache->job_list[i].selection_points;
1240 if (pixbuf_cache->job_list[i].selection_region)
1241 selection->covered_region = gdk_region_copy (pixbuf_cache->job_list[i].selection_region);
1242 retval = g_list_append (retval, selection);
1248 for (i = 0; i < pixbuf_cache->preload_cache_size; i++) {
1249 if (page >= ev_page_cache_get_n_pages (page_cache))
1252 if (pixbuf_cache->next_job[i].selection_points.x1 != -1) {
1253 selection = g_new0 (EvViewSelection, 1);
1254 selection->page = page;
1255 selection->rect = pixbuf_cache->next_job[i].selection_points;
1256 if (pixbuf_cache->next_job[i].selection_region)
1257 selection->covered_region = gdk_region_copy (pixbuf_cache->next_job[i].selection_region);
1258 retval = g_list_append (retval, selection);
1268 ev_pixbuf_cache_reload_page (EvPixbufCache *pixbuf_cache,
1274 CacheJobInfo *job_info;
1275 EvPageCache *page_cache;
1278 job_info = find_job_cache (pixbuf_cache, page);
1279 if (job_info == NULL)
1282 page_cache = ev_page_cache_get (pixbuf_cache->document);
1283 ev_page_cache_get_size (page_cache, page, rotation, scale,
1286 add_job (pixbuf_cache, job_info, page_cache, region,
1287 width, height, page, rotation, scale,
1288 EV_JOB_PRIORITY_URGENT);