Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

#pragma warning disable 1591
#pragma warning disable CA1401 // P/Invokes should not be visible
#pragma warning disable IDE1006 // Naming style

namespace OpenCvSharp.Internal;

static partial class NativeMethods
{
[LibraryImport(DllExtern), UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
public static partial ExceptionStatus core_ocl_haveOpenCL(out int returnValue);

[LibraryImport(DllExtern), UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
public static partial ExceptionStatus core_ocl_useOpenCL(out int returnValue);

[LibraryImport(DllExtern), UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
public static partial ExceptionStatus core_ocl_setUseOpenCL(int flag);

[LibraryImport(DllExtern), UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
public static partial ExceptionStatus core_ocl_finish();
}
55 changes: 55 additions & 0 deletions src/OpenCvSharp/Modules/core/Cv2.Ocl.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using OpenCvSharp.Internal;

namespace OpenCvSharp;

public static partial class Cv2
{
/// <summary>
/// cv::ocl functions.
/// </summary>
public static partial class Ocl
{
/// <summary>
/// Returns whether an OpenCL runtime with at least one platform is available.
/// </summary>
public static bool HaveOpenCL()
{
NativeMethods.HandleException(
NativeMethods.core_ocl_haveOpenCL(out var returnValue));
return returnValue != 0;
}

/// <summary>
/// Returns whether OpenCL is currently enabled for the calling thread.
/// </summary>
public static bool UseOpenCL()
{
NativeMethods.HandleException(
NativeMethods.core_ocl_useOpenCL(out var returnValue));
return returnValue != 0;
}

/// <summary>
/// Enables or disables OpenCL use for the calling thread.
/// </summary>
/// <param name="flag">True to enable OpenCL when a suitable runtime and device are available; otherwise, false.</param>
public static void SetUseOpenCL(bool flag)
{
NativeMethods.HandleException(
NativeMethods.core_ocl_setUseOpenCL(flag ? 1 : 0));
}

/// <summary>
/// Waits for all queued OpenCL operations in the current default queue to complete.
/// </summary>
/// <remarks>
/// This method blocks the calling thread and is primarily useful at synchronization boundaries and when measuring OpenCL execution time.
/// Calling it after every operation can reduce performance by preventing asynchronous execution.
/// </remarks>
public static void Finish()
{
NativeMethods.HandleException(
NativeMethods.core_ocl_finish());
}
}
}
3 changes: 2 additions & 1 deletion src/OpenCvSharpExtern/core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
#include "core_Mat.h"
#include "core_UMat.h"
#include "core_MatExpr.h"
#include "core_ocl.h"
#include "core_OutputArray.h"
#include "core_PCA.h"
#include "core_SparseMat.h"
#include "core_SVD.h"
#include "core_LDA.h"
#include "core_LDA.h"
35 changes: 35 additions & 0 deletions src/OpenCvSharpExtern/core_ocl.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#pragma once

#include "include_opencv.h"
#include <opencv2/core/ocl.hpp>

// ReSharper disable CppInconsistentNaming
// ReSharper disable CppNonInlineFunctionDefinitionInHeaderFile

CVAPI(ExceptionStatus) core_ocl_haveOpenCL(int* returnValue)
{
return cvTry([&] {
*returnValue = cv::ocl::haveOpenCL() ? 1 : 0;
});
}

CVAPI(ExceptionStatus) core_ocl_useOpenCL(int* returnValue)
{
return cvTry([&] {
*returnValue = cv::ocl::useOpenCL() ? 1 : 0;
});
}

CVAPI(ExceptionStatus) core_ocl_setUseOpenCL(int flag)
{
return cvTry([&] {
cv::ocl::setUseOpenCL(flag != 0);
});
}

CVAPI(ExceptionStatus) core_ocl_finish()
{
return cvTry([] {
cv::ocl::finish();
});
}
38 changes: 38 additions & 0 deletions test/OpenCvSharp.Tests/core/OclTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using Xunit;

namespace OpenCvSharp.Tests.Core;

public class OclTest : TestBase
{
[Fact]
public void RuntimeStatusCanBeQueriedAndRestored()
{
var initiallyEnabled = Cv2.Ocl.UseOpenCL();
Assert.False(initiallyEnabled && !Cv2.Ocl.HaveOpenCL());

try
{
Cv2.Ocl.SetUseOpenCL(false);
Assert.False(Cv2.Ocl.UseOpenCL());
}
finally
{
Cv2.Ocl.SetUseOpenCL(initiallyEnabled);
}

Assert.Equal(initiallyEnabled, Cv2.Ocl.UseOpenCL());
}

[Fact]
public void FinishAfterUMatOperation()
{
using var src = new UMat(32, 32, MatType.CV_8UC1, Scalar.All(1));
using var dst = new UMat();

Cv2.GaussianBlur(src, dst, new Size(5, 5), 0);
Cv2.Ocl.Finish();

using var result = dst.GetMat(AccessFlag.READ);
Assert.Equal(1, result.At<byte>(0, 0));
}
}
Loading