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