forked from NatronGitHub/Natron
-
Notifications
You must be signed in to change notification settings - Fork 0
/
OfxEffectInstance.cpp
3233 lines (2809 loc) · 119 KB
/
OfxEffectInstance.cpp
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
/* ***** BEGIN LICENSE BLOCK *****
* This file is part of Natron <https://natrongithub.github.io/>,
* (C) 2018-2021 The Natron developers
* (C) 2013-2018 INRIA and Alexandre Gauthier-Foichat
*
* Natron 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 2 of the License, or
* (at your option) any later version.
*
* Natron 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 Natron. If not, see <http://www.gnu.org/licenses/gpl-2.0.html>
* ***** END LICENSE BLOCK ***** */
// ***** BEGIN PYTHON BLOCK *****
// from <https://docs.python.org/3/c-api/intro.html#include-files>:
// "Since Python may define some pre-processor definitions which affect the standard headers on some systems, you must include Python.h before any standard headers are included."
#include <Python.h>
// ***** END PYTHON BLOCK *****
#include "OfxEffectInstance.h"
#include <locale>
#include <limits>
#include <cassert>
#include <stdexcept>
#include <QtCore/QDebug>
#include <QtCore/QByteArray>
#include <QtCore/QReadWriteLock>
#include <QtCore/QPointF>
// ofxhPropertySuite.h:565:37: warning: 'this' pointer cannot be null in well-defined C++ code; comparison may be assumed to always evaluate to true [-Wtautological-undefined-compare]
CLANG_DIAG_OFF(unknown-pragmas)
CLANG_DIAG_OFF(tautological-undefined-compare) // appeared in clang 3.5
#include <ofxhPluginCache.h>
#include <ofxhPluginAPICache.h>
#include <ofxhImageEffect.h>
#include <ofxhImageEffectAPI.h>
#include <ofxOpenGLRender.h>
#include <ofxhHost.h>
CLANG_DIAG_ON(tautological-undefined-compare)
CLANG_DIAG_ON(unknown-pragmas)
#include <tuttle/ofxReadWrite.h>
#include <ofxNatron.h>
#include <nuke/fnOfxExtensions.h>
#include "Engine/AppInstance.h"
#include "Engine/AppManager.h"
#include "Engine/KnobFile.h"
#include "Engine/KnobTypes.h"
#include "Engine/CreateNodeArgs.h"
#include "Engine/Node.h"
#include "Engine/NodeSerialization.h"
#include "Engine/NodeMetadata.h"
#include "Engine/OfxClipInstance.h"
#include "Engine/OfxImageEffectInstance.h"
#include "Engine/OfxOverlayInteract.h"
#include "Engine/OfxParamInstance.h"
#include "Engine/Project.h"
#include "Engine/ReadNode.h"
#include "Engine/RotoLayer.h"
#include "Engine/TimeLine.h"
#include "Engine/Transform.h"
#include "Engine/UndoCommand.h"
#include "Engine/ViewIdx.h"
#include "Engine/ViewerInstance.h"
#include "Engine/WriteNode.h"
#ifdef DEBUG
#include "Engine/TLSHolder.h"
#endif
NATRON_NAMESPACE_ENTER
using std::cout; using std::endl; using std::string;
namespace {
/**
* @class This class is helpful to set thread-storage data on the clips of an effect
* When destroyed, it is removed from the clips, ensuring they are removed.
* It is to be instantiated right before calling the action that will need the per thread-storage
* This way even if exceptions are thrown, clip thread-storage will be purged.
*
* All the info set on clip thread-storage are "cached" data that might be needed by a call of the OpenFX API which would
* otherwise require a recursive action call, which is forbidden by the specification.
* The more you pass parameters, the safer you are that the plug-in will not attempt recursive action calls but the more expensive
* it is.
**/
class ClipsThreadStorageSetter
{
public:
ClipsThreadStorageSetter(OfxImageEffectInstance* effect,
ViewIdx view,
unsigned mipmapLevel)
: effect(effect)
{
const std::map<std::string, OFX::Host::ImageEffect::ClipInstance*>& clips = effect->getClips();
for (std::map<std::string, OFX::Host::ImageEffect::ClipInstance*>::const_iterator it = clips.begin(); it != clips.end(); ++it) {
OfxClipInstance* clip = dynamic_cast<OfxClipInstance*>(it->second);
assert(clip);
if (clip) {
clip->setClipTLS( view, mipmapLevel, ImagePlaneDesc::getNoneComponents() );
}
}
}
virtual ~ClipsThreadStorageSetter()
{
const std::map<std::string, OFX::Host::ImageEffect::ClipInstance*>& clips = effect->getClips();
for (std::map<std::string, OFX::Host::ImageEffect::ClipInstance*>::const_iterator it = clips.begin(); it != clips.end(); ++it) {
OfxClipInstance* clip = dynamic_cast<OfxClipInstance*>(it->second);
assert(clip);
if (clip) {
clip->invalidateClipTLS();
}
}
}
private:
OfxImageEffectInstance* effect;
};
class RenderThreadStorageSetter
{
public:
RenderThreadStorageSetter(OfxImageEffectInstance* effect,
ViewIdx view,
unsigned int mipmapLevel,
const ImagePlaneDesc& currentPlane,
const EffectInstance::InputImagesMap& inputImages)
: effect(effect)
{
const std::map<std::string, OFX::Host::ImageEffect::ClipInstance*>& clips = effect->getClips();
for (std::map<std::string, OFX::Host::ImageEffect::ClipInstance*>::const_iterator it = clips.begin(); it != clips.end(); ++it) {
OfxClipInstance* clip = dynamic_cast<OfxClipInstance*>(it->second);
assert(clip);
if (clip) {
if ( clip->isOutput() ) {
clip->setClipTLS(view, mipmapLevel, currentPlane);
} else {
int inputNb = clip->getInputNb();
EffectInstance::InputImagesMap::const_iterator foundClip = inputImages.find(inputNb);
if ( ( foundClip != inputImages.end() ) && !foundClip->second.empty() ) {
const ImagePtr& img = foundClip->second.front();
assert(img);
clip->setClipTLS( view, mipmapLevel, img->getComponents() );
} else {
clip->setClipTLS( view, mipmapLevel, ImagePlaneDesc::getNoneComponents() );
}
}
}
}
}
virtual ~RenderThreadStorageSetter()
{
const std::map<std::string, OFX::Host::ImageEffect::ClipInstance*>& clips = effect->getClips();
for (std::map<std::string, OFX::Host::ImageEffect::ClipInstance*>::const_iterator it = clips.begin(); it != clips.end(); ++it) {
OfxClipInstance* clip = dynamic_cast<OfxClipInstance*>(it->second);
assert(clip);
if (clip) {
clip->invalidateClipTLS();
}
}
}
private:
OfxImageEffectInstance* effect;
};
} // anon namespace
struct OfxEffectInstancePrivate
{
boost::scoped_ptr<OfxImageEffectInstance> effect;
std::string natronPluginID; //< small cache to avoid calls to generateImageEffectClassName
boost::scoped_ptr<OfxOverlayInteract> overlayInteract; // ptr to the overlay interact if any
KnobStringWPtr cursorKnob; // secret knob for ofx effects so they can set the cursor
KnobIntWPtr selectionRectangleStateKnob;
KnobStringWPtr undoRedoTextKnob;
KnobBoolWPtr undoRedoStateKnob;
mutable QReadWriteLock preferencesLock;
mutable QReadWriteLock renderSafetyLock;
mutable RenderSafetyEnum renderSafety;
mutable bool wasRenderSafetySet;
ContextEnum context;
struct ClipsInfo
{
ClipsInfo()
: optional(false)
, mask(false)
, rotoBrush(false)
, clip(NULL)
, label()
, hint()
, visible(true)
{
}
bool optional;
bool mask;
bool rotoBrush;
OfxClipInstance* clip;
std::string label;
std::string hint;
bool visible;
};
std::vector<ClipsInfo> clipsInfos;
OfxClipInstance* outputClip;
int nbSourceClips;
SequentialPreferenceEnum sequentialPref;
mutable QMutex supportsConcurrentGLRendersMutex;
bool supportsConcurrentGLRenders;
bool isOutput; //if the OfxNode can output a file somehow
bool penDown; // true when the overlay trapped a penDow action
bool created; // true after the call to createInstance
bool initialized; //true when the image effect instance has been created and populated
/*
Some OpenFX do not handle render scale properly when it comes to overlay interacts.
We try to keep a blacklist of these and call overlay actions with render scale = 1 in that
case
*/
bool overlaysCanHandleRenderScale;
bool supportsMultipleClipPARs;
bool supportsMultipleClipDepths;
bool doesTemporalAccess;
bool multiplanar;
OfxEffectInstancePrivate()
: effect()
, natronPluginID()
, overlayInteract()
, cursorKnob()
, selectionRectangleStateKnob()
, undoRedoTextKnob()
, undoRedoStateKnob()
, preferencesLock(QReadWriteLock::Recursive)
, renderSafetyLock()
, renderSafety(eRenderSafetyUnsafe)
, wasRenderSafetySet(false)
, context(eContextNone)
, clipsInfos()
, outputClip(0)
, nbSourceClips(0)
, sequentialPref(eSequentialPreferenceNotSequential)
, supportsConcurrentGLRendersMutex()
, supportsConcurrentGLRenders(false)
, isOutput(false)
, penDown(false)
, created(false)
, initialized(false)
, overlaysCanHandleRenderScale(true)
, supportsMultipleClipPARs(false)
, supportsMultipleClipDepths(false)
, doesTemporalAccess(false)
, multiplanar(false)
{
}
OfxEffectInstancePrivate(const OfxEffectInstancePrivate& other)
: effect()
, natronPluginID(other.natronPluginID)
, overlayInteract()
, preferencesLock(QReadWriteLock::Recursive)
, renderSafetyLock()
, renderSafety(other.renderSafety)
, wasRenderSafetySet(other.wasRenderSafetySet)
, context(other.context)
, clipsInfos(other.clipsInfos)
, outputClip(other.outputClip)
, nbSourceClips(other.nbSourceClips)
, sequentialPref(other.sequentialPref)
, supportsConcurrentGLRendersMutex()
, supportsConcurrentGLRenders(other.supportsConcurrentGLRenders)
, isOutput(other.isOutput)
, penDown(other.penDown)
, created(other.created)
, initialized(other.initialized)
, overlaysCanHandleRenderScale(other.overlaysCanHandleRenderScale)
, supportsMultipleClipPARs(other.supportsMultipleClipPARs)
, supportsMultipleClipDepths(other.supportsMultipleClipDepths)
, doesTemporalAccess(other.doesTemporalAccess)
, multiplanar(other.multiplanar)
{
}
};
OfxEffectInstance::OfxEffectInstance(NodePtr node)
: AbstractOfxEffectInstance(node)
, _imp( new OfxEffectInstancePrivate() )
{
QObject::connect( this, SIGNAL(syncPrivateDataRequested()), this, SLOT(onSyncPrivateDataRequested()) );
}
OfxEffectInstance::OfxEffectInstance(const OfxEffectInstance& other)
: AbstractOfxEffectInstance(other)
, _imp( new OfxEffectInstancePrivate(*other._imp) )
{
QObject::connect( this, SIGNAL(syncPrivateDataRequested()), this, SLOT(onSyncPrivateDataRequested()) );
}
OfxImageEffectInstance*
OfxEffectInstance::effectInstance()
{
return _imp->effect.get();
}
const OfxImageEffectInstance*
OfxEffectInstance::effectInstance() const
{
return _imp->effect.get();
}
bool
OfxEffectInstance::isCreated() const
{
return _imp->created;
}
bool
OfxEffectInstance::isInitialized() const
{
return _imp->initialized;
}
void
OfxEffectInstance::createOfxImageEffectInstance(OFX::Host::ImageEffect::ImageEffectPlugin* plugin,
OFX::Host::ImageEffect::Descriptor* desc,
ContextEnum context,
const NodeSerialization* serialization,
const CreateNodeArgs& args
#ifndef NATRON_ENABLE_IO_META_NODES
,
bool allowFileDialogs,
bool *hasUsedFileDialog
#endif
)
{
/*Replicate of the code in OFX::Host::ImageEffect::ImageEffectPlugin::createInstance.
We need to pass more parameters to the constructor . That means we cannot
create it in the virtual function newInstance. Thus we create it before
instantiating the OfxImageEffect. The problem is that calling OFX::Host::ImageEffect::ImageEffectPlugin::createInstance
creates the OfxImageEffect and calls populate(). populate() will actually create all OfxClipInstance and OfxParamInstance.
All these subclasses need a valid pointer to an this. Hence we need to set the pointer to this in
OfxImageEffect BEFORE calling populate().
*/
///Only called from the main thread.
assert( QThread::currentThread() == qApp->thread() );
assert(plugin && desc && context != eContextNone);
_imp->context = context;
if (context == eContextWriter) {
_imp->isOutput = true;
}
if (context == eContextWriter || context == eContextReader) {
// Writers don't support render scale (full-resolution images are written to disk)
// Readers don't support render scale otherwise each mipmap level would require a file decoding
setSupportsRenderScaleMaybe(eSupportsNo);
}
std::string images;
try {
_imp->effect.reset( new OfxImageEffectInstance(plugin, *desc, mapContextToString(context), false) );
assert(_imp->effect);
OfxEffectInstancePtr thisShared = boost::dynamic_pointer_cast<OfxEffectInstance>( shared_from_this() );
_imp->effect->setOfxEffectInstance(thisShared);
_imp->natronPluginID = plugin->getIdentifier();
OfxEffectInstance::MappedInputV clips = inputClipsCopyWithoutOutput();
_imp->nbSourceClips = (int)clips.size();
_imp->clipsInfos.resize( clips.size() );
for (unsigned i = 0; i < clips.size(); ++i) {
OfxEffectInstancePrivate::ClipsInfo info;
info.optional = clips[i]->isOptional() || info.rotoBrush;
info.mask = clips[i]->isMask();
info.rotoBrush = clips[i]->getName() == CLIP_OFX_ROTO && getNode()->isRotoNode();
info.clip = NULL;
// label, hint, visible are set below
_imp->clipsInfos[i] = info;
}
getNode()->refreshAcceptedBitDepths();
_imp->supportsMultipleClipPARs = _imp->effect->supportsMultipleClipPARs();
_imp->supportsMultipleClipDepths = _imp->effect->supportsMultipleClipDepths();
_imp->doesTemporalAccess = _imp->effect->temporalAccess();
_imp->multiplanar = _imp->effect->isMultiPlanar();
int sequential = _imp->effect->getPlugin()->getDescriptor().getProps().getIntProperty(kOfxImageEffectInstancePropSequentialRender);
switch (sequential) {
case 0:
_imp->sequentialPref = eSequentialPreferenceNotSequential;
break;
case 1:
_imp->sequentialPref = eSequentialPreferenceOnlySequential;
break;
case 2:
_imp->sequentialPref = eSequentialPreferencePreferSequential;
break;
default:
_imp->sequentialPref = eSequentialPreferenceNotSequential;
break;
}
beginChanges();
OfxStatus stat;
{
SET_CAN_SET_VALUE(true);
///Create clips & parameters
stat = _imp->effect->populate();
for (unsigned i = 0; i < clips.size(); ++i) {
_imp->clipsInfos[i].clip = dynamic_cast<OfxClipInstance*>( _imp->effect->getClip( clips[i]->getName() ) );
_imp->clipsInfos[i].label = _imp->clipsInfos[i].clip->getLabel();
_imp->clipsInfos[i].hint = _imp->clipsInfos[i].clip->getHint();
_imp->clipsInfos[i].visible = !_imp->clipsInfos[i].clip->isSecret();
assert(_imp->clipsInfos[i].clip);
}
_imp->outputClip = dynamic_cast<OfxClipInstance*>( _imp->effect->getClip(kOfxImageEffectOutputClipName) );
assert(_imp->outputClip);
_imp->effect->addParamsToTheirParents();
int nPages = _imp->effect->getDescriptor().getProps().getDimension(kOfxPluginPropParamPageOrder);
std::list<std::string> pagesOrder;
for (int i = 0; i < nPages; ++i) {
const std::string& pageName = _imp->effect->getDescriptor().getProps().getStringProperty(kOfxPluginPropParamPageOrder, i);
pagesOrder.push_back(pageName);
}
if ( !pagesOrder.empty() ) {
getNode()->setPagesOrder(pagesOrder);
}
if (stat != kOfxStatOK) {
throw std::runtime_error("Error while populating the Ofx image effect");
}
assert( _imp->effect->getPlugin() );
assert( _imp->effect->getPlugin()->getPluginHandle() );
assert( _imp->effect->getPlugin()->getPluginHandle()->getOfxPlugin() );
assert(_imp->effect->getPlugin()->getPluginHandle()->getOfxPlugin()->mainEntry);
getNode()->createRotoContextConditionnally();
getNode()->initializeInputs();
getNode()->initializeKnobs(serialization != 0);
{
KnobIPtr foundCursorKnob = getKnobByName(kNatronOfxParamCursorName);
if (foundCursorKnob) {
KnobStringPtr isStringKnob = boost::dynamic_pointer_cast<KnobString>(foundCursorKnob);
_imp->cursorKnob = isStringKnob;
}
}
{
KnobIPtr foundSelKnob = getKnobByName(kNatronOfxImageEffectSelectionRectangle);
if (foundSelKnob) {
KnobIntPtr isIntKnob = boost::dynamic_pointer_cast<KnobInt>(foundSelKnob);
_imp->selectionRectangleStateKnob = isIntKnob;
}
}
{
KnobIPtr foundTextKnob = getKnobByName(kNatronOfxParamUndoRedoText);
if (foundTextKnob) {
KnobStringPtr isStringKnob = boost::dynamic_pointer_cast<KnobString>(foundTextKnob);
_imp->undoRedoTextKnob = isStringKnob;
}
}
{
KnobIPtr foundUndoRedoKnob = getKnobByName(kNatronOfxParamUndoRedoState);
if (foundUndoRedoKnob) {
KnobBoolPtr isBool = boost::dynamic_pointer_cast<KnobBool>(foundUndoRedoKnob);
_imp->undoRedoStateKnob = isBool;
}
}
///before calling the createInstanceAction, load values
if ( serialization && !serialization->isNull() ) {
getNode()->loadKnobs(*serialization);
}
getNode()->setValuesFromSerialization(args);
#ifndef NATRON_ENABLE_IO_META_NODES
//////////////////////////////////////////////////////
///////For READERS & WRITERS only we open an image file dialog
if ( !getApp()->isCreatingPythonGroup() && allowFileDialogs && isReader() && !( serialization && !serialization->isNull() ) && paramValues.empty() ) {
images = getApp()->openImageFileDialog();
} else if ( !getApp()->isCreatingPythonGroup() && allowFileDialogs && isWriter() && !( serialization && !serialization->isNull() ) && paramValues.empty() ) {
images = getApp()->saveImageFileDialog();
}
if ( !images.empty() ) {
*hasUsedFileDialog = true;
KnobSerializationPtr defaultFile = createDefaultValueForParam(kOfxImageEffectFileParamName, images);
CreateNodeArgs::DefaultValuesList list;
list.push_back(defaultFile);
std::string canonicalFilename = images;
getApp()->getProject()->canonicalizePath(canonicalFilename);
int firstFrame, lastFrame;
Node::getOriginalFrameRangeForReader(getPluginID(), canonicalFilename, &firstFrame, &lastFrame);
list.push_back( createDefaultValueForParam(kReaderParamNameOriginalFrameRange, firstFrame, lastFrame) );
getNode()->setValuesFromSerialization(list);
}
//////////////////////////////////////////////////////
#endif
///Set default metadata since the OpenFX plug-in may fetch images in its constructor
setDefaultMetadata();
{
///Take the preferences lock so that it cannot be modified throughout the action.
QReadLocker preferencesLocker(&_imp->preferencesLock);
stat = _imp->effect->createInstanceAction();
}
_imp->created = true;
} // SET_CAN_SET_VALUE(true);
if ( (stat != kOfxStatOK) && (stat != kOfxStatReplyDefault) ) {
QString message;
int type;
NodePtr messageContainer = getNode();
#ifdef NATRON_ENABLE_IO_META_NODES
NodePtr ioContainer = messageContainer->getIOContainer();
if (ioContainer) {
messageContainer = ioContainer;
}
#endif
messageContainer->getPersistentMessage(&message, &type);
if (message.isEmpty()) {
throw std::runtime_error("Could not create effect instance for plugin");
} else {
throw std::runtime_error(message.toStdString());
}
}
OfxPointD scaleOne;
scaleOne.x = 1.;
scaleOne.y = 1.;
// Try to set renderscale support at plugin creation.
// This is not always possible (e.g. if a param has a wrong value).
if (supportsRenderScaleMaybe() == eSupportsMaybe) {
// does the effect support renderscale?
double first = INT_MIN, last = INT_MAX;
getFrameRange(&first, &last);
if ( (first == INT_MIN) || (last == INT_MAX) ) {
first = last = getApp()->getTimeLine()->currentFrame();
}
ClipsThreadStorageSetter clipSetter(effectInstance(),
ViewIdx(0),
0);
double time = first;
OfxRectD rod;
OfxStatus rodstat = _imp->effect->getRegionOfDefinitionAction(time, scaleOne, 0, rod);
if ( (rodstat == kOfxStatOK) || (rodstat == kOfxStatReplyDefault) ) {
OfxPointD scale;
scale.x = 0.5;
scale.y = 0.5;
rodstat = _imp->effect->getRegionOfDefinitionAction(time, scale, 0, rod);
if ( (rodstat == kOfxStatOK) || (rodstat == kOfxStatReplyDefault) ) {
setSupportsRenderScaleMaybe(eSupportsYes);
} else {
setSupportsRenderScaleMaybe(eSupportsNo);
}
}
}
if ( isReader() && serialization && !serialization->isNull() ) {
getNode()->refreshCreatedViews(true /*silent*/);
}
} catch (const std::exception & e) {
qDebug() << "Error: Caught exception while creating OfxImageEffectInstance" << ": " << e.what();
_imp->effect.reset();
throw;
} catch (...) {
qDebug() << "Error: Caught exception while creating OfxImageEffectInstance";
_imp->effect.reset();
throw;
}
_imp->initialized = true;
endChanges();
} // createOfxImageEffectInstance
OfxEffectInstance::~OfxEffectInstance()
{
_imp->overlayInteract.reset();
if (_imp->effect) {
_imp->effect->destroyInstanceAction();
}
}
EffectInstancePtr
OfxEffectInstance::createRenderClone()
{
OfxEffectInstancePtr clone( new OfxEffectInstance(*this) );
clone->_imp->effect.reset( new OfxImageEffectInstance(*_imp->effect) );
assert(clone->_imp->effect);
clone->_imp->effect->setOfxEffectInstance(clone);
OfxStatus stat;
{
///Take the preferences lock so that it cannot be modified throughout the action.
QReadLocker preferencesLocker(&clone->_imp->preferencesLock);
stat = clone->_imp->effect->createInstanceAction();
}
if ( (stat != kOfxStatOK) && (stat != kOfxStatReplyDefault) ) {
// Failed to create clone...
return EffectInstancePtr();
}
return clone;
}
bool
OfxEffectInstance::isEffectCreated() const
{
return _imp->created;
}
bool
OfxEffectInstance::isPluginDescriptionInMarkdown() const
{
assert(_imp->context != eContextNone);
if ( effectInstance() ) {
return effectInstance()->getProps().getIntProperty(kNatronOfxPropDescriptionIsMarkdown);
} else {
return false;
}
}
std::string
OfxEffectInstance::getPluginDescription() const
{
assert(_imp->context != eContextNone);
if ( effectInstance() ) {
return effectInstance()->getProps().getStringProperty(kOfxPropPluginDescription);
} else {
return "";
}
}
void
OfxEffectInstance::tryInitializeOverlayInteracts()
{
assert(_imp->context != eContextNone);
if (_imp->overlayInteract) {
// already created
return;
}
QString pluginID = QString::fromUtf8( getPluginID().c_str() );
/*
Currently genarts plug-ins do not handle render scale properly for overlays
*/
if ( pluginID.startsWith( QString::fromUtf8("com.genarts.") ) ) {
_imp->overlaysCanHandleRenderScale = false;
}
/*create overlay instance if any*/
assert(_imp->effect);
OfxPluginEntryPoint *overlayEntryPoint = _imp->effect->getOverlayInteractMainEntry();
if (overlayEntryPoint) {
_imp->overlayInteract.reset( new OfxOverlayInteract(*_imp->effect, 8, true) );
double sx, sy;
effectInstance()->getRenderScaleRecursive(sx, sy);
RenderScale s(sx, sy);
{
ClipsThreadStorageSetter clipSetter(effectInstance(),
ViewIdx(0),
0);
{
SET_CAN_SET_VALUE(true);
///Take the preferences lock so that it cannot be modified throughout the action.
QReadLocker preferencesLocker(&_imp->preferencesLock);
_imp->overlayInteract->createInstanceAction();
}
}
///Fetch all parameters that are overlay slave
std::vector<std::string> slaveParams;
_imp->overlayInteract->getSlaveToParam(slaveParams);
for (U32 i = 0; i < slaveParams.size(); ++i) {
KnobIPtr param;
const std::vector<KnobIPtr> & knobs = getKnobs();
for (std::vector<KnobIPtr>::const_iterator it = knobs.begin(); it != knobs.end(); ++it) {
if ( (*it)->getOriginalName() == slaveParams[i] ) {
param = *it;
break;
}
}
if (!param) {
qDebug() << "OfxEffectInstance::tryInitializeOverlayInteracts(): slaveToParam " << slaveParams[i].c_str() << " not available";
} else {
addOverlaySlaveParam(param);
}
}
//For multi-instances, redraw is already taken care of by the GUI
if ( !getNode()->getParentMultiInstance() ) {
getApp()->redrawAllViewers();
}
}
///for each param, if it has a valid custom interact, create it
const std::list<OFX::Host::Param::Instance*> & params = effectInstance()->getParamList();
for (std::list<OFX::Host::Param::Instance*>::const_iterator it = params.begin(); it != params.end(); ++it) {
OfxParamToKnob* paramToKnob = dynamic_cast<OfxParamToKnob*>(*it);
assert(paramToKnob);
if (!paramToKnob) {
continue;
}
OfxPluginEntryPoint* interactEntryPoint = paramToKnob->getCustomOverlayInteractEntryPoint(*it);
if (!interactEntryPoint) {
continue;
}
KnobIPtr knob = paramToKnob->getKnob();
const OFX::Host::Property::PropSpec* interactDescProps = OfxImageEffectInstance::getOfxParamOverlayInteractDescProps();
OFX::Host::Interact::Descriptor &interactDesc = paramToKnob->getInteractDesc();
interactDesc.getProperties().addProperties(interactDescProps);
interactDesc.setEntryPoint(interactEntryPoint);
#pragma message WARN("FIXME: bitdepth and hasalpha are probably wrong")
interactDesc.describe(/*bitdepthPerComponent=*/ 8, /*hasAlpha=*/ false);
OfxParamOverlayInteractPtr overlayInteract( new OfxParamOverlayInteract( knob.get(), interactDesc, effectInstance()->getHandle()) );
knob->setCustomInteract(overlayInteract);
overlayInteract->createInstanceAction();
}
} // OfxEffectInstance::tryInitializeOverlayInteracts
void
OfxEffectInstance::setInteractColourPicker(const OfxRGBAColourD& color, bool setColor, bool hasColor)
{
if (!_imp->overlayInteract) {
return;
}
if (!_imp->overlayInteract->isColorPickerRequired()) {
return;
}
if (!hasColor) {
_imp->overlayInteract->setHasColorPicker(false);
} else {
if (setColor) {
_imp->overlayInteract->setLastColorPickerColor(color);
}
_imp->overlayInteract->setHasColorPicker(true);
}
ignore_result(_imp->overlayInteract->redraw());
}
bool
OfxEffectInstance::isOutput() const
{
assert(_imp->context != eContextNone);
return _imp->isOutput;
}
bool
OfxEffectInstance::isGenerator() const
{
#if 1
/*
* This is to deal with effects that can be both filters and generators (e.g: like constant or S_Zap)
* Some plug-ins unfortunately do not behave exactly the same in these 2 contexts and we want them to behave
* as a general context. So we just look for the presence of the generator context to determine if the plug-in
* is really a generator or not.
*/
assert( effectInstance() );
const std::set<std::string> & contexts = effectInstance()->getPlugin()->getContexts();
std::set<std::string>::const_iterator foundGenerator = contexts.find(kOfxImageEffectContextGenerator);
std::set<std::string>::const_iterator foundReader = contexts.find(kOfxImageEffectContextReader);
if ( ( foundGenerator != contexts.end() ) || ( foundReader != contexts.end() ) ) {
return true;
}
return false;
#else
assert(_context != eContextNone);
return _context == eContextGenerator || _context == eContextReader;
#endif
}
bool
OfxEffectInstance::isReader() const
{
assert(_imp->context != eContextNone);
return _imp->context == eContextReader;
}
bool
OfxEffectInstance::isVideoReader() const
{
return isReader() && ReadNode::isVideoReader( getPluginID() );
}
bool
OfxEffectInstance::isVideoWriter() const
{
return isWriter() && WriteNode::isVideoWriter( getPluginID() );
}
bool
OfxEffectInstance::isWriter() const
{
assert(_imp->context != eContextNone);
return _imp->context == eContextWriter;
}
bool
OfxEffectInstance::isTrackerNodePlugin() const
{
assert(_imp->context != eContextNone);
return _imp->context == eContextTracker;
}
bool
OfxEffectInstance::isFilter() const
{
assert(_imp->context != eContextNone);
const std::set<std::string> & contexts = effectInstance()->getPlugin()->getContexts();
bool foundGeneral = contexts.find(kOfxImageEffectContextGeneral) != contexts.end();
bool foundFilter = contexts.find(kOfxImageEffectContextFilter) != contexts.end();
return foundFilter || (foundGeneral && getNInputs() > 0);
}
/*group is a string as such:
Toto/Superplugins/blabla
This functions extracts the all parts of such a grouping, e.g in this case
it would return [Toto,Superplugins,blabla].*/
static
QStringList
ofxExtractAllPartsOfGrouping(const QString & pluginIdentifier,
int /*versionMajor*/,
int /*versionMinor*/,
const QString & /*pluginLabel*/,
const QString & str)
{
QString s(str);
std::string stdIdentifier = pluginIdentifier.toStdString();
s.replace( QLatin1Char('\\'), QLatin1Char('/') );
QStringList out;
if ( ( pluginIdentifier.startsWith( QString::fromUtf8("com.genarts.sapphire.") ) || s.startsWith( QString::fromUtf8("Sapphire ") ) || s.startsWith( QString::fromUtf8(" Sapphire ") ) ) &&
!s.startsWith( QString::fromUtf8("Sapphire/") ) ) {
out.push_back( QString::fromUtf8("Sapphire") );
} else if ( ( pluginIdentifier.startsWith( QString::fromUtf8("com.genarts.monsters.") ) || s.startsWith( QString::fromUtf8("Monsters ") ) || s.startsWith( QString::fromUtf8(" Monsters ") ) ) &&
!s.startsWith( QString::fromUtf8("Monsters/") ) ) {
out.push_back( QString::fromUtf8("Monsters") );
} else if ( ( pluginIdentifier == QString::fromUtf8("uk.co.thefoundry.keylight.keylight") ) ||
( pluginIdentifier == QString::fromUtf8("jp.co.ise.imagica:PrimattePlugin") ) ) {
s = QString::fromUtf8(PLUGIN_GROUP_KEYER);
} else if ( ( pluginIdentifier == QString::fromUtf8("uk.co.thefoundry.noisetools.denoise") ) ||
pluginIdentifier.startsWith( QString::fromUtf8("com.rubbermonkey:FilmConvert") ) ) {
s = QString::fromUtf8(PLUGIN_GROUP_FILTER);
} else if ( pluginIdentifier.startsWith( QString::fromUtf8("com.NewBlue.Titler") ) ) {
s = QString::fromUtf8(PLUGIN_GROUP_PAINT);
} else if ( pluginIdentifier.startsWith( QString::fromUtf8("com.FXHOME.HitFilm") ) ) {
// HitFilm uses grouping such as "HitFilm - Keying - Matte Enhancement"
s.replace( QString::fromUtf8(" - "), QString::fromUtf8("/") );
} else if ( pluginIdentifier.startsWith( QString::fromUtf8("com.redgiantsoftware.Universe") ) && s.startsWith( QString::fromUtf8("Universe ") ) ) {
// Red Giant Universe uses grouping such as "Universe Blur"
out.push_back( QString::fromUtf8("Universe") );
} else if ( pluginIdentifier.startsWith( QString::fromUtf8("com.NewBlue.") ) && s.startsWith( QString::fromUtf8("NewBlue ") ) ) {
// NewBlueFX uses grouping such as "NewBlue Elements"
out.push_back( QString::fromUtf8("NewBlue") );
} else if ( (stdIdentifier == "tuttle.avreader") ||
(stdIdentifier == "tuttle.avwriter") ||
(stdIdentifier == "tuttle.dpxwriter") ||
(stdIdentifier == "tuttle.exrreader") ||
(stdIdentifier == "tuttle.exrwriter") ||
(stdIdentifier == "tuttle.imagemagickreader") ||
(stdIdentifier == "tuttle.jpeg2000reader") ||
(stdIdentifier == "tuttle.jpeg2000writer") ||
(stdIdentifier == "tuttle.jpegreader") ||
(stdIdentifier == "tuttle.jpegwriter") ||
(stdIdentifier == "tuttle.oiioreader") ||
(stdIdentifier == "tuttle.oiiowriter") ||
(stdIdentifier == "tuttle.pngreader") ||
(stdIdentifier == "tuttle.pngwriter") ||
(stdIdentifier == "tuttle.rawreader") ||
(stdIdentifier == "tuttle.turbojpegreader") ||
(stdIdentifier == "tuttle.turbojpegwriter") ) {
out.push_back( QString::fromUtf8(PLUGIN_GROUP_IMAGE) );
if ( pluginIdentifier.endsWith( QString::fromUtf8("reader") ) ) {
s = QString::fromUtf8(PLUGIN_GROUP_IMAGE_READERS);
} else {
s = QString::fromUtf8(PLUGIN_GROUP_IMAGE_WRITERS);
}
} else if ( (stdIdentifier == "tuttle.checkerboard") ||
(stdIdentifier == "tuttle.colorbars") ||
(stdIdentifier == "tuttle.colorcube") || // TuttleColorCube
(stdIdentifier == "tuttle.colorgradient") ||
(stdIdentifier == "tuttle.colorwheel") ||
(stdIdentifier == "tuttle.constant") ||
(stdIdentifier == "tuttle.inputbuffer") ||
(stdIdentifier == "tuttle.outputbuffer") ||
(stdIdentifier == "tuttle.ramp") ||
(stdIdentifier == "tuttle.seexpr") ) {
s = QString::fromUtf8(PLUGIN_GROUP_IMAGE);
} else if ( (stdIdentifier == "tuttle.bitdepth") ||
(stdIdentifier == "tuttle.colorgradation") ||
(stdIdentifier == "tuttle.colorspace") ||
(stdIdentifier == "tuttle.colorsuppress") ||
(stdIdentifier == "tuttle.colortransfer") ||
(stdIdentifier == "tuttle.colortransform") ||
(stdIdentifier == "tuttle.ctl") ||
(stdIdentifier == "tuttle.invert") ||
(stdIdentifier == "tuttle.lut") ||
(stdIdentifier == "tuttle.normalize") ) {
s = QString::fromUtf8(PLUGIN_GROUP_COLOR);
} else if ( (stdIdentifier == "tuttle.ocio.colorspace") ||
(stdIdentifier == "tuttle.ocio.lut") ) {
out.push_back( QString::fromUtf8(PLUGIN_GROUP_COLOR) );
s = QString::fromUtf8("OCIO");
} else if ( (stdIdentifier == "tuttle.gamma") ||
(stdIdentifier == "tuttle.mathoperator") ) {
out.push_back( QString::fromUtf8(PLUGIN_GROUP_COLOR) );
s = QString::fromUtf8("Math");
} else if ( (stdIdentifier == "tuttle.channelshuffle") ) {
s = QString::fromUtf8(PLUGIN_GROUP_CHANNEL);
} else if ( (stdIdentifier == "tuttle.component") ||
(stdIdentifier == "tuttle.fade") ||
(stdIdentifier == "tuttle.merge") ) {
s = QString::fromUtf8(PLUGIN_GROUP_MERGE);
} else if ( (stdIdentifier == "tuttle.anisotropicdiffusion") ||
(stdIdentifier == "tuttle.anisotropictensors") ||
(stdIdentifier == "tuttle.blur") ||
(stdIdentifier == "tuttle.convolution") ||
(stdIdentifier == "tuttle.floodfill") ||
(stdIdentifier == "tuttle.localmaxima") ||
(stdIdentifier == "tuttle.nlmdenoiser") ||
(stdIdentifier == "tuttle.sobel") ||
(stdIdentifier == "tuttle.thinning") ) {
s = QString::fromUtf8(PLUGIN_GROUP_FILTER);
} else if ( (stdIdentifier == "tuttle.crop") ||
(stdIdentifier == "tuttle.flip") ||
(stdIdentifier == "tuttle.lensdistort") ||
(stdIdentifier == "tuttle.move2d") ||
(stdIdentifier == "tuttle.pinning") ||
(stdIdentifier == "tuttle.pushpixel") ||
(stdIdentifier == "tuttle.resize") ||
(stdIdentifier == "tuttle.swscale") ||
(stdIdentifier == "tuttle.warp") ) {
s = QString::fromUtf8(PLUGIN_GROUP_TRANSFORM);
} else if ( (stdIdentifier == "tuttle.timeshift") ) {
s = QString::fromUtf8(PLUGIN_GROUP_TIME);
} else if ( (stdIdentifier == "tuttle.text") ) {
s = QString::fromUtf8(PLUGIN_GROUP_PAINT);
} else if ( (stdIdentifier == "tuttle.basickeyer") ||
(stdIdentifier == "tuttle.colorspacekeyer") ||
(stdIdentifier == "tuttle.histogramkeyer") ||
(stdIdentifier == "tuttle.idkeyer") ) {
s = QString::fromUtf8(PLUGIN_GROUP_KEYER);
} else if ( (stdIdentifier == "tuttle.colorCube") || // TuttleColorCubeViewer
(stdIdentifier == "tuttle.colorcubeviewer") ||