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
1457 lines (1274 loc) · 52.7 KB
/
Copy pathpixelpipe_hb.c
File metadata and controls
1457 lines (1274 loc) · 52.7 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--2010 johannes hanika.
copyright (c) 2011 henrik andersson.
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 "develop/pixelpipe.h"
#include "develop/blend.h"
#include "develop/tiling.h"
#include "gui/gtk.h"
#include "control/control.h"
#include "control/signal.h"
#include "common/opencl.h"
#include "libs/lib.h"
#include "libs/colorpicker.h"
#include "iop/colorout.h"
#include <assert.h>
#include <string.h>
#include <strings.h>
#include <stdlib.h>
#include <math.h>
#include <unistd.h>
// this is to ensure compatibility with pixelpipe_gegl.c, which does not need to build the other module:
#include "develop/pixelpipe_cache.c"
#define max(a,b) ((a) > (b) ? (a) : (b))
static char *_pipe_type_to_str(int pipe_type)
{
char *r;
switch(pipe_type)
{
case DT_DEV_PIXELPIPE_PREVIEW:
r = "preview";
break;
case DT_DEV_PIXELPIPE_FULL:
r = "full";
break;
case DT_DEV_PIXELPIPE_THUMBNAIL:
r = "thumbnail";
break;
case DT_DEV_PIXELPIPE_EXPORT:
default:
r = "export";
break;
}
return r;
}
int dt_dev_pixelpipe_init_export(dt_dev_pixelpipe_t *pipe, int32_t width, int32_t height)
{
int res = dt_dev_pixelpipe_init_cached(pipe, 4*sizeof(float)*width*height, 2);
pipe->type = DT_DEV_PIXELPIPE_EXPORT;
return res;
}
int dt_dev_pixelpipe_init_thumbnail(dt_dev_pixelpipe_t *pipe, int32_t width, int32_t height)
{
int res = dt_dev_pixelpipe_init_cached(pipe, 4*sizeof(float)*width*height, 2);
pipe->type = DT_DEV_PIXELPIPE_THUMBNAIL;
return res;
}
int dt_dev_pixelpipe_init_preview(dt_dev_pixelpipe_t *pipe)
{
int res = dt_dev_pixelpipe_init_cached(pipe, 4*sizeof(float)*darktable.thumbnail_width*darktable.thumbnail_height, 5);
pipe->type = DT_DEV_PIXELPIPE_PREVIEW;
return res;
}
int dt_dev_pixelpipe_init(dt_dev_pixelpipe_t *pipe)
{
int res = dt_dev_pixelpipe_init_cached(pipe, 4*sizeof(float)*darktable.thumbnail_width*darktable.thumbnail_height, 5);
pipe->type = DT_DEV_PIXELPIPE_FULL;
return res;
}
int dt_dev_pixelpipe_init_cached(dt_dev_pixelpipe_t *pipe, int32_t size, int32_t entries)
{
pipe->devid = -1;
pipe->changed = DT_DEV_PIPE_UNCHANGED;
pipe->processed_width = pipe->backbuf_width = pipe->iwidth = 0;
pipe->processed_height = pipe->backbuf_height = pipe->iheight = 0;
pipe->nodes = NULL;
pipe->backbuf_size = size;
if(!dt_dev_pixelpipe_cache_init(&(pipe->cache), entries, pipe->backbuf_size))
return 0;
pipe->backbuf = NULL;
pipe->processing = 0;
pipe->shutdown = 0;
pipe->opencl_error = 0;
pipe->tiling = 0;
pipe->mask_display = 0;
pipe->input_timestamp = 0;
dt_pthread_mutex_init(&(pipe->backbuf_mutex), NULL);
dt_pthread_mutex_init(&(pipe->busy_mutex), NULL);
return 1;
}
void dt_dev_pixelpipe_set_input(dt_dev_pixelpipe_t *pipe, dt_develop_t *dev, float *input, int width, int height, float iscale)
{
pipe->iwidth = width;
pipe->iheight = height;
pipe->iflipped = 0;
pipe->iscale = iscale;
pipe->input = input;
pipe->image = dev->image_storage;
}
void dt_dev_pixelpipe_cleanup(dt_dev_pixelpipe_t *pipe)
{
dt_pthread_mutex_lock(&pipe->backbuf_mutex);
pipe->backbuf = NULL;
// 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->cache));
dt_pthread_mutex_unlock(&pipe->backbuf_mutex);
dt_pthread_mutex_destroy(&(pipe->backbuf_mutex));
dt_pthread_mutex_destroy(&(pipe->busy_mutex));
}
void dt_dev_pixelpipe_cleanup_nodes(dt_dev_pixelpipe_t *pipe)
{
// FIXME: either this or all process() -> gdk mutices have to be changed!
// (this is a circular dependency on busy_mutex and the gdk mutex)
dt_pthread_mutex_lock(&pipe->busy_mutex);
pipe->shutdown = 1;
// destroy all nodes
GList *nodes = pipe->nodes;
while(nodes)
{
dt_dev_pixelpipe_iop_t *piece = (dt_dev_pixelpipe_iop_t *)nodes->data;
// printf("cleanup module `%s'\n", piece->module->name());
piece->module->cleanup_pipe(piece->module, pipe, piece);
free(piece->blendop_data);
free(piece);
nodes = g_list_next(nodes);
}
g_list_free(pipe->nodes);
pipe->nodes = NULL;
dt_pthread_mutex_unlock(&pipe->busy_mutex);
}
void dt_dev_pixelpipe_create_nodes(dt_dev_pixelpipe_t *pipe, dt_develop_t *dev)
{
dt_pthread_mutex_lock(&pipe->busy_mutex);
pipe->shutdown = 0;
g_assert(pipe->nodes == NULL);
// for all modules in dev:
GList *modules = dev->iop;
while(modules)
{
dt_iop_module_t *module = (dt_iop_module_t *)modules->data;
// if(module->enabled) // no! always create nodes. just don't process.
{
dt_dev_pixelpipe_iop_t *piece = (dt_dev_pixelpipe_iop_t *)malloc(sizeof(dt_dev_pixelpipe_iop_t));
piece->enabled = module->enabled;
piece->colors = 4;
piece->iscale = pipe->iscale;
piece->iwidth = pipe->iwidth;
piece->iheight = pipe->iheight;
piece->module = module;
piece->pipe = pipe;
piece->data = NULL;
piece->hash = 0;
dt_iop_init_pipe(piece->module, pipe,piece);
pipe->nodes = g_list_append(pipe->nodes, piece);
}
modules = g_list_next(modules);
}
dt_pthread_mutex_unlock(&pipe->busy_mutex);
}
// helper
void dt_dev_pixelpipe_synch(dt_dev_pixelpipe_t *pipe, dt_develop_t *dev, GList *history)
{
dt_dev_history_item_t *hist = (dt_dev_history_item_t *)history->data;
// find piece in nodes list
GList *nodes = pipe->nodes;
dt_dev_pixelpipe_iop_t *piece = NULL;
while(nodes)
{
piece = (dt_dev_pixelpipe_iop_t *)nodes->data;
if(piece->module == hist->module)
{
piece->enabled = hist->enabled;
dt_iop_commit_params(hist->module, hist->params, hist->blend_params, pipe, piece);
}
nodes = g_list_next(nodes);
}
}
void dt_dev_pixelpipe_synch_all(dt_dev_pixelpipe_t *pipe, dt_develop_t *dev)
{
dt_pthread_mutex_lock(&pipe->busy_mutex);
// call reset_params on all pieces first.
GList *nodes = pipe->nodes;
while(nodes)
{
dt_dev_pixelpipe_iop_t *piece = (dt_dev_pixelpipe_iop_t *)nodes->data;
piece->hash = 0;
piece->enabled = piece->module->default_enabled;
dt_iop_commit_params(piece->module, piece->module->default_params, piece->module->default_blendop_params, pipe, piece);
nodes = g_list_next(nodes);
}
// go through all history items and adjust params
GList *history = dev->history;
for(int k=0; k<dev->history_end; k++)
{
dt_dev_pixelpipe_synch(pipe, dev, history);
history = g_list_next(history);
}
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_pixelpipe_synch(pipe, dev, history);
dt_pthread_mutex_unlock(&pipe->busy_mutex);
}
void dt_dev_pixelpipe_change(dt_dev_pixelpipe_t *pipe, struct dt_develop_t *dev)
{
dt_pthread_mutex_lock(&dev->history_mutex);
// case DT_DEV_PIPE_UNCHANGED: case DT_DEV_PIPE_ZOOMED:
if(pipe->changed & DT_DEV_PIPE_TOP_CHANGED)
{
// only top history item changed.
dt_dev_pixelpipe_synch_top(pipe, dev);
}
if(pipe->changed & DT_DEV_PIPE_SYNCH)
{
// pipeline topology remains intact, only change all params.
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);
}
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);
}
// TODO:
void dt_dev_pixelpipe_add_node(dt_dev_pixelpipe_t *pipe, dt_develop_t *dev, int n)
{
}
// TODO:
void dt_dev_pixelpipe_remove_node(dt_dev_pixelpipe_t *pipe, dt_develop_t *dev, int n)
{
}
static int
get_output_bpp(dt_iop_module_t *module, dt_dev_pixelpipe_t *pipe, dt_dev_pixelpipe_iop_t *piece, dt_develop_t *dev)
{
if(!module)
{
// first input.
// mipf and non-raw images have 4 floats per pixel
if(pipe->type == DT_DEV_PIXELPIPE_PREVIEW || !(pipe->image.flags & DT_IMAGE_RAW)) return 4*sizeof(float);
else return pipe->image.bpp;
}
return module->output_bpp(module, pipe, piece);
}
// helper for color picking
static void
pixelpipe_picker(dt_iop_module_t *module, const float *img, const dt_iop_roi_t *roi,
float *picked_color, float *picked_color_min, float *picked_color_max)
{
int box[4];
int point[2];
float Lab[3];
for(int k=0; k<3; k++) picked_color_min[k] = 666.0f;
for(int k=0; k<3; k++) picked_color_max[k] = -666.0f;
for(int k=0; k<3; k++) Lab[k] = 0.0f;
// Initializing bounds of colorpicker box
for(int k=0; k<4; k+=2) box[k] = MIN(roi->width -1, MAX(0, module->color_picker_box[k]*roi->width));
for(int k=1; k<4; k+=2) box[k] = MIN(roi->height-1, MAX(0, module->color_picker_box[k]*roi->height));
// Initializing bounds of colorpicker point
point[0] = MIN(roi->width - 1, MAX(0, module->color_picker_point[0] * roi->width));
point[1] = MIN(roi->height - 1, MAX(0, module->color_picker_point[1] * roi->height));
if(darktable.lib->proxy.colorpicker.size)
{
const float w = 1.0/((box[3]-box[1]+1)*(box[2]-box[0]+1));
for(int j=box[1]; j<=box[3]; j++) for(int i=box[0]; i<=box[2]; i++)
{
const float L = img[4*(roi->width*j + i) + 0];
const float a = img[4*(roi->width*j + i) + 1];
const float b = img[4*(roi->width*j + i) + 2];
Lab[0] += w*L;
Lab[1] += w*a;
Lab[2] += w*b;
picked_color_min[0] = fminf(picked_color_min[0], L);
picked_color_min[1] = fminf(picked_color_min[1], a);
picked_color_min[2] = fminf(picked_color_min[2], b);
picked_color_max[0] = fmaxf(picked_color_max[0], L);
picked_color_max[1] = fmaxf(picked_color_max[1], a);
picked_color_max[2] = fmaxf(picked_color_max[2], b);
for(int k=0; k<3; k++) picked_color[k] = Lab[k];
}
}
else
{
for(int i = 0; i < 3; i++)
picked_color[i]
= picked_color_min[i]
= picked_color_max[i]
= img[4*(roi->width*point[1] + point[0]) + i];
}
}
// recursive helper for process:
static int
dt_dev_pixelpipe_process_rec(dt_dev_pixelpipe_t *pipe, dt_develop_t *dev, void **output, void **cl_mem_output, int *out_bpp,
const dt_iop_roi_t *roi_out, GList *modules, GList *pieces, int pos)
{
dt_iop_roi_t roi_in = *roi_out;
void *input = NULL;
void *cl_mem_input = NULL;
*cl_mem_output = NULL;
dt_iop_module_t *module = NULL;
dt_dev_pixelpipe_iop_t *piece = NULL;
if(modules)
{
module = (dt_iop_module_t *)modules->data;
piece = (dt_dev_pixelpipe_iop_t *)pieces->data;
// skip this module?
if(!piece->enabled || (dev->gui_module && dev->gui_module->operation_tags_filter() & module->operation_tags()))
return dt_dev_pixelpipe_process_rec(pipe, dev, output, cl_mem_output, out_bpp, &roi_in, g_list_previous(modules), g_list_previous(pieces), pos-1);
}
const int bpp = get_output_bpp(module, pipe, piece, dev);
*out_bpp = bpp;
const size_t bufsize = bpp*roi_out->width*roi_out->height;
// 1) if cached buffer is still available, return data
dt_pthread_mutex_lock(&pipe->busy_mutex);
if(pipe->shutdown)
{
dt_pthread_mutex_unlock(&pipe->busy_mutex);
return 1;
}
uint64_t hash = dt_dev_pixelpipe_cache_hash(pipe->image.id, roi_out, pipe, pos);
if(dt_dev_pixelpipe_cache_available(&(pipe->cache), hash))
{
// if(module) printf("found valid buf pos %d in cache for module %s %s %lu\n", pos, module->op, pipe == dev->preview_pipe ? "[preview]" : "", hash);
// copy over cached processed max for clipping:
if(piece) for(int k=0; k<3; k++) pipe->processed_maximum[k] = piece->processed_maximum[k];
else for(int k=0; k<3; k++) pipe->processed_maximum[k] = 1.0f;
(void) dt_dev_pixelpipe_cache_get(&(pipe->cache), hash, bufsize, output);
dt_pthread_mutex_unlock(&pipe->busy_mutex);
if(!modules) return 0;
// go to post-collect directly:
goto post_process_collect_info;
}
else dt_pthread_mutex_unlock(&pipe->busy_mutex);
// 2) if history changed or exit event, abort processing?
// preview pipe: abort on all but zoom events (same buffer anyways)
if(dt_iop_breakpoint(dev, pipe)) return 1;
// if image has changed, stop now.
if(pipe == dev->pipe && dev->image_force_reload) return 1;
if(pipe == dev->preview_pipe && dev->preview_loading) return 1;
if(dev->gui_leaving) return 1;
// 3) input -> output
if(!modules)
{
// 3a) import input array with given scale and roi
dt_pthread_mutex_lock(&pipe->busy_mutex);
if(pipe->shutdown)
{
dt_pthread_mutex_unlock(&pipe->busy_mutex);
return 1;
}
dt_times_t start;
dt_get_times(&start);
if(pipe->type != DT_DEV_PIXELPIPE_PREVIEW)
{
if(roi_out->scale == 1.0 && roi_out->x == 0 && roi_out->y == 0 && pipe->iwidth == roi_out->width && pipe->iheight == roi_out->height)
{
*output = pipe->input;
}
else if(dt_dev_pixelpipe_cache_get(&(pipe->cache), hash, bufsize, output))
{
memset(*output, 0, pipe->backbuf_size);
if(roi_in.scale == 1.0f)
{
// fast branch for 1:1 pixel copies.
// last minute clamping to catch potential out-of-bounds in roi_in and roi_out
const int in_x = MAX(roi_in.x, 0);
const int in_y = MAX(roi_in.y, 0);
const int cp_width = MIN(roi_out->width, pipe->iwidth - in_x);
const int cp_height = MIN(roi_out->height, pipe->iheight - in_y);
#ifdef _OPENMP
#pragma omp parallel for schedule(static) default(none) shared(pipe,roi_out,roi_in,output)
#endif
for(int j=0; j<cp_height; j++)
memcpy(((char *)*output) + bpp*j*roi_out->width, ((char *)pipe->input) + bpp*(in_x + (in_y + j)*pipe->iwidth), bpp*cp_width);
}
else
{
roi_in.x /= roi_out->scale;
roi_in.y /= roi_out->scale;
roi_in.width = pipe->iwidth;
roi_in.height = pipe->iheight;
roi_in.scale = 1.0f;
dt_iop_clip_and_zoom(*output, pipe->input, roi_out, &roi_in, roi_out->width, pipe->iwidth);
}
}
// else found in cache.
}
// optimized branch (for mipf-preview):
else if(pipe->type == DT_DEV_PIXELPIPE_PREVIEW && roi_out->scale == 1.0 && roi_out->x == 0 && roi_out->y == 0 && pipe->iwidth == roi_out->width && pipe->iheight == roi_out->height) *output = pipe->input;
else
{
// reserve new cache line: output
if(dt_dev_pixelpipe_cache_get(&(pipe->cache), hash, bufsize, output))
{
roi_in.x /= roi_out->scale;
roi_in.y /= roi_out->scale;
roi_in.width = pipe->iwidth;
roi_in.height = pipe->iheight;
roi_in.scale = 1.0f;
dt_iop_clip_and_zoom(*output, pipe->input, roi_out, &roi_in, roi_out->width, pipe->iwidth);
}
}
dt_show_times(&start, "[dev_pixelpipe]", "initing base buffer [%s]", _pipe_type_to_str(pipe->type));
dt_pthread_mutex_unlock(&pipe->busy_mutex);
}
else
{
// 3b) recurse and obtain output array in &input
// get region of interest which is needed in input
dt_pthread_mutex_lock(&pipe->busy_mutex);
if(pipe->shutdown)
{
dt_pthread_mutex_unlock(&pipe->busy_mutex);
return 1;
}
module->modify_roi_in(module, piece, roi_out, &roi_in);
dt_pthread_mutex_unlock(&pipe->busy_mutex);
// recurse to get actual data of input buffer
int in_bpp;
if(dt_dev_pixelpipe_process_rec(pipe, dev, &input, &cl_mem_input, &in_bpp, &roi_in, g_list_previous(modules), g_list_previous(pieces), pos-1)) return 1;
piece = (dt_dev_pixelpipe_iop_t *)pieces->data;
// reserve new cache line: output
dt_pthread_mutex_lock(&pipe->busy_mutex);
if(pipe->shutdown)
{
dt_pthread_mutex_unlock(&pipe->busy_mutex);
return 1;
}
if(!strcmp(module->op, "gamma"))
(void) dt_dev_pixelpipe_cache_get_important(&(pipe->cache), hash, bufsize, output);
else
(void) dt_dev_pixelpipe_cache_get(&(pipe->cache), hash, bufsize, output);
dt_pthread_mutex_unlock(&pipe->busy_mutex);
// if(module) printf("reserving new buf in cache for module %s %s: %ld buf %lX\n", module->op, pipe == dev->preview_pipe ? "[preview]" : "", hash, (long int)*output);
// tonecurve/levels histogram (collect luminance only):
dt_pthread_mutex_lock(&pipe->busy_mutex);
if(pipe->shutdown)
{
dt_pthread_mutex_unlock(&pipe->busy_mutex);
return 1;
}
if(dev->gui_attached && pipe == dev->preview_pipe && (strcmp(module->op, "tonecurve") == 0 || strcmp(module->op, "levels") == 0))
{
float *pixel = (float *)input;
float *histogram_pre = NULL;
float *histogram_pre_max = NULL;
if(!strcmp(module->op, "tonecurve"))
{
histogram_pre = dev->histogram_pre_tonecurve;
histogram_pre_max = &(dev->histogram_pre_tonecurve_max);
}
else
{
histogram_pre = dev->histogram_pre_levels;
histogram_pre_max = &(dev->histogram_pre_levels_max);
}
*histogram_pre_max = 0;
memset (histogram_pre, 0, sizeof(float)*4*64);
for(int j=0; j<roi_in.height; j+=4) for(int i=0; i<roi_in.width; i+=4)
{
uint8_t L = CLAMP(63/100.0*(pixel[4*j*roi_in.width+4*i]), 0, 63);
histogram_pre[4*L+3] ++;
}
// don't count <= 0 pixels
for(int k=19; k<4*64; k+=4) *histogram_pre_max = *histogram_pre_max > histogram_pre[k] ? *histogram_pre_max : histogram_pre[k];
dt_pthread_mutex_unlock(&pipe->busy_mutex);
if(module->widget) dt_control_queue_redraw_widget(module->widget);
}
else dt_pthread_mutex_unlock(&pipe->busy_mutex);
// if module requested color statistics, get mean in box from preview pipe
dt_pthread_mutex_lock(&pipe->busy_mutex);
if(pipe->shutdown)
{
dt_pthread_mutex_unlock(&pipe->busy_mutex);
return 1;
}
#if 0
// Lab color picking for module
if(dev->gui_attached && pipe == dev->preview_pipe && // pick from preview pipe to get pixels outside the viewport
(module == dev->gui_module || !strcmp(module->op, "colorout")) && // only modules with focus or colorout for bottom panel can pick
module->request_color_pick) // and they need to want to pick ;)
{
pixelpipe_picker(module, (float *)input, &roi_in, module->picked_color, module->picked_color_min, module->picked_color_max);
dt_pthread_mutex_unlock(&pipe->busy_mutex);
gboolean i_own_lock = dt_control_gdk_lock();
if(module->widget) gtk_widget_queue_draw(module->widget);
if (i_own_lock) dt_control_gdk_unlock();
}
else dt_pthread_mutex_unlock(&pipe->busy_mutex);
#else
dt_pthread_mutex_unlock(&pipe->busy_mutex);
#endif
// actual pixel processing done by module
dt_pthread_mutex_lock(&pipe->busy_mutex);
if(pipe->shutdown)
{
dt_pthread_mutex_unlock(&pipe->busy_mutex);
return 1;
}
dt_times_t start;
dt_get_times(&start);
dt_develop_tiling_t tiling = { 0 };
/* get tiling requirement of module */
module->tiling_callback(module, piece, &roi_in, roi_out, &tiling);
assert(tiling.factor > 0.0f && tiling.factor < 100.0f);
#ifdef HAVE_OPENCL
/* do we have opencl at all? did user tell us to use it? did we get a resource? */
if (dt_opencl_is_inited() && pipe->opencl_enabled && pipe->devid >= 0)
{
int success_opencl = TRUE;
/* if input is on gpu memory only, remember this fact to later take appropriate action */
int valid_input_on_gpu_only = (cl_mem_input != NULL);
/* general remark: in case of opencl errors within modules or out-of-memory on GPU, we transparently
fall back to the respective cpu module and continue in pixelpipe. If we encounter errors we set
pipe->opencl_error=1, return this function with value 1, and leave appropriate action to the calling
function, which normally would restart pixelpipe without opencl.
Late errors are sometimes detected when trying to get back data from device into host memory and
are treated in the same manner. */
/* try to enter opencl path after checking some module specific pre-requisites */
if(module->process_cl && piece->process_cl_ready)
{
// fprintf(stderr, "[opencl_pixelpipe 0] factor %f, overhead %d, width %d, height %d, bpp %d\n", (double)tiling.factor, tiling.overhead, roi_in.width, roi_in.height, bpp);
// fprintf(stderr, "[opencl_pixelpipe 1] for module `%s', have bufs %lX and %lX \n", module->op, (long int)cl_mem_input, (long int)*cl_mem_output);
// fprintf(stderr, "[opencl_pixelpipe 1] module '%s'\n", module->op);
if(dt_opencl_image_fits_device(pipe->devid, max(roi_in.width, roi_out->width), max(roi_in.height, roi_out->height), max(in_bpp, bpp), tiling.factor, tiling.overhead))
{
/* image is small enough -> try to directly process entire image with opencl */
// fprintf(stderr, "[opencl_pixelpipe 2] module '%s' running directly with process_cl\n", module->op);
/* input is not on gpu memory -> copy it there */
if (cl_mem_input == NULL)
{
cl_mem_input = dt_opencl_copy_host_to_device(pipe->devid, input, roi_in.width, roi_in.height, in_bpp);
if (cl_mem_input == NULL)
{
dt_print(DT_DEBUG_OPENCL, "[opencl_pixelpipe] couldn't generate input buffer for module %s\n", module->op);
success_opencl = FALSE;
}
}
/* try to allocate GPU memory for output */
if (success_opencl)
{
*cl_mem_output = dt_opencl_alloc_device(pipe->devid, roi_out->width, roi_out->height, bpp);
if (*cl_mem_output == NULL)
{
dt_print(DT_DEBUG_OPENCL, "[opencl_pixelpipe] couldn't allocate output buffer for module %s\n", module->op);
success_opencl = FALSE;
}
}
// fprintf(stderr, "[opencl_pixelpipe 2] for module `%s', have bufs %lX and %lX \n", module->op, (long int)cl_mem_input, (long int)*cl_mem_output);
/* now call process_cl of module; module should emit meaningful messages in case of error */
if (success_opencl)
success_opencl = module->process_cl(module, piece, cl_mem_input, *cl_mem_output, &roi_in, roi_out);
/* process blending */
if (success_opencl)
success_opencl = dt_develop_blend_process_cl(module, piece, cl_mem_input, *cl_mem_output, &roi_in, roi_out);
/* synchronization point for opencl pipe */
if (success_opencl)
success_opencl = dt_opencl_finish(pipe->devid);
}
else if(module->flags() & IOP_FLAGS_ALLOW_TILING)
{
/* image is too big for direct opencl processing -> try to process image via tiling */
// fprintf(stderr, "[opencl_pixelpipe 3] module '%s' tiling with process_tiling_cl\n", module->op);
/* we might need to copy back valid image from device to host */
if (cl_mem_input != NULL)
{
cl_int err;
/* copy back to CPU buffer, then clean unneeded buffer */
err = dt_opencl_copy_device_to_host(pipe->devid, input, cl_mem_input, roi_in.width, roi_in.height, in_bpp);
if (err != CL_SUCCESS)
{
/* late opencl error */
dt_print(DT_DEBUG_OPENCL, "[opencl_pixelpipe (a)] late opencl error detected while copying back to cpu buffer: %d\n", err);
dt_opencl_release_mem_object(cl_mem_input);
pipe->opencl_error = 1;
dt_pthread_mutex_unlock(&pipe->busy_mutex);
return 1;
}
dt_opencl_release_mem_object(cl_mem_input);
cl_mem_input = NULL;
valid_input_on_gpu_only = FALSE;
}
/* now call process_tiling_cl of module; module should emit meaningful messages in case of error */
if (success_opencl)
success_opencl = module->process_tiling_cl(module, piece, input, *output, &roi_in, roi_out, in_bpp);
/* do process blending on cpu (this is anyhow fast enough) */
if (success_opencl)
dt_develop_blend_process(module, piece, input, *output, &roi_in, roi_out);
/* synchronization point for opencl pipe */
if (success_opencl)
success_opencl = dt_opencl_finish(pipe->devid);
}
else
{
/* image is too big for direct opencl and tiling is not allowed -> no opencl processing for this module */
success_opencl = FALSE;
}
if(pipe->shutdown)
{
if (cl_mem_input) dt_opencl_release_mem_object(cl_mem_input);
if (*cl_mem_output) dt_opencl_release_mem_object(*cl_mem_output);
cl_mem_input = *cl_mem_output = NULL;
dt_pthread_mutex_unlock(&pipe->busy_mutex);
return 1;
}
// if (rand() % 20 == 0) success_opencl = FALSE; // Test code: simulate spurious failures
/* finally check, if we were successful */
if (success_opencl)
{
/* Nice, everything went fine */
/* we can now release cl_mem_input */
if (cl_mem_input) dt_opencl_release_mem_object(cl_mem_input);
// we speculate on the next plug-in to possibly copy back cl_mem_output to output,
// so we're not just yet invalidating the (empty) output cache line.
}
else
{
/* Bad luck, opencl failed. Let's clean up and fall back to cpu module */
dt_print(DT_DEBUG_OPENCL, "[opencl_pixelpipe] failed to run module '%s'. fall back to cpu path\n", module->op);
// fprintf(stderr, "[opencl_pixelpipe 4] module '%s' running on cpu\n", module->op);
/* we might need to free unused output buffer */
if (*cl_mem_output != NULL)
{
dt_opencl_release_mem_object(*cl_mem_output);
*cl_mem_output = NULL;
}
/* check where our input buffer is located */
if (cl_mem_input != NULL)
{
cl_int err;
/* copy back to host memory, then clean no longer needed opencl buffer.
important info: in order to make this possible, opencl modules must
not spoil their input buffer, even in case of errors. */
err = dt_opencl_copy_device_to_host(pipe->devid, input, cl_mem_input, roi_in.width, roi_in.height, in_bpp);
if (err != CL_SUCCESS)
{
/* late opencl error */
dt_print(DT_DEBUG_OPENCL, "[opencl_pixelpipe (b)] late opencl error detected while copying back to cpu buffer: %d\n", err);
dt_opencl_release_mem_object(cl_mem_input);
pipe->opencl_error = 1;
dt_pthread_mutex_unlock(&pipe->busy_mutex);
return 1;
}
dt_opencl_release_mem_object(cl_mem_input);
valid_input_on_gpu_only = FALSE;
}
/* process module on cpu. use tiling if needed and possible. */
if((module->flags() & IOP_FLAGS_ALLOW_TILING) &&
!dt_tiling_piece_fits_host_memory(max(roi_in.width, roi_out->width), max(roi_in.height, roi_out->height),
max(in_bpp, bpp), tiling.factor, tiling.overhead))
module->process_tiling(module, piece, input, *output, &roi_in, roi_out, in_bpp);
else
module->process(module, piece, input, *output, &roi_in, roi_out);
/* process blending on cpu */
dt_develop_blend_process(module, piece, input, *output, &roi_in, roi_out);
}
if(pipe->shutdown)
{
dt_pthread_mutex_unlock(&pipe->busy_mutex);
return 1;
}
}
else
{
/* we are not allowed to use opencl for this module */
// fprintf(stderr, "[opencl_pixelpipe 3] for module `%s', have bufs %lX and %lX \n", module->op, (long int)cl_mem_input, (long int)*cl_mem_output);
*cl_mem_output = NULL;
/* cleanup unneeded opencl buffer, and copy back to CPU buffer */
if(cl_mem_input != NULL)
{
cl_int err;
err = dt_opencl_copy_device_to_host(pipe->devid, input, cl_mem_input, roi_in.width, roi_in.height, in_bpp);
// if (rand() % 5 == 0) err = !CL_SUCCESS; // Test code: simulate spurious failures
if (err != CL_SUCCESS)
{
/* late opencl error */
dt_print(DT_DEBUG_OPENCL, "[opencl_pixelpipe (c)] late opencl error detected while copying back to cpu buffer: %d\n", err);
dt_opencl_release_mem_object(cl_mem_input);
pipe->opencl_error = 1;
dt_pthread_mutex_unlock(&pipe->busy_mutex);
return 1;
}
dt_opencl_release_mem_object(cl_mem_input);
valid_input_on_gpu_only = FALSE;
}
/* process module on cpu. use tiling if needed and possible. */
if((module->flags() & IOP_FLAGS_ALLOW_TILING) &&
!dt_tiling_piece_fits_host_memory(max(roi_in.width, roi_out->width), max(roi_in.height, roi_out->height),
max(in_bpp, bpp), tiling.factor, tiling.overhead))
module->process_tiling(module, piece, input, *output, &roi_in, roi_out, in_bpp);
else
module->process(module, piece, input, *output, &roi_in, roi_out);
/* process blending */
dt_develop_blend_process(module, piece, input, *output, &roi_in, roi_out);
if(pipe->shutdown)
{
dt_pthread_mutex_unlock(&pipe->busy_mutex);
return 1;
}
}
/* input is still only on GPU? Let's invalidate CPU input buffer then */
if (valid_input_on_gpu_only) dt_dev_pixelpipe_cache_invalidate(&(pipe->cache), input);
}
else
{
/* opencl is not inited or not enabled or we got no resource/device -> everything runs on cpu */
/* process module on cpu. use tiling if needed and possible. */
if((module->flags() & IOP_FLAGS_ALLOW_TILING) &&
!dt_tiling_piece_fits_host_memory(max(roi_in.width, roi_out->width), max(roi_in.height, roi_out->height),
max(in_bpp, bpp), tiling.factor, tiling.overhead))
module->process_tiling(module, piece, input, *output, &roi_in, roi_out, in_bpp);
else
module->process(module, piece, input, *output, &roi_in, roi_out);
// Lab color picking for module
if(dev->gui_attached && pipe == dev->preview_pipe && // pick from preview pipe to get pixels outside the viewport
module == dev->gui_module && // only modules with focus can pick
module->request_color_pick) // and they need to want to pick ;)
{
pixelpipe_picker(module, (float *)input, &roi_in, module->picked_color, module->picked_color_min, module->picked_color_max);
pixelpipe_picker(module, (float *)(*output), roi_out, module->picked_output_color, module->picked_output_color_min, module->picked_output_color_max);
dt_pthread_mutex_unlock(&pipe->busy_mutex);
gboolean i_own_lock = dt_control_gdk_lock();
gtk_widget_queue_draw(module->widget);
if (i_own_lock) dt_control_gdk_unlock();
dt_pthread_mutex_lock(&pipe->busy_mutex);
}
if(pipe->shutdown)
{
dt_pthread_mutex_unlock(&pipe->busy_mutex);
return 1;
}
/* process blending */
dt_develop_blend_process(module, piece, input, *output, &roi_in, roi_out);
}
#else
/* process module on cpu. use tiling if needed and possible. */
if((module->flags() & IOP_FLAGS_ALLOW_TILING) &&
!dt_tiling_piece_fits_host_memory(max(roi_in.width, roi_out->width), max(roi_in.height, roi_out->height),
max(in_bpp, bpp), tiling.factor, tiling.overhead))
module->process_tiling(module, piece, input, *output, &roi_in, roi_out, in_bpp);
else
module->process(module, piece, input, *output, &roi_in, roi_out);
// Lab color picking for module
if(dev->gui_attached && pipe == dev->preview_pipe && // pick from preview pipe to get pixels outside the viewport
module == dev->gui_module && // only modules with focus can pick
module->request_color_pick) // and they need to want to pick ;)
{
pixelpipe_picker(module, (float *)input, &roi_in, module->picked_color, module->picked_color_min, module->picked_color_max);
pixelpipe_picker(module, (float *)(*output), roi_out, module->picked_output_color, module->picked_output_color_min, module->picked_output_color_max);
dt_pthread_mutex_unlock(&pipe->busy_mutex);
gboolean i_own_lock = dt_control_gdk_lock();
gtk_widget_queue_draw(module->widget);
if (i_own_lock) dt_control_gdk_unlock();
dt_pthread_mutex_lock(&pipe->busy_mutex);
}
if(pipe->shutdown)
{
dt_pthread_mutex_unlock(&pipe->busy_mutex);
return 1;
}
/* process blending */
dt_develop_blend_process(module, piece, input, *output, &roi_in, roi_out);
#endif
dt_show_times(&start, "[dev_pixelpipe]", "processing `%s' [%s]", module->name(),
_pipe_type_to_str(pipe->type));
// in case we get this buffer from the cache, also get the processed max:
for(int k=0; k<3; k++) piece->processed_maximum[k] = pipe->processed_maximum[k];
dt_pthread_mutex_unlock(&pipe->busy_mutex);
if(module == darktable.develop->gui_module)
{
// give the input buffer to the currently focussed plugin more weight.
// the user is likely to change that one soon, so keep it in cache.
dt_dev_pixelpipe_cache_reweight(&(pipe->cache), input);
}
#ifdef _DEBUG
dt_pthread_mutex_lock(&pipe->busy_mutex);
if(pipe->shutdown)
{
dt_pthread_mutex_unlock(&pipe->busy_mutex);
return 1;
}
if(strcmp(module->op, "gamma") && bpp == sizeof(float)*4) for(int k=0; k<4*roi_out->width*roi_out->height; k++)
{
if((k&3)<3 && !isfinite(((float*)(*output))[k]))
{
fprintf(stderr, "[dev_pixelpipe] module `%s' outputs non-finite floats!\n", module->name());
break;
}
}
dt_pthread_mutex_unlock(&pipe->busy_mutex);
#endif
post_process_collect_info:
dt_pthread_mutex_lock(&pipe->busy_mutex);
if(pipe->shutdown)
{
dt_pthread_mutex_unlock(&pipe->busy_mutex);
return 1;
}
// Picking RGB for the live samples and converting to Lab
if(dev->gui_attached
&& pipe == dev->preview_pipe
&& (strcmp(module->op, "gamma") == 0)
&& darktable.lib->proxy.colorpicker.live_samples) // samples to pick
{
dt_colorpicker_sample_t *sample = NULL;
GSList *samples = darktable.lib->proxy.colorpicker.live_samples;
while(samples)
{
sample = samples->data;
if(sample->locked)
{
samples = g_slist_next(samples);
continue;
}
uint8_t *pixel = (uint8_t*)*output;
for(int k=0; k<3; k++) sample->picked_color_rgb_min[k] = 255;
for(int k=0; k<3; k++) sample->picked_color_rgb_max[k] = 0;
int box[4];
int point[2];
float rgb[3];
for(int k=0; k<3; k++) rgb[k] = 0.0f;
for(int k=0; k<4; k+=2)
box[k] = MIN(roi_out->width -1,
MAX(0, sample->box[k]*roi_out->width));
for(int k=1; k<4; k+=2)
box[k] = MIN(roi_out->height-1,
MAX(0, sample->box[k]*roi_out->height));
point[0] = MIN(roi_out->width -1,
MAX(0, sample->point[0] * roi_out->width));
point[1] = MIN(roi_out->height -1,
MAX(0, sample->point[1] * roi_out->height));
const float w = 1.0/((box[3]-box[1]+1)*(box[2]-box[0]+1));
if(sample->size == DT_COLORPICKER_SIZE_BOX)
{
for(int j=box[1]; j<=box[3]; j++) for(int i=box[0]; i<=box[2]; i++)
{
for(int k=0; k<3; k++)
{
sample->picked_color_rgb_min[k] =
MIN(sample->picked_color_rgb_min[k],
pixel[4*(roi_out->width*j + i) + 2-k]);
sample->picked_color_rgb_max[k] =
MAX(sample->picked_color_rgb_max[k],
pixel[4*(roi_out->width*j + i) + 2-k]);
rgb[k] += w*pixel[4*(roi_out->width*j + i) + 2-k];
}
}
for(int k=0; k<3; k++) sample->picked_color_rgb_mean[k] = rgb[k];
}
else
{
for(int i = 0; i < 3; i++)
sample->picked_color_rgb_mean[i]
= sample->picked_color_rgb_min[i]
= sample->picked_color_rgb_max[i]
= pixel[4*(roi_out->width*point[1] + point[0]) + 2-i];
}
// Converting the RGB values to Lab