1 #include "ev-pixbuf-cache.h"
2 #include "ev-job-queue.h"
3 #include "ev-page-cache.h"
4 #include "ev-selection.h"
6 typedef struct _CacheJobInfo
11 /* Data we get from rendering */
14 GdkRegion *text_mapping;
17 * Selection_points are the coordinates encapsulated in selection.
18 * target_points is the target selection size. */
19 EvRectangle selection_points;
20 EvRectangle target_points;
24 GdkRegion *selection_region;
31 /* We keep a link to our containing view just for style information. */
37 /* preload_cache_size is the number of pages prior to the current
38 * visible area that we cache. It's normally 1, but could be 2 in the
41 int preload_cache_size;
42 CacheJobInfo *prev_job;
43 CacheJobInfo *job_list;
44 CacheJobInfo *next_job;
47 struct _EvPixbufCacheClass
49 GObjectClass parent_class;
51 void (* job_finished) (EvPixbufCache *pixbuf_cache);
61 static guint signals[N_SIGNALS] = {0, };
63 static void ev_pixbuf_cache_init (EvPixbufCache *pixbuf_cache);
64 static void ev_pixbuf_cache_class_init (EvPixbufCacheClass *pixbuf_cache);
65 static void ev_pixbuf_cache_finalize (GObject *object);
66 static void ev_pixbuf_cache_dispose (GObject *object);
67 static void job_finished_cb (EvJob *job,
68 EvPixbufCache *pixbuf_cache);
69 static CacheJobInfo *find_job_cache (EvPixbufCache *pixbuf_cache,
71 static void copy_job_to_job_info (EvJobRender *job_render,
72 CacheJobInfo *job_info,
73 EvPixbufCache *pixbuf_cache);
74 static gboolean new_selection_pixbuf_needed(EvPixbufCache *pixbuf_cache,
75 CacheJobInfo *job_info,
80 /* These are used for iterating through the prev and next arrays */
81 #define FIRST_VISABLE_PREV(pixbuf_cache) \
82 (MAX (0, pixbuf_cache->preload_cache_size + 1 - pixbuf_cache->start_page))
83 #define VISIBLE_NEXT_LEN(pixbuf_cache, page_cache) \
84 (MIN(pixbuf_cache->preload_cache_size, ev_page_cache_get_n_pages (page_cache) - (1 + pixbuf_cache->end_page)))
85 #define PAGE_CACHE_LEN(pixbuf_cache) \
86 ((pixbuf_cache->end_page - pixbuf_cache->start_page) + 1)
88 G_DEFINE_TYPE (EvPixbufCache, ev_pixbuf_cache, G_TYPE_OBJECT)
91 ev_pixbuf_cache_init (EvPixbufCache *pixbuf_cache)
93 pixbuf_cache->start_page = 0;
94 pixbuf_cache->end_page = 0;
95 pixbuf_cache->job_list = g_new0 (CacheJobInfo, PAGE_CACHE_LEN (pixbuf_cache));
97 pixbuf_cache->preload_cache_size = 2;
98 pixbuf_cache->prev_job = g_new0 (CacheJobInfo, pixbuf_cache->preload_cache_size);
99 pixbuf_cache->next_job = g_new0 (CacheJobInfo, pixbuf_cache->preload_cache_size);
103 ev_pixbuf_cache_class_init (EvPixbufCacheClass *class)
105 GObjectClass *object_class;
107 object_class = G_OBJECT_CLASS (class);
109 object_class->finalize = ev_pixbuf_cache_finalize;
110 object_class->dispose = ev_pixbuf_cache_dispose;
112 signals[JOB_FINISHED] = g_signal_new ("job-finished",
113 G_OBJECT_CLASS_TYPE (object_class),
114 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
115 G_STRUCT_OFFSET (EvPixbufCacheClass, job_finished),
117 g_cclosure_marshal_VOID__VOID,
122 ev_pixbuf_cache_finalize (GObject *object)
124 EvPixbufCache *pixbuf_cache;
126 pixbuf_cache = EV_PIXBUF_CACHE (object);
128 g_free (pixbuf_cache->prev_job);
129 g_free (pixbuf_cache->job_list);
130 g_free (pixbuf_cache->next_job);
134 dispose_cache_job_info (CacheJobInfo *job_info,
137 if (job_info == NULL)
140 g_signal_handlers_disconnect_by_func (job_info->job,
141 G_CALLBACK (job_finished_cb),
143 ev_job_queue_remove_job (job_info->job);
144 g_object_unref (G_OBJECT (job_info->job));
145 job_info->job = NULL;
147 if (job_info->pixbuf) {
148 g_object_unref (G_OBJECT (job_info->pixbuf));
149 job_info->pixbuf = NULL;
151 if (job_info->link_mapping) {
152 ev_link_mapping_free (job_info->link_mapping);
153 job_info->link_mapping = NULL;
155 if (job_info->text_mapping) {
156 gdk_region_destroy (job_info->text_mapping);
157 job_info->text_mapping = NULL;
159 if (job_info->selection) {
160 g_object_unref (G_OBJECT (job_info->selection));
161 job_info->selection = NULL;
163 if (job_info->selection_region) {
164 gdk_region_destroy (job_info->selection_region);
165 job_info->selection_region = NULL;
168 g_object_unref (G_OBJECT (job_info->rc));
172 job_info->points_set = FALSE;
176 ev_pixbuf_cache_dispose (GObject *object)
178 EvPixbufCache *pixbuf_cache;
181 pixbuf_cache = EV_PIXBUF_CACHE (object);
183 for (i = 0; i < pixbuf_cache->preload_cache_size; i++) {
184 dispose_cache_job_info (pixbuf_cache->prev_job + i, pixbuf_cache);
185 dispose_cache_job_info (pixbuf_cache->next_job + i, pixbuf_cache);
188 for (i = 0; i < PAGE_CACHE_LEN (pixbuf_cache); i++) {
189 dispose_cache_job_info (pixbuf_cache->job_list + i, pixbuf_cache);
195 ev_pixbuf_cache_new (GtkWidget *view,
196 EvDocument *document)
198 EvPixbufCache *pixbuf_cache;
200 pixbuf_cache = (EvPixbufCache *) g_object_new (EV_TYPE_PIXBUF_CACHE, NULL);
201 /* This is a backlink, so we don't ref this */
202 pixbuf_cache->view = view;
203 pixbuf_cache->document = document;
209 job_finished_cb (EvJob *job,
210 EvPixbufCache *pixbuf_cache)
212 CacheJobInfo *job_info;
213 EvJobRender *job_render = EV_JOB_RENDER (job);
215 /* If the job is outside of our interest, we silently discard it */
216 if ((job_render->rc->page < (pixbuf_cache->start_page - pixbuf_cache->preload_cache_size)) ||
217 (job_render->rc->page > (pixbuf_cache->end_page + pixbuf_cache->preload_cache_size))) {
218 g_object_unref (job);
222 job_info = find_job_cache (pixbuf_cache, job_render->rc->page);
224 copy_job_to_job_info (job_render, job_info, pixbuf_cache);
226 g_signal_emit (pixbuf_cache, signals[JOB_FINISHED], 0);
229 /* This checks a job to see if the job would generate the right sized pixbuf
230 * given a scale. If it won't, it removes the job and clears it to NULL.
233 check_job_size_and_unref (EvPixbufCache *pixbuf_cache,
234 CacheJobInfo *job_info,
235 EvPageCache *page_cache,
243 if (job_info->job == NULL)
246 ev_page_cache_get_size (page_cache,
247 EV_JOB_RENDER (job_info->job)->rc->page,
248 EV_JOB_RENDER (job_info->job)->rc->rotation,
252 if (width == EV_JOB_RENDER (job_info->job)->target_width &&
253 height == EV_JOB_RENDER (job_info->job)->target_height)
256 g_signal_handlers_disconnect_by_func (job_info->job,
257 G_CALLBACK (job_finished_cb),
259 ev_job_queue_remove_job (job_info->job);
260 g_object_unref (job_info->job);
261 job_info->job = NULL;
264 /* Do all function that copies a job from an older cache to it's position in the
265 * new cache. It clears the old job if it doesn't have a place.
268 move_one_job (CacheJobInfo *job_info,
269 EvPixbufCache *pixbuf_cache,
271 CacheJobInfo *new_job_list,
272 CacheJobInfo *new_prev_job,
273 CacheJobInfo *new_next_job,
276 EvJobPriority priority)
278 CacheJobInfo *target_page = NULL;
280 EvJobPriority new_priority;
282 if (page < (start_page - pixbuf_cache->preload_cache_size) ||
283 page > (end_page + pixbuf_cache->preload_cache_size)) {
284 dispose_cache_job_info (job_info, pixbuf_cache);
288 /* find the target page to copy it over to. */
289 if (page < start_page) {
290 page_offset = (page - (start_page - pixbuf_cache->preload_cache_size));
292 g_assert (page_offset >= 0 &&
293 page_offset < pixbuf_cache->preload_cache_size);
294 target_page = new_prev_job + page_offset;
295 new_priority = EV_JOB_PRIORITY_LOW;
296 } else if (page > end_page) {
297 page_offset = (page - (end_page + 1));
299 g_assert (page_offset >= 0 &&
300 page_offset < pixbuf_cache->preload_cache_size);
301 target_page = new_next_job + page_offset;
302 new_priority = EV_JOB_PRIORITY_LOW;
304 page_offset = page - start_page;
305 g_assert (page_offset >= 0 &&
306 page_offset <= ((end_page - start_page) + 1));
307 new_priority = EV_JOB_PRIORITY_HIGH;
308 target_page = new_job_list + page_offset;
311 *target_page = *job_info;
312 job_info->job = NULL;
313 job_info->pixbuf = NULL;
314 job_info->link_mapping = NULL;
316 if (new_priority != priority && target_page->job) {
317 ev_job_queue_update_job (target_page->job, new_priority);
324 ev_pixbuf_cache_update_range (EvPixbufCache *pixbuf_cache,
328 CacheJobInfo *new_job_list;
329 CacheJobInfo *new_prev_job;
330 CacheJobInfo *new_next_job;
331 EvPageCache *page_cache;
334 if (pixbuf_cache->start_page == start_page &&
335 pixbuf_cache->end_page == end_page)
338 page_cache = ev_page_cache_get (pixbuf_cache->document);
340 new_job_list = g_new0 (CacheJobInfo, (end_page - start_page) + 1);
341 new_prev_job = g_new0 (CacheJobInfo, pixbuf_cache->preload_cache_size);
342 new_next_job = g_new0 (CacheJobInfo, pixbuf_cache->preload_cache_size);
344 /* We go through each job in the old cache and either clear it or move
345 * it to a new location. */
347 /* Start with the prev cache. */
348 page = pixbuf_cache->start_page - pixbuf_cache->preload_cache_size;
349 for (i = 0; i < pixbuf_cache->preload_cache_size; i++) {
351 dispose_cache_job_info (pixbuf_cache->prev_job + i, pixbuf_cache);
353 move_one_job (pixbuf_cache->prev_job + i,
355 new_job_list, new_prev_job, new_next_job,
356 start_page, end_page, EV_JOB_PRIORITY_LOW);
361 page = pixbuf_cache->start_page;
362 for (i = 0; i < PAGE_CACHE_LEN (pixbuf_cache); i++) {
363 move_one_job (pixbuf_cache->job_list + i,
365 new_job_list, new_prev_job, new_next_job,
366 start_page, end_page, EV_JOB_PRIORITY_HIGH);
370 for (i = 0; i < pixbuf_cache->preload_cache_size; i++) {
371 if (page >= ev_page_cache_get_n_pages (page_cache)) {
372 dispose_cache_job_info (pixbuf_cache->next_job + i, pixbuf_cache);
374 move_one_job (pixbuf_cache->next_job + i,
376 new_job_list, new_prev_job, new_next_job,
377 start_page, end_page, EV_JOB_PRIORITY_LOW);
382 g_free (pixbuf_cache->job_list);
383 g_free (pixbuf_cache->prev_job);
384 g_free (pixbuf_cache->next_job);
386 pixbuf_cache->job_list = new_job_list;
387 pixbuf_cache->prev_job = new_prev_job;
388 pixbuf_cache->next_job = new_next_job;
390 pixbuf_cache->start_page = start_page;
391 pixbuf_cache->end_page = end_page;
395 copy_job_to_job_info (EvJobRender *job_render,
396 CacheJobInfo *job_info,
397 EvPixbufCache *pixbuf_cache)
400 job_info->points_set = FALSE;
402 if (job_info->pixbuf) {
403 g_object_unref (G_OBJECT (job_info->pixbuf));
405 job_info->pixbuf = g_object_ref (job_render->pixbuf);
408 g_object_unref (G_OBJECT (job_info->rc));
410 job_info->rc = g_object_ref (job_render->rc);
412 if (job_render->include_links) {
413 if (job_info->link_mapping)
414 ev_link_mapping_free (job_info->link_mapping);
415 job_info->link_mapping = job_render->link_mapping;
418 if (job_render->include_text) {
419 if (job_info->text_mapping)
420 gdk_region_destroy (job_info->text_mapping);
421 job_info->text_mapping = job_render->text_mapping;
425 if (job_render->include_selection) {
427 if (job_info->selection) {
428 g_object_unref (G_OBJECT (job_info->selection));
429 job_info->selection = NULL;
431 if (job_info->selection_region) {
432 gdk_region_destroy (job_info->selection_region);
433 job_info->selection_region = NULL;
436 job_info->selection_points = job_render->selection_points;
437 job_info->selection_region = gdk_region_copy (job_render->selection_region);
438 job_info->selection = g_object_ref (job_render->selection);
439 g_assert (job_info->selection_points.x1 >= 0);
443 g_signal_handlers_disconnect_by_func (job_info->job,
444 G_CALLBACK (job_finished_cb),
446 ev_job_queue_remove_job (job_info->job);
447 g_object_unref (G_OBJECT (job_info->job));
448 job_info->job = NULL;
454 find_job_cache (EvPixbufCache *pixbuf_cache,
459 if (page < (pixbuf_cache->start_page - pixbuf_cache->preload_cache_size) ||
460 page > (pixbuf_cache->end_page + pixbuf_cache->preload_cache_size))
463 if (page < pixbuf_cache->start_page) {
464 page_offset = (page - (pixbuf_cache->start_page - pixbuf_cache->preload_cache_size));
466 g_assert (page_offset >= 0 &&
467 page_offset < pixbuf_cache->preload_cache_size);
468 return pixbuf_cache->prev_job + page_offset;
471 if (page > pixbuf_cache->end_page) {
472 page_offset = (page - (pixbuf_cache->end_page + 1));
474 g_assert (page_offset >= 0 &&
475 page_offset < pixbuf_cache->preload_cache_size);
476 return pixbuf_cache->next_job + page_offset;
479 page_offset = page - pixbuf_cache->start_page;
480 g_assert (page_offset >= 0 &&
481 page_offset <= PAGE_CACHE_LEN(pixbuf_cache));
482 return pixbuf_cache->job_list + page_offset;
486 ev_pixbuf_cache_clear_job_sizes (EvPixbufCache *pixbuf_cache,
489 EvPageCache *page_cache;
492 page_cache = ev_page_cache_get (pixbuf_cache->document);
494 for (i = 0; i < PAGE_CACHE_LEN (pixbuf_cache); i++) {
495 check_job_size_and_unref (pixbuf_cache, pixbuf_cache->job_list + i, page_cache, scale);
498 for (i = 0; i < pixbuf_cache->preload_cache_size; i++) {
499 check_job_size_and_unref (pixbuf_cache, pixbuf_cache->prev_job + i, page_cache, scale);
500 check_job_size_and_unref (pixbuf_cache, pixbuf_cache->next_job + i, page_cache, scale);
504 #define FIRST_VISABLE_PREV(pixbuf_cache) \
505 (MAX (0, pixbuf_cache->preload_cache_size + 1 - pixbuf_cache->start_page))
508 get_selection_colors (GtkWidget *widget, GdkColor **text, GdkColor **base)
510 if (GTK_WIDGET_HAS_FOCUS (widget)) {
511 *text = &widget->style->text [GTK_STATE_SELECTED];
512 *base = &widget->style->base [GTK_STATE_SELECTED];
514 *text = &widget->style->text [GTK_STATE_ACTIVE];
515 *base = &widget->style->base [GTK_STATE_ACTIVE];
520 add_job_if_needed (EvPixbufCache *pixbuf_cache,
521 CacheJobInfo *job_info,
522 EvPageCache *page_cache,
526 EvJobPriority priority)
528 gboolean include_links = FALSE;
529 gboolean include_text = FALSE;
530 gboolean include_selection = FALSE;
532 GdkColor *text, *base;
537 ev_page_cache_get_size (page_cache, page, rotation,
538 scale, &width, &height);
540 if (job_info->pixbuf &&
541 gdk_pixbuf_get_width (job_info->pixbuf) == width &&
542 gdk_pixbuf_get_height (job_info->pixbuf) == height)
545 /* make a new job now */
546 if (job_info->rc == NULL) {
547 job_info->rc = ev_render_context_new (rotation, page, scale);
549 ev_render_context_set_rotation (job_info->rc, rotation);
550 ev_render_context_set_page (job_info->rc, page);
551 ev_render_context_set_scale (job_info->rc, scale);
554 /* Figure out what else we need for this job */
555 if (job_info->link_mapping == NULL)
556 include_links = TRUE;
557 if (job_info->text_mapping == NULL)
559 if (new_selection_pixbuf_needed (pixbuf_cache, job_info, page, scale)) {
560 include_selection = TRUE;
563 gtk_widget_ensure_style (pixbuf_cache->view);
565 get_selection_colors (pixbuf_cache->view, &text, &base);
567 job_info->job = ev_job_render_new (pixbuf_cache->document,
570 &(job_info->target_points),
575 ev_job_queue_add_job (job_info->job, priority);
576 g_signal_connect (job_info->job, "finished", G_CALLBACK (job_finished_cb), pixbuf_cache);
581 ev_pixbuf_cache_add_jobs_if_needed (EvPixbufCache *pixbuf_cache,
585 EvPageCache *page_cache;
586 CacheJobInfo *job_info;
590 page_cache = ev_page_cache_get (pixbuf_cache->document);
592 for (i = 0; i < PAGE_CACHE_LEN (pixbuf_cache); i++) {
593 job_info = (pixbuf_cache->job_list + i);
594 page = pixbuf_cache->start_page + i;
596 add_job_if_needed (pixbuf_cache, job_info,
597 page_cache, page, rotation, scale,
598 EV_JOB_PRIORITY_HIGH);
601 for (i = FIRST_VISABLE_PREV(pixbuf_cache); i < pixbuf_cache->preload_cache_size; i++) {
602 job_info = (pixbuf_cache->prev_job + i);
603 page = pixbuf_cache->start_page - pixbuf_cache->preload_cache_size + i;
605 add_job_if_needed (pixbuf_cache, job_info,
606 page_cache, page, rotation, scale,
607 EV_JOB_PRIORITY_LOW);
610 for (i = 0; i < VISIBLE_NEXT_LEN(pixbuf_cache, page_cache); i++) {
611 job_info = (pixbuf_cache->next_job + i);
612 page = pixbuf_cache->end_page + 1 + i;
614 add_job_if_needed (pixbuf_cache, job_info,
615 page_cache, page, rotation, scale,
616 EV_JOB_PRIORITY_LOW);
622 ev_pixbuf_cache_set_page_range (EvPixbufCache *pixbuf_cache,
627 GList *selection_list)
629 EvPageCache *page_cache;
631 g_return_if_fail (EV_IS_PIXBUF_CACHE (pixbuf_cache));
633 page_cache = ev_page_cache_get (pixbuf_cache->document);
635 g_return_if_fail (start_page >= 0 && start_page < ev_page_cache_get_n_pages (page_cache));
636 g_return_if_fail (end_page >= 0 && end_page < ev_page_cache_get_n_pages (page_cache));
637 g_return_if_fail (end_page >= start_page);
639 /* First, resize the page_range as needed. We cull old pages
641 ev_pixbuf_cache_update_range (pixbuf_cache, start_page, end_page);
643 /* Then, we update the current jobs to see if any of them are the wrong
644 * size, we remove them if we need to. */
645 ev_pixbuf_cache_clear_job_sizes (pixbuf_cache, scale);
647 /* Next, we update the target selection for our pages */
648 ev_pixbuf_cache_set_selection_list (pixbuf_cache, selection_list);
650 /* Finally, we add the new jobs for all the sizes that don't have a
652 ev_pixbuf_cache_add_jobs_if_needed (pixbuf_cache, rotation, scale);
656 ev_pixbuf_cache_get_pixbuf (EvPixbufCache *pixbuf_cache,
659 CacheJobInfo *job_info;
661 job_info = find_job_cache (pixbuf_cache, page);
662 if (job_info == NULL)
665 /* We don't need to wait for the idle to handle the callback */
667 EV_JOB (job_info->job)->finished) {
668 copy_job_to_job_info (EV_JOB_RENDER (job_info->job), job_info, pixbuf_cache);
671 return job_info->pixbuf;
675 ev_pixbuf_cache_get_link_mapping (EvPixbufCache *pixbuf_cache,
678 CacheJobInfo *job_info;
680 job_info = find_job_cache (pixbuf_cache, page);
681 if (job_info == NULL)
684 /* We don't need to wait for the idle to handle the callback */
686 EV_JOB (job_info->job)->finished) {
687 copy_job_to_job_info (EV_JOB_RENDER (job_info->job), job_info, pixbuf_cache);
690 return job_info->link_mapping;
694 new_selection_pixbuf_needed (EvPixbufCache *pixbuf_cache,
695 CacheJobInfo *job_info,
699 EvPageCache *page_cache;
702 if (job_info->selection) {
703 page_cache = ev_page_cache_get (pixbuf_cache->document);
704 ev_page_cache_get_size (page_cache, page, job_info->rc->rotation,
705 scale, &width, &height);
707 if (width != gdk_pixbuf_get_width (job_info->selection) ||
708 height != gdk_pixbuf_get_height (job_info->selection))
711 if (job_info->points_set)
718 clear_selection_if_needed (EvPixbufCache *pixbuf_cache,
719 CacheJobInfo *job_info,
723 if (new_selection_pixbuf_needed (pixbuf_cache, job_info, page, scale)) {
724 if (job_info->selection)
725 g_object_unref (job_info->selection);
726 job_info->selection = NULL;
727 job_info->selection_points.x1 = -1;
732 ev_pixbuf_cache_get_text_mapping (EvPixbufCache *pixbuf_cache,
735 CacheJobInfo *job_info;
737 job_info = find_job_cache (pixbuf_cache, page);
738 if (job_info == NULL)
741 /* We don't need to wait for the idle to handle the callback */
743 EV_JOB (job_info->job)->finished) {
744 copy_job_to_job_info (EV_JOB_RENDER (job_info->job), job_info, pixbuf_cache);
747 return job_info->text_mapping;
750 /* Clears the cache of jobs and pixbufs.
753 ev_pixbuf_cache_clear (EvPixbufCache *pixbuf_cache)
757 for (i = 0; i < pixbuf_cache->preload_cache_size; i++) {
758 dispose_cache_job_info (pixbuf_cache->prev_job + i, pixbuf_cache);
759 dispose_cache_job_info (pixbuf_cache->next_job + i, pixbuf_cache);
762 for (i = 0; i < PAGE_CACHE_LEN (pixbuf_cache); i++) {
763 dispose_cache_job_info (pixbuf_cache->job_list + i, pixbuf_cache);
769 ev_pixbuf_cache_style_changed (EvPixbufCache *pixbuf_cache)
773 /* FIXME: doesn't update running jobs. */
774 for (i = 0; i < pixbuf_cache->preload_cache_size; i++) {
775 CacheJobInfo *job_info;
777 job_info = pixbuf_cache->prev_job + i;
778 if (job_info->selection) {
779 g_object_unref (G_OBJECT (job_info->selection));
780 job_info->selection = NULL;
783 job_info = pixbuf_cache->next_job + i;
784 if (job_info->selection) {
785 g_object_unref (G_OBJECT (job_info->selection));
786 job_info->selection = NULL;
790 for (i = 0; i < PAGE_CACHE_LEN (pixbuf_cache); i++) {
791 CacheJobInfo *job_info;
793 job_info = pixbuf_cache->job_list + i;
794 if (job_info->selection) {
795 g_object_unref (G_OBJECT (job_info->selection));
796 job_info->selection = NULL;
802 ev_pixbuf_cache_get_selection_pixbuf (EvPixbufCache *pixbuf_cache,
807 CacheJobInfo *job_info;
809 /* the document does not implement the selection interface */
810 if (!EV_IS_SELECTION (pixbuf_cache->document))
813 job_info = find_job_cache (pixbuf_cache, page);
814 if (job_info == NULL)
817 /* No selection on this page */
818 if (!job_info->points_set)
822 g_assert (job_info->rc);
823 ev_render_context_set_scale (job_info->rc, scale);
825 /* If we have a running job, we just return what we have under the
826 * assumption that it'll be updated later and we can scale it as need
828 if (job_info->job && EV_JOB_RENDER (job_info->job)->include_selection)
829 return job_info->selection;
831 /* Now, lets see if we need to resize the image. If we do, we clear the
833 clear_selection_if_needed (pixbuf_cache, job_info, page, scale);
835 /* Finally, we see if the two scales are the same, and get a new pixbuf
836 * if needed. We do this synchronously for now. At some point, we
837 * _should_ be able to get rid of the doc_mutex, so the synchronicity
838 * doesn't kill us. Rendering a few glyphs should really be fast.
840 if (ev_rect_cmp (&(job_info->target_points), &(job_info->selection_points))) {
841 EvRectangle *old_points;
842 GdkColor *text, *base;
844 /* we need to get a new selection pixbuf */
845 ev_document_doc_mutex_lock ();
846 if (job_info->selection_points.x1 < 0) {
847 g_assert (job_info->selection == NULL);
850 g_assert (job_info->selection != NULL);
851 old_points = &(job_info->selection_points);
854 if (job_info->selection_region)
855 gdk_region_destroy (job_info->selection_region);
856 job_info->selection_region =
857 ev_selection_get_selection_region (EV_SELECTION (pixbuf_cache->document),
859 &(job_info->target_points));
861 gtk_widget_ensure_style (pixbuf_cache->view);
863 get_selection_colors (pixbuf_cache->view, &text, &base);
865 ev_selection_render_selection (EV_SELECTION (pixbuf_cache->document),
866 job_info->rc, &(job_info->selection),
867 &(job_info->target_points),
870 job_info->selection_points = job_info->target_points;
871 ev_document_doc_mutex_unlock ();
874 *region = job_info->selection_region;
875 return job_info->selection;
879 update_job_selection (CacheJobInfo *job_info,
880 EvViewSelection *selection)
882 job_info->points_set = TRUE;
883 job_info->target_points = selection->rect;
887 clear_job_selection (CacheJobInfo *job_info)
889 job_info->points_set = FALSE;
891 if (job_info->selection) {
892 g_object_unref (job_info->selection);
893 job_info->selection = NULL;
897 /* This function will reset the selection on pages that no longer have them, and
898 * will update the target_selection on those that need it. It will _not_ free
899 * the previous selection_list -- that's up to caller to do.
902 ev_pixbuf_cache_set_selection_list (EvPixbufCache *pixbuf_cache,
903 GList *selection_list)
905 EvPageCache *page_cache;
906 EvViewSelection *selection;
907 GList *list = selection_list;
911 g_return_if_fail (EV_IS_PIXBUF_CACHE (pixbuf_cache));
913 if (!EV_IS_SELECTION (pixbuf_cache->document))
916 page_cache = ev_page_cache_get (pixbuf_cache->document);
918 /* We check each area to see what needs updating, and what needs freeing; */
919 page = pixbuf_cache->start_page - pixbuf_cache->preload_cache_size;
920 for (i = 0; i < pixbuf_cache->preload_cache_size; i++) {
928 if (((EvViewSelection *)list->data)->page == page) {
929 selection = list->data;
931 } else if (((EvViewSelection *)list->data)->page > page)
937 update_job_selection (pixbuf_cache->prev_job + i, selection);
939 clear_job_selection (pixbuf_cache->prev_job + i);
943 page = pixbuf_cache->start_page;
944 for (i = 0; i < PAGE_CACHE_LEN (pixbuf_cache); i++) {
947 if (((EvViewSelection *)list->data)->page == page) {
948 selection = list->data;
950 } else if (((EvViewSelection *)list->data)->page > page)
956 update_job_selection (pixbuf_cache->job_list + i, selection);
958 clear_job_selection (pixbuf_cache->job_list + i);
962 for (i = 0; i < pixbuf_cache->preload_cache_size; i++) {
963 if (page >= ev_page_cache_get_n_pages (page_cache))
968 if (((EvViewSelection *)list->data)->page == page) {
969 selection = list->data;
971 } else if (((EvViewSelection *)list->data)->page > page)
977 update_job_selection (pixbuf_cache->next_job + i, selection);
979 clear_job_selection (pixbuf_cache->next_job + i);
985 /* Returns what the pixbuf cache thinks is */
988 ev_pixbuf_cache_get_selection_list (EvPixbufCache *pixbuf_cache)
990 EvPageCache *page_cache;
991 EvViewSelection *selection;
992 GList *retval = NULL;
996 g_return_val_if_fail (EV_IS_PIXBUF_CACHE (pixbuf_cache), NULL);
998 page_cache = ev_page_cache_get (pixbuf_cache->document);
1000 /* We check each area to see what needs updating, and what needs freeing; */
1001 page = pixbuf_cache->start_page - pixbuf_cache->preload_cache_size;
1002 for (i = 0; i < pixbuf_cache->preload_cache_size; i++) {
1008 if (pixbuf_cache->prev_job[i].selection_points.x1 != -1) {
1009 selection = g_new0 (EvViewSelection, 1);
1010 selection->page = page;
1011 selection->rect = pixbuf_cache->prev_job[i].selection_points;
1012 if (pixbuf_cache->prev_job[i].selection_region)
1013 selection->covered_region = gdk_region_copy (pixbuf_cache->prev_job[i].selection_region);
1014 retval = g_list_append (retval, selection);
1020 page = pixbuf_cache->start_page;
1021 for (i = 0; i < PAGE_CACHE_LEN (pixbuf_cache); i++) {
1022 if (pixbuf_cache->job_list[i].selection_points.x1 != -1) {
1023 selection = g_new0 (EvViewSelection, 1);
1024 selection->page = page;
1025 selection->rect = pixbuf_cache->job_list[i].selection_points;
1026 if (pixbuf_cache->job_list[i].selection_region)
1027 selection->covered_region = gdk_region_copy (pixbuf_cache->job_list[i].selection_region);
1028 retval = g_list_append (retval, selection);
1034 for (i = 0; i < pixbuf_cache->preload_cache_size; i++) {
1035 if (page >= ev_page_cache_get_n_pages (page_cache))
1038 if (pixbuf_cache->next_job[i].selection_points.x1 != -1) {
1039 selection = g_new0 (EvViewSelection, 1);
1040 selection->page = page;
1041 selection->rect = pixbuf_cache->next_job[i].selection_points;
1042 if (pixbuf_cache->next_job[i].selection_region)
1043 selection->covered_region = gdk_region_copy (pixbuf_cache->next_job[i].selection_region);
1044 retval = g_list_append (retval, selection);