]> www.fi.muni.cz Git - evince.git/blob - shell/ev-pixbuf-cache.c
Really make use of the orientation bit of the render context. Use the
[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         GdkPixbuf *pixbuf;
10         EvRenderContext *rc;
11         GList *link_mapping;
12         GdkRegion *text_mapping;
13
14         /* Selection info.  If the *_points structs are unset, we put -1 in x1.
15          * selection_points are the coordinates encapsulated in selection.
16          * new_points is the target selection size. */
17         EvRectangle selection_points;
18         GdkPixbuf *selection;
19         EvRectangle new_points;
20 } CacheJobInfo;
21
22 struct _EvPixbufCache
23 {
24         GObject parent;
25
26         EvDocument  *document;
27         int start_page;
28         int end_page;
29
30         /* preload_cache_size is the number of pages prior to the current
31          * visible area that we cache.  It's normally 1, but could be 2 in the
32          * case of twin pages.
33          */
34         int preload_cache_size;
35         CacheJobInfo *prev_job;
36         CacheJobInfo *job_list;
37         CacheJobInfo *next_job;
38 };
39
40 struct _EvPixbufCacheClass
41 {
42         GObjectClass parent_class;
43
44         void (* job_finished) (EvPixbufCache *pixbuf_cache);
45 };
46
47
48 enum
49 {
50         JOB_FINISHED,
51         N_SIGNALS,
52 };
53
54 static guint signals[N_SIGNALS] = {0, };
55
56 static void          ev_pixbuf_cache_init       (EvPixbufCache      *pixbuf_cache);
57 static void          ev_pixbuf_cache_class_init (EvPixbufCacheClass *pixbuf_cache);
58 static void          ev_pixbuf_cache_finalize   (GObject            *object);
59 static void          ev_pixbuf_cache_dispose    (GObject            *object);
60 static void          job_finished_cb            (EvJob              *job,
61                                                  EvPixbufCache      *pixbuf_cache);
62 static CacheJobInfo *find_job_cache             (EvPixbufCache      *pixbuf_cache,
63                                                  int                 page);
64 static void          copy_job_to_job_info       (EvJobRender        *job_render,
65                                                  CacheJobInfo       *job_info,
66                                                  EvPixbufCache      *pixbuf_cache);
67 static gboolean      new_selection_pixbuf_needed(EvPixbufCache *pixbuf_cache,
68                                                  CacheJobInfo  *job_info,
69                                                  gint           page,
70                                                  gfloat         scale);
71
72
73 /* These are used for iterating through the prev and next arrays */
74 #define FIRST_VISABLE_PREV(pixbuf_cache) \
75         (MAX (0, pixbuf_cache->preload_cache_size + 1 - pixbuf_cache->start_page))
76 #define VISIBLE_NEXT_LEN(pixbuf_cache, page_cache) \
77         (MIN(pixbuf_cache->preload_cache_size, ev_page_cache_get_n_pages (page_cache) - (1 + pixbuf_cache->end_page)))
78 #define PAGE_CACHE_LEN(pixbuf_cache) \
79         ((pixbuf_cache->end_page - pixbuf_cache->start_page) + 1)
80
81 G_DEFINE_TYPE (EvPixbufCache, ev_pixbuf_cache, G_TYPE_OBJECT)
82
83 static void
84 ev_pixbuf_cache_init (EvPixbufCache *pixbuf_cache)
85 {
86         pixbuf_cache->start_page = 0;
87         pixbuf_cache->end_page = 0;
88         pixbuf_cache->job_list = g_new0 (CacheJobInfo, PAGE_CACHE_LEN (pixbuf_cache));
89
90         pixbuf_cache->preload_cache_size = 2;
91         pixbuf_cache->prev_job = g_new0 (CacheJobInfo, pixbuf_cache->preload_cache_size);
92         pixbuf_cache->next_job = g_new0 (CacheJobInfo, pixbuf_cache->preload_cache_size);
93 }
94
95 static void
96 ev_pixbuf_cache_class_init (EvPixbufCacheClass *class)
97 {
98         GObjectClass *object_class;
99
100         object_class = G_OBJECT_CLASS (class);
101
102         object_class->finalize = ev_pixbuf_cache_finalize;
103         object_class->dispose = ev_pixbuf_cache_dispose;
104
105         signals[JOB_FINISHED] = g_signal_new ("job-finished",
106                                             G_OBJECT_CLASS_TYPE (object_class),
107                                             G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
108                                             G_STRUCT_OFFSET (EvPixbufCacheClass, job_finished),
109                                             NULL, NULL,
110                                             g_cclosure_marshal_VOID__VOID,
111                                             G_TYPE_NONE, 0);
112 }
113
114 static void
115 ev_pixbuf_cache_finalize (GObject *object)
116 {
117         EvPixbufCache *pixbuf_cache;
118
119         pixbuf_cache = EV_PIXBUF_CACHE (object);
120
121         g_free (pixbuf_cache->prev_job);
122         g_free (pixbuf_cache->job_list);
123         g_free (pixbuf_cache->next_job);
124 }
125
126 static void
127 dispose_cache_job_info (CacheJobInfo *job_info,
128                         gpointer      data)
129 {
130         if (job_info == NULL)
131                 return;
132         if (job_info->job) {
133                 g_signal_handlers_disconnect_by_func (job_info->job,
134                                                       G_CALLBACK (job_finished_cb),
135                                                       data);
136                 ev_job_queue_remove_job (job_info->job);
137                 g_object_unref (G_OBJECT (job_info->job));
138                 job_info->job = NULL;
139         }
140         if (job_info->pixbuf) {
141                 g_object_unref (G_OBJECT (job_info->pixbuf));
142                 job_info->pixbuf = NULL;
143         }
144         if (job_info->link_mapping) {
145                 ev_link_mapping_free (job_info->link_mapping);
146                 job_info->link_mapping = NULL;
147         }
148         if (job_info->text_mapping) {
149                 gdk_region_destroy (job_info->text_mapping);
150                 job_info->text_mapping = NULL;
151         }
152         if (job_info->selection) {
153                 g_object_unref (G_OBJECT (job_info->selection));
154                 job_info->selection = NULL;
155         }
156
157         job_info->selection_points.x1 = -1;
158         job_info->new_points.x1 = -1;
159 }
160
161 static void
162 ev_pixbuf_cache_dispose (GObject *object)
163 {
164         EvPixbufCache *pixbuf_cache;
165         int i;
166
167         pixbuf_cache = EV_PIXBUF_CACHE (object);
168
169         for (i = 0; i < pixbuf_cache->preload_cache_size; i++) {
170                 dispose_cache_job_info (pixbuf_cache->prev_job + i, pixbuf_cache);
171                 dispose_cache_job_info (pixbuf_cache->next_job + i, pixbuf_cache);
172         }
173
174         for (i = 0; i < PAGE_CACHE_LEN (pixbuf_cache); i++) {
175                 dispose_cache_job_info (pixbuf_cache->job_list + i, pixbuf_cache);
176         }
177 }
178
179
180 EvPixbufCache *
181 ev_pixbuf_cache_new (EvDocument *document)
182 {
183         EvPixbufCache *pixbuf_cache;
184
185         pixbuf_cache = (EvPixbufCache *) g_object_new (EV_TYPE_PIXBUF_CACHE, NULL);
186         pixbuf_cache->document = document;
187
188         return pixbuf_cache;
189 }
190
191 static void
192 job_finished_cb (EvJob         *job,
193                  EvPixbufCache *pixbuf_cache)
194 {
195         CacheJobInfo *job_info;
196         EvJobRender *job_render = EV_JOB_RENDER (job);
197         GdkPixbuf *pixbuf;
198
199         /* If the job is outside of our interest, we silently discard it */
200         if ((job_render->rc->page < (pixbuf_cache->start_page - pixbuf_cache->preload_cache_size)) ||
201             (job_render->rc->page > (pixbuf_cache->end_page + pixbuf_cache->preload_cache_size))) {
202                 g_object_unref (job);
203                 return;
204         }
205         
206         job_info = find_job_cache (pixbuf_cache, job_render->rc->page);
207
208         pixbuf = g_object_ref (job_render->pixbuf);
209         if (job_info->pixbuf)
210                 g_object_unref (job_info->pixbuf);
211         job_info->pixbuf = pixbuf;
212
213         if (job_render->link_mapping) {
214                 if (job_info->link_mapping)
215                         ev_link_mapping_free (job_info->link_mapping);
216                 job_info->link_mapping = job_render->link_mapping;
217         }
218
219         if (job_render->text_mapping) {
220                 if (job_info->text_mapping)
221                         gdk_region_destroy (job_info->text_mapping);
222                 job_info->text_mapping = job_render->text_mapping;
223         }
224
225         if (job_render->include_selection) {
226                 if (job_info->selection)
227                         g_object_unref (job_info->selection);
228                 job_info->selection_points = job_render->selection_points;
229                 job_info->selection = job_render->selection;
230                 g_assert (job_info->selection_points.x1 >= 0);
231         }
232         
233         if (job_info->job == job)
234                 job_info->job = NULL;
235         g_object_unref (job);
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 (CacheJobInfo *job_info,
245                           EvPageCache  *page_cache,
246                           gfloat        scale)
247 {
248         gint width;
249         gint height;
250
251         g_assert (job_info);
252
253         if (job_info->job == NULL)
254                 return;
255
256         ev_page_cache_get_size (page_cache,
257                                 EV_JOB_RENDER (job_info->job)->rc->page,
258                                 EV_JOB_RENDER (job_info->job)->rc->orientation,
259                                 scale,
260                                 &width, &height);
261                                 
262         if (width == EV_JOB_RENDER (job_info->job)->target_width &&
263             height == EV_JOB_RENDER (job_info->job)->target_height)
264                 return;
265
266         /* Try to remove the job.  If we can't, then the thread has already
267          * picked it up and we are going get a signal when it's done.  If we
268          * can, then the job is fully dead and will never rnu.. */
269         if (ev_job_queue_remove_job (job_info->job))
270                 g_object_unref (job_info->job);
271
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
327         if (new_priority != priority && target_page->job) {
328                 ev_job_queue_update_job (target_page->job, new_priority);
329         }
330 }
331
332
333
334 static void
335 ev_pixbuf_cache_update_range (EvPixbufCache *pixbuf_cache,
336                               gint           start_page,
337                               gint           end_page)
338 {
339         CacheJobInfo *new_job_list;
340         CacheJobInfo *new_prev_job;
341         CacheJobInfo *new_next_job;
342         EvPageCache *page_cache;
343         int i, page;
344
345         if (pixbuf_cache->start_page == start_page &&
346             pixbuf_cache->end_page == end_page)
347                 return;
348
349         page_cache = ev_page_cache_get (pixbuf_cache->document);
350
351         new_job_list = g_new0 (CacheJobInfo, (end_page - start_page) + 1);
352         new_prev_job = g_new0 (CacheJobInfo, pixbuf_cache->preload_cache_size);
353         new_next_job = g_new0 (CacheJobInfo, pixbuf_cache->preload_cache_size);
354
355         /* We go through each job in the old cache and either clear it or move
356          * it to a new location. */
357
358         /* Start with the prev cache. */
359         page = pixbuf_cache->start_page - pixbuf_cache->preload_cache_size;
360         for (i = 0; i < pixbuf_cache->preload_cache_size; i++) {
361                 if (page < 0) {
362                         dispose_cache_job_info (pixbuf_cache->prev_job + i, pixbuf_cache);
363                 } else {
364                         move_one_job (pixbuf_cache->prev_job + i,
365                                       pixbuf_cache, page,
366                                       new_job_list, new_prev_job, new_next_job,
367                                       start_page, end_page, EV_JOB_PRIORITY_LOW);
368                 }
369                 page ++;
370         }
371
372         page = pixbuf_cache->start_page;
373         for (i = 0; i < PAGE_CACHE_LEN (pixbuf_cache); i++) {
374                 move_one_job (pixbuf_cache->job_list + i,
375                               pixbuf_cache, page,
376                               new_job_list, new_prev_job, new_next_job,
377                               start_page, end_page, EV_JOB_PRIORITY_HIGH);
378                 page ++;
379         }
380
381         for (i = 0; i < pixbuf_cache->preload_cache_size; i++) {
382                 if (page >= ev_page_cache_get_n_pages (page_cache)) {
383                         dispose_cache_job_info (pixbuf_cache->next_job + i, pixbuf_cache);
384                 } else {
385                         move_one_job (pixbuf_cache->next_job + i,
386                                       pixbuf_cache, page,
387                                       new_job_list, new_prev_job, new_next_job,
388                                       start_page, end_page, EV_JOB_PRIORITY_LOW);
389                 }
390                 page ++;
391         }
392
393         g_free (pixbuf_cache->job_list);
394         g_free (pixbuf_cache->prev_job);
395         g_free (pixbuf_cache->next_job);
396
397         pixbuf_cache->job_list = new_job_list;
398         pixbuf_cache->prev_job = new_prev_job;
399         pixbuf_cache->next_job = new_next_job;
400
401         pixbuf_cache->start_page = start_page;
402         pixbuf_cache->end_page = end_page;
403 }
404
405 static void
406 copy_job_to_job_info (EvJobRender   *job_render,
407                       CacheJobInfo  *job_info,
408                       EvPixbufCache *pixbuf_cache)
409 {
410         GdkPixbuf *pixbuf;
411
412         pixbuf = g_object_ref (job_render->pixbuf);
413
414         dispose_cache_job_info (job_info, pixbuf_cache);
415
416         job_info->pixbuf = pixbuf;
417         if (job_render->link_mapping)
418                 job_info->link_mapping = job_render->link_mapping;
419         if (job_render->text_mapping)
420                 job_info->text_mapping = job_render->text_mapping;      
421 }
422
423 static CacheJobInfo *
424 find_job_cache (EvPixbufCache *pixbuf_cache,
425                 int            page)
426 {
427         int page_offset;
428
429         if (page < (pixbuf_cache->start_page - pixbuf_cache->preload_cache_size) ||
430             page > (pixbuf_cache->end_page + pixbuf_cache->preload_cache_size))
431                 return NULL;
432
433         if (page < pixbuf_cache->start_page) {
434                 page_offset = (page - (pixbuf_cache->start_page - pixbuf_cache->preload_cache_size));
435
436                 g_assert (page_offset >= 0 &&
437                           page_offset < pixbuf_cache->preload_cache_size);
438                 return pixbuf_cache->prev_job + page_offset;
439         }
440
441         if (page > pixbuf_cache->end_page) {
442                 page_offset = (page - (pixbuf_cache->end_page + 1));
443
444                 g_assert (page_offset >= 0 &&
445                           page_offset < pixbuf_cache->preload_cache_size);
446                 return pixbuf_cache->next_job + page_offset;
447         }
448
449         page_offset = page - pixbuf_cache->start_page;
450         g_assert (page_offset >= 0 &&
451                   page_offset <= PAGE_CACHE_LEN(pixbuf_cache));
452         return pixbuf_cache->job_list + page_offset;
453 }
454
455 static void
456 ev_pixbuf_cache_clear_job_sizes (EvPixbufCache *pixbuf_cache,
457                                  gfloat         scale)
458 {
459         EvPageCache *page_cache;
460         int i;
461
462         page_cache = ev_page_cache_get (pixbuf_cache->document);
463
464         for (i = 0; i < PAGE_CACHE_LEN (pixbuf_cache); i++) {
465                 check_job_size_and_unref (pixbuf_cache->job_list + i, page_cache, scale);
466         }
467
468         for (i = 0; i < pixbuf_cache->preload_cache_size; i++) {
469                 check_job_size_and_unref (pixbuf_cache->prev_job + i, page_cache, scale);
470                 check_job_size_and_unref (pixbuf_cache->next_job + i, page_cache, scale);
471         }
472 }
473
474 #define FIRST_VISABLE_PREV(pixbuf_cache) \
475         (MAX (0, pixbuf_cache->preload_cache_size + 1 - pixbuf_cache->start_page))
476
477 static void
478 add_job_if_needed (EvPixbufCache *pixbuf_cache,
479                    CacheJobInfo  *job_info,
480                    EvPageCache   *page_cache,
481                    gint           page,
482                    EvOrientation  orientation,
483                    gfloat         scale,
484                    EvJobPriority  priority)
485 {
486         gboolean include_links = FALSE;
487         gboolean include_text = FALSE;
488         gboolean include_selection = FALSE;
489         int width, height;
490
491         if (job_info->job)
492                 return;
493
494         ev_page_cache_get_size (page_cache, page, orientation,
495                                 scale, &width, &height);
496
497         if (job_info->pixbuf &&
498             gdk_pixbuf_get_width (job_info->pixbuf) == width &&
499             gdk_pixbuf_get_height (job_info->pixbuf) == height)
500                 return;
501
502         /* make a new job now */
503         if (job_info->rc == NULL) {
504                 job_info->rc = ev_render_context_new (orientation, page, scale);
505         } else {
506                 ev_render_context_set_page (job_info->rc, page);
507                 ev_render_context_set_scale (job_info->rc, scale);
508                 ev_render_context_set_orientation (job_info->rc, orientation);
509         }
510
511         /* Figure out what else we need for this job */
512         if (job_info->link_mapping == NULL)
513                 include_links = TRUE;
514         if (job_info->text_mapping == NULL)
515                 include_text = TRUE;
516         if (new_selection_pixbuf_needed (pixbuf_cache, job_info, page, scale)) {
517                 include_selection = TRUE;
518         }
519
520         job_info->job = ev_job_render_new (pixbuf_cache->document,
521                                            job_info->rc,
522                                            width, height,
523                                            &(job_info->new_points),
524                                            include_links,
525                                            include_text,
526                                            include_selection);
527         ev_job_queue_add_job (job_info->job, priority);
528         g_signal_connect (job_info->job, "finished", G_CALLBACK (job_finished_cb), pixbuf_cache);
529 }
530
531
532 static void
533 ev_pixbuf_cache_add_jobs_if_needed (EvPixbufCache *pixbuf_cache,
534                                     EvOrientation  orientation,
535                                     gfloat         scale)
536 {
537         EvPageCache *page_cache;
538         CacheJobInfo *job_info;
539         int page;
540         int i;
541
542         page_cache = ev_page_cache_get (pixbuf_cache->document);
543
544         for (i = 0; i < PAGE_CACHE_LEN (pixbuf_cache); i++) {
545                 job_info = (pixbuf_cache->job_list + i);
546                 page = pixbuf_cache->start_page + i;
547
548                 add_job_if_needed (pixbuf_cache, job_info,
549                                    page_cache, page, orientation, scale,
550                                    EV_JOB_PRIORITY_HIGH);
551         }
552
553         for (i = FIRST_VISABLE_PREV(pixbuf_cache); i < pixbuf_cache->preload_cache_size; i++) {
554                 job_info = (pixbuf_cache->prev_job + i);
555                 page = pixbuf_cache->start_page - pixbuf_cache->preload_cache_size + i;
556
557                 add_job_if_needed (pixbuf_cache, job_info,
558                                    page_cache, page, orientation, scale,
559                                    EV_JOB_PRIORITY_LOW);
560         }
561
562         for (i = 0; i < VISIBLE_NEXT_LEN(pixbuf_cache, page_cache); i++) {
563                 job_info = (pixbuf_cache->next_job + i);
564                 page = pixbuf_cache->end_page + 1 + i;
565
566                 add_job_if_needed (pixbuf_cache, job_info,
567                                    page_cache, page, orientation, scale,
568                                    EV_JOB_PRIORITY_LOW);
569         }
570
571 }
572
573 void
574 ev_pixbuf_cache_set_page_range (EvPixbufCache *pixbuf_cache,
575                                 gint           start_page,
576                                 gint           end_page,
577                                 EvOrientation  orientation,
578                                 gfloat         scale,
579                                 GList          *selection_list)
580 {
581         EvPageCache *page_cache;
582
583         g_return_if_fail (EV_IS_PIXBUF_CACHE (pixbuf_cache));
584
585         page_cache = ev_page_cache_get (pixbuf_cache->document);
586
587         g_return_if_fail (start_page >= 0 && start_page < ev_page_cache_get_n_pages (page_cache));
588         g_return_if_fail (end_page >= 0 && end_page < ev_page_cache_get_n_pages (page_cache));
589         g_return_if_fail (end_page >= start_page);
590
591         /* First, resize the page_range as needed.  We cull old pages
592          * mercilessly. */
593         ev_pixbuf_cache_update_range (pixbuf_cache, start_page, end_page);
594
595         /* Then, we update the current jobs to see if any of them are the wrong
596          * size, we remove them if we need to. */
597         ev_pixbuf_cache_clear_job_sizes (pixbuf_cache, scale);
598
599         /* Next, we update the target selection for our pages */
600         ev_pixbuf_cache_set_selection_list (pixbuf_cache, selection_list);
601
602         /* Finally, we add the new jobs for all the sizes that don't have a
603          * pixbuf */
604         ev_pixbuf_cache_add_jobs_if_needed (pixbuf_cache, orientation, scale);
605 }
606
607 GdkPixbuf *
608 ev_pixbuf_cache_get_pixbuf (EvPixbufCache *pixbuf_cache,
609                             gint           page)
610 {
611         CacheJobInfo *job_info;
612
613         job_info = find_job_cache (pixbuf_cache, page);
614         if (job_info == NULL)
615                 return NULL;
616
617         /* We don't need to wait for the idle to handle the callback */
618         if (job_info->job &&
619             EV_JOB (job_info->job)->finished) {
620                 copy_job_to_job_info (EV_JOB_RENDER (job_info->job), job_info, pixbuf_cache);
621         }
622
623         return job_info->pixbuf;
624 }
625
626 GList *
627 ev_pixbuf_cache_get_link_mapping (EvPixbufCache *pixbuf_cache,
628                                   gint           page)
629 {
630         CacheJobInfo *job_info;
631
632         job_info = find_job_cache (pixbuf_cache, page);
633         if (job_info == NULL)
634                 return NULL;
635
636         /* We don't need to wait for the idle to handle the callback */
637         if (job_info->job &&
638             EV_JOB (job_info->job)->finished) {
639                 copy_job_to_job_info (EV_JOB_RENDER (job_info->job), job_info, pixbuf_cache);
640         }
641         
642         return job_info->link_mapping;
643 }
644
645 /* Selection */
646 static gboolean
647 new_selection_pixbuf_needed (EvPixbufCache *pixbuf_cache,
648                              CacheJobInfo  *job_info,
649                              gint           page,
650                              gfloat         scale)
651 {
652         EvPageCache *page_cache;
653         gint width, height;
654
655         if (job_info->selection) {
656                 page_cache = ev_page_cache_get (pixbuf_cache->document);
657                 ev_page_cache_get_size (page_cache, page, job_info->rc->orientation,
658                                         scale, &width, &height);
659                 
660                 if (width != gdk_pixbuf_get_width (job_info->selection) ||
661                     height != gdk_pixbuf_get_height (job_info->selection))
662                         return TRUE;
663         } else {
664                 if (job_info->new_points.x1 >= 0)
665                         return TRUE;
666         }
667         return FALSE;
668 }
669
670 static void
671 clear_selection_if_needed (EvPixbufCache *pixbuf_cache,
672                            CacheJobInfo  *job_info,
673                            gint           page,
674                            gfloat         scale)
675 {
676         if (new_selection_pixbuf_needed (pixbuf_cache, job_info, page, scale)) {
677                 if (job_info->selection)
678                         g_object_unref (job_info->selection);
679                 job_info->selection = NULL;
680                 job_info->selection_points.x1 = -1;
681         }
682 }
683
684 GdkRegion *
685 ev_pixbuf_cache_get_text_mapping      (EvPixbufCache *pixbuf_cache,
686                                       gint           page)
687 {
688         CacheJobInfo *job_info;
689
690         job_info = find_job_cache (pixbuf_cache, page);
691         if (job_info == NULL)
692                 return NULL;
693
694         /* We don't need to wait for the idle to handle the callback */
695         if (job_info->job &&
696             EV_JOB (job_info->job)->finished) {
697                 copy_job_to_job_info (EV_JOB_RENDER (job_info->job), job_info, pixbuf_cache);
698         }
699         
700         return job_info->text_mapping;
701 }
702
703 GdkPixbuf *
704 ev_pixbuf_cache_get_selection_pixbuf (EvPixbufCache *pixbuf_cache,
705                                       gint           page,
706                                       gfloat         scale)
707 {
708         CacheJobInfo *job_info;
709
710         /* the document does not implement the selection interface */
711         if (!EV_IS_SELECTION (pixbuf_cache->document))
712                 return NULL;
713
714         job_info = find_job_cache (pixbuf_cache, page);
715         if (job_info == NULL)
716                 return NULL;
717
718         /* No selection on this page */
719         if (job_info->new_points.x1 < 0)
720                 return NULL;
721
722         /* If we have a running job, we just return what we have under the
723          * assumption that it'll be updated later and we can scale it as need
724          * be */
725         if (job_info->job && EV_JOB_RENDER (job_info->job)->include_selection)
726                 return job_info->selection;
727
728         /* Now, lets see if we need to resize the image.  If we do, we clear the
729          * old one. */
730         clear_selection_if_needed (pixbuf_cache, job_info, page, scale);
731
732         /* Finally, we see if the two scales are the same, and get a new pixbuf
733          * if needed.  We do this synchronously for now.  At some point, we
734          * _should_ be able to get rid of the doc_mutex, so the synchronicity
735          * doesn't kill us.  Rendering a few glyphs should really be fast.
736          */
737         if (ev_rect_cmp (&(job_info->new_points), &(job_info->selection_points))) {
738                 EvRenderContext *rc;
739
740                 rc = ev_render_context_new (EV_ORIENTATION_PORTRAIT,
741                                             page,
742                                             scale);
743
744                 /* we need to get a new selection pixbuf */
745                 ev_document_doc_mutex_lock ();
746                 if (job_info->selection_points.x1 < 0) {
747                         g_assert (job_info->selection == NULL);
748                         ev_selection_render_selection (EV_SELECTION (pixbuf_cache->document),
749                                                        rc, &(job_info->selection),
750                                                        &(job_info->new_points),
751                                                        NULL);
752                 } else {
753                         g_assert (job_info->selection != NULL);
754                         ev_selection_render_selection (EV_SELECTION (pixbuf_cache->document),
755                                                        rc, &(job_info->selection),
756                                                        &(job_info->new_points),
757                                                        &(job_info->selection_points));
758                 }
759                 job_info->selection_points = job_info->new_points;
760                 ev_document_doc_mutex_unlock ();
761
762         }
763         return job_info->selection;
764 }
765
766
767 static void
768 update_job_selection (CacheJobInfo    *job_info,
769                       EvViewSelection *selection)
770 {
771         if (job_info->selection == NULL)
772                 job_info->selection_points.x1 = -1;
773         job_info->new_points = selection->rect;
774 }
775
776 static void
777 clear_job_selection (CacheJobInfo *job_info)
778 {
779         job_info->selection_points.x1 = -1;
780         job_info->new_points.x1 = -1;
781
782         if (job_info->selection) {
783                 g_object_unref (job_info->selection);
784                 job_info->selection = NULL;
785         }
786 }
787
788 /* This function will reset the selection on pages that no longer have them, and
789  * will update the target_selection on those that need it.
790  */
791 void
792 ev_pixbuf_cache_set_selection_list (EvPixbufCache *pixbuf_cache,
793                                     GList         *selection_list)
794 {
795         EvPageCache *page_cache;
796         EvViewSelection *selection;
797         GList *list = selection_list;
798         int page;
799         int i;
800
801         g_return_if_fail (EV_IS_PIXBUF_CACHE (pixbuf_cache));
802
803         page_cache = ev_page_cache_get (pixbuf_cache->document);
804
805         /* We check each area to see what needs updating, and what needs freeing; */
806         page = pixbuf_cache->start_page - pixbuf_cache->preload_cache_size;
807         for (i = 0; i < pixbuf_cache->preload_cache_size; i++) {
808                 if (page < 0) {
809                         page ++;
810                         continue;
811                 }
812
813                 selection = NULL;
814                 while (list) {
815                         if (((EvViewSelection *)list->data)->page == page) {
816                                 selection = list->data;
817                                 break;
818                         } else if (((EvViewSelection *)list->data)->page > page) 
819                                 break;
820                         list = list->next;
821                 }
822
823                 if (selection)
824                         update_job_selection (pixbuf_cache->prev_job + i, selection);
825                 else
826                         clear_job_selection (pixbuf_cache->prev_job + i);
827                 page ++;
828         }
829
830         page = pixbuf_cache->start_page;
831         for (i = 0; i < PAGE_CACHE_LEN (pixbuf_cache); i++) {
832                 selection = NULL;
833                 while (list) {
834                         if (((EvViewSelection *)list->data)->page == page) {
835                                 selection = list->data;
836                                 break;
837                         } else if (((EvViewSelection *)list->data)->page > page) 
838                                 break;
839                         list = list->next;
840                 }
841
842                 if (selection)
843                         update_job_selection (pixbuf_cache->job_list + i, selection);
844                 else
845                         clear_job_selection (pixbuf_cache->job_list + i);
846                 page ++;
847         }
848
849         for (i = 0; i < pixbuf_cache->preload_cache_size; i++) {
850                 if (page >= ev_page_cache_get_n_pages (page_cache))
851                         break;
852                 
853                 selection = NULL;
854                 while (list) {
855                         if (((EvViewSelection *)list->data)->page == page) {
856                                 selection = list->data;
857                                 break;
858                         } else if (((EvViewSelection *)list->data)->page > page) 
859                                 break;
860                         list = list->next;
861                 }
862
863                 if (selection)
864                         update_job_selection (pixbuf_cache->next_job + i, selection);
865                 else
866                         clear_job_selection (pixbuf_cache->next_job + i);
867                 page ++;
868         }
869 }