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