Skip to content

Commit 43ddf0c

Browse files
shimatclaude
andcommitted
Eliminate the remaining classic DllImport entries and dead DllImport comments
Six entry points still took a delegate directly as a P/Invoke parameter, which [LibraryImport]'s source generator does not support, so they stayed on classic [DllImport]. Each is routed through a static [UnmanagedCallersOnly] trampoline instead, following the pattern already established for highgui's setMouseCallback/createTrackbar: - face_FacemarkTrain_setFaceDetector / face_FacemarkKazemi_setFaceDetector and geometry_SACSegmentation_setCustomModelConstraints support multiple concurrent instances, each with its own callback, but neither native entry point threaded a userData pointer back to the raw callback - the previous per-delegate marshaled thunk was the only thing disambiguating instances. Native (face_Facemark.h/geometry.h) now carries a void* userData alongside the callback pointer through to the point of invocation, mirroring what videoio's ManagedStreamReader already did; the managed side boxes the real delegate into a GCHandle-rooted context and passes its IntPtr through as that userData. - videoio_VideoCapture_new6/open3's ManagedStreamReader::ReadCallback/ SeekCallback already carried userData through to the point of invocation, so StreamReaderBridge just needed to become a GCHandle-based context holder instead of an instance whose delegates were marshaled directly. - tracking_TrackerKCF_setFeatureExtractor's cv::TrackerKCF:: FeatureExtractorCallbackFN has no per-instance user-data slot at all (already documented as process-wide-singleton-only), so no GCHandle context is needed there - only the field the fixed trampoline reads is swapped. Also deleted the dead, commented-out //[DllImport(...)] lines left over from the original Phase 2 bulk conversion to LibraryImport, scattered across NativeMethods_highgui.cs, NativeMethods_videoio.cs, and the core/ imgproc NativeMethods files - they had no live counterpart to convert and were pure noise. Testing: - dotnet build src/OpenCvSharp/OpenCvSharp.csproj -c Release: 0 warnings, 0 errors. - cmake --build (OpenCvSharpExtern, Release) for the face_Facemark.h/geometry.h changes. - dotnet test test/OpenCvSharp.Tests -f net10.0: 1730 passed, 0 failed, 23 skipped (pre-existing, platform/model-file-gated skips) - including FacemarkKazemiTest. CustomFaceDetectorIsInvoked, SACSegmentationTest.CustomModelConstraintsRejecting AllModelsYieldsZeroModels, TrackerKCFTest.SetFeatureExtractor, and VideoCaptureTest.OpenFromCustomStreamReader, which exercise the new trampolines end-to-end. Fixes #2077 Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
1 parent 4aa709f commit 43ddf0c

19 files changed

Lines changed: 231 additions & 175 deletions

src/OpenCvSharp/Internal/PInvoke/NativeMethods/NativeMethods_highgui.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,4 @@ public static partial ExceptionStatus highgui_getTrackbarPos(
104104

105105
[LibraryImport(DllExtern), UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
106106
public static partial ExceptionStatus highgui_setTrackbarMin([MarshalAs(UnmanagedType.LPStr)] string trackbarName, [MarshalAs(UnmanagedType.LPStr)] string winName, int minVal);
107-
108-
//[DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl, BestFitMapping = false, ThrowOnUnmappableChar = true, ExactSpelling = true)]
109-
//public static extern ExceptionStatus highgui_createButton(
110-
// [MarshalAs(UnmanagedType.LPStr)] string barName, IntPtr onChange, IntPtr userData, int type, int initialButtonState, out int returnValue);
111107
}

src/OpenCvSharp/Internal/PInvoke/NativeMethods/NativeMethods_videoio.cs

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,12 @@ public static partial ExceptionStatus videoio_VideoCapture_new4(
7171
[LibraryImport(DllExtern), UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
7272
public static partial ExceptionStatus videoio_VideoCapture_new5(int device, int apiPreference, [In] int[] @params, int paramsLength, out IntPtr returnValue);
7373

74-
[DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
75-
public static extern ExceptionStatus videoio_VideoCapture_new6(
76-
[MarshalAs(UnmanagedType.FunctionPtr)] StreamReaderReadCallback readCallback,
77-
[MarshalAs(UnmanagedType.FunctionPtr)] StreamReaderSeekCallback seekCallback,
74+
// readCallback/seekCallback are function pointers to static, [UnmanagedCallersOnly] trampolines
75+
// (see StreamReaderBridge); userData is a GCHandle to the context rooting the real IStreamReader,
76+
// not a caller-supplied value.
77+
[LibraryImport(DllExtern), UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
78+
public static partial ExceptionStatus videoio_VideoCapture_new6(
79+
IntPtr readCallback, IntPtr seekCallback,
7880
IntPtr userData, int apiPreference, [In] int[] @params, int paramsLength, out IntPtr returnValue);
7981

8082
[LibraryImport(DllExtern), UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
@@ -87,11 +89,13 @@ public static partial ExceptionStatus videoio_VideoCapture_open1(
8789
[LibraryImport(DllExtern), UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
8890
public static partial ExceptionStatus videoio_VideoCapture_open2(OpenCvSafeHandle obj, int device, int apiPreference, out int returnValue);
8991

90-
[DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
91-
public static extern ExceptionStatus videoio_VideoCapture_open3(
92+
// readCallback/seekCallback are function pointers to static, [UnmanagedCallersOnly] trampolines
93+
// (see StreamReaderBridge); userData is a GCHandle to the context rooting the real IStreamReader,
94+
// not a caller-supplied value.
95+
[LibraryImport(DllExtern), UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
96+
public static partial ExceptionStatus videoio_VideoCapture_open3(
9297
OpenCvSafeHandle obj,
93-
[MarshalAs(UnmanagedType.FunctionPtr)] StreamReaderReadCallback readCallback,
94-
[MarshalAs(UnmanagedType.FunctionPtr)] StreamReaderSeekCallback seekCallback,
98+
IntPtr readCallback, IntPtr seekCallback,
9599
IntPtr userData, int apiPreference, [In] int[] @params, int paramsLength, out int returnValue);
96100

97101
[LibraryImport(DllExtern), UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
@@ -116,9 +120,6 @@ public static partial ExceptionStatus videoio_VideoCapture_open4(
116120
[LibraryImport(DllExtern), UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
117121
public static partial ExceptionStatus videoio_VideoCapture_operatorRightShift_Mat(OpenCvSafeHandle obj, IntPtr image);
118122

119-
//[DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
120-
//public static extern ExceptionStatus videoio_VideoCapture_operatorRightShift_UMat(IntPtr obj, IntPtr image);
121-
122123
[LibraryImport(DllExtern), UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
123124
internal static partial ExceptionStatus videoio_VideoCapture_read_OutputArray(OpenCvSafeHandle obj, in OutputArrayProxy image, out int returnValue);
124125
[LibraryImport(DllExtern), UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
@@ -194,9 +195,6 @@ public static partial ExceptionStatus videoio_VideoWriter_open4(
194195
[LibraryImport(DllExtern), UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
195196
public static partial ExceptionStatus videoio_VideoWriter_release(OpenCvSafeHandle obj);
196197

197-
//[DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
198-
//public static extern ExceptionStatus videoio_VideoWriter_OperatorLeftShift(IntPtr obj, IntPtr image);
199-
200198
[LibraryImport(DllExtern), UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
201199
internal static partial ExceptionStatus videoio_VideoWriter_write(OpenCvSafeHandle obj, in InputArrayProxy image, out int returnValue);
202200

src/OpenCvSharp/Internal/PInvoke/NativeMethods/core/NativeMethods_core_FileStorage.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,6 @@ public static partial ExceptionStatus core_FileStorage_open(
3131
[LibraryImport(DllExtern), UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
3232
public static partial ExceptionStatus core_FileStorage_isOpened(OpenCvSafeHandle obj, out int returnValue);
3333

34-
//[DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
35-
//public static extern ExceptionStatus core_FileStorage_release(IntPtr obj);
36-
3734
[LibraryImport(DllExtern), UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
3835
public static partial ExceptionStatus core_FileStorage_releaseAndGetString(
3936
OpenCvSafeHandle obj, IntPtr outString);

src/OpenCvSharp/Internal/PInvoke/NativeMethods/core/NativeMethods_core_InputArray.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,16 +40,12 @@ static partial class NativeMethods
4040

4141
[LibraryImport(DllExtern), UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
4242
public static partial ExceptionStatus core_InputArray_getMat(OpenCvSafeHandle ia, int idx, out IntPtr returnValue);
43-
//[DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
44-
//public static extern ExceptionStatus core_InputArray_getMat_(IntPtr ia, int idx, out IntPtr returnValue);
4543

4644
[LibraryImport(DllExtern), UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
4745
public static partial ExceptionStatus core_InputArray_getUMat(OpenCvSafeHandle ia, int idx, out IntPtr returnValue);
48-
46+
4947
[LibraryImport(DllExtern), UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
5048
public static partial ExceptionStatus core_InputArray_getMatVector(OpenCvSafeHandle ia, IntPtr mv);
51-
//[DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
52-
//public static extern void core_InputArray_getUMatVector(IntPtr ia, IntPtr umv);
5349

5450
[LibraryImport(DllExtern), UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
5551
public static partial ExceptionStatus core_InputArray_getFlags(OpenCvSafeHandle ia, out int returnValue);

src/OpenCvSharp/Internal/PInvoke/NativeMethods/core/NativeMethods_core_Mat.cs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,6 @@ public static partial ExceptionStatus core_Mat_ones_MatShape(
6464
[LibraryImport(DllExtern), UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
6565
public static partial ExceptionStatus core_Mat_new12(IntPtr mat, out IntPtr returnValue);
6666

67-
//[DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
68-
//public static extern ExceptionStatus core_Mat_release(IntPtr mat);
6967
[LibraryImport(DllExtern), UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
7068
public static partial ExceptionStatus core_Mat_delete(IntPtr mat);
7169

@@ -259,12 +257,6 @@ public static partial ExceptionStatus core_Mat_checkVector(
259257
[LibraryImport(DllExtern), UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
260258
public static partial ExceptionStatus core_Mat_stepAt(OpenCvSafeHandle self, int i, out IntPtr returnValue);
261259

262-
//[DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
263-
//public static extern ExceptionStatus core_Mat_assignment_FromMat(IntPtr self, IntPtr newMat);
264-
265-
//[DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
266-
//public static extern ExceptionStatus core_Mat_assignment_FromScalar(IntPtr self, Scalar scalar);
267-
268260
[LibraryImport(DllExtern), UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
269261
public static partial ExceptionStatus core_abs_Mat(IntPtr e, out IntPtr returnValue);
270262

src/OpenCvSharp/Internal/PInvoke/NativeMethods/core/NativeMethods_core_OutputArray.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ static partial class NativeMethods
1212
[LibraryImport(DllExtern), UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
1313
public static partial ExceptionStatus core_OutputArray_new_byMat(IntPtr mat, out IntPtr returnValue);
1414

15-
//[DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
16-
//public static extern ExceptionStatus core_OutputArray_new_byGpuMat(IntPtr mat, out IntPtr returnValue);
1715
[LibraryImport(DllExtern), UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
1816
public static partial ExceptionStatus core_OutputArray_new_byUMat(IntPtr mat, out IntPtr returnValue);
1917

src/OpenCvSharp/Internal/PInvoke/NativeMethods/core/NativeMethods_core_UMat.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,6 @@ static partial class NativeMethods
3939
[LibraryImport(DllExtern), UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
4040
public static partial ExceptionStatus core_UMat_new9(IntPtr umat, Range[] ranges, out IntPtr returnValue);
4141

42-
43-
44-
//[DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
45-
//public static extern ExceptionStatus core_UMat_release(IntPtr mat);
4642
[LibraryImport(DllExtern), UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
4743
public static partial ExceptionStatus core_UMat_delete(IntPtr umat);
4844

src/OpenCvSharp/Internal/PInvoke/NativeMethods/face/NativeMethods_face_Facemark.cs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,12 @@ internal static partial ExceptionStatus face_FacemarkTrain_addTrainingSample(
7070
internal static partial ExceptionStatus face_FacemarkTrain_getFaces(
7171
OpenCvSafeHandle obj, in InputArrayProxy image, IntPtr faces, out int returnValue);
7272

73-
[DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
74-
internal static extern ExceptionStatus face_FacemarkTrain_setFaceDetector(
75-
OpenCvSafeHandle obj, FacemarkFaceDetectorNativeCallback callback,
73+
// callback is a function pointer to a static, [UnmanagedCallersOnly] trampoline (see
74+
// FacemarkFaceDetectorBridge); userData is a GCHandle to the context rooting the real managed
75+
// detector, round-tripped opaquely through callbackData/face_Facemark_faceDetectorThunk.
76+
[LibraryImport(DllExtern), UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
77+
internal static partial ExceptionStatus face_FacemarkTrain_setFaceDetector(
78+
OpenCvSafeHandle obj, IntPtr callback, IntPtr userData,
7679
out IntPtr callbackData, out int returnValue);
7780

7881
#endregion
@@ -218,9 +221,12 @@ internal static partial ExceptionStatus face_FacemarkKazemi_training(
218221
internal static partial ExceptionStatus face_FacemarkKazemi_getFaces(
219222
OpenCvSafeHandle obj, in InputArrayProxy image, IntPtr faces, out int returnValue);
220223

221-
[DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
222-
internal static extern ExceptionStatus face_FacemarkKazemi_setFaceDetector(
223-
OpenCvSafeHandle obj, FacemarkFaceDetectorNativeCallback callback,
224+
// callback is a function pointer to a static, [UnmanagedCallersOnly] trampoline (see
225+
// FacemarkFaceDetectorBridge); userData is a GCHandle to the context rooting the real managed
226+
// detector, round-tripped opaquely through callbackData/face_Facemark_faceDetectorThunk.
227+
[LibraryImport(DllExtern), UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
228+
internal static partial ExceptionStatus face_FacemarkKazemi_setFaceDetector(
229+
OpenCvSafeHandle obj, IntPtr callback, IntPtr userData,
224230
out IntPtr callbackData, out int returnValue);
225231

226232
[LibraryImport(DllExtern), UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]

src/OpenCvSharp/Internal/PInvoke/NativeMethods/geometry/NativeMethods_geometry.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -688,10 +688,12 @@ internal static partial ExceptionStatus geometry_SACSegmentation_segment(
688688
[LibraryImport(DllExtern), UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
689689
public static partial ExceptionStatus geometry_SACSegmentation_getRandomGeneratorState(OpenCvSafeHandle obj, out ulong returnValue);
690690

691-
// LibraryImport does not support marshaling delegate parameters, so this one uses classic DllImport.
692-
[DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
693-
public static extern ExceptionStatus geometry_SACSegmentation_setCustomModelConstraints(
694-
OpenCvSafeHandle obj, SacModelConstraintNativeCallback? callback);
691+
// callback is a function pointer to a static, [UnmanagedCallersOnly] trampoline (see
692+
// SACSegmentation.SetCustomModelConstraints), or IntPtr.Zero to clear the constraint; userData
693+
// is a GCHandle to the context rooting the real managed delegate, not a caller-supplied value.
694+
[LibraryImport(DllExtern), UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
695+
public static partial ExceptionStatus geometry_SACSegmentation_setCustomModelConstraints(
696+
OpenCvSafeHandle obj, IntPtr callback, IntPtr userData);
695697

696698
// RegionGrowing3D
697699

src/OpenCvSharp/Internal/PInvoke/NativeMethods/imgproc/NativeMethods_imgproc.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -301,9 +301,6 @@ internal static partial ExceptionStatus imgproc_blendLinear(
301301
[LibraryImport(DllExtern), UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
302302
internal static partial ExceptionStatus imgproc_demosaicing(in InputArrayProxy src, in OutputArrayProxy dst, int code, int dstCn);
303303

304-
//[DllImport(DllExtern, CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
305-
//public static extern ExceptionStatus imgproc_HuMoments(ref Moments.NativeStruct moments, [MarshalAs(UnmanagedType.LPArray)] double[] hu);
306-
307304
[LibraryImport(DllExtern), UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
308305
internal static partial ExceptionStatus imgproc_matchTemplate(
309306
in InputArrayProxy image, in InputArrayProxy templ, in OutputArrayProxy result, int method, in InputArrayProxy mask);

0 commit comments

Comments
 (0)