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 GList *form_field_mapping;
24 GList *annots_mapping;
25 GdkRegion *text_mapping;
28 * Selection_points are the coordinates encapsulated in selection.
29 * target_points is the target selection size. */
30 EvRectangle selection_points;
31 EvRectangle target_points;
32 EvSelectionStyle selection_style;
35 cairo_surface_t *selection;
36 GdkRegion *selection_region;
43 /* We keep a link to our containing view just for style information. */
48 gboolean inverted_colors;
50 /* preload_cache_size is the number of pages prior to the current
51 * visible area that we cache. It's normally 1, but could be 2 in the
54 int preload_cache_size;
55 CacheJobInfo *prev_job;
56 CacheJobInfo *job_list;
57 CacheJobInfo *next_job;
60 struct _EvPixbufCacheClass
62 GObjectClass parent_class;
64 void (* job_finished) (EvPixbufCache *pixbuf_cache);
74 static guint signals[N_SIGNALS] = {0, };
76 static void ev_pixbuf_cache_init (EvPixbufCache *pixbuf_cache);
77 static void ev_pixbuf_cache_class_init (EvPixbufCacheClass *pixbuf_cache);
78 static void ev_pixbuf_cache_finalize (GObject *object);
79 static void ev_pixbuf_cache_dispose (GObject *object);
80 static void job_page_ready_cb (EvJob *job,
81 EvPixbufCache *pixbuf_cache);
82 static void job_finished_cb (EvJob *job,
83 EvPixbufCache *pixbuf_cache);
84 static CacheJobInfo *find_job_cache (EvPixbufCache *pixbuf_cache,
86 static void copy_job_to_job_info (EvJobRender *job_render,
87 CacheJobInfo *job_info,
88 EvPixbufCache *pixbuf_cache);
89 static void copy_job_page_and_selection_to_job_info (EvJobRender *job_render,
90 CacheJobInfo *job_info,
91 EvPixbufCache *pixbuf_cache);
92 static gboolean new_selection_surface_needed(EvPixbufCache *pixbuf_cache,
93 CacheJobInfo *job_info,
98 /* These are used for iterating through the prev and next arrays */
99 #define FIRST_VISIBLE_PREV(pixbuf_cache) \
100 (MAX (0, pixbuf_cache->preload_cache_size - pixbuf_cache->start_page))
101 #define VISIBLE_NEXT_LEN(pixbuf_cache) \
102 (MIN(pixbuf_cache->preload_cache_size, ev_document_get_n_pages (pixbuf_cache->document) - (1 + pixbuf_cache->end_page)))
103 #define PAGE_CACHE_LEN(pixbuf_cache) \
104 ((pixbuf_cache->end_page - pixbuf_cache->start_page) + 1)
106 G_DEFINE_TYPE (EvPixbufCache, ev_pixbuf_cache, G_TYPE_OBJECT)
109 ev_pixbuf_cache_init (EvPixbufCache *pixbuf_cache)
111 pixbuf_cache->start_page = 0;
112 pixbuf_cache->end_page = 0;
113 pixbuf_cache->job_list = g_new0 (CacheJobInfo, PAGE_CACHE_LEN (pixbuf_cache));
115 pixbuf_cache->preload_cache_size = 2;
116 pixbuf_cache->prev_job = g_new0 (CacheJobInfo, pixbuf_cache->preload_cache_size);
117 pixbuf_cache->next_job = g_new0 (CacheJobInfo, pixbuf_cache->preload_cache_size);
121 ev_pixbuf_cache_class_init (EvPixbufCacheClass *class)
123 GObjectClass *object_class;
125 object_class = G_OBJECT_CLASS (class);
127 object_class->finalize = ev_pixbuf_cache_finalize;
128 object_class->dispose = ev_pixbuf_cache_dispose;
130 signals[JOB_FINISHED] =
131 g_signal_new ("job-finished",
132 G_OBJECT_CLASS_TYPE (object_class),
133 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
134 G_STRUCT_OFFSET (EvPixbufCacheClass, job_finished),
136 g_cclosure_marshal_VOID__POINTER,
142 ev_pixbuf_cache_finalize (GObject *object)
144 EvPixbufCache *pixbuf_cache;
146 pixbuf_cache = EV_PIXBUF_CACHE (object);
148 g_free (pixbuf_cache->prev_job);
149 g_free (pixbuf_cache->job_list);
150 g_free (pixbuf_cache->next_job);
152 G_OBJECT_CLASS (ev_pixbuf_cache_parent_class)->finalize (object);
156 dispose_cache_job_info (CacheJobInfo *job_info,
159 if (job_info == NULL)
162 g_signal_handlers_disconnect_by_func (job_info->job,
163 G_CALLBACK (job_page_ready_cb),
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->link_mapping) {
181 ev_mapping_list_free (job_info->link_mapping, g_object_unref);
182 job_info->link_mapping = NULL;
184 if (job_info->image_mapping) {
185 ev_mapping_list_free (job_info->image_mapping, g_object_unref);
186 job_info->image_mapping = NULL;
188 if (job_info->form_field_mapping) {
189 ev_mapping_list_free (job_info->form_field_mapping, g_object_unref);
190 job_info->form_field_mapping = NULL;
192 if (job_info->annots_mapping) {
193 ev_mapping_list_free (job_info->annots_mapping, g_object_unref);
194 job_info->annots_mapping = NULL;
196 if (job_info->text_mapping) {
197 gdk_region_destroy (job_info->text_mapping);
198 job_info->text_mapping = NULL;
200 if (job_info->selection) {
201 cairo_surface_destroy (job_info->selection);
202 job_info->selection = NULL;
204 if (job_info->selection_region) {
205 gdk_region_destroy (job_info->selection_region);
206 job_info->selection_region = NULL;
209 g_object_unref (G_OBJECT (job_info->rc));
213 job_info->points_set = FALSE;
217 ev_pixbuf_cache_dispose (GObject *object)
219 EvPixbufCache *pixbuf_cache;
222 pixbuf_cache = EV_PIXBUF_CACHE (object);
224 for (i = 0; i < pixbuf_cache->preload_cache_size; i++) {
225 dispose_cache_job_info (pixbuf_cache->prev_job + i, pixbuf_cache);
226 dispose_cache_job_info (pixbuf_cache->next_job + i, pixbuf_cache);
229 for (i = 0; i < PAGE_CACHE_LEN (pixbuf_cache); i++) {
230 dispose_cache_job_info (pixbuf_cache->job_list + i, pixbuf_cache);
233 G_OBJECT_CLASS (ev_pixbuf_cache_parent_class)->dispose (object);
238 ev_pixbuf_cache_new (GtkWidget *view,
239 EvDocument *document)
241 EvPixbufCache *pixbuf_cache;
243 pixbuf_cache = (EvPixbufCache *) g_object_new (EV_TYPE_PIXBUF_CACHE, NULL);
244 /* This is a backlink, so we don't ref this */
245 pixbuf_cache->view = view;
246 pixbuf_cache->document = document;
252 job_page_ready_cb (EvJob *job,
253 EvPixbufCache *pixbuf_cache)
255 CacheJobInfo *job_info;
256 EvJobRender *job_render = EV_JOB_RENDER (job);
258 /* If the job is outside of our interest, we silently discard it */
259 if ((job_render->page < (pixbuf_cache->start_page - pixbuf_cache->preload_cache_size)) ||
260 (job_render->page > (pixbuf_cache->end_page + pixbuf_cache->preload_cache_size))) {
261 g_object_unref (job);
265 job_info = find_job_cache (pixbuf_cache, job_render->page);
267 copy_job_page_and_selection_to_job_info (job_render, job_info, pixbuf_cache);
268 g_signal_emit (pixbuf_cache, signals[JOB_FINISHED], 0, job_info->region);
272 job_finished_cb (EvJob *job,
273 EvPixbufCache *pixbuf_cache)
275 CacheJobInfo *job_info;
276 EvJobRender *job_render = EV_JOB_RENDER (job);
278 /* If the job is outside of our interest, we silently discard it */
279 if ((job_render->page < (pixbuf_cache->start_page - pixbuf_cache->preload_cache_size)) ||
280 (job_render->page > (pixbuf_cache->end_page + pixbuf_cache->preload_cache_size))) {
281 g_object_unref (job);
285 job_info = find_job_cache (pixbuf_cache, job_render->page);
286 copy_job_to_job_info (job_render, job_info, pixbuf_cache);
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_page_ready_cb),
316 g_signal_handlers_disconnect_by_func (job_info->job,
317 G_CALLBACK (job_finished_cb),
319 ev_job_cancel (job_info->job);
320 g_object_unref (job_info->job);
321 job_info->job = NULL;
324 /* Do all function that copies a job from an older cache to it's position in the
325 * new cache. It clears the old job if it doesn't have a place.
328 move_one_job (CacheJobInfo *job_info,
329 EvPixbufCache *pixbuf_cache,
331 CacheJobInfo *new_job_list,
332 CacheJobInfo *new_prev_job,
333 CacheJobInfo *new_next_job,
338 CacheJobInfo *target_page = NULL;
342 if (page < (start_page - pixbuf_cache->preload_cache_size) ||
343 page > (end_page + pixbuf_cache->preload_cache_size)) {
344 dispose_cache_job_info (job_info, pixbuf_cache);
348 /* find the target page to copy it over to. */
349 if (page < start_page) {
350 page_offset = (page - (start_page - pixbuf_cache->preload_cache_size));
352 g_assert (page_offset >= 0 &&
353 page_offset < pixbuf_cache->preload_cache_size);
354 target_page = new_prev_job + page_offset;
355 new_priority = EV_JOB_PRIORITY_LOW;
356 } else if (page > end_page) {
357 page_offset = (page - (end_page + 1));
359 g_assert (page_offset >= 0 &&
360 page_offset < pixbuf_cache->preload_cache_size);
361 target_page = new_next_job + page_offset;
362 new_priority = EV_JOB_PRIORITY_LOW;
364 page_offset = page - start_page;
365 g_assert (page_offset >= 0 &&
366 page_offset <= ((end_page - start_page) + 1));
367 new_priority = EV_JOB_PRIORITY_URGENT;
368 target_page = new_job_list + page_offset;
371 *target_page = *job_info;
372 job_info->job = NULL;
373 job_info->region = NULL;
374 job_info->surface = NULL;
375 job_info->link_mapping = NULL;
376 job_info->image_mapping = NULL;
377 job_info->form_field_mapping = NULL;
378 job_info->annots_mapping = NULL;
380 if (new_priority != priority && target_page->job) {
381 ev_job_scheduler_update_job (target_page->job, new_priority);
386 ev_pixbuf_cache_update_range (EvPixbufCache *pixbuf_cache,
390 CacheJobInfo *new_job_list;
391 CacheJobInfo *new_prev_job;
392 CacheJobInfo *new_next_job;
395 if (pixbuf_cache->start_page == start_page &&
396 pixbuf_cache->end_page == end_page)
399 new_job_list = g_new0 (CacheJobInfo, (end_page - start_page) + 1);
400 new_prev_job = g_new0 (CacheJobInfo, pixbuf_cache->preload_cache_size);
401 new_next_job = g_new0 (CacheJobInfo, pixbuf_cache->preload_cache_size);
403 /* We go through each job in the old cache and either clear it or move
404 * it to a new location. */
406 /* Start with the prev cache. */
407 page = pixbuf_cache->start_page - pixbuf_cache->preload_cache_size;
408 for (i = 0; i < pixbuf_cache->preload_cache_size; i++) {
410 dispose_cache_job_info (pixbuf_cache->prev_job + i, pixbuf_cache);
412 move_one_job (pixbuf_cache->prev_job + i,
414 new_job_list, new_prev_job, new_next_job,
415 start_page, end_page, EV_JOB_PRIORITY_LOW);
420 page = pixbuf_cache->start_page;
421 for (i = 0; i < PAGE_CACHE_LEN (pixbuf_cache); i++) {
422 move_one_job (pixbuf_cache->job_list + i,
424 new_job_list, new_prev_job, new_next_job,
425 start_page, end_page, EV_JOB_PRIORITY_URGENT);
429 for (i = 0; i < pixbuf_cache->preload_cache_size; i++) {
430 if (page >= ev_document_get_n_pages (pixbuf_cache->document)) {
431 dispose_cache_job_info (pixbuf_cache->next_job + i, pixbuf_cache);
433 move_one_job (pixbuf_cache->next_job + i,
435 new_job_list, new_prev_job, new_next_job,
436 start_page, end_page, EV_JOB_PRIORITY_LOW);
441 g_free (pixbuf_cache->job_list);
442 g_free (pixbuf_cache->prev_job);
443 g_free (pixbuf_cache->next_job);
445 pixbuf_cache->job_list = new_job_list;
446 pixbuf_cache->prev_job = new_prev_job;
447 pixbuf_cache->next_job = new_next_job;
449 pixbuf_cache->start_page = start_page;
450 pixbuf_cache->end_page = end_page;
454 copy_job_page_and_selection_to_job_info (EvJobRender *job_render,
455 CacheJobInfo *job_info,
456 EvPixbufCache *pixbuf_cache)
458 if (job_info->rc == NULL) {
459 job_info->rc = ev_render_context_new (job_render->ev_page,
460 job_render->rotation,
463 ev_render_context_set_page (job_info->rc, job_render->ev_page);
464 ev_render_context_set_rotation (job_info->rc, job_render->rotation);
465 ev_render_context_set_scale (job_info->rc, job_render->scale);
468 if (job_info->surface) {
469 cairo_surface_destroy (job_info->surface);
471 job_info->surface = cairo_surface_reference (job_render->surface);
472 if (pixbuf_cache->inverted_colors) {
473 ev_document_misc_invert_surface (job_info->surface);
476 job_info->points_set = FALSE;
477 if (job_render->flags & EV_RENDER_INCLUDE_SELECTION) {
478 if (job_info->selection) {
479 cairo_surface_destroy (job_info->selection);
480 job_info->selection = NULL;
482 if (job_info->selection_region) {
483 gdk_region_destroy (job_info->selection_region);
484 job_info->selection_region = NULL;
487 job_info->selection_points = job_render->selection_points;
488 job_info->selection_region = gdk_region_copy (job_render->selection_region);
489 job_info->selection = cairo_surface_reference (job_render->selection);
490 g_assert (job_info->selection_points.x1 >= 0);
491 job_info->points_set = TRUE;
495 g_signal_handlers_disconnect_by_func (job_info->job,
496 G_CALLBACK (job_page_ready_cb),
500 job_info->page_ready = TRUE;
504 copy_job_to_job_info (EvJobRender *job_render,
505 CacheJobInfo *job_info,
506 EvPixbufCache *pixbuf_cache)
508 if (!job_info->page_ready) {
509 g_signal_emit (pixbuf_cache, signals[JOB_FINISHED], 0, job_info->region);
510 copy_job_page_and_selection_to_job_info (job_render,
515 if (job_render->flags & EV_RENDER_INCLUDE_LINKS) {
516 if (job_info->link_mapping)
517 ev_mapping_list_free (job_info->link_mapping, g_object_unref);
518 job_info->link_mapping = job_render->link_mapping;
521 if (job_render->flags & EV_RENDER_INCLUDE_IMAGES) {
522 if (job_info->image_mapping)
523 ev_mapping_list_free (job_info->image_mapping, g_object_unref);
524 job_info->image_mapping = job_render->image_mapping;
527 if (job_render->flags & EV_RENDER_INCLUDE_FORMS) {
528 if (job_info->form_field_mapping)
529 ev_mapping_list_free (job_info->form_field_mapping, g_object_unref);
530 job_info->form_field_mapping = job_render->form_field_mapping;
533 if (job_render->flags & EV_RENDER_INCLUDE_ANNOTS) {
534 if (job_info->annots_mapping)
535 ev_mapping_list_free (job_info->annots_mapping, g_object_unref);
536 job_info->annots_mapping = job_render->annots_mapping;
539 if (job_render->flags & EV_RENDER_INCLUDE_TEXT) {
540 if (job_info->text_mapping)
541 gdk_region_destroy (job_info->text_mapping);
542 job_info->text_mapping = job_render->text_mapping;
546 g_signal_handlers_disconnect_by_func (job_info->job,
547 G_CALLBACK (job_finished_cb),
549 ev_job_cancel (job_info->job);
550 g_object_unref (job_info->job);
551 job_info->job = NULL;
555 static CacheJobInfo *
556 find_job_cache (EvPixbufCache *pixbuf_cache,
561 if (page < (pixbuf_cache->start_page - pixbuf_cache->preload_cache_size) ||
562 page > (pixbuf_cache->end_page + pixbuf_cache->preload_cache_size))
565 if (page < pixbuf_cache->start_page) {
566 page_offset = (page - (pixbuf_cache->start_page - pixbuf_cache->preload_cache_size));
568 g_assert (page_offset >= 0 &&
569 page_offset < pixbuf_cache->preload_cache_size);
570 return pixbuf_cache->prev_job + page_offset;
573 if (page > pixbuf_cache->end_page) {
574 page_offset = (page - (pixbuf_cache->end_page + 1));
576 g_assert (page_offset >= 0 &&
577 page_offset < pixbuf_cache->preload_cache_size);
578 return pixbuf_cache->next_job + page_offset;
581 page_offset = page - pixbuf_cache->start_page;
582 g_assert (page_offset >= 0 &&
583 page_offset <= PAGE_CACHE_LEN(pixbuf_cache));
584 return pixbuf_cache->job_list + page_offset;
588 ev_pixbuf_cache_clear_job_sizes (EvPixbufCache *pixbuf_cache,
593 for (i = 0; i < PAGE_CACHE_LEN (pixbuf_cache); i++) {
594 check_job_size_and_unref (pixbuf_cache, pixbuf_cache->job_list + i, scale);
597 for (i = 0; i < pixbuf_cache->preload_cache_size; i++) {
598 check_job_size_and_unref (pixbuf_cache, pixbuf_cache->prev_job + i, scale);
599 check_job_size_and_unref (pixbuf_cache, pixbuf_cache->next_job + i, scale);
604 get_selection_colors (GtkWidget *widget, GdkColor **text, GdkColor **base)
606 if (GTK_WIDGET_HAS_FOCUS (widget)) {
607 *text = &widget->style->text [GTK_STATE_SELECTED];
608 *base = &widget->style->base [GTK_STATE_SELECTED];
610 *text = &widget->style->text [GTK_STATE_ACTIVE];
611 *base = &widget->style->base [GTK_STATE_ACTIVE];
616 add_job (EvPixbufCache *pixbuf_cache,
617 CacheJobInfo *job_info,
624 EvJobPriority priority)
626 EvRenderFlags flags = 0;
628 job_info->page_ready = FALSE;
630 if (job_info->region)
631 gdk_region_destroy (job_info->region);
632 job_info->region = region ? gdk_region_copy (region) : NULL;
634 /* Figure out what else we need for this job */
635 if (job_info->link_mapping == NULL)
636 flags |= EV_RENDER_INCLUDE_LINKS;
637 if (job_info->image_mapping == NULL)
638 flags |= EV_RENDER_INCLUDE_IMAGES;
639 if (job_info->form_field_mapping == NULL)
640 flags |= EV_RENDER_INCLUDE_FORMS;
641 if (job_info->annots_mapping == NULL)
642 flags |= EV_RENDER_INCLUDE_ANNOTS;
643 if (job_info->text_mapping == NULL)
644 flags |= EV_RENDER_INCLUDE_TEXT;
646 job_info->job = ev_job_render_new (pixbuf_cache->document,
647 page, rotation, scale,
651 if (new_selection_surface_needed (pixbuf_cache, job_info, page, scale)) {
652 GdkColor *text, *base;
654 gtk_widget_ensure_style (pixbuf_cache->view);
655 get_selection_colors (pixbuf_cache->view, &text, &base);
656 ev_job_render_set_selection_info (EV_JOB_RENDER (job_info->job),
657 &(job_info->target_points),
658 job_info->selection_style,
662 g_signal_connect (job_info->job, "page-ready",
663 G_CALLBACK (job_page_ready_cb),
665 g_signal_connect (job_info->job, "finished",
666 G_CALLBACK (job_finished_cb),
668 ev_job_scheduler_push_job (job_info->job, priority);
672 add_job_if_needed (EvPixbufCache *pixbuf_cache,
673 CacheJobInfo *job_info,
677 EvJobPriority priority)
684 _get_page_size_for_scale_and_rotation (pixbuf_cache->document,
685 page, scale, rotation,
688 if (job_info->surface &&
689 cairo_image_surface_get_width (job_info->surface) == width &&
690 cairo_image_surface_get_height (job_info->surface) == height)
693 add_job (pixbuf_cache, job_info, NULL,
694 width, height, page, rotation, scale,
699 ev_pixbuf_cache_add_jobs_if_needed (EvPixbufCache *pixbuf_cache,
703 CacheJobInfo *job_info;
707 for (i = 0; i < PAGE_CACHE_LEN (pixbuf_cache); i++) {
708 job_info = (pixbuf_cache->job_list + i);
709 page = pixbuf_cache->start_page + i;
711 add_job_if_needed (pixbuf_cache, job_info,
712 page, rotation, scale,
713 EV_JOB_PRIORITY_URGENT);
716 for (i = FIRST_VISIBLE_PREV(pixbuf_cache); i < pixbuf_cache->preload_cache_size; i++) {
717 job_info = (pixbuf_cache->prev_job + i);
718 page = pixbuf_cache->start_page - pixbuf_cache->preload_cache_size + i;
720 add_job_if_needed (pixbuf_cache, job_info,
721 page, rotation, scale,
722 EV_JOB_PRIORITY_LOW);
725 for (i = 0; i < VISIBLE_NEXT_LEN(pixbuf_cache); i++) {
726 job_info = (pixbuf_cache->next_job + i);
727 page = pixbuf_cache->end_page + 1 + i;
729 add_job_if_needed (pixbuf_cache, job_info,
730 page, rotation, scale,
731 EV_JOB_PRIORITY_LOW);
737 ev_pixbuf_cache_set_page_range (EvPixbufCache *pixbuf_cache,
742 GList *selection_list)
744 g_return_if_fail (EV_IS_PIXBUF_CACHE (pixbuf_cache));
746 g_return_if_fail (start_page >= 0 && start_page < ev_document_get_n_pages (pixbuf_cache->document));
747 g_return_if_fail (end_page >= 0 && end_page < ev_document_get_n_pages (pixbuf_cache->document));
748 g_return_if_fail (end_page >= start_page);
750 /* First, resize the page_range as needed. We cull old pages
752 ev_pixbuf_cache_update_range (pixbuf_cache, start_page, end_page);
754 /* Then, we update the current jobs to see if any of them are the wrong
755 * size, we remove them if we need to. */
756 ev_pixbuf_cache_clear_job_sizes (pixbuf_cache, scale);
758 /* Next, we update the target selection for our pages */
759 ev_pixbuf_cache_set_selection_list (pixbuf_cache, selection_list);
761 /* Finally, we add the new jobs for all the sizes that don't have a
763 ev_pixbuf_cache_add_jobs_if_needed (pixbuf_cache, rotation, scale);
767 ev_pixbuf_cache_set_inverted_colors (EvPixbufCache *pixbuf_cache,
768 gboolean inverted_colors)
772 if (pixbuf_cache->inverted_colors == inverted_colors)
775 pixbuf_cache->inverted_colors = inverted_colors;
777 for (i = 0; i < pixbuf_cache->preload_cache_size; i++) {
778 CacheJobInfo *job_info;
780 job_info = pixbuf_cache->prev_job + i;
781 if (job_info->surface)
782 ev_document_misc_invert_surface (job_info->surface);
784 job_info = pixbuf_cache->next_job + i;
785 if (job_info->surface)
786 ev_document_misc_invert_surface (job_info->surface);
789 for (i = 0; i < PAGE_CACHE_LEN (pixbuf_cache); i++) {
790 CacheJobInfo *job_info;
792 job_info = pixbuf_cache->job_list + i;
793 if (job_info->surface)
794 ev_document_misc_invert_surface (job_info->surface);
799 ev_pixbuf_cache_get_surface (EvPixbufCache *pixbuf_cache,
802 CacheJobInfo *job_info;
804 job_info = find_job_cache (pixbuf_cache, page);
805 if (job_info == NULL)
808 if (job_info->page_ready)
809 return job_info->surface;
811 /* We don't need to wait for the idle to handle the callback */
813 EV_JOB_RENDER (job_info->job)->page_ready) {
814 copy_job_page_and_selection_to_job_info (EV_JOB_RENDER (job_info->job), job_info, pixbuf_cache);
815 g_signal_emit (pixbuf_cache, signals[JOB_FINISHED], 0, job_info->region);
818 return job_info->surface;
822 ev_pixbuf_cache_get_link_mapping (EvPixbufCache *pixbuf_cache,
825 CacheJobInfo *job_info;
827 job_info = find_job_cache (pixbuf_cache, page);
828 if (job_info == NULL)
831 /* We don't need to wait for the idle to handle the callback */
833 EV_JOB (job_info->job)->finished) {
834 copy_job_to_job_info (EV_JOB_RENDER (job_info->job), job_info, pixbuf_cache);
837 return job_info->link_mapping;
841 ev_pixbuf_cache_get_image_mapping (EvPixbufCache *pixbuf_cache,
844 CacheJobInfo *job_info;
846 if (!EV_IS_DOCUMENT_IMAGES (pixbuf_cache->document))
849 job_info = find_job_cache (pixbuf_cache, page);
850 if (job_info == NULL)
853 /* We don't need to wait for the idle to handle the callback */
855 EV_JOB (job_info->job)->finished) {
856 copy_job_to_job_info (EV_JOB_RENDER (job_info->job), job_info, pixbuf_cache);
859 return job_info->image_mapping;
863 ev_pixbuf_cache_get_form_field_mapping (EvPixbufCache *pixbuf_cache,
866 CacheJobInfo *job_info;
868 if (!EV_IS_DOCUMENT_FORMS (pixbuf_cache->document))
871 job_info = find_job_cache (pixbuf_cache, page);
872 if (job_info == NULL)
875 /* We don't need to wait for the idle to handle the callback */
877 EV_JOB (job_info->job)->finished) {
878 copy_job_to_job_info (EV_JOB_RENDER(job_info->job), job_info, pixbuf_cache);
881 return job_info->form_field_mapping;
885 ev_pixbuf_cache_get_annots_mapping (EvPixbufCache *pixbuf_cache,
888 CacheJobInfo *job_info;
890 if (!EV_IS_DOCUMENT_ANNOTATIONS (pixbuf_cache->document))
893 job_info = find_job_cache (pixbuf_cache, page);
894 if (job_info == NULL)
897 /* We don't need to wait for the idle to handle the callback */
899 EV_JOB (job_info->job)->finished) {
900 copy_job_to_job_info (EV_JOB_RENDER (job_info->job), job_info, pixbuf_cache);
903 return job_info->annots_mapping;
907 new_selection_surface_needed (EvPixbufCache *pixbuf_cache,
908 CacheJobInfo *job_info,
912 if (job_info->selection && job_info->rc) {
914 gint selection_width, selection_height;
916 _get_page_size_for_scale_and_rotation (pixbuf_cache->document,
917 page, scale, job_info->rc->rotation,
920 selection_width = cairo_image_surface_get_width (job_info->selection);
921 selection_height = cairo_image_surface_get_height (job_info->selection);
923 if (width != selection_width || height != selection_height)
926 if (job_info->points_set)
934 clear_selection_if_needed (EvPixbufCache *pixbuf_cache,
935 CacheJobInfo *job_info,
939 if (new_selection_surface_needed (pixbuf_cache, job_info, page, scale)) {
940 if (job_info->selection)
941 cairo_surface_destroy (job_info->selection);
942 job_info->selection = NULL;
943 job_info->selection_points.x1 = -1;
948 ev_pixbuf_cache_get_text_mapping (EvPixbufCache *pixbuf_cache,
951 CacheJobInfo *job_info;
953 job_info = find_job_cache (pixbuf_cache, page);
954 if (job_info == NULL)
957 /* We don't need to wait for the idle to handle the callback */
959 EV_JOB (job_info->job)->finished) {
960 copy_job_to_job_info (EV_JOB_RENDER (job_info->job), job_info, pixbuf_cache);
963 return job_info->text_mapping;
966 /* Clears the cache of jobs and pixbufs.
969 ev_pixbuf_cache_clear (EvPixbufCache *pixbuf_cache)
973 for (i = 0; i < pixbuf_cache->preload_cache_size; i++) {
974 dispose_cache_job_info (pixbuf_cache->prev_job + i, pixbuf_cache);
975 dispose_cache_job_info (pixbuf_cache->next_job + i, pixbuf_cache);
978 for (i = 0; i < PAGE_CACHE_LEN (pixbuf_cache); i++) {
979 dispose_cache_job_info (pixbuf_cache->job_list + i, pixbuf_cache);
985 ev_pixbuf_cache_style_changed (EvPixbufCache *pixbuf_cache)
989 /* FIXME: doesn't update running jobs. */
990 for (i = 0; i < pixbuf_cache->preload_cache_size; i++) {
991 CacheJobInfo *job_info;
993 job_info = pixbuf_cache->prev_job + i;
994 if (job_info->selection) {
995 cairo_surface_destroy (job_info->selection);
996 job_info->selection = NULL;
999 job_info = pixbuf_cache->next_job + i;
1000 if (job_info->selection) {
1001 cairo_surface_destroy (job_info->selection);
1002 job_info->selection = NULL;
1006 for (i = 0; i < PAGE_CACHE_LEN (pixbuf_cache); i++) {
1007 CacheJobInfo *job_info;
1009 job_info = pixbuf_cache->job_list + i;
1010 if (job_info->selection) {
1011 cairo_surface_destroy (job_info->selection);
1012 job_info->selection = NULL;
1018 ev_pixbuf_cache_get_selection_surface (EvPixbufCache *pixbuf_cache,
1023 CacheJobInfo *job_info;
1025 /* the document does not implement the selection interface */
1026 if (!EV_IS_SELECTION (pixbuf_cache->document))
1029 job_info = find_job_cache (pixbuf_cache, page);
1030 if (job_info == NULL)
1033 /* No selection on this page */
1034 if (!job_info->points_set)
1037 /* Create new render context if needed (selection + fast scrolling) */
1038 if (job_info->rc == NULL) {
1040 ev_page = ev_document_get_page (pixbuf_cache->document, page);
1041 job_info->rc = ev_render_context_new (ev_page, 0, scale);
1042 g_object_unref (ev_page);
1046 ev_render_context_set_scale (job_info->rc, scale);
1048 /* If we have a running job, we just return what we have under the
1049 * assumption that it'll be updated later and we can scale it as need
1051 if (job_info->job && (EV_JOB_RENDER (job_info->job)->flags & EV_RENDER_INCLUDE_SELECTION))
1052 return job_info->selection;
1054 /* Now, lets see if we need to resize the image. If we do, we clear the
1056 clear_selection_if_needed (pixbuf_cache, job_info, page, scale);
1058 /* Finally, we see if the two scales are the same, and get a new pixbuf
1059 * if needed. We do this synchronously for now. At some point, we
1060 * _should_ be able to get rid of the doc_mutex, so the synchronicity
1061 * doesn't kill us. Rendering a few glyphs should really be fast.
1063 if (ev_rect_cmp (&(job_info->target_points), &(job_info->selection_points))) {
1064 EvRectangle *old_points;
1065 GdkColor *text, *base;
1067 /* we need to get a new selection pixbuf */
1068 ev_document_doc_mutex_lock ();
1069 if (job_info->selection_points.x1 < 0) {
1070 g_assert (job_info->selection == NULL);
1073 g_assert (job_info->selection != NULL);
1074 old_points = &(job_info->selection_points);
1077 if (job_info->selection_region)
1078 gdk_region_destroy (job_info->selection_region);
1079 job_info->selection_region =
1080 ev_selection_get_selection_region (EV_SELECTION (pixbuf_cache->document),
1082 job_info->selection_style,
1083 &(job_info->target_points));
1085 gtk_widget_ensure_style (pixbuf_cache->view);
1087 get_selection_colors (pixbuf_cache->view, &text, &base);
1089 ev_selection_render_selection (EV_SELECTION (pixbuf_cache->document),
1090 job_info->rc, &(job_info->selection),
1091 &(job_info->target_points),
1093 job_info->selection_style,
1095 job_info->selection_points = job_info->target_points;
1096 ev_document_doc_mutex_unlock ();
1099 *region = job_info->selection_region;
1100 return job_info->selection;
1104 update_job_selection (CacheJobInfo *job_info,
1105 EvViewSelection *selection)
1107 job_info->points_set = TRUE;
1108 job_info->target_points = selection->rect;
1109 job_info->selection_style = selection->style;
1113 clear_job_selection (CacheJobInfo *job_info)
1115 job_info->points_set = FALSE;
1116 job_info->selection_points.x1 = -1;
1118 if (job_info->selection) {
1119 cairo_surface_destroy (job_info->selection);
1120 job_info->selection = NULL;
1124 /* This function will reset the selection on pages that no longer have them, and
1125 * will update the target_selection on those that need it. It will _not_ free
1126 * the previous selection_list -- that's up to caller to do.
1129 ev_pixbuf_cache_set_selection_list (EvPixbufCache *pixbuf_cache,
1130 GList *selection_list)
1132 EvViewSelection *selection;
1133 GList *list = selection_list;
1137 g_return_if_fail (EV_IS_PIXBUF_CACHE (pixbuf_cache));
1139 if (!EV_IS_SELECTION (pixbuf_cache->document))
1142 /* We check each area to see what needs updating, and what needs freeing; */
1143 page = pixbuf_cache->start_page - pixbuf_cache->preload_cache_size;
1144 for (i = 0; i < pixbuf_cache->preload_cache_size; i++) {
1152 if (((EvViewSelection *)list->data)->page == page) {
1153 selection = list->data;
1155 } else if (((EvViewSelection *)list->data)->page > page)
1161 update_job_selection (pixbuf_cache->prev_job + i, selection);
1163 clear_job_selection (pixbuf_cache->prev_job + i);
1167 page = pixbuf_cache->start_page;
1168 for (i = 0; i < PAGE_CACHE_LEN (pixbuf_cache); i++) {
1171 if (((EvViewSelection *)list->data)->page == page) {
1172 selection = list->data;
1174 } else if (((EvViewSelection *)list->data)->page > page)
1180 update_job_selection (pixbuf_cache->job_list + i, selection);
1182 clear_job_selection (pixbuf_cache->job_list + i);
1186 for (i = 0; i < pixbuf_cache->preload_cache_size; i++) {
1187 if (page >= ev_document_get_n_pages (pixbuf_cache->document))
1192 if (((EvViewSelection *)list->data)->page == page) {
1193 selection = list->data;
1195 } else if (((EvViewSelection *)list->data)->page > page)
1201 update_job_selection (pixbuf_cache->next_job + i, selection);
1203 clear_job_selection (pixbuf_cache->next_job + i);
1209 /* Returns what the pixbuf cache thinks is */
1212 ev_pixbuf_cache_get_selection_list (EvPixbufCache *pixbuf_cache)
1214 EvViewSelection *selection;
1215 GList *retval = NULL;
1219 g_return_val_if_fail (EV_IS_PIXBUF_CACHE (pixbuf_cache), NULL);
1221 /* We check each area to see what needs updating, and what needs freeing; */
1222 page = pixbuf_cache->start_page - pixbuf_cache->preload_cache_size;
1223 for (i = 0; i < pixbuf_cache->preload_cache_size; i++) {
1229 if (pixbuf_cache->prev_job[i].selection_points.x1 != -1) {
1230 selection = g_new0 (EvViewSelection, 1);
1231 selection->page = page;
1232 selection->rect = pixbuf_cache->prev_job[i].selection_points;
1233 if (pixbuf_cache->prev_job[i].selection_region)
1234 selection->covered_region = gdk_region_copy (pixbuf_cache->prev_job[i].selection_region);
1235 retval = g_list_append (retval, selection);
1241 page = pixbuf_cache->start_page;
1242 for (i = 0; i < PAGE_CACHE_LEN (pixbuf_cache); i++) {
1243 if (pixbuf_cache->job_list[i].selection_points.x1 != -1) {
1244 selection = g_new0 (EvViewSelection, 1);
1245 selection->page = page;
1246 selection->rect = pixbuf_cache->job_list[i].selection_points;
1247 if (pixbuf_cache->job_list[i].selection_region)
1248 selection->covered_region = gdk_region_copy (pixbuf_cache->job_list[i].selection_region);
1249 retval = g_list_append (retval, selection);
1255 for (i = 0; i < pixbuf_cache->preload_cache_size; i++) {
1256 if (page >= ev_document_get_n_pages (pixbuf_cache->document))
1259 if (pixbuf_cache->next_job[i].selection_points.x1 != -1) {
1260 selection = g_new0 (EvViewSelection, 1);
1261 selection->page = page;
1262 selection->rect = pixbuf_cache->next_job[i].selection_points;
1263 if (pixbuf_cache->next_job[i].selection_region)
1264 selection->covered_region = gdk_region_copy (pixbuf_cache->next_job[i].selection_region);
1265 retval = g_list_append (retval, selection);
1275 ev_pixbuf_cache_reload_page (EvPixbufCache *pixbuf_cache,
1281 CacheJobInfo *job_info;
1284 job_info = find_job_cache (pixbuf_cache, page);
1285 if (job_info == NULL)
1288 _get_page_size_for_scale_and_rotation (pixbuf_cache->document,
1289 page, scale, rotation,
1291 add_job (pixbuf_cache, job_info, region,
1292 width, height, page, rotation, scale,
1293 EV_JOB_PRIORITY_URGENT);