]> www.fi.muni.cz Git - evince.git/blob - shell/ev-pixbuf-cache.c
630bdde5bfbebc70cd552ff7681e4c33eb23034b
[evince.git] / shell / ev-pixbuf-cache.c
1 #include "ev-pixbuf-cache.h"
2 #include "ev-job-queue.h"
3 #include "ev-page-cache.h"
4 #include "ev-selection.h"
5
6 typedef struct _CacheJobInfo
7 {
8         EvJob *job;
9         EvRenderContext *rc;
10
11         /* Data we get from rendering */
12         GdkPixbuf *pixbuf;
13         GList *link_mapping;
14         GdkRegion *text_mapping;
15         
16         /* Selection data. 
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;
21         gboolean    points_set;
22         
23         GdkPixbuf *selection;
24         GdkRegion *selection_region;
25 } CacheJobInfo;
26
27 struct _EvPixbufCache
28 {
29         GObject parent;
30
31         /* We keep a link to our containing view just for style information. */
32         GtkWidget *view;
33         EvDocument *document;
34         int start_page;
35         int end_page;
36
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
39          * case of twin pages.
40          */
41         int preload_cache_size;
42         CacheJobInfo *prev_job;
43         CacheJobInfo *job_list;
44         CacheJobInfo *next_job;
45 };
46
47 struct _EvPixbufCacheClass
48 {
49         GObjectClass parent_class;
50
51         void (* job_finished) (EvPixbufCache *pixbuf_cache);
52 };
53
54
55 enum
56 {
57         JOB_FINISHED,
58         N_SIGNALS,
59 };
60
61 static guint signals[N_SIGNALS] = {0, };
62
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,
70                                                  int                 page);
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,
76                                                  gint                page,
77                                                  gfloat              scale);
78
79
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)
87
88 G_DEFINE_TYPE (EvPixbufCache, ev_pixbuf_cache, G_TYPE_OBJECT)
89
90 static void
91 ev_pixbuf_cache_init (EvPixbufCache *pixbuf_cache)
92 {
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));
96
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);
100 }
101
102 static void
103 ev_pixbuf_cache_class_init (EvPixbufCacheClass *class)
104 {
105         GObjectClass *object_class;
106
107         object_class = G_OBJECT_CLASS (class);
108
109         object_class->finalize = ev_pixbuf_cache_finalize;
110         object_class->dispose = ev_pixbuf_cache_dispose;
111
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),
116                                             NULL, NULL,
117                                             g_cclosure_marshal_VOID__VOID,
118                                             G_TYPE_NONE, 0);
119 }
120
121 static void
122 ev_pixbuf_cache_finalize (GObject *object)
123 {
124         EvPixbufCache *pixbuf_cache;
125
126         pixbuf_cache = EV_PIXBUF_CACHE (object);
127
128         g_free (pixbuf_cache->prev_job);
129         g_free (pixbuf_cache->job_list);
130         g_free (pixbuf_cache->next_job);
131 }
132
133 static void
134 dispose_cache_job_info (CacheJobInfo *job_info,
135                         gpointer      data)
136 {
137         if (job_info == NULL)
138                 return;
139         if (job_info->job) {
140                 g_signal_handlers_disconnect_by_func (job_info->job,
141                                                       G_CALLBACK (job_finished_cb),
142                                                       data);
143                 ev_job_queue_remove_job (job_info->job);
144                 g_object_unref (G_OBJECT (job_info->job));
145                 job_info->job = NULL;
146         }
147         if (job_info->pixbuf) {
148                 g_object_unref (G_OBJECT (job_info->pixbuf));
149                 job_info->pixbuf = NULL;
150         }
151         if (job_info->link_mapping) {
152                 ev_link_mapping_free (job_info->link_mapping);
153                 job_info->link_mapping = NULL;
154         }
155         if (job_info->text_mapping) {
156                 gdk_region_destroy (job_info->text_mapping);
157                 job_info->text_mapping = NULL;
158         }
159         if (job_info->selection) {
160                 g_object_unref (G_OBJECT (job_info->selection));
161                 job_info->selection = NULL;
162         }
163         if (job_info->selection_region) {
164                 gdk_region_destroy (job_info->selection_region);
165                 job_info->selection_region = NULL;
166         }
167         if (job_info->rc) {
168                 g_object_unref (G_OBJECT (job_info->rc));
169                 job_info->rc = NULL;
170         }
171
172         job_info->points_set = FALSE;
173 }
174
175 static void
176 ev_pixbuf_cache_dispose (GObject *object)
177 {
178         EvPixbufCache *pixbuf_cache;
179         int i;
180
181         pixbuf_cache = EV_PIXBUF_CACHE (object);
182
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);
186         }
187
188         for (i = 0; i < PAGE_CACHE_LEN (pixbuf_cache); i++) {
189                 dispose_cache_job_info (pixbuf_cache->job_list + i, pixbuf_cache);
190         }
191 }
192
193
194 EvPixbufCache *
195 ev_pixbuf_cache_new (GtkWidget  *view,
196                      EvDocument *document)
197 {
198         EvPixbufCache *pixbuf_cache;
199
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;
204
205         return pixbuf_cache;
206 }
207
208 static void
209 job_finished_cb (EvJob         *job,
210                  EvPixbufCache *pixbuf_cache)
211 {
212         CacheJobInfo *job_info;
213         EvJobRender *job_render = EV_JOB_RENDER (job);
214
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);
219                 return;
220         }
221         
222         job_info = find_job_cache (pixbuf_cache, job_render->rc->page);
223
224         copy_job_to_job_info (job_render, job_info, pixbuf_cache);
225
226         g_signal_emit (pixbuf_cache, signals[JOB_FINISHED], 0);
227 }
228
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.
231  */
232 static void
233 check_job_size_and_unref (EvPixbufCache *pixbuf_cache,
234                           CacheJobInfo *job_info,
235                           EvPageCache  *page_cache,
236                           gfloat        scale)
237 {
238         gint width;
239         gint height;
240
241         g_assert (job_info);
242
243         if (job_info->job == NULL)
244                 return;
245
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,
249                                 scale,
250                                 &width, &height);
251                                 
252         if (width == EV_JOB_RENDER (job_info->job)->target_width &&
253             height == EV_JOB_RENDER (job_info->job)->target_height)
254                 return;
255
256         g_signal_handlers_disconnect_by_func (job_info->job,
257                                               G_CALLBACK (job_finished_cb),
258                                               pixbuf_cache);
259         ev_job_queue_remove_job (job_info->job);
260         g_object_unref (job_info->job);
261         job_info->job = NULL;
262 }
263
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.
266  */
267 static void
268 move_one_job (CacheJobInfo  *job_info,
269               EvPixbufCache *pixbuf_cache,
270               int            page,
271               CacheJobInfo  *new_job_list,
272               CacheJobInfo  *new_prev_job,
273               CacheJobInfo  *new_next_job,
274               int            start_page,
275               int            end_page,
276               EvJobPriority  priority)
277 {
278         CacheJobInfo *target_page = NULL;
279         int page_offset;
280         EvJobPriority new_priority;
281
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);
285                 return;
286         }
287
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));
291
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));
298
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;
303         } else {
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;
309         }
310
311         *target_page = *job_info;
312         job_info->job = NULL;
313         job_info->pixbuf = NULL;
314         job_info->link_mapping = NULL;
315
316         if (new_priority != priority && target_page->job) {
317                 ev_job_queue_update_job (target_page->job, new_priority);
318         }
319 }
320
321
322
323 static void
324 ev_pixbuf_cache_update_range (EvPixbufCache *pixbuf_cache,
325                               gint           start_page,
326                               gint           end_page)
327 {
328         CacheJobInfo *new_job_list;
329         CacheJobInfo *new_prev_job;
330         CacheJobInfo *new_next_job;
331         EvPageCache *page_cache;
332         int i, page;
333
334         if (pixbuf_cache->start_page == start_page &&
335             pixbuf_cache->end_page == end_page)
336                 return;
337
338         page_cache = ev_page_cache_get (pixbuf_cache->document);
339
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);
343
344         /* We go through each job in the old cache and either clear it or move
345          * it to a new location. */
346
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++) {
350                 if (page < 0) {
351                         dispose_cache_job_info (pixbuf_cache->prev_job + i, pixbuf_cache);
352                 } else {
353                         move_one_job (pixbuf_cache->prev_job + i,
354                                       pixbuf_cache, page,
355                                       new_job_list, new_prev_job, new_next_job,
356                                       start_page, end_page, EV_JOB_PRIORITY_LOW);
357                 }
358                 page ++;
359         }
360
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,
364                               pixbuf_cache, page,
365                               new_job_list, new_prev_job, new_next_job,
366                               start_page, end_page, EV_JOB_PRIORITY_HIGH);
367                 page ++;
368         }
369
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);
373                 } else {
374                         move_one_job (pixbuf_cache->next_job + i,
375                                       pixbuf_cache, page,
376                                       new_job_list, new_prev_job, new_next_job,
377                                       start_page, end_page, EV_JOB_PRIORITY_LOW);
378                 }
379                 page ++;
380         }
381
382         g_free (pixbuf_cache->job_list);
383         g_free (pixbuf_cache->prev_job);
384         g_free (pixbuf_cache->next_job);
385
386         pixbuf_cache->job_list = new_job_list;
387         pixbuf_cache->prev_job = new_prev_job;
388         pixbuf_cache->next_job = new_next_job;
389
390         pixbuf_cache->start_page = start_page;
391         pixbuf_cache->end_page = end_page;
392 }
393
394 static void
395 copy_job_to_job_info (EvJobRender   *job_render,
396                       CacheJobInfo  *job_info,
397                       EvPixbufCache *pixbuf_cache)
398 {
399
400         job_info->points_set = FALSE;
401
402         if (job_info->pixbuf) {
403                 g_object_unref (G_OBJECT (job_info->pixbuf));
404         }
405         job_info->pixbuf = g_object_ref (job_render->pixbuf);
406
407         if (job_info->rc) {
408                 g_object_unref (G_OBJECT (job_info->rc));
409         }
410         job_info->rc = g_object_ref (job_render->rc);
411
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;
416         }
417
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;
422         }
423         
424
425         if (job_render->include_selection) {
426
427                 if (job_info->selection) {
428                         g_object_unref (G_OBJECT (job_info->selection));
429                         job_info->selection = NULL;
430                 }
431                 if (job_info->selection_region) {
432                         gdk_region_destroy (job_info->selection_region);
433                         job_info->selection_region = NULL;
434                 }
435                 
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);
440         }
441
442         if (job_info->job) {
443                 g_signal_handlers_disconnect_by_func (job_info->job,
444                                                       G_CALLBACK (job_finished_cb),
445                                                       pixbuf_cache);
446                 ev_job_queue_remove_job (job_info->job);
447                 g_object_unref (G_OBJECT (job_info->job));
448                 job_info->job = NULL;
449         }
450
451 }
452
453 static CacheJobInfo*
454 find_job_cache (EvPixbufCache *pixbuf_cache,
455                 int            page)
456 {
457         int page_offset;
458
459         if (page < (pixbuf_cache->start_page - pixbuf_cache->preload_cache_size) ||
460             page > (pixbuf_cache->end_page + pixbuf_cache->preload_cache_size))
461                 return NULL;
462
463         if (page < pixbuf_cache->start_page) {
464                 page_offset = (page - (pixbuf_cache->start_page - pixbuf_cache->preload_cache_size));
465
466                 g_assert (page_offset >= 0 &&
467                           page_offset < pixbuf_cache->preload_cache_size);
468                 return pixbuf_cache->prev_job + page_offset;
469         }
470
471         if (page > pixbuf_cache->end_page) {
472                 page_offset = (page - (pixbuf_cache->end_page + 1));
473
474                 g_assert (page_offset >= 0 &&
475                           page_offset < pixbuf_cache->preload_cache_size);
476                 return pixbuf_cache->next_job + page_offset;
477         }
478
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;
483 }
484
485 static void
486 ev_pixbuf_cache_clear_job_sizes (EvPixbufCache *pixbuf_cache,
487                                  gfloat         scale)
488 {
489         EvPageCache *page_cache;
490         int i;
491
492         page_cache = ev_page_cache_get (pixbuf_cache->document);
493
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);
496         }
497
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);
501         }
502 }
503
504 #define FIRST_VISABLE_PREV(pixbuf_cache) \
505         (MAX (0, pixbuf_cache->preload_cache_size + 1 - pixbuf_cache->start_page))
506
507 static void
508 get_selection_colors (GtkWidget *widget, GdkColor **text, GdkColor **base)
509 {
510     if (GTK_WIDGET_HAS_FOCUS (widget)) {
511         *text = &widget->style->text [GTK_STATE_SELECTED];
512         *base = &widget->style->base [GTK_STATE_SELECTED];
513     } else {
514         *text = &widget->style->text [GTK_STATE_ACTIVE];
515         *base = &widget->style->base [GTK_STATE_ACTIVE];
516     }
517 }
518
519 static void
520 add_job_if_needed (EvPixbufCache *pixbuf_cache,
521                    CacheJobInfo  *job_info,
522                    EvPageCache   *page_cache,
523                    gint           page,
524                    gint           rotation,
525                    gfloat         scale,
526                    EvJobPriority  priority)
527 {
528         gboolean include_links = FALSE;
529         gboolean include_text = FALSE;
530         gboolean include_selection = FALSE;
531         int width, height;
532         GdkColor *text, *base;
533
534         if (job_info->job)
535                 return;
536
537         ev_page_cache_get_size (page_cache, page, rotation,
538                                 scale, &width, &height);
539
540         if (job_info->pixbuf &&
541             gdk_pixbuf_get_width (job_info->pixbuf) == width &&
542             gdk_pixbuf_get_height (job_info->pixbuf) == height)
543                 return;
544
545         /* make a new job now */
546         if (job_info->rc == NULL) {
547                 job_info->rc = ev_render_context_new (rotation, page, scale);
548         } else {
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);
552         }
553
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)
558                 include_text = TRUE;
559         if (new_selection_pixbuf_needed (pixbuf_cache, job_info, page, scale)) {
560                 include_selection = TRUE;
561         }
562
563         gtk_widget_ensure_style (pixbuf_cache->view);
564
565         get_selection_colors (pixbuf_cache->view, &text, &base);
566
567         job_info->job = ev_job_render_new (pixbuf_cache->document,
568                                            job_info->rc,
569                                            width, height,
570                                            &(job_info->target_points),
571                                            text, base,
572                                            include_links,
573                                            include_text,
574                                            include_selection);
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);
577 }
578
579
580 static void
581 ev_pixbuf_cache_add_jobs_if_needed (EvPixbufCache *pixbuf_cache,
582                                     gint           rotation,
583                                     gfloat         scale)
584 {
585         EvPageCache *page_cache;
586         CacheJobInfo *job_info;
587         int page;
588         int i;
589
590         page_cache = ev_page_cache_get (pixbuf_cache->document);
591
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;
595
596                 add_job_if_needed (pixbuf_cache, job_info,
597                                    page_cache, page, rotation, scale,
598                                    EV_JOB_PRIORITY_HIGH);
599         }
600
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;
604
605                 add_job_if_needed (pixbuf_cache, job_info,
606                                    page_cache, page, rotation, scale,
607                                    EV_JOB_PRIORITY_LOW);
608         }
609
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;
613
614                 add_job_if_needed (pixbuf_cache, job_info,
615                                    page_cache, page, rotation, scale,
616                                    EV_JOB_PRIORITY_LOW);
617         }
618
619 }
620
621 void
622 ev_pixbuf_cache_set_page_range (EvPixbufCache  *pixbuf_cache,
623                                 gint            start_page,
624                                 gint            end_page,
625                                 gint            rotation,
626                                 gfloat          scale,
627                                 GList          *selection_list)
628 {
629         EvPageCache *page_cache;
630
631         g_return_if_fail (EV_IS_PIXBUF_CACHE (pixbuf_cache));
632
633         page_cache = ev_page_cache_get (pixbuf_cache->document);
634
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);
638
639         /* First, resize the page_range as needed.  We cull old pages
640          * mercilessly. */
641         ev_pixbuf_cache_update_range (pixbuf_cache, start_page, end_page);
642
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);
646
647         /* Next, we update the target selection for our pages */
648         ev_pixbuf_cache_set_selection_list (pixbuf_cache, selection_list);
649
650         /* Finally, we add the new jobs for all the sizes that don't have a
651          * pixbuf */
652         ev_pixbuf_cache_add_jobs_if_needed (pixbuf_cache, rotation, scale);
653 }
654
655 GdkPixbuf *
656 ev_pixbuf_cache_get_pixbuf (EvPixbufCache *pixbuf_cache,
657                             gint           page)
658 {
659         CacheJobInfo *job_info;
660
661         job_info = find_job_cache (pixbuf_cache, page);
662         if (job_info == NULL)
663                 return NULL;
664
665         /* We don't need to wait for the idle to handle the callback */
666         if (job_info->job &&
667             EV_JOB (job_info->job)->finished) {
668                 copy_job_to_job_info (EV_JOB_RENDER (job_info->job), job_info, pixbuf_cache);
669         }
670
671         return job_info->pixbuf;
672 }
673
674 GList *
675 ev_pixbuf_cache_get_link_mapping (EvPixbufCache *pixbuf_cache,
676                                   gint           page)
677 {
678         CacheJobInfo *job_info;
679
680         job_info = find_job_cache (pixbuf_cache, page);
681         if (job_info == NULL)
682                 return NULL;
683
684         /* We don't need to wait for the idle to handle the callback */
685         if (job_info->job &&
686             EV_JOB (job_info->job)->finished) {
687                 copy_job_to_job_info (EV_JOB_RENDER (job_info->job), job_info, pixbuf_cache);
688         }
689         
690         return job_info->link_mapping;
691 }
692
693 static gboolean
694 new_selection_pixbuf_needed (EvPixbufCache *pixbuf_cache,
695                              CacheJobInfo  *job_info,
696                              gint           page,
697                              gfloat         scale)
698 {
699         EvPageCache *page_cache;
700         gint width, height;
701
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);
706                 
707                 if (width != gdk_pixbuf_get_width (job_info->selection) ||
708                     height != gdk_pixbuf_get_height (job_info->selection))
709                         return TRUE;
710         } else {
711                 if (job_info->points_set)
712                         return TRUE;
713         }
714         return FALSE;
715 }
716
717 static void
718 clear_selection_if_needed (EvPixbufCache *pixbuf_cache,
719                            CacheJobInfo  *job_info,
720                            gint           page,
721                            gfloat         scale)
722 {
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;
728         }
729 }
730
731 GdkRegion *
732 ev_pixbuf_cache_get_text_mapping      (EvPixbufCache *pixbuf_cache,
733                                        gint           page)
734 {
735         CacheJobInfo *job_info;
736
737         job_info = find_job_cache (pixbuf_cache, page);
738         if (job_info == NULL)
739                 return NULL;
740
741         /* We don't need to wait for the idle to handle the callback */
742         if (job_info->job &&
743             EV_JOB (job_info->job)->finished) {
744                 copy_job_to_job_info (EV_JOB_RENDER (job_info->job), job_info, pixbuf_cache);
745         }
746         
747         return job_info->text_mapping;
748 }
749
750 /* Clears the cache of jobs and pixbufs.
751  */
752 void
753 ev_pixbuf_cache_clear (EvPixbufCache *pixbuf_cache)
754 {
755         int i;
756
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);
760         }
761
762         for (i = 0; i < PAGE_CACHE_LEN (pixbuf_cache); i++) {
763                 dispose_cache_job_info (pixbuf_cache->job_list + i, pixbuf_cache);
764         }
765 }
766
767
768 void
769 ev_pixbuf_cache_style_changed (EvPixbufCache *pixbuf_cache)
770 {
771         gint i;
772
773         /* FIXME: doesn't update running jobs. */
774         for (i = 0; i < pixbuf_cache->preload_cache_size; i++) {
775                 CacheJobInfo *job_info;
776
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;
781                 }
782
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;
787                 }
788         }
789
790         for (i = 0; i < PAGE_CACHE_LEN (pixbuf_cache); i++) {
791                 CacheJobInfo *job_info;
792
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;
797                 }
798         }
799 }
800
801 GdkPixbuf *
802 ev_pixbuf_cache_get_selection_pixbuf (EvPixbufCache  *pixbuf_cache,
803                                       gint            page,
804                                       gfloat          scale,
805                                       GdkRegion     **region)
806 {
807         CacheJobInfo *job_info;
808
809         /* the document does not implement the selection interface */
810         if (!EV_IS_SELECTION (pixbuf_cache->document))
811                 return NULL;
812
813         job_info = find_job_cache (pixbuf_cache, page);
814         if (job_info == NULL)
815                 return NULL;
816
817         /* No selection on this page */
818         if (!job_info->points_set)
819                 return NULL;
820
821         /* Update the rc */
822         g_assert (job_info->rc);
823         ev_render_context_set_scale (job_info->rc, scale);
824
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
827          * be */
828         if (job_info->job && EV_JOB_RENDER (job_info->job)->include_selection)
829                 return job_info->selection;
830
831         /* Now, lets see if we need to resize the image.  If we do, we clear the
832          * old one. */
833         clear_selection_if_needed (pixbuf_cache, job_info, page, scale);
834
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.
839          */
840         if (ev_rect_cmp (&(job_info->target_points), &(job_info->selection_points))) {
841                 EvRectangle *old_points;
842                 GdkColor *text, *base;
843
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);
848                         old_points = NULL;
849                 } else {
850                         g_assert (job_info->selection != NULL);
851                         old_points = &(job_info->selection_points);
852                 }
853
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),
858                                                            job_info->rc,
859                                                            &(job_info->target_points));
860
861                 gtk_widget_ensure_style (pixbuf_cache->view);
862
863                 get_selection_colors (pixbuf_cache->view, &text, &base);
864
865                 ev_selection_render_selection (EV_SELECTION (pixbuf_cache->document),
866                                                job_info->rc, &(job_info->selection),
867                                                &(job_info->target_points),
868                                                old_points,
869                                                text, base);
870                 job_info->selection_points = job_info->target_points;
871                 ev_document_doc_mutex_unlock ();
872         }
873         if (region)
874                 *region = job_info->selection_region;
875         return job_info->selection;
876 }
877
878 static void
879 update_job_selection (CacheJobInfo    *job_info,
880                       EvViewSelection *selection)
881 {
882         job_info->points_set = TRUE;            
883         job_info->target_points = selection->rect;
884 }
885
886 static void
887 clear_job_selection (CacheJobInfo *job_info)
888 {
889         job_info->points_set = FALSE;
890         job_info->selection_points.x1 = -1;
891
892         if (job_info->selection) {
893                 g_object_unref (job_info->selection);
894                 job_info->selection = NULL;
895         }
896 }
897
898 /* This function will reset the selection on pages that no longer have them, and
899  * will update the target_selection on those that need it.  It will _not_ free
900  * the previous selection_list -- that's up to caller to do.
901  */
902 void
903 ev_pixbuf_cache_set_selection_list (EvPixbufCache *pixbuf_cache,
904                                     GList         *selection_list)
905 {
906         EvPageCache *page_cache;
907         EvViewSelection *selection;
908         GList *list = selection_list;
909         int page;
910         int i;
911
912         g_return_if_fail (EV_IS_PIXBUF_CACHE (pixbuf_cache));
913
914         if (!EV_IS_SELECTION (pixbuf_cache->document))
915                 return;
916
917         page_cache = ev_page_cache_get (pixbuf_cache->document);
918
919         /* We check each area to see what needs updating, and what needs freeing; */
920         page = pixbuf_cache->start_page - pixbuf_cache->preload_cache_size;
921         for (i = 0; i < pixbuf_cache->preload_cache_size; i++) {
922                 if (page < 0) {
923                         page ++;
924                         continue;
925                 }
926
927                 selection = NULL;
928                 while (list) {
929                         if (((EvViewSelection *)list->data)->page == page) {
930                                 selection = list->data;
931                                 break;
932                         } else if (((EvViewSelection *)list->data)->page > page) 
933                                 break;
934                         list = list->next;
935                 }
936
937                 if (selection)
938                         update_job_selection (pixbuf_cache->prev_job + i, selection);
939                 else
940                         clear_job_selection (pixbuf_cache->prev_job + i);
941                 page ++;
942         }
943
944         page = pixbuf_cache->start_page;
945         for (i = 0; i < PAGE_CACHE_LEN (pixbuf_cache); i++) {
946                 selection = NULL;
947                 while (list) {
948                         if (((EvViewSelection *)list->data)->page == page) {
949                                 selection = list->data;
950                                 break;
951                         } else if (((EvViewSelection *)list->data)->page > page) 
952                                 break;
953                         list = list->next;
954                 }
955
956                 if (selection)
957                         update_job_selection (pixbuf_cache->job_list + i, selection);
958                 else
959                         clear_job_selection (pixbuf_cache->job_list + i);
960                 page ++;
961         }
962
963         for (i = 0; i < pixbuf_cache->preload_cache_size; i++) {
964                 if (page >= ev_page_cache_get_n_pages (page_cache))
965                         break;
966
967                 selection = NULL;
968                 while (list) {
969                         if (((EvViewSelection *)list->data)->page == page) {
970                                 selection = list->data;
971                                 break;
972                         } else if (((EvViewSelection *)list->data)->page > page) 
973                                 break;
974                         list = list->next;
975                 }
976
977                 if (selection)
978                         update_job_selection (pixbuf_cache->next_job + i, selection);
979                 else
980                         clear_job_selection (pixbuf_cache->next_job + i);
981                 page ++;
982         }
983 }
984
985
986 /* Returns what the pixbuf cache thinks is */
987
988 GList *
989 ev_pixbuf_cache_get_selection_list (EvPixbufCache *pixbuf_cache)
990 {
991         EvPageCache *page_cache;
992         EvViewSelection *selection;
993         GList *retval = NULL;
994         int page;
995         int i;
996
997         g_return_val_if_fail (EV_IS_PIXBUF_CACHE (pixbuf_cache), NULL);
998
999         page_cache = ev_page_cache_get (pixbuf_cache->document);
1000
1001         /* We check each area to see what needs updating, and what needs freeing; */
1002         page = pixbuf_cache->start_page - pixbuf_cache->preload_cache_size;
1003         for (i = 0; i < pixbuf_cache->preload_cache_size; i++) {
1004                 if (page < 0) {
1005                         page ++;
1006                         continue;
1007                 }
1008
1009                 if (pixbuf_cache->prev_job[i].selection_points.x1 != -1) {
1010                         selection = g_new0 (EvViewSelection, 1);
1011                         selection->page = page;
1012                         selection->rect = pixbuf_cache->prev_job[i].selection_points;
1013                         if (pixbuf_cache->prev_job[i].selection_region)
1014                                 selection->covered_region = gdk_region_copy (pixbuf_cache->prev_job[i].selection_region);
1015                         retval = g_list_append (retval, selection);
1016                 }
1017                 
1018                 page ++;
1019         }
1020
1021         page = pixbuf_cache->start_page;
1022         for (i = 0; i < PAGE_CACHE_LEN (pixbuf_cache); i++) {
1023                 if (pixbuf_cache->job_list[i].selection_points.x1 != -1) {
1024                         selection = g_new0 (EvViewSelection, 1);
1025                         selection->page = page;
1026                         selection->rect = pixbuf_cache->job_list[i].selection_points;
1027                         if (pixbuf_cache->job_list[i].selection_region)
1028                                 selection->covered_region = gdk_region_copy (pixbuf_cache->job_list[i].selection_region);
1029                         retval = g_list_append (retval, selection);
1030                 }
1031                 
1032                 page ++;
1033         }
1034
1035         for (i = 0; i < pixbuf_cache->preload_cache_size; i++) {
1036                 if (page >= ev_page_cache_get_n_pages (page_cache))
1037                         break;
1038
1039                 if (pixbuf_cache->next_job[i].selection_points.x1 != -1) {
1040                         selection = g_new0 (EvViewSelection, 1);
1041                         selection->page = page;
1042                         selection->rect = pixbuf_cache->next_job[i].selection_points;
1043                         if (pixbuf_cache->next_job[i].selection_region)
1044                                 selection->covered_region = gdk_region_copy (pixbuf_cache->next_job[i].selection_region);
1045                         retval = g_list_append (retval, selection);
1046                 }
1047                 
1048                 page ++;
1049         }
1050
1051         return retval;
1052 }
1053