]> www.fi.muni.cz Git - evince.git/blob - shell/ev-pixbuf-cache.c
*** empty log message ***
[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                                 scale,
259                                 &width, &height);
260                                 
261         if (width == EV_JOB_RENDER (job_info->job)->target_width &&
262             height == EV_JOB_RENDER (job_info->job)->target_height)
263                 return;
264
265         /* Try to remove the job.  If we can't, then the thread has already
266          * picked it up and we are going get a signal when it's done.  If we
267          * can, then the job is fully dead and will never rnu.. */
268         if (ev_job_queue_remove_job (job_info->job))
269                 g_object_unref (job_info->job);
270
271         job_info->job = NULL;
272 }
273
274 /* Do all function that copies a job from an older cache to it's position in the
275  * new cache.  It clears the old job if it doesn't have a place.
276  */
277 static void
278 move_one_job (CacheJobInfo  *job_info,
279               EvPixbufCache *pixbuf_cache,
280               int            page,
281               CacheJobInfo  *new_job_list,
282               CacheJobInfo  *new_prev_job,
283               CacheJobInfo  *new_next_job,
284               int            start_page,
285               int            end_page,
286               EvJobPriority  priority)
287 {
288         CacheJobInfo *target_page = NULL;
289         int page_offset;
290         EvJobPriority new_priority;
291
292         if (page < (start_page - pixbuf_cache->preload_cache_size) ||
293             page > (end_page + pixbuf_cache->preload_cache_size)) {
294                 dispose_cache_job_info (job_info, pixbuf_cache);
295                 return;
296         }
297
298         /* find the target page to copy it over to. */
299         if (page < start_page) {
300                 page_offset = (page - (start_page - pixbuf_cache->preload_cache_size));
301
302                 g_assert (page_offset >= 0 &&
303                           page_offset < pixbuf_cache->preload_cache_size);
304                 target_page = new_prev_job + page_offset;
305                 new_priority = EV_JOB_PRIORITY_LOW;
306         } else if (page > end_page) {
307                 page_offset = (page - (end_page + 1));
308
309                 g_assert (page_offset >= 0 &&
310                           page_offset < pixbuf_cache->preload_cache_size);
311                 target_page = new_next_job + page_offset;
312                 new_priority = EV_JOB_PRIORITY_LOW;
313         } else {
314                 page_offset = page - start_page;
315                 g_assert (page_offset >= 0 &&
316                           page_offset <= ((end_page - start_page) + 1));
317                 new_priority = EV_JOB_PRIORITY_HIGH;
318                 target_page = new_job_list + page_offset;
319         }
320
321         *target_page = *job_info;
322         job_info->job = NULL;
323         job_info->pixbuf = NULL;
324         job_info->link_mapping = NULL;
325
326         if (new_priority != priority && target_page->job) {
327                 ev_job_queue_update_job (target_page->job, new_priority);
328         }
329 }
330
331
332
333 static void
334 ev_pixbuf_cache_update_range (EvPixbufCache *pixbuf_cache,
335                               gint           start_page,
336                               gint           end_page)
337 {
338         CacheJobInfo *new_job_list;
339         CacheJobInfo *new_prev_job;
340         CacheJobInfo *new_next_job;
341         EvPageCache *page_cache;
342         int i, page;
343
344         if (pixbuf_cache->start_page == start_page &&
345             pixbuf_cache->end_page == end_page)
346                 return;
347
348         page_cache = ev_page_cache_get (pixbuf_cache->document);
349
350         new_job_list = g_new0 (CacheJobInfo, (end_page - start_page) + 1);
351         new_prev_job = g_new0 (CacheJobInfo, pixbuf_cache->preload_cache_size);
352         new_next_job = g_new0 (CacheJobInfo, pixbuf_cache->preload_cache_size);
353
354         /* We go through each job in the old cache and either clear it or move
355          * it to a new location. */
356
357         /* Start with the prev cache. */
358         page = pixbuf_cache->start_page - pixbuf_cache->preload_cache_size;
359         for (i = 0; i < pixbuf_cache->preload_cache_size; i++) {
360                 if (page < 0) {
361                         dispose_cache_job_info (pixbuf_cache->prev_job + i, pixbuf_cache);
362                 } else {
363                         move_one_job (pixbuf_cache->prev_job + i,
364                                       pixbuf_cache, page,
365                                       new_job_list, new_prev_job, new_next_job,
366                                       start_page, end_page, EV_JOB_PRIORITY_LOW);
367                 }
368                 page ++;
369         }
370
371         page = pixbuf_cache->start_page;
372         for (i = 0; i < PAGE_CACHE_LEN (pixbuf_cache); i++) {
373                 move_one_job (pixbuf_cache->job_list + i,
374                               pixbuf_cache, page,
375                               new_job_list, new_prev_job, new_next_job,
376                               start_page, end_page, EV_JOB_PRIORITY_HIGH);
377                 page ++;
378         }
379
380         for (i = 0; i < pixbuf_cache->preload_cache_size; i++) {
381                 if (page >= ev_page_cache_get_n_pages (page_cache)) {
382                         dispose_cache_job_info (pixbuf_cache->next_job + i, pixbuf_cache);
383                 } else {
384                         move_one_job (pixbuf_cache->next_job + i,
385                                       pixbuf_cache, page,
386                                       new_job_list, new_prev_job, new_next_job,
387                                       start_page, end_page, EV_JOB_PRIORITY_LOW);
388                 }
389                 page ++;
390         }
391
392         g_free (pixbuf_cache->job_list);
393         g_free (pixbuf_cache->prev_job);
394         g_free (pixbuf_cache->next_job);
395
396         pixbuf_cache->job_list = new_job_list;
397         pixbuf_cache->prev_job = new_prev_job;
398         pixbuf_cache->next_job = new_next_job;
399
400         pixbuf_cache->start_page = start_page;
401         pixbuf_cache->end_page = end_page;
402 }
403
404 static void
405 copy_job_to_job_info (EvJobRender   *job_render,
406                       CacheJobInfo  *job_info,
407                       EvPixbufCache *pixbuf_cache)
408 {
409         GdkPixbuf *pixbuf;
410
411         pixbuf = g_object_ref (job_render->pixbuf);
412
413         dispose_cache_job_info (job_info, pixbuf_cache);
414
415         job_info->pixbuf = pixbuf;
416         if (job_render->link_mapping)
417                 job_info->link_mapping = job_render->link_mapping;
418         if (job_render->text_mapping)
419                 job_info->text_mapping = job_render->text_mapping;      
420 }
421
422 static CacheJobInfo *
423 find_job_cache (EvPixbufCache *pixbuf_cache,
424                 int            page)
425 {
426         int page_offset;
427
428         if (page < (pixbuf_cache->start_page - pixbuf_cache->preload_cache_size) ||
429             page > (pixbuf_cache->end_page + pixbuf_cache->preload_cache_size))
430                 return NULL;
431
432         if (page < pixbuf_cache->start_page) {
433                 page_offset = (page - (pixbuf_cache->start_page - pixbuf_cache->preload_cache_size));
434
435                 g_assert (page_offset >= 0 &&
436                           page_offset < pixbuf_cache->preload_cache_size);
437                 return pixbuf_cache->prev_job + page_offset;
438         }
439
440         if (page > pixbuf_cache->end_page) {
441                 page_offset = (page - (pixbuf_cache->end_page + 1));
442
443                 g_assert (page_offset >= 0 &&
444                           page_offset < pixbuf_cache->preload_cache_size);
445                 return pixbuf_cache->next_job + page_offset;
446         }
447
448         page_offset = page - pixbuf_cache->start_page;
449         g_assert (page_offset >= 0 &&
450                   page_offset <= PAGE_CACHE_LEN(pixbuf_cache));
451         return pixbuf_cache->job_list + page_offset;
452 }
453
454 static void
455 ev_pixbuf_cache_clear_job_sizes (EvPixbufCache *pixbuf_cache,
456                                  gfloat         scale)
457 {
458         EvPageCache *page_cache;
459         int i;
460
461         page_cache = ev_page_cache_get (pixbuf_cache->document);
462
463         for (i = 0; i < PAGE_CACHE_LEN (pixbuf_cache); i++) {
464                 check_job_size_and_unref (pixbuf_cache->job_list + i, page_cache, scale);
465         }
466
467         for (i = 0; i < pixbuf_cache->preload_cache_size; i++) {
468                 check_job_size_and_unref (pixbuf_cache->prev_job + i, page_cache, scale);
469                 check_job_size_and_unref (pixbuf_cache->next_job + i, page_cache, scale);
470         }
471 }
472
473 #define FIRST_VISABLE_PREV(pixbuf_cache) \
474         (MAX (0, pixbuf_cache->preload_cache_size + 1 - pixbuf_cache->start_page))
475
476 static void
477 add_job_if_needed (EvPixbufCache *pixbuf_cache,
478                    CacheJobInfo  *job_info,
479                    EvPageCache   *page_cache,
480                    gint           page,
481                    gfloat         scale,
482                    EvJobPriority  priority)
483 {
484         gboolean include_links = FALSE;
485         gboolean include_text = FALSE;
486         gboolean include_selection = FALSE;
487         int width, height;
488
489         if (job_info->job)
490                 return;
491
492         ev_page_cache_get_size (page_cache,
493                                 page, scale,
494                                 &width, &height);
495
496         if (job_info->pixbuf &&
497             gdk_pixbuf_get_width (job_info->pixbuf) == width &&
498             gdk_pixbuf_get_height (job_info->pixbuf) == height)
499                 return;
500
501         /* make a new job now */
502         if (job_info->rc == NULL) {
503                 job_info->rc = ev_render_context_new (EV_ORIENTATION_PORTRAIT,
504                                                       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         }
509
510         /* Figure out what else we need for this job */
511         if (job_info->link_mapping == NULL)
512                 include_links = TRUE;
513         if (job_info->text_mapping == NULL)
514                 include_text = TRUE;
515         if (new_selection_pixbuf_needed (pixbuf_cache, job_info, page, scale)) {
516                 include_selection = TRUE;
517         }
518
519         job_info->job = ev_job_render_new (pixbuf_cache->document,
520                                            job_info->rc,
521                                            width, height,
522                                            &(job_info->new_points),
523                                            include_links,
524                                            include_text,
525                                            include_selection);
526         ev_job_queue_add_job (job_info->job, priority);
527         g_signal_connect (job_info->job, "finished", G_CALLBACK (job_finished_cb), pixbuf_cache);
528 }
529
530
531 static void
532 ev_pixbuf_cache_add_jobs_if_needed (EvPixbufCache *pixbuf_cache,
533                                     gfloat         scale)
534 {
535         EvPageCache *page_cache;
536         CacheJobInfo *job_info;
537         int page;
538         int i;
539
540         page_cache = ev_page_cache_get (pixbuf_cache->document);
541
542         for (i = 0; i < PAGE_CACHE_LEN (pixbuf_cache); i++) {
543                 job_info = (pixbuf_cache->job_list + i);
544                 page = pixbuf_cache->start_page + i;
545
546                 add_job_if_needed (pixbuf_cache, job_info,
547                                    page_cache, page, scale,
548                                    EV_JOB_PRIORITY_HIGH);
549         }
550
551         for (i = FIRST_VISABLE_PREV(pixbuf_cache); i < pixbuf_cache->preload_cache_size; i++) {
552                 job_info = (pixbuf_cache->prev_job + i);
553                 page = pixbuf_cache->start_page - pixbuf_cache->preload_cache_size + i;
554
555                 add_job_if_needed (pixbuf_cache, job_info,
556                                    page_cache, page, scale,
557                                    EV_JOB_PRIORITY_LOW);
558         }
559
560         for (i = 0; i < VISIBLE_NEXT_LEN(pixbuf_cache, page_cache); i++) {
561                 job_info = (pixbuf_cache->next_job + i);
562                 page = pixbuf_cache->end_page + 1 + i;
563
564                 add_job_if_needed (pixbuf_cache, job_info,
565                                    page_cache, page, scale,
566                                    EV_JOB_PRIORITY_LOW);
567         }
568
569 }
570
571 void
572 ev_pixbuf_cache_set_page_range (EvPixbufCache *pixbuf_cache,
573                                 gint           start_page,
574                                 gint           end_page,
575                                 gfloat         scale,
576                                 GList          *selection_list)
577 {
578         EvPageCache *page_cache;
579
580         g_return_if_fail (EV_IS_PIXBUF_CACHE (pixbuf_cache));
581
582         page_cache = ev_page_cache_get (pixbuf_cache->document);
583
584         g_return_if_fail (start_page >= 0 && start_page < ev_page_cache_get_n_pages (page_cache));
585         g_return_if_fail (end_page >= 0 && end_page < ev_page_cache_get_n_pages (page_cache));
586         g_return_if_fail (end_page >= start_page);
587
588         /* First, resize the page_range as needed.  We cull old pages
589          * mercilessly. */
590         ev_pixbuf_cache_update_range (pixbuf_cache, start_page, end_page);
591
592         /* Then, we update the current jobs to see if any of them are the wrong
593          * size, we remove them if we need to. */
594         ev_pixbuf_cache_clear_job_sizes (pixbuf_cache, scale);
595
596         /* Next, we update the target selection for our pages */
597         ev_pixbuf_cache_set_selection_list (pixbuf_cache, selection_list);
598
599         /* Finally, we add the new jobs for all the sizes that don't have a
600          * pixbuf */
601         ev_pixbuf_cache_add_jobs_if_needed (pixbuf_cache, scale);
602 }
603
604 GdkPixbuf *
605 ev_pixbuf_cache_get_pixbuf (EvPixbufCache *pixbuf_cache,
606                             gint           page)
607 {
608         CacheJobInfo *job_info;
609
610         job_info = find_job_cache (pixbuf_cache, page);
611         if (job_info == NULL)
612                 return NULL;
613
614         /* We don't need to wait for the idle to handle the callback */
615         if (job_info->job &&
616             EV_JOB (job_info->job)->finished) {
617                 copy_job_to_job_info (EV_JOB_RENDER (job_info->job), job_info, pixbuf_cache);
618         }
619
620         return job_info->pixbuf;
621 }
622
623 GList *
624 ev_pixbuf_cache_get_link_mapping (EvPixbufCache *pixbuf_cache,
625                                   gint           page)
626 {
627         CacheJobInfo *job_info;
628
629         job_info = find_job_cache (pixbuf_cache, page);
630         if (job_info == NULL)
631                 return NULL;
632
633         /* We don't need to wait for the idle to handle the callback */
634         if (job_info->job &&
635             EV_JOB (job_info->job)->finished) {
636                 copy_job_to_job_info (EV_JOB_RENDER (job_info->job), job_info, pixbuf_cache);
637         }
638         
639         return job_info->link_mapping;
640 }
641
642 /* Selection */
643 static gboolean
644 new_selection_pixbuf_needed (EvPixbufCache *pixbuf_cache,
645                              CacheJobInfo  *job_info,
646                              gint           page,
647                              gfloat         scale)
648 {
649         EvPageCache *page_cache;
650         gint width, height;
651
652         if (job_info->selection) {
653                 page_cache = ev_page_cache_get (pixbuf_cache->document);
654                 ev_page_cache_get_size (page_cache, page, scale,
655                                         &width, &height);
656                 
657                 if (width != gdk_pixbuf_get_width (job_info->selection) ||
658                     height != gdk_pixbuf_get_height (job_info->selection))
659                         return TRUE;
660         } else {
661                 if (job_info->new_points.x1 >= 0)
662                         return TRUE;
663         }
664         return FALSE;
665 }
666
667 static void
668 clear_selection_if_needed (EvPixbufCache *pixbuf_cache,
669                            CacheJobInfo  *job_info,
670                            gint           page,
671                            gfloat         scale)
672 {
673         if (new_selection_pixbuf_needed (pixbuf_cache, job_info, page, scale)) {
674                 if (job_info->selection)
675                         g_object_unref (job_info->selection);
676                 job_info->selection = NULL;
677                 job_info->selection_points.x1 = -1;
678         }
679 }
680
681 GdkRegion *
682 ev_pixbuf_cache_get_text_mapping      (EvPixbufCache *pixbuf_cache,
683                                       gint           page)
684 {
685         CacheJobInfo *job_info;
686
687         job_info = find_job_cache (pixbuf_cache, page);
688         if (job_info == NULL)
689                 return NULL;
690
691         /* We don't need to wait for the idle to handle the callback */
692         if (job_info->job &&
693             EV_JOB (job_info->job)->finished) {
694                 copy_job_to_job_info (EV_JOB_RENDER (job_info->job), job_info, pixbuf_cache);
695         }
696         
697         return job_info->text_mapping;
698 }
699
700 GdkPixbuf *
701 ev_pixbuf_cache_get_selection_pixbuf (EvPixbufCache *pixbuf_cache,
702                                       gint           page,
703                                       gfloat         scale)
704 {
705         CacheJobInfo *job_info;
706
707         /* the document does not implement the selection interface */
708         if (!EV_IS_SELECTION (pixbuf_cache->document))
709                 return NULL;
710
711         job_info = find_job_cache (pixbuf_cache, page);
712         if (job_info == NULL)
713                 return NULL;
714
715         /* No selection on this page */
716         if (job_info->new_points.x1 < 0)
717                 return NULL;
718
719         /* If we have a running job, we just return what we have under the
720          * assumption that it'll be updated later and we can scale it as need
721          * be */
722         if (job_info->job && EV_JOB_RENDER (job_info->job)->include_selection)
723                 return job_info->selection;
724
725         /* Now, lets see if we need to resize the image.  If we do, we clear the
726          * old one. */
727         clear_selection_if_needed (pixbuf_cache, job_info, page, scale);
728
729         /* Finally, we see if the two scales are the same, and get a new pixbuf
730          * if needed.  We do this synchronously for now.  At some point, we
731          * _should_ be able to get rid of the doc_mutex, so the synchronicity
732          * doesn't kill us.  Rendering a few glyphs should really be fast.
733          */
734         if (ev_rect_cmp (&(job_info->new_points), &(job_info->selection_points))) {
735                 EvRenderContext *rc;
736
737                 rc = ev_render_context_new (EV_ORIENTATION_PORTRAIT,
738                                             page,
739                                             scale);
740
741                 /* we need to get a new selection pixbuf */
742                 ev_document_doc_mutex_lock ();
743                 if (job_info->selection_points.x1 < 0) {
744                         g_assert (job_info->selection == NULL);
745                         ev_selection_render_selection (EV_SELECTION (pixbuf_cache->document),
746                                                        rc, &(job_info->selection),
747                                                        &(job_info->new_points),
748                                                        NULL);
749                 } else {
750                         g_assert (job_info->selection != NULL);
751                         ev_selection_render_selection (EV_SELECTION (pixbuf_cache->document),
752                                                        rc, &(job_info->selection),
753                                                        &(job_info->new_points),
754                                                        &(job_info->selection_points));
755                 }
756                 job_info->selection_points = job_info->new_points;
757                 ev_document_doc_mutex_unlock ();
758
759         }
760         return job_info->selection;
761 }
762
763
764 static void
765 update_job_selection (CacheJobInfo    *job_info,
766                       EvViewSelection *selection)
767 {
768         if (job_info->selection == NULL)
769                 job_info->selection_points.x1 = -1;
770         job_info->new_points = selection->rect;
771 }
772
773 static void
774 clear_job_selection (CacheJobInfo *job_info)
775 {
776         job_info->selection_points.x1 = -1;
777         job_info->new_points.x1 = -1;
778
779         if (job_info->selection) {
780                 g_object_unref (job_info->selection);
781                 job_info->selection = NULL;
782         }
783 }
784
785 /* This function will reset the selection on pages that no longer have them, and
786  * will update the target_selection on those that need it.
787  */
788 void
789 ev_pixbuf_cache_set_selection_list (EvPixbufCache *pixbuf_cache,
790                                     GList         *selection_list)
791 {
792         EvPageCache *page_cache;
793         EvViewSelection *selection;
794         GList *list = selection_list;
795         int page;
796         int i;
797
798         g_return_if_fail (EV_IS_PIXBUF_CACHE (pixbuf_cache));
799
800         page_cache = ev_page_cache_get (pixbuf_cache->document);
801
802         /* We check each area to see what needs updating, and what needs freeing; */
803         page = pixbuf_cache->start_page - pixbuf_cache->preload_cache_size;
804         for (i = 0; i < pixbuf_cache->preload_cache_size; i++) {
805                 if (page < 0) {
806                         page ++;
807                         continue;
808                 }
809
810                 selection = NULL;
811                 while (list) {
812                         if (((EvViewSelection *)list->data)->page == page) {
813                                 selection = list->data;
814                                 break;
815                         } else if (((EvViewSelection *)list->data)->page > page) 
816                                 break;
817                         list = list->next;
818                 }
819
820                 if (selection)
821                         update_job_selection (pixbuf_cache->prev_job + i, selection);
822                 else
823                         clear_job_selection (pixbuf_cache->prev_job + i);
824                 page ++;
825         }
826
827         page = pixbuf_cache->start_page;
828         for (i = 0; i < PAGE_CACHE_LEN (pixbuf_cache); i++) {
829                 selection = NULL;
830                 while (list) {
831                         if (((EvViewSelection *)list->data)->page == page) {
832                                 selection = list->data;
833                                 break;
834                         } else if (((EvViewSelection *)list->data)->page > page) 
835                                 break;
836                         list = list->next;
837                 }
838
839                 if (selection)
840                         update_job_selection (pixbuf_cache->job_list + i, selection);
841                 else
842                         clear_job_selection (pixbuf_cache->job_list + i);
843                 page ++;
844         }
845
846         for (i = 0; i < pixbuf_cache->preload_cache_size; i++) {
847                 if (page >= ev_page_cache_get_n_pages (page_cache))
848                         break;
849                 
850                 selection = NULL;
851                 while (list) {
852                         if (((EvViewSelection *)list->data)->page == page) {
853                                 selection = list->data;
854                                 break;
855                         } else if (((EvViewSelection *)list->data)->page > page) 
856                                 break;
857                         list = list->next;
858                 }
859
860                 if (selection)
861                         update_job_selection (pixbuf_cache->next_job + i, selection);
862                 else
863                         clear_job_selection (pixbuf_cache->next_job + i);
864                 page ++;
865         }
866 }