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