forked from NatronGitHub/Natron
-
Notifications
You must be signed in to change notification settings - Fork 0
/
OfxClipInstance.cpp
1714 lines (1471 loc) · 58.9 KB
/
OfxClipInstance.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 "OfxClipInstance.h"
#include <cfloat>
#include <limits>
#include <bitset>
#include <cassert>
#include <stdexcept>
#include <sstream> // stringstream
#if !defined(SBK_RUN) && !defined(Q_MOC_RUN)
GCC_DIAG_UNUSED_LOCAL_TYPEDEFS_OFF
#include <boost/math/special_functions/fpclassify.hpp>
GCC_DIAG_UNUSED_LOCAL_TYPEDEFS_ON
#endif
#include <QtCore/QTextStream>
#include <QtCore/QDebug>
#include <QtCore/QCoreApplication>
#include "Engine/CacheEntry.h"
#include "Engine/OfxEffectInstance.h"
#include "Engine/OfxImageEffectInstance.h"
#include "Engine/Settings.h"
#include "Engine/Image.h"
#include "Engine/ImageParams.h"
#include "Engine/TimeLine.h"
#include "Engine/Hash64.h"
#include "Engine/AppInstance.h"
#include "Engine/AppManager.h"
#include "Engine/Node.h"
#include "Engine/ViewerInstance.h"
#include "Engine/RotoContext.h"
#include "Engine/Transform.h"
#include "Engine/TLSHolder.h"
#include "Engine/Project.h"
#include "Engine/ViewIdx.h"
#include <nuke/fnOfxExtensions.h>
#include <ofxOpenGLRender.h>
#include <ofxNatron.h>
NATRON_NAMESPACE_ENTER
struct OfxClipInstancePrivate
{
public:
OfxClipInstance* _publicInterface;
OfxEffectInstanceWPtr nodeInstance;
OfxImageEffectInstance* const effect;
double aspectRatio;
bool optional;
bool mask;
boost::shared_ptr<TLSHolder<OfxClipInstance::ClipTLSData> > tlsData;
public:
OfxClipInstancePrivate(OfxClipInstance* publicInterface,
const OfxEffectInstancePtr& nodeInstance,
OfxImageEffectInstance* effect)
: _publicInterface(publicInterface)
, nodeInstance(nodeInstance)
, effect(effect)
, aspectRatio(1.)
, optional(false)
, mask(false)
, tlsData( new TLSHolder<OfxClipInstance::ClipTLSData>() )
{
}
const std::vector<std::string>& getComponentsPresentInternal(const OfxClipInstance::ClipDataTLSPtr& tls) const;
};
OfxClipInstance::OfxClipInstance(const OfxEffectInstancePtr& nodeInstance,
OfxImageEffectInstance* effect,
int /*index*/,
OFX::Host::ImageEffect::ClipDescriptor* desc)
: OFX::Host::ImageEffect::ClipInstance(effect, *desc)
, _imp( new OfxClipInstancePrivate(this, nodeInstance, effect) )
{
assert(nodeInstance && effect);
_imp->optional = isOptional();
_imp->mask = isMask();
}
OfxClipInstance::~OfxClipInstance()
{
}
// callback which should update label
void
OfxClipInstance::setLabel()
{
OfxEffectInstancePtr effect = _imp->nodeInstance.lock();
if (effect) {
int inputNb = getInputNb();
if (inputNb >= 0) {
effect->onClipLabelChanged(inputNb, getLabel());
}
}
}
// callback which should set secret state as appropriate
void OfxClipInstance::setSecret()
{
OfxEffectInstancePtr effect = _imp->nodeInstance.lock();
if (effect) {
int inputNb = getInputNb();
if (inputNb >= 0) {
effect->onClipSecretChanged(inputNb, isSecret());
}
}
}
// callback which should update hint
void OfxClipInstance::setHint()
{
OfxEffectInstancePtr effect = _imp->nodeInstance.lock();
if (effect) {
int inputNb = getInputNb();
if (inputNb >= 0) {
effect->onClipHintChanged(inputNb, getHint());
}
}
}
bool
OfxClipInstance::getIsOptional() const
{
return _imp->optional;
}
bool
OfxClipInstance::getIsMask() const
{
return _imp->mask;
}
const std::string &
OfxClipInstance::getUnmappedBitDepth() const
{
static const std::string byteStr(kOfxBitDepthByte);
static const std::string shortStr(kOfxBitDepthShort);
static const std::string halfStr(kOfxBitDepthHalf);
static const std::string floatStr(kOfxBitDepthFloat);
static const std::string noneStr(kOfxBitDepthNone);
EffectInstancePtr inputNode = getAssociatedNode();
if (inputNode) {
///Get the input node's output preferred bit depth
ImageBitDepthEnum depth = inputNode->getBitDepth(-1);
switch (depth) {
case eImageBitDepthByte:
return byteStr;
break;
case eImageBitDepthShort:
return shortStr;
break;
case eImageBitDepthHalf:
return halfStr;
break;
case eImageBitDepthFloat:
return floatStr;
break;
default:
break;
}
}
///Return the highest bit depth supported by the plugin
EffectInstancePtr effect = getEffectHolder();
if (effect) {
const std::string& ret = natronsDepthToOfxDepth( effect->getNode()->getClosestSupportedBitDepth(eImageBitDepthFloat) );
if (ret == floatStr) {
return floatStr;
} else if (ret == shortStr) {
return shortStr;
} else if (ret == byteStr) {
return byteStr;
}
}
return noneStr;
} // OfxClipInstance::getUnmappedBitDepth
const std::string &
OfxClipInstance::getUnmappedComponents() const
{
EffectInstancePtr effect = getAssociatedNode();
std::string ret;
if (effect) {
///Get the input node's output preferred bit depth and componentns
ClipDataTLSPtr tls = _imp->tlsData->getOrCreateTLSData();
ImagePlaneDesc metadataPlane, metadataPairedPlane;
effect->getMetadataComponents( -1, &metadataPlane, &metadataPairedPlane);
// Default to RGBA
if (metadataPlane.getNumComponents() == 0) {
metadataPlane = ImagePlaneDesc::getRGBAComponents();
}
ret = ImagePlaneDesc::mapPlaneToOFXComponentsTypeString(metadataPlane);
} else {
// The node is not connected but optional, return the closest supported components
// of the first connected non optional input.
if (_imp->optional) {
effect = getEffectHolder();
int nInputs = effect->getNInputs();
for (int i = 0; i < nInputs; ++i) {
ImagePlaneDesc metadataPlane, metadataPairedPlane;
effect->getMetadataComponents(i, &metadataPlane, &metadataPairedPlane);
if (metadataPlane.getNumComponents() > 0) {
ret = ImagePlaneDesc::mapPlaneToOFXComponentsTypeString(metadataPlane);
}
}
}
// last-resort: black and transparent image means RGBA.
if (ret.empty()) {
ret = ImagePlaneDesc::mapPlaneToOFXComponentsTypeString(ImagePlaneDesc::getRGBAComponents());
}
}
ClipDataTLSPtr tls = _imp->tlsData->getOrCreateTLSData();
tls->unmappedComponents = ret;
return tls->unmappedComponents;
}
// PreMultiplication -
//
// kOfxImageOpaque - the image is opaque and so has no premultiplication state
// kOfxImagePreMultiplied - the image is premultiplied by it's alpha
// kOfxImageUnPreMultiplied - the image is unpremultiplied
const std::string &
OfxClipInstance::getPremult() const
{
EffectInstancePtr effect = getEffectHolder();
if (!effect) {
return natronsPremultToOfxPremult(eImagePremultiplicationPremultiplied);
}
if ( isOutput() ) {
return natronsPremultToOfxPremult( effect->getPremult() );
} else {
EffectInstancePtr associatedNode = getAssociatedNode();
return associatedNode ? natronsPremultToOfxPremult( associatedNode->getPremult() ) : natronsPremultToOfxPremult(eImagePremultiplicationPremultiplied);
}
}
const std::vector<std::string>&
OfxClipInstancePrivate::getComponentsPresentInternal(const OfxClipInstance::ClipDataTLSPtr& tls) const
{
tls->componentsPresent.clear();
EffectInstancePtr effect = _publicInterface->getEffectHolder();
if (!effect) {
return tls->componentsPresent;
}
int inputNb = _publicInterface->getInputNb();
double time = effect->getCurrentTime();
ViewIdx view = effect->getCurrentView();
std::list<ImagePlaneDesc> availableLayers;
effect->getAvailableLayers(time, view, inputNb, &availableLayers);
for (std::list<ImagePlaneDesc>::iterator it = availableLayers.begin(); it != availableLayers.end(); ++it) {
std::string ofxPlane = ImagePlaneDesc::mapPlaneToOFXPlaneString(*it);
tls->componentsPresent.push_back(ofxPlane);
}
return tls->componentsPresent;
}
// overridden from OFX::Host::ImageEffect::ClipInstance
/*
* We have to use TLS here because the OpenFX API necessitate that strings
* live through the entire duration of the calling action. The is the only way
* to have it thread-safe and local to a current calling time.
*/
const std::vector<std::string>&
OfxClipInstance::getComponentsPresent() const OFX_EXCEPTION_SPEC
{
try {
//The components present have just been computed in the previous call to getDimension()
//so we are fine here
ClipDataTLSPtr tls = _imp->tlsData->getOrCreateTLSData();
return tls->componentsPresent;
} catch (...) {
throw OFX::Host::Property::Exception(kOfxStatErrUnknown);
}
}
// overridden from OFX::Host::ImageEffect::ClipInstance
int
OfxClipInstance::getDimension(const std::string &name) const OFX_EXCEPTION_SPEC
{
if (name != kFnOfxImageEffectPropComponentsPresent) {
return OFX::Host::ImageEffect::ClipInstance::getDimension(name);
}
try {
ClipDataTLSPtr tls = _imp->tlsData->getOrCreateTLSData();
const std::vector<std::string>& components = _imp->getComponentsPresentInternal(tls);
return (int)components.size();
} catch (...) {
throw OFX::Host::Property::Exception(kOfxStatErrUnknown);
}
}
const std::string &
OfxClipInstance::getComponents() const
{
/*
The property returned by the clip might differ from the one held on the image if the associated effect
is identity or if the effect is multi-planar
*/
return _components;
}
// overridden from OFX::Host::ImageEffect::ClipInstance
// Pixel Aspect Ratio -
//
// The pixel aspect ratio of a clip or image.
double
OfxClipInstance::getAspectRatio() const
{
/*
The property returned by the clip might differ from the one held on the image if the associated effect
is identity
*/
return _imp->aspectRatio;
}
void
OfxClipInstance::setAspectRatio(double par)
{
//This is protected by the clip preferences read/write lock in OfxEffectInstance
_imp->aspectRatio = par;
}
OfxRectI
OfxClipInstance::getFormat() const
{
EffectInstancePtr effect = getEffectHolder();
RectI nRect;
if ( isOutput() || (getName() == CLIP_OFX_ROTO) ) {
nRect = effect->getOutputFormat();
} else {
EffectInstancePtr inputNode = getAssociatedNode();
if (inputNode) {
inputNode = inputNode->getNearestNonIdentity( effect->getCurrentTime() );
}
if (!inputNode) {
Format f;
effect->getApp()->getProject()->getProjectDefaultFormat(&f);
nRect = f;
} else {
nRect = inputNode->getOutputFormat();
}
}
OfxRectI ret = {nRect.x1, nRect.y1, nRect.x2, nRect.y2};
return ret;
}
// Frame Rate -
double
OfxClipInstance::getFrameRate() const
{
/*
The frame rate property cannot be held onto images, hence return the "actual" frame rate,
taking into account the node from which the image came from wrt the identity state
*/
EffectInstancePtr effect = getEffectHolder();
if ( isOutput() || (getName() == CLIP_OFX_ROTO) ) {
return effect->getFrameRate();
}
EffectInstancePtr inputNode = getAssociatedNode();
if (inputNode) {
inputNode = inputNode->getNearestNonIdentity( effect->getCurrentTime() );
}
if (!inputNode) {
return effect->getApp()->getProjectFrameRate();
} else {
return inputNode->getFrameRate();
}
}
// overridden from OFX::Host::ImageEffect::ClipInstance
// Frame Range (startFrame, endFrame) -
//
// The frame range over which a clip has images.
void
OfxClipInstance::getFrameRange(double &startFrame,
double &endFrame) const
{
EffectInstancePtr n = getAssociatedNode();
if (n) {
U64 hash = n->getRenderHash();
n->getFrameRange_public(hash, &startFrame, &endFrame);
} else {
n = getEffectHolder();
double first, last;
n->getApp()->getFrameRange(&first, &last);
startFrame = first;
endFrame = last;
}
}
// overridden from OFX::Host::ImageEffect::ClipInstance
/// Field Order - Which spatial field occurs temporally first in a frame.
/// \returns
/// - kOfxImageFieldNone - the clip material is unfielded
/// - kOfxImageFieldLower - the clip material is fielded, with image rows 0,2,4.... occurring first in a frame
/// - kOfxImageFieldUpper - the clip material is fielded, with image rows line 1,3,5.... occurring first in a frame
const std::string &
OfxClipInstance::getFieldOrder() const
{
EffectInstancePtr effect = getEffectHolder();
if (!effect) {
return natronsFieldingToOfxFielding(eImageFieldingOrderNone);
}
if ( isOutput() ) {
return natronsFieldingToOfxFielding( effect->getFieldingOrder() );
} else {
EffectInstancePtr associatedNode = getAssociatedNode();
return associatedNode ? natronsFieldingToOfxFielding( associatedNode->getFieldingOrder() ) : natronsFieldingToOfxFielding(eImageFieldingOrderNone);
}
}
// overridden from OFX::Host::ImageEffect::ClipInstance
// Connected -
//
// Says whether the clip is actually connected at the moment.
bool
OfxClipInstance::getConnected() const
{
///a roto brush is always connected
EffectInstancePtr effect = getEffectHolder();
assert(effect);
if ( (getName() == CLIP_OFX_ROTO) && effect->getNode()->isRotoNode() ) {
return true;
} else {
if (_isOutput) {
return effect->hasOutputConnected();
} else {
int inputNb = getInputNb();
EffectInstancePtr input;
if ( !effect->getNode()->isMaskEnabled(inputNb) ) {
return false;
}
if (!input) {
input = effect->getInput(inputNb);
}
return input.get() != 0;
}
}
}
// overridden from OFX::Host::ImageEffect::ClipInstance
// Unmapped Frame Rate -
//
// The unmaped frame range over which an output clip has images.
double
OfxClipInstance::getUnmappedFrameRate() const
{
EffectInstancePtr inputNode = getAssociatedNode();
if (inputNode) {
///Get the input node preferred frame rate
return inputNode->getFrameRate();
} else {
///The node is not connected, return project frame rate
return getEffectHolder()->getApp()->getProjectFrameRate();
}
}
// overridden from OFX::Host::ImageEffect::ClipInstance
// Unmapped Frame Range -
//
// The unmaped frame range over which an output clip has images.
// this is applicable only to hosts and plugins that allow a plugin to change frame rates
void
OfxClipInstance::getUnmappedFrameRange(double &unmappedStartFrame,
double &unmappedEndFrame) const
{
EffectInstancePtr inputNode = getAssociatedNode();
if (inputNode) {
///Get the input node preferred frame range
return inputNode->getFrameRange_public(inputNode->getRenderHash(), &unmappedStartFrame, &unmappedEndFrame);
} else {
///The node is not connected, return project frame range
return getEffectHolder()->getApp()->getProject()->getFrameRange(&unmappedStartFrame, &unmappedEndFrame);
}
}
// Continuous Samples -
//
// 0 if the images can only be sampled at discreet times (eg: the clip is a sequence of frames),
// 1 if the images can only be sampled continuously (eg: the clip is in fact an animating roto spline and can be rendered anywhen).
bool
OfxClipInstance::getContinuousSamples() const
{
EffectInstancePtr effect = getEffectHolder();
if (!effect) {
return false;
}
if ( isOutput() ) {
return effect->canRenderContinuously();
} else {
EffectInstancePtr associatedNode = getAssociatedNode();
return associatedNode ? associatedNode->canRenderContinuously() : false;
}
}
void
OfxClipInstance::getRegionOfDefinitionInternal(OfxTime time,
ViewIdx view,
unsigned int mipmapLevel,
EffectInstance* associatedNode,
OfxRectD* ret) const
{
RotoDrawableItemPtr attachedStroke;
EffectInstancePtr effect = getEffectHolder();
if (effect) {
assert( effect->getNode() );
attachedStroke = effect->getNode()->getAttachedRotoItem();
}
bool inputIsMask = _imp->mask;
RectD rod;
if ( attachedStroke && ( inputIsMask || (getName() == CLIP_OFX_ROTO) ) ) {
effect->getNode()->getPaintStrokeRoD(time, &rod);
ret->x1 = rod.x1;
ret->x2 = rod.x2;
ret->y1 = rod.y1;
ret->y2 = rod.y2;
return;
} else if (effect) {
RotoContextPtr rotoCtx = effect->getNode()->getRotoContext();
if ( rotoCtx && (getName() == CLIP_OFX_ROTO) ) {
rotoCtx->getMaskRegionOfDefinition(time, view, &rod);
ret->x1 = rod.x1;
ret->x2 = rod.x2;
ret->y1 = rod.y1;
ret->y2 = rod.y2;
return;
}
}
if (associatedNode) {
bool isProjectFormat;
U64 nodeHash = associatedNode->getRenderHash();
RectD rod;
RenderScale scale( Image::getScaleFromMipMapLevel(mipmapLevel) );
StatusEnum st = associatedNode->getRegionOfDefinition_public(nodeHash, time, scale, view, &rod, &isProjectFormat);
if (st == eStatusFailed) {
ret->x1 = 0.;
ret->x2 = 0.;
ret->y1 = 0.;
ret->y2 = 0.;
} else {
ret->x1 = rod.left();
ret->x2 = rod.right();
ret->y1 = rod.bottom();
ret->y2 = rod.top();
}
} else {
ret->x1 = 0.;
ret->x2 = 0.;
ret->y1 = 0.;
ret->y2 = 0.;
}
} // OfxClipInstance::getRegionOfDefinitionInternal
// overridden from OFX::Host::ImageEffect::ClipInstance
OfxRectD
OfxClipInstance::getRegionOfDefinition(OfxTime time,
int view) const
{
OfxRectD rod;
unsigned int mipmapLevel;
EffectInstancePtr associatedNode = getAssociatedNode();
/// The node might be disabled, hence we navigate upstream to find the first non disabled node.
if (associatedNode) {
associatedNode = associatedNode->getNearestNonDisabled();
}
///We don't have to do the same kind of navigation if the effect is identity because the effect is supposed to have
///the same RoD as the input if it is identity.
if (!associatedNode) {
///Doesn't matter, input is not connected
mipmapLevel = 0;
} else {
ClipDataTLSPtr tls = _imp->tlsData->getOrCreateTLSData();
if ( !tls->mipMapLevel.empty() ) {
mipmapLevel = tls->mipMapLevel.back();
} else {
mipmapLevel = 0;
}
}
getRegionOfDefinitionInternal(time, ViewIdx(view), mipmapLevel, associatedNode.get(), &rod);
return rod;
}
// overridden from OFX::Host::ImageEffect::ClipInstance
/// override this to return the rod on the clip canonical coords!
OfxRectD
OfxClipInstance::getRegionOfDefinition(OfxTime time) const
{
OfxRectD ret;
unsigned int mipmapLevel;
ViewIdx view(0);
EffectInstancePtr associatedNode = getAssociatedNode();
/// The node might be disabled, hence we navigate upstream to find the first non disabled node.
if (associatedNode) {
associatedNode = associatedNode->getNearestNonDisabled();
}
///We don't have to do the same kind of navigation if the effect is identity because the effect is supposed to have
///the same RoD as the input if it is identity.
if (!associatedNode) {
///Doesn't matter, input is not connected
mipmapLevel = 0;
} else {
ClipDataTLSPtr tls = _imp->tlsData->getOrCreateTLSData();
if ( !tls->view.empty() ) {
view = tls->view.back();
}
if ( !tls->mipMapLevel.empty() ) {
mipmapLevel = tls->mipMapLevel.back();
} else {
mipmapLevel = 0;
}
}
getRegionOfDefinitionInternal(time, view, mipmapLevel, associatedNode.get(), &ret);
return ret;
} // getRegionOfDefinition
#ifdef OFX_SUPPORTS_OPENGLRENDER
// overridden from OFX::Host::ImageEffect::ClipInstance
/// override this to fill in the OpenGL texture at the given time.
/// The bounds of the image on the image plane should be
/// 'appropriate', typically the value returned in getRegionsOfInterest
/// on the effect instance. Outside a render call, the optionalBounds should
/// be 'appropriate' for the.
/// If bounds is not null, fetch the indicated section of the canonical image plane.
OFX::Host::ImageEffect::Texture*
OfxClipInstance::loadTexture(OfxTime time,
const char *format,
const OfxRectD *optionalBounds)
{
ImageBitDepthEnum depth = eImageBitDepthNone;
if (format) {
depth = ofxDepthToNatronDepth( std::string(format) );
}
OFX::Host::ImageEffect::Texture* texture = 0;
if ( !getImagePlaneInternal(time, ViewSpec::current(), optionalBounds, 0 /*plane*/, format ? &depth : 0, 0 /*image*/, &texture) ) {
return 0;
}
return texture;
}
#endif
// overridden from OFX::Host::ImageEffect::ClipInstance
/// override this to fill in the image at the given time.
/// The bounds of the image on the image plane should be
/// 'appropriate', typically the value returned in getRegionsOfInterest
/// on the effect instance. Outside a render call, the optionalBounds should
/// be 'appropriate' for the.
/// If bounds is not null, fetch the indicated section of the canonical image plane.
OFX::Host::ImageEffect::Image*
OfxClipInstance::getImage(OfxTime time,
const OfxRectD *optionalBounds)
{
OFX::Host::ImageEffect::Image* image = 0;
if ( !getImagePlaneInternal(time, ViewSpec::current(), optionalBounds, 0 /*plane*/, 0 /*texdepth*/, &image, 0 /*tex*/) ) {
return 0;
}
return image;
}
// overridden from OFX::Host::ImageEffect::ClipInstance
OFX::Host::ImageEffect::Image*
OfxClipInstance::getStereoscopicImage(OfxTime time,
int view,
const OfxRectD *optionalBounds)
{
OFX::Host::ImageEffect::Image* image = 0;
if ( !getImagePlaneInternal(time, ViewSpec(view), optionalBounds, 0 /*plane*/, 0 /*texdepth*/, &image, 0 /*tex*/) ) {
return 0;
}
return image;
}
// overridden from OFX::Host::ImageEffect::ClipInstance
OFX::Host::ImageEffect::Image*
OfxClipInstance::getImagePlane(OfxTime time,
int view,
const std::string& plane,
const OfxRectD *optionalBounds)
{
if ( (boost::math::isnan)(time) ) {
// time is NaN
return NULL;
}
ViewSpec spec;
// The Foundry Furnace plug-ins pass -1 to the view parameter, we need to deal with it.
if (view == -1) {
spec = ViewSpec::current();
} else {
spec = ViewIdx(view);
}
OFX::Host::ImageEffect::Image* image = 0;
if ( !getImagePlaneInternal(time, spec, optionalBounds, &plane, 0 /*texdepth*/, &image, 0 /*tex*/) ) {
return 0;
}
return image;
}
bool
OfxClipInstance::getImagePlaneInternal(OfxTime time,
ViewSpec view,
const OfxRectD *optionalBounds,
const std::string* ofxPlane,
const ImageBitDepthEnum* textureDepth,
OFX::Host::ImageEffect::Image** image,
OFX::Host::ImageEffect::Texture** texture)
{
if ( (boost::math::isnan)(time) ) {
// time is NaN
return false;
}
if ( isOutput() ) {
return getOutputImageInternal(ofxPlane, textureDepth, image, texture);
} else {
return getInputImageInternal(time, view, optionalBounds, ofxPlane, textureDepth, image, texture);
}
}
bool
OfxClipInstance::getInputImageInternal(const OfxTime time,
const ViewSpec viewParam,
const OfxRectD *optionalBounds,
const std::string* ofxPlane,
const ImageBitDepthEnum* textureDepth,
OFX::Host::ImageEffect::Image** retImage,
OFX::Host::ImageEffect::Texture** retTexture)
{
assert( !isOutput() );
assert( (retImage && !retTexture) || (!retImage && retTexture) );
ClipDataTLSPtr tls = _imp->tlsData->getTLSData();
RenderActionDataPtr renderData;
//If components param is not set (i.e: the plug-in uses regular clipGetImage call) then figure out the plane from the TLS set in OfxEffectInstance::render
//otherwise use the param sent by the plug-in call of clipGetImagePlane
if (tls) {
if ( !tls->renderData.empty() ) {
renderData = tls->renderData.back();
assert(renderData);
}
}
EffectInstancePtr effect = getEffectHolder();
assert(effect);
int inputnb = getInputNb();
const std::string& thisClipComponents = getComponents();
//If components param is not set (i.e: the plug-in uses regular clipGetImage call) then figure out the plane from the TLS set in OfxEffectInstance::render
//otherwise use the param sent by the plug-in call of clipGetImagePlane
//bool isMultiplanar = effect->isMultiPlanar();
ImagePlaneDesc comp;
if (!ofxPlane) {
EffectInstance::ComponentsNeededMapPtr neededComps;
effect->getThreadLocalNeededComponents(&neededComps);
bool foundCompsInTLS = false;
if (neededComps) {
EffectInstance::ComponentsNeededMap::iterator found = neededComps->find(inputnb);
if ( found != neededComps->end() ) {
if ( found->second.empty() ) {
///We are in the case of a multi-plane effect who did not specify correctly the needed components for an input
//fallback on the basic components indicated on the clip
//This could be the case for example for the Mask Input
ImagePlaneDesc pairedComp;
ImagePlaneDesc::mapOFXComponentsTypeStringToPlanes( thisClipComponents, &comp, &pairedComp );
foundCompsInTLS = true;
//qDebug() << _imp->nodeInstance->getScriptName_mt_safe().c_str() << " didn't specify any needed components via getClipComponents for clip " << getName().c_str();
} else {
comp = found->second.front();
foundCompsInTLS = true;
}
}
}
if (!foundCompsInTLS) {
///We are in analysis or the effect does not have any input
std::bitset<4> processChannels;
bool isAll;
std::list<ImagePlaneDesc> availableLayers;
effect->getAvailableLayers(time, ViewIdx(0), inputnb, &availableLayers);
if (!effect->getNode()->getSelectedLayer(inputnb, availableLayers, &processChannels, &isAll, &comp)) {
//There's no selector...fallback on the basic components indicated on the clip
ImagePlaneDesc pairedComp;
ImagePlaneDesc::mapOFXComponentsTypeStringToPlanes( thisClipComponents, &comp, &pairedComp );
}
}
} else {
if (*ofxPlane == kFnOfxImagePlaneColour) {
ImagePlaneDesc pairedComp;
ImagePlaneDesc::mapOFXComponentsTypeStringToPlanes( thisClipComponents, &comp, &pairedComp );
} else {
comp = ImagePlaneDesc::mapOFXPlaneStringToPlane(*ofxPlane);
}
}
if (comp.getNumComponents() == 0) {
return false;
}
if ( (boost::math::isnan)(time) ) {
// time is NaN
return false;
}
unsigned int mipMapLevel = 0;
// Get mipmaplevel and view from the TLS
#ifdef DEBUG
if ( !tls || tls->view.empty() ) {
if ( QThread::currentThread() != qApp->thread() ) {
qDebug() << effect->getNode()->getScriptName_mt_safe().c_str() << " is trying to call clipGetImage on a thread "
"not controlled by Natron (probably from the multi-thread suite).\n If you're a developer of that plug-in, please "
"fix it. Natron is now going to try to recover from that mistake but doing so can yield unpredictable results.";
}
}
#endif
assert( !viewParam.isAll() );
ViewIdx view;
if (tls) {
if ( viewParam.isCurrent() ) {
if ( tls->view.empty() ) {
view = ViewIdx(0);
} else {
view = tls->view.back();
}
} else {
view = ViewIdx( viewParam.value() );
}
if ( tls->mipMapLevel.empty() ) {
mipMapLevel = 0;
} else {
mipMapLevel = tls->mipMapLevel.back();
}
} else {
if ( viewParam.isCurrent() ) {
// no TLS
view = ViewIdx(0);
} else {
view = ViewIdx( viewParam.value() );
}
}
// If the plug-in is requesting the colour plane, it is expected that we return
// an image mapped to the clip components
const bool mapImageToClipPref = !ofxPlane || *ofxPlane == kFnOfxImagePlaneColour;
//Check if the plug-in already called clipGetImage on this image, in which case we may already have an OfxImage laying around
//so we try to re-use it.
if (renderData) {
for (std::list<OfxImageCommon*>::const_iterator it = renderData->imagesBeingRendered.begin(); it != renderData->imagesBeingRendered.end(); ++it) {
ImagePtr internalImage = (*it)->getInternalImage();
if (!internalImage) {
continue;
}
bool sameComponents = (mapImageToClipPref && (*it)->getComponentsString() == thisClipComponents) ||
(!mapImageToClipPref && (*it)->getComponentsString() == *ofxPlane);
if ( sameComponents && (internalImage->getMipMapLevel() == mipMapLevel) &&
( time == internalImage->getTime() ) &&
( view == internalImage->getKey().getView() ) ) {
if (retImage) {
OfxImage* isImage = dynamic_cast<OfxImage*>(*it);
if (isImage) {
*retImage = isImage;
isImage->addReference();
return true;
}
} else if (retTexture) {
OfxTexture* isTex = dynamic_cast<OfxTexture*>(*it);
if (isTex) {
*retTexture = isTex;
isTex->addReference();
return true;
}
}
}
}
}
RenderScale renderScale( Image::getScaleFromMipMapLevel(mipMapLevel) );
RectD bounds;
if (optionalBounds) {
bounds.x1 = optionalBounds->x1;
bounds.y1 = optionalBounds->y1;
bounds.x2 = optionalBounds->x2;
bounds.y2 = optionalBounds->y2;
}
bool multiPlanar = effect->isMultiPlanar();
RectI renderWindow;
Transform::Matrix3x3Ptr transform;
ImagePtr image = effect->getImage(inputnb, time, renderScale, view,
optionalBounds ? &bounds : NULL,
&comp,
mapImageToClipPref,
false /*dontUpscale*/,
retTexture != 0 ? eStorageModeGLTex : eStorageModeRAM,
textureDepth,
&renderWindow,
&transform);