1 #include "ev-pixbuf-cache.h"
2 #include "ev-job-queue.h"
3 #include "ev-page-cache.h"
4 #include "ev-document-images.h"
5 #include "ev-document-forms.h"
7 #include "ev-form-field.h"
9 typedef struct _CacheJobInfo
15 /* Region of the page that needs to be drawn */
18 /* Data we get from rendering */
19 cairo_surface_t *surface;
22 GList *form_field_mapping;
23 GdkRegion *text_mapping;
26 * Selection_points are the coordinates encapsulated in selection.
27 * target_points is the target selection size. */
28 EvRectangle selection_points;
29 EvRectangle target_points;
30 EvSelectionStyle selection_style;
33 cairo_surface_t *selection;
34 GdkRegion *selection_region;
41 /* We keep a link to our containing view just for style information. */
47 /* preload_cache_size is the number of pages prior to the current
48 * visible area that we cache. It's normally 1, but could be 2 in the
51 int preload_cache_size;
52 CacheJobInfo *prev_job;
53 CacheJobInfo *job_list;
54 CacheJobInfo *next_job;
57 struct _EvPixbufCacheClass
59 GObjectClass parent_class;
61 void (* job_finished) (EvPixbufCache *pixbuf_cache);
71 static guint signals[N_SIGNALS] = {0, };
73 static void ev_pixbuf_cache_init (EvPixbufCache *pixbuf_cache);
74 static void ev_pixbuf_cache_class_init (EvPixbufCacheClass *pixbuf_cache);
75 static void ev_pixbuf_cache_finalize (GObject *object);
76 static void ev_pixbuf_cache_dispose (GObject *object);
77 static void job_page_ready_cb (EvJob *job,
78 EvPixbufCache *pixbuf_cache);
79 static void job_finished_cb (EvJob *job,
80 EvPixbufCache *pixbuf_cache);
81 static CacheJobInfo *find_job_cache (EvPixbufCache *pixbuf_cache,
83 static void copy_job_to_job_info (EvJobRender *job_render,
84 CacheJobInfo *job_info,
85 EvPixbufCache *pixbuf_cache);
86 static void copy_job_page_and_selection_to_job_info (EvJobRender *job_render,
87 CacheJobInfo *job_info,
88 EvPixbufCache *pixbuf_cache);
89 static gboolean new_selection_surface_needed(EvPixbufCache *pixbuf_cache,
90 CacheJobInfo *job_info,
95 /* These are used for iterating through the prev and next arrays */
96 #define FIRST_VISABLE_PREV(pixbuf_cache) \
97 (MAX (0, pixbuf_cache->preload_cache_size + 1 - pixbuf_cache->start_page))
98 #define VISIBLE_NEXT_LEN(pixbuf_cache, page_cache) \
99 (MIN(pixbuf_cache->preload_cache_size, ev_page_cache_get_n_pages (page_cache) - (1 + pixbuf_cache->end_page)))
100 #define PAGE_CACHE_LEN(pixbuf_cache) \
101 ((pixbuf_cache->end_page - pixbuf_cache->start_page) + 1)
103 G_DEFINE_TYPE (EvPixbufCache, ev_pixbuf_cache, G_TYPE_OBJECT)
106 ev_pixbuf_cache_init (EvPixbufCache *pixbuf_cache)
108 pixbuf_cache->start_page = 0;
109 pixbuf_cache->end_page = 0;
110 pixbuf_cache->job_list = g_new0 (CacheJobInfo, PAGE_CACHE_LEN (pixbuf_cache));
112 pixbuf_cache->preload_cache_size = 2;
113 pixbuf_cache->prev_job = g_new0 (CacheJobInfo, pixbuf_cache->preload_cache_size);
114 pixbuf_cache->next_job = g_new0 (CacheJobInfo, pixbuf_cache->preload_cache_size);
118 ev_pixbuf_cache_class_init (EvPixbufCacheClass *class)
120 GObjectClass *object_class;
122 object_class = G_OBJECT_CLASS (class);
124 object_class->finalize = ev_pixbuf_cache_finalize;
125 object_class->dispose = ev_pixbuf_cache_dispose;
127 signals[JOB_FINISHED] =
128 g_signal_new ("job-finished",
129 G_OBJECT_CLASS_TYPE (object_class),
130 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
131 G_STRUCT_OFFSET (EvPixbufCacheClass, job_finished),
133 g_cclosure_marshal_VOID__POINTER,
139 ev_pixbuf_cache_finalize (GObject *object)
141 EvPixbufCache *pixbuf_cache;
143 pixbuf_cache = EV_PIXBUF_CACHE (object);
145 g_free (pixbuf_cache->prev_job);
146 g_free (pixbuf_cache->job_list);
147 g_free (pixbuf_cache->next_job);
149 G_OBJECT_CLASS (ev_pixbuf_cache_parent_class)->finalize (object);
153 dispose_cache_job_info (CacheJobInfo *job_info,
156 if (job_info == NULL)
159 g_signal_handlers_disconnect_by_func (job_info->job,
160 G_CALLBACK (job_page_ready_cb),
162 g_signal_handlers_disconnect_by_func (job_info->job,
163 G_CALLBACK (job_finished_cb),
165 ev_job_queue_remove_job (job_info->job);
166 g_object_unref (G_OBJECT (job_info->job));
167 job_info->job = NULL;
169 if (job_info->surface) {
170 cairo_surface_destroy (job_info->surface);
171 job_info->surface = NULL;
173 if (job_info->region) {
174 gdk_region_destroy (job_info->region);
175 job_info->region = NULL;
177 if (job_info->link_mapping) {
178 ev_link_mapping_free (job_info->link_mapping);
179 job_info->link_mapping = NULL;
181 if (job_info->image_mapping) {
182 ev_image_mapping_free (job_info->image_mapping);
183 job_info->image_mapping = NULL;
185 if (job_info->form_field_mapping) {
186 ev_form_field_mapping_free (job_info->form_field_mapping);
187 job_info->form_field_mapping = NULL;
189 if (job_info->text_mapping) {
190 gdk_region_destroy (job_info->text_mapping);
191 job_info->text_mapping = NULL;
193 if (job_info->selection) {
194 cairo_surface_destroy (job_info->selection);
195 job_info->selection = NULL;
197 if (job_info->selection_region) {
198 gdk_region_destroy (job_info->selection_region);
199 job_info->selection_region = NULL;
202 g_object_unref (G_OBJECT (job_info->rc));
206 job_info->points_set = FALSE;
210 ev_pixbuf_cache_dispose (GObject *object)
212 EvPixbufCache *pixbuf_cache;
215 pixbuf_cache = EV_PIXBUF_CACHE (object);
217 for (i = 0; i < pixbuf_cache->preload_cache_size; i++) {
218 dispose_cache_job_info (pixbuf_cache->prev_job + i, pixbuf_cache);
219 dispose_cache_job_info (pixbuf_cache->next_job + i, pixbuf_cache);
222 for (i = 0; i < PAGE_CACHE_LEN (pixbuf_cache); i++) {
223 dispose_cache_job_info (pixbuf_cache->job_list + i, pixbuf_cache);
226 G_OBJECT_CLASS (ev_pixbuf_cache_parent_class)->dispose (object);
231 ev_pixbuf_cache_new (GtkWidget *view,
232 EvDocument *document)
234 EvPixbufCache *pixbuf_cache;
236 pixbuf_cache = (EvPixbufCache *) g_object_new (EV_TYPE_PIXBUF_CACHE, NULL);
237 /* This is a backlink, so we don't ref this */
238 pixbuf_cache->view = view;
239 pixbuf_cache->document = document;
245 job_page_ready_cb (EvJob *job,
246 EvPixbufCache *pixbuf_cache)
248 CacheJobInfo *job_info;
249 EvJobRender *job_render = EV_JOB_RENDER (job);
251 /* If the job is outside of our interest, we silently discard it */
252 if ((job_render->rc->page < (pixbuf_cache->start_page - pixbuf_cache->preload_cache_size)) ||
253 (job_render->rc->page > (pixbuf_cache->end_page + pixbuf_cache->preload_cache_size))) {
254 g_object_unref (job);
258 job_info = find_job_cache (pixbuf_cache, job_render->rc->page);
260 copy_job_page_and_selection_to_job_info (job_render, job_info, pixbuf_cache);
261 g_signal_emit (pixbuf_cache, signals[JOB_FINISHED], 0, job_info->region);
265 job_finished_cb (EvJob *job,
266 EvPixbufCache *pixbuf_cache)
268 CacheJobInfo *job_info;
269 EvJobRender *job_render = EV_JOB_RENDER (job);
271 /* If the job is outside of our interest, we silently discard it */
272 if ((job_render->rc->page < (pixbuf_cache->start_page - pixbuf_cache->preload_cache_size)) ||
273 (job_render->rc->page > (pixbuf_cache->end_page + pixbuf_cache->preload_cache_size))) {
274 g_object_unref (job);
278 job_info = find_job_cache (pixbuf_cache, job_render->rc->page);
279 copy_job_to_job_info (job_render, job_info, pixbuf_cache);
282 /* This checks a job to see if the job would generate the right sized pixbuf
283 * given a scale. If it won't, it removes the job and clears it to NULL.
286 check_job_size_and_unref (EvPixbufCache *pixbuf_cache,
287 CacheJobInfo *job_info,
288 EvPageCache *page_cache,
296 if (job_info->job == NULL)
299 ev_page_cache_get_size (page_cache,
300 EV_JOB_RENDER (job_info->job)->rc->page,
301 EV_JOB_RENDER (job_info->job)->rc->rotation,
305 if (width == EV_JOB_RENDER (job_info->job)->target_width &&
306 height == EV_JOB_RENDER (job_info->job)->target_height)
309 g_signal_handlers_disconnect_by_func (job_info->job,
310 G_CALLBACK (job_finished_cb),
312 ev_job_queue_remove_job (job_info->job);
313 g_object_unref (job_info->job);
314 job_info->job = NULL;
317 /* Do all function that copies a job from an older cache to it's position in the
318 * new cache. It clears the old job if it doesn't have a place.
321 move_one_job (CacheJobInfo *job_info,
322 EvPixbufCache *pixbuf_cache,
324 CacheJobInfo *new_job_list,
325 CacheJobInfo *new_prev_job,
326 CacheJobInfo *new_next_job,
329 EvJobPriority priority)
331 CacheJobInfo *target_page = NULL;
333 EvJobPriority new_priority;
335 if (page < (start_page - pixbuf_cache->preload_cache_size) ||
336 page > (end_page + pixbuf_cache->preload_cache_size)) {
337 dispose_cache_job_info (job_info, pixbuf_cache);
341 /* find the target page to copy it over to. */
342 if (page < start_page) {
343 page_offset = (page - (start_page - pixbuf_cache->preload_cache_size));
345 g_assert (page_offset >= 0 &&
346 page_offset < pixbuf_cache->preload_cache_size);
347 target_page = new_prev_job + page_offset;
348 new_priority = EV_JOB_PRIORITY_LOW;
349 } else if (page > end_page) {
350 page_offset = (page - (end_page + 1));
352 g_assert (page_offset >= 0 &&
353 page_offset < pixbuf_cache->preload_cache_size);
354 target_page = new_next_job + page_offset;
355 new_priority = EV_JOB_PRIORITY_LOW;
357 page_offset = page - start_page;
358 g_assert (page_offset >= 0 &&
359 page_offset <= ((end_page - start_page) + 1));
360 new_priority = EV_JOB_PRIORITY_HIGH;
361 target_page = new_job_list + page_offset;
364 *target_page = *job_info;
365 job_info->job = NULL;
366 job_info->region = NULL;
367 job_info->surface = NULL;
368 job_info->link_mapping = NULL;
369 job_info->image_mapping = NULL;
370 job_info->form_field_mapping = NULL;
372 if (new_priority != priority && target_page->job) {
373 ev_job_queue_update_job (target_page->job, new_priority);
378 ev_pixbuf_cache_update_range (EvPixbufCache *pixbuf_cache,
382 CacheJobInfo *new_job_list;
383 CacheJobInfo *new_prev_job;
384 CacheJobInfo *new_next_job;
385 EvPageCache *page_cache;
388 if (pixbuf_cache->start_page == start_page &&
389 pixbuf_cache->end_page == end_page)
392 page_cache = ev_page_cache_get (pixbuf_cache->document);
394 new_job_list = g_new0 (CacheJobInfo, (end_page - start_page) + 1);
395 new_prev_job = g_new0 (CacheJobInfo, pixbuf_cache->preload_cache_size);
396 new_next_job = g_new0 (CacheJobInfo, pixbuf_cache->preload_cache_size);
398 /* We go through each job in the old cache and either clear it or move
399 * it to a new location. */
401 /* Start with the prev cache. */
402 page = pixbuf_cache->start_page - pixbuf_cache->preload_cache_size;
403 for (i = 0; i < pixbuf_cache->preload_cache_size; i++) {
405 dispose_cache_job_info (pixbuf_cache->prev_job + i, pixbuf_cache);
407 move_one_job (pixbuf_cache->prev_job + i,
409 new_job_list, new_prev_job, new_next_job,
410 start_page, end_page, EV_JOB_PRIORITY_LOW);
415 page = pixbuf_cache->start_page;
416 for (i = 0; i < PAGE_CACHE_LEN (pixbuf_cache); i++) {
417 move_one_job (pixbuf_cache->job_list + i,
419 new_job_list, new_prev_job, new_next_job,
420 start_page, end_page, EV_JOB_PRIORITY_HIGH);
424 for (i = 0; i < pixbuf_cache->preload_cache_size; i++) {
425 if (page >= ev_page_cache_get_n_pages (page_cache)) {
426 dispose_cache_job_info (pixbuf_cache->next_job + i, pixbuf_cache);
428 move_one_job (pixbuf_cache->next_job + i,
430 new_job_list, new_prev_job, new_next_job,
431 start_page, end_page, EV_JOB_PRIORITY_LOW);
436 g_free (pixbuf_cache->job_list);
437 g_free (pixbuf_cache->prev_job);
438 g_free (pixbuf_cache->next_job);
440 pixbuf_cache->job_list = new_job_list;
441 pixbuf_cache->prev_job = new_prev_job;
442 pixbuf_cache->next_job = new_next_job;
444 pixbuf_cache->start_page = start_page;
445 pixbuf_cache->end_page = end_page;
449 copy_job_page_and_selection_to_job_info (EvJobRender *job_render,
450 CacheJobInfo *job_info,
451 EvPixbufCache *pixbuf_cache)
453 if (job_info->surface) {
454 cairo_surface_destroy (job_info->surface);
456 job_info->surface = cairo_surface_reference (job_render->surface);
459 g_object_unref (G_OBJECT (job_info->rc));
461 job_info->rc = g_object_ref (job_render->rc);
463 job_info->points_set = FALSE;
464 if (job_render->include_selection) {
465 if (job_info->selection) {
466 cairo_surface_destroy (job_info->selection);
467 job_info->selection = NULL;
469 if (job_info->selection_region) {
470 gdk_region_destroy (job_info->selection_region);
471 job_info->selection_region = NULL;
474 job_info->selection_points = job_render->selection_points;
475 job_info->selection_region = gdk_region_copy (job_render->selection_region);
476 job_info->selection = cairo_surface_reference (job_render->selection);
477 g_assert (job_info->selection_points.x1 >= 0);
478 job_info->points_set = TRUE;
482 g_signal_handlers_disconnect_by_func (job_info->job,
483 G_CALLBACK (job_page_ready_cb),
487 job_info->page_ready = TRUE;
491 copy_job_to_job_info (EvJobRender *job_render,
492 CacheJobInfo *job_info,
493 EvPixbufCache *pixbuf_cache)
495 if (job_render->include_links) {
496 if (job_info->link_mapping)
497 ev_link_mapping_free (job_info->link_mapping);
498 job_info->link_mapping = job_render->link_mapping;
501 if (job_render->include_images) {
502 if (job_info->image_mapping)
503 ev_image_mapping_free (job_info->image_mapping);
504 job_info->image_mapping = job_render->image_mapping;
507 if (job_render->include_forms) {
508 if (job_info->form_field_mapping)
509 ev_form_field_mapping_free (job_info->form_field_mapping);
510 job_info->form_field_mapping = job_render->form_field_mapping;
513 if (job_render->include_text) {
514 if (job_info->text_mapping)
515 gdk_region_destroy (job_info->text_mapping);
516 job_info->text_mapping = job_render->text_mapping;
520 g_signal_handlers_disconnect_by_func (job_info->job,
521 G_CALLBACK (job_finished_cb),
523 ev_job_queue_remove_job (job_info->job);
524 g_object_unref (G_OBJECT (job_info->job));
525 job_info->job = NULL;
529 static CacheJobInfo *
530 find_job_cache (EvPixbufCache *pixbuf_cache,
535 if (page < (pixbuf_cache->start_page - pixbuf_cache->preload_cache_size) ||
536 page > (pixbuf_cache->end_page + pixbuf_cache->preload_cache_size))
539 if (page < pixbuf_cache->start_page) {
540 page_offset = (page - (pixbuf_cache->start_page - pixbuf_cache->preload_cache_size));
542 g_assert (page_offset >= 0 &&
543 page_offset < pixbuf_cache->preload_cache_size);
544 return pixbuf_cache->prev_job + page_offset;
547 if (page > pixbuf_cache->end_page) {
548 page_offset = (page - (pixbuf_cache->end_page + 1));
550 g_assert (page_offset >= 0 &&
551 page_offset < pixbuf_cache->preload_cache_size);
552 return pixbuf_cache->next_job + page_offset;
555 page_offset = page - pixbuf_cache->start_page;
556 g_assert (page_offset >= 0 &&
557 page_offset <= PAGE_CACHE_LEN(pixbuf_cache));
558 return pixbuf_cache->job_list + page_offset;
562 ev_pixbuf_cache_clear_job_sizes (EvPixbufCache *pixbuf_cache,
565 EvPageCache *page_cache;
568 page_cache = ev_page_cache_get (pixbuf_cache->document);
570 for (i = 0; i < PAGE_CACHE_LEN (pixbuf_cache); i++) {
571 check_job_size_and_unref (pixbuf_cache, pixbuf_cache->job_list + i, page_cache, scale);
574 for (i = 0; i < pixbuf_cache->preload_cache_size; i++) {
575 check_job_size_and_unref (pixbuf_cache, pixbuf_cache->prev_job + i, page_cache, scale);
576 check_job_size_and_unref (pixbuf_cache, pixbuf_cache->next_job + i, page_cache, scale);
580 #define FIRST_VISABLE_PREV(pixbuf_cache) \
581 (MAX (0, pixbuf_cache->preload_cache_size + 1 - pixbuf_cache->start_page))
584 get_selection_colors (GtkWidget *widget, GdkColor **text, GdkColor **base)
586 if (GTK_WIDGET_HAS_FOCUS (widget)) {
587 *text = &widget->style->text [GTK_STATE_SELECTED];
588 *base = &widget->style->base [GTK_STATE_SELECTED];
590 *text = &widget->style->text [GTK_STATE_ACTIVE];
591 *base = &widget->style->base [GTK_STATE_ACTIVE];
596 add_job (EvPixbufCache *pixbuf_cache,
597 CacheJobInfo *job_info,
598 EvPageCache *page_cache,
605 EvJobPriority priority)
607 gboolean include_links = FALSE;
608 gboolean include_text = FALSE;
609 gboolean include_selection = FALSE;
610 gboolean include_images = TRUE;
611 gboolean include_forms = FALSE;
612 GdkColor *text, *base;
614 job_info->page_ready = FALSE;
616 if (job_info->rc == NULL) {
617 job_info->rc = ev_render_context_new (rotation, page, scale);
619 ev_render_context_set_rotation (job_info->rc, rotation);
620 ev_render_context_set_page (job_info->rc, page);
621 ev_render_context_set_scale (job_info->rc, scale);
624 if (job_info->region)
625 gdk_region_destroy (job_info->region);
626 job_info->region = region ? gdk_region_copy (region) : NULL;
628 /* Figure out what else we need for this job */
629 if (job_info->link_mapping == NULL)
630 include_links = TRUE;
631 if (job_info->image_mapping == NULL)
632 include_images = TRUE;
633 if (job_info->form_field_mapping == NULL)
634 include_forms = TRUE;
635 if (job_info->text_mapping == NULL)
637 if (new_selection_surface_needed (pixbuf_cache, job_info, page, scale)) {
638 include_selection = TRUE;
641 gtk_widget_ensure_style (pixbuf_cache->view);
643 get_selection_colors (pixbuf_cache->view, &text, &base);
645 job_info->job = ev_job_render_new (pixbuf_cache->document,
648 &(job_info->target_points),
649 job_info->selection_style,
656 ev_job_queue_add_job (job_info->job, priority);
657 g_signal_connect (G_OBJECT (job_info->job), "page-ready",
658 G_CALLBACK (job_page_ready_cb),
660 g_signal_connect (G_OBJECT (job_info->job), "finished",
661 G_CALLBACK (job_finished_cb),
666 add_job_if_needed (EvPixbufCache *pixbuf_cache,
667 CacheJobInfo *job_info,
668 EvPageCache *page_cache,
672 EvJobPriority priority)
679 ev_page_cache_get_size (page_cache, page, rotation,
680 scale, &width, &height);
682 if (job_info->surface &&
683 cairo_image_surface_get_width (job_info->surface) == width &&
684 cairo_image_surface_get_height (job_info->surface) == height)
687 add_job (pixbuf_cache, job_info, page_cache, NULL,
688 width, height, page, rotation, scale,
693 ev_pixbuf_cache_add_jobs_if_needed (EvPixbufCache *pixbuf_cache,
697 EvPageCache *page_cache;
698 CacheJobInfo *job_info;
702 page_cache = ev_page_cache_get (pixbuf_cache->document);
704 for (i = 0; i < PAGE_CACHE_LEN (pixbuf_cache); i++) {
705 job_info = (pixbuf_cache->job_list + i);
706 page = pixbuf_cache->start_page + i;
708 add_job_if_needed (pixbuf_cache, job_info,
709 page_cache, page, rotation, scale,
710 EV_JOB_PRIORITY_HIGH);
713 for (i = FIRST_VISABLE_PREV(pixbuf_cache); i < pixbuf_cache->preload_cache_size; i++) {
714 job_info = (pixbuf_cache->prev_job + i);
715 page = pixbuf_cache->start_page - pixbuf_cache->preload_cache_size + i;
717 add_job_if_needed (pixbuf_cache, job_info,
718 page_cache, page, rotation, scale,
719 EV_JOB_PRIORITY_LOW);
722 for (i = 0; i < VISIBLE_NEXT_LEN(pixbuf_cache, page_cache); i++) {
723 job_info = (pixbuf_cache->next_job + i);
724 page = pixbuf_cache->end_page + 1 + i;
726 add_job_if_needed (pixbuf_cache, job_info,
727 page_cache, page, rotation, scale,
728 EV_JOB_PRIORITY_LOW);
734 ev_pixbuf_cache_set_page_range (EvPixbufCache *pixbuf_cache,
739 GList *selection_list)
741 EvPageCache *page_cache;
743 g_return_if_fail (EV_IS_PIXBUF_CACHE (pixbuf_cache));
745 page_cache = ev_page_cache_get (pixbuf_cache->document);
747 g_return_if_fail (start_page >= 0 && start_page < ev_page_cache_get_n_pages (page_cache));
748 g_return_if_fail (end_page >= 0 && end_page < ev_page_cache_get_n_pages (page_cache));
749 g_return_if_fail (end_page >= start_page);
751 /* First, resize the page_range as needed. We cull old pages
753 ev_pixbuf_cache_update_range (pixbuf_cache, start_page, end_page);
755 /* Then, we update the current jobs to see if any of them are the wrong
756 * size, we remove them if we need to. */
757 ev_pixbuf_cache_clear_job_sizes (pixbuf_cache, scale);
759 /* Next, we update the target selection for our pages */
760 ev_pixbuf_cache_set_selection_list (pixbuf_cache, selection_list);
762 /* Finally, we add the new jobs for all the sizes that don't have a
764 ev_pixbuf_cache_add_jobs_if_needed (pixbuf_cache, rotation, scale);
768 ev_pixbuf_cache_get_surface (EvPixbufCache *pixbuf_cache,
771 CacheJobInfo *job_info;
773 job_info = find_job_cache (pixbuf_cache, page);
774 if (job_info == NULL)
777 if (job_info->page_ready)
778 return job_info->surface;
780 /* We don't need to wait for the idle to handle the callback */
782 EV_JOB_RENDER (job_info->job)->page_ready) {
783 copy_job_page_and_selection_to_job_info (EV_JOB_RENDER (job_info->job), job_info, pixbuf_cache);
784 g_signal_emit (pixbuf_cache, signals[JOB_FINISHED], 0, job_info->region);
787 return job_info->surface;
791 ev_pixbuf_cache_get_link_mapping (EvPixbufCache *pixbuf_cache,
794 CacheJobInfo *job_info;
796 job_info = find_job_cache (pixbuf_cache, page);
797 if (job_info == NULL)
800 /* We don't need to wait for the idle to handle the callback */
802 EV_JOB (job_info->job)->finished) {
803 copy_job_to_job_info (EV_JOB_RENDER (job_info->job), job_info, pixbuf_cache);
806 return job_info->link_mapping;
810 ev_pixbuf_cache_get_image_mapping (EvPixbufCache *pixbuf_cache,
813 CacheJobInfo *job_info;
815 if (!EV_IS_DOCUMENT_IMAGES (pixbuf_cache->document))
818 job_info = find_job_cache (pixbuf_cache, page);
819 if (job_info == NULL)
822 /* We don't need to wait for the idle to handle the callback */
824 EV_JOB (job_info->job)->finished) {
825 copy_job_to_job_info (EV_JOB_RENDER (job_info->job), job_info, pixbuf_cache);
828 return job_info->image_mapping;
832 ev_pixbuf_cache_get_form_field_mapping (EvPixbufCache *pixbuf_cache,
835 CacheJobInfo *job_info;
837 if (!EV_IS_DOCUMENT_FORMS (pixbuf_cache->document))
840 job_info = find_job_cache (pixbuf_cache, page);
841 if (job_info == NULL)
844 /* We don't need to wait for the idle to handle the callback */
846 EV_JOB (job_info->job)->finished) {
847 copy_job_to_job_info (EV_JOB_RENDER(job_info->job), job_info, pixbuf_cache);
850 return job_info->form_field_mapping;
854 new_selection_surface_needed (EvPixbufCache *pixbuf_cache,
855 CacheJobInfo *job_info,
859 EvPageCache *page_cache;
861 if (job_info->selection) {
863 gint selection_width, selection_height;
865 page_cache = ev_page_cache_get (pixbuf_cache->document);
866 ev_page_cache_get_size (page_cache, page,
867 job_info->rc->rotation,
868 scale, &width, &height);
870 selection_width = cairo_image_surface_get_width (job_info->selection);
871 selection_height = cairo_image_surface_get_height (job_info->selection);
873 if (width != selection_width || height != selection_height)
876 if (job_info->points_set)
884 clear_selection_if_needed (EvPixbufCache *pixbuf_cache,
885 CacheJobInfo *job_info,
889 if (new_selection_surface_needed (pixbuf_cache, job_info, page, scale)) {
890 if (job_info->selection)
891 cairo_surface_destroy (job_info->selection);
892 job_info->selection = NULL;
893 job_info->selection_points.x1 = -1;
898 ev_pixbuf_cache_get_text_mapping (EvPixbufCache *pixbuf_cache,
901 CacheJobInfo *job_info;
903 job_info = find_job_cache (pixbuf_cache, page);
904 if (job_info == NULL)
907 /* We don't need to wait for the idle to handle the callback */
909 EV_JOB (job_info->job)->finished) {
910 copy_job_to_job_info (EV_JOB_RENDER (job_info->job), job_info, pixbuf_cache);
913 return job_info->text_mapping;
916 /* Clears the cache of jobs and pixbufs.
919 ev_pixbuf_cache_clear (EvPixbufCache *pixbuf_cache)
923 for (i = 0; i < pixbuf_cache->preload_cache_size; i++) {
924 dispose_cache_job_info (pixbuf_cache->prev_job + i, pixbuf_cache);
925 dispose_cache_job_info (pixbuf_cache->next_job + i, pixbuf_cache);
928 for (i = 0; i < PAGE_CACHE_LEN (pixbuf_cache); i++) {
929 dispose_cache_job_info (pixbuf_cache->job_list + i, pixbuf_cache);
935 ev_pixbuf_cache_style_changed (EvPixbufCache *pixbuf_cache)
939 /* FIXME: doesn't update running jobs. */
940 for (i = 0; i < pixbuf_cache->preload_cache_size; i++) {
941 CacheJobInfo *job_info;
943 job_info = pixbuf_cache->prev_job + i;
944 if (job_info->selection) {
945 cairo_surface_destroy (job_info->selection);
946 job_info->selection = NULL;
949 job_info = pixbuf_cache->next_job + i;
950 if (job_info->selection) {
951 cairo_surface_destroy (job_info->selection);
952 job_info->selection = NULL;
956 for (i = 0; i < PAGE_CACHE_LEN (pixbuf_cache); i++) {
957 CacheJobInfo *job_info;
959 job_info = pixbuf_cache->job_list + i;
960 if (job_info->selection) {
961 cairo_surface_destroy (job_info->selection);
962 job_info->selection = NULL;
968 ev_pixbuf_cache_get_selection_surface (EvPixbufCache *pixbuf_cache,
973 CacheJobInfo *job_info;
975 /* the document does not implement the selection interface */
976 if (!EV_IS_SELECTION (pixbuf_cache->document))
979 job_info = find_job_cache (pixbuf_cache, page);
980 if (job_info == NULL)
983 /* No selection on this page */
984 if (!job_info->points_set)
988 g_assert (job_info->rc);
989 ev_render_context_set_scale (job_info->rc, scale);
991 /* If we have a running job, we just return what we have under the
992 * assumption that it'll be updated later and we can scale it as need
994 if (job_info->job && EV_JOB_RENDER (job_info->job)->include_selection)
995 return job_info->selection;
997 /* Now, lets see if we need to resize the image. If we do, we clear the
999 clear_selection_if_needed (pixbuf_cache, job_info, page, scale);
1001 /* Finally, we see if the two scales are the same, and get a new pixbuf
1002 * if needed. We do this synchronously for now. At some point, we
1003 * _should_ be able to get rid of the doc_mutex, so the synchronicity
1004 * doesn't kill us. Rendering a few glyphs should really be fast.
1006 if (ev_rect_cmp (&(job_info->target_points), &(job_info->selection_points))) {
1007 EvRectangle *old_points;
1008 GdkColor *text, *base;
1010 /* we need to get a new selection pixbuf */
1011 ev_document_doc_mutex_lock ();
1012 if (job_info->selection_points.x1 < 0) {
1013 g_assert (job_info->selection == NULL);
1016 g_assert (job_info->selection != NULL);
1017 old_points = &(job_info->selection_points);
1020 if (job_info->selection_region)
1021 gdk_region_destroy (job_info->selection_region);
1022 job_info->selection_region =
1023 ev_selection_get_selection_region (EV_SELECTION (pixbuf_cache->document),
1025 job_info->selection_style,
1026 &(job_info->target_points));
1028 gtk_widget_ensure_style (pixbuf_cache->view);
1030 get_selection_colors (pixbuf_cache->view, &text, &base);
1032 ev_selection_render_selection (EV_SELECTION (pixbuf_cache->document),
1033 job_info->rc, &(job_info->selection),
1034 &(job_info->target_points),
1036 job_info->selection_style,
1038 job_info->selection_points = job_info->target_points;
1039 ev_document_doc_mutex_unlock ();
1042 *region = job_info->selection_region;
1043 return job_info->selection;
1047 update_job_selection (CacheJobInfo *job_info,
1048 EvViewSelection *selection)
1050 job_info->points_set = TRUE;
1051 job_info->target_points = selection->rect;
1052 job_info->selection_style = selection->style;
1056 clear_job_selection (CacheJobInfo *job_info)
1058 job_info->points_set = FALSE;
1059 job_info->selection_points.x1 = -1;
1061 if (job_info->selection) {
1062 cairo_surface_destroy (job_info->selection);
1063 job_info->selection = NULL;
1067 /* This function will reset the selection on pages that no longer have them, and
1068 * will update the target_selection on those that need it. It will _not_ free
1069 * the previous selection_list -- that's up to caller to do.
1072 ev_pixbuf_cache_set_selection_list (EvPixbufCache *pixbuf_cache,
1073 GList *selection_list)
1075 EvPageCache *page_cache;
1076 EvViewSelection *selection;
1077 GList *list = selection_list;
1081 g_return_if_fail (EV_IS_PIXBUF_CACHE (pixbuf_cache));
1083 if (!EV_IS_SELECTION (pixbuf_cache->document))
1086 page_cache = ev_page_cache_get (pixbuf_cache->document);
1088 /* We check each area to see what needs updating, and what needs freeing; */
1089 page = pixbuf_cache->start_page - pixbuf_cache->preload_cache_size;
1090 for (i = 0; i < pixbuf_cache->preload_cache_size; i++) {
1098 if (((EvViewSelection *)list->data)->page == page) {
1099 selection = list->data;
1101 } else if (((EvViewSelection *)list->data)->page > page)
1107 update_job_selection (pixbuf_cache->prev_job + i, selection);
1109 clear_job_selection (pixbuf_cache->prev_job + i);
1113 page = pixbuf_cache->start_page;
1114 for (i = 0; i < PAGE_CACHE_LEN (pixbuf_cache); i++) {
1117 if (((EvViewSelection *)list->data)->page == page) {
1118 selection = list->data;
1120 } else if (((EvViewSelection *)list->data)->page > page)
1126 update_job_selection (pixbuf_cache->job_list + i, selection);
1128 clear_job_selection (pixbuf_cache->job_list + i);
1132 for (i = 0; i < pixbuf_cache->preload_cache_size; i++) {
1133 if (page >= ev_page_cache_get_n_pages (page_cache))
1138 if (((EvViewSelection *)list->data)->page == page) {
1139 selection = list->data;
1141 } else if (((EvViewSelection *)list->data)->page > page)
1147 update_job_selection (pixbuf_cache->next_job + i, selection);
1149 clear_job_selection (pixbuf_cache->next_job + i);
1155 /* Returns what the pixbuf cache thinks is */
1158 ev_pixbuf_cache_get_selection_list (EvPixbufCache *pixbuf_cache)
1160 EvPageCache *page_cache;
1161 EvViewSelection *selection;
1162 GList *retval = NULL;
1166 g_return_val_if_fail (EV_IS_PIXBUF_CACHE (pixbuf_cache), NULL);
1168 page_cache = ev_page_cache_get (pixbuf_cache->document);
1170 /* We check each area to see what needs updating, and what needs freeing; */
1171 page = pixbuf_cache->start_page - pixbuf_cache->preload_cache_size;
1172 for (i = 0; i < pixbuf_cache->preload_cache_size; i++) {
1178 if (pixbuf_cache->prev_job[i].selection_points.x1 != -1) {
1179 selection = g_new0 (EvViewSelection, 1);
1180 selection->page = page;
1181 selection->rect = pixbuf_cache->prev_job[i].selection_points;
1182 if (pixbuf_cache->prev_job[i].selection_region)
1183 selection->covered_region = gdk_region_copy (pixbuf_cache->prev_job[i].selection_region);
1184 retval = g_list_append (retval, selection);
1190 page = pixbuf_cache->start_page;
1191 for (i = 0; i < PAGE_CACHE_LEN (pixbuf_cache); i++) {
1192 if (pixbuf_cache->job_list[i].selection_points.x1 != -1) {
1193 selection = g_new0 (EvViewSelection, 1);
1194 selection->page = page;
1195 selection->rect = pixbuf_cache->job_list[i].selection_points;
1196 if (pixbuf_cache->job_list[i].selection_region)
1197 selection->covered_region = gdk_region_copy (pixbuf_cache->job_list[i].selection_region);
1198 retval = g_list_append (retval, selection);
1204 for (i = 0; i < pixbuf_cache->preload_cache_size; i++) {
1205 if (page >= ev_page_cache_get_n_pages (page_cache))
1208 if (pixbuf_cache->next_job[i].selection_points.x1 != -1) {
1209 selection = g_new0 (EvViewSelection, 1);
1210 selection->page = page;
1211 selection->rect = pixbuf_cache->next_job[i].selection_points;
1212 if (pixbuf_cache->next_job[i].selection_region)
1213 selection->covered_region = gdk_region_copy (pixbuf_cache->next_job[i].selection_region);
1214 retval = g_list_append (retval, selection);
1224 ev_pixbuf_cache_reload_page (EvPixbufCache *pixbuf_cache,
1230 CacheJobInfo *job_info;
1231 EvPageCache *page_cache;
1234 job_info = find_job_cache (pixbuf_cache, page);
1235 if (job_info == NULL)
1238 page_cache = ev_page_cache_get (pixbuf_cache->document);
1239 ev_page_cache_get_size (page_cache, page, rotation, scale,
1242 add_job (pixbuf_cache, job_info, page_cache, region,
1243 width, height, page, rotation, scale,
1244 EV_JOB_PRIORITY_HIGH);