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