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