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