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