2 #include "ev-pixbuf-cache.h"
3 #include "ev-job-queue.h"
4 #include "ev-page-cache.h"
5 #include "ev-document-images.h"
6 #include "ev-document-forms.h"
8 #include "ev-form-field.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 GdkRegion *text_mapping;
27 * Selection_points are the coordinates encapsulated in selection.
28 * target_points is the target selection size. */
29 EvRectangle selection_points;
30 EvRectangle target_points;
31 EvSelectionStyle selection_style;
34 cairo_surface_t *selection;
35 GdkRegion *selection_region;
42 /* We keep a link to our containing view just for style information. */
48 /* preload_cache_size is the number of pages prior to the current
49 * visible area that we cache. It's normally 1, but could be 2 in the
52 int preload_cache_size;
53 CacheJobInfo *prev_job;
54 CacheJobInfo *job_list;
55 CacheJobInfo *next_job;
58 struct _EvPixbufCacheClass
60 GObjectClass parent_class;
62 void (* job_finished) (EvPixbufCache *pixbuf_cache);
72 static guint signals[N_SIGNALS] = {0, };
74 static void ev_pixbuf_cache_init (EvPixbufCache *pixbuf_cache);
75 static void ev_pixbuf_cache_class_init (EvPixbufCacheClass *pixbuf_cache);
76 static void ev_pixbuf_cache_finalize (GObject *object);
77 static void ev_pixbuf_cache_dispose (GObject *object);
78 static void job_page_ready_cb (EvJob *job,
79 EvPixbufCache *pixbuf_cache);
80 static void job_finished_cb (EvJob *job,
81 EvPixbufCache *pixbuf_cache);
82 static CacheJobInfo *find_job_cache (EvPixbufCache *pixbuf_cache,
84 static void copy_job_to_job_info (EvJobRender *job_render,
85 CacheJobInfo *job_info,
86 EvPixbufCache *pixbuf_cache);
87 static void copy_job_page_and_selection_to_job_info (EvJobRender *job_render,
88 CacheJobInfo *job_info,
89 EvPixbufCache *pixbuf_cache);
90 static gboolean new_selection_surface_needed(EvPixbufCache *pixbuf_cache,
91 CacheJobInfo *job_info,
96 /* These are used for iterating through the prev and next arrays */
97 #define FIRST_VISABLE_PREV(pixbuf_cache) \
98 (MAX (0, pixbuf_cache->preload_cache_size + 1 - pixbuf_cache->start_page))
99 #define VISIBLE_NEXT_LEN(pixbuf_cache, page_cache) \
100 (MIN(pixbuf_cache->preload_cache_size, ev_page_cache_get_n_pages (page_cache) - (1 + pixbuf_cache->end_page)))
101 #define PAGE_CACHE_LEN(pixbuf_cache) \
102 ((pixbuf_cache->end_page - pixbuf_cache->start_page) + 1)
104 G_DEFINE_TYPE (EvPixbufCache, ev_pixbuf_cache, G_TYPE_OBJECT)
107 ev_pixbuf_cache_init (EvPixbufCache *pixbuf_cache)
109 pixbuf_cache->start_page = 0;
110 pixbuf_cache->end_page = 0;
111 pixbuf_cache->job_list = g_new0 (CacheJobInfo, PAGE_CACHE_LEN (pixbuf_cache));
113 pixbuf_cache->preload_cache_size = 2;
114 pixbuf_cache->prev_job = g_new0 (CacheJobInfo, pixbuf_cache->preload_cache_size);
115 pixbuf_cache->next_job = g_new0 (CacheJobInfo, pixbuf_cache->preload_cache_size);
119 ev_pixbuf_cache_class_init (EvPixbufCacheClass *class)
121 GObjectClass *object_class;
123 object_class = G_OBJECT_CLASS (class);
125 object_class->finalize = ev_pixbuf_cache_finalize;
126 object_class->dispose = ev_pixbuf_cache_dispose;
128 signals[JOB_FINISHED] =
129 g_signal_new ("job-finished",
130 G_OBJECT_CLASS_TYPE (object_class),
131 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
132 G_STRUCT_OFFSET (EvPixbufCacheClass, job_finished),
134 g_cclosure_marshal_VOID__POINTER,
140 ev_pixbuf_cache_finalize (GObject *object)
142 EvPixbufCache *pixbuf_cache;
144 pixbuf_cache = EV_PIXBUF_CACHE (object);
146 g_free (pixbuf_cache->prev_job);
147 g_free (pixbuf_cache->job_list);
148 g_free (pixbuf_cache->next_job);
150 G_OBJECT_CLASS (ev_pixbuf_cache_parent_class)->finalize (object);
154 dispose_cache_job_info (CacheJobInfo *job_info,
157 if (job_info == NULL)
160 g_signal_handlers_disconnect_by_func (job_info->job,
161 G_CALLBACK (job_page_ready_cb),
163 g_signal_handlers_disconnect_by_func (job_info->job,
164 G_CALLBACK (job_finished_cb),
166 ev_job_queue_remove_job (job_info->job);
167 g_object_unref (G_OBJECT (job_info->job));
168 job_info->job = NULL;
170 if (job_info->surface) {
171 cairo_surface_destroy (job_info->surface);
172 job_info->surface = NULL;
174 if (job_info->region) {
175 gdk_region_destroy (job_info->region);
176 job_info->region = NULL;
178 if (job_info->link_mapping) {
179 ev_link_mapping_free (job_info->link_mapping);
180 job_info->link_mapping = NULL;
182 if (job_info->image_mapping) {
183 ev_image_mapping_free (job_info->image_mapping);
184 job_info->image_mapping = NULL;
186 if (job_info->form_field_mapping) {
187 ev_form_field_mapping_free (job_info->form_field_mapping);
188 job_info->form_field_mapping = NULL;
190 if (job_info->text_mapping) {
191 gdk_region_destroy (job_info->text_mapping);
192 job_info->text_mapping = NULL;
194 if (job_info->selection) {
195 cairo_surface_destroy (job_info->selection);
196 job_info->selection = NULL;
198 if (job_info->selection_region) {
199 gdk_region_destroy (job_info->selection_region);
200 job_info->selection_region = NULL;
203 g_object_unref (G_OBJECT (job_info->rc));
207 job_info->points_set = FALSE;
211 ev_pixbuf_cache_dispose (GObject *object)
213 EvPixbufCache *pixbuf_cache;
216 pixbuf_cache = EV_PIXBUF_CACHE (object);
218 for (i = 0; i < pixbuf_cache->preload_cache_size; i++) {
219 dispose_cache_job_info (pixbuf_cache->prev_job + i, pixbuf_cache);
220 dispose_cache_job_info (pixbuf_cache->next_job + i, pixbuf_cache);
223 for (i = 0; i < PAGE_CACHE_LEN (pixbuf_cache); i++) {
224 dispose_cache_job_info (pixbuf_cache->job_list + i, pixbuf_cache);
227 G_OBJECT_CLASS (ev_pixbuf_cache_parent_class)->dispose (object);
232 ev_pixbuf_cache_new (GtkWidget *view,
233 EvDocument *document)
235 EvPixbufCache *pixbuf_cache;
237 pixbuf_cache = (EvPixbufCache *) g_object_new (EV_TYPE_PIXBUF_CACHE, NULL);
238 /* This is a backlink, so we don't ref this */
239 pixbuf_cache->view = view;
240 pixbuf_cache->document = document;
246 job_page_ready_cb (EvJob *job,
247 EvPixbufCache *pixbuf_cache)
249 CacheJobInfo *job_info;
250 EvJobRender *job_render = EV_JOB_RENDER (job);
252 /* If the job is outside of our interest, we silently discard it */
253 if ((job_render->page < (pixbuf_cache->start_page - pixbuf_cache->preload_cache_size)) ||
254 (job_render->page > (pixbuf_cache->end_page + pixbuf_cache->preload_cache_size))) {
255 g_object_unref (job);
259 job_info = find_job_cache (pixbuf_cache, job_render->page);
261 copy_job_page_and_selection_to_job_info (job_render, job_info, pixbuf_cache);
262 g_signal_emit (pixbuf_cache, signals[JOB_FINISHED], 0, job_info->region);
266 job_finished_cb (EvJob *job,
267 EvPixbufCache *pixbuf_cache)
269 CacheJobInfo *job_info;
270 EvJobRender *job_render = EV_JOB_RENDER (job);
272 /* If the job is outside of our interest, we silently discard it */
273 if ((job_render->page < (pixbuf_cache->start_page - pixbuf_cache->preload_cache_size)) ||
274 (job_render->page > (pixbuf_cache->end_page + pixbuf_cache->preload_cache_size))) {
275 g_object_unref (job);
279 job_info = find_job_cache (pixbuf_cache, job_render->page);
280 copy_job_to_job_info (job_render, job_info, pixbuf_cache);
283 /* This checks a job to see if the job would generate the right sized pixbuf
284 * given a scale. If it won't, it removes the job and clears it to NULL.
287 check_job_size_and_unref (EvPixbufCache *pixbuf_cache,
288 CacheJobInfo *job_info,
289 EvPageCache *page_cache,
297 if (job_info->job == NULL)
300 ev_page_cache_get_size (page_cache,
301 EV_JOB_RENDER (job_info->job)->page,
302 EV_JOB_RENDER (job_info->job)->rotation,
306 if (width == EV_JOB_RENDER (job_info->job)->target_width &&
307 height == EV_JOB_RENDER (job_info->job)->target_height)
310 g_signal_handlers_disconnect_by_func (job_info->job,
311 G_CALLBACK (job_page_ready_cb),
313 g_signal_handlers_disconnect_by_func (job_info->job,
314 G_CALLBACK (job_finished_cb),
316 ev_job_queue_remove_job (job_info->job);
317 g_object_unref (job_info->job);
318 job_info->job = NULL;
321 /* Do all function that copies a job from an older cache to it's position in the
322 * new cache. It clears the old job if it doesn't have a place.
325 move_one_job (CacheJobInfo *job_info,
326 EvPixbufCache *pixbuf_cache,
328 CacheJobInfo *new_job_list,
329 CacheJobInfo *new_prev_job,
330 CacheJobInfo *new_next_job,
333 EvJobPriority priority)
335 CacheJobInfo *target_page = NULL;
337 EvJobPriority new_priority;
339 if (page < (start_page - pixbuf_cache->preload_cache_size) ||
340 page > (end_page + pixbuf_cache->preload_cache_size)) {
341 dispose_cache_job_info (job_info, pixbuf_cache);
345 /* find the target page to copy it over to. */
346 if (page < start_page) {
347 page_offset = (page - (start_page - pixbuf_cache->preload_cache_size));
349 g_assert (page_offset >= 0 &&
350 page_offset < pixbuf_cache->preload_cache_size);
351 target_page = new_prev_job + page_offset;
352 new_priority = EV_JOB_PRIORITY_LOW;
353 } else if (page > end_page) {
354 page_offset = (page - (end_page + 1));
356 g_assert (page_offset >= 0 &&
357 page_offset < pixbuf_cache->preload_cache_size);
358 target_page = new_next_job + page_offset;
359 new_priority = EV_JOB_PRIORITY_LOW;
361 page_offset = page - start_page;
362 g_assert (page_offset >= 0 &&
363 page_offset <= ((end_page - start_page) + 1));
364 new_priority = EV_JOB_PRIORITY_HIGH;
365 target_page = new_job_list + page_offset;
368 *target_page = *job_info;
369 job_info->job = NULL;
370 job_info->region = NULL;
371 job_info->surface = NULL;
372 job_info->link_mapping = NULL;
373 job_info->image_mapping = NULL;
374 job_info->form_field_mapping = NULL;
376 if (new_priority != priority && target_page->job) {
377 ev_job_queue_update_job (target_page->job, new_priority);
382 ev_pixbuf_cache_update_range (EvPixbufCache *pixbuf_cache,
386 CacheJobInfo *new_job_list;
387 CacheJobInfo *new_prev_job;
388 CacheJobInfo *new_next_job;
389 EvPageCache *page_cache;
392 if (pixbuf_cache->start_page == start_page &&
393 pixbuf_cache->end_page == end_page)
396 page_cache = ev_page_cache_get (pixbuf_cache->document);
398 new_job_list = g_new0 (CacheJobInfo, (end_page - start_page) + 1);
399 new_prev_job = g_new0 (CacheJobInfo, pixbuf_cache->preload_cache_size);
400 new_next_job = g_new0 (CacheJobInfo, pixbuf_cache->preload_cache_size);
402 /* We go through each job in the old cache and either clear it or move
403 * it to a new location. */
405 /* Start with the prev cache. */
406 page = pixbuf_cache->start_page - pixbuf_cache->preload_cache_size;
407 for (i = 0; i < pixbuf_cache->preload_cache_size; i++) {
409 dispose_cache_job_info (pixbuf_cache->prev_job + i, pixbuf_cache);
411 move_one_job (pixbuf_cache->prev_job + i,
413 new_job_list, new_prev_job, new_next_job,
414 start_page, end_page, EV_JOB_PRIORITY_LOW);
419 page = pixbuf_cache->start_page;
420 for (i = 0; i < PAGE_CACHE_LEN (pixbuf_cache); i++) {
421 move_one_job (pixbuf_cache->job_list + i,
423 new_job_list, new_prev_job, new_next_job,
424 start_page, end_page, EV_JOB_PRIORITY_HIGH);
428 for (i = 0; i < pixbuf_cache->preload_cache_size; i++) {
429 if (page >= ev_page_cache_get_n_pages (page_cache)) {
430 dispose_cache_job_info (pixbuf_cache->next_job + i, pixbuf_cache);
432 move_one_job (pixbuf_cache->next_job + i,
434 new_job_list, new_prev_job, new_next_job,
435 start_page, end_page, EV_JOB_PRIORITY_LOW);
440 g_free (pixbuf_cache->job_list);
441 g_free (pixbuf_cache->prev_job);
442 g_free (pixbuf_cache->next_job);
444 pixbuf_cache->job_list = new_job_list;
445 pixbuf_cache->prev_job = new_prev_job;
446 pixbuf_cache->next_job = new_next_job;
448 pixbuf_cache->start_page = start_page;
449 pixbuf_cache->end_page = end_page;
453 copy_job_page_and_selection_to_job_info (EvJobRender *job_render,
454 CacheJobInfo *job_info,
455 EvPixbufCache *pixbuf_cache)
457 if (job_info->rc == NULL) {
458 job_info->rc = ev_render_context_new (job_render->ev_page,
459 job_render->rotation,
462 ev_render_context_set_page (job_info->rc, job_render->ev_page);
463 ev_render_context_set_rotation (job_info->rc, job_render->rotation);
464 ev_render_context_set_scale (job_info->rc, job_render->scale);
467 if (job_info->surface) {
468 cairo_surface_destroy (job_info->surface);
470 job_info->surface = cairo_surface_reference (job_render->surface);
472 job_info->points_set = FALSE;
473 if (job_render->flags & EV_RENDER_INCLUDE_SELECTION) {
474 if (job_info->selection) {
475 cairo_surface_destroy (job_info->selection);
476 job_info->selection = NULL;
478 if (job_info->selection_region) {
479 gdk_region_destroy (job_info->selection_region);
480 job_info->selection_region = NULL;
483 job_info->selection_points = job_render->selection_points;
484 job_info->selection_region = gdk_region_copy (job_render->selection_region);
485 job_info->selection = cairo_surface_reference (job_render->selection);
486 g_assert (job_info->selection_points.x1 >= 0);
487 job_info->points_set = TRUE;
491 g_signal_handlers_disconnect_by_func (job_info->job,
492 G_CALLBACK (job_page_ready_cb),
496 job_info->page_ready = TRUE;
500 copy_job_to_job_info (EvJobRender *job_render,
501 CacheJobInfo *job_info,
502 EvPixbufCache *pixbuf_cache)
504 if (job_render->flags & EV_RENDER_INCLUDE_LINKS) {
505 if (job_info->link_mapping)
506 ev_link_mapping_free (job_info->link_mapping);
507 job_info->link_mapping = job_render->link_mapping;
510 if (job_render->flags & EV_RENDER_INCLUDE_IMAGES) {
511 if (job_info->image_mapping)
512 ev_image_mapping_free (job_info->image_mapping);
513 job_info->image_mapping = job_render->image_mapping;
516 if (job_render->flags & EV_RENDER_INCLUDE_FORMS) {
517 if (job_info->form_field_mapping)
518 ev_form_field_mapping_free (job_info->form_field_mapping);
519 job_info->form_field_mapping = job_render->form_field_mapping;
522 if (job_render->flags & EV_RENDER_INCLUDE_TEXT) {
523 if (job_info->text_mapping)
524 gdk_region_destroy (job_info->text_mapping);
525 job_info->text_mapping = job_render->text_mapping;
529 g_signal_handlers_disconnect_by_func (job_info->job,
530 G_CALLBACK (job_finished_cb),
532 ev_job_queue_remove_job (job_info->job);
533 g_object_unref (G_OBJECT (job_info->job));
534 job_info->job = NULL;
538 static CacheJobInfo *
539 find_job_cache (EvPixbufCache *pixbuf_cache,
544 if (page < (pixbuf_cache->start_page - pixbuf_cache->preload_cache_size) ||
545 page > (pixbuf_cache->end_page + pixbuf_cache->preload_cache_size))
548 if (page < pixbuf_cache->start_page) {
549 page_offset = (page - (pixbuf_cache->start_page - pixbuf_cache->preload_cache_size));
551 g_assert (page_offset >= 0 &&
552 page_offset < pixbuf_cache->preload_cache_size);
553 return pixbuf_cache->prev_job + page_offset;
556 if (page > pixbuf_cache->end_page) {
557 page_offset = (page - (pixbuf_cache->end_page + 1));
559 g_assert (page_offset >= 0 &&
560 page_offset < pixbuf_cache->preload_cache_size);
561 return pixbuf_cache->next_job + page_offset;
564 page_offset = page - pixbuf_cache->start_page;
565 g_assert (page_offset >= 0 &&
566 page_offset <= PAGE_CACHE_LEN(pixbuf_cache));
567 return pixbuf_cache->job_list + page_offset;
571 ev_pixbuf_cache_clear_job_sizes (EvPixbufCache *pixbuf_cache,
574 EvPageCache *page_cache;
577 page_cache = ev_page_cache_get (pixbuf_cache->document);
579 for (i = 0; i < PAGE_CACHE_LEN (pixbuf_cache); i++) {
580 check_job_size_and_unref (pixbuf_cache, pixbuf_cache->job_list + i, page_cache, scale);
583 for (i = 0; i < pixbuf_cache->preload_cache_size; i++) {
584 check_job_size_and_unref (pixbuf_cache, pixbuf_cache->prev_job + i, page_cache, scale);
585 check_job_size_and_unref (pixbuf_cache, pixbuf_cache->next_job + i, page_cache, scale);
589 #define FIRST_VISABLE_PREV(pixbuf_cache) \
590 (MAX (0, pixbuf_cache->preload_cache_size + 1 - pixbuf_cache->start_page))
593 get_selection_colors (GtkWidget *widget, GdkColor **text, GdkColor **base)
595 if (GTK_WIDGET_HAS_FOCUS (widget)) {
596 *text = &widget->style->text [GTK_STATE_SELECTED];
597 *base = &widget->style->base [GTK_STATE_SELECTED];
599 *text = &widget->style->text [GTK_STATE_ACTIVE];
600 *base = &widget->style->base [GTK_STATE_ACTIVE];
605 add_job (EvPixbufCache *pixbuf_cache,
606 CacheJobInfo *job_info,
607 EvPageCache *page_cache,
614 EvJobPriority priority)
616 EvRenderFlags flags = 0;
618 job_info->page_ready = FALSE;
620 if (job_info->region)
621 gdk_region_destroy (job_info->region);
622 job_info->region = region ? gdk_region_copy (region) : NULL;
624 /* Figure out what else we need for this job */
625 if (job_info->link_mapping == NULL)
626 flags |= EV_RENDER_INCLUDE_LINKS;
627 if (job_info->image_mapping == NULL)
628 flags |= EV_RENDER_INCLUDE_IMAGES;
629 if (job_info->form_field_mapping == NULL)
630 flags |= EV_RENDER_INCLUDE_FORMS;
631 if (job_info->text_mapping == NULL)
632 flags |= EV_RENDER_INCLUDE_TEXT;
634 job_info->job = ev_job_render_new (pixbuf_cache->document,
635 page, rotation, scale,
639 if (new_selection_surface_needed (pixbuf_cache, job_info, page, scale)) {
640 GdkColor *text, *base;
642 gtk_widget_ensure_style (pixbuf_cache->view);
643 get_selection_colors (pixbuf_cache->view, &text, &base);
644 ev_job_render_set_selection_info (EV_JOB_RENDER (job_info->job),
645 &(job_info->target_points),
646 job_info->selection_style,
650 ev_job_queue_add_job (job_info->job, priority);
651 g_signal_connect (G_OBJECT (job_info->job), "page-ready",
652 G_CALLBACK (job_page_ready_cb),
654 g_signal_connect (G_OBJECT (job_info->job), "finished",
655 G_CALLBACK (job_finished_cb),
660 add_job_if_needed (EvPixbufCache *pixbuf_cache,
661 CacheJobInfo *job_info,
662 EvPageCache *page_cache,
666 EvJobPriority priority)
673 ev_page_cache_get_size (page_cache, page, rotation,
674 scale, &width, &height);
676 if (job_info->surface &&
677 cairo_image_surface_get_width (job_info->surface) == width &&
678 cairo_image_surface_get_height (job_info->surface) == height)
681 add_job (pixbuf_cache, job_info, page_cache, NULL,
682 width, height, page, rotation, scale,
687 ev_pixbuf_cache_add_jobs_if_needed (EvPixbufCache *pixbuf_cache,
691 EvPageCache *page_cache;
692 CacheJobInfo *job_info;
696 page_cache = ev_page_cache_get (pixbuf_cache->document);
698 for (i = 0; i < PAGE_CACHE_LEN (pixbuf_cache); i++) {
699 job_info = (pixbuf_cache->job_list + i);
700 page = pixbuf_cache->start_page + i;
702 add_job_if_needed (pixbuf_cache, job_info,
703 page_cache, page, rotation, scale,
704 EV_JOB_PRIORITY_HIGH);
707 for (i = FIRST_VISABLE_PREV(pixbuf_cache); i < pixbuf_cache->preload_cache_size; i++) {
708 job_info = (pixbuf_cache->prev_job + i);
709 page = pixbuf_cache->start_page - pixbuf_cache->preload_cache_size + i;
711 add_job_if_needed (pixbuf_cache, job_info,
712 page_cache, page, rotation, scale,
713 EV_JOB_PRIORITY_LOW);
716 for (i = 0; i < VISIBLE_NEXT_LEN(pixbuf_cache, page_cache); i++) {
717 job_info = (pixbuf_cache->next_job + i);
718 page = pixbuf_cache->end_page + 1 + i;
720 add_job_if_needed (pixbuf_cache, job_info,
721 page_cache, page, rotation, scale,
722 EV_JOB_PRIORITY_LOW);
728 ev_pixbuf_cache_set_page_range (EvPixbufCache *pixbuf_cache,
733 GList *selection_list)
735 EvPageCache *page_cache;
737 g_return_if_fail (EV_IS_PIXBUF_CACHE (pixbuf_cache));
739 page_cache = ev_page_cache_get (pixbuf_cache->document);
741 g_return_if_fail (start_page >= 0 && start_page < ev_page_cache_get_n_pages (page_cache));
742 g_return_if_fail (end_page >= 0 && end_page < ev_page_cache_get_n_pages (page_cache));
743 g_return_if_fail (end_page >= start_page);
745 /* First, resize the page_range as needed. We cull old pages
747 ev_pixbuf_cache_update_range (pixbuf_cache, start_page, end_page);
749 /* Then, we update the current jobs to see if any of them are the wrong
750 * size, we remove them if we need to. */
751 ev_pixbuf_cache_clear_job_sizes (pixbuf_cache, scale);
753 /* Next, we update the target selection for our pages */
754 ev_pixbuf_cache_set_selection_list (pixbuf_cache, selection_list);
756 /* Finally, we add the new jobs for all the sizes that don't have a
758 ev_pixbuf_cache_add_jobs_if_needed (pixbuf_cache, rotation, scale);
762 ev_pixbuf_cache_get_surface (EvPixbufCache *pixbuf_cache,
765 CacheJobInfo *job_info;
767 job_info = find_job_cache (pixbuf_cache, page);
768 if (job_info == NULL)
771 if (job_info->page_ready)
772 return job_info->surface;
774 /* We don't need to wait for the idle to handle the callback */
776 EV_JOB_RENDER (job_info->job)->page_ready) {
777 copy_job_page_and_selection_to_job_info (EV_JOB_RENDER (job_info->job), job_info, pixbuf_cache);
778 g_signal_emit (pixbuf_cache, signals[JOB_FINISHED], 0, job_info->region);
781 return job_info->surface;
785 ev_pixbuf_cache_get_link_mapping (EvPixbufCache *pixbuf_cache,
788 CacheJobInfo *job_info;
790 job_info = find_job_cache (pixbuf_cache, page);
791 if (job_info == NULL)
794 /* We don't need to wait for the idle to handle the callback */
796 EV_JOB (job_info->job)->finished) {
797 copy_job_to_job_info (EV_JOB_RENDER (job_info->job), job_info, pixbuf_cache);
800 return job_info->link_mapping;
804 ev_pixbuf_cache_get_image_mapping (EvPixbufCache *pixbuf_cache,
807 CacheJobInfo *job_info;
809 if (!EV_IS_DOCUMENT_IMAGES (pixbuf_cache->document))
812 job_info = find_job_cache (pixbuf_cache, page);
813 if (job_info == NULL)
816 /* We don't need to wait for the idle to handle the callback */
818 EV_JOB (job_info->job)->finished) {
819 copy_job_to_job_info (EV_JOB_RENDER (job_info->job), job_info, pixbuf_cache);
822 return job_info->image_mapping;
826 ev_pixbuf_cache_get_form_field_mapping (EvPixbufCache *pixbuf_cache,
829 CacheJobInfo *job_info;
831 if (!EV_IS_DOCUMENT_FORMS (pixbuf_cache->document))
834 job_info = find_job_cache (pixbuf_cache, page);
835 if (job_info == NULL)
838 /* We don't need to wait for the idle to handle the callback */
840 EV_JOB (job_info->job)->finished) {
841 copy_job_to_job_info (EV_JOB_RENDER(job_info->job), job_info, pixbuf_cache);
844 return job_info->form_field_mapping;
848 new_selection_surface_needed (EvPixbufCache *pixbuf_cache,
849 CacheJobInfo *job_info,
853 EvPageCache *page_cache;
855 if (job_info->selection) {
857 gint selection_width, selection_height;
859 page_cache = ev_page_cache_get (pixbuf_cache->document);
860 ev_page_cache_get_size (page_cache, page,
861 job_info->rc->rotation,
862 scale, &width, &height);
864 selection_width = cairo_image_surface_get_width (job_info->selection);
865 selection_height = cairo_image_surface_get_height (job_info->selection);
867 if (width != selection_width || height != selection_height)
870 if (job_info->points_set)
878 clear_selection_if_needed (EvPixbufCache *pixbuf_cache,
879 CacheJobInfo *job_info,
883 if (new_selection_surface_needed (pixbuf_cache, job_info, page, scale)) {
884 if (job_info->selection)
885 cairo_surface_destroy (job_info->selection);
886 job_info->selection = NULL;
887 job_info->selection_points.x1 = -1;
892 ev_pixbuf_cache_get_text_mapping (EvPixbufCache *pixbuf_cache,
895 CacheJobInfo *job_info;
897 job_info = find_job_cache (pixbuf_cache, page);
898 if (job_info == NULL)
901 /* We don't need to wait for the idle to handle the callback */
903 EV_JOB (job_info->job)->finished) {
904 copy_job_to_job_info (EV_JOB_RENDER (job_info->job), job_info, pixbuf_cache);
907 return job_info->text_mapping;
910 /* Clears the cache of jobs and pixbufs.
913 ev_pixbuf_cache_clear (EvPixbufCache *pixbuf_cache)
917 for (i = 0; i < pixbuf_cache->preload_cache_size; i++) {
918 dispose_cache_job_info (pixbuf_cache->prev_job + i, pixbuf_cache);
919 dispose_cache_job_info (pixbuf_cache->next_job + i, pixbuf_cache);
922 for (i = 0; i < PAGE_CACHE_LEN (pixbuf_cache); i++) {
923 dispose_cache_job_info (pixbuf_cache->job_list + i, pixbuf_cache);
929 ev_pixbuf_cache_style_changed (EvPixbufCache *pixbuf_cache)
933 /* FIXME: doesn't update running jobs. */
934 for (i = 0; i < pixbuf_cache->preload_cache_size; i++) {
935 CacheJobInfo *job_info;
937 job_info = pixbuf_cache->prev_job + i;
938 if (job_info->selection) {
939 cairo_surface_destroy (job_info->selection);
940 job_info->selection = NULL;
943 job_info = pixbuf_cache->next_job + i;
944 if (job_info->selection) {
945 cairo_surface_destroy (job_info->selection);
946 job_info->selection = NULL;
950 for (i = 0; i < PAGE_CACHE_LEN (pixbuf_cache); i++) {
951 CacheJobInfo *job_info;
953 job_info = pixbuf_cache->job_list + i;
954 if (job_info->selection) {
955 cairo_surface_destroy (job_info->selection);
956 job_info->selection = NULL;
962 ev_pixbuf_cache_get_selection_surface (EvPixbufCache *pixbuf_cache,
967 CacheJobInfo *job_info;
969 /* the document does not implement the selection interface */
970 if (!EV_IS_SELECTION (pixbuf_cache->document))
973 job_info = find_job_cache (pixbuf_cache, page);
974 if (job_info == NULL)
977 /* No selection on this page */
978 if (!job_info->points_set)
982 g_assert (job_info->rc);
983 ev_render_context_set_scale (job_info->rc, scale);
985 /* If we have a running job, we just return what we have under the
986 * assumption that it'll be updated later and we can scale it as need
988 if (job_info->job && (EV_JOB_RENDER (job_info->job)->flags & EV_RENDER_INCLUDE_SELECTION))
989 return job_info->selection;
991 /* Now, lets see if we need to resize the image. If we do, we clear the
993 clear_selection_if_needed (pixbuf_cache, job_info, page, scale);
995 /* Finally, we see if the two scales are the same, and get a new pixbuf
996 * if needed. We do this synchronously for now. At some point, we
997 * _should_ be able to get rid of the doc_mutex, so the synchronicity
998 * doesn't kill us. Rendering a few glyphs should really be fast.
1000 if (ev_rect_cmp (&(job_info->target_points), &(job_info->selection_points))) {
1001 EvRectangle *old_points;
1002 GdkColor *text, *base;
1004 /* we need to get a new selection pixbuf */
1005 ev_document_doc_mutex_lock ();
1006 if (job_info->selection_points.x1 < 0) {
1007 g_assert (job_info->selection == NULL);
1010 g_assert (job_info->selection != NULL);
1011 old_points = &(job_info->selection_points);
1014 if (job_info->selection_region)
1015 gdk_region_destroy (job_info->selection_region);
1016 job_info->selection_region =
1017 ev_selection_get_selection_region (EV_SELECTION (pixbuf_cache->document),
1019 job_info->selection_style,
1020 &(job_info->target_points));
1022 gtk_widget_ensure_style (pixbuf_cache->view);
1024 get_selection_colors (pixbuf_cache->view, &text, &base);
1026 ev_selection_render_selection (EV_SELECTION (pixbuf_cache->document),
1027 job_info->rc, &(job_info->selection),
1028 &(job_info->target_points),
1030 job_info->selection_style,
1032 job_info->selection_points = job_info->target_points;
1033 ev_document_doc_mutex_unlock ();
1036 *region = job_info->selection_region;
1037 return job_info->selection;
1041 update_job_selection (CacheJobInfo *job_info,
1042 EvViewSelection *selection)
1044 job_info->points_set = TRUE;
1045 job_info->target_points = selection->rect;
1046 job_info->selection_style = selection->style;
1050 clear_job_selection (CacheJobInfo *job_info)
1052 job_info->points_set = FALSE;
1053 job_info->selection_points.x1 = -1;
1055 if (job_info->selection) {
1056 cairo_surface_destroy (job_info->selection);
1057 job_info->selection = NULL;
1061 /* This function will reset the selection on pages that no longer have them, and
1062 * will update the target_selection on those that need it. It will _not_ free
1063 * the previous selection_list -- that's up to caller to do.
1066 ev_pixbuf_cache_set_selection_list (EvPixbufCache *pixbuf_cache,
1067 GList *selection_list)
1069 EvPageCache *page_cache;
1070 EvViewSelection *selection;
1071 GList *list = selection_list;
1075 g_return_if_fail (EV_IS_PIXBUF_CACHE (pixbuf_cache));
1077 if (!EV_IS_SELECTION (pixbuf_cache->document))
1080 page_cache = ev_page_cache_get (pixbuf_cache->document);
1082 /* We check each area to see what needs updating, and what needs freeing; */
1083 page = pixbuf_cache->start_page - pixbuf_cache->preload_cache_size;
1084 for (i = 0; i < pixbuf_cache->preload_cache_size; i++) {
1092 if (((EvViewSelection *)list->data)->page == page) {
1093 selection = list->data;
1095 } else if (((EvViewSelection *)list->data)->page > page)
1101 update_job_selection (pixbuf_cache->prev_job + i, selection);
1103 clear_job_selection (pixbuf_cache->prev_job + i);
1107 page = pixbuf_cache->start_page;
1108 for (i = 0; i < PAGE_CACHE_LEN (pixbuf_cache); i++) {
1111 if (((EvViewSelection *)list->data)->page == page) {
1112 selection = list->data;
1114 } else if (((EvViewSelection *)list->data)->page > page)
1120 update_job_selection (pixbuf_cache->job_list + i, selection);
1122 clear_job_selection (pixbuf_cache->job_list + i);
1126 for (i = 0; i < pixbuf_cache->preload_cache_size; i++) {
1127 if (page >= ev_page_cache_get_n_pages (page_cache))
1132 if (((EvViewSelection *)list->data)->page == page) {
1133 selection = list->data;
1135 } else if (((EvViewSelection *)list->data)->page > page)
1141 update_job_selection (pixbuf_cache->next_job + i, selection);
1143 clear_job_selection (pixbuf_cache->next_job + i);
1149 /* Returns what the pixbuf cache thinks is */
1152 ev_pixbuf_cache_get_selection_list (EvPixbufCache *pixbuf_cache)
1154 EvPageCache *page_cache;
1155 EvViewSelection *selection;
1156 GList *retval = NULL;
1160 g_return_val_if_fail (EV_IS_PIXBUF_CACHE (pixbuf_cache), NULL);
1162 page_cache = ev_page_cache_get (pixbuf_cache->document);
1164 /* We check each area to see what needs updating, and what needs freeing; */
1165 page = pixbuf_cache->start_page - pixbuf_cache->preload_cache_size;
1166 for (i = 0; i < pixbuf_cache->preload_cache_size; i++) {
1172 if (pixbuf_cache->prev_job[i].selection_points.x1 != -1) {
1173 selection = g_new0 (EvViewSelection, 1);
1174 selection->page = page;
1175 selection->rect = pixbuf_cache->prev_job[i].selection_points;
1176 if (pixbuf_cache->prev_job[i].selection_region)
1177 selection->covered_region = gdk_region_copy (pixbuf_cache->prev_job[i].selection_region);
1178 retval = g_list_append (retval, selection);
1184 page = pixbuf_cache->start_page;
1185 for (i = 0; i < PAGE_CACHE_LEN (pixbuf_cache); i++) {
1186 if (pixbuf_cache->job_list[i].selection_points.x1 != -1) {
1187 selection = g_new0 (EvViewSelection, 1);
1188 selection->page = page;
1189 selection->rect = pixbuf_cache->job_list[i].selection_points;
1190 if (pixbuf_cache->job_list[i].selection_region)
1191 selection->covered_region = gdk_region_copy (pixbuf_cache->job_list[i].selection_region);
1192 retval = g_list_append (retval, selection);
1198 for (i = 0; i < pixbuf_cache->preload_cache_size; i++) {
1199 if (page >= ev_page_cache_get_n_pages (page_cache))
1202 if (pixbuf_cache->next_job[i].selection_points.x1 != -1) {
1203 selection = g_new0 (EvViewSelection, 1);
1204 selection->page = page;
1205 selection->rect = pixbuf_cache->next_job[i].selection_points;
1206 if (pixbuf_cache->next_job[i].selection_region)
1207 selection->covered_region = gdk_region_copy (pixbuf_cache->next_job[i].selection_region);
1208 retval = g_list_append (retval, selection);
1218 ev_pixbuf_cache_reload_page (EvPixbufCache *pixbuf_cache,
1224 CacheJobInfo *job_info;
1225 EvPageCache *page_cache;
1228 job_info = find_job_cache (pixbuf_cache, page);
1229 if (job_info == NULL)
1232 page_cache = ev_page_cache_get (pixbuf_cache->document);
1233 ev_page_cache_get_size (page_cache, page, rotation, scale,
1236 add_job (pixbuf_cache, job_info, page_cache, region,
1237 width, height, page, rotation, scale,
1238 EV_JOB_PRIORITY_HIGH);