]> www.fi.muni.cz Git - evince.git/blob - libview/ev-pixbuf-cache.c
Move EvView specific code to a libeviview library so that it can be
[evince.git] / libview / 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-document-links.h"
8 #include "ev-image.h"
9 #include "ev-form-field.h"
10
11 typedef struct _CacheJobInfo
12 {
13         EvJob *job;
14         EvRenderContext *rc;
15         gboolean page_ready;
16
17         /* Region of the page that needs to be drawn */
18         GdkRegion *region; 
19
20         /* Data we get from rendering */
21         cairo_surface_t *surface;
22         GList *link_mapping;
23         GList *image_mapping;
24         GList *form_field_mapping;
25         GdkRegion *text_mapping;
26         
27         /* Selection data. 
28          * Selection_points are the coordinates encapsulated in selection.
29          * target_points is the target selection size. */
30         EvRectangle      selection_points;
31         EvRectangle      target_points;
32         EvSelectionStyle selection_style;
33         gboolean         points_set;
34         
35         cairo_surface_t *selection;
36         GdkRegion *selection_region;
37 } CacheJobInfo;
38
39 struct _EvPixbufCache
40 {
41         GObject parent;
42
43         /* We keep a link to our containing view just for style information. */
44         GtkWidget *view;
45         EvDocument *document;
46         int start_page;
47         int end_page;
48
49         /* preload_cache_size is the number of pages prior to the current
50          * visible area that we cache.  It's normally 1, but could be 2 in the
51          * case of twin pages.
52          */
53         int preload_cache_size;
54         CacheJobInfo *prev_job;
55         CacheJobInfo *job_list;
56         CacheJobInfo *next_job;
57 };
58
59 struct _EvPixbufCacheClass
60 {
61         GObjectClass parent_class;
62
63         void (* job_finished) (EvPixbufCache *pixbuf_cache);
64 };
65
66
67 enum
68 {
69         JOB_FINISHED,
70         N_SIGNALS,
71 };
72
73 static guint signals[N_SIGNALS] = {0, };
74
75 static void          ev_pixbuf_cache_init       (EvPixbufCache      *pixbuf_cache);
76 static void          ev_pixbuf_cache_class_init (EvPixbufCacheClass *pixbuf_cache);
77 static void          ev_pixbuf_cache_finalize   (GObject            *object);
78 static void          ev_pixbuf_cache_dispose    (GObject            *object);
79 static void          job_page_ready_cb          (EvJob              *job,
80                                                  EvPixbufCache      *pixbuf_cache);
81 static void          job_finished_cb            (EvJob              *job,
82                                                  EvPixbufCache      *pixbuf_cache);
83 static CacheJobInfo *find_job_cache             (EvPixbufCache      *pixbuf_cache,
84                                                  int                 page);
85 static void          copy_job_to_job_info       (EvJobRender        *job_render,
86                                                  CacheJobInfo       *job_info,
87                                                  EvPixbufCache      *pixbuf_cache);
88 static void          copy_job_page_and_selection_to_job_info (EvJobRender        *job_render,
89                                                               CacheJobInfo       *job_info,
90                                                               EvPixbufCache      *pixbuf_cache);
91 static gboolean      new_selection_surface_needed(EvPixbufCache      *pixbuf_cache,
92                                                   CacheJobInfo       *job_info,
93                                                   gint                page,
94                                                   gfloat              scale);
95
96
97 /* These are used for iterating through the prev and next arrays */
98 #define FIRST_VISIBLE_PREV(pixbuf_cache) \
99         (MAX (0, pixbuf_cache->preload_cache_size - pixbuf_cache->start_page))
100 #define VISIBLE_NEXT_LEN(pixbuf_cache, page_cache) \
101         (MIN(pixbuf_cache->preload_cache_size, ev_page_cache_get_n_pages (page_cache) - (1 + pixbuf_cache->end_page)))
102 #define PAGE_CACHE_LEN(pixbuf_cache) \
103         ((pixbuf_cache->end_page - pixbuf_cache->start_page) + 1)
104
105 G_DEFINE_TYPE (EvPixbufCache, ev_pixbuf_cache, G_TYPE_OBJECT)
106
107 static void
108 ev_pixbuf_cache_init (EvPixbufCache *pixbuf_cache)
109 {
110         pixbuf_cache->start_page = 0;
111         pixbuf_cache->end_page = 0;
112         pixbuf_cache->job_list = g_new0 (CacheJobInfo, PAGE_CACHE_LEN (pixbuf_cache));
113
114         pixbuf_cache->preload_cache_size = 2;
115         pixbuf_cache->prev_job = g_new0 (CacheJobInfo, pixbuf_cache->preload_cache_size);
116         pixbuf_cache->next_job = g_new0 (CacheJobInfo, pixbuf_cache->preload_cache_size);
117 }
118
119 static void
120 ev_pixbuf_cache_class_init (EvPixbufCacheClass *class)
121 {
122         GObjectClass *object_class;
123
124         object_class = G_OBJECT_CLASS (class);
125
126         object_class->finalize = ev_pixbuf_cache_finalize;
127         object_class->dispose = ev_pixbuf_cache_dispose;
128
129         signals[JOB_FINISHED] =
130                 g_signal_new ("job-finished",
131                               G_OBJECT_CLASS_TYPE (object_class),
132                               G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
133                               G_STRUCT_OFFSET (EvPixbufCacheClass, job_finished),
134                               NULL, NULL,
135                               g_cclosure_marshal_VOID__POINTER,
136                               G_TYPE_NONE, 1,
137                               G_TYPE_POINTER);
138 }
139
140 static void
141 ev_pixbuf_cache_finalize (GObject *object)
142 {
143         EvPixbufCache *pixbuf_cache;
144
145         pixbuf_cache = EV_PIXBUF_CACHE (object);
146
147         g_free (pixbuf_cache->prev_job);
148         g_free (pixbuf_cache->job_list);
149         g_free (pixbuf_cache->next_job);
150
151         G_OBJECT_CLASS (ev_pixbuf_cache_parent_class)->finalize (object);
152 }
153
154 static void
155 dispose_cache_job_info (CacheJobInfo *job_info,
156                         gpointer      data)
157 {
158         if (job_info == NULL)
159                 return;
160         if (job_info->job) {
161                 g_signal_handlers_disconnect_by_func (job_info->job,
162                                                       G_CALLBACK (job_page_ready_cb),
163                                                       data);
164                 g_signal_handlers_disconnect_by_func (job_info->job,
165                                                       G_CALLBACK (job_finished_cb),
166                                                       data);
167                 ev_job_cancel (job_info->job);
168                 g_object_unref (job_info->job);
169                 job_info->job = NULL;
170         }
171         if (job_info->surface) {
172                 cairo_surface_destroy (job_info->surface);
173                 job_info->surface = NULL;
174         }
175         if (job_info->region) {
176                 gdk_region_destroy (job_info->region);
177                 job_info->region = NULL;
178         }
179         if (job_info->link_mapping) {
180                 ev_link_mapping_free (job_info->link_mapping);
181                 job_info->link_mapping = NULL;
182         }
183         if (job_info->image_mapping) {
184                 ev_image_mapping_free (job_info->image_mapping);
185                 job_info->image_mapping = NULL;
186         }
187         if (job_info->form_field_mapping) {
188                 ev_form_field_mapping_free (job_info->form_field_mapping);
189                 job_info->form_field_mapping = NULL;
190         }
191         if (job_info->text_mapping) {
192                 gdk_region_destroy (job_info->text_mapping);
193                 job_info->text_mapping = NULL;
194         }
195         if (job_info->selection) {
196                 cairo_surface_destroy (job_info->selection);
197                 job_info->selection = NULL;
198         }
199         if (job_info->selection_region) {
200                 gdk_region_destroy (job_info->selection_region);
201                 job_info->selection_region = NULL;
202         }
203         if (job_info->rc) {
204                 g_object_unref (G_OBJECT (job_info->rc));
205                 job_info->rc = NULL;
206         }
207
208         job_info->points_set = FALSE;
209 }
210
211 static void
212 ev_pixbuf_cache_dispose (GObject *object)
213 {
214         EvPixbufCache *pixbuf_cache;
215         int i;
216
217         pixbuf_cache = EV_PIXBUF_CACHE (object);
218
219         for (i = 0; i < pixbuf_cache->preload_cache_size; i++) {
220                 dispose_cache_job_info (pixbuf_cache->prev_job + i, pixbuf_cache);
221                 dispose_cache_job_info (pixbuf_cache->next_job + i, pixbuf_cache);
222         }
223
224         for (i = 0; i < PAGE_CACHE_LEN (pixbuf_cache); i++) {
225                 dispose_cache_job_info (pixbuf_cache->job_list + i, pixbuf_cache);
226         }
227
228         G_OBJECT_CLASS (ev_pixbuf_cache_parent_class)->dispose (object);
229 }
230
231
232 EvPixbufCache *
233 ev_pixbuf_cache_new (GtkWidget  *view,
234                      EvDocument *document)
235 {
236         EvPixbufCache *pixbuf_cache;
237
238         pixbuf_cache = (EvPixbufCache *) g_object_new (EV_TYPE_PIXBUF_CACHE, NULL);
239         /* This is a backlink, so we don't ref this */ 
240         pixbuf_cache->view = view;
241         pixbuf_cache->document = document;
242
243         return pixbuf_cache;
244 }
245
246 static void
247 job_page_ready_cb (EvJob         *job,
248                    EvPixbufCache *pixbuf_cache)
249 {
250         CacheJobInfo *job_info;
251         EvJobRender *job_render = EV_JOB_RENDER (job);
252
253         /* If the job is outside of our interest, we silently discard it */
254         if ((job_render->page < (pixbuf_cache->start_page - pixbuf_cache->preload_cache_size)) ||
255             (job_render->page > (pixbuf_cache->end_page + pixbuf_cache->preload_cache_size))) {
256                 g_object_unref (job);
257                 return;
258         }
259
260         job_info = find_job_cache (pixbuf_cache, job_render->page);
261
262         copy_job_page_and_selection_to_job_info (job_render, job_info, pixbuf_cache);
263         g_signal_emit (pixbuf_cache, signals[JOB_FINISHED], 0, job_info->region);
264 }
265
266 static void
267 job_finished_cb (EvJob         *job,
268                  EvPixbufCache *pixbuf_cache)
269 {
270         CacheJobInfo *job_info;
271         EvJobRender *job_render = EV_JOB_RENDER (job);
272
273         /* If the job is outside of our interest, we silently discard it */
274         if ((job_render->page < (pixbuf_cache->start_page - pixbuf_cache->preload_cache_size)) ||
275             (job_render->page > (pixbuf_cache->end_page + pixbuf_cache->preload_cache_size))) {
276                 g_object_unref (job);
277                 return;
278         }
279
280         job_info = find_job_cache (pixbuf_cache, job_render->page);
281         copy_job_to_job_info (job_render, job_info, pixbuf_cache);
282 }
283
284 /* This checks a job to see if the job would generate the right sized pixbuf
285  * given a scale.  If it won't, it removes the job and clears it to NULL.
286  */
287 static void
288 check_job_size_and_unref (EvPixbufCache *pixbuf_cache,
289                           CacheJobInfo *job_info,
290                           EvPageCache  *page_cache,
291                           gfloat        scale)
292 {
293         gint width;
294         gint height;
295
296         g_assert (job_info);
297
298         if (job_info->job == NULL)
299                 return;
300
301         ev_page_cache_get_size (page_cache,
302                                 EV_JOB_RENDER (job_info->job)->page,
303                                 EV_JOB_RENDER (job_info->job)->rotation,
304                                 scale,
305                                 &width, &height);
306                                 
307         if (width == EV_JOB_RENDER (job_info->job)->target_width &&
308             height == EV_JOB_RENDER (job_info->job)->target_height)
309                 return;
310
311         g_signal_handlers_disconnect_by_func (job_info->job,
312                                               G_CALLBACK (job_page_ready_cb),
313                                               pixbuf_cache);
314         g_signal_handlers_disconnect_by_func (job_info->job,
315                                               G_CALLBACK (job_finished_cb),
316                                               pixbuf_cache);
317         ev_job_cancel (job_info->job);
318         g_object_unref (job_info->job);
319         job_info->job = NULL;
320 }
321
322 /* Do all function that copies a job from an older cache to it's position in the
323  * new cache.  It clears the old job if it doesn't have a place.
324  */
325 static void
326 move_one_job (CacheJobInfo  *job_info,
327               EvPixbufCache *pixbuf_cache,
328               int            page,
329               CacheJobInfo  *new_job_list,
330               CacheJobInfo  *new_prev_job,
331               CacheJobInfo  *new_next_job,
332               int            start_page,
333               int            end_page,
334               gint           priority)
335 {
336         CacheJobInfo *target_page = NULL;
337         int page_offset;
338         gint new_priority;
339
340         if (page < (start_page - pixbuf_cache->preload_cache_size) ||
341             page > (end_page + pixbuf_cache->preload_cache_size)) {
342                 dispose_cache_job_info (job_info, pixbuf_cache);
343                 return;
344         }
345
346         /* find the target page to copy it over to. */
347         if (page < start_page) {
348                 page_offset = (page - (start_page - pixbuf_cache->preload_cache_size));
349
350                 g_assert (page_offset >= 0 &&
351                           page_offset < pixbuf_cache->preload_cache_size);
352                 target_page = new_prev_job + page_offset;
353                 new_priority = EV_JOB_PRIORITY_LOW;
354         } else if (page > end_page) {
355                 page_offset = (page - (end_page + 1));
356
357                 g_assert (page_offset >= 0 &&
358                           page_offset < pixbuf_cache->preload_cache_size);
359                 target_page = new_next_job + page_offset;
360                 new_priority = EV_JOB_PRIORITY_LOW;
361         } else {
362                 page_offset = page - start_page;
363                 g_assert (page_offset >= 0 &&
364                           page_offset <= ((end_page - start_page) + 1));
365                 new_priority = EV_JOB_PRIORITY_URGENT;
366                 target_page = new_job_list + page_offset;
367         }
368
369         *target_page = *job_info;
370         job_info->job = NULL;
371         job_info->region = NULL;
372         job_info->surface = NULL;
373         job_info->link_mapping = NULL;
374         job_info->image_mapping = NULL;
375         job_info->form_field_mapping = NULL;
376
377         if (new_priority != priority && target_page->job) {
378                 ev_job_scheduler_update_job (target_page->job, new_priority);
379         }
380 }
381
382 static void
383 ev_pixbuf_cache_update_range (EvPixbufCache *pixbuf_cache,
384                               gint           start_page,
385                               gint           end_page)
386 {
387         CacheJobInfo *new_job_list;
388         CacheJobInfo *new_prev_job;
389         CacheJobInfo *new_next_job;
390         EvPageCache *page_cache;
391         int i, page;
392
393         if (pixbuf_cache->start_page == start_page &&
394             pixbuf_cache->end_page == end_page)
395                 return;
396
397         page_cache = ev_page_cache_get (pixbuf_cache->document);
398
399         new_job_list = g_new0 (CacheJobInfo, (end_page - start_page) + 1);
400         new_prev_job = g_new0 (CacheJobInfo, pixbuf_cache->preload_cache_size);
401         new_next_job = g_new0 (CacheJobInfo, pixbuf_cache->preload_cache_size);
402
403         /* We go through each job in the old cache and either clear it or move
404          * it to a new location. */
405
406         /* Start with the prev cache. */
407         page = pixbuf_cache->start_page - pixbuf_cache->preload_cache_size;
408         for (i = 0; i < pixbuf_cache->preload_cache_size; i++) {
409                 if (page < 0) {
410                         dispose_cache_job_info (pixbuf_cache->prev_job + i, pixbuf_cache);
411                 } else {
412                         move_one_job (pixbuf_cache->prev_job + i,
413                                       pixbuf_cache, page,
414                                       new_job_list, new_prev_job, new_next_job,
415                                       start_page, end_page, EV_JOB_PRIORITY_LOW);
416                 }
417                 page ++;
418         }
419
420         page = pixbuf_cache->start_page;
421         for (i = 0; i < PAGE_CACHE_LEN (pixbuf_cache); i++) {
422                 move_one_job (pixbuf_cache->job_list + i,
423                               pixbuf_cache, page,
424                               new_job_list, new_prev_job, new_next_job,
425                               start_page, end_page, EV_JOB_PRIORITY_URGENT);
426                 page ++;
427         }
428
429         for (i = 0; i < pixbuf_cache->preload_cache_size; i++) {
430                 if (page >= ev_page_cache_get_n_pages (page_cache)) {
431                         dispose_cache_job_info (pixbuf_cache->next_job + i, pixbuf_cache);
432                 } else {
433                         move_one_job (pixbuf_cache->next_job + i,
434                                       pixbuf_cache, page,
435                                       new_job_list, new_prev_job, new_next_job,
436                                       start_page, end_page, EV_JOB_PRIORITY_LOW);
437                 }
438                 page ++;
439         }
440
441         g_free (pixbuf_cache->job_list);
442         g_free (pixbuf_cache->prev_job);
443         g_free (pixbuf_cache->next_job);
444
445         pixbuf_cache->job_list = new_job_list;
446         pixbuf_cache->prev_job = new_prev_job;
447         pixbuf_cache->next_job = new_next_job;
448
449         pixbuf_cache->start_page = start_page;
450         pixbuf_cache->end_page = end_page;
451 }
452
453 static void
454 copy_job_page_and_selection_to_job_info (EvJobRender   *job_render,
455                                          CacheJobInfo  *job_info,
456                                          EvPixbufCache *pixbuf_cache)
457 {
458         if (job_info->rc == NULL) {
459                 job_info->rc = ev_render_context_new (job_render->ev_page,
460                                                       job_render->rotation,
461                                                       job_render->scale);
462         } else {
463                 ev_render_context_set_page (job_info->rc, job_render->ev_page);
464                 ev_render_context_set_rotation (job_info->rc, job_render->rotation);
465                 ev_render_context_set_scale (job_info->rc, job_render->scale);
466         }
467         
468         if (job_info->surface) {
469                 cairo_surface_destroy (job_info->surface);
470         }
471         job_info->surface = cairo_surface_reference (job_render->surface);
472
473         job_info->points_set = FALSE;
474         if (job_render->flags & EV_RENDER_INCLUDE_SELECTION) {
475                 if (job_info->selection) {
476                         cairo_surface_destroy (job_info->selection);
477                         job_info->selection = NULL;
478                 }
479                 if (job_info->selection_region) {
480                         gdk_region_destroy (job_info->selection_region);
481                         job_info->selection_region = NULL;
482                 }
483                 
484                 job_info->selection_points = job_render->selection_points;
485                 job_info->selection_region = gdk_region_copy (job_render->selection_region);
486                 job_info->selection = cairo_surface_reference (job_render->selection);
487                 g_assert (job_info->selection_points.x1 >= 0);
488                 job_info->points_set = TRUE;
489         }
490
491         if (job_info->job) {
492                 g_signal_handlers_disconnect_by_func (job_info->job,
493                                                       G_CALLBACK (job_page_ready_cb),
494                                                       pixbuf_cache);
495         }
496
497         job_info->page_ready = TRUE;
498 }
499
500 static void
501 copy_job_to_job_info (EvJobRender   *job_render,
502                       CacheJobInfo  *job_info,
503                       EvPixbufCache *pixbuf_cache)
504 {
505         if (!job_info->page_ready) {
506                 g_signal_emit (pixbuf_cache, signals[JOB_FINISHED], 0, job_info->region);
507                 copy_job_page_and_selection_to_job_info (job_render,
508                                                          job_info,
509                                                          pixbuf_cache);
510         }
511         
512         if (job_render->flags & EV_RENDER_INCLUDE_LINKS) {
513                 if (job_info->link_mapping)
514                         ev_link_mapping_free (job_info->link_mapping);
515                 job_info->link_mapping = job_render->link_mapping;
516         }
517
518         if (job_render->flags & EV_RENDER_INCLUDE_IMAGES) {
519                 if (job_info->image_mapping)
520                         ev_image_mapping_free (job_info->image_mapping);
521                 job_info->image_mapping = job_render->image_mapping;
522         }
523
524         if (job_render->flags & EV_RENDER_INCLUDE_FORMS) {
525                 if (job_info->form_field_mapping)
526                         ev_form_field_mapping_free (job_info->form_field_mapping);
527                 job_info->form_field_mapping = job_render->form_field_mapping;
528         }
529
530         if (job_render->flags & EV_RENDER_INCLUDE_TEXT) {
531                 if (job_info->text_mapping)
532                         gdk_region_destroy (job_info->text_mapping);
533                 job_info->text_mapping = job_render->text_mapping;
534         }
535
536         if (job_info->job) {
537                 g_signal_handlers_disconnect_by_func (job_info->job,
538                                                       G_CALLBACK (job_finished_cb),
539                                                       pixbuf_cache);
540                 ev_job_cancel (job_info->job);
541                 g_object_unref (job_info->job);
542                 job_info->job = NULL;
543         }
544 }
545
546 static CacheJobInfo *
547 find_job_cache (EvPixbufCache *pixbuf_cache,
548                 int            page)
549 {
550         int page_offset;
551
552         if (page < (pixbuf_cache->start_page - pixbuf_cache->preload_cache_size) ||
553             page > (pixbuf_cache->end_page + pixbuf_cache->preload_cache_size))
554                 return NULL;
555
556         if (page < pixbuf_cache->start_page) {
557                 page_offset = (page - (pixbuf_cache->start_page - pixbuf_cache->preload_cache_size));
558
559                 g_assert (page_offset >= 0 &&
560                           page_offset < pixbuf_cache->preload_cache_size);
561                 return pixbuf_cache->prev_job + page_offset;
562         }
563
564         if (page > pixbuf_cache->end_page) {
565                 page_offset = (page - (pixbuf_cache->end_page + 1));
566
567                 g_assert (page_offset >= 0 &&
568                           page_offset < pixbuf_cache->preload_cache_size);
569                 return pixbuf_cache->next_job + page_offset;
570         }
571
572         page_offset = page - pixbuf_cache->start_page;
573         g_assert (page_offset >= 0 &&
574                   page_offset <= PAGE_CACHE_LEN(pixbuf_cache));
575         return pixbuf_cache->job_list + page_offset;
576 }
577
578 static void
579 ev_pixbuf_cache_clear_job_sizes (EvPixbufCache *pixbuf_cache,
580                                  gfloat         scale)
581 {
582         EvPageCache *page_cache;
583         int i;
584
585         page_cache = ev_page_cache_get (pixbuf_cache->document);
586
587         for (i = 0; i < PAGE_CACHE_LEN (pixbuf_cache); i++) {
588                 check_job_size_and_unref (pixbuf_cache, pixbuf_cache->job_list + i, page_cache, scale);
589         }
590
591         for (i = 0; i < pixbuf_cache->preload_cache_size; i++) {
592                 check_job_size_and_unref (pixbuf_cache, pixbuf_cache->prev_job + i, page_cache, scale);
593                 check_job_size_and_unref (pixbuf_cache, pixbuf_cache->next_job + i, page_cache, scale);
594         }
595 }
596
597 static void
598 get_selection_colors (GtkWidget *widget, GdkColor **text, GdkColor **base)
599 {
600     if (GTK_WIDGET_HAS_FOCUS (widget)) {
601         *text = &widget->style->text [GTK_STATE_SELECTED];
602         *base = &widget->style->base [GTK_STATE_SELECTED];
603     } else {
604         *text = &widget->style->text [GTK_STATE_ACTIVE];
605         *base = &widget->style->base [GTK_STATE_ACTIVE];
606     }
607 }
608
609 static void
610 add_job (EvPixbufCache *pixbuf_cache,
611          CacheJobInfo  *job_info,
612          EvPageCache   *page_cache,
613          GdkRegion     *region,
614          gint           width,
615          gint           height,
616          gint           page,
617          gint           rotation,
618          gfloat         scale,
619          EvJobPriority  priority)
620 {
621         EvRenderFlags flags = 0;
622
623         job_info->page_ready = FALSE;
624         
625         if (job_info->region)
626                 gdk_region_destroy (job_info->region);
627         job_info->region = region ? gdk_region_copy (region) : NULL;
628
629         /* Figure out what else we need for this job */
630         if (job_info->link_mapping == NULL)
631                 flags |= EV_RENDER_INCLUDE_LINKS;
632         if (job_info->image_mapping == NULL)
633                 flags |= EV_RENDER_INCLUDE_IMAGES;
634         if (job_info->form_field_mapping == NULL)
635                 flags |= EV_RENDER_INCLUDE_FORMS;
636         if (job_info->text_mapping == NULL)
637                 flags |= EV_RENDER_INCLUDE_TEXT;
638
639         job_info->job = ev_job_render_new (pixbuf_cache->document,
640                                            page, rotation, scale,
641                                            width, height,
642                                            flags);
643         
644         if (new_selection_surface_needed (pixbuf_cache, job_info, page, scale)) {
645                 GdkColor *text, *base;
646
647                 gtk_widget_ensure_style (pixbuf_cache->view);
648                 get_selection_colors (pixbuf_cache->view, &text, &base);
649                 ev_job_render_set_selection_info (EV_JOB_RENDER (job_info->job), 
650                                                   &(job_info->target_points),
651                                                   job_info->selection_style,
652                                                   text, base);
653         }
654
655         g_signal_connect (G_OBJECT (job_info->job), "page-ready",
656                           G_CALLBACK (job_page_ready_cb),
657                           pixbuf_cache);
658         g_signal_connect (G_OBJECT (job_info->job), "finished",
659                           G_CALLBACK (job_finished_cb),
660                           pixbuf_cache);
661         ev_job_scheduler_push_job (job_info->job, priority);
662 }
663
664 static void
665 add_job_if_needed (EvPixbufCache *pixbuf_cache,
666                    CacheJobInfo  *job_info,
667                    EvPageCache   *page_cache,
668                    gint           page,
669                    gint           rotation,
670                    gfloat         scale,
671                    EvJobPriority  priority)
672 {
673         gint width, height;
674
675         if (job_info->job)
676                 return;
677
678         ev_page_cache_get_size (page_cache, page, rotation,
679                                 scale, &width, &height);
680
681         if (job_info->surface &&
682             cairo_image_surface_get_width (job_info->surface) == width &&
683             cairo_image_surface_get_height (job_info->surface) == height)
684                 return;
685
686         add_job (pixbuf_cache, job_info, page_cache, NULL,
687                  width, height, page, rotation, scale,
688                  priority);
689 }
690
691 static void
692 ev_pixbuf_cache_add_jobs_if_needed (EvPixbufCache *pixbuf_cache,
693                                     gint           rotation,
694                                     gfloat         scale)
695 {
696         EvPageCache *page_cache;
697         CacheJobInfo *job_info;
698         int page;
699         int i;
700
701         page_cache = ev_page_cache_get (pixbuf_cache->document);
702
703         for (i = 0; i < PAGE_CACHE_LEN (pixbuf_cache); i++) {
704                 job_info = (pixbuf_cache->job_list + i);
705                 page = pixbuf_cache->start_page + i;
706
707                 add_job_if_needed (pixbuf_cache, job_info,
708                                    page_cache, page, rotation, scale,
709                                    EV_JOB_PRIORITY_URGENT);
710         }
711
712         for (i = FIRST_VISIBLE_PREV(pixbuf_cache); i < pixbuf_cache->preload_cache_size; i++) {
713                 job_info = (pixbuf_cache->prev_job + i);
714                 page = pixbuf_cache->start_page - pixbuf_cache->preload_cache_size + i;
715
716                 add_job_if_needed (pixbuf_cache, job_info,
717                                    page_cache, page, rotation, scale,
718                                    EV_JOB_PRIORITY_LOW);
719         }
720
721         for (i = 0; i < VISIBLE_NEXT_LEN(pixbuf_cache, page_cache); i++) {
722                 job_info = (pixbuf_cache->next_job + i);
723                 page = pixbuf_cache->end_page + 1 + i;
724
725                 add_job_if_needed (pixbuf_cache, job_info,
726                                    page_cache, page, rotation, scale,
727                                    EV_JOB_PRIORITY_LOW);
728         }
729
730 }
731
732 void
733 ev_pixbuf_cache_set_page_range (EvPixbufCache  *pixbuf_cache,
734                                 gint            start_page,
735                                 gint            end_page,
736                                 gint            rotation,
737                                 gfloat          scale,
738                                 GList          *selection_list)
739 {
740         EvPageCache *page_cache;
741
742         g_return_if_fail (EV_IS_PIXBUF_CACHE (pixbuf_cache));
743
744         page_cache = ev_page_cache_get (pixbuf_cache->document);
745
746         g_return_if_fail (start_page >= 0 && start_page < ev_page_cache_get_n_pages (page_cache));
747         g_return_if_fail (end_page >= 0 && end_page < ev_page_cache_get_n_pages (page_cache));
748         g_return_if_fail (end_page >= start_page);
749
750         /* First, resize the page_range as needed.  We cull old pages
751          * mercilessly. */
752         ev_pixbuf_cache_update_range (pixbuf_cache, start_page, end_page);
753
754         /* Then, we update the current jobs to see if any of them are the wrong
755          * size, we remove them if we need to. */
756         ev_pixbuf_cache_clear_job_sizes (pixbuf_cache, scale);
757
758         /* Next, we update the target selection for our pages */
759         ev_pixbuf_cache_set_selection_list (pixbuf_cache, selection_list);
760
761         /* Finally, we add the new jobs for all the sizes that don't have a
762          * pixbuf */
763         ev_pixbuf_cache_add_jobs_if_needed (pixbuf_cache, rotation, scale);
764 }
765
766 cairo_surface_t *
767 ev_pixbuf_cache_get_surface (EvPixbufCache *pixbuf_cache,
768                              gint           page)
769 {
770         CacheJobInfo *job_info;
771
772         job_info = find_job_cache (pixbuf_cache, page);
773         if (job_info == NULL)
774                 return NULL;
775
776         if (job_info->page_ready)
777                 return job_info->surface;
778         
779         /* We don't need to wait for the idle to handle the callback */
780         if (job_info->job &&
781             EV_JOB_RENDER (job_info->job)->page_ready) {
782                 copy_job_page_and_selection_to_job_info (EV_JOB_RENDER (job_info->job), job_info, pixbuf_cache);
783                 g_signal_emit (pixbuf_cache, signals[JOB_FINISHED], 0, job_info->region);
784         }
785
786         return job_info->surface;
787 }
788
789 GList *
790 ev_pixbuf_cache_get_link_mapping (EvPixbufCache *pixbuf_cache,
791                                   gint           page)
792 {
793         CacheJobInfo *job_info;
794
795         job_info = find_job_cache (pixbuf_cache, page);
796         if (job_info == NULL)
797                 return NULL;
798
799         /* We don't need to wait for the idle to handle the callback */
800         if (job_info->job &&
801             EV_JOB (job_info->job)->finished) {
802                 copy_job_to_job_info (EV_JOB_RENDER (job_info->job), job_info, pixbuf_cache);
803         }
804
805         return job_info->link_mapping;
806 }
807
808 GList *
809 ev_pixbuf_cache_get_image_mapping (EvPixbufCache *pixbuf_cache,
810                                    gint           page)
811 {
812         CacheJobInfo *job_info;
813
814         if (!EV_IS_DOCUMENT_IMAGES (pixbuf_cache->document))
815                 return NULL;
816         
817         job_info = find_job_cache (pixbuf_cache, page);
818         if (job_info == NULL)
819                 return NULL;
820
821         /* We don't need to wait for the idle to handle the callback */
822         if (job_info->job &&
823             EV_JOB (job_info->job)->finished) {
824                 copy_job_to_job_info (EV_JOB_RENDER (job_info->job), job_info, pixbuf_cache);
825         }
826
827         return job_info->image_mapping;
828 }
829
830 GList *
831 ev_pixbuf_cache_get_form_field_mapping (EvPixbufCache *pixbuf_cache,
832                                         gint           page)
833 {
834         CacheJobInfo *job_info;
835
836         if (!EV_IS_DOCUMENT_FORMS (pixbuf_cache->document))
837                 return NULL;
838         
839         job_info = find_job_cache (pixbuf_cache, page);
840         if (job_info == NULL)
841                 return NULL;
842
843         /* We don't need to wait for the idle to handle the callback */
844         if (job_info->job &&
845            EV_JOB (job_info->job)->finished) {
846                 copy_job_to_job_info (EV_JOB_RENDER(job_info->job), job_info, pixbuf_cache);
847         }
848         
849         return job_info->form_field_mapping;
850 }
851
852 static gboolean
853 new_selection_surface_needed (EvPixbufCache *pixbuf_cache,
854                               CacheJobInfo  *job_info,
855                               gint           page,
856                               gfloat         scale)
857 {
858         EvPageCache *page_cache;
859
860         if (job_info->selection && job_info->rc) {
861                 gint width, height;
862                 gint selection_width, selection_height;
863                 
864                 page_cache = ev_page_cache_get (pixbuf_cache->document);
865                 ev_page_cache_get_size (page_cache, page,
866                                         job_info->rc->rotation,
867                                         scale, &width, &height);
868
869                 selection_width = cairo_image_surface_get_width (job_info->selection);
870                 selection_height = cairo_image_surface_get_height (job_info->selection);
871                 
872                 if (width != selection_width || height != selection_height)
873                         return TRUE;
874         } else {
875                 if (job_info->points_set)
876                         return TRUE;
877         }
878         
879         return FALSE;
880 }
881
882 static void
883 clear_selection_if_needed (EvPixbufCache *pixbuf_cache,
884                            CacheJobInfo  *job_info,
885                            gint           page,
886                            gfloat         scale)
887 {
888         if (new_selection_surface_needed (pixbuf_cache, job_info, page, scale)) {
889                 if (job_info->selection)
890                         cairo_surface_destroy (job_info->selection);
891                 job_info->selection = NULL;
892                 job_info->selection_points.x1 = -1;
893         }
894 }
895
896 GdkRegion *
897 ev_pixbuf_cache_get_text_mapping (EvPixbufCache *pixbuf_cache,
898                                   gint           page)
899 {
900         CacheJobInfo *job_info;
901
902         job_info = find_job_cache (pixbuf_cache, page);
903         if (job_info == NULL)
904                 return NULL;
905
906         /* We don't need to wait for the idle to handle the callback */
907         if (job_info->job &&
908             EV_JOB (job_info->job)->finished) {
909                 copy_job_to_job_info (EV_JOB_RENDER (job_info->job), job_info, pixbuf_cache);
910         }
911         
912         return job_info->text_mapping;
913 }
914
915 /* Clears the cache of jobs and pixbufs.
916  */
917 void
918 ev_pixbuf_cache_clear (EvPixbufCache *pixbuf_cache)
919 {
920         int i;
921
922         for (i = 0; i < pixbuf_cache->preload_cache_size; i++) {
923                 dispose_cache_job_info (pixbuf_cache->prev_job + i, pixbuf_cache);
924                 dispose_cache_job_info (pixbuf_cache->next_job + i, pixbuf_cache);
925         }
926
927         for (i = 0; i < PAGE_CACHE_LEN (pixbuf_cache); i++) {
928                 dispose_cache_job_info (pixbuf_cache->job_list + i, pixbuf_cache);
929         }
930 }
931
932
933 void
934 ev_pixbuf_cache_style_changed (EvPixbufCache *pixbuf_cache)
935 {
936         gint i;
937
938         /* FIXME: doesn't update running jobs. */
939         for (i = 0; i < pixbuf_cache->preload_cache_size; i++) {
940                 CacheJobInfo *job_info;
941
942                 job_info = pixbuf_cache->prev_job + i;
943                 if (job_info->selection) {
944                         cairo_surface_destroy (job_info->selection);
945                         job_info->selection = NULL;
946                 }
947
948                 job_info = pixbuf_cache->next_job + i;
949                 if (job_info->selection) {
950                         cairo_surface_destroy (job_info->selection);
951                         job_info->selection = NULL;
952                 }
953         }
954
955         for (i = 0; i < PAGE_CACHE_LEN (pixbuf_cache); i++) {
956                 CacheJobInfo *job_info;
957
958                 job_info = pixbuf_cache->job_list + i;
959                 if (job_info->selection) {
960                         cairo_surface_destroy (job_info->selection);
961                         job_info->selection = NULL;
962                 }
963         }
964 }
965
966 cairo_surface_t *
967 ev_pixbuf_cache_get_selection_surface (EvPixbufCache  *pixbuf_cache,
968                                        gint            page,
969                                        gfloat          scale,
970                                        GdkRegion     **region)
971 {
972         CacheJobInfo *job_info;
973
974         /* the document does not implement the selection interface */
975         if (!EV_IS_SELECTION (pixbuf_cache->document))
976                 return NULL;
977
978         job_info = find_job_cache (pixbuf_cache, page);
979         if (job_info == NULL)
980                 return NULL;
981
982         /* No selection on this page */
983         if (!job_info->points_set)
984                 return NULL;
985
986         /* Create new render context if needed (selection + fast scrolling) */
987         if (job_info->rc == NULL) {
988                 EvPage  *ev_page;
989                 ev_page = ev_document_get_page (pixbuf_cache->document, page);
990                 job_info->rc = ev_render_context_new (ev_page, 0, scale);
991                 g_object_unref (ev_page);
992         }
993
994         /* Update the rc */
995         ev_render_context_set_scale (job_info->rc, scale);
996
997         /* If we have a running job, we just return what we have under the
998          * assumption that it'll be updated later and we can scale it as need
999          * be */
1000         if (job_info->job && (EV_JOB_RENDER (job_info->job)->flags & EV_RENDER_INCLUDE_SELECTION))
1001                 return job_info->selection;
1002
1003         /* Now, lets see if we need to resize the image.  If we do, we clear the
1004          * old one. */
1005         clear_selection_if_needed (pixbuf_cache, job_info, page, scale);
1006
1007         /* Finally, we see if the two scales are the same, and get a new pixbuf
1008          * if needed.  We do this synchronously for now.  At some point, we
1009          * _should_ be able to get rid of the doc_mutex, so the synchronicity
1010          * doesn't kill us.  Rendering a few glyphs should really be fast.
1011          */
1012         if (ev_rect_cmp (&(job_info->target_points), &(job_info->selection_points))) {
1013                 EvRectangle *old_points;
1014                 GdkColor *text, *base;
1015
1016                 /* we need to get a new selection pixbuf */
1017                 ev_document_doc_mutex_lock ();
1018                 if (job_info->selection_points.x1 < 0) {
1019                         g_assert (job_info->selection == NULL);
1020                         old_points = NULL;
1021                 } else {
1022                         g_assert (job_info->selection != NULL);
1023                         old_points = &(job_info->selection_points);
1024                 }
1025
1026                 if (job_info->selection_region)
1027                         gdk_region_destroy (job_info->selection_region);
1028                 job_info->selection_region =
1029                         ev_selection_get_selection_region (EV_SELECTION (pixbuf_cache->document),
1030                                                            job_info->rc,
1031                                                            job_info->selection_style,
1032                                                            &(job_info->target_points));
1033
1034                 gtk_widget_ensure_style (pixbuf_cache->view);
1035
1036                 get_selection_colors (pixbuf_cache->view, &text, &base);
1037
1038                 ev_selection_render_selection (EV_SELECTION (pixbuf_cache->document),
1039                                                job_info->rc, &(job_info->selection),
1040                                                &(job_info->target_points),
1041                                                old_points,
1042                                                job_info->selection_style,
1043                                                text, base);
1044                 job_info->selection_points = job_info->target_points;
1045                 ev_document_doc_mutex_unlock ();
1046         }
1047         if (region)
1048                 *region = job_info->selection_region;
1049         return job_info->selection;
1050 }
1051
1052 static void
1053 update_job_selection (CacheJobInfo    *job_info,
1054                       EvViewSelection *selection)
1055 {
1056         job_info->points_set = TRUE;            
1057         job_info->target_points = selection->rect;
1058         job_info->selection_style = selection->style;
1059 }
1060
1061 static void
1062 clear_job_selection (CacheJobInfo *job_info)
1063 {
1064         job_info->points_set = FALSE;
1065         job_info->selection_points.x1 = -1;
1066
1067         if (job_info->selection) {
1068                 cairo_surface_destroy (job_info->selection);
1069                 job_info->selection = NULL;
1070         }
1071 }
1072
1073 /* This function will reset the selection on pages that no longer have them, and
1074  * will update the target_selection on those that need it.  It will _not_ free
1075  * the previous selection_list -- that's up to caller to do.
1076  */
1077 void
1078 ev_pixbuf_cache_set_selection_list (EvPixbufCache *pixbuf_cache,
1079                                     GList         *selection_list)
1080 {
1081         EvPageCache *page_cache;
1082         EvViewSelection *selection;
1083         GList *list = selection_list;
1084         int page;
1085         int i;
1086
1087         g_return_if_fail (EV_IS_PIXBUF_CACHE (pixbuf_cache));
1088
1089         if (!EV_IS_SELECTION (pixbuf_cache->document))
1090                 return;
1091
1092         page_cache = ev_page_cache_get (pixbuf_cache->document);
1093
1094         /* We check each area to see what needs updating, and what needs freeing; */
1095         page = pixbuf_cache->start_page - pixbuf_cache->preload_cache_size;
1096         for (i = 0; i < pixbuf_cache->preload_cache_size; i++) {
1097                 if (page < 0) {
1098                         page ++;
1099                         continue;
1100                 }
1101
1102                 selection = NULL;
1103                 while (list) {
1104                         if (((EvViewSelection *)list->data)->page == page) {
1105                                 selection = list->data;
1106                                 break;
1107                         } else if (((EvViewSelection *)list->data)->page > page) 
1108                                 break;
1109                         list = list->next;
1110                 }
1111
1112                 if (selection)
1113                         update_job_selection (pixbuf_cache->prev_job + i, selection);
1114                 else
1115                         clear_job_selection (pixbuf_cache->prev_job + i);
1116                 page ++;
1117         }
1118
1119         page = pixbuf_cache->start_page;
1120         for (i = 0; i < PAGE_CACHE_LEN (pixbuf_cache); i++) {
1121                 selection = NULL;
1122                 while (list) {
1123                         if (((EvViewSelection *)list->data)->page == page) {
1124                                 selection = list->data;
1125                                 break;
1126                         } else if (((EvViewSelection *)list->data)->page > page) 
1127                                 break;
1128                         list = list->next;
1129                 }
1130
1131                 if (selection)
1132                         update_job_selection (pixbuf_cache->job_list + i, selection);
1133                 else
1134                         clear_job_selection (pixbuf_cache->job_list + i);
1135                 page ++;
1136         }
1137
1138         for (i = 0; i < pixbuf_cache->preload_cache_size; i++) {
1139                 if (page >= ev_page_cache_get_n_pages (page_cache))
1140                         break;
1141
1142                 selection = NULL;
1143                 while (list) {
1144                         if (((EvViewSelection *)list->data)->page == page) {
1145                                 selection = list->data;
1146                                 break;
1147                         } else if (((EvViewSelection *)list->data)->page > page) 
1148                                 break;
1149                         list = list->next;
1150                 }
1151
1152                 if (selection)
1153                         update_job_selection (pixbuf_cache->next_job + i, selection);
1154                 else
1155                         clear_job_selection (pixbuf_cache->next_job + i);
1156                 page ++;
1157         }
1158 }
1159
1160
1161 /* Returns what the pixbuf cache thinks is */
1162
1163 GList *
1164 ev_pixbuf_cache_get_selection_list (EvPixbufCache *pixbuf_cache)
1165 {
1166         EvPageCache *page_cache;
1167         EvViewSelection *selection;
1168         GList *retval = NULL;
1169         int page;
1170         int i;
1171
1172         g_return_val_if_fail (EV_IS_PIXBUF_CACHE (pixbuf_cache), NULL);
1173
1174         page_cache = ev_page_cache_get (pixbuf_cache->document);
1175
1176         /* We check each area to see what needs updating, and what needs freeing; */
1177         page = pixbuf_cache->start_page - pixbuf_cache->preload_cache_size;
1178         for (i = 0; i < pixbuf_cache->preload_cache_size; i++) {
1179                 if (page < 0) {
1180                         page ++;
1181                         continue;
1182                 }
1183
1184                 if (pixbuf_cache->prev_job[i].selection_points.x1 != -1) {
1185                         selection = g_new0 (EvViewSelection, 1);
1186                         selection->page = page;
1187                         selection->rect = pixbuf_cache->prev_job[i].selection_points;
1188                         if (pixbuf_cache->prev_job[i].selection_region)
1189                                 selection->covered_region = gdk_region_copy (pixbuf_cache->prev_job[i].selection_region);
1190                         retval = g_list_append (retval, selection);
1191                 }
1192                 
1193                 page ++;
1194         }
1195
1196         page = pixbuf_cache->start_page;
1197         for (i = 0; i < PAGE_CACHE_LEN (pixbuf_cache); i++) {
1198                 if (pixbuf_cache->job_list[i].selection_points.x1 != -1) {
1199                         selection = g_new0 (EvViewSelection, 1);
1200                         selection->page = page;
1201                         selection->rect = pixbuf_cache->job_list[i].selection_points;
1202                         if (pixbuf_cache->job_list[i].selection_region)
1203                                 selection->covered_region = gdk_region_copy (pixbuf_cache->job_list[i].selection_region);
1204                         retval = g_list_append (retval, selection);
1205                 }
1206                 
1207                 page ++;
1208         }
1209
1210         for (i = 0; i < pixbuf_cache->preload_cache_size; i++) {
1211                 if (page >= ev_page_cache_get_n_pages (page_cache))
1212                         break;
1213
1214                 if (pixbuf_cache->next_job[i].selection_points.x1 != -1) {
1215                         selection = g_new0 (EvViewSelection, 1);
1216                         selection->page = page;
1217                         selection->rect = pixbuf_cache->next_job[i].selection_points;
1218                         if (pixbuf_cache->next_job[i].selection_region)
1219                                 selection->covered_region = gdk_region_copy (pixbuf_cache->next_job[i].selection_region);
1220                         retval = g_list_append (retval, selection);
1221                 }
1222                 
1223                 page ++;
1224         }
1225
1226         return retval;
1227 }
1228
1229 void           
1230 ev_pixbuf_cache_reload_page (EvPixbufCache *pixbuf_cache,
1231                              GdkRegion     *region,
1232                              gint           page,
1233                              gint           rotation,
1234                              gdouble        scale)
1235 {
1236         CacheJobInfo *job_info;
1237         EvPageCache *page_cache;
1238         gint width, height;
1239
1240         job_info = find_job_cache (pixbuf_cache, page);
1241         if (job_info == NULL)
1242                 return;
1243         
1244         page_cache = ev_page_cache_get (pixbuf_cache->document);
1245         ev_page_cache_get_size (page_cache, page, rotation, scale,
1246                                 &width, &height);
1247
1248         add_job (pixbuf_cache, job_info, page_cache, region,
1249                  width, height, page, rotation, scale,
1250                  EV_JOB_PRIORITY_URGENT);
1251 }
1252
1253