2 #include "ev-pixbuf-cache.h"
3 #include "ev-job-scheduler.h"
4 #include "ev-page-cache.h"
5 #include "ev-document-images.h"
6 #include "ev-document-forms.h"
7 #include "ev-document-links.h"
9 #include "ev-form-field.h"
11 typedef struct _CacheJobInfo
17 /* Region of the page that needs to be drawn */
20 /* Data we get from rendering */
21 cairo_surface_t *surface;
24 GList *form_field_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_link_mapping_free (job_info->link_mapping);
181 job_info->link_mapping = NULL;
183 if (job_info->image_mapping) {
184 ev_image_mapping_free (job_info->image_mapping);
185 job_info->image_mapping = NULL;
187 if (job_info->form_field_mapping) {
188 ev_form_field_mapping_free (job_info->form_field_mapping);
189 job_info->form_field_mapping = NULL;
191 if (job_info->text_mapping) {
192 gdk_region_destroy (job_info->text_mapping);
193 job_info->text_mapping = NULL;
195 if (job_info->selection) {
196 cairo_surface_destroy (job_info->selection);
197 job_info->selection = NULL;
199 if (job_info->selection_region) {
200 gdk_region_destroy (job_info->selection_region);
201 job_info->selection_region = NULL;
204 g_object_unref (G_OBJECT (job_info->rc));
208 job_info->points_set = FALSE;
212 ev_pixbuf_cache_dispose (GObject *object)
214 EvPixbufCache *pixbuf_cache;
217 pixbuf_cache = EV_PIXBUF_CACHE (object);
219 for (i = 0; i < pixbuf_cache->preload_cache_size; i++) {
220 dispose_cache_job_info (pixbuf_cache->prev_job + i, pixbuf_cache);
221 dispose_cache_job_info (pixbuf_cache->next_job + i, pixbuf_cache);
224 for (i = 0; i < PAGE_CACHE_LEN (pixbuf_cache); i++) {
225 dispose_cache_job_info (pixbuf_cache->job_list + i, pixbuf_cache);
228 G_OBJECT_CLASS (ev_pixbuf_cache_parent_class)->dispose (object);
233 ev_pixbuf_cache_new (GtkWidget *view,
234 EvDocument *document)
236 EvPixbufCache *pixbuf_cache;
238 pixbuf_cache = (EvPixbufCache *) g_object_new (EV_TYPE_PIXBUF_CACHE, NULL);
239 /* This is a backlink, so we don't ref this */
240 pixbuf_cache->view = view;
241 pixbuf_cache->document = document;
247 job_page_ready_cb (EvJob *job,
248 EvPixbufCache *pixbuf_cache)
250 CacheJobInfo *job_info;
251 EvJobRender *job_render = EV_JOB_RENDER (job);
253 /* If the job is outside of our interest, we silently discard it */
254 if ((job_render->page < (pixbuf_cache->start_page - pixbuf_cache->preload_cache_size)) ||
255 (job_render->page > (pixbuf_cache->end_page + pixbuf_cache->preload_cache_size))) {
256 g_object_unref (job);
260 job_info = find_job_cache (pixbuf_cache, job_render->page);
262 copy_job_page_and_selection_to_job_info (job_render, job_info, pixbuf_cache);
263 g_signal_emit (pixbuf_cache, signals[JOB_FINISHED], 0, job_info->region);
267 job_finished_cb (EvJob *job,
268 EvPixbufCache *pixbuf_cache)
270 CacheJobInfo *job_info;
271 EvJobRender *job_render = EV_JOB_RENDER (job);
273 /* If the job is outside of our interest, we silently discard it */
274 if ((job_render->page < (pixbuf_cache->start_page - pixbuf_cache->preload_cache_size)) ||
275 (job_render->page > (pixbuf_cache->end_page + pixbuf_cache->preload_cache_size))) {
276 g_object_unref (job);
280 job_info = find_job_cache (pixbuf_cache, job_render->page);
281 copy_job_to_job_info (job_render, job_info, pixbuf_cache);
284 /* This checks a job to see if the job would generate the right sized pixbuf
285 * given a scale. If it won't, it removes the job and clears it to NULL.
288 check_job_size_and_unref (EvPixbufCache *pixbuf_cache,
289 CacheJobInfo *job_info,
290 EvPageCache *page_cache,
298 if (job_info->job == NULL)
301 ev_page_cache_get_size (page_cache,
302 EV_JOB_RENDER (job_info->job)->page,
303 EV_JOB_RENDER (job_info->job)->rotation,
307 if (width == EV_JOB_RENDER (job_info->job)->target_width &&
308 height == EV_JOB_RENDER (job_info->job)->target_height)
311 g_signal_handlers_disconnect_by_func (job_info->job,
312 G_CALLBACK (job_page_ready_cb),
314 g_signal_handlers_disconnect_by_func (job_info->job,
315 G_CALLBACK (job_finished_cb),
317 ev_job_cancel (job_info->job);
318 g_object_unref (job_info->job);
319 job_info->job = NULL;
322 /* Do all function that copies a job from an older cache to it's position in the
323 * new cache. It clears the old job if it doesn't have a place.
326 move_one_job (CacheJobInfo *job_info,
327 EvPixbufCache *pixbuf_cache,
329 CacheJobInfo *new_job_list,
330 CacheJobInfo *new_prev_job,
331 CacheJobInfo *new_next_job,
336 CacheJobInfo *target_page = NULL;
340 if (page < (start_page - pixbuf_cache->preload_cache_size) ||
341 page > (end_page + pixbuf_cache->preload_cache_size)) {
342 dispose_cache_job_info (job_info, pixbuf_cache);
346 /* find the target page to copy it over to. */
347 if (page < start_page) {
348 page_offset = (page - (start_page - pixbuf_cache->preload_cache_size));
350 g_assert (page_offset >= 0 &&
351 page_offset < pixbuf_cache->preload_cache_size);
352 target_page = new_prev_job + page_offset;
353 new_priority = EV_JOB_PRIORITY_LOW;
354 } else if (page > end_page) {
355 page_offset = (page - (end_page + 1));
357 g_assert (page_offset >= 0 &&
358 page_offset < pixbuf_cache->preload_cache_size);
359 target_page = new_next_job + page_offset;
360 new_priority = EV_JOB_PRIORITY_LOW;
362 page_offset = page - start_page;
363 g_assert (page_offset >= 0 &&
364 page_offset <= ((end_page - start_page) + 1));
365 new_priority = EV_JOB_PRIORITY_URGENT;
366 target_page = new_job_list + page_offset;
369 *target_page = *job_info;
370 job_info->job = NULL;
371 job_info->region = NULL;
372 job_info->surface = NULL;
373 job_info->link_mapping = NULL;
374 job_info->image_mapping = NULL;
375 job_info->form_field_mapping = NULL;
377 if (new_priority != priority && target_page->job) {
378 ev_job_scheduler_update_job (target_page->job, new_priority);
383 ev_pixbuf_cache_update_range (EvPixbufCache *pixbuf_cache,
387 CacheJobInfo *new_job_list;
388 CacheJobInfo *new_prev_job;
389 CacheJobInfo *new_next_job;
390 EvPageCache *page_cache;
393 if (pixbuf_cache->start_page == start_page &&
394 pixbuf_cache->end_page == end_page)
397 page_cache = ev_page_cache_get (pixbuf_cache->document);
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_page_cache_get_n_pages (page_cache)) {
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);
473 job_info->points_set = FALSE;
474 if (job_render->flags & EV_RENDER_INCLUDE_SELECTION) {
475 if (job_info->selection) {
476 cairo_surface_destroy (job_info->selection);
477 job_info->selection = NULL;
479 if (job_info->selection_region) {
480 gdk_region_destroy (job_info->selection_region);
481 job_info->selection_region = NULL;
484 job_info->selection_points = job_render->selection_points;
485 job_info->selection_region = gdk_region_copy (job_render->selection_region);
486 job_info->selection = cairo_surface_reference (job_render->selection);
487 g_assert (job_info->selection_points.x1 >= 0);
488 job_info->points_set = TRUE;
492 g_signal_handlers_disconnect_by_func (job_info->job,
493 G_CALLBACK (job_page_ready_cb),
497 job_info->page_ready = TRUE;
501 copy_job_to_job_info (EvJobRender *job_render,
502 CacheJobInfo *job_info,
503 EvPixbufCache *pixbuf_cache)
505 if (!job_info->page_ready) {
506 g_signal_emit (pixbuf_cache, signals[JOB_FINISHED], 0, job_info->region);
507 copy_job_page_and_selection_to_job_info (job_render,
512 if (job_render->flags & EV_RENDER_INCLUDE_LINKS) {
513 if (job_info->link_mapping)
514 ev_link_mapping_free (job_info->link_mapping);
515 job_info->link_mapping = job_render->link_mapping;
518 if (job_render->flags & EV_RENDER_INCLUDE_IMAGES) {
519 if (job_info->image_mapping)
520 ev_image_mapping_free (job_info->image_mapping);
521 job_info->image_mapping = job_render->image_mapping;
524 if (job_render->flags & EV_RENDER_INCLUDE_FORMS) {
525 if (job_info->form_field_mapping)
526 ev_form_field_mapping_free (job_info->form_field_mapping);
527 job_info->form_field_mapping = job_render->form_field_mapping;
530 if (job_render->flags & EV_RENDER_INCLUDE_TEXT) {
531 if (job_info->text_mapping)
532 gdk_region_destroy (job_info->text_mapping);
533 job_info->text_mapping = job_render->text_mapping;
537 g_signal_handlers_disconnect_by_func (job_info->job,
538 G_CALLBACK (job_finished_cb),
540 ev_job_cancel (job_info->job);
541 g_object_unref (job_info->job);
542 job_info->job = NULL;
546 static CacheJobInfo *
547 find_job_cache (EvPixbufCache *pixbuf_cache,
552 if (page < (pixbuf_cache->start_page - pixbuf_cache->preload_cache_size) ||
553 page > (pixbuf_cache->end_page + pixbuf_cache->preload_cache_size))
556 if (page < pixbuf_cache->start_page) {
557 page_offset = (page - (pixbuf_cache->start_page - pixbuf_cache->preload_cache_size));
559 g_assert (page_offset >= 0 &&
560 page_offset < pixbuf_cache->preload_cache_size);
561 return pixbuf_cache->prev_job + page_offset;
564 if (page > pixbuf_cache->end_page) {
565 page_offset = (page - (pixbuf_cache->end_page + 1));
567 g_assert (page_offset >= 0 &&
568 page_offset < pixbuf_cache->preload_cache_size);
569 return pixbuf_cache->next_job + page_offset;
572 page_offset = page - pixbuf_cache->start_page;
573 g_assert (page_offset >= 0 &&
574 page_offset <= PAGE_CACHE_LEN(pixbuf_cache));
575 return pixbuf_cache->job_list + page_offset;
579 ev_pixbuf_cache_clear_job_sizes (EvPixbufCache *pixbuf_cache,
582 EvPageCache *page_cache;
585 page_cache = ev_page_cache_get (pixbuf_cache->document);
587 for (i = 0; i < PAGE_CACHE_LEN (pixbuf_cache); i++) {
588 check_job_size_and_unref (pixbuf_cache, pixbuf_cache->job_list + i, page_cache, scale);
591 for (i = 0; i < pixbuf_cache->preload_cache_size; i++) {
592 check_job_size_and_unref (pixbuf_cache, pixbuf_cache->prev_job + i, page_cache, scale);
593 check_job_size_and_unref (pixbuf_cache, pixbuf_cache->next_job + i, page_cache, scale);
598 get_selection_colors (GtkWidget *widget, GdkColor **text, GdkColor **base)
600 if (GTK_WIDGET_HAS_FOCUS (widget)) {
601 *text = &widget->style->text [GTK_STATE_SELECTED];
602 *base = &widget->style->base [GTK_STATE_SELECTED];
604 *text = &widget->style->text [GTK_STATE_ACTIVE];
605 *base = &widget->style->base [GTK_STATE_ACTIVE];
610 add_job (EvPixbufCache *pixbuf_cache,
611 CacheJobInfo *job_info,
612 EvPageCache *page_cache,
619 EvJobPriority priority)
621 EvRenderFlags flags = 0;
623 job_info->page_ready = FALSE;
625 if (job_info->region)
626 gdk_region_destroy (job_info->region);
627 job_info->region = region ? gdk_region_copy (region) : NULL;
629 /* Figure out what else we need for this job */
630 if (job_info->link_mapping == NULL)
631 flags |= EV_RENDER_INCLUDE_LINKS;
632 if (job_info->image_mapping == NULL)
633 flags |= EV_RENDER_INCLUDE_IMAGES;
634 if (job_info->form_field_mapping == NULL)
635 flags |= EV_RENDER_INCLUDE_FORMS;
636 if (job_info->text_mapping == NULL)
637 flags |= EV_RENDER_INCLUDE_TEXT;
639 job_info->job = ev_job_render_new (pixbuf_cache->document,
640 page, rotation, scale,
644 if (new_selection_surface_needed (pixbuf_cache, job_info, page, scale)) {
645 GdkColor *text, *base;
647 gtk_widget_ensure_style (pixbuf_cache->view);
648 get_selection_colors (pixbuf_cache->view, &text, &base);
649 ev_job_render_set_selection_info (EV_JOB_RENDER (job_info->job),
650 &(job_info->target_points),
651 job_info->selection_style,
655 g_signal_connect (job_info->job, "page-ready",
656 G_CALLBACK (job_page_ready_cb),
658 g_signal_connect (job_info->job, "finished",
659 G_CALLBACK (job_finished_cb),
661 ev_job_scheduler_push_job (job_info->job, priority);
665 add_job_if_needed (EvPixbufCache *pixbuf_cache,
666 CacheJobInfo *job_info,
667 EvPageCache *page_cache,
671 EvJobPriority priority)
678 ev_page_cache_get_size (page_cache, page, rotation,
679 scale, &width, &height);
681 if (job_info->surface &&
682 cairo_image_surface_get_width (job_info->surface) == width &&
683 cairo_image_surface_get_height (job_info->surface) == height)
686 add_job (pixbuf_cache, job_info, page_cache, NULL,
687 width, height, page, rotation, scale,
692 ev_pixbuf_cache_add_jobs_if_needed (EvPixbufCache *pixbuf_cache,
696 EvPageCache *page_cache;
697 CacheJobInfo *job_info;
701 page_cache = ev_page_cache_get (pixbuf_cache->document);
703 for (i = 0; i < PAGE_CACHE_LEN (pixbuf_cache); i++) {
704 job_info = (pixbuf_cache->job_list + i);
705 page = pixbuf_cache->start_page + i;
707 add_job_if_needed (pixbuf_cache, job_info,
708 page_cache, page, rotation, scale,
709 EV_JOB_PRIORITY_URGENT);
712 for (i = FIRST_VISIBLE_PREV(pixbuf_cache); i < pixbuf_cache->preload_cache_size; i++) {
713 job_info = (pixbuf_cache->prev_job + i);
714 page = pixbuf_cache->start_page - pixbuf_cache->preload_cache_size + i;
716 add_job_if_needed (pixbuf_cache, job_info,
717 page_cache, page, rotation, scale,
718 EV_JOB_PRIORITY_LOW);
721 for (i = 0; i < VISIBLE_NEXT_LEN(pixbuf_cache, page_cache); i++) {
722 job_info = (pixbuf_cache->next_job + i);
723 page = pixbuf_cache->end_page + 1 + i;
725 add_job_if_needed (pixbuf_cache, job_info,
726 page_cache, page, rotation, scale,
727 EV_JOB_PRIORITY_LOW);
733 ev_pixbuf_cache_set_page_range (EvPixbufCache *pixbuf_cache,
738 GList *selection_list)
740 EvPageCache *page_cache;
742 g_return_if_fail (EV_IS_PIXBUF_CACHE (pixbuf_cache));
744 page_cache = ev_page_cache_get (pixbuf_cache->document);
746 g_return_if_fail (start_page >= 0 && start_page < ev_page_cache_get_n_pages (page_cache));
747 g_return_if_fail (end_page >= 0 && end_page < ev_page_cache_get_n_pages (page_cache));
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_get_surface (EvPixbufCache *pixbuf_cache,
770 CacheJobInfo *job_info;
772 job_info = find_job_cache (pixbuf_cache, page);
773 if (job_info == NULL)
776 if (job_info->page_ready)
777 return job_info->surface;
779 /* We don't need to wait for the idle to handle the callback */
781 EV_JOB_RENDER (job_info->job)->page_ready) {
782 copy_job_page_and_selection_to_job_info (EV_JOB_RENDER (job_info->job), job_info, pixbuf_cache);
783 g_signal_emit (pixbuf_cache, signals[JOB_FINISHED], 0, job_info->region);
786 return job_info->surface;
790 ev_pixbuf_cache_get_link_mapping (EvPixbufCache *pixbuf_cache,
793 CacheJobInfo *job_info;
795 job_info = find_job_cache (pixbuf_cache, page);
796 if (job_info == NULL)
799 /* We don't need to wait for the idle to handle the callback */
801 EV_JOB (job_info->job)->finished) {
802 copy_job_to_job_info (EV_JOB_RENDER (job_info->job), job_info, pixbuf_cache);
805 return job_info->link_mapping;
809 ev_pixbuf_cache_get_image_mapping (EvPixbufCache *pixbuf_cache,
812 CacheJobInfo *job_info;
814 if (!EV_IS_DOCUMENT_IMAGES (pixbuf_cache->document))
817 job_info = find_job_cache (pixbuf_cache, page);
818 if (job_info == NULL)
821 /* We don't need to wait for the idle to handle the callback */
823 EV_JOB (job_info->job)->finished) {
824 copy_job_to_job_info (EV_JOB_RENDER (job_info->job), job_info, pixbuf_cache);
827 return job_info->image_mapping;
831 ev_pixbuf_cache_get_form_field_mapping (EvPixbufCache *pixbuf_cache,
834 CacheJobInfo *job_info;
836 if (!EV_IS_DOCUMENT_FORMS (pixbuf_cache->document))
839 job_info = find_job_cache (pixbuf_cache, page);
840 if (job_info == NULL)
843 /* We don't need to wait for the idle to handle the callback */
845 EV_JOB (job_info->job)->finished) {
846 copy_job_to_job_info (EV_JOB_RENDER(job_info->job), job_info, pixbuf_cache);
849 return job_info->form_field_mapping;
853 new_selection_surface_needed (EvPixbufCache *pixbuf_cache,
854 CacheJobInfo *job_info,
858 EvPageCache *page_cache;
860 if (job_info->selection && job_info->rc) {
862 gint selection_width, selection_height;
864 page_cache = ev_page_cache_get (pixbuf_cache->document);
865 ev_page_cache_get_size (page_cache, page,
866 job_info->rc->rotation,
867 scale, &width, &height);
869 selection_width = cairo_image_surface_get_width (job_info->selection);
870 selection_height = cairo_image_surface_get_height (job_info->selection);
872 if (width != selection_width || height != selection_height)
875 if (job_info->points_set)
883 clear_selection_if_needed (EvPixbufCache *pixbuf_cache,
884 CacheJobInfo *job_info,
888 if (new_selection_surface_needed (pixbuf_cache, job_info, page, scale)) {
889 if (job_info->selection)
890 cairo_surface_destroy (job_info->selection);
891 job_info->selection = NULL;
892 job_info->selection_points.x1 = -1;
897 ev_pixbuf_cache_get_text_mapping (EvPixbufCache *pixbuf_cache,
900 CacheJobInfo *job_info;
902 job_info = find_job_cache (pixbuf_cache, page);
903 if (job_info == NULL)
906 /* We don't need to wait for the idle to handle the callback */
908 EV_JOB (job_info->job)->finished) {
909 copy_job_to_job_info (EV_JOB_RENDER (job_info->job), job_info, pixbuf_cache);
912 return job_info->text_mapping;
915 /* Clears the cache of jobs and pixbufs.
918 ev_pixbuf_cache_clear (EvPixbufCache *pixbuf_cache)
922 for (i = 0; i < pixbuf_cache->preload_cache_size; i++) {
923 dispose_cache_job_info (pixbuf_cache->prev_job + i, pixbuf_cache);
924 dispose_cache_job_info (pixbuf_cache->next_job + i, pixbuf_cache);
927 for (i = 0; i < PAGE_CACHE_LEN (pixbuf_cache); i++) {
928 dispose_cache_job_info (pixbuf_cache->job_list + i, pixbuf_cache);
934 ev_pixbuf_cache_style_changed (EvPixbufCache *pixbuf_cache)
938 /* FIXME: doesn't update running jobs. */
939 for (i = 0; i < pixbuf_cache->preload_cache_size; i++) {
940 CacheJobInfo *job_info;
942 job_info = pixbuf_cache->prev_job + i;
943 if (job_info->selection) {
944 cairo_surface_destroy (job_info->selection);
945 job_info->selection = NULL;
948 job_info = pixbuf_cache->next_job + i;
949 if (job_info->selection) {
950 cairo_surface_destroy (job_info->selection);
951 job_info->selection = NULL;
955 for (i = 0; i < PAGE_CACHE_LEN (pixbuf_cache); i++) {
956 CacheJobInfo *job_info;
958 job_info = pixbuf_cache->job_list + i;
959 if (job_info->selection) {
960 cairo_surface_destroy (job_info->selection);
961 job_info->selection = NULL;
967 ev_pixbuf_cache_get_selection_surface (EvPixbufCache *pixbuf_cache,
972 CacheJobInfo *job_info;
974 /* the document does not implement the selection interface */
975 if (!EV_IS_SELECTION (pixbuf_cache->document))
978 job_info = find_job_cache (pixbuf_cache, page);
979 if (job_info == NULL)
982 /* No selection on this page */
983 if (!job_info->points_set)
986 /* Create new render context if needed (selection + fast scrolling) */
987 if (job_info->rc == NULL) {
989 ev_page = ev_document_get_page (pixbuf_cache->document, page);
990 job_info->rc = ev_render_context_new (ev_page, 0, scale);
991 g_object_unref (ev_page);
995 ev_render_context_set_scale (job_info->rc, scale);
997 /* If we have a running job, we just return what we have under the
998 * assumption that it'll be updated later and we can scale it as need
1000 if (job_info->job && (EV_JOB_RENDER (job_info->job)->flags & EV_RENDER_INCLUDE_SELECTION))
1001 return job_info->selection;
1003 /* Now, lets see if we need to resize the image. If we do, we clear the
1005 clear_selection_if_needed (pixbuf_cache, job_info, page, scale);
1007 /* Finally, we see if the two scales are the same, and get a new pixbuf
1008 * if needed. We do this synchronously for now. At some point, we
1009 * _should_ be able to get rid of the doc_mutex, so the synchronicity
1010 * doesn't kill us. Rendering a few glyphs should really be fast.
1012 if (ev_rect_cmp (&(job_info->target_points), &(job_info->selection_points))) {
1013 EvRectangle *old_points;
1014 GdkColor *text, *base;
1016 /* we need to get a new selection pixbuf */
1017 ev_document_doc_mutex_lock ();
1018 if (job_info->selection_points.x1 < 0) {
1019 g_assert (job_info->selection == NULL);
1022 g_assert (job_info->selection != NULL);
1023 old_points = &(job_info->selection_points);
1026 if (job_info->selection_region)
1027 gdk_region_destroy (job_info->selection_region);
1028 job_info->selection_region =
1029 ev_selection_get_selection_region (EV_SELECTION (pixbuf_cache->document),
1031 job_info->selection_style,
1032 &(job_info->target_points));
1034 gtk_widget_ensure_style (pixbuf_cache->view);
1036 get_selection_colors (pixbuf_cache->view, &text, &base);
1038 ev_selection_render_selection (EV_SELECTION (pixbuf_cache->document),
1039 job_info->rc, &(job_info->selection),
1040 &(job_info->target_points),
1042 job_info->selection_style,
1044 job_info->selection_points = job_info->target_points;
1045 ev_document_doc_mutex_unlock ();
1048 *region = job_info->selection_region;
1049 return job_info->selection;
1053 update_job_selection (CacheJobInfo *job_info,
1054 EvViewSelection *selection)
1056 job_info->points_set = TRUE;
1057 job_info->target_points = selection->rect;
1058 job_info->selection_style = selection->style;
1062 clear_job_selection (CacheJobInfo *job_info)
1064 job_info->points_set = FALSE;
1065 job_info->selection_points.x1 = -1;
1067 if (job_info->selection) {
1068 cairo_surface_destroy (job_info->selection);
1069 job_info->selection = NULL;
1073 /* This function will reset the selection on pages that no longer have them, and
1074 * will update the target_selection on those that need it. It will _not_ free
1075 * the previous selection_list -- that's up to caller to do.
1078 ev_pixbuf_cache_set_selection_list (EvPixbufCache *pixbuf_cache,
1079 GList *selection_list)
1081 EvPageCache *page_cache;
1082 EvViewSelection *selection;
1083 GList *list = selection_list;
1087 g_return_if_fail (EV_IS_PIXBUF_CACHE (pixbuf_cache));
1089 if (!EV_IS_SELECTION (pixbuf_cache->document))
1092 page_cache = ev_page_cache_get (pixbuf_cache->document);
1094 /* We check each area to see what needs updating, and what needs freeing; */
1095 page = pixbuf_cache->start_page - pixbuf_cache->preload_cache_size;
1096 for (i = 0; i < pixbuf_cache->preload_cache_size; i++) {
1104 if (((EvViewSelection *)list->data)->page == page) {
1105 selection = list->data;
1107 } else if (((EvViewSelection *)list->data)->page > page)
1113 update_job_selection (pixbuf_cache->prev_job + i, selection);
1115 clear_job_selection (pixbuf_cache->prev_job + i);
1119 page = pixbuf_cache->start_page;
1120 for (i = 0; i < PAGE_CACHE_LEN (pixbuf_cache); i++) {
1123 if (((EvViewSelection *)list->data)->page == page) {
1124 selection = list->data;
1126 } else if (((EvViewSelection *)list->data)->page > page)
1132 update_job_selection (pixbuf_cache->job_list + i, selection);
1134 clear_job_selection (pixbuf_cache->job_list + i);
1138 for (i = 0; i < pixbuf_cache->preload_cache_size; i++) {
1139 if (page >= ev_page_cache_get_n_pages (page_cache))
1144 if (((EvViewSelection *)list->data)->page == page) {
1145 selection = list->data;
1147 } else if (((EvViewSelection *)list->data)->page > page)
1153 update_job_selection (pixbuf_cache->next_job + i, selection);
1155 clear_job_selection (pixbuf_cache->next_job + i);
1161 /* Returns what the pixbuf cache thinks is */
1164 ev_pixbuf_cache_get_selection_list (EvPixbufCache *pixbuf_cache)
1166 EvPageCache *page_cache;
1167 EvViewSelection *selection;
1168 GList *retval = NULL;
1172 g_return_val_if_fail (EV_IS_PIXBUF_CACHE (pixbuf_cache), NULL);
1174 page_cache = ev_page_cache_get (pixbuf_cache->document);
1176 /* We check each area to see what needs updating, and what needs freeing; */
1177 page = pixbuf_cache->start_page - pixbuf_cache->preload_cache_size;
1178 for (i = 0; i < pixbuf_cache->preload_cache_size; i++) {
1184 if (pixbuf_cache->prev_job[i].selection_points.x1 != -1) {
1185 selection = g_new0 (EvViewSelection, 1);
1186 selection->page = page;
1187 selection->rect = pixbuf_cache->prev_job[i].selection_points;
1188 if (pixbuf_cache->prev_job[i].selection_region)
1189 selection->covered_region = gdk_region_copy (pixbuf_cache->prev_job[i].selection_region);
1190 retval = g_list_append (retval, selection);
1196 page = pixbuf_cache->start_page;
1197 for (i = 0; i < PAGE_CACHE_LEN (pixbuf_cache); i++) {
1198 if (pixbuf_cache->job_list[i].selection_points.x1 != -1) {
1199 selection = g_new0 (EvViewSelection, 1);
1200 selection->page = page;
1201 selection->rect = pixbuf_cache->job_list[i].selection_points;
1202 if (pixbuf_cache->job_list[i].selection_region)
1203 selection->covered_region = gdk_region_copy (pixbuf_cache->job_list[i].selection_region);
1204 retval = g_list_append (retval, selection);
1210 for (i = 0; i < pixbuf_cache->preload_cache_size; i++) {
1211 if (page >= ev_page_cache_get_n_pages (page_cache))
1214 if (pixbuf_cache->next_job[i].selection_points.x1 != -1) {
1215 selection = g_new0 (EvViewSelection, 1);
1216 selection->page = page;
1217 selection->rect = pixbuf_cache->next_job[i].selection_points;
1218 if (pixbuf_cache->next_job[i].selection_region)
1219 selection->covered_region = gdk_region_copy (pixbuf_cache->next_job[i].selection_region);
1220 retval = g_list_append (retval, selection);
1230 ev_pixbuf_cache_reload_page (EvPixbufCache *pixbuf_cache,
1236 CacheJobInfo *job_info;
1237 EvPageCache *page_cache;
1240 job_info = find_job_cache (pixbuf_cache, page);
1241 if (job_info == NULL)
1244 page_cache = ev_page_cache_get (pixbuf_cache->document);
1245 ev_page_cache_get_size (page_cache, page, rotation, scale,
1248 add_job (pixbuf_cache, job_info, page_cache, region,
1249 width, height, page, rotation, scale,
1250 EV_JOB_PRIORITY_URGENT);