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