forked from darktable-org/darktable
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpixelpipe_hb.c
More file actions
3725 lines (3288 loc) · 141 KB
/
pixelpipe_hb.c
File metadata and controls
3725 lines (3288 loc) · 141 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-2025 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 "common/color_picker.h"
#include "common/colorspaces.h"
#include "common/histogram.h"
#include "common/opencl.h"
#include "common/iop_order.h"
#include "common/imagebuf.h"
#include "control/control.h"
#include "control/signal.h"
#include "develop/blend.h"
#include "develop/format.h"
#include "develop/imageop_math.h"
#include "develop/develop.h"
#include "develop/tiling.h"
#include "develop/masks.h"
#include "gui/gtk.h"
#include "imageio/imageio_common.h"
#include "libs/colorpicker.h"
#include "libs/lib.h"
#include "gui/color_picker_proxy.h"
#include <assert.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <unistd.h>
#define DT_DEV_AVERAGE_DELAY_START 250
#define DT_DEV_PREVIEW_AVERAGE_DELAY_START 50
typedef enum dt_pixelpipe_flow_t
{
PIXELPIPE_FLOW_NONE = 0,
PIXELPIPE_FLOW_HISTOGRAM_NONE = 1 << 0,
PIXELPIPE_FLOW_HISTOGRAM_ON_CPU = 1 << 1,
PIXELPIPE_FLOW_HISTOGRAM_ON_GPU = 1 << 2,
PIXELPIPE_FLOW_PROCESSED_ON_CPU = 1 << 3,
PIXELPIPE_FLOW_PROCESSED_ON_GPU = 1 << 4,
PIXELPIPE_FLOW_PROCESSED_WITH_TILING = 1 << 5,
PIXELPIPE_FLOW_BLENDED_ON_CPU = 1 << 6,
PIXELPIPE_FLOW_BLENDED_ON_GPU = 1 << 7
} dt_pixelpipe_flow_t;
#include "develop/pixelpipe_cache.c"
const char *dt_dev_pixelpipe_type_to_str(const dt_dev_pixelpipe_type_t pipe_type)
{
const gboolean fast = pipe_type & DT_DEV_PIXELPIPE_FAST;
const gboolean dev = pipe_type & DT_DEV_PIXELPIPE_IMAGE;
#define PT_STR(name) fast ? dev ? #name "/fast/dev" \
: #name "/fast" \
: dev ? #name "/dev" \
: #name
switch(pipe_type & DT_DEV_PIXELPIPE_ANY)
{
case DT_DEV_PIXELPIPE_PREVIEW: return PT_STR(preview);
case DT_DEV_PIXELPIPE_PREVIEW2: return PT_STR(preview2);
case DT_DEV_PIXELPIPE_FULL: return PT_STR(full);
case DT_DEV_PIXELPIPE_THUMBNAIL: return PT_STR(thumbnail);
case DT_DEV_PIXELPIPE_EXPORT: return PT_STR(export);
default: return PT_STR(unknown);
}
#undef PT_STR
}
void dt_print_pipe_ext(const char *title,
const dt_dev_pixelpipe_t *pipe,
const dt_iop_module_t *module,
const int device,
const dt_iop_roi_t *roi_in,
const dt_iop_roi_t *roi_out,
const char *msg, ...)
{
char vtit[128];
char vmod[128];
char order[64] = { 0 };
char dev[32] = { 0 };
char vbuf[1024] = { 0 };
char roi[128] = { 0 };
char roo[128] = { 0 };
char pname[32] = { 0 };
char masking[64] = { 0 };
char area[32];
char shift[32];
snprintf(vtit, sizeof(vtit), "%s", title);
if(module)
{
if(module->iop_order == INT_MAX)
snprintf(order, sizeof(order), "MAX");
else if(module->iop_order < INT_MAX && module->iop_order >= 0)
snprintf(order, sizeof(order), "%i", module->iop_order);
else
snprintf(order, sizeof(order), "NEG %i", module->iop_order);
}
snprintf(vmod, sizeof(vmod), "%s%s",
module ? module->op : "",
module ? dt_iop_get_instance_id(module) : "");
if(device == DT_DEVICE_CPU)
snprintf(dev, sizeof(dev), "CPU");
else if(device > DT_DEVICE_CPU)
snprintf(dev, sizeof(dev), "CL%i", device);
else if(device != DT_DEVICE_NONE)
snprintf(dev, sizeof(dev), "??? %i", device);
const gboolean show_roo = roi_out && (!roi_in || memcmp(roi_in, roi_out, sizeof(dt_iop_roi_t)));
if(roi_in)
{
snprintf(shift, sizeof(shift), "(%i/%i)", roi_in->x, roi_in->y);
snprintf(area, sizeof(area), "%ix%i", roi_in->width, roi_in->height);
snprintf(roi, sizeof(roi), "%11s %10s sc=%.3f%s", shift, area, roi_in->scale, show_roo ? "" : ";");
}
if(show_roo)
{
snprintf(shift, sizeof(shift), "(%i/%i)", roi_out->x, roi_out->y);
snprintf(area, sizeof(area), "%ix%i", roi_out->width, roi_out->height);
snprintf(roo, sizeof(roo), " --> %11s %10s sc=%.3f;", shift, area, roi_out->scale);
}
if(pipe)
{
snprintf(pname, sizeof(pname), "[%s%s]",
dt_dev_pixelpipe_type_to_str(pipe->type),
(pipe->type & (DT_DEV_PIXELPIPE_FULL | DT_DEV_PIXELPIPE_PREVIEW2))
&& darktable.develop->late_scaling.enabled ? " HQ" : "");
if(pipe->mask_display == DT_DEV_PIXELPIPE_DISPLAY_PASSTHRU)
snprintf(masking, sizeof(masking), " passthru");
else if(pipe->mask_display & DT_DEV_PIXELPIPE_DISPLAY_ANY)
snprintf(masking, sizeof(masking),
" masking=%#x %s%s", pipe->mask_display,
pipe->bypass_blendif ? ", bypass blend" : "",
pipe->mask_display & DT_DEV_PIXELPIPE_DISPLAY_STICKY ? ", sticky" : "");
}
va_list ap;
va_start(ap, msg);
vsnprintf(vbuf, sizeof(vbuf), msg, ap);
va_end(ap);
dt_print_ext("%-25s %-3s %-16s %-22s %4s %s%s%s%s",
vtit, dev, pname, vmod, order, roi, roo, masking, vbuf);
}
gboolean dt_dev_pixelpipe_init_export(dt_dev_pixelpipe_t *pipe,
const int32_t width,
const int32_t height,
const int levels,
const gboolean store_masks)
{
const gboolean res =
dt_dev_pixelpipe_init_cached(pipe, sizeof(float) * 4 * width * height,
DT_PIPECACHE_MIN, 0);
pipe->type = DT_DEV_PIXELPIPE_EXPORT;
pipe->levels = levels;
pipe->store_all_raster_masks = store_masks;
return res;
}
gboolean dt_dev_pixelpipe_init_thumbnail(dt_dev_pixelpipe_t *pipe,
const int32_t width,
const int32_t height)
{
const gboolean res =
dt_dev_pixelpipe_init_cached(pipe, sizeof(float) * 4 * width * height,
DT_PIPECACHE_MIN, 0);
pipe->type = DT_DEV_PIXELPIPE_THUMBNAIL;
return res;
}
gboolean dt_dev_pixelpipe_init_dummy(dt_dev_pixelpipe_t *pipe,
const int32_t width,
const int32_t height)
{
const gboolean res =
dt_dev_pixelpipe_init_cached(pipe, sizeof(float) * 4 * width * height, 0, 0);
pipe->type = DT_DEV_PIXELPIPE_THUMBNAIL;
pipe->average_delay = DT_DEV_AVERAGE_DELAY_START;
return res;
}
gboolean dt_dev_pixelpipe_init_preview(dt_dev_pixelpipe_t *pipe)
{
const gboolean res =
dt_dev_pixelpipe_init_cached(pipe, 0, darktable.pipe_cache ? 12 : DT_PIPECACHE_MIN, 0);
pipe->type = DT_DEV_PIXELPIPE_PREVIEW;
pipe->average_delay = DT_DEV_PREVIEW_AVERAGE_DELAY_START;
return res;
}
gboolean dt_dev_pixelpipe_init_preview2(dt_dev_pixelpipe_t *pipe)
{
const gboolean res =
dt_dev_pixelpipe_init_cached(pipe, 0, darktable.pipe_cache ? 5 : DT_PIPECACHE_MIN, 0);
pipe->type = DT_DEV_PIXELPIPE_PREVIEW2;
pipe->average_delay = DT_DEV_PREVIEW_AVERAGE_DELAY_START;
return res;
}
gboolean dt_dev_pixelpipe_init(dt_dev_pixelpipe_t *pipe)
{
const size_t csize = MAX(64*1024*1024, darktable.dtresources.mipmap_memory / 4);
const gboolean res =
dt_dev_pixelpipe_init_cached(pipe, 0, darktable.pipe_cache ? 64 : DT_PIPECACHE_MIN,
csize);
pipe->type = DT_DEV_PIXELPIPE_FULL;
return res;
}
gboolean dt_dev_pixelpipe_init_cached(dt_dev_pixelpipe_t *pipe,
const size_t size,
const int32_t entries,
const size_t memlimit)
{
pipe->devid = DT_DEVICE_CPU;
pipe->loading = FALSE;
pipe->input_changed = FALSE;
pipe->changed = DT_DEV_PIPE_UNCHANGED;
pipe->status = DT_DEV_PIXELPIPE_DIRTY;
pipe->processed_width = pipe->backbuf_width = pipe->iwidth = pipe->final_width = 0;
pipe->processed_height = pipe->backbuf_height = pipe->iheight = pipe->final_height = 0;
pipe->nodes = NULL;
pipe->backbuf_size = size;
pipe->cache_obsolete = FALSE;
pipe->backbuf = NULL;
pipe->backbuf_scale = 0.0f;
memset(pipe->backbuf_zoom_pos, 0, sizeof(dt_dev_zoom_pos_t));
pipe->output_imgid = NO_IMGID;
memset(&pipe->scharr, 0, sizeof(dt_dev_detail_mask_t));
pipe->want_detail_mask = FALSE;
pipe->processing = FALSE;
dt_atomic_set_int(&pipe->shutdown, DT_DEV_PIXELPIPE_STOP_NO);
pipe->opencl_error = FALSE;
pipe->tiling = FALSE;
pipe->mask_display = DT_DEV_PIXELPIPE_DISPLAY_NONE;
pipe->bypass_blendif = FALSE;
pipe->input_timestamp = 0;
pipe->levels = IMAGEIO_RGB | IMAGEIO_INT8;
dt_pthread_mutex_init(&pipe->mutex, NULL);
dt_pthread_mutex_init(&pipe->backbuf_mutex, NULL);
dt_pthread_mutex_init(&pipe->busy_mutex, NULL);
pipe->icc_type = DT_COLORSPACE_NONE;
pipe->icc_filename = NULL;
pipe->icc_intent = DT_INTENT_LAST;
pipe->iop = NULL;
pipe->iop_order_list = NULL;
pipe->forms = NULL;
pipe->store_all_raster_masks = FALSE;
pipe->work_profile_info = NULL;
pipe->input_profile_info = NULL;
pipe->output_profile_info = NULL;
pipe->runs = 0;
pipe->bcache_data = NULL;
pipe->bcache_hash = DT_INVALID_HASH;
return dt_dev_pixelpipe_cache_init(pipe, entries, size, memlimit);
}
size_t dt_get_available_pipe_mem(const dt_dev_pixelpipe_t *pipe)
{
const size_t allmem = dt_get_available_mem();
return MAX(DT_MEGA, allmem / (pipe->type & DT_DEV_PIXELPIPE_THUMBNAIL ? 3 : 1));
}
static void get_output_format(dt_iop_module_t *module,
dt_dev_pixelpipe_t *pipe,
dt_dev_pixelpipe_iop_t *piece,
dt_develop_t *dev,
dt_iop_buffer_dsc_t *dsc)
{
if(module) return module->output_format(module, pipe, piece, dsc);
// first input.
*dsc = pipe->image.buf_dsc;
if(!(dt_image_is_raw(&pipe->image)))
{
// image max is normalized before
for(int k = 0; k < 4; k++) dsc->processed_maximum[k] = 1.0f;
}
}
void dt_dev_pixelpipe_set_input(dt_dev_pixelpipe_t *pipe,
dt_develop_t *dev,
float *input,
const int width,
const int height,
const float iscale)
{
pipe->iwidth = width;
pipe->iheight = height;
pipe->iscale = iscale;
pipe->input = input;
pipe->image = dev->image_storage;
get_output_format(NULL, pipe, NULL, dev, &pipe->dsc);
}
void dt_dev_pixelpipe_set_icc(dt_dev_pixelpipe_t *pipe,
const dt_colorspaces_color_profile_type_t icc_type,
const gchar *icc_filename,
const dt_iop_color_intent_t icc_intent)
{
pipe->icc_type = icc_type;
g_free(pipe->icc_filename);
pipe->icc_filename = g_strdup(icc_filename ? icc_filename : "");
pipe->icc_intent = icc_intent;
}
void dt_dev_pixelpipe_cleanup(dt_dev_pixelpipe_t *pipe)
{
dt_pthread_mutex_lock(&pipe->backbuf_mutex);
// blocks while busy and sets shutdown bit:
dt_dev_pixelpipe_cleanup_nodes(pipe);
// so now it's safe to clean up cache:
dt_dev_pixelpipe_cache_cleanup(pipe);
dt_free_align(pipe->bcache_data);
pipe->icc_type = DT_COLORSPACE_NONE;
g_free(pipe->icc_filename);
pipe->icc_filename = NULL;
if(pipe->type & DT_DEV_PIXELPIPE_SCREEN) g_free(pipe->backbuf);
pipe->backbuf = NULL;
pipe->backbuf_width = 0;
pipe->backbuf_height = 0;
dt_pthread_mutex_unlock(&pipe->backbuf_mutex);
dt_pthread_mutex_destroy(&pipe->backbuf_mutex);
pipe->output_imgid = NO_IMGID;
if(pipe->forms)
{
g_list_free_full(pipe->forms, (void (*)(void *))dt_masks_free_form);
pipe->forms = NULL;
}
dt_pthread_mutex_destroy(&pipe->busy_mutex);
dt_pthread_mutex_destroy(&pipe->mutex);
}
void dt_dev_pixelpipe_cleanup_nodes(dt_dev_pixelpipe_t *pipe)
{
// tell pipe that it should shut itself down if currently running
dt_atomic_set_int(&pipe->shutdown, DT_DEV_PIXELPIPE_STOP_NODES);
// FIXME: either this or all process() -> gdk mutices have to be changed!
// (this is a circular dependency on busy_mutex and the gdk mutex)
// [[does the above still apply?]]
dt_pthread_mutex_lock(&pipe->busy_mutex); // block until the pipe has shut down
// destroy all nodes
for(GList *nodes = pipe->nodes; nodes; nodes = g_list_next(nodes))
{
dt_dev_pixelpipe_iop_t *piece = nodes->data;
// printf("cleanup module `%s'\n", piece->module->name());
piece->module->cleanup_pipe(piece->module, pipe, piece);
free(piece->blendop_data);
piece->blendop_data = NULL;
dt_free_align(piece->histogram);
piece->histogram = NULL;
g_hash_table_destroy(piece->raster_masks);
piece->raster_masks = NULL;
free(piece);
}
g_list_free(pipe->nodes);
pipe->nodes = NULL;
dt_dev_clear_scharr_mask(pipe);
pipe->want_detail_mask = FALSE;
// also cleanup iop here
if(pipe->iop)
{
g_list_free(pipe->iop);
pipe->iop = NULL;
}
// and iop order
g_list_free_full(pipe->iop_order_list, free);
pipe->iop_order_list = NULL;
dt_pthread_mutex_unlock(&pipe->busy_mutex); // safe for others to mess with the pipe now
}
void dt_dev_pixelpipe_rebuild(dt_develop_t *dev)
{
dev->full.pipe->changed |= DT_DEV_PIPE_REMOVE;
dev->preview_pipe->changed |= DT_DEV_PIPE_REMOVE;
dev->preview2.pipe->changed |= DT_DEV_PIPE_REMOVE;
dev->full.pipe->cache_obsolete = TRUE;
dev->preview_pipe->cache_obsolete = TRUE;
dev->preview2.pipe->cache_obsolete = TRUE;
// invalidate buffers and force redraw of darkroom
dt_dev_invalidate_all(dev);
}
void dt_dev_pixelpipe_create_nodes(dt_dev_pixelpipe_t *pipe,
dt_develop_t *dev)
{
dt_pthread_mutex_lock(&pipe->busy_mutex); // block until pipe is idle
// clear any pending shutdown request
dt_atomic_set_int(&pipe->shutdown, DT_DEV_PIXELPIPE_STOP_NO);
// check that the pipe was actually properly cleaned up after the last run
g_assert(pipe->nodes == NULL);
g_assert(pipe->iop == NULL);
g_assert(pipe->iop_order_list == NULL);
pipe->iop_order_list = dt_ioppr_iop_order_copy_deep(dev->iop_order_list);
// for all modules in dev:
pipe->iop = g_list_copy(dev->iop);
for(GList *modules = pipe->iop; modules; modules = g_list_next(modules))
{
dt_iop_module_t *module = modules->data;
dt_dev_pixelpipe_iop_t *piece = calloc(1, sizeof(dt_dev_pixelpipe_iop_t));
piece->enabled = module->enabled;
piece->request_histogram = DT_REQUEST_ONLY_IN_GUI;
piece->histogram_params.roi = NULL;
piece->histogram_params.bins_count = 256;
piece->histogram_stats.bins_count = 0;
piece->histogram_stats.pixels = 0;
piece->colors = module->default_colorspace(module, pipe, NULL) == IOP_CS_RAW ? 1 : 4;
piece->iscale = pipe->iscale;
piece->iwidth = pipe->iwidth;
piece->iheight = pipe->iheight;
piece->module = module;
piece->pipe = pipe;
piece->data = NULL;
piece->hash = DT_INVALID_HASH;
piece->process_cl_ready = FALSE;
piece->process_tiling_ready = FALSE;
piece->raster_masks = g_hash_table_new_full(g_direct_hash,
g_direct_equal, NULL, dt_free_align_ptr);
memset(&piece->processed_roi_in, 0, sizeof(piece->processed_roi_in));
memset(&piece->processed_roi_out, 0, sizeof(piece->processed_roi_out));
dt_iop_init_pipe(piece->module, pipe, piece);
pipe->nodes = g_list_append(pipe->nodes, piece);
}
dt_pthread_mutex_unlock(&pipe->busy_mutex); // safe for others to
// use/mess with the
// pipe now
}
// helper
static void _dev_pixelpipe_synch(dt_dev_pixelpipe_t *pipe,
dt_develop_t *dev,
GList *history)
{
dt_dev_history_item_t *hist = history->data;
// find piece in nodes list
dt_dev_pixelpipe_iop_t *piece = NULL;
const dt_image_t *img = &pipe->image;
const dt_imgid_t imgid = img->id;
const gboolean rawprep_img = dt_image_is_rawprepare_supported(img);
for(GList *nodes = pipe->nodes; nodes; nodes = g_list_next(nodes))
{
piece = nodes->data;
if(piece->module == hist->module)
{
const gboolean active = hist->enabled;
piece->enabled = active;
// the last crop module in the pixelpipe might handle the exposing if enabled
if(piece->module->flags() & IOP_FLAGS_CROP_EXPOSER)
dev->cropping.exposer = active ? piece->module : NULL;
// Styles, presets or history copy&paste might set history items
// not appropriate for the image. Fixing that seemed to be
// almost impossible after long discussions but at least we can
// test, correct and add a problem hint here.
if(dt_iop_module_is(piece->module->so, "demosaic")
|| dt_iop_module_is(piece->module->so, "rawprepare"))
{
if(rawprep_img && !active)
piece->enabled = TRUE;
else if(!rawprep_img && active)
piece->enabled = FALSE;
}
else if((dt_iop_module_is(piece->module->so, "rawdenoise"))
|| (dt_iop_module_is(piece->module->so, "hotpixels"))
|| (dt_iop_module_is(piece->module->so, "cacorrect")))
{
if(!rawprep_img && active) piece->enabled = FALSE;
}
if(piece->enabled != hist->enabled)
{
if(piece->enabled)
dt_iop_set_module_trouble_message
(piece->module,
_("enabled as required"),
_("history had module disabled but it is required for"
" this type of image.\nlikely introduced by applying a preset,"
" style or history copy&paste"),
NULL);
else
dt_iop_set_module_trouble_message
(piece->module,
_("disabled as not appropriate"),
_("history had module enabled but it is not allowed for this type"
" of image.\nlikely introduced by applying a preset, style or"
" history copy&paste"),
NULL);
dt_print_pipe(DT_DEBUG_PIPE, "pipe synch problem",
pipe, piece->module, DT_DEVICE_NONE, NULL, NULL,
"piece enabling mismatch for image %i, piece hash=%" PRIx64,
imgid, piece->hash);
}
if(active && hist->iop_order == INT_MAX)
{
piece->enabled = FALSE;
dt_print_pipe(DT_DEBUG_PARAMS | DT_DEBUG_PIPE,
"dt_dev_pixelpipe_synch",
pipe, piece->module, DT_DEVICE_NONE, NULL, NULL,
"enabled module with iop_order of INT_MAX is disabled");
}
// disable pieces if included in list
if(piece->enabled && dev->module_filter_out)
{
for(GList *m = dev->module_filter_out; m; m = g_list_next(m))
{
char *mod = (char *)(m->data);
if(dt_iop_module_is(piece->module->so, mod))
{
piece->enabled = FALSE;
dt_print_pipe(DT_DEBUG_PARAMS | DT_DEBUG_PIPE,
"dt_dev_pixelpipe_synch",
pipe, piece->module, DT_DEVICE_NONE, NULL, NULL,
"module is disabled because it's included in module_filter_out");
}
}
}
dt_iop_commit_params(hist->module, hist->params, hist->blend_params, pipe, piece);
dt_print_pipe(DT_DEBUG_PARAMS,
"committed",
pipe, piece->module, DT_DEVICE_NONE, NULL, NULL,
"%s piece hash=%" PRIx64,
piece->enabled ? "enabled " : "disabled",
piece->hash);
if(piece->enabled && piece->blendop_data)
{
const dt_develop_blend_params_t *const bp = piece->blendop_data;
const gboolean valid_mask = bp->mask_mode > DEVELOP_MASK_ENABLED;
if(!feqf(bp->details, 0.0f, 1e-6) && valid_mask)
dt_dev_pixelpipe_usedetails(piece);
}
}
}
}
void dt_dev_pixelpipe_synch_all(dt_dev_pixelpipe_t *pipe, dt_develop_t *dev)
{
dt_pthread_mutex_lock(&pipe->busy_mutex);
double start = dt_get_debug_wtime();
dev->cropping.exposer = NULL;
dt_print_pipe(DT_DEBUG_PARAMS, "synch all module defaults",
pipe, NULL, DT_DEVICE_NONE, NULL, NULL);
// call reset_params on all pieces first. This is mandatory to init
// utility modules that don't have an history stack
for(GList *nodes = pipe->nodes; nodes; nodes = g_list_next(nodes))
{
dt_dev_pixelpipe_iop_t *piece = nodes->data;
piece->hash = DT_INVALID_HASH;
piece->enabled = piece->module->default_enabled;
dt_iop_commit_params(piece->module,
piece->module->default_params,
piece->module->default_blendop_params,
pipe, piece);
}
double defaults = dt_get_debug_wtime();
dt_print_pipe(DT_DEBUG_PARAMS, "synch all module history",
pipe, NULL, DT_DEVICE_NONE, NULL, NULL);
dt_dev_clear_scharr_mask(pipe);
pipe->want_detail_mask = FALSE;
/* go through all history items and adjust params
We might call dt_dev_pixelpipe_usedetails() with want_detail_mask == FALSE
here resulting in a pipecache invalidation.
Can this somehow be avoided?
*/
GList *history = dev->history;
for(int k = 0; k < dev->history_end && history; k++)
{
_dev_pixelpipe_synch(pipe, dev, history);
history = g_list_next(history);
}
dt_print_pipe(DT_DEBUG_PARAMS,
"synch all modules done",
pipe, NULL, DT_DEVICE_NONE, NULL, NULL,
"defaults %.4fs, history %.4fs",
defaults - start, dt_get_wtime() - defaults);
dt_pthread_mutex_unlock(&pipe->busy_mutex);
}
void dt_dev_pixelpipe_synch_top(dt_dev_pixelpipe_t *pipe, dt_develop_t *dev)
{
dt_pthread_mutex_lock(&pipe->busy_mutex);
GList *history = g_list_nth(dev->history, dev->history_end - 1);
if(history)
{
dt_dev_history_item_t *hist = history->data;
dt_print_pipe(DT_DEBUG_PARAMS, "synch top history module",
pipe, hist->module, DT_DEVICE_NONE, NULL, NULL);
_dev_pixelpipe_synch(pipe, dev, history);
}
else
{
dt_print_pipe(DT_DEBUG_PARAMS, "synch top history module missing!",
pipe, NULL, DT_DEVICE_NONE, NULL, NULL);
}
dt_pthread_mutex_unlock(&pipe->busy_mutex);
}
void dt_dev_pixelpipe_change(dt_dev_pixelpipe_t *pipe, dt_develop_t *dev)
{
dt_pthread_mutex_lock(&dev->history_mutex);
dt_print_pipe(DT_DEBUG_PIPE, "dev_pixelpipe_change",
pipe, NULL, DT_DEVICE_NONE, NULL, NULL, "%s%s%s%s%s",
pipe->changed & DT_DEV_PIPE_ZOOMED ? "zoomed, " : "",
pipe->changed & DT_DEV_PIPE_TOP_CHANGED ? "top changed, " : "",
pipe->changed & DT_DEV_PIPE_SYNCH ? "synch all, " : "",
pipe->changed & DT_DEV_PIPE_REMOVE ? "pipe remove" : "",
pipe->changed == DT_DEV_PIPE_UNCHANGED ? "dimension" : "");
if(pipe->changed & (DT_DEV_PIPE_TOP_CHANGED | DT_DEV_PIPE_SYNCH | DT_DEV_PIPE_REMOVE))
{
const gboolean sync_all = pipe->changed & (DT_DEV_PIPE_SYNCH | DT_DEV_PIPE_REMOVE);
const gboolean sync_remove = pipe->changed & DT_DEV_PIPE_REMOVE;
if((pipe->changed & DT_DEV_PIPE_TOP_CHANGED) && !sync_all)
{
// only top history item changed. Not required if we synch_all
dt_dev_pixelpipe_synch_top(pipe, dev);
}
if((pipe->changed & DT_DEV_PIPE_SYNCH) && !sync_remove)
{
// pipeline topology remains intact but change all params. Not
// required if we rebuild all nodes
dt_dev_pixelpipe_synch_all(pipe, dev);
}
if(pipe->changed & DT_DEV_PIPE_REMOVE)
{
// modules have been added in between or removed. need to
// rebuild the whole pipeline.
dt_dev_pixelpipe_cleanup_nodes(pipe);
dt_dev_pixelpipe_create_nodes(pipe, dev);
dt_dev_pixelpipe_synch_all(pipe, dev);
}
}
pipe->changed = DT_DEV_PIPE_UNCHANGED;
dt_pthread_mutex_unlock(&dev->history_mutex);
dt_dev_pixelpipe_get_dimensions(pipe, dev,
pipe->iwidth, pipe->iheight,
&pipe->processed_width,
&pipe->processed_height);
}
void dt_dev_pixelpipe_usedetails(dt_dev_pixelpipe_iop_t *piece)
{
dt_dev_pixelpipe_t *pipe = piece->pipe;
if(!pipe->want_detail_mask)
{
dt_print_pipe(DT_DEBUG_PIPE, "details requested", pipe, piece->module, DT_DEVICE_NONE, NULL, NULL);
dt_dev_pixelpipe_cache_invalidate_later(pipe, 0);
pipe->want_detail_mask = TRUE;
}
}
static void _dump_pipe_pfm_diff(const char *mod,
const void *indata,
const dt_iop_roi_t *roi_in,
const int inbpp,
const void *outdata,
const dt_iop_roi_t *roi_out,
const int outbpp,
const char *pipe)
{
if(!darktable.dump_pfm_pipe) return;
if(!mod) return;
if(!dt_str_commasubstring(darktable.dump_pfm_pipe, mod)) return;
if(inbpp != outbpp) return;
if(!(inbpp == 16 || inbpp == 4)) return;
const int fchannels = inbpp / 4;
float *mixed = dt_alloc_align_float((size_t)fchannels * roi_out->width * roi_out->height);
if(!mixed) return;
const float *in = indata;
const float *out = outdata;
DT_OMP_FOR(collapse(2))
for(int row = 0; row < roi_out->height; row++)
{
for(int col = 0; col < roi_out->width; col++)
{
const size_t ox = fchannels * (row * roi_out->width + col);
const int irow = row + roi_out->y;
const int icol = col + roi_out->x;
for(int c = 0; c < fchannels; c++)
{
if((irow < roi_in->height)
&& (icol < roi_in->width)
&& (icol >= 0)
&& (irow >= 0))
{
mixed[ox+c] = fabsf(in[fchannels * (irow * roi_in->width + icol)+c] - out[ox+c]);
}
else
mixed[ox+c] = 0.0f;
}
}
}
dt_dump_pfm_file(pipe, mixed, roi_out->width, roi_out->height,
outbpp, mod, "[dt_dump_pipe_pfm]", TRUE, TRUE, TRUE);
dt_free_align(mixed);
}
// helper to get per module histogram
static void _histogram_collect(dt_dev_pixelpipe_iop_t *piece,
const void *pixel,
const dt_iop_roi_t *roi,
uint32_t **histogram,
uint32_t *histogram_max)
{
dt_dev_histogram_collection_params_t histogram_params = piece->histogram_params;
dt_histogram_roi_t histogram_roi;
// if the current module does did not specified its own ROI, use the full ROI
if(histogram_params.roi == NULL)
{
histogram_roi = (dt_histogram_roi_t){
.width = roi->width,
.height = roi->height,
.crop_x = 0,
.crop_y = 0,
.crop_right = 0,
.crop_bottom = 0
};
histogram_params.roi = &histogram_roi;
}
const dt_iop_colorspace_type_t cst =
piece->module->input_colorspace(piece->module, piece->pipe, piece);
dt_histogram_helper(&histogram_params, &piece->histogram_stats, cst,
piece->module->histogram_cst,
pixel, histogram, histogram_max,
piece->module->histogram_middle_grey,
dt_ioppr_get_pipe_work_profile_info(piece->pipe));
}
#ifdef HAVE_OPENCL
// helper to get per module histogram for OpenCL
//
// this algorithm is inefficient as hell when it comes to larger
// images. it's only acceptable as long as we work on small image
// sizes like in image preview
static void _histogram_collect_cl(const int devid,
dt_dev_pixelpipe_iop_t *piece,
cl_mem img,
const dt_iop_roi_t *roi,
uint32_t **histogram,
uint32_t *histogram_max,
float *buffer,
const size_t bufsize)
{
float *tmpbuf = NULL;
float *pixel = NULL;
// if buffer is supplied and if size fits let's use it
if(buffer && bufsize >= (size_t)roi->width * roi->height * 4 * sizeof(float))
pixel = buffer;
else
pixel = tmpbuf = dt_alloc_align_float((size_t)4 * roi->width * roi->height);
if(!pixel) return;
if(dt_opencl_copy_device_to_host(devid, pixel, img,
roi->width, roi->height, sizeof(float) * 4) != CL_SUCCESS)
{
dt_free_align(tmpbuf);
dt_print_pipe(DT_DEBUG_PIPE | DT_DEBUG_OPENCL, "[histogram_collect]",
piece->pipe, piece->module, devid, roi, NULL,
"Couldn't read histogramm data");
return;
}
dt_dev_histogram_collection_params_t histogram_params = piece->histogram_params;
dt_histogram_roi_t histogram_roi;
// if the current module does did not specified its own ROI, use the full ROI
if(histogram_params.roi == NULL)
{
histogram_roi = (dt_histogram_roi_t){
.width = roi->width,
.height = roi->height,
.crop_x = 0,
.crop_y = 0,
.crop_right = 0,
.crop_bottom = 0
};
histogram_params.roi = &histogram_roi;
}
const dt_iop_colorspace_type_t cst =
piece->module->input_colorspace(piece->module, piece->pipe, piece);
dt_histogram_helper(&histogram_params, &piece->histogram_stats,
cst, piece->module->histogram_cst,
pixel, histogram, histogram_max,
piece->module->histogram_middle_grey,
dt_ioppr_get_pipe_work_profile_info(piece->pipe));
dt_free_align(tmpbuf);
}
#endif
// color picking for module
// FIXME: make called with: lib_colorpicker_sample_statistics pick
static void _pixelpipe_picker(dt_iop_module_t *module,
dt_dev_pixelpipe_iop_t *piece,
dt_iop_buffer_dsc_t *dsc,
const float *pixel,
const dt_iop_roi_t *roi,
float *picked_color,
float *picked_color_min,
float *picked_color_max,
const dt_iop_colorspace_type_t image_cst,
const dt_pixelpipe_picker_source_t picker_source)
{
int box[4] = { 0 };
lib_colorpicker_stats pick;
const gboolean nobox =
dt_color_picker_box(module, roi,
darktable.lib->proxy.colorpicker.primary_sample,
picker_source, box);
if(!nobox)
{
dt_print_pipe(DT_DEBUG_PIPE | DT_DEBUG_PICKER,
picker_source == PIXELPIPE_PICKER_INPUT
? "pixelpipe IN picker"
: "pixelpipe OUT picker",
piece->pipe, module, DT_DEVICE_CPU, roi, NULL,
"%s (%.3f %.3f %.3f) -> %s (%s), %sbox %i/%i -- %i/%i",
dt_iop_colorspace_to_name(dsc->cst),
dsc->temperature.coeffs[0], dsc->temperature.coeffs[1], dsc->temperature.coeffs[2],
dt_iop_colorspace_to_name(image_cst),
dt_iop_colorspace_to_name(dt_iop_color_picker_get_active_cst(module)),
darktable.lib->proxy.colorpicker.primary_sample->denoise
? "denoised "
: "",
box[0], box[1], box[2], box[3]);
dt_color_picker_helper(dsc, pixel, roi, box,
darktable.lib->proxy.colorpicker.primary_sample->denoise,
pick, image_cst,
dt_iop_color_picker_get_active_cst(module),
dt_ioppr_get_pipe_current_profile_info(module, piece->pipe));
}
for_four_channels(k)
{
picked_color_min[k] = nobox ? FLT_MAX : pick[DT_PICK_MIN][k];
picked_color_max[k] = nobox ? -FLT_MAX : pick[DT_PICK_MAX][k];
picked_color[k] = nobox ? 0.0f : pick[DT_PICK_MEAN][k];
}
}
#ifdef HAVE_OPENCL
// helper for OpenCL color picking for module
//
// this algorithm is inefficient as hell when it comes to larger
// images. it's only acceptable as long as we work on small image
// sizes like in image preview an OpenCL picker implementation would
// help
//
// FIXME: make called with: lib_colorpicker_sample_statistics pick
static void _pixelpipe_picker_cl(const int devid,
dt_iop_module_t *module,
dt_dev_pixelpipe_iop_t *piece,
dt_iop_buffer_dsc_t *dsc,
cl_mem img,
const dt_iop_roi_t *roi,
float *picked_color,
float *picked_color_min,
float *picked_color_max,
float *buffer,
const size_t bufsize,
const dt_iop_colorspace_type_t image_cst,
const dt_pixelpipe_picker_source_t picker_source)
{
int box[4] = { 0 };
// make sure we return safe data in case of errors
for_four_channels(k)
{
picked_color_min[k] = FLT_MAX;
picked_color_max[k] = -FLT_MAX;
picked_color[k] = 0.0f;
}
if(dt_color_picker_box(module, roi,
darktable.lib->proxy.colorpicker.primary_sample,
picker_source, box))
return;
const size_t origin[3] = { box[0], box[1], 0 };
const size_t region[3] = { box[2] - box[0], box[3] - box[1], 1 };
float *pixel = NULL;
float *tmpbuf = NULL;
const size_t size = region[0] * region[1];
const size_t bpp = dt_iop_buffer_dsc_to_bpp(dsc);
// if a buffer is supplied and if size fits let's use it
if(buffer && bufsize >= size * bpp)
pixel = buffer;
else
pixel = tmpbuf = dt_alloc_aligned(size * bpp);
if(pixel == NULL) return;
// get the required part of the image from opencl device
if(dt_opencl_read_host_from_device_raw(devid, pixel, img,
origin, region, region[0] * bpp, CL_TRUE) != CL_SUCCESS)
{
dt_print_pipe(DT_DEBUG_PIPE,
picker_source == PIXELPIPE_PICKER_INPUT
? "pixelpipe IN picker CL"
: "pixelpipe OUT picker CL",
piece->pipe, module, devid, roi, NULL, "Couldn't read picker data");
goto error;
}
dt_print_pipe(DT_DEBUG_PIPE | DT_DEBUG_PICKER,
picker_source == PIXELPIPE_PICKER_INPUT
? "pixelpipe IN picker CL"
: "pixelpipe OUT picker CL",
piece->pipe, module, devid, roi, NULL,
"%s (%.3f %.3f %.3f) -> %s (%s), %sbox %i/%i -- %i/%i",
dt_iop_colorspace_to_name(dsc->cst),
dsc->temperature.coeffs[0], dsc->temperature.coeffs[1], dsc->temperature.coeffs[2],
dt_iop_colorspace_to_name(image_cst),
dt_iop_colorspace_to_name(dt_iop_color_picker_get_active_cst(module)),
darktable.lib->proxy.colorpicker.primary_sample->denoise
? "denoised "
: "",
box[0], box[1], box[2], box[3]);