]> www.fi.muni.cz Git - evince.git/blob - shell/ev-pixbuf-cache.c
Add EvPage so that we can hold a reference to the backend page. Form
[evince.git] / shell / ev-pixbuf-cache.c
1 #include <config.h>
2 #include "ev-pixbuf-cache.h"
3 #include "ev-job-queue.h"
4 #include "ev-page-cache.h"
5 #include "ev-document-images.h"
6 #include "ev-document-forms.h"
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_queue_remove_job (job_info->job);
167                 g_object_unref (G_OBJECT (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->rc->page->index < (pixbuf_cache->start_page - pixbuf_cache->preload_cache_size)) ||
254             (job_render->rc->page->index > (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->rc->page->index);
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->rc->page->index < (pixbuf_cache->start_page - pixbuf_cache->preload_cache_size)) ||
274             (job_render->rc->page->index > (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->rc->page->index);
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)->rc->page->index,
302                                 EV_JOB_RENDER (job_info->job)->rc->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_queue_remove_job (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               EvJobPriority  priority)
334 {
335         CacheJobInfo *target_page = NULL;
336         int page_offset;
337         EvJobPriority 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_HIGH;
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_queue_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_HIGH);
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->surface) {
458                 cairo_surface_destroy (job_info->surface);
459         }
460         job_info->surface = cairo_surface_reference (job_render->surface);
461
462         if (job_info->rc) {
463                 g_object_unref (G_OBJECT (job_info->rc));
464         }
465         job_info->rc = g_object_ref (job_render->rc);
466
467         job_info->points_set = FALSE;
468         if (job_render->include_selection) {
469                 if (job_info->selection) {
470                         cairo_surface_destroy (job_info->selection);
471                         job_info->selection = NULL;
472                 }
473                 if (job_info->selection_region) {
474                         gdk_region_destroy (job_info->selection_region);
475                         job_info->selection_region = NULL;
476                 }
477                 
478                 job_info->selection_points = job_render->selection_points;
479                 job_info->selection_region = gdk_region_copy (job_render->selection_region);
480                 job_info->selection = cairo_surface_reference (job_render->selection);
481                 g_assert (job_info->selection_points.x1 >= 0);
482                 job_info->points_set = TRUE;
483         }
484
485         if (job_info->job) {
486                 g_signal_handlers_disconnect_by_func (job_info->job,
487                                                       G_CALLBACK (job_page_ready_cb),
488                                                       pixbuf_cache);
489         }
490
491         job_info->page_ready = TRUE;
492 }
493
494 static void
495 copy_job_to_job_info (EvJobRender   *job_render,
496                       CacheJobInfo  *job_info,
497                       EvPixbufCache *pixbuf_cache)
498 {
499         if (job_render->include_links) {
500                 if (job_info->link_mapping)
501                         ev_link_mapping_free (job_info->link_mapping);
502                 job_info->link_mapping = job_render->link_mapping;
503         }
504
505         if (job_render->include_images) {
506                 if (job_info->image_mapping)
507                         ev_image_mapping_free (job_info->image_mapping);
508                 job_info->image_mapping = job_render->image_mapping;
509         }
510
511         if (job_render->include_forms) {
512                 if (job_info->form_field_mapping)
513                         ev_form_field_mapping_free (job_info->form_field_mapping);
514                 job_info->form_field_mapping = job_render->form_field_mapping;
515         }
516
517         if (job_render->include_text) {
518                 if (job_info->text_mapping)
519                         gdk_region_destroy (job_info->text_mapping);
520                 job_info->text_mapping = job_render->text_mapping;
521         }
522
523         if (job_info->job) {
524                 g_signal_handlers_disconnect_by_func (job_info->job,
525                                                       G_CALLBACK (job_finished_cb),
526                                                       pixbuf_cache);
527                 ev_job_queue_remove_job (job_info->job);
528                 g_object_unref (G_OBJECT (job_info->job));
529                 job_info->job = NULL;
530         }
531 }
532
533 static CacheJobInfo *
534 find_job_cache (EvPixbufCache *pixbuf_cache,
535                 int            page)
536 {
537         int page_offset;
538
539         if (page < (pixbuf_cache->start_page - pixbuf_cache->preload_cache_size) ||
540             page > (pixbuf_cache->end_page + pixbuf_cache->preload_cache_size))
541                 return NULL;
542
543         if (page < pixbuf_cache->start_page) {
544                 page_offset = (page - (pixbuf_cache->start_page - pixbuf_cache->preload_cache_size));
545
546                 g_assert (page_offset >= 0 &&
547                           page_offset < pixbuf_cache->preload_cache_size);
548                 return pixbuf_cache->prev_job + page_offset;
549         }
550
551         if (page > pixbuf_cache->end_page) {
552                 page_offset = (page - (pixbuf_cache->end_page + 1));
553
554                 g_assert (page_offset >= 0 &&
555                           page_offset < pixbuf_cache->preload_cache_size);
556                 return pixbuf_cache->next_job + page_offset;
557         }
558
559         page_offset = page - pixbuf_cache->start_page;
560         g_assert (page_offset >= 0 &&
561                   page_offset <= PAGE_CACHE_LEN(pixbuf_cache));
562         return pixbuf_cache->job_list + page_offset;
563 }
564
565 static void
566 ev_pixbuf_cache_clear_job_sizes (EvPixbufCache *pixbuf_cache,
567                                  gfloat         scale)
568 {
569         EvPageCache *page_cache;
570         int i;
571
572         page_cache = ev_page_cache_get (pixbuf_cache->document);
573
574         for (i = 0; i < PAGE_CACHE_LEN (pixbuf_cache); i++) {
575                 check_job_size_and_unref (pixbuf_cache, pixbuf_cache->job_list + i, page_cache, scale);
576         }
577
578         for (i = 0; i < pixbuf_cache->preload_cache_size; i++) {
579                 check_job_size_and_unref (pixbuf_cache, pixbuf_cache->prev_job + i, page_cache, scale);
580                 check_job_size_and_unref (pixbuf_cache, pixbuf_cache->next_job + i, page_cache, scale);
581         }
582 }
583
584 #define FIRST_VISABLE_PREV(pixbuf_cache) \
585         (MAX (0, pixbuf_cache->preload_cache_size + 1 - pixbuf_cache->start_page))
586
587 static void
588 get_selection_colors (GtkWidget *widget, GdkColor **text, GdkColor **base)
589 {
590     if (GTK_WIDGET_HAS_FOCUS (widget)) {
591         *text = &widget->style->text [GTK_STATE_SELECTED];
592         *base = &widget->style->base [GTK_STATE_SELECTED];
593     } else {
594         *text = &widget->style->text [GTK_STATE_ACTIVE];
595         *base = &widget->style->base [GTK_STATE_ACTIVE];
596     }
597 }
598
599 static void
600 add_job (EvPixbufCache *pixbuf_cache,
601          CacheJobInfo  *job_info,
602          EvPageCache   *page_cache,
603          GdkRegion     *region,
604          gint           width,
605          gint           height,
606          gint           page,
607          gint           rotation,
608          gfloat         scale,
609          EvJobPriority  priority)
610 {
611         EvPage  *ev_page;
612         gboolean include_links = FALSE;
613         gboolean include_text = FALSE;
614         gboolean include_selection = FALSE;
615         gboolean include_images = FALSE;
616         gboolean include_forms = FALSE;
617         GdkColor *text, *base;
618
619         job_info->page_ready = FALSE;
620         
621         /* FIXME: we shouldn't lock here */
622         ev_document_doc_mutex_lock ();
623         ev_page = ev_document_get_page (pixbuf_cache->document, page);
624         ev_document_doc_mutex_unlock ();
625         
626         if (job_info->rc == NULL) {
627                 job_info->rc = ev_render_context_new (ev_page, rotation, scale);
628         } else {
629                 ev_render_context_set_page (job_info->rc, ev_page);
630                 ev_render_context_set_rotation (job_info->rc, rotation);
631                 ev_render_context_set_scale (job_info->rc, scale);
632         }
633
634         g_object_unref (ev_page);
635
636         if (job_info->region)
637                 gdk_region_destroy (job_info->region);
638         job_info->region = region ? gdk_region_copy (region) : NULL;
639
640         /* Figure out what else we need for this job */
641         if (job_info->link_mapping == NULL)
642                 include_links = TRUE;
643         if (job_info->image_mapping == NULL)
644                 include_images = TRUE;
645         if (job_info->form_field_mapping == NULL)
646                 include_forms = TRUE;
647         if (job_info->text_mapping == NULL)
648                 include_text = TRUE;
649         if (new_selection_surface_needed (pixbuf_cache, job_info, page, scale)) {
650                 include_selection = TRUE;
651         }
652
653         gtk_widget_ensure_style (pixbuf_cache->view);
654
655         get_selection_colors (pixbuf_cache->view, &text, &base);
656
657         job_info->job = ev_job_render_new (pixbuf_cache->document,
658                                            job_info->rc,
659                                            width, height,
660                                            &(job_info->target_points),
661                                            job_info->selection_style,
662                                            text, base,
663                                            include_forms,
664                                            include_links,
665                                            include_images,
666                                            include_text,
667                                            include_selection);
668         ev_job_queue_add_job (job_info->job, priority);
669         g_signal_connect (G_OBJECT (job_info->job), "page-ready",
670                           G_CALLBACK (job_page_ready_cb),
671                           pixbuf_cache);
672         g_signal_connect (G_OBJECT (job_info->job), "finished",
673                           G_CALLBACK (job_finished_cb),
674                           pixbuf_cache);
675 }
676
677 static void
678 add_job_if_needed (EvPixbufCache *pixbuf_cache,
679                    CacheJobInfo  *job_info,
680                    EvPageCache   *page_cache,
681                    gint           page,
682                    gint           rotation,
683                    gfloat         scale,
684                    EvJobPriority  priority)
685 {
686         gint width, height;
687
688         if (job_info->job)
689                 return;
690
691         ev_page_cache_get_size (page_cache, page, rotation,
692                                 scale, &width, &height);
693
694         if (job_info->surface &&
695             cairo_image_surface_get_width (job_info->surface) == width &&
696             cairo_image_surface_get_height (job_info->surface) == height)
697                 return;
698
699         add_job (pixbuf_cache, job_info, page_cache, NULL,
700                  width, height, page, rotation, scale,
701                  priority);
702 }
703
704 static void
705 ev_pixbuf_cache_add_jobs_if_needed (EvPixbufCache *pixbuf_cache,
706                                     gint           rotation,
707                                     gfloat         scale)
708 {
709         EvPageCache *page_cache;
710         CacheJobInfo *job_info;
711         int page;
712         int i;
713
714         page_cache = ev_page_cache_get (pixbuf_cache->document);
715
716         for (i = 0; i < PAGE_CACHE_LEN (pixbuf_cache); i++) {
717                 job_info = (pixbuf_cache->job_list + i);
718                 page = pixbuf_cache->start_page + i;
719
720                 add_job_if_needed (pixbuf_cache, job_info,
721                                    page_cache, page, rotation, scale,
722                                    EV_JOB_PRIORITY_HIGH);
723         }
724
725         for (i = FIRST_VISABLE_PREV(pixbuf_cache); i < pixbuf_cache->preload_cache_size; i++) {
726                 job_info = (pixbuf_cache->prev_job + i);
727                 page = pixbuf_cache->start_page - pixbuf_cache->preload_cache_size + i;
728
729                 add_job_if_needed (pixbuf_cache, job_info,
730                                    page_cache, page, rotation, scale,
731                                    EV_JOB_PRIORITY_LOW);
732         }
733
734         for (i = 0; i < VISIBLE_NEXT_LEN(pixbuf_cache, page_cache); i++) {
735                 job_info = (pixbuf_cache->next_job + i);
736                 page = pixbuf_cache->end_page + 1 + i;
737
738                 add_job_if_needed (pixbuf_cache, job_info,
739                                    page_cache, page, rotation, scale,
740                                    EV_JOB_PRIORITY_LOW);
741         }
742
743 }
744
745 void
746 ev_pixbuf_cache_set_page_range (EvPixbufCache  *pixbuf_cache,
747                                 gint            start_page,
748                                 gint            end_page,
749                                 gint            rotation,
750                                 gfloat          scale,
751                                 GList          *selection_list)
752 {
753         EvPageCache *page_cache;
754
755         g_return_if_fail (EV_IS_PIXBUF_CACHE (pixbuf_cache));
756
757         page_cache = ev_page_cache_get (pixbuf_cache->document);
758
759         g_return_if_fail (start_page >= 0 && start_page < ev_page_cache_get_n_pages (page_cache));
760         g_return_if_fail (end_page >= 0 && end_page < ev_page_cache_get_n_pages (page_cache));
761         g_return_if_fail (end_page >= start_page);
762
763         /* First, resize the page_range as needed.  We cull old pages
764          * mercilessly. */
765         ev_pixbuf_cache_update_range (pixbuf_cache, start_page, end_page);
766
767         /* Then, we update the current jobs to see if any of them are the wrong
768          * size, we remove them if we need to. */
769         ev_pixbuf_cache_clear_job_sizes (pixbuf_cache, scale);
770
771         /* Next, we update the target selection for our pages */
772         ev_pixbuf_cache_set_selection_list (pixbuf_cache, selection_list);
773
774         /* Finally, we add the new jobs for all the sizes that don't have a
775          * pixbuf */
776         ev_pixbuf_cache_add_jobs_if_needed (pixbuf_cache, rotation, scale);
777 }
778
779 cairo_surface_t *
780 ev_pixbuf_cache_get_surface (EvPixbufCache *pixbuf_cache,
781                              gint           page)
782 {
783         CacheJobInfo *job_info;
784
785         job_info = find_job_cache (pixbuf_cache, page);
786         if (job_info == NULL)
787                 return NULL;
788
789         if (job_info->page_ready)
790                 return job_info->surface;
791         
792         /* We don't need to wait for the idle to handle the callback */
793         if (job_info->job &&
794             EV_JOB_RENDER (job_info->job)->page_ready) {
795                 copy_job_page_and_selection_to_job_info (EV_JOB_RENDER (job_info->job), job_info, pixbuf_cache);
796                 g_signal_emit (pixbuf_cache, signals[JOB_FINISHED], 0, job_info->region);
797         }
798
799         return job_info->surface;
800 }
801
802 GList *
803 ev_pixbuf_cache_get_link_mapping (EvPixbufCache *pixbuf_cache,
804                                   gint           page)
805 {
806         CacheJobInfo *job_info;
807
808         job_info = find_job_cache (pixbuf_cache, page);
809         if (job_info == NULL)
810                 return NULL;
811
812         /* We don't need to wait for the idle to handle the callback */
813         if (job_info->job &&
814             EV_JOB (job_info->job)->finished) {
815                 copy_job_to_job_info (EV_JOB_RENDER (job_info->job), job_info, pixbuf_cache);
816         }
817
818         return job_info->link_mapping;
819 }
820
821 GList *
822 ev_pixbuf_cache_get_image_mapping (EvPixbufCache *pixbuf_cache,
823                                    gint           page)
824 {
825         CacheJobInfo *job_info;
826
827         if (!EV_IS_DOCUMENT_IMAGES (pixbuf_cache->document))
828                 return NULL;
829         
830         job_info = find_job_cache (pixbuf_cache, page);
831         if (job_info == NULL)
832                 return NULL;
833
834         /* We don't need to wait for the idle to handle the callback */
835         if (job_info->job &&
836             EV_JOB (job_info->job)->finished) {
837                 copy_job_to_job_info (EV_JOB_RENDER (job_info->job), job_info, pixbuf_cache);
838         }
839
840         return job_info->image_mapping;
841 }
842
843 GList *
844 ev_pixbuf_cache_get_form_field_mapping (EvPixbufCache *pixbuf_cache,
845                                         gint           page)
846 {
847         CacheJobInfo *job_info;
848
849         if (!EV_IS_DOCUMENT_FORMS (pixbuf_cache->document))
850                 return NULL;
851         
852         job_info = find_job_cache (pixbuf_cache, page);
853         if (job_info == NULL)
854                 return NULL;
855
856         /* We don't need to wait for the idle to handle the callback */
857         if (job_info->job &&
858            EV_JOB (job_info->job)->finished) {
859                 copy_job_to_job_info (EV_JOB_RENDER(job_info->job), job_info, pixbuf_cache);
860         }
861         
862         return job_info->form_field_mapping;
863 }
864
865 static gboolean
866 new_selection_surface_needed (EvPixbufCache *pixbuf_cache,
867                              CacheJobInfo  *job_info,
868                              gint           page,
869                              gfloat         scale)
870 {
871         EvPageCache *page_cache;
872
873         if (job_info->selection) {
874                 gint width, height;
875                 gint selection_width, selection_height;
876                 
877                 page_cache = ev_page_cache_get (pixbuf_cache->document);
878                 ev_page_cache_get_size (page_cache, page,
879                                         job_info->rc->rotation,
880                                         scale, &width, &height);
881
882                 selection_width = cairo_image_surface_get_width (job_info->selection);
883                 selection_height = cairo_image_surface_get_height (job_info->selection);
884                 
885                 if (width != selection_width || height != selection_height)
886                         return TRUE;
887         } else {
888                 if (job_info->points_set)
889                         return TRUE;
890         }
891         
892         return FALSE;
893 }
894
895 static void
896 clear_selection_if_needed (EvPixbufCache *pixbuf_cache,
897                            CacheJobInfo  *job_info,
898                            gint           page,
899                            gfloat         scale)
900 {
901         if (new_selection_surface_needed (pixbuf_cache, job_info, page, scale)) {
902                 if (job_info->selection)
903                         cairo_surface_destroy (job_info->selection);
904                 job_info->selection = NULL;
905                 job_info->selection_points.x1 = -1;
906         }
907 }
908
909 GdkRegion *
910 ev_pixbuf_cache_get_text_mapping (EvPixbufCache *pixbuf_cache,
911                                   gint           page)
912 {
913         CacheJobInfo *job_info;
914
915         job_info = find_job_cache (pixbuf_cache, page);
916         if (job_info == NULL)
917                 return NULL;
918
919         /* We don't need to wait for the idle to handle the callback */
920         if (job_info->job &&
921             EV_JOB (job_info->job)->finished) {
922                 copy_job_to_job_info (EV_JOB_RENDER (job_info->job), job_info, pixbuf_cache);
923         }
924         
925         return job_info->text_mapping;
926 }
927
928 /* Clears the cache of jobs and pixbufs.
929  */
930 void
931 ev_pixbuf_cache_clear (EvPixbufCache *pixbuf_cache)
932 {
933         int i;
934
935         for (i = 0; i < pixbuf_cache->preload_cache_size; i++) {
936                 dispose_cache_job_info (pixbuf_cache->prev_job + i, pixbuf_cache);
937                 dispose_cache_job_info (pixbuf_cache->next_job + i, pixbuf_cache);
938         }
939
940         for (i = 0; i < PAGE_CACHE_LEN (pixbuf_cache); i++) {
941                 dispose_cache_job_info (pixbuf_cache->job_list + i, pixbuf_cache);
942         }
943 }
944
945
946 void
947 ev_pixbuf_cache_style_changed (EvPixbufCache *pixbuf_cache)
948 {
949         gint i;
950
951         /* FIXME: doesn't update running jobs. */
952         for (i = 0; i < pixbuf_cache->preload_cache_size; i++) {
953                 CacheJobInfo *job_info;
954
955                 job_info = pixbuf_cache->prev_job + i;
956                 if (job_info->selection) {
957                         cairo_surface_destroy (job_info->selection);
958                         job_info->selection = NULL;
959                 }
960
961                 job_info = pixbuf_cache->next_job + i;
962                 if (job_info->selection) {
963                         cairo_surface_destroy (job_info->selection);
964                         job_info->selection = NULL;
965                 }
966         }
967
968         for (i = 0; i < PAGE_CACHE_LEN (pixbuf_cache); i++) {
969                 CacheJobInfo *job_info;
970
971                 job_info = pixbuf_cache->job_list + i;
972                 if (job_info->selection) {
973                         cairo_surface_destroy (job_info->selection);
974                         job_info->selection = NULL;
975                 }
976         }
977 }
978
979 cairo_surface_t *
980 ev_pixbuf_cache_get_selection_surface (EvPixbufCache  *pixbuf_cache,
981                                        gint            page,
982                                        gfloat          scale,
983                                        GdkRegion     **region)
984 {
985         CacheJobInfo *job_info;
986
987         /* the document does not implement the selection interface */
988         if (!EV_IS_SELECTION (pixbuf_cache->document))
989                 return NULL;
990
991         job_info = find_job_cache (pixbuf_cache, page);
992         if (job_info == NULL)
993                 return NULL;
994
995         /* No selection on this page */
996         if (!job_info->points_set)
997                 return NULL;
998
999         /* Update the rc */
1000         g_assert (job_info->rc);
1001         ev_render_context_set_scale (job_info->rc, scale);
1002
1003         /* If we have a running job, we just return what we have under the
1004          * assumption that it'll be updated later and we can scale it as need
1005          * be */
1006         if (job_info->job && EV_JOB_RENDER (job_info->job)->include_selection)
1007                 return job_info->selection;
1008
1009         /* Now, lets see if we need to resize the image.  If we do, we clear the
1010          * old one. */
1011         clear_selection_if_needed (pixbuf_cache, job_info, page, scale);
1012
1013         /* Finally, we see if the two scales are the same, and get a new pixbuf
1014          * if needed.  We do this synchronously for now.  At some point, we
1015          * _should_ be able to get rid of the doc_mutex, so the synchronicity
1016          * doesn't kill us.  Rendering a few glyphs should really be fast.
1017          */
1018         if (ev_rect_cmp (&(job_info->target_points), &(job_info->selection_points))) {
1019                 EvRectangle *old_points;
1020                 GdkColor *text, *base;
1021
1022                 /* we need to get a new selection pixbuf */
1023                 ev_document_doc_mutex_lock ();
1024                 if (job_info->selection_points.x1 < 0) {
1025                         g_assert (job_info->selection == NULL);
1026                         old_points = NULL;
1027                 } else {
1028                         g_assert (job_info->selection != NULL);
1029                         old_points = &(job_info->selection_points);
1030                 }
1031
1032                 if (job_info->selection_region)
1033                         gdk_region_destroy (job_info->selection_region);
1034                 job_info->selection_region =
1035                         ev_selection_get_selection_region (EV_SELECTION (pixbuf_cache->document),
1036                                                            job_info->rc,
1037                                                            job_info->selection_style,
1038                                                            &(job_info->target_points));
1039
1040                 gtk_widget_ensure_style (pixbuf_cache->view);
1041
1042                 get_selection_colors (pixbuf_cache->view, &text, &base);
1043
1044                 ev_selection_render_selection (EV_SELECTION (pixbuf_cache->document),
1045                                                job_info->rc, &(job_info->selection),
1046                                                &(job_info->target_points),
1047                                                old_points,
1048                                                job_info->selection_style,
1049                                                text, base);
1050                 job_info->selection_points = job_info->target_points;
1051                 ev_document_doc_mutex_unlock ();
1052         }
1053         if (region)
1054                 *region = job_info->selection_region;
1055         return job_info->selection;
1056 }
1057
1058 static void
1059 update_job_selection (CacheJobInfo    *job_info,
1060                       EvViewSelection *selection)
1061 {
1062         job_info->points_set = TRUE;            
1063         job_info->target_points = selection->rect;
1064         job_info->selection_style = selection->style;
1065 }
1066
1067 static void
1068 clear_job_selection (CacheJobInfo *job_info)
1069 {
1070         job_info->points_set = FALSE;
1071         job_info->selection_points.x1 = -1;
1072
1073         if (job_info->selection) {
1074                 cairo_surface_destroy (job_info->selection);
1075                 job_info->selection = NULL;
1076         }
1077 }
1078
1079 /* This function will reset the selection on pages that no longer have them, and
1080  * will update the target_selection on those that need it.  It will _not_ free
1081  * the previous selection_list -- that's up to caller to do.
1082  */
1083 void
1084 ev_pixbuf_cache_set_selection_list (EvPixbufCache *pixbuf_cache,
1085                                     GList         *selection_list)
1086 {
1087         EvPageCache *page_cache;
1088         EvViewSelection *selection;
1089         GList *list = selection_list;
1090         int page;
1091         int i;
1092
1093         g_return_if_fail (EV_IS_PIXBUF_CACHE (pixbuf_cache));
1094
1095         if (!EV_IS_SELECTION (pixbuf_cache->document))
1096                 return;
1097
1098         page_cache = ev_page_cache_get (pixbuf_cache->document);
1099
1100         /* We check each area to see what needs updating, and what needs freeing; */
1101         page = pixbuf_cache->start_page - pixbuf_cache->preload_cache_size;
1102         for (i = 0; i < pixbuf_cache->preload_cache_size; i++) {
1103                 if (page < 0) {
1104                         page ++;
1105                         continue;
1106                 }
1107
1108                 selection = NULL;
1109                 while (list) {
1110                         if (((EvViewSelection *)list->data)->page == page) {
1111                                 selection = list->data;
1112                                 break;
1113                         } else if (((EvViewSelection *)list->data)->page > page) 
1114                                 break;
1115                         list = list->next;
1116                 }
1117
1118                 if (selection)
1119                         update_job_selection (pixbuf_cache->prev_job + i, selection);
1120                 else
1121                         clear_job_selection (pixbuf_cache->prev_job + i);
1122                 page ++;
1123         }
1124
1125         page = pixbuf_cache->start_page;
1126         for (i = 0; i < PAGE_CACHE_LEN (pixbuf_cache); i++) {
1127                 selection = NULL;
1128                 while (list) {
1129                         if (((EvViewSelection *)list->data)->page == page) {
1130                                 selection = list->data;
1131                                 break;
1132                         } else if (((EvViewSelection *)list->data)->page > page) 
1133                                 break;
1134                         list = list->next;
1135                 }
1136
1137                 if (selection)
1138                         update_job_selection (pixbuf_cache->job_list + i, selection);
1139                 else
1140                         clear_job_selection (pixbuf_cache->job_list + i);
1141                 page ++;
1142         }
1143
1144         for (i = 0; i < pixbuf_cache->preload_cache_size; i++) {
1145                 if (page >= ev_page_cache_get_n_pages (page_cache))
1146                         break;
1147
1148                 selection = NULL;
1149                 while (list) {
1150                         if (((EvViewSelection *)list->data)->page == page) {
1151                                 selection = list->data;
1152                                 break;
1153                         } else if (((EvViewSelection *)list->data)->page > page) 
1154                                 break;
1155                         list = list->next;
1156                 }
1157
1158                 if (selection)
1159                         update_job_selection (pixbuf_cache->next_job + i, selection);
1160                 else
1161                         clear_job_selection (pixbuf_cache->next_job + i);
1162                 page ++;
1163         }
1164 }
1165
1166
1167 /* Returns what the pixbuf cache thinks is */
1168
1169 GList *
1170 ev_pixbuf_cache_get_selection_list (EvPixbufCache *pixbuf_cache)
1171 {
1172         EvPageCache *page_cache;
1173         EvViewSelection *selection;
1174         GList *retval = NULL;
1175         int page;
1176         int i;
1177
1178         g_return_val_if_fail (EV_IS_PIXBUF_CACHE (pixbuf_cache), NULL);
1179
1180         page_cache = ev_page_cache_get (pixbuf_cache->document);
1181
1182         /* We check each area to see what needs updating, and what needs freeing; */
1183         page = pixbuf_cache->start_page - pixbuf_cache->preload_cache_size;
1184         for (i = 0; i < pixbuf_cache->preload_cache_size; i++) {
1185                 if (page < 0) {
1186                         page ++;
1187                         continue;
1188                 }
1189
1190                 if (pixbuf_cache->prev_job[i].selection_points.x1 != -1) {
1191                         selection = g_new0 (EvViewSelection, 1);
1192                         selection->page = page;
1193                         selection->rect = pixbuf_cache->prev_job[i].selection_points;
1194                         if (pixbuf_cache->prev_job[i].selection_region)
1195                                 selection->covered_region = gdk_region_copy (pixbuf_cache->prev_job[i].selection_region);
1196                         retval = g_list_append (retval, selection);
1197                 }
1198                 
1199                 page ++;
1200         }
1201
1202         page = pixbuf_cache->start_page;
1203         for (i = 0; i < PAGE_CACHE_LEN (pixbuf_cache); i++) {
1204                 if (pixbuf_cache->job_list[i].selection_points.x1 != -1) {
1205                         selection = g_new0 (EvViewSelection, 1);
1206                         selection->page = page;
1207                         selection->rect = pixbuf_cache->job_list[i].selection_points;
1208                         if (pixbuf_cache->job_list[i].selection_region)
1209                                 selection->covered_region = gdk_region_copy (pixbuf_cache->job_list[i].selection_region);
1210                         retval = g_list_append (retval, selection);
1211                 }
1212                 
1213                 page ++;
1214         }
1215
1216         for (i = 0; i < pixbuf_cache->preload_cache_size; i++) {
1217                 if (page >= ev_page_cache_get_n_pages (page_cache))
1218                         break;
1219
1220                 if (pixbuf_cache->next_job[i].selection_points.x1 != -1) {
1221                         selection = g_new0 (EvViewSelection, 1);
1222                         selection->page = page;
1223                         selection->rect = pixbuf_cache->next_job[i].selection_points;
1224                         if (pixbuf_cache->next_job[i].selection_region)
1225                                 selection->covered_region = gdk_region_copy (pixbuf_cache->next_job[i].selection_region);
1226                         retval = g_list_append (retval, selection);
1227                 }
1228                 
1229                 page ++;
1230         }
1231
1232         return retval;
1233 }
1234
1235 void           
1236 ev_pixbuf_cache_reload_page (EvPixbufCache *pixbuf_cache,
1237                              GdkRegion     *region,
1238                              gint           page,
1239                              gint           rotation,
1240                              gdouble        scale)
1241 {
1242         CacheJobInfo *job_info;
1243         EvPageCache *page_cache;
1244         gint width, height;
1245
1246         job_info = find_job_cache (pixbuf_cache, page);
1247         if (job_info == NULL)
1248                 return;
1249         
1250         page_cache = ev_page_cache_get (pixbuf_cache->document);
1251         ev_page_cache_get_size (page_cache, page, rotation, scale,
1252                                 &width, &height);
1253
1254         add_job (pixbuf_cache, job_info, page_cache, region,
1255                  width, height, page, rotation, scale,
1256                  EV_JOB_PRIORITY_HIGH);
1257 }
1258
1259