-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathdevelop.c
More file actions
4030 lines (3474 loc) · 138 KB
/
Copy pathdevelop.c
File metadata and controls
4030 lines (3474 loc) · 138 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
This file is part of darktable,
Copyright (C) 2009-2026 darktable developers.
darktable is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
darktable is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with darktable. If not, see <http://www.gnu.org/licenses/>.
*/
#include <assert.h>
#include <glib/gprintf.h>
#include <math.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <unistd.h>
#include "common/atomic.h"
#include "common/debug.h"
#include "common/history.h"
#include "common/image_cache.h"
#include "common/mipmap_cache.h"
#include "common/opencl.h"
#include "common/tags.h"
#include "common/presets.h"
#include "control/conf.h"
#include "control/control.h"
#include "control/jobs.h"
#include "develop/blend.h"
#include "develop/develop.h"
#include "develop/imageop.h"
#include "develop/lightroom.h"
#include "develop/masks.h"
#include "libs/modulegroups.h"
#include "gui/gtk.h"
#include "gui/presets.h"
#include "imageio/imageio_common.h"
#ifdef USE_LUA
#include "lua/call.h"
#endif
#define DT_DEV_AVERAGE_DELAY_COUNT 5
// Forward declaration
static inline void _dt_dev_load_raw(dt_develop_t *dev, const dt_imgid_t imgid);
void dt_dev_init(dt_develop_t *dev,
const gboolean gui_attached)
{
memset(dev, 0, sizeof(dt_develop_t));
dev->full_preview = FALSE;
dev->gui_module = NULL;
dev->timestamp = 0;
dev->gui_leaving = FALSE;
dev->gui_synch = FALSE;
pthread_mutexattr_t recursive_locking;
pthread_mutexattr_init(&recursive_locking);
pthread_mutexattr_settype(&recursive_locking, PTHREAD_MUTEX_RECURSIVE);
dt_pthread_mutex_init(&dev->history_mutex, &recursive_locking);
dev->snapshot_id = -1;
dev->history_end = 0;
dev->history = NULL; // empty list
dev->history_postpone_invalidate = FALSE;
dev->module_filter_out = NULL;
dev->gui_attached = gui_attached;
dev->full.width = -1;
dev->full.height = -1;
dt_image_init(&dev->image_storage);
dev->history_updating = dev->image_force_reload = FALSE;
dev->autosaving = FALSE;
dev->autosave_time = 0.0;
dev->image_invalid_cnt = 0;
dev->full.pipe = dev->preview_pipe = dev->preview2.pipe = NULL;
dev->histogram_pre_tonecurve = NULL;
dev->histogram_pre_levels = NULL;
dev->forms = NULL;
dev->form_visible = NULL;
dev->form_gui = NULL;
dev->allforms = NULL;
if(dev->gui_attached)
{
dev->full.pipe = malloc(sizeof(dt_dev_pixelpipe_t));
dev->preview_pipe = malloc(sizeof(dt_dev_pixelpipe_t));
dev->preview2.pipe = malloc(sizeof(dt_dev_pixelpipe_t));
dt_dev_pixelpipe_init(dev->full.pipe);
dt_dev_pixelpipe_init_preview(dev->preview_pipe);
dt_dev_pixelpipe_init_preview2(dev->preview2.pipe);
dev->histogram_pre_tonecurve = (uint32_t *)calloc(4 * 256, sizeof(uint32_t));
dev->histogram_pre_levels = (uint32_t *)calloc(4 * 256, sizeof(uint32_t));
// FIXME: these are uint32_t, setting to -1 is confusing
dev->histogram_pre_tonecurve_max = -1;
dev->histogram_pre_levels_max = -1;
dev->darkroom_mouse_in_center_area = FALSE;
dev->darkroom_skip_mouse_events = FALSE;
if(darktable.gui)
{
dev->full.ppd = darktable.gui->ppd;
dev->full.dpi = darktable.gui->dpi;
dev->full.dpi_factor = darktable.gui->dpi_factor;
dev->full.widget = dt_ui_center(darktable.gui->ui);
}
}
dev->iop_instance = 0;
dev->iop = NULL;
dev->alliop = NULL;
dev->allprofile_info = NULL;
dev->iop_order_version = 0;
dev->iop_order_list = NULL;
dev->proxy.exposure.module = NULL;
dt_dev_init_chroma(dev);
dev->late_scaling.enabled = FALSE;
dev->rawoverexposed.enabled = FALSE;
dev->rawoverexposed.mode =
dt_conf_get_int("darkroom/ui/rawoverexposed/mode");
dev->rawoverexposed.colorscheme =
dt_conf_get_int("darkroom/ui/rawoverexposed/colorscheme");
dev->rawoverexposed.threshold =
dt_conf_get_float("darkroom/ui/rawoverexposed/threshold");
dev->overexposed.enabled = FALSE;
dev->overexposed.mode = dt_conf_get_int("darkroom/ui/overexposed/mode");
dev->overexposed.colorscheme = dt_conf_get_int("darkroom/ui/overexposed/colorscheme");
dev->overexposed.lower = dt_conf_get_float("darkroom/ui/overexposed/lower");
dev->overexposed.upper = dt_conf_get_float("darkroom/ui/overexposed/upper");
dev->full.color_assessment = dt_conf_get_bool("full_window/color_assessment");
dev->preview2.color_assessment = dt_conf_get_bool("second_window/color_assessment");
dev->constrain_zoom = dt_conf_get_bool("darkroom/ui/constrain_zoom");
dev->full.zoom = dev->preview2.zoom = DT_ZOOM_FIT;
dev->full.closeup = dev->preview2.closeup = 0;
dev->full.zoom_x = dev->full.zoom_y = dev->preview2.zoom_x = dev->preview2.zoom_y = 0.0f;
dev->full.zoom_scale = dev->preview2.zoom_scale = 1.0f;
// Set back-pointers from viewports to their owning develop
dev->full.dev = dev;
dev->preview2.dev = dev;
// Initialize pinned image state
dev->preview2_pinned = FALSE;
dev->preview2_pinned_dev = NULL;
}
// Shutdown and cleanup a pinned dev, waiting for any in-progress jobs
static void _cleanup_pinned_dev(dt_develop_t *pinned_dev)
{
if(!pinned_dev) return;
pinned_dev->gui_leaving = TRUE;
pinned_dev->preview2.widget = NULL;
if(pinned_dev->preview2.pipe)
dt_dev_pixelpipe_set_shutdown(pinned_dev->preview2.pipe, DT_DEV_PIXELPIPE_STOP_NODES);
if(pinned_dev->preview_pipe)
dt_dev_pixelpipe_set_shutdown(pinned_dev->preview_pipe, DT_DEV_PIXELPIPE_STOP_NODES);
if(pinned_dev->full.pipe)
dt_dev_pixelpipe_set_shutdown(pinned_dev->full.pipe, DT_DEV_PIXELPIPE_STOP_NODES);
if(pinned_dev->preview2.pipe)
{
dt_pthread_mutex_lock(&pinned_dev->preview2.pipe->mutex);
dt_pthread_mutex_unlock(&pinned_dev->preview2.pipe->mutex);
dt_pthread_mutex_lock(&pinned_dev->preview2.pipe->busy_mutex);
dt_pthread_mutex_unlock(&pinned_dev->preview2.pipe->busy_mutex);
}
dt_dev_cleanup(pinned_dev);
free(pinned_dev);
}
void dt_dev_cleanup(dt_develop_t *dev)
{
if(!dev) return;
// image_cache does not have to be unref'd, this is done outside develop module.
dt_dev_init_chroma(dev);
if(dev->full.pipe)
{
dt_dev_pixelpipe_cleanup(dev->full.pipe);
free(dev->full.pipe);
}
if(dev->preview_pipe)
{
dt_dev_pixelpipe_cleanup(dev->preview_pipe);
free(dev->preview_pipe);
}
if(dev->preview2.pipe)
{
dt_dev_pixelpipe_cleanup(dev->preview2.pipe);
free(dev->preview2.pipe);
}
while(dev->history)
{
dt_dev_free_history_item(((dt_dev_history_item_t *)dev->history->data));
dev->history = g_list_delete_link(dev->history, dev->history);
}
while(dev->iop)
{
dt_iop_cleanup_module((dt_iop_module_t *)dev->iop->data);
free(dev->iop->data);
dev->iop = g_list_delete_link(dev->iop, dev->iop);
}
while(dev->alliop)
{
dt_iop_cleanup_module((dt_iop_module_t *)dev->alliop->data);
free(dev->alliop->data);
dev->alliop = g_list_delete_link(dev->alliop, dev->alliop);
}
g_list_free_full(dev->iop_order_list, free);
while(dev->allprofile_info)
{
dt_ioppr_cleanup_profile_info
((dt_iop_order_iccprofile_info_t *)dev->allprofile_info->data);
dt_free_align(dev->allprofile_info->data);
dev->allprofile_info = g_list_delete_link(dev->allprofile_info, dev->allprofile_info);
}
dt_pthread_mutex_destroy(&dev->history_mutex);
if(dev->histogram_pre_tonecurve) free(dev->histogram_pre_tonecurve);
if(dev->histogram_pre_levels) free(dev->histogram_pre_levels);
dev->histogram_pre_tonecurve = dev->histogram_pre_levels = NULL;
// Clean up pinned develop
// Clean up pinned develop
if(dev->preview2_pinned_dev)
{
_cleanup_pinned_dev(dev->preview2_pinned_dev);
dev->preview2_pinned_dev = NULL;
}
dev->preview2_pinned = FALSE;
g_list_free_full(dev->forms, (void (*)(void *))dt_masks_free_form);
g_list_free_full(dev->allforms, (void (*)(void *))dt_masks_free_form);
dt_conf_set_int("darkroom/ui/rawoverexposed/mode",
dev->rawoverexposed.mode);
dt_conf_set_int("darkroom/ui/rawoverexposed/colorscheme",
dev->rawoverexposed.colorscheme);
dt_conf_set_float("darkroom/ui/rawoverexposed/threshold",
dev->rawoverexposed.threshold);
dt_conf_set_int("darkroom/ui/overexposed/mode", dev->overexposed.mode);
dt_conf_set_int("darkroom/ui/overexposed/colorscheme", dev->overexposed.colorscheme);
dt_conf_set_float("darkroom/ui/overexposed/lower", dev->overexposed.lower);
dt_conf_set_float("darkroom/ui/overexposed/upper", dev->overexposed.upper);
g_list_free(dev->module_filter_out);
}
void dt_dev_process_image(dt_develop_t *dev)
{
if(!dev->gui_attached || dev->full.pipe->processing) return;
const gboolean err = dt_control_add_job_res(dt_dev_process_image_job_create(dev), DT_CTL_WORKER_ZOOM_1);
if(err) dt_print(DT_DEBUG_ALWAYS, "[dev_process_image] job queue exceeded!");
}
void dt_dev_process_preview(dt_develop_t *dev)
{
if(!dev->gui_attached) return;
const gboolean err = dt_control_add_job_res(dt_dev_process_preview_job_create(dev), DT_CTL_WORKER_ZOOM_FILL);
if(err) dt_print(DT_DEBUG_ALWAYS, "[dev_process_preview] job queue exceeded!");
}
void dt_dev_process_preview2(dt_develop_t *dev)
{
if(!dev->gui_attached && !dev->preview2.widget) return;
const gboolean err = dt_control_add_job_res(dt_dev_process_preview2_job_create(dev), DT_CTL_WORKER_ZOOM_2);
if(err) dt_print(DT_DEBUG_ALWAYS, "[dev_process_preview2] job queue exceeded!");
}
void dt_dev_invalidate(dt_develop_t *dev)
{
dev->full.pipe->status = DT_DEV_PIXELPIPE_DIRTY;
dev->timestamp++;
if(dev->preview_pipe)
dev->preview_pipe->input_timestamp = dev->timestamp;
if(dev->preview2.pipe)
dev->preview2.pipe->input_timestamp = dev->timestamp;
}
void dt_dev_invalidate_all(dt_develop_t *dev)
{
if(dev->full.pipe)
dev->full.pipe->status = DT_DEV_PIXELPIPE_DIRTY;
if(dev->preview_pipe)
dev->preview_pipe->status = DT_DEV_PIXELPIPE_DIRTY;
if(dev->preview2.pipe)
dev->preview2.pipe->status = DT_DEV_PIXELPIPE_DIRTY;
dev->timestamp++;
}
// Helper to find the cloned module in pinned_dev that corresponds to a module in src_dev
static dt_iop_module_t *_find_cloned_module(dt_develop_t *dev, dt_iop_module_t *src_mod)
{
if(!src_mod) return NULL;
for(GList *iter = dev->iop; iter; iter = g_list_next(iter))
{
dt_iop_module_t *mod = (dt_iop_module_t *)iter->data;
// During cloning we preserve the instance ID and other unique fields
if(mod->instance == src_mod->instance && g_strcmp0(mod->op, src_mod->op) == 0 &&
mod->multi_priority == src_mod->multi_priority)
return mod;
}
return NULL;
}
// Helper to clone a module for pinned dev
static dt_iop_module_t *_clone_module(dt_develop_t *dev, dt_iop_module_t *src_mod)
{
dt_iop_module_t *new_mod = calloc(1, sizeof(dt_iop_module_t));
if(dt_iop_load_module_by_so(new_mod, src_mod->so, dev))
{
free(new_mod);
return NULL;
}
new_mod->instance = src_mod->instance;
new_mod->enabled = src_mod->enabled;
new_mod->iop_order = src_mod->iop_order;
new_mod->multi_priority = src_mod->multi_priority;
g_strlcpy(new_mod->multi_name, src_mod->multi_name, sizeof(new_mod->multi_name));
new_mod->multi_name_hand_edited = src_mod->multi_name_hand_edited;
new_mod->hide_enable_button = src_mod->hide_enable_button;
if(src_mod->params)
memcpy(new_mod->params, src_mod->params, src_mod->params_size);
if(new_mod->blend_params && src_mod->blend_params)
memcpy(new_mod->blend_params, src_mod->blend_params, sizeof(dt_develop_blend_params_t));
return new_mod;
}
static GList *_duplicate_iop_list(dt_develop_t *pinned_dev, dt_develop_t *main_dev)
{
GList *new_list = NULL;
for(GList *iter = main_dev->iop; iter; iter = g_list_next(iter))
{
dt_iop_module_t *src_mod = (dt_iop_module_t *)iter->data;
dt_iop_module_t *new_mod = _clone_module(pinned_dev, src_mod);
if(new_mod)
new_list = g_list_append(new_list, new_mod);
}
return new_list;
}
static GList *_duplicate_history_list(dt_develop_t *pinned_dev, GList *src_history)
{
GList *new_history = dt_history_duplicate(src_history);
for(GList *iter = new_history; iter; iter = g_list_next(iter))
{
dt_dev_history_item_t *item = (dt_dev_history_item_t *)iter->data;
item->module = _find_cloned_module(pinned_dev, item->module);
}
return new_history;
}
// Allocate a new pinned develop struct, initialise it without GUI, copy the
// preview2 viewport from main_dev, and create the three pixelpipes.
// Returns NULL on allocation failure; the caller must free on error paths.
static dt_develop_t *_alloc_pinned_dev(dt_develop_t *main_dev)
{
dt_develop_t *pinned_dev = malloc(sizeof(dt_develop_t));
if(!pinned_dev) return NULL;
dt_dev_init(pinned_dev, FALSE);
pinned_dev->preview2.width = main_dev->preview2.width;
pinned_dev->preview2.height = main_dev->preview2.height;
pinned_dev->preview2.orig_width = main_dev->preview2.orig_width;
pinned_dev->preview2.orig_height = main_dev->preview2.orig_height;
pinned_dev->preview2.border_size = main_dev->preview2.border_size;
pinned_dev->preview2.dpi = main_dev->preview2.dpi;
pinned_dev->preview2.dpi_factor = main_dev->preview2.dpi_factor;
pinned_dev->preview2.ppd = main_dev->preview2.ppd;
pinned_dev->preview2.color_assessment = main_dev->preview2.color_assessment;
pinned_dev->preview2.zoom = main_dev->preview2.zoom;
pinned_dev->preview2.closeup = main_dev->preview2.closeup;
pinned_dev->preview2.zoom_x = main_dev->preview2.zoom_x;
pinned_dev->preview2.zoom_y = main_dev->preview2.zoom_y;
pinned_dev->preview2.zoom_scale = main_dev->preview2.zoom_scale;
pinned_dev->preview2.widget = main_dev->preview2.widget;
pinned_dev->preview2.pin_button = NULL;
pinned_dev->preview2.dev = pinned_dev;
pinned_dev->full.pipe = malloc(sizeof(dt_dev_pixelpipe_t));
pinned_dev->preview_pipe = malloc(sizeof(dt_dev_pixelpipe_t));
pinned_dev->preview2.pipe = malloc(sizeof(dt_dev_pixelpipe_t));
dt_dev_pixelpipe_init(pinned_dev->full.pipe);
dt_dev_pixelpipe_init_preview(pinned_dev->preview_pipe);
dt_dev_pixelpipe_init_preview2(pinned_dev->preview2.pipe);
return pinned_dev;
}
// Atomically install new_pinned_dev as the active pinned dev, clean up
// the old one (if any), kick off processing, and sync the pin button.
// Always leaves dev->preview2_pinned == TRUE.
static void _activate_pinned_dev(dt_develop_t *dev, dt_develop_t *new_pinned_dev)
{
new_pinned_dev->full.pipe->loading = FALSE;
new_pinned_dev->preview_pipe->loading = FALSE;
new_pinned_dev->preview2.pipe->loading = TRUE;
new_pinned_dev->preview2.pipe->status = DT_DEV_PIXELPIPE_DIRTY;
new_pinned_dev->preview2.pipe->changed |= DT_DEV_PIPE_SYNCH;
dt_develop_t *old_pinned_dev = dev->preview2_pinned_dev;
dev->preview2_pinned_dev = new_pinned_dev;
dev->preview2_pinned = TRUE;
if(old_pinned_dev)
_cleanup_pinned_dev(old_pinned_dev);
dt_dev_process_preview2(new_pinned_dev);
// Update the pin button without re-firing its toggled callback, which would
// call dt_dev_toggle_preview2_pinned and undo the pin.
if(dev->preview2.pin_button)
{
g_signal_handlers_block_matched(G_OBJECT(dev->preview2.pin_button),
G_SIGNAL_MATCH_DATA, 0, 0, NULL, NULL, dev);
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(dev->preview2.pin_button), TRUE);
g_signal_handlers_unblock_matched(G_OBJECT(dev->preview2.pin_button),
G_SIGNAL_MATCH_DATA, 0, 0, NULL, NULL, dev);
gtk_widget_set_tooltip_text(dev->preview2.pin_button, _("unpin image"));
}
dt_toast_log(_("image pinned"));
}
// Pin the currently-edited image, cloning its in-memory pipeline state so
// that unsaved edits are reflected immediately in the second window.
static void _pin_image(dt_develop_t *dev)
{
dt_develop_t *pinned_dev = _alloc_pinned_dev(dev);
if(!pinned_dev)
{
dev->preview2_pinned = FALSE;
dt_toast_log(_("failed to create pinned develop"));
return;
}
const dt_imgid_t imgid = dev->image_storage.id;
dt_lock_image(imgid);
_dt_dev_load_raw(pinned_dev, imgid);
dt_pthread_mutex_lock(&darktable.dev_threadsafe);
pinned_dev->iop = _duplicate_iop_list(pinned_dev, dev);
for(GList *m = pinned_dev->iop; m; m = g_list_next(m))
dt_iop_reload_defaults((dt_iop_module_t *)m->data);
pinned_dev->forms = dt_masks_dup_forms_deep(dev->forms, NULL);
pinned_dev->iop_instance = dev->iop_instance;
pinned_dev->history = _duplicate_history_list(pinned_dev, dev->history);
pinned_dev->history_end = dev->history_end;
pinned_dev->history_last_module = _find_cloned_module(pinned_dev, dev->history_last_module);
pinned_dev->iop_order_version = dev->iop_order_version;
pinned_dev->iop_order_list = dt_ioppr_iop_order_copy_deep(dev->iop_order_list);
memcpy(&pinned_dev->chroma, &dev->chroma, sizeof(dt_dev_chroma_t));
pinned_dev->chroma.temperature = _find_cloned_module(pinned_dev, dev->chroma.temperature);
pinned_dev->chroma.adaptation = _find_cloned_module(pinned_dev, dev->chroma.adaptation);
dt_pthread_mutex_unlock(&darktable.dev_threadsafe);
dt_unlock_image(imgid);
_activate_pinned_dev(dev, pinned_dev);
}
static void _unpin_image(dt_develop_t *dev)
{
if(dev->preview2_pinned_dev)
{
_cleanup_pinned_dev(dev->preview2_pinned_dev);
dev->preview2_pinned_dev = NULL;
}
// Force main dev's preview2 pipe to update
dev->preview2.pipe->status = DT_DEV_PIXELPIPE_DIRTY;
dev->preview2.pipe->changed |= DT_DEV_PIPE_SYNCH;
// Update the pin button without re-firing its toggled callback.
if(dev->preview2.pin_button)
{
g_signal_handlers_block_matched(G_OBJECT(dev->preview2.pin_button),
G_SIGNAL_MATCH_DATA, 0, 0, NULL, NULL, dev);
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(dev->preview2.pin_button), FALSE);
g_signal_handlers_unblock_matched(G_OBJECT(dev->preview2.pin_button),
G_SIGNAL_MATCH_DATA, 0, 0, NULL, NULL, dev);
gtk_widget_set_tooltip_text(dev->preview2.pin_button, _("pin current image"));
}
dt_toast_log(_("image unpinned"));
}
void dt_dev_toggle_preview2_pinned(dt_develop_t *dev)
{
if(!dev) return;
// If we're trying to pin, validate the image first
if(!dev->preview2_pinned)
{
if(!dt_is_valid_imgid(dev->image_storage.id))
{
dt_toast_log(_("no valid image to pin"));
return;
}
if(dev->full.pipe && dev->full.pipe->loading)
{
dt_toast_log(_("please wait for image to load"));
return;
}
}
dev->preview2_pinned = !dev->preview2_pinned;
if(dev->preview2_pinned)
_pin_image(dev);
else
_unpin_image(dev);
// Force a redraw of the second window
if(dev->preview2.widget)
gtk_widget_queue_draw(dev->preview2.widget);
}
void dt_dev_pin_image(dt_develop_t *dev, const dt_imgid_t imgid)
{
if(!dev || !dt_is_valid_imgid(imgid)) return;
// If pinning the currently-edited image, clone the in-memory pipeline so
// that the currently selected history point is respected (same as the
// regular pin button behaviour).
if(imgid == dev->image_storage.id)
{
_pin_image(dev);
return;
}
dt_develop_t *pinned_dev = _alloc_pinned_dev(dev);
if(!pinned_dev)
{
dt_toast_log(_("failed to create pinned develop"));
return;
}
// Load the image with its own history from the database
dt_dev_load_image(pinned_dev, imgid);
_activate_pinned_dev(dev, pinned_dev);
}
void dt_dev_invalidate_preview(dt_develop_t *dev)
{
assert(dev);
dev->preview_pipe->status = DT_DEV_PIXELPIPE_DIRTY;
dev->timestamp++;
if(dev->full.pipe)
dev->full.pipe->input_timestamp = dev->timestamp;
if(dev->preview2.pipe)
dev->preview2.pipe->input_timestamp = dev->timestamp;
}
static void _dev_average_delay_update(const dt_times_t *start,
uint32_t *average_delay)
{
dt_times_t end;
dt_get_times(&end);
*average_delay += ((end.clock - start->clock) * 1000 / DT_DEV_AVERAGE_DELAY_COUNT
- *average_delay / DT_DEV_AVERAGE_DELAY_COUNT);
}
void dt_dev_process_image_job(dt_develop_t *dev,
dt_dev_viewport_t *port,
dt_dev_pixelpipe_t *pipe,
dt_signal_t signal,
const int devid)
{
if(dev->full.pipe->loading && pipe != dev->full.pipe)
{
// raw is already loading, no use starting another file access, we wait.
return;
}
if(port == &dev->preview2 && !(port->widget && GTK_IS_WIDGET(port->widget)))
{
return;
}
dt_pthread_mutex_lock(&pipe->mutex);
if(dev->gui_leaving || (dt_atomic_get_int(&pipe->shutdown) != DT_DEV_PIXELPIPE_STOP_NO))
{
dt_pthread_mutex_unlock(&pipe->mutex);
return;
}
dt_control_busy_enter();
pipe->input_timestamp = dev->timestamp;
// let gui know to draw preview instead of us, if it's there:
pipe->status = DT_DEV_PIXELPIPE_RUNNING;
dt_times_t start;
dt_get_perf_times(&start);
dt_mipmap_buffer_t buf;
dt_mipmap_cache_get(&buf, dev->image_storage.id,
port ? DT_MIPMAP_FULL : DT_MIPMAP_F,
port ? DT_MIPMAP_BLOCKING : DT_MIPMAP_BEST_EFFORT,
'r');
dev->image_storage.load_status = buf.loader_status;
dt_show_times(&start, "[dt_dev_process_image_job] loading image.");
// failed to load raw?
if(!buf.buf)
{
dt_control_busy_leave();
pipe->status = DT_DEV_PIXELPIPE_DIRTY;
dt_pthread_mutex_unlock(&pipe->mutex);
dev->image_invalid_cnt++;
return; // not loaded yet. load will issue a gtk redraw on
// completion, which in turn will trigger us again later.
}
dt_dev_pixelpipe_set_input(pipe, dev, (float *)buf.buf, buf.width, buf.height,
port ? 1.0 : buf.iscale);
// We require calculation of pixelpipe dimensions via dt_dev_pixelpipe_change() in these cases
gboolean initial_change = pipe->loading || dev->image_force_reload || pipe->input_changed;
// adjust pipeline according to changed flag set by {add,pop}_history_item.
if(pipe->loading)
{
// init pixel pipeline
dt_pthread_mutex_lock(&dev->history_mutex);
dt_dev_pixelpipe_cleanup_nodes(pipe);
dt_dev_pixelpipe_create_nodes(pipe, dev);
dt_pthread_mutex_unlock(&dev->history_mutex);
if(pipe == dev->full.pipe)
{
if(dev->image_force_reload) dt_dev_pixelpipe_cache_flush(pipe);
dev->image_force_reload = FALSE;
if(dev->gui_attached)
{
// during load, a mipf update could have been issued.
dev->preview_pipe->input_changed = TRUE;
dev->preview_pipe->status = DT_DEV_PIXELPIPE_DIRTY;
dev->preview_pipe->changed |= DT_DEV_PIPE_SYNCH;
dev->preview2.pipe->input_changed = TRUE;
dev->preview2.pipe->status = DT_DEV_PIXELPIPE_DIRTY;
dev->preview2.pipe->changed |= DT_DEV_PIPE_SYNCH;
dev->gui_synch = TRUE; // notify gui thread we want to synch
// (call gui_update on the modules)
}
else
{
// Explicitly set loading to FALSE to avoid a race condition
// where the pipe is still marked as loading when the caller
// checks it. Without this, the caller might wait for the
// pipe to finish loading, but it never will. This was causing
// failure to render composites in overlay.c and watermark.c.
pipe->loading = FALSE;
if(dev->preview_pipe) dev->preview_pipe->loading = FALSE;
if(dev->preview2.pipe) dev->preview2.pipe->loading = FALSE;
}
pipe->changed |= DT_DEV_PIPE_SYNCH;
}
else
{
dt_dev_pixelpipe_cache_flush(pipe);
pipe->loading = FALSE;
}
}
if(port != &dev->full && pipe->input_changed)
{
dt_dev_pixelpipe_cache_flush(pipe);
pipe->input_changed = FALSE;
}
int restarts = 0; // to check looping
restart:
if(dev->gui_leaving)
{
dt_mipmap_cache_release(&buf);
dt_control_busy_leave();
pipe->status = DT_DEV_PIXELPIPE_INVALID;
dt_pthread_mutex_unlock(&pipe->mutex);
return;
}
if(port == &dev->full)
pipe->input_timestamp = dev->timestamp;
const gboolean changing = (pipe->changed != DT_DEV_PIPE_UNCHANGED) || initial_change;
const gboolean port_loading = port && pipe->loading;
const gboolean require_zoom_test = (pipe->changed & ~DT_DEV_PIPE_ZOOMED) || initial_change;
initial_change = FALSE; // don't enforce dt_dev_pixelpipe_change() for restarts
const float anticipate_move = pipe->changed & DT_DEV_PIPE_ZOOMED
? dt_conf_get_float("darkroom/ui/anticipate_move")
: 1.0f;
/* dt_dev_pixelpipe_change()
locks history mutex while syncing nodes
finally calculates dimensions
leaves clean pipe->changed
*/
if(changing || port_loading)
dt_dev_pixelpipe_change(pipe, dev);
float scale = 1.0f;
int window_width = G_MAXINT;
int window_height = G_MAXINT;
float zoom_x = 0.0f;
float zoom_y = 0.0f;
if(port)
{
/* if just changed to an image with a different aspect ratio or
altered image orientation, the prior zoom xy could now be beyond
the image boundary.
dt_dev_zoom_move() possibly leaves port->pipe->changed DT_DEV_PIPE_ZOOMED;
and might redraw the widgets as a side effect
*/
if(port_loading || require_zoom_test)
{
dt_print_pipe(DT_DEBUG_PIPE, "dt_dev_zoom_move", pipe, NULL, DT_DEVICE_NONE, NULL, NULL, "%s%s",
port_loading ? "port_loading " : "",
require_zoom_test ? "required_test" : "");
dt_dev_zoom_move(port, DT_ZOOM_MOVE, 0.0f, 0, 0.0f, 0.0f, TRUE);
}
// determine scale according to current dimensions
dt_dev_zoom_t zoom;
int closeup;
dt_dev_get_viewport_params(port, &zoom, &closeup, &zoom_x, &zoom_y);
scale = dt_dev_get_zoom_scale(port, zoom, 1.0f, FALSE) * port->ppd;
// Make sure we always have enough data for the port's width & height
const int cscale = 1 << closeup;
window_width = port->width * port->ppd * anticipate_move / cscale + 2*cscale;
window_height = port->height * port->ppd * anticipate_move / cscale + 2*cscale;
}
const int pipe_width = scale * pipe->processed_width;
const int pipe_height = scale * pipe->processed_height;
const int wd = MIN(window_width, pipe_width);
const int ht = MIN(window_height, pipe_height);
const int x = port ? CLAMP(pipe_width * (.5 + zoom_x) - wd / 2, 0, pipe_width - wd) : 0;
const int y = port ? CLAMP(pipe_height * (.5 + zoom_y) - ht / 2, 0, pipe_height - ht) : 0;
dt_get_times(&start);
const gboolean early = dt_dev_pixelpipe_process(pipe, dev, x, y, wd, ht, scale, devid);
const dt_dev_pixelpipe_stopper_t shutdown = dt_atomic_exch_int(&pipe->shutdown, DT_DEV_PIXELPIPE_STOP_NO);
const gboolean stopped = early || shutdown != DT_DEV_PIXELPIPE_STOP_NO;
const dt_iop_roi_t proi = (dt_iop_roi_t) {.x = x, .y = y, .width = wd, .height = ht, .scale = scale };
dt_print_pipe(DT_DEBUG_PIPE, stopped ? "pixelpipe_process stopped" : "pixelpipe_process good",
pipe, NULL, DT_DEVICE_NONE, &proi, NULL, "%s%s %s%s%s%s%s",
restarts > 5 ? "LOOPING " : "",
dt_dev_pixelpipe_shutdown_to_str(shutdown),
dev->image_force_reload ? "image_force_reload " : "",
pipe->loading ? "pipe_loading " : "",
pipe->input_changed ? "pipe_input_changed " : "",
pipe->changed & DT_DEV_PIPE_ZOOMED ? "zoomed " : "",
pipe->changed & DT_DEV_PIPE_SYNCH ? "synch" : "");
restarts++;
if(stopped)
{
// In some cases we don't restart the pipe but exit with DT_DEV_PIXELPIPE_INVALID status, see _dev_pixelpipe_early_exit()
const gboolean img_changed = dev->image_force_reload || pipe->loading || pipe->input_changed;
if(img_changed)
{
if(dev->image_force_reload)
{
dt_dev_pixelpipe_cache_flush(pipe);
dev->image_force_reload = FALSE;
}
dt_mipmap_cache_release(&buf);
dt_control_busy_leave();
pipe->status = DT_DEV_PIXELPIPE_INVALID;
dt_pthread_mutex_unlock(&pipe->mutex);
return;
}
/* pixelpipe stopped due to changed pipe nodes, HQ mode changes or module aborts
while processing the pipe. All require restarts as pipe status is not valid yet.
*/
if(shutdown != DT_DEV_PIXELPIPE_STOP_NO)
{
dt_print_pipe(DT_DEBUG_PIPE | DT_DEBUG_VERBOSE, "image_job restart", pipe, NULL, DT_DEVICE_NONE, &proi, NULL);
goto restart;
}
}
/* Restarts are required because of
- DT_DEV_PIPE_SYNCH, DT_DEV_PIPE_TOP_CHANGED or DT_DEV_PIPE_REMOVE if there was
a history change without taken dt_dev_pixelpipe_change()
- DT_DEV_PIPE_ZOOMED if we miss pixelpipe dimension.
*/
if(port && pipe->changed != DT_DEV_PIPE_UNCHANGED)
{
/* A "special case" handling
If a non-image pixelpipe finished with `stopped == FALSE` and `changed == DT_DEV_PIPE_ZOOMED`
we don't have to restart but only have to calculate it's dimension and clear pipe->changed
avoiding superfluous pixelpipe runs.
With every restart - even without UI actions - we update zoom, x, y, wd and ht, results can
oscillate and might never converge resulting in lots of restarts or even infinite loops.
*/
if(pipe->changed == DT_DEV_PIPE_ZOOMED && !stopped && !dt_pipe_is_image(pipe))
{
dt_dev_pixelpipe_get_dimensions(pipe, dev, pipe->iwidth, pipe->iheight,
&pipe->processed_width,
&pipe->processed_height);
pipe->changed = DT_DEV_PIPE_UNCHANGED;
}
else
{
dt_print_pipe(DT_DEBUG_PIPE | DT_DEBUG_VERBOSE, "image_job restart", pipe, NULL, DT_DEVICE_NONE, &proi, NULL);
goto restart;
}
}
dt_print_pipe(DT_DEBUG_PIPE, "process_image_job done", pipe, NULL, DT_DEVICE_NONE, &proi, NULL, "\n");
dt_show_times_f(&start,
"[dev_process_image] pixel pipeline", "processing `%s'",
dev->image_storage.filename);
_dev_average_delay_update(&start, &pipe->average_delay);
pipe->status = DT_DEV_PIXELPIPE_VALID;
pipe->loading = FALSE;
dev->image_invalid_cnt = 0;
dt_mipmap_cache_release(&buf);
// if a widget needs to be redrawn there's the DT_SIGNAL_*_PIPE_FINISHED signals
dt_control_busy_leave();
dt_pthread_mutex_unlock(&pipe->mutex);
const gboolean signalling = dev->gui_attached && !dev->gui_leaving && signal != -1;
if(port) // reminder: only the preview pipe is called without a port
{
if(signalling && signal != DT_SIGNAL_DEVELOP_PREVIEW_PIPE_FINISHED)
DT_CONTROL_SIGNAL_RAISE(signal);
else if(port->widget && !dev->gui_attached)
// pinned dev has gui_attached=FALSE, so manually queue redraw
dt_control_queue_redraw_widget(port->widget);
return;
}
// rest is for preview pipe only
if(!dev->history_postpone_invalidate)
dt_image_update_final_size(dev->preview_pipe->output_imgid);
if(signalling) // raise this after possibly updating the final size
DT_CONTROL_SIGNAL_RAISE(signal);
dev->gui_previous_pipe_time = dt_get_wtime();
#ifdef USE_LUA
dt_lua_async_call_alien(dt_lua_event_trigger_wrapper,
0, NULL, NULL,
LUA_ASYNC_TYPENAME, "const char*", "pixelpipe-processing-complete",
LUA_ASYNC_TYPENAME, "dt_lua_image_t", GINT_TO_POINTER(dev->image_storage.id),
LUA_ASYNC_DONE);
#endif
}
static inline void _dt_dev_load_pipeline_defaults(dt_develop_t *dev)
{
for(const GList *modules = g_list_last(dev->iop);
modules;
modules = g_list_previous(modules))
{
dt_iop_module_t *module = modules->data;
dt_iop_reload_defaults(module);
}
}
// load the raw and get the new image struct, blocking in gui thread
static inline void _dt_dev_load_raw(dt_develop_t *dev,
const dt_imgid_t imgid)
{
// first load the raw, to make sure dt_image_t will contain all and correct data.
dt_mipmap_buffer_t buf;
dt_times_t start;
dt_get_perf_times(&start);
dt_mipmap_cache_get(&buf, imgid, DT_MIPMAP_FULL, DT_MIPMAP_BLOCKING, 'r');
dt_mipmap_cache_release(&buf);
dt_show_times(&start, "[dt_dev_load_raw] loading the image.");
const dt_image_t *image = dt_image_cache_get(imgid, 'r');
dev->image_storage = *image;
dt_image_cache_read_release(image);
// dev->requested_id = (dev->image_storage.load_status == DT_IMAGEIO_OK) ? dev->image_storage.id : 0;
dev->requested_id = dev->image_storage.id;
}
void dt_dev_reload_image(dt_develop_t *dev,
const dt_imgid_t imgid)
{
_dt_dev_load_raw(dev, imgid);
dev->image_force_reload = TRUE;
dev->full.pipe->loading = dev->preview_pipe->loading = dev->preview2.pipe->loading = TRUE;
dev->full.pipe->changed |= DT_DEV_PIPE_SYNCH;
dt_dev_invalidate(dev); // only invalidate image, preview will follow once it's loaded.
}
float dt_dev_get_zoom_scale(dt_dev_viewport_t *port,
const dt_dev_zoom_t zoom,
const int closeup_factor,
const gboolean preview)
{
float zoom_scale = .0f;
int procw, proch;
dt_dev_get_processed_size(port, &procw, &proch);
const float w = (float)port->width / procw;
const float h = (float)port->height / proch;
switch(zoom)
{
case DT_ZOOM_FIT:
zoom_scale = fminf(w, h);
break;
case DT_ZOOM_FILL:
zoom_scale = fmaxf(w, h);
break;
case DT_ZOOM_1:
zoom_scale = closeup_factor;
break;
default: // DT_ZOOM_FREE
zoom_scale = port->zoom_scale;
break;
}
if(!zoom_scale) zoom_scale = 1.0f;
if(preview)
zoom_scale *= darktable.develop->preview_pipe->iscale;
return zoom_scale;
}
float dt_dev_get_zoom_scale_full(void)
{
dt_dev_zoom_t zoom;
int closeup;
dt_dev_get_viewport_params(&darktable.develop->full, &zoom, &closeup, NULL, NULL);
return dt_dev_get_zoom_scale(&darktable.develop->full, zoom, 1 << closeup, TRUE);
}
float dt_dev_get_zoomed_in(void)
{
dt_dev_zoom_t zoom;
int closeup;
dt_dev_get_viewport_params(&darktable.develop->full, &zoom, &closeup, NULL, NULL);
const float min_scale = dt_dev_get_zoom_scale(&darktable.develop->full, DT_ZOOM_FIT, 1<<closeup, FALSE);
const float cur_scale = dt_dev_get_zoom_scale(&darktable.develop->full, zoom, 1<<closeup, FALSE);
return cur_scale / min_scale;
}
void dt_dev_load_image(dt_develop_t *dev,
const dt_imgid_t imgid)
{
dt_lock_image(imgid);
_dt_dev_load_raw(dev, imgid);
if(dev->full.pipe)