2 #include "ev-pixbuf-cache.h"
3 #include "ev-job-scheduler.h"
4 #include "ev-page-cache.h"
5 #include "ev-mapping.h"
6 #include "ev-document-forms.h"
7 #include "ev-document-images.h"
8 #include "ev-document-annotations.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. */
49 /* preload_cache_size is the number of pages prior to the current
50 * visible area that we cache. It's normally 1, but could be 2 in the
53 int preload_cache_size;
54 CacheJobInfo *prev_job;
55 CacheJobInfo *job_list;
56 CacheJobInfo *next_job;
59 struct _EvPixbufCacheClass
61 GObjectClass parent_class;
63 void (* job_finished) (EvPixbufCache *pixbuf_cache);
73 static guint signals[N_SIGNALS] = {0, };
75 static void ev_pixbuf_cache_init (EvPixbufCache *pixbuf_cache);
76 static void ev_pixbuf_cache_class_init (EvPixbufCacheClass *pixbuf_cache);
77 static void ev_pixbuf_cache_finalize (GObject *object);
78 static void ev_pixbuf_cache_dispose (GObject *object);
79 static void job_page_ready_cb (EvJob *job,
80 EvPixbufCache *pixbuf_cache);
81 static void job_finished_cb (EvJob *job,
82 EvPixbufCache *pixbuf_cache);
83 static CacheJobInfo *find_job_cache (EvPixbufCache *pixbuf_cache,
85 static void copy_job_to_job_info (EvJobRender *job_render,
86 CacheJobInfo *job_info,
87 EvPixbufCache *pixbuf_cache);
88 static void copy_job_page_and_selection_to_job_info (EvJobRender *job_render,
89 CacheJobInfo *job_info,
90 EvPixbufCache *pixbuf_cache);
91 static gboolean new_selection_surface_needed(EvPixbufCache *pixbuf_cache,
92 CacheJobInfo *job_info,
97 /* These are used for iterating through the prev and next arrays */
98 #define FIRST_VISIBLE_PREV(pixbuf_cache) \
99 (MAX (0, pixbuf_cache->preload_cache_size - pixbuf_cache->start_page))
100 #define VISIBLE_NEXT_LEN(pixbuf_cache, page_cache) \
101 (MIN(pixbuf_cache->preload_cache_size, ev_page_cache_get_n_pages (page_cache) - (1 + pixbuf_cache->end_page)))
102 #define PAGE_CACHE_LEN(pixbuf_cache) \
103 ((pixbuf_cache->end_page - pixbuf_cache->start_page) + 1)
105 G_DEFINE_TYPE (EvPixbufCache, ev_pixbuf_cache, G_TYPE_OBJECT)
108 ev_pixbuf_cache_init (EvPixbufCache *pixbuf_cache)
110 pixbuf_cache->start_page = 0;
111 pixbuf_cache->end_page = 0;
112 pixbuf_cache->job_list = g_new0 (CacheJobInfo, PAGE_CACHE_LEN (pixbuf_cache));
114 pixbuf_cache->preload_cache_size = 2;
115 pixbuf_cache->prev_job = g_new0 (CacheJobInfo, pixbuf_cache->preload_cache_size);
116 pixbuf_cache->next_job = g_new0 (CacheJobInfo, pixbuf_cache->preload_cache_size);
120 ev_pixbuf_cache_class_init (EvPixbufCacheClass *class)
122 GObjectClass *object_class;
124 object_class = G_OBJECT_CLASS (class);
126 object_class->finalize = ev_pixbuf_cache_finalize;
127 object_class->dispose = ev_pixbuf_cache_dispose;
129 signals[JOB_FINISHED] =
130 g_signal_new ("job-finished",
131 G_OBJECT_CLASS_TYPE (object_class),
132 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
133 G_STRUCT_OFFSET (EvPixbufCacheClass, job_finished),
135 g_cclosure_marshal_VOID__POINTER,
141 ev_pixbuf_cache_finalize (GObject *object)
143 EvPixbufCache *pixbuf_cache;
145 pixbuf_cache = EV_PIXBUF_CACHE (object);
147 g_free (pixbuf_cache->prev_job);
148 g_free (pixbuf_cache->job_list);
149 g_free (pixbuf_cache->next_job);
151 G_OBJECT_CLASS (ev_pixbuf_cache_parent_class)->finalize (object);
155 dispose_cache_job_info (CacheJobInfo *job_info,
158 if (job_info == NULL)
161 g_signal_handlers_disconnect_by_func (job_info->job,
162 G_CALLBACK (job_page_ready_cb),
164 g_signal_handlers_disconnect_by_func (job_info->job,
165 G_CALLBACK (job_finished_cb),
167 ev_job_cancel (job_info->job);
168 g_object_unref (job_info->job);
169 job_info->job = NULL;
171 if (job_info->surface) {
172 cairo_surface_destroy (job_info->surface);
173 job_info->surface = NULL;
175 if (job_info->region) {
176 gdk_region_destroy (job_info->region);
177 job_info->region = NULL;
179 if (job_info->link_mapping) {
180 ev_mapping_list_free (job_info->link_mapping, g_object_unref);
181 job_info->link_mapping = NULL;
183 if (job_info->image_mapping) {
184 ev_mapping_list_free (job_info->image_mapping, g_object_unref);
185 job_info->image_mapping = NULL;
187 if (job_info->form_field_mapping) {
188 ev_mapping_list_free (job_info->form_field_mapping, g_object_unref);
189 job_info->form_field_mapping = NULL;
191 if (job_info->annots_mapping) {
192 ev_mapping_list_free (job_info->annots_mapping, g_object_unref);
193 job_info->annots_mapping = NULL;
195 if (job_info->text_mapping) {
196 gdk_region_destroy (job_info->text_mapping);
197 job_info->text_mapping = NULL;
199 if (job_info->selection) {
200 cairo_surface_destroy (job_info->selection);
201 job_info->selection = NULL;
203 if (job_info->selection_region) {
204 gdk_region_destroy (job_info->selection_region);
205 job_info->selection_region = NULL;
208 g_object_unref (G_OBJECT (job_info->rc));
212 job_info->points_set = FALSE;
216 ev_pixbuf_cache_dispose (GObject *object)
218 EvPixbufCache *pixbuf_cache;
221 pixbuf_cache = EV_PIXBUF_CACHE (object);
223 for (i = 0; i < pixbuf_cache->preload_cache_size; i++) {
224 dispose_cache_job_info (pixbuf_cache->prev_job + i, pixbuf_cache);
225 dispose_cache_job_info (pixbuf_cache->next_job + i, pixbuf_cache);
228 for (i = 0; i < PAGE_CACHE_LEN (pixbuf_cache); i++) {
229 dispose_cache_job_info (pixbuf_cache->job_list + i, pixbuf_cache);
232 G_OBJECT_CLASS (ev_pixbuf_cache_parent_class)->dispose (object);
237 ev_pixbuf_cache_new (GtkWidget *view,
238 EvDocument *document)
240 EvPixbufCache *pixbuf_cache;
242 pixbuf_cache = (EvPixbufCache *) g_object_new (EV_TYPE_PIXBUF_CACHE, NULL);
243 /* This is a backlink, so we don't ref this */
244 pixbuf_cache->view = view;
245 pixbuf_cache->document = document;
251 job_page_ready_cb (EvJob *job,
252 EvPixbufCache *pixbuf_cache)
254 CacheJobInfo *job_info;
255 EvJobRender *job_render = EV_JOB_RENDER (job);
257 /* If the job is outside of our interest, we silently discard it */
258 if ((job_render->page < (pixbuf_cache->start_page - pixbuf_cache->preload_cache_size)) ||
259 (job_render->page > (pixbuf_cache->end_page + pixbuf_cache->preload_cache_size))) {
260 g_object_unref (job);
264 job_info = find_job_cache (pixbuf_cache, job_render->page);
266 copy_job_page_and_selection_to_job_info (job_render, job_info, pixbuf_cache);
267 g_signal_emit (pixbuf_cache, signals[JOB_FINISHED], 0, job_info->region);
271 job_finished_cb (EvJob *job,
272 EvPixbufCache *pixbuf_cache)
274 CacheJobInfo *job_info;
275 EvJobRender *job_render = EV_JOB_RENDER (job);
277 /* If the job is outside of our interest, we silently discard it */
278 if ((job_render->page < (pixbuf_cache->start_page - pixbuf_cache->preload_cache_size)) ||
279 (job_render->page > (pixbuf_cache->end_page + pixbuf_cache->preload_cache_size))) {
280 g_object_unref (job);
284 job_info = find_job_cache (pixbuf_cache, job_render->page);
285 copy_job_to_job_info (job_render, job_info, pixbuf_cache);
288 /* This checks a job to see if the job would generate the right sized pixbuf
289 * given a scale. If it won't, it removes the job and clears it to NULL.
292 check_job_size_and_unref (EvPixbufCache *pixbuf_cache,
293 CacheJobInfo *job_info,
294 EvPageCache *page_cache,
302 if (job_info->job == NULL)
305 ev_page_cache_get_size (page_cache,
306 EV_JOB_RENDER (job_info->job)->page,
307 EV_JOB_RENDER (job_info->job)->rotation,
311 if (width == EV_JOB_RENDER (job_info->job)->target_width &&
312 height == EV_JOB_RENDER (job_info->job)->target_height)
315 g_signal_handlers_disconnect_by_func (job_info->job,
316 G_CALLBACK (job_page_ready_cb),
318 g_signal_handlers_disconnect_by_func (job_info->job,
319 G_CALLBACK (job_finished_cb),
321 ev_job_cancel (job_info->job);
322 g_object_unref (job_info->job);
323 job_info->job = NULL;
326 /* Do all function that copies a job from an older cache to it's position in the
327 * new cache. It clears the old job if it doesn't have a place.
330 move_one_job (CacheJobInfo *job_info,
331 EvPixbufCache *pixbuf_cache,
333 CacheJobInfo *new_job_list,
334 CacheJobInfo *new_prev_job,
335 CacheJobInfo *new_next_job,
340 CacheJobInfo *target_page = NULL;
344 if (page < (start_page - pixbuf_cache->preload_cache_size) ||
345 page > (end_page + pixbuf_cache->preload_cache_size)) {
346 dispose_cache_job_info (job_info, pixbuf_cache);
350 /* find the target page to copy it over to. */
351 if (page < start_page) {
352 page_offset = (page - (start_page - pixbuf_cache->preload_cache_size));
354 g_assert (page_offset >= 0 &&
355 page_offset < pixbuf_cache->preload_cache_size);
356 target_page = new_prev_job + page_offset;
357 new_priority = EV_JOB_PRIORITY_LOW;
358 } else if (page > end_page) {
359 page_offset = (page - (end_page + 1));
361 g_assert (page_offset >= 0 &&
362 page_offset < pixbuf_cache->preload_cache_size);
363 target_page = new_next_job + page_offset;
364 new_priority = EV_JOB_PRIORITY_LOW;
366 page_offset = page - start_page;
367 g_assert (page_offset >= 0 &&
368 page_offset <= ((end_page - start_page) + 1));
369 new_priority = EV_JOB_PRIORITY_URGENT;
370 target_page = new_job_list + page_offset;
373 *target_page = *job_info;
374 job_info->job = NULL;
375 job_info->region = NULL;
376 job_info->surface = NULL;
377 job_info->link_mapping = NULL;
378 job_info->image_mapping = NULL;
379 job_info->form_field_mapping = NULL;
380 job_info->annots_mapping = NULL;
382 if (new_priority != priority && target_page->job) {
383 ev_job_scheduler_update_job (target_page->job, new_priority);
388 ev_pixbuf_cache_update_range (EvPixbufCache *pixbuf_cache,
392 CacheJobInfo *new_job_list;
393 CacheJobInfo *new_prev_job;
394 CacheJobInfo *new_next_job;
395 EvPageCache *page_cache;
398 if (pixbuf_cache->start_page == start_page &&
399 pixbuf_cache->end_page == end_page)
402 page_cache = ev_page_cache_get (pixbuf_cache->document);
404 new_job_list = g_new0 (CacheJobInfo, (end_page - start_page) + 1);
405 new_prev_job = g_new0 (CacheJobInfo, pixbuf_cache->preload_cache_size);
406 new_next_job = g_new0 (CacheJobInfo, pixbuf_cache->preload_cache_size);
408 /* We go through each job in the old cache and either clear it or move
409 * it to a new location. */
411 /* Start with the prev cache. */
412 page = pixbuf_cache->start_page - pixbuf_cache->preload_cache_size;
413 for (i = 0; i < pixbuf_cache->preload_cache_size; i++) {
415 dispose_cache_job_info (pixbuf_cache->prev_job + i, pixbuf_cache);
417 move_one_job (pixbuf_cache->prev_job + i,
419 new_job_list, new_prev_job, new_next_job,
420 start_page, end_page, EV_JOB_PRIORITY_LOW);
425 page = pixbuf_cache->start_page;
426 for (i = 0; i < PAGE_CACHE_LEN (pixbuf_cache); i++) {
427 move_one_job (pixbuf_cache->job_list + i,
429 new_job_list, new_prev_job, new_next_job,
430 start_page, end_page, EV_JOB_PRIORITY_URGENT);
434 for (i = 0; i < pixbuf_cache->preload_cache_size; i++) {
435 if (page >= ev_page_cache_get_n_pages (page_cache)) {
436 dispose_cache_job_info (pixbuf_cache->next_job + i, pixbuf_cache);
438 move_one_job (pixbuf_cache->next_job + i,
440 new_job_list, new_prev_job, new_next_job,
441 start_page, end_page, EV_JOB_PRIORITY_LOW);
446 g_free (pixbuf_cache->job_list);
447 g_free (pixbuf_cache->prev_job);
448 g_free (pixbuf_cache->next_job);
450 pixbuf_cache->job_list = new_job_list;
451 pixbuf_cache->prev_job = new_prev_job;
452 pixbuf_cache->next_job = new_next_job;
454 pixbuf_cache->start_page = start_page;
455 pixbuf_cache->end_page = end_page;
459 copy_job_page_and_selection_to_job_info (EvJobRender *job_render,
460 CacheJobInfo *job_info,
461 EvPixbufCache *pixbuf_cache)
463 if (job_info->rc == NULL) {
464 job_info->rc = ev_render_context_new (job_render->ev_page,
465 job_render->rotation,
468 ev_render_context_set_page (job_info->rc, job_render->ev_page);
469 ev_render_context_set_rotation (job_info->rc, job_render->rotation);
470 ev_render_context_set_scale (job_info->rc, job_render->scale);
473 if (job_info->surface) {
474 cairo_surface_destroy (job_info->surface);
476 job_info->surface = cairo_surface_reference (job_render->surface);
478 job_info->points_set = FALSE;
479 if (job_render->flags & EV_RENDER_INCLUDE_SELECTION) {
480 if (job_info->selection) {
481 cairo_surface_destroy (job_info->selection);
482 job_info->selection = NULL;
484 if (job_info->selection_region) {
485 gdk_region_destroy (job_info->selection_region);
486 job_info->selection_region = NULL;
489 job_info->selection_points = job_render->selection_points;
490 job_info->selection_region = gdk_region_copy (job_render->selection_region);
491 job_info->selection = cairo_surface_reference (job_render->selection);
492 g_assert (job_info->selection_points.x1 >= 0);
493 job_info->points_set = TRUE;
497 g_signal_handlers_disconnect_by_func (job_info->job,
498 G_CALLBACK (job_page_ready_cb),
502 job_info->page_ready = TRUE;
506 copy_job_to_job_info (EvJobRender *job_render,
507 CacheJobInfo *job_info,
508 EvPixbufCache *pixbuf_cache)
510 if (!job_info->page_ready) {
511 g_signal_emit (pixbuf_cache, signals[JOB_FINISHED], 0, job_info->region);
512 copy_job_page_and_selection_to_job_info (job_render,
517 if (job_render->flags & EV_RENDER_INCLUDE_LINKS) {
518 if (job_info->link_mapping)
519 ev_mapping_list_free (job_info->link_mapping, g_object_unref);
520 job_info->link_mapping = job_render->link_mapping;
523 if (job_render->flags & EV_RENDER_INCLUDE_IMAGES) {
524 if (job_info->image_mapping)
525 ev_mapping_list_free (job_info->image_mapping, g_object_unref);
526 job_info->image_mapping = job_render->image_mapping;
529 if (job_render->flags & EV_RENDER_INCLUDE_FORMS) {
530 if (job_info->form_field_mapping)
531 ev_mapping_list_free (job_info->form_field_mapping, g_object_unref);
532 job_info->form_field_mapping = job_render->form_field_mapping;
535 if (job_render->flags & EV_RENDER_INCLUDE_ANNOTS) {
536 if (job_info->annots_mapping)
537 ev_mapping_list_free (job_info->annots_mapping, g_object_unref);
538 job_info->annots_mapping = job_render->annots_mapping;
541 if (job_render->flags & EV_RENDER_INCLUDE_TEXT) {
542 if (job_info->text_mapping)
543 gdk_region_destroy (job_info->text_mapping);
544 job_info->text_mapping = job_render->text_mapping;
548 g_signal_handlers_disconnect_by_func (job_info->job,
549 G_CALLBACK (job_finished_cb),
551 ev_job_cancel (job_info->job);
552 g_object_unref (job_info->job);
553 job_info->job = NULL;
557 static CacheJobInfo *
558 find_job_cache (EvPixbufCache *pixbuf_cache,
563 if (page < (pixbuf_cache->start_page - pixbuf_cache->preload_cache_size) ||
564 page > (pixbuf_cache->end_page + pixbuf_cache->preload_cache_size))
567 if (page < pixbuf_cache->start_page) {
568 page_offset = (page - (pixbuf_cache->start_page - pixbuf_cache->preload_cache_size));
570 g_assert (page_offset >= 0 &&
571 page_offset < pixbuf_cache->preload_cache_size);
572 return pixbuf_cache->prev_job + page_offset;
575 if (page > pixbuf_cache->end_page) {
576 page_offset = (page - (pixbuf_cache->end_page + 1));
578 g_assert (page_offset >= 0 &&
579 page_offset < pixbuf_cache->preload_cache_size);
580 return pixbuf_cache->next_job + page_offset;
583 page_offset = page - pixbuf_cache->start_page;
584 g_assert (page_offset >= 0 &&
585 page_offset <= PAGE_CACHE_LEN(pixbuf_cache));
586 return pixbuf_cache->job_list + page_offset;
590 ev_pixbuf_cache_clear_job_sizes (EvPixbufCache *pixbuf_cache,
593 EvPageCache *page_cache;
596 page_cache = ev_page_cache_get (pixbuf_cache->document);
598 for (i = 0; i < PAGE_CACHE_LEN (pixbuf_cache); i++) {
599 check_job_size_and_unref (pixbuf_cache, pixbuf_cache->job_list + i, page_cache, scale);
602 for (i = 0; i < pixbuf_cache->preload_cache_size; i++) {
603 check_job_size_and_unref (pixbuf_cache, pixbuf_cache->prev_job + i, page_cache, scale);
604 check_job_size_and_unref (pixbuf_cache, pixbuf_cache->next_job + i, page_cache, scale);
609 get_selection_colors (GtkWidget *widget, GdkColor **text, GdkColor **base)
611 if (GTK_WIDGET_HAS_FOCUS (widget)) {
612 *text = &widget->style->text [GTK_STATE_SELECTED];
613 *base = &widget->style->base [GTK_STATE_SELECTED];
615 *text = &widget->style->text [GTK_STATE_ACTIVE];
616 *base = &widget->style->base [GTK_STATE_ACTIVE];
621 add_job (EvPixbufCache *pixbuf_cache,
622 CacheJobInfo *job_info,
623 EvPageCache *page_cache,
630 EvJobPriority priority)
632 EvRenderFlags flags = 0;
634 job_info->page_ready = FALSE;
636 if (job_info->region)
637 gdk_region_destroy (job_info->region);
638 job_info->region = region ? gdk_region_copy (region) : NULL;
640 /* Figure out what else we need for this job */
641 if (job_info->link_mapping == NULL)
642 flags |= EV_RENDER_INCLUDE_LINKS;
643 if (job_info->image_mapping == NULL)
644 flags |= EV_RENDER_INCLUDE_IMAGES;
645 if (job_info->form_field_mapping == NULL)
646 flags |= EV_RENDER_INCLUDE_FORMS;
647 if (job_info->annots_mapping == NULL)
648 flags |= EV_RENDER_INCLUDE_ANNOTS;
649 if (job_info->text_mapping == NULL)
650 flags |= EV_RENDER_INCLUDE_TEXT;
652 job_info->job = ev_job_render_new (pixbuf_cache->document,
653 page, rotation, scale,
657 if (new_selection_surface_needed (pixbuf_cache, job_info, page, scale)) {
658 GdkColor *text, *base;
660 gtk_widget_ensure_style (pixbuf_cache->view);
661 get_selection_colors (pixbuf_cache->view, &text, &base);
662 ev_job_render_set_selection_info (EV_JOB_RENDER (job_info->job),
663 &(job_info->target_points),
664 job_info->selection_style,
668 g_signal_connect (job_info->job, "page-ready",
669 G_CALLBACK (job_page_ready_cb),
671 g_signal_connect (job_info->job, "finished",
672 G_CALLBACK (job_finished_cb),
674 ev_job_scheduler_push_job (job_info->job, priority);
678 add_job_if_needed (EvPixbufCache *pixbuf_cache,
679 CacheJobInfo *job_info,
680 EvPageCache *page_cache,
684 EvJobPriority priority)
691 ev_page_cache_get_size (page_cache, page, rotation,
692 scale, &width, &height);
694 if (job_info->surface &&
695 cairo_image_surface_get_width (job_info->surface) == width &&
696 cairo_image_surface_get_height (job_info->surface) == height)
699 add_job (pixbuf_cache, job_info, page_cache, NULL,
700 width, height, page, rotation, scale,
705 ev_pixbuf_cache_add_jobs_if_needed (EvPixbufCache *pixbuf_cache,
709 EvPageCache *page_cache;
710 CacheJobInfo *job_info;
714 page_cache = ev_page_cache_get (pixbuf_cache->document);
716 for (i = 0; i < PAGE_CACHE_LEN (pixbuf_cache); i++) {
717 job_info = (pixbuf_cache->job_list + i);
718 page = pixbuf_cache->start_page + i;
720 add_job_if_needed (pixbuf_cache, job_info,
721 page_cache, page, rotation, scale,
722 EV_JOB_PRIORITY_URGENT);
725 for (i = FIRST_VISIBLE_PREV(pixbuf_cache); i < pixbuf_cache->preload_cache_size; i++) {
726 job_info = (pixbuf_cache->prev_job + i);
727 page = pixbuf_cache->start_page - pixbuf_cache->preload_cache_size + i;
729 add_job_if_needed (pixbuf_cache, job_info,
730 page_cache, page, rotation, scale,
731 EV_JOB_PRIORITY_LOW);
734 for (i = 0; i < VISIBLE_NEXT_LEN(pixbuf_cache, page_cache); i++) {
735 job_info = (pixbuf_cache->next_job + i);
736 page = pixbuf_cache->end_page + 1 + i;
738 add_job_if_needed (pixbuf_cache, job_info,
739 page_cache, page, rotation, scale,
740 EV_JOB_PRIORITY_LOW);
746 ev_pixbuf_cache_set_page_range (EvPixbufCache *pixbuf_cache,
751 GList *selection_list)
753 EvPageCache *page_cache;
755 g_return_if_fail (EV_IS_PIXBUF_CACHE (pixbuf_cache));
757 page_cache = ev_page_cache_get (pixbuf_cache->document);
759 g_return_if_fail (start_page >= 0 && start_page < ev_page_cache_get_n_pages (page_cache));
760 g_return_if_fail (end_page >= 0 && end_page < ev_page_cache_get_n_pages (page_cache));
761 g_return_if_fail (end_page >= start_page);
763 /* First, resize the page_range as needed. We cull old pages
765 ev_pixbuf_cache_update_range (pixbuf_cache, start_page, end_page);
767 /* Then, we update the current jobs to see if any of them are the wrong
768 * size, we remove them if we need to. */
769 ev_pixbuf_cache_clear_job_sizes (pixbuf_cache, scale);
771 /* Next, we update the target selection for our pages */
772 ev_pixbuf_cache_set_selection_list (pixbuf_cache, selection_list);
774 /* Finally, we add the new jobs for all the sizes that don't have a
776 ev_pixbuf_cache_add_jobs_if_needed (pixbuf_cache, rotation, scale);
780 ev_pixbuf_cache_get_surface (EvPixbufCache *pixbuf_cache,
783 CacheJobInfo *job_info;
785 job_info = find_job_cache (pixbuf_cache, page);
786 if (job_info == NULL)
789 if (job_info->page_ready)
790 return job_info->surface;
792 /* We don't need to wait for the idle to handle the callback */
794 EV_JOB_RENDER (job_info->job)->page_ready) {
795 copy_job_page_and_selection_to_job_info (EV_JOB_RENDER (job_info->job), job_info, pixbuf_cache);
796 g_signal_emit (pixbuf_cache, signals[JOB_FINISHED], 0, job_info->region);
799 return job_info->surface;
803 ev_pixbuf_cache_get_link_mapping (EvPixbufCache *pixbuf_cache,
806 CacheJobInfo *job_info;
808 job_info = find_job_cache (pixbuf_cache, page);
809 if (job_info == NULL)
812 /* We don't need to wait for the idle to handle the callback */
814 EV_JOB (job_info->job)->finished) {
815 copy_job_to_job_info (EV_JOB_RENDER (job_info->job), job_info, pixbuf_cache);
818 return job_info->link_mapping;
822 ev_pixbuf_cache_get_image_mapping (EvPixbufCache *pixbuf_cache,
825 CacheJobInfo *job_info;
827 if (!EV_IS_DOCUMENT_IMAGES (pixbuf_cache->document))
830 job_info = find_job_cache (pixbuf_cache, page);
831 if (job_info == NULL)
834 /* We don't need to wait for the idle to handle the callback */
836 EV_JOB (job_info->job)->finished) {
837 copy_job_to_job_info (EV_JOB_RENDER (job_info->job), job_info, pixbuf_cache);
840 return job_info->image_mapping;
844 ev_pixbuf_cache_get_form_field_mapping (EvPixbufCache *pixbuf_cache,
847 CacheJobInfo *job_info;
849 if (!EV_IS_DOCUMENT_FORMS (pixbuf_cache->document))
852 job_info = find_job_cache (pixbuf_cache, page);
853 if (job_info == NULL)
856 /* We don't need to wait for the idle to handle the callback */
858 EV_JOB (job_info->job)->finished) {
859 copy_job_to_job_info (EV_JOB_RENDER(job_info->job), job_info, pixbuf_cache);
862 return job_info->form_field_mapping;
866 ev_pixbuf_cache_get_annots_mapping (EvPixbufCache *pixbuf_cache,
869 CacheJobInfo *job_info;
871 if (!EV_IS_DOCUMENT_ANNOTATIONS (pixbuf_cache->document))
874 job_info = find_job_cache (pixbuf_cache, page);
875 if (job_info == NULL)
878 /* We don't need to wait for the idle to handle the callback */
880 EV_JOB (job_info->job)->finished) {
881 copy_job_to_job_info (EV_JOB_RENDER (job_info->job), job_info, pixbuf_cache);
884 return job_info->annots_mapping;
888 new_selection_surface_needed (EvPixbufCache *pixbuf_cache,
889 CacheJobInfo *job_info,
893 EvPageCache *page_cache;
895 if (job_info->selection && job_info->rc) {
897 gint selection_width, selection_height;
899 page_cache = ev_page_cache_get (pixbuf_cache->document);
900 ev_page_cache_get_size (page_cache, page,
901 job_info->rc->rotation,
902 scale, &width, &height);
904 selection_width = cairo_image_surface_get_width (job_info->selection);
905 selection_height = cairo_image_surface_get_height (job_info->selection);
907 if (width != selection_width || height != selection_height)
910 if (job_info->points_set)
918 clear_selection_if_needed (EvPixbufCache *pixbuf_cache,
919 CacheJobInfo *job_info,
923 if (new_selection_surface_needed (pixbuf_cache, job_info, page, scale)) {
924 if (job_info->selection)
925 cairo_surface_destroy (job_info->selection);
926 job_info->selection = NULL;
927 job_info->selection_points.x1 = -1;
932 ev_pixbuf_cache_get_text_mapping (EvPixbufCache *pixbuf_cache,
935 CacheJobInfo *job_info;
937 job_info = find_job_cache (pixbuf_cache, page);
938 if (job_info == NULL)
941 /* We don't need to wait for the idle to handle the callback */
943 EV_JOB (job_info->job)->finished) {
944 copy_job_to_job_info (EV_JOB_RENDER (job_info->job), job_info, pixbuf_cache);
947 return job_info->text_mapping;
950 /* Clears the cache of jobs and pixbufs.
953 ev_pixbuf_cache_clear (EvPixbufCache *pixbuf_cache)
957 for (i = 0; i < pixbuf_cache->preload_cache_size; i++) {
958 dispose_cache_job_info (pixbuf_cache->prev_job + i, pixbuf_cache);
959 dispose_cache_job_info (pixbuf_cache->next_job + i, pixbuf_cache);
962 for (i = 0; i < PAGE_CACHE_LEN (pixbuf_cache); i++) {
963 dispose_cache_job_info (pixbuf_cache->job_list + i, pixbuf_cache);
969 ev_pixbuf_cache_style_changed (EvPixbufCache *pixbuf_cache)
973 /* FIXME: doesn't update running jobs. */
974 for (i = 0; i < pixbuf_cache->preload_cache_size; i++) {
975 CacheJobInfo *job_info;
977 job_info = pixbuf_cache->prev_job + i;
978 if (job_info->selection) {
979 cairo_surface_destroy (job_info->selection);
980 job_info->selection = NULL;
983 job_info = pixbuf_cache->next_job + i;
984 if (job_info->selection) {
985 cairo_surface_destroy (job_info->selection);
986 job_info->selection = NULL;
990 for (i = 0; i < PAGE_CACHE_LEN (pixbuf_cache); i++) {
991 CacheJobInfo *job_info;
993 job_info = pixbuf_cache->job_list + i;
994 if (job_info->selection) {
995 cairo_surface_destroy (job_info->selection);
996 job_info->selection = NULL;
1002 ev_pixbuf_cache_get_selection_surface (EvPixbufCache *pixbuf_cache,
1007 CacheJobInfo *job_info;
1009 /* the document does not implement the selection interface */
1010 if (!EV_IS_SELECTION (pixbuf_cache->document))
1013 job_info = find_job_cache (pixbuf_cache, page);
1014 if (job_info == NULL)
1017 /* No selection on this page */
1018 if (!job_info->points_set)
1021 /* Create new render context if needed (selection + fast scrolling) */
1022 if (job_info->rc == NULL) {
1024 ev_page = ev_document_get_page (pixbuf_cache->document, page);
1025 job_info->rc = ev_render_context_new (ev_page, 0, scale);
1026 g_object_unref (ev_page);
1030 ev_render_context_set_scale (job_info->rc, scale);
1032 /* If we have a running job, we just return what we have under the
1033 * assumption that it'll be updated later and we can scale it as need
1035 if (job_info->job && (EV_JOB_RENDER (job_info->job)->flags & EV_RENDER_INCLUDE_SELECTION))
1036 return job_info->selection;
1038 /* Now, lets see if we need to resize the image. If we do, we clear the
1040 clear_selection_if_needed (pixbuf_cache, job_info, page, scale);
1042 /* Finally, we see if the two scales are the same, and get a new pixbuf
1043 * if needed. We do this synchronously for now. At some point, we
1044 * _should_ be able to get rid of the doc_mutex, so the synchronicity
1045 * doesn't kill us. Rendering a few glyphs should really be fast.
1047 if (ev_rect_cmp (&(job_info->target_points), &(job_info->selection_points))) {
1048 EvRectangle *old_points;
1049 GdkColor *text, *base;
1051 /* we need to get a new selection pixbuf */
1052 ev_document_doc_mutex_lock ();
1053 if (job_info->selection_points.x1 < 0) {
1054 g_assert (job_info->selection == NULL);
1057 g_assert (job_info->selection != NULL);
1058 old_points = &(job_info->selection_points);
1061 if (job_info->selection_region)
1062 gdk_region_destroy (job_info->selection_region);
1063 job_info->selection_region =
1064 ev_selection_get_selection_region (EV_SELECTION (pixbuf_cache->document),
1066 job_info->selection_style,
1067 &(job_info->target_points));
1069 gtk_widget_ensure_style (pixbuf_cache->view);
1071 get_selection_colors (pixbuf_cache->view, &text, &base);
1073 ev_selection_render_selection (EV_SELECTION (pixbuf_cache->document),
1074 job_info->rc, &(job_info->selection),
1075 &(job_info->target_points),
1077 job_info->selection_style,
1079 job_info->selection_points = job_info->target_points;
1080 ev_document_doc_mutex_unlock ();
1083 *region = job_info->selection_region;
1084 return job_info->selection;
1088 update_job_selection (CacheJobInfo *job_info,
1089 EvViewSelection *selection)
1091 job_info->points_set = TRUE;
1092 job_info->target_points = selection->rect;
1093 job_info->selection_style = selection->style;
1097 clear_job_selection (CacheJobInfo *job_info)
1099 job_info->points_set = FALSE;
1100 job_info->selection_points.x1 = -1;
1102 if (job_info->selection) {
1103 cairo_surface_destroy (job_info->selection);
1104 job_info->selection = NULL;
1108 /* This function will reset the selection on pages that no longer have them, and
1109 * will update the target_selection on those that need it. It will _not_ free
1110 * the previous selection_list -- that's up to caller to do.
1113 ev_pixbuf_cache_set_selection_list (EvPixbufCache *pixbuf_cache,
1114 GList *selection_list)
1116 EvPageCache *page_cache;
1117 EvViewSelection *selection;
1118 GList *list = selection_list;
1122 g_return_if_fail (EV_IS_PIXBUF_CACHE (pixbuf_cache));
1124 if (!EV_IS_SELECTION (pixbuf_cache->document))
1127 page_cache = ev_page_cache_get (pixbuf_cache->document);
1129 /* We check each area to see what needs updating, and what needs freeing; */
1130 page = pixbuf_cache->start_page - pixbuf_cache->preload_cache_size;
1131 for (i = 0; i < pixbuf_cache->preload_cache_size; i++) {
1139 if (((EvViewSelection *)list->data)->page == page) {
1140 selection = list->data;
1142 } else if (((EvViewSelection *)list->data)->page > page)
1148 update_job_selection (pixbuf_cache->prev_job + i, selection);
1150 clear_job_selection (pixbuf_cache->prev_job + i);
1154 page = pixbuf_cache->start_page;
1155 for (i = 0; i < PAGE_CACHE_LEN (pixbuf_cache); i++) {
1158 if (((EvViewSelection *)list->data)->page == page) {
1159 selection = list->data;
1161 } else if (((EvViewSelection *)list->data)->page > page)
1167 update_job_selection (pixbuf_cache->job_list + i, selection);
1169 clear_job_selection (pixbuf_cache->job_list + i);
1173 for (i = 0; i < pixbuf_cache->preload_cache_size; i++) {
1174 if (page >= ev_page_cache_get_n_pages (page_cache))
1179 if (((EvViewSelection *)list->data)->page == page) {
1180 selection = list->data;
1182 } else if (((EvViewSelection *)list->data)->page > page)
1188 update_job_selection (pixbuf_cache->next_job + i, selection);
1190 clear_job_selection (pixbuf_cache->next_job + i);
1196 /* Returns what the pixbuf cache thinks is */
1199 ev_pixbuf_cache_get_selection_list (EvPixbufCache *pixbuf_cache)
1201 EvPageCache *page_cache;
1202 EvViewSelection *selection;
1203 GList *retval = NULL;
1207 g_return_val_if_fail (EV_IS_PIXBUF_CACHE (pixbuf_cache), NULL);
1209 page_cache = ev_page_cache_get (pixbuf_cache->document);
1211 /* We check each area to see what needs updating, and what needs freeing; */
1212 page = pixbuf_cache->start_page - pixbuf_cache->preload_cache_size;
1213 for (i = 0; i < pixbuf_cache->preload_cache_size; i++) {
1219 if (pixbuf_cache->prev_job[i].selection_points.x1 != -1) {
1220 selection = g_new0 (EvViewSelection, 1);
1221 selection->page = page;
1222 selection->rect = pixbuf_cache->prev_job[i].selection_points;
1223 if (pixbuf_cache->prev_job[i].selection_region)
1224 selection->covered_region = gdk_region_copy (pixbuf_cache->prev_job[i].selection_region);
1225 retval = g_list_append (retval, selection);
1231 page = pixbuf_cache->start_page;
1232 for (i = 0; i < PAGE_CACHE_LEN (pixbuf_cache); i++) {
1233 if (pixbuf_cache->job_list[i].selection_points.x1 != -1) {
1234 selection = g_new0 (EvViewSelection, 1);
1235 selection->page = page;
1236 selection->rect = pixbuf_cache->job_list[i].selection_points;
1237 if (pixbuf_cache->job_list[i].selection_region)
1238 selection->covered_region = gdk_region_copy (pixbuf_cache->job_list[i].selection_region);
1239 retval = g_list_append (retval, selection);
1245 for (i = 0; i < pixbuf_cache->preload_cache_size; i++) {
1246 if (page >= ev_page_cache_get_n_pages (page_cache))
1249 if (pixbuf_cache->next_job[i].selection_points.x1 != -1) {
1250 selection = g_new0 (EvViewSelection, 1);
1251 selection->page = page;
1252 selection->rect = pixbuf_cache->next_job[i].selection_points;
1253 if (pixbuf_cache->next_job[i].selection_region)
1254 selection->covered_region = gdk_region_copy (pixbuf_cache->next_job[i].selection_region);
1255 retval = g_list_append (retval, selection);
1265 ev_pixbuf_cache_reload_page (EvPixbufCache *pixbuf_cache,
1271 CacheJobInfo *job_info;
1272 EvPageCache *page_cache;
1275 job_info = find_job_cache (pixbuf_cache, page);
1276 if (job_info == NULL)
1279 page_cache = ev_page_cache_get (pixbuf_cache->document);
1280 ev_page_cache_get_size (page_cache, page, rotation, scale,
1283 add_job (pixbuf_cache, job_info, page_cache, region,
1284 width, height, page, rotation, scale,
1285 EV_JOB_PRIORITY_URGENT);