]> www.fi.muni.cz Git - evince.git/blob - shell/ev-pixbuf-cache.c
Updated german translation
[evince.git] / shell / ev-pixbuf-cache.c
1 #include <config.h>
2 #include "ev-pixbuf-cache.h"
3 #include "ev-job-scheduler.h"
4 #include "ev-page-cache.h"
5 #include "ev-document-images.h"
6 #include "ev-document-forms.h"
7 #include "ev-image.h"
8 #include "ev-form-field.h"
9
10 typedef struct _CacheJobInfo
11 {
12         EvJob *job;
13         EvRenderContext *rc;
14         gboolean page_ready;
15
16         /* Region of the page that needs to be drawn */
17         GdkRegion *region; 
18
19         /* Data we get from rendering */
20         cairo_surface_t *surface;
21         GList *link_mapping;
22         GList *image_mapping;
23         GList *form_field_mapping;
24         GdkRegion *text_mapping;
25         
26         /* Selection data. 
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;
32         gboolean         points_set;
33         
34         cairo_surface_t *selection;
35         GdkRegion *selection_region;
36 } CacheJobInfo;
37
38 struct _EvPixbufCache
39 {
40         GObject parent;
41
42         /* We keep a link to our containing view just for style information. */
43         GtkWidget *view;
44         EvDocument *document;
45         int start_page;
46         int end_page;
47
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
50          * case of twin pages.
51          */
52         int preload_cache_size;
53         CacheJobInfo *prev_job;
54         CacheJobInfo *job_list;
55         CacheJobInfo *next_job;
56 };
57
58 struct _EvPixbufCacheClass
59 {
60         GObjectClass parent_class;
61
62         void (* job_finished) (EvPixbufCache *pixbuf_cache);
63 };
64
65
66 enum
67 {
68         JOB_FINISHED,
69         N_SIGNALS,
70 };
71
72 static guint signals[N_SIGNALS] = {0, };
73
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,
83                                                  int                 page);
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,
92                                                   gint                page,
93                                                   gfloat              scale);
94
95
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)
103
104 G_DEFINE_TYPE (EvPixbufCache, ev_pixbuf_cache, G_TYPE_OBJECT)
105
106 static void
107 ev_pixbuf_cache_init (EvPixbufCache *pixbuf_cache)
108 {
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));
112
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);
116 }
117
118 static void
119 ev_pixbuf_cache_class_init (EvPixbufCacheClass *class)
120 {
121         GObjectClass *object_class;
122
123         object_class = G_OBJECT_CLASS (class);
124
125         object_class->finalize = ev_pixbuf_cache_finalize;
126         object_class->dispose = ev_pixbuf_cache_dispose;
127
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),
133                               NULL, NULL,
134                               g_cclosure_marshal_VOID__POINTER,
135                               G_TYPE_NONE, 1,
136                               G_TYPE_POINTER);
137 }
138
139 static void
140 ev_pixbuf_cache_finalize (GObject *object)
141 {
142         EvPixbufCache *pixbuf_cache;
143
144         pixbuf_cache = EV_PIXBUF_CACHE (object);
145
146         g_free (pixbuf_cache->prev_job);
147         g_free (pixbuf_cache->job_list);
148         g_free (pixbuf_cache->next_job);
149
150         G_OBJECT_CLASS (ev_pixbuf_cache_parent_class)->finalize (object);
151 }
152
153 static void
154 dispose_cache_job_info (CacheJobInfo *job_info,
155                         gpointer      data)
156 {
157         if (job_info == NULL)
158                 return;
159         if (job_info->job) {
160                 g_signal_handlers_disconnect_by_func (job_info->job,
161                                                       G_CALLBACK (job_page_ready_cb),
162                                                       data);
163                 g_signal_handlers_disconnect_by_func (job_info->job,
164                                                       G_CALLBACK (job_finished_cb),
165                                                       data);
166                 ev_job_cancel (job_info->job);
167                 g_object_unref (job_info->job);
168                 job_info->job = NULL;
169         }
170         if (job_info->surface) {
171                 cairo_surface_destroy (job_info->surface);
172                 job_info->surface = NULL;
173         }
174         if (job_info->region) {
175                 gdk_region_destroy (job_info->region);
176                 job_info->region = NULL;
177         }
178         if (job_info->link_mapping) {
179                 ev_link_mapping_free (job_info->link_mapping);
180                 job_info->link_mapping = NULL;
181         }
182         if (job_info->image_mapping) {
183                 ev_image_mapping_free (job_info->image_mapping);
184                 job_info->image_mapping = NULL;
185         }
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;
189         }
190         if (job_info->text_mapping) {
191                 gdk_region_destroy (job_info->text_mapping);
192                 job_info->text_mapping = NULL;
193         }
194         if (job_info->selection) {
195                 cairo_surface_destroy (job_info->selection);
196                 job_info->selection = NULL;
197         }
198         if (job_info->selection_region) {
199                 gdk_region_destroy (job_info->selection_region);
200                 job_info->selection_region = NULL;
201         }
202         if (job_info->rc) {
203                 g_object_unref (G_OBJECT (job_info->rc));
204                 job_info->rc = NULL;
205         }
206
207         job_info->points_set = FALSE;
208 }
209
210 static void
211 ev_pixbuf_cache_dispose (GObject *object)
212 {
213         EvPixbufCache *pixbuf_cache;
214         int i;
215
216         pixbuf_cache = EV_PIXBUF_CACHE (object);
217
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);
221         }
222
223         for (i = 0; i < PAGE_CACHE_LEN (pixbuf_cache); i++) {
224                 dispose_cache_job_info (pixbuf_cache->job_list + i, pixbuf_cache);
225         }
226
227         G_OBJECT_CLASS (ev_pixbuf_cache_parent_class)->dispose (object);
228 }
229
230
231 EvPixbufCache *
232 ev_pixbuf_cache_new (GtkWidget  *view,
233                      EvDocument *document)
234 {
235         EvPixbufCache *pixbuf_cache;
236
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;
241
242         return pixbuf_cache;
243 }
244
245 static void
246 job_page_ready_cb (EvJob         *job,
247                    EvPixbufCache *pixbuf_cache)
248 {
249         CacheJobInfo *job_info;
250         EvJobRender *job_render = EV_JOB_RENDER (job);
251
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);
256                 return;
257         }
258
259         job_info = find_job_cache (pixbuf_cache, job_render->page);
260
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);
263 }
264
265 static void
266 job_finished_cb (EvJob         *job,
267                  EvPixbufCache *pixbuf_cache)
268 {
269         CacheJobInfo *job_info;
270         EvJobRender *job_render = EV_JOB_RENDER (job);
271
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);
276                 return;
277         }
278
279         job_info = find_job_cache (pixbuf_cache, job_render->page);
280         copy_job_to_job_info (job_render, job_info, pixbuf_cache);
281 }
282
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.
285  */
286 static void
287 check_job_size_and_unref (EvPixbufCache *pixbuf_cache,
288                           CacheJobInfo *job_info,
289                           EvPageCache  *page_cache,
290                           gfloat        scale)
291 {
292         gint width;
293         gint height;
294
295         g_assert (job_info);
296
297         if (job_info->job == NULL)
298                 return;
299
300         ev_page_cache_get_size (page_cache,
301                                 EV_JOB_RENDER (job_info->job)->page,
302                                 EV_JOB_RENDER (job_info->job)->rotation,
303                                 scale,
304                                 &width, &height);
305                                 
306         if (width == EV_JOB_RENDER (job_info->job)->target_width &&
307             height == EV_JOB_RENDER (job_info->job)->target_height)
308                 return;
309
310         g_signal_handlers_disconnect_by_func (job_info->job,
311                                               G_CALLBACK (job_page_ready_cb),
312                                               pixbuf_cache);
313         g_signal_handlers_disconnect_by_func (job_info->job,
314                                               G_CALLBACK (job_finished_cb),
315                                               pixbuf_cache);
316         ev_job_cancel (job_info->job);
317         g_object_unref (job_info->job);
318         job_info->job = NULL;
319 }
320
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.
323  */
324 static void
325 move_one_job (CacheJobInfo  *job_info,
326               EvPixbufCache *pixbuf_cache,
327               int            page,
328               CacheJobInfo  *new_job_list,
329               CacheJobInfo  *new_prev_job,
330               CacheJobInfo  *new_next_job,
331               int            start_page,
332               int            end_page,
333               gint           priority)
334 {
335         CacheJobInfo *target_page = NULL;
336         int page_offset;
337         gint new_priority;
338
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);
342                 return;
343         }
344
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));
348
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));
355
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;
360         } else {
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_URGENT;
365                 target_page = new_job_list + page_offset;
366         }
367
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;
375
376         if (new_priority != priority && target_page->job) {
377                 ev_job_scheduler_update_job (target_page->job, new_priority);
378         }
379 }
380
381 static void
382 ev_pixbuf_cache_update_range (EvPixbufCache *pixbuf_cache,
383                               gint           start_page,
384                               gint           end_page)
385 {
386         CacheJobInfo *new_job_list;
387         CacheJobInfo *new_prev_job;
388         CacheJobInfo *new_next_job;
389         EvPageCache *page_cache;
390         int i, page;
391
392         if (pixbuf_cache->start_page == start_page &&
393             pixbuf_cache->end_page == end_page)
394                 return;
395
396         page_cache = ev_page_cache_get (pixbuf_cache->document);
397
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);
401
402         /* We go through each job in the old cache and either clear it or move
403          * it to a new location. */
404
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++) {
408                 if (page < 0) {
409                         dispose_cache_job_info (pixbuf_cache->prev_job + i, pixbuf_cache);
410                 } else {
411                         move_one_job (pixbuf_cache->prev_job + i,
412                                       pixbuf_cache, page,
413                                       new_job_list, new_prev_job, new_next_job,
414                                       start_page, end_page, EV_JOB_PRIORITY_LOW);
415                 }
416                 page ++;
417         }
418
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,
422                               pixbuf_cache, page,
423                               new_job_list, new_prev_job, new_next_job,
424                               start_page, end_page, EV_JOB_PRIORITY_URGENT);
425                 page ++;
426         }
427
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);
431                 } else {
432                         move_one_job (pixbuf_cache->next_job + i,
433                                       pixbuf_cache, page,
434                                       new_job_list, new_prev_job, new_next_job,
435                                       start_page, end_page, EV_JOB_PRIORITY_LOW);
436                 }
437                 page ++;
438         }
439
440         g_free (pixbuf_cache->job_list);
441         g_free (pixbuf_cache->prev_job);
442         g_free (pixbuf_cache->next_job);
443
444         pixbuf_cache->job_list = new_job_list;
445         pixbuf_cache->prev_job = new_prev_job;
446         pixbuf_cache->next_job = new_next_job;
447
448         pixbuf_cache->start_page = start_page;
449         pixbuf_cache->end_page = end_page;
450 }
451
452 static void
453 copy_job_page_and_selection_to_job_info (EvJobRender   *job_render,
454                                          CacheJobInfo  *job_info,
455                                          EvPixbufCache *pixbuf_cache)
456 {
457         if (job_info->rc == NULL) {
458                 job_info->rc = ev_render_context_new (job_render->ev_page,
459                                                       job_render->rotation,
460                                                       job_render->scale);
461         } else {
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);
465         }
466         
467         if (job_info->surface) {
468                 cairo_surface_destroy (job_info->surface);
469         }
470         job_info->surface = cairo_surface_reference (job_render->surface);
471
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;
477                 }
478                 if (job_info->selection_region) {
479                         gdk_region_destroy (job_info->selection_region);
480                         job_info->selection_region = NULL;
481                 }
482                 
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;
488         }
489
490         if (job_info->job) {
491                 g_signal_handlers_disconnect_by_func (job_info->job,
492                                                       G_CALLBACK (job_page_ready_cb),
493                                                       pixbuf_cache);
494         }
495
496         job_info->page_ready = TRUE;
497 }
498
499 static void
500 copy_job_to_job_info (EvJobRender   *job_render,
501                       CacheJobInfo  *job_info,
502                       EvPixbufCache *pixbuf_cache)
503 {
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;
508         }
509
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;
514         }
515
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;
520         }
521
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;
526         }
527
528         if (job_info->job) {
529                 g_signal_handlers_disconnect_by_func (job_info->job,
530                                                       G_CALLBACK (job_finished_cb),
531                                                       pixbuf_cache);
532                 ev_job_cancel (job_info->job);
533                 g_object_unref (job_info->job);
534                 job_info->job = NULL;
535         }
536 }
537
538 static CacheJobInfo *
539 find_job_cache (EvPixbufCache *pixbuf_cache,
540                 int            page)
541 {
542         int page_offset;
543
544         if (page < (pixbuf_cache->start_page - pixbuf_cache->preload_cache_size) ||
545             page > (pixbuf_cache->end_page + pixbuf_cache->preload_cache_size))
546                 return NULL;
547
548         if (page < pixbuf_cache->start_page) {
549                 page_offset = (page - (pixbuf_cache->start_page - pixbuf_cache->preload_cache_size));
550
551                 g_assert (page_offset >= 0 &&
552                           page_offset < pixbuf_cache->preload_cache_size);
553                 return pixbuf_cache->prev_job + page_offset;
554         }
555
556         if (page > pixbuf_cache->end_page) {
557                 page_offset = (page - (pixbuf_cache->end_page + 1));
558
559                 g_assert (page_offset >= 0 &&
560                           page_offset < pixbuf_cache->preload_cache_size);
561                 return pixbuf_cache->next_job + page_offset;
562         }
563
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;
568 }
569
570 static void
571 ev_pixbuf_cache_clear_job_sizes (EvPixbufCache *pixbuf_cache,
572                                  gfloat         scale)
573 {
574         EvPageCache *page_cache;
575         int i;
576
577         page_cache = ev_page_cache_get (pixbuf_cache->document);
578
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);
581         }
582
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);
586         }
587 }
588
589 #define FIRST_VISABLE_PREV(pixbuf_cache) \
590         (MAX (0, pixbuf_cache->preload_cache_size + 1 - pixbuf_cache->start_page))
591
592 static void
593 get_selection_colors (GtkWidget *widget, GdkColor **text, GdkColor **base)
594 {
595     if (GTK_WIDGET_HAS_FOCUS (widget)) {
596         *text = &widget->style->text [GTK_STATE_SELECTED];
597         *base = &widget->style->base [GTK_STATE_SELECTED];
598     } else {
599         *text = &widget->style->text [GTK_STATE_ACTIVE];
600         *base = &widget->style->base [GTK_STATE_ACTIVE];
601     }
602 }
603
604 static void
605 add_job (EvPixbufCache *pixbuf_cache,
606          CacheJobInfo  *job_info,
607          EvPageCache   *page_cache,
608          GdkRegion     *region,
609          gint           width,
610          gint           height,
611          gint           page,
612          gint           rotation,
613          gfloat         scale,
614          EvJobPriority  priority)
615 {
616         EvRenderFlags flags = 0;
617
618         job_info->page_ready = FALSE;
619         
620         if (job_info->region)
621                 gdk_region_destroy (job_info->region);
622         job_info->region = region ? gdk_region_copy (region) : NULL;
623
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;
633
634         job_info->job = ev_job_render_new (pixbuf_cache->document,
635                                            page, rotation, scale,
636                                            width, height,
637                                            flags);
638         
639         if (new_selection_surface_needed (pixbuf_cache, job_info, page, scale)) {
640                 GdkColor *text, *base;
641
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,
647                                                   text, base);
648         }
649
650         g_signal_connect (G_OBJECT (job_info->job), "page-ready",
651                           G_CALLBACK (job_page_ready_cb),
652                           pixbuf_cache);
653         g_signal_connect (G_OBJECT (job_info->job), "finished",
654                           G_CALLBACK (job_finished_cb),
655                           pixbuf_cache);
656         ev_job_scheduler_push_job (job_info->job, priority);
657 }
658
659 static void
660 add_job_if_needed (EvPixbufCache *pixbuf_cache,
661                    CacheJobInfo  *job_info,
662                    EvPageCache   *page_cache,
663                    gint           page,
664                    gint           rotation,
665                    gfloat         scale,
666                    EvJobPriority  priority)
667 {
668         gint width, height;
669
670         if (job_info->job)
671                 return;
672
673         ev_page_cache_get_size (page_cache, page, rotation,
674                                 scale, &width, &height);
675
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)
679                 return;
680
681         add_job (pixbuf_cache, job_info, page_cache, NULL,
682                  width, height, page, rotation, scale,
683                  priority);
684 }
685
686 static void
687 ev_pixbuf_cache_add_jobs_if_needed (EvPixbufCache *pixbuf_cache,
688                                     gint           rotation,
689                                     gfloat         scale)
690 {
691         EvPageCache *page_cache;
692         CacheJobInfo *job_info;
693         int page;
694         int i;
695
696         page_cache = ev_page_cache_get (pixbuf_cache->document);
697
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;
701
702                 add_job_if_needed (pixbuf_cache, job_info,
703                                    page_cache, page, rotation, scale,
704                                    EV_JOB_PRIORITY_URGENT);
705         }
706
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;
710
711                 add_job_if_needed (pixbuf_cache, job_info,
712                                    page_cache, page, rotation, scale,
713                                    EV_JOB_PRIORITY_LOW);
714         }
715
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;
719
720                 add_job_if_needed (pixbuf_cache, job_info,
721                                    page_cache, page, rotation, scale,
722                                    EV_JOB_PRIORITY_LOW);
723         }
724
725 }
726
727 void
728 ev_pixbuf_cache_set_page_range (EvPixbufCache  *pixbuf_cache,
729                                 gint            start_page,
730                                 gint            end_page,
731                                 gint            rotation,
732                                 gfloat          scale,
733                                 GList          *selection_list)
734 {
735         EvPageCache *page_cache;
736
737         g_return_if_fail (EV_IS_PIXBUF_CACHE (pixbuf_cache));
738
739         page_cache = ev_page_cache_get (pixbuf_cache->document);
740
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);
744
745         /* First, resize the page_range as needed.  We cull old pages
746          * mercilessly. */
747         ev_pixbuf_cache_update_range (pixbuf_cache, start_page, end_page);
748
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);
752
753         /* Next, we update the target selection for our pages */
754         ev_pixbuf_cache_set_selection_list (pixbuf_cache, selection_list);
755
756         /* Finally, we add the new jobs for all the sizes that don't have a
757          * pixbuf */
758         ev_pixbuf_cache_add_jobs_if_needed (pixbuf_cache, rotation, scale);
759 }
760
761 cairo_surface_t *
762 ev_pixbuf_cache_get_surface (EvPixbufCache *pixbuf_cache,
763                              gint           page)
764 {
765         CacheJobInfo *job_info;
766
767         job_info = find_job_cache (pixbuf_cache, page);
768         if (job_info == NULL)
769                 return NULL;
770
771         if (job_info->page_ready)
772                 return job_info->surface;
773         
774         /* We don't need to wait for the idle to handle the callback */
775         if (job_info->job &&
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);
779         }
780
781         return job_info->surface;
782 }
783
784 GList *
785 ev_pixbuf_cache_get_link_mapping (EvPixbufCache *pixbuf_cache,
786                                   gint           page)
787 {
788         CacheJobInfo *job_info;
789
790         job_info = find_job_cache (pixbuf_cache, page);
791         if (job_info == NULL)
792                 return NULL;
793
794         /* We don't need to wait for the idle to handle the callback */
795         if (job_info->job &&
796             EV_JOB (job_info->job)->finished) {
797                 copy_job_to_job_info (EV_JOB_RENDER (job_info->job), job_info, pixbuf_cache);
798         }
799
800         return job_info->link_mapping;
801 }
802
803 GList *
804 ev_pixbuf_cache_get_image_mapping (EvPixbufCache *pixbuf_cache,
805                                    gint           page)
806 {
807         CacheJobInfo *job_info;
808
809         if (!EV_IS_DOCUMENT_IMAGES (pixbuf_cache->document))
810                 return NULL;
811         
812         job_info = find_job_cache (pixbuf_cache, page);
813         if (job_info == NULL)
814                 return NULL;
815
816         /* We don't need to wait for the idle to handle the callback */
817         if (job_info->job &&
818             EV_JOB (job_info->job)->finished) {
819                 copy_job_to_job_info (EV_JOB_RENDER (job_info->job), job_info, pixbuf_cache);
820         }
821
822         return job_info->image_mapping;
823 }
824
825 GList *
826 ev_pixbuf_cache_get_form_field_mapping (EvPixbufCache *pixbuf_cache,
827                                         gint           page)
828 {
829         CacheJobInfo *job_info;
830
831         if (!EV_IS_DOCUMENT_FORMS (pixbuf_cache->document))
832                 return NULL;
833         
834         job_info = find_job_cache (pixbuf_cache, page);
835         if (job_info == NULL)
836                 return NULL;
837
838         /* We don't need to wait for the idle to handle the callback */
839         if (job_info->job &&
840            EV_JOB (job_info->job)->finished) {
841                 copy_job_to_job_info (EV_JOB_RENDER(job_info->job), job_info, pixbuf_cache);
842         }
843         
844         return job_info->form_field_mapping;
845 }
846
847 static gboolean
848 new_selection_surface_needed (EvPixbufCache *pixbuf_cache,
849                               CacheJobInfo  *job_info,
850                               gint           page,
851                               gfloat         scale)
852 {
853         EvPageCache *page_cache;
854
855         if (job_info->selection) {
856                 gint width, height;
857                 gint selection_width, selection_height;
858                 
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);
863
864                 selection_width = cairo_image_surface_get_width (job_info->selection);
865                 selection_height = cairo_image_surface_get_height (job_info->selection);
866                 
867                 if (width != selection_width || height != selection_height)
868                         return TRUE;
869         } else {
870                 if (job_info->points_set)
871                         return TRUE;
872         }
873         
874         return FALSE;
875 }
876
877 static void
878 clear_selection_if_needed (EvPixbufCache *pixbuf_cache,
879                            CacheJobInfo  *job_info,
880                            gint           page,
881                            gfloat         scale)
882 {
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;
888         }
889 }
890
891 GdkRegion *
892 ev_pixbuf_cache_get_text_mapping (EvPixbufCache *pixbuf_cache,
893                                   gint           page)
894 {
895         CacheJobInfo *job_info;
896
897         job_info = find_job_cache (pixbuf_cache, page);
898         if (job_info == NULL)
899                 return NULL;
900
901         /* We don't need to wait for the idle to handle the callback */
902         if (job_info->job &&
903             EV_JOB (job_info->job)->finished) {
904                 copy_job_to_job_info (EV_JOB_RENDER (job_info->job), job_info, pixbuf_cache);
905         }
906         
907         return job_info->text_mapping;
908 }
909
910 /* Clears the cache of jobs and pixbufs.
911  */
912 void
913 ev_pixbuf_cache_clear (EvPixbufCache *pixbuf_cache)
914 {
915         int i;
916
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);
920         }
921
922         for (i = 0; i < PAGE_CACHE_LEN (pixbuf_cache); i++) {
923                 dispose_cache_job_info (pixbuf_cache->job_list + i, pixbuf_cache);
924         }
925 }
926
927
928 void
929 ev_pixbuf_cache_style_changed (EvPixbufCache *pixbuf_cache)
930 {
931         gint i;
932
933         /* FIXME: doesn't update running jobs. */
934         for (i = 0; i < pixbuf_cache->preload_cache_size; i++) {
935                 CacheJobInfo *job_info;
936
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;
941                 }
942
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;
947                 }
948         }
949
950         for (i = 0; i < PAGE_CACHE_LEN (pixbuf_cache); i++) {
951                 CacheJobInfo *job_info;
952
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;
957                 }
958         }
959 }
960
961 cairo_surface_t *
962 ev_pixbuf_cache_get_selection_surface (EvPixbufCache  *pixbuf_cache,
963                                        gint            page,
964                                        gfloat          scale,
965                                        GdkRegion     **region)
966 {
967         CacheJobInfo *job_info;
968
969         /* the document does not implement the selection interface */
970         if (!EV_IS_SELECTION (pixbuf_cache->document))
971                 return NULL;
972
973         job_info = find_job_cache (pixbuf_cache, page);
974         if (job_info == NULL)
975                 return NULL;
976
977         /* No selection on this page */
978         if (!job_info->points_set)
979                 return NULL;
980
981         /* Update the rc */
982         g_assert (job_info->rc);
983         ev_render_context_set_scale (job_info->rc, scale);
984
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
987          * be */
988         if (job_info->job && (EV_JOB_RENDER (job_info->job)->flags & EV_RENDER_INCLUDE_SELECTION))
989                 return job_info->selection;
990
991         /* Now, lets see if we need to resize the image.  If we do, we clear the
992          * old one. */
993         clear_selection_if_needed (pixbuf_cache, job_info, page, scale);
994
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.
999          */
1000         if (ev_rect_cmp (&(job_info->target_points), &(job_info->selection_points))) {
1001                 EvRectangle *old_points;
1002                 GdkColor *text, *base;
1003
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);
1008                         old_points = NULL;
1009                 } else {
1010                         g_assert (job_info->selection != NULL);
1011                         old_points = &(job_info->selection_points);
1012                 }
1013
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),
1018                                                            job_info->rc,
1019                                                            job_info->selection_style,
1020                                                            &(job_info->target_points));
1021
1022                 gtk_widget_ensure_style (pixbuf_cache->view);
1023
1024                 get_selection_colors (pixbuf_cache->view, &text, &base);
1025
1026                 ev_selection_render_selection (EV_SELECTION (pixbuf_cache->document),
1027                                                job_info->rc, &(job_info->selection),
1028                                                &(job_info->target_points),
1029                                                old_points,
1030                                                job_info->selection_style,
1031                                                text, base);
1032                 job_info->selection_points = job_info->target_points;
1033                 ev_document_doc_mutex_unlock ();
1034         }
1035         if (region)
1036                 *region = job_info->selection_region;
1037         return job_info->selection;
1038 }
1039
1040 static void
1041 update_job_selection (CacheJobInfo    *job_info,
1042                       EvViewSelection *selection)
1043 {
1044         job_info->points_set = TRUE;            
1045         job_info->target_points = selection->rect;
1046         job_info->selection_style = selection->style;
1047 }
1048
1049 static void
1050 clear_job_selection (CacheJobInfo *job_info)
1051 {
1052         job_info->points_set = FALSE;
1053         job_info->selection_points.x1 = -1;
1054
1055         if (job_info->selection) {
1056                 cairo_surface_destroy (job_info->selection);
1057                 job_info->selection = NULL;
1058         }
1059 }
1060
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.
1064  */
1065 void
1066 ev_pixbuf_cache_set_selection_list (EvPixbufCache *pixbuf_cache,
1067                                     GList         *selection_list)
1068 {
1069         EvPageCache *page_cache;
1070         EvViewSelection *selection;
1071         GList *list = selection_list;
1072         int page;
1073         int i;
1074
1075         g_return_if_fail (EV_IS_PIXBUF_CACHE (pixbuf_cache));
1076
1077         if (!EV_IS_SELECTION (pixbuf_cache->document))
1078                 return;
1079
1080         page_cache = ev_page_cache_get (pixbuf_cache->document);
1081
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++) {
1085                 if (page < 0) {
1086                         page ++;
1087                         continue;
1088                 }
1089
1090                 selection = NULL;
1091                 while (list) {
1092                         if (((EvViewSelection *)list->data)->page == page) {
1093                                 selection = list->data;
1094                                 break;
1095                         } else if (((EvViewSelection *)list->data)->page > page) 
1096                                 break;
1097                         list = list->next;
1098                 }
1099
1100                 if (selection)
1101                         update_job_selection (pixbuf_cache->prev_job + i, selection);
1102                 else
1103                         clear_job_selection (pixbuf_cache->prev_job + i);
1104                 page ++;
1105         }
1106
1107         page = pixbuf_cache->start_page;
1108         for (i = 0; i < PAGE_CACHE_LEN (pixbuf_cache); i++) {
1109                 selection = NULL;
1110                 while (list) {
1111                         if (((EvViewSelection *)list->data)->page == page) {
1112                                 selection = list->data;
1113                                 break;
1114                         } else if (((EvViewSelection *)list->data)->page > page) 
1115                                 break;
1116                         list = list->next;
1117                 }
1118
1119                 if (selection)
1120                         update_job_selection (pixbuf_cache->job_list + i, selection);
1121                 else
1122                         clear_job_selection (pixbuf_cache->job_list + i);
1123                 page ++;
1124         }
1125
1126         for (i = 0; i < pixbuf_cache->preload_cache_size; i++) {
1127                 if (page >= ev_page_cache_get_n_pages (page_cache))
1128                         break;
1129
1130                 selection = NULL;
1131                 while (list) {
1132                         if (((EvViewSelection *)list->data)->page == page) {
1133                                 selection = list->data;
1134                                 break;
1135                         } else if (((EvViewSelection *)list->data)->page > page) 
1136                                 break;
1137                         list = list->next;
1138                 }
1139
1140                 if (selection)
1141                         update_job_selection (pixbuf_cache->next_job + i, selection);
1142                 else
1143                         clear_job_selection (pixbuf_cache->next_job + i);
1144                 page ++;
1145         }
1146 }
1147
1148
1149 /* Returns what the pixbuf cache thinks is */
1150
1151 GList *
1152 ev_pixbuf_cache_get_selection_list (EvPixbufCache *pixbuf_cache)
1153 {
1154         EvPageCache *page_cache;
1155         EvViewSelection *selection;
1156         GList *retval = NULL;
1157         int page;
1158         int i;
1159
1160         g_return_val_if_fail (EV_IS_PIXBUF_CACHE (pixbuf_cache), NULL);
1161
1162         page_cache = ev_page_cache_get (pixbuf_cache->document);
1163
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++) {
1167                 if (page < 0) {
1168                         page ++;
1169                         continue;
1170                 }
1171
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);
1179                 }
1180                 
1181                 page ++;
1182         }
1183
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);
1193                 }
1194                 
1195                 page ++;
1196         }
1197
1198         for (i = 0; i < pixbuf_cache->preload_cache_size; i++) {
1199                 if (page >= ev_page_cache_get_n_pages (page_cache))
1200                         break;
1201
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);
1209                 }
1210                 
1211                 page ++;
1212         }
1213
1214         return retval;
1215 }
1216
1217 void           
1218 ev_pixbuf_cache_reload_page (EvPixbufCache *pixbuf_cache,
1219                              GdkRegion     *region,
1220                              gint           page,
1221                              gint           rotation,
1222                              gdouble        scale)
1223 {
1224         CacheJobInfo *job_info;
1225         EvPageCache *page_cache;
1226         gint width, height;
1227
1228         job_info = find_job_cache (pixbuf_cache, page);
1229         if (job_info == NULL)
1230                 return;
1231         
1232         page_cache = ev_page_cache_get (pixbuf_cache->document);
1233         ev_page_cache_get_size (page_cache, page, rotation, scale,
1234                                 &width, &height);
1235
1236         add_job (pixbuf_cache, job_info, page_cache, region,
1237                  width, height, page, rotation, scale,
1238                  EV_JOB_PRIORITY_URGENT);
1239 }
1240
1241