]> www.fi.muni.cz Git - evince.git/blob - shell/ev-pixbuf-cache.c
Use cairo image surfaces instead of GDK pixbufs for drawing pages and
[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         cairo_surface_t *surface;
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         cairo_surface_t *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_surface_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->surface) {
153                 cairo_surface_destroy (job_info->surface);
154                 job_info->surface = 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                 cairo_surface_destroy (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->surface = 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->surface) {
415                 cairo_surface_destroy (job_info->surface);
416         }
417         job_info->surface = cairo_surface_reference (job_render->surface);
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                         cairo_surface_destroy (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 = cairo_surface_reference (job_render->selection);
455                 g_assert (job_info->selection_points.x1 >= 0);
456                 job_info->points_set = TRUE;
457         }
458
459         if (job_info->job) {
460                 g_signal_handlers_disconnect_by_func (job_info->job,
461                                                       G_CALLBACK (job_finished_cb),
462                                                       pixbuf_cache);
463                 ev_job_queue_remove_job (job_info->job);
464                 g_object_unref (G_OBJECT (job_info->job));
465                 job_info->job = NULL;
466         }
467 }
468
469 static CacheJobInfo *
470 find_job_cache (EvPixbufCache *pixbuf_cache,
471                 int            page)
472 {
473         int page_offset;
474
475         if (page < (pixbuf_cache->start_page - pixbuf_cache->preload_cache_size) ||
476             page > (pixbuf_cache->end_page + pixbuf_cache->preload_cache_size))
477                 return NULL;
478
479         if (page < pixbuf_cache->start_page) {
480                 page_offset = (page - (pixbuf_cache->start_page - pixbuf_cache->preload_cache_size));
481
482                 g_assert (page_offset >= 0 &&
483                           page_offset < pixbuf_cache->preload_cache_size);
484                 return pixbuf_cache->prev_job + page_offset;
485         }
486
487         if (page > pixbuf_cache->end_page) {
488                 page_offset = (page - (pixbuf_cache->end_page + 1));
489
490                 g_assert (page_offset >= 0 &&
491                           page_offset < pixbuf_cache->preload_cache_size);
492                 return pixbuf_cache->next_job + page_offset;
493         }
494
495         page_offset = page - pixbuf_cache->start_page;
496         g_assert (page_offset >= 0 &&
497                   page_offset <= PAGE_CACHE_LEN(pixbuf_cache));
498         return pixbuf_cache->job_list + page_offset;
499 }
500
501 static void
502 ev_pixbuf_cache_clear_job_sizes (EvPixbufCache *pixbuf_cache,
503                                  gfloat         scale)
504 {
505         EvPageCache *page_cache;
506         int i;
507
508         page_cache = ev_page_cache_get (pixbuf_cache->document);
509
510         for (i = 0; i < PAGE_CACHE_LEN (pixbuf_cache); i++) {
511                 check_job_size_and_unref (pixbuf_cache, pixbuf_cache->job_list + i, page_cache, scale);
512         }
513
514         for (i = 0; i < pixbuf_cache->preload_cache_size; i++) {
515                 check_job_size_and_unref (pixbuf_cache, pixbuf_cache->prev_job + i, page_cache, scale);
516                 check_job_size_and_unref (pixbuf_cache, pixbuf_cache->next_job + i, page_cache, scale);
517         }
518 }
519
520 #define FIRST_VISABLE_PREV(pixbuf_cache) \
521         (MAX (0, pixbuf_cache->preload_cache_size + 1 - pixbuf_cache->start_page))
522
523 static void
524 get_selection_colors (GtkWidget *widget, GdkColor **text, GdkColor **base)
525 {
526     if (GTK_WIDGET_HAS_FOCUS (widget)) {
527         *text = &widget->style->text [GTK_STATE_SELECTED];
528         *base = &widget->style->base [GTK_STATE_SELECTED];
529     } else {
530         *text = &widget->style->text [GTK_STATE_ACTIVE];
531         *base = &widget->style->base [GTK_STATE_ACTIVE];
532     }
533 }
534
535 static void
536 add_job_if_needed (EvPixbufCache *pixbuf_cache,
537                    CacheJobInfo  *job_info,
538                    EvPageCache   *page_cache,
539                    gint           page,
540                    gint           rotation,
541                    gfloat         scale,
542                    EvJobPriority  priority)
543 {
544         gboolean include_links = FALSE;
545         gboolean include_text = FALSE;
546         gboolean include_selection = FALSE;
547         gboolean include_images = FALSE;
548         int width, height;
549         GdkColor *text, *base;
550
551         if (job_info->job)
552                 return;
553
554         ev_page_cache_get_size (page_cache, page, rotation,
555                                 scale, &width, &height);
556
557         if (job_info->surface &&
558             cairo_image_surface_get_width (job_info->surface) == width &&
559             cairo_image_surface_get_height (job_info->surface) == height)
560                 return;
561
562         /* make a new job now */
563         if (job_info->rc == NULL) {
564                 job_info->rc = ev_render_context_new (rotation, page, scale);
565         } else {
566                 ev_render_context_set_rotation (job_info->rc, rotation);
567                 ev_render_context_set_page (job_info->rc, page);
568                 ev_render_context_set_scale (job_info->rc, scale);
569         }
570
571         /* Figure out what else we need for this job */
572         if (job_info->link_mapping == NULL)
573                 include_links = TRUE;
574         if (job_info->image_mapping == NULL)
575                 include_images = TRUE;
576         if (job_info->text_mapping == NULL)
577                 include_text = TRUE;
578         if (new_selection_surface_needed (pixbuf_cache, job_info, page, scale)) {
579                 include_selection = TRUE;
580         }
581
582         gtk_widget_ensure_style (pixbuf_cache->view);
583
584         get_selection_colors (pixbuf_cache->view, &text, &base);
585
586         job_info->job = ev_job_render_new (pixbuf_cache->document,
587                                            job_info->rc,
588                                            width, height,
589                                            &(job_info->target_points),
590                                            text, base,
591                                            include_links,
592                                            include_images,
593                                            include_text,
594                                            include_selection);
595         ev_job_queue_add_job (job_info->job, priority);
596         g_signal_connect (job_info->job, "finished", G_CALLBACK (job_finished_cb), pixbuf_cache);
597 }
598
599
600 static void
601 ev_pixbuf_cache_add_jobs_if_needed (EvPixbufCache *pixbuf_cache,
602                                     gint           rotation,
603                                     gfloat         scale)
604 {
605         EvPageCache *page_cache;
606         CacheJobInfo *job_info;
607         int page;
608         int i;
609
610         page_cache = ev_page_cache_get (pixbuf_cache->document);
611
612         for (i = 0; i < PAGE_CACHE_LEN (pixbuf_cache); i++) {
613                 job_info = (pixbuf_cache->job_list + i);
614                 page = pixbuf_cache->start_page + i;
615
616                 add_job_if_needed (pixbuf_cache, job_info,
617                                    page_cache, page, rotation, scale,
618                                    EV_JOB_PRIORITY_HIGH);
619         }
620
621         for (i = FIRST_VISABLE_PREV(pixbuf_cache); i < pixbuf_cache->preload_cache_size; i++) {
622                 job_info = (pixbuf_cache->prev_job + i);
623                 page = pixbuf_cache->start_page - pixbuf_cache->preload_cache_size + i;
624
625                 add_job_if_needed (pixbuf_cache, job_info,
626                                    page_cache, page, rotation, scale,
627                                    EV_JOB_PRIORITY_LOW);
628         }
629
630         for (i = 0; i < VISIBLE_NEXT_LEN(pixbuf_cache, page_cache); i++) {
631                 job_info = (pixbuf_cache->next_job + i);
632                 page = pixbuf_cache->end_page + 1 + i;
633
634                 add_job_if_needed (pixbuf_cache, job_info,
635                                    page_cache, page, rotation, scale,
636                                    EV_JOB_PRIORITY_LOW);
637         }
638
639 }
640
641 void
642 ev_pixbuf_cache_set_page_range (EvPixbufCache  *pixbuf_cache,
643                                 gint            start_page,
644                                 gint            end_page,
645                                 gint            rotation,
646                                 gfloat          scale,
647                                 GList          *selection_list)
648 {
649         EvPageCache *page_cache;
650
651         g_return_if_fail (EV_IS_PIXBUF_CACHE (pixbuf_cache));
652
653         page_cache = ev_page_cache_get (pixbuf_cache->document);
654
655         g_return_if_fail (start_page >= 0 && start_page < ev_page_cache_get_n_pages (page_cache));
656         g_return_if_fail (end_page >= 0 && end_page < ev_page_cache_get_n_pages (page_cache));
657         g_return_if_fail (end_page >= start_page);
658
659         /* First, resize the page_range as needed.  We cull old pages
660          * mercilessly. */
661         ev_pixbuf_cache_update_range (pixbuf_cache, start_page, end_page);
662
663         /* Then, we update the current jobs to see if any of them are the wrong
664          * size, we remove them if we need to. */
665         ev_pixbuf_cache_clear_job_sizes (pixbuf_cache, scale);
666
667         /* Next, we update the target selection for our pages */
668         ev_pixbuf_cache_set_selection_list (pixbuf_cache, selection_list);
669
670         /* Finally, we add the new jobs for all the sizes that don't have a
671          * pixbuf */
672         ev_pixbuf_cache_add_jobs_if_needed (pixbuf_cache, rotation, scale);
673 }
674
675 cairo_surface_t *
676 ev_pixbuf_cache_get_surface (EvPixbufCache *pixbuf_cache,
677                              gint           page)
678 {
679         CacheJobInfo *job_info;
680
681         job_info = find_job_cache (pixbuf_cache, page);
682         if (job_info == NULL)
683                 return NULL;
684
685         /* We don't need to wait for the idle to handle the callback */
686         if (job_info->job &&
687             EV_JOB (job_info->job)->finished) {
688                 copy_job_to_job_info (EV_JOB_RENDER (job_info->job), job_info, pixbuf_cache);
689         }
690
691         return job_info->surface;
692 }
693
694 GList *
695 ev_pixbuf_cache_get_link_mapping (EvPixbufCache *pixbuf_cache,
696                                   gint           page)
697 {
698         CacheJobInfo *job_info;
699
700         job_info = find_job_cache (pixbuf_cache, page);
701         if (job_info == NULL)
702                 return NULL;
703
704         /* We don't need to wait for the idle to handle the callback */
705         if (job_info->job &&
706             EV_JOB (job_info->job)->finished) {
707                 copy_job_to_job_info (EV_JOB_RENDER (job_info->job), job_info, pixbuf_cache);
708         }
709
710         return job_info->link_mapping;
711 }
712
713 GList *
714 ev_pixbuf_cache_get_image_mapping (EvPixbufCache *pixbuf_cache,
715                                    gint           page)
716 {
717         CacheJobInfo *job_info;
718
719         if (!EV_IS_DOCUMENT_IMAGES (pixbuf_cache->document))
720                 return NULL;
721         
722         job_info = find_job_cache (pixbuf_cache, page);
723         if (job_info == NULL)
724                 return NULL;
725
726         /* We don't need to wait for the idle to handle the callback */
727         if (job_info->job &&
728             EV_JOB (job_info->job)->finished) {
729                 copy_job_to_job_info (EV_JOB_RENDER (job_info->job), job_info, pixbuf_cache);
730         }
731
732         return job_info->image_mapping;
733 }
734
735 static gboolean
736 new_selection_surface_needed (EvPixbufCache *pixbuf_cache,
737                              CacheJobInfo  *job_info,
738                              gint           page,
739                              gfloat         scale)
740 {
741         EvPageCache *page_cache;
742
743         if (job_info->selection) {
744                 gint width, height;
745                 gint selection_width, selection_height;
746                 
747                 page_cache = ev_page_cache_get (pixbuf_cache->document);
748                 ev_page_cache_get_size (page_cache, page,
749                                         job_info->rc->rotation,
750                                         scale, &width, &height);
751
752                 selection_width = cairo_image_surface_get_width (job_info->selection);
753                 selection_height = cairo_image_surface_get_height (job_info->selection);
754                 
755                 if (width != selection_width || height != selection_height)
756                         return TRUE;
757         } else {
758                 if (job_info->points_set)
759                         return TRUE;
760         }
761         
762         return FALSE;
763 }
764
765 static void
766 clear_selection_if_needed (EvPixbufCache *pixbuf_cache,
767                            CacheJobInfo  *job_info,
768                            gint           page,
769                            gfloat         scale)
770 {
771         if (new_selection_surface_needed (pixbuf_cache, job_info, page, scale)) {
772                 if (job_info->selection)
773                         cairo_surface_destroy (job_info->selection);
774                 job_info->selection = NULL;
775                 job_info->selection_points.x1 = -1;
776         }
777 }
778
779 GdkRegion *
780 ev_pixbuf_cache_get_text_mapping (EvPixbufCache *pixbuf_cache,
781                                   gint           page)
782 {
783         CacheJobInfo *job_info;
784
785         job_info = find_job_cache (pixbuf_cache, page);
786         if (job_info == NULL)
787                 return NULL;
788
789         /* We don't need to wait for the idle to handle the callback */
790         if (job_info->job &&
791             EV_JOB (job_info->job)->finished) {
792                 copy_job_to_job_info (EV_JOB_RENDER (job_info->job), job_info, pixbuf_cache);
793         }
794         
795         return job_info->text_mapping;
796 }
797
798 /* Clears the cache of jobs and pixbufs.
799  */
800 void
801 ev_pixbuf_cache_clear (EvPixbufCache *pixbuf_cache)
802 {
803         int i;
804
805         for (i = 0; i < pixbuf_cache->preload_cache_size; i++) {
806                 dispose_cache_job_info (pixbuf_cache->prev_job + i, pixbuf_cache);
807                 dispose_cache_job_info (pixbuf_cache->next_job + i, pixbuf_cache);
808         }
809
810         for (i = 0; i < PAGE_CACHE_LEN (pixbuf_cache); i++) {
811                 dispose_cache_job_info (pixbuf_cache->job_list + i, pixbuf_cache);
812         }
813 }
814
815
816 void
817 ev_pixbuf_cache_style_changed (EvPixbufCache *pixbuf_cache)
818 {
819         gint i;
820
821         /* FIXME: doesn't update running jobs. */
822         for (i = 0; i < pixbuf_cache->preload_cache_size; i++) {
823                 CacheJobInfo *job_info;
824
825                 job_info = pixbuf_cache->prev_job + i;
826                 if (job_info->selection) {
827                         cairo_surface_destroy (job_info->selection);
828                         job_info->selection = NULL;
829                 }
830
831                 job_info = pixbuf_cache->next_job + i;
832                 if (job_info->selection) {
833                         cairo_surface_destroy (job_info->selection);
834                         job_info->selection = NULL;
835                 }
836         }
837
838         for (i = 0; i < PAGE_CACHE_LEN (pixbuf_cache); i++) {
839                 CacheJobInfo *job_info;
840
841                 job_info = pixbuf_cache->job_list + i;
842                 if (job_info->selection) {
843                         cairo_surface_destroy (job_info->selection);
844                         job_info->selection = NULL;
845                 }
846         }
847 }
848
849 cairo_surface_t *
850 ev_pixbuf_cache_get_selection_surface (EvPixbufCache  *pixbuf_cache,
851                                        gint            page,
852                                        gfloat          scale,
853                                        GdkRegion     **region)
854 {
855         CacheJobInfo *job_info;
856
857         /* the document does not implement the selection interface */
858         if (!EV_IS_SELECTION (pixbuf_cache->document))
859                 return NULL;
860
861         job_info = find_job_cache (pixbuf_cache, page);
862         if (job_info == NULL)
863                 return NULL;
864
865         /* No selection on this page */
866         if (!job_info->points_set)
867                 return NULL;
868
869         /* Update the rc */
870         g_assert (job_info->rc);
871         ev_render_context_set_scale (job_info->rc, scale);
872
873         /* If we have a running job, we just return what we have under the
874          * assumption that it'll be updated later and we can scale it as need
875          * be */
876         if (job_info->job && EV_JOB_RENDER (job_info->job)->include_selection)
877                 return job_info->selection;
878
879         /* Now, lets see if we need to resize the image.  If we do, we clear the
880          * old one. */
881         clear_selection_if_needed (pixbuf_cache, job_info, page, scale);
882
883         /* Finally, we see if the two scales are the same, and get a new pixbuf
884          * if needed.  We do this synchronously for now.  At some point, we
885          * _should_ be able to get rid of the doc_mutex, so the synchronicity
886          * doesn't kill us.  Rendering a few glyphs should really be fast.
887          */
888         if (ev_rect_cmp (&(job_info->target_points), &(job_info->selection_points))) {
889                 EvRectangle *old_points;
890                 GdkColor *text, *base;
891
892                 /* we need to get a new selection pixbuf */
893                 ev_document_doc_mutex_lock ();
894                 if (job_info->selection_points.x1 < 0) {
895                         g_assert (job_info->selection == NULL);
896                         old_points = NULL;
897                 } else {
898                         g_assert (job_info->selection != NULL);
899                         old_points = &(job_info->selection_points);
900                 }
901
902                 if (job_info->selection_region)
903                         gdk_region_destroy (job_info->selection_region);
904                 job_info->selection_region =
905                         ev_selection_get_selection_region (EV_SELECTION (pixbuf_cache->document),
906                                                            job_info->rc,
907                                                            &(job_info->target_points));
908
909                 gtk_widget_ensure_style (pixbuf_cache->view);
910
911                 get_selection_colors (pixbuf_cache->view, &text, &base);
912
913                 ev_selection_render_selection (EV_SELECTION (pixbuf_cache->document),
914                                                job_info->rc, &(job_info->selection),
915                                                &(job_info->target_points),
916                                                old_points,
917                                                text, base);
918                 job_info->selection_points = job_info->target_points;
919                 ev_document_doc_mutex_unlock ();
920         }
921         if (region)
922                 *region = job_info->selection_region;
923         return job_info->selection;
924 }
925
926 static void
927 update_job_selection (CacheJobInfo    *job_info,
928                       EvViewSelection *selection)
929 {
930         job_info->points_set = TRUE;            
931         job_info->target_points = selection->rect;
932 }
933
934 static void
935 clear_job_selection (CacheJobInfo *job_info)
936 {
937         job_info->points_set = FALSE;
938         job_info->selection_points.x1 = -1;
939
940         if (job_info->selection) {
941                 cairo_surface_destroy (job_info->selection);
942                 job_info->selection = NULL;
943         }
944 }
945
946 /* This function will reset the selection on pages that no longer have them, and
947  * will update the target_selection on those that need it.  It will _not_ free
948  * the previous selection_list -- that's up to caller to do.
949  */
950 void
951 ev_pixbuf_cache_set_selection_list (EvPixbufCache *pixbuf_cache,
952                                     GList         *selection_list)
953 {
954         EvPageCache *page_cache;
955         EvViewSelection *selection;
956         GList *list = selection_list;
957         int page;
958         int i;
959
960         g_return_if_fail (EV_IS_PIXBUF_CACHE (pixbuf_cache));
961
962         if (!EV_IS_SELECTION (pixbuf_cache->document))
963                 return;
964
965         page_cache = ev_page_cache_get (pixbuf_cache->document);
966
967         /* We check each area to see what needs updating, and what needs freeing; */
968         page = pixbuf_cache->start_page - pixbuf_cache->preload_cache_size;
969         for (i = 0; i < pixbuf_cache->preload_cache_size; i++) {
970                 if (page < 0) {
971                         page ++;
972                         continue;
973                 }
974
975                 selection = NULL;
976                 while (list) {
977                         if (((EvViewSelection *)list->data)->page == page) {
978                                 selection = list->data;
979                                 break;
980                         } else if (((EvViewSelection *)list->data)->page > page) 
981                                 break;
982                         list = list->next;
983                 }
984
985                 if (selection)
986                         update_job_selection (pixbuf_cache->prev_job + i, selection);
987                 else
988                         clear_job_selection (pixbuf_cache->prev_job + i);
989                 page ++;
990         }
991
992         page = pixbuf_cache->start_page;
993         for (i = 0; i < PAGE_CACHE_LEN (pixbuf_cache); i++) {
994                 selection = NULL;
995                 while (list) {
996                         if (((EvViewSelection *)list->data)->page == page) {
997                                 selection = list->data;
998                                 break;
999                         } else if (((EvViewSelection *)list->data)->page > page) 
1000                                 break;
1001                         list = list->next;
1002                 }
1003
1004                 if (selection)
1005                         update_job_selection (pixbuf_cache->job_list + i, selection);
1006                 else
1007                         clear_job_selection (pixbuf_cache->job_list + i);
1008                 page ++;
1009         }
1010
1011         for (i = 0; i < pixbuf_cache->preload_cache_size; i++) {
1012                 if (page >= ev_page_cache_get_n_pages (page_cache))
1013                         break;
1014
1015                 selection = NULL;
1016                 while (list) {
1017                         if (((EvViewSelection *)list->data)->page == page) {
1018                                 selection = list->data;
1019                                 break;
1020                         } else if (((EvViewSelection *)list->data)->page > page) 
1021                                 break;
1022                         list = list->next;
1023                 }
1024
1025                 if (selection)
1026                         update_job_selection (pixbuf_cache->next_job + i, selection);
1027                 else
1028                         clear_job_selection (pixbuf_cache->next_job + i);
1029                 page ++;
1030         }
1031 }
1032
1033
1034 /* Returns what the pixbuf cache thinks is */
1035
1036 GList *
1037 ev_pixbuf_cache_get_selection_list (EvPixbufCache *pixbuf_cache)
1038 {
1039         EvPageCache *page_cache;
1040         EvViewSelection *selection;
1041         GList *retval = NULL;
1042         int page;
1043         int i;
1044
1045         g_return_val_if_fail (EV_IS_PIXBUF_CACHE (pixbuf_cache), NULL);
1046
1047         page_cache = ev_page_cache_get (pixbuf_cache->document);
1048
1049         /* We check each area to see what needs updating, and what needs freeing; */
1050         page = pixbuf_cache->start_page - pixbuf_cache->preload_cache_size;
1051         for (i = 0; i < pixbuf_cache->preload_cache_size; i++) {
1052                 if (page < 0) {
1053                         page ++;
1054                         continue;
1055                 }
1056
1057                 if (pixbuf_cache->prev_job[i].selection_points.x1 != -1) {
1058                         selection = g_new0 (EvViewSelection, 1);
1059                         selection->page = page;
1060                         selection->rect = pixbuf_cache->prev_job[i].selection_points;
1061                         if (pixbuf_cache->prev_job[i].selection_region)
1062                                 selection->covered_region = gdk_region_copy (pixbuf_cache->prev_job[i].selection_region);
1063                         retval = g_list_append (retval, selection);
1064                 }
1065                 
1066                 page ++;
1067         }
1068
1069         page = pixbuf_cache->start_page;
1070         for (i = 0; i < PAGE_CACHE_LEN (pixbuf_cache); i++) {
1071                 if (pixbuf_cache->job_list[i].selection_points.x1 != -1) {
1072                         selection = g_new0 (EvViewSelection, 1);
1073                         selection->page = page;
1074                         selection->rect = pixbuf_cache->job_list[i].selection_points;
1075                         if (pixbuf_cache->job_list[i].selection_region)
1076                                 selection->covered_region = gdk_region_copy (pixbuf_cache->job_list[i].selection_region);
1077                         retval = g_list_append (retval, selection);
1078                 }
1079                 
1080                 page ++;
1081         }
1082
1083         for (i = 0; i < pixbuf_cache->preload_cache_size; i++) {
1084                 if (page >= ev_page_cache_get_n_pages (page_cache))
1085                         break;
1086
1087                 if (pixbuf_cache->next_job[i].selection_points.x1 != -1) {
1088                         selection = g_new0 (EvViewSelection, 1);
1089                         selection->page = page;
1090                         selection->rect = pixbuf_cache->next_job[i].selection_points;
1091                         if (pixbuf_cache->next_job[i].selection_region)
1092                                 selection->covered_region = gdk_region_copy (pixbuf_cache->next_job[i].selection_region);
1093                         retval = g_list_append (retval, selection);
1094                 }
1095                 
1096                 page ++;
1097         }
1098
1099         return retval;
1100 }
1101