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