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