Merge pull request #8204 from terfendail:ovx_tlcontext
This commit is contained in:
commit
47c4dcc8a3
13
3rdparty/openvx/hal/openvx_hal.cpp
vendored
13
3rdparty/openvx/hal/openvx_hal.cpp
vendored
@ -52,8 +52,17 @@ struct Tick
|
||||
|
||||
inline ivx::Context& getOpenVXHALContext()
|
||||
{
|
||||
// not thread safe
|
||||
static ivx::Context instance = ivx::Context::create();
|
||||
#if __cplusplus >= 201103L || (defined(_MSC_VER) && _MSC_VER >= 1800)
|
||||
//CXX11
|
||||
static thread_local ivx::Context instance = ivx::Context::create();
|
||||
#else //__cplusplus >= 201103L || _MSC_VER >= 1800
|
||||
//CXX98
|
||||
#ifdef WIN32
|
||||
static __declspec(thread) ivx::Context instance = ivx::Context::create();
|
||||
#else
|
||||
static __thread ivx::Context instance = ivx::Context::create();
|
||||
#endif
|
||||
#endif
|
||||
return instance;
|
||||
}
|
||||
|
||||
|
||||
@ -20,6 +20,12 @@
|
||||
#define IVX_USE_OPENCV
|
||||
#include "ivx.hpp"
|
||||
|
||||
namespace cv{
|
||||
namespace ovx{
|
||||
// Get common thread local OpenVX context
|
||||
CV_EXPORTS_W ivx::Context& getOpenVXContext();
|
||||
}}
|
||||
|
||||
#define CV_OVX_RUN(condition, func, ...) \
|
||||
if (cv::useOpenVX() && (condition) && func) \
|
||||
{ \
|
||||
|
||||
@ -627,6 +627,9 @@ public:
|
||||
virtual void deleteDataInstance(void* pData) const = 0;
|
||||
|
||||
int key_;
|
||||
|
||||
public:
|
||||
void cleanup(); //! Release created TLS data container objects. It is similar to release() call, but it keeps TLS container valid.
|
||||
};
|
||||
|
||||
// Main TLS data class
|
||||
@ -638,13 +641,15 @@ public:
|
||||
inline ~TLSData() { release(); } // Release key and delete associated data
|
||||
inline T* get() const { return (T*)getData(); } // Get data associated with key
|
||||
|
||||
// Get data from all threads
|
||||
// Get data from all threads
|
||||
inline void gather(std::vector<T*> &data) const
|
||||
{
|
||||
std::vector<void*> &dataVoid = reinterpret_cast<std::vector<void*>&>(data);
|
||||
gatherData(dataVoid);
|
||||
}
|
||||
|
||||
inline void cleanup() { TLSDataContainer::cleanup(); }
|
||||
|
||||
private:
|
||||
virtual void* createDataInstance() const {return new T;} // Wrapper to allocate data by template
|
||||
virtual void deleteDataInstance(void* pData) const {delete (T*)pData;} // Wrapper to release data by template
|
||||
|
||||
@ -4673,7 +4673,7 @@ static bool _openvx_cvt(const T* src, size_t sstep,
|
||||
|
||||
try
|
||||
{
|
||||
Context context = Context::create();
|
||||
Context context = ovx::getOpenVXContext();
|
||||
|
||||
// Other conversions are marked as "experimental"
|
||||
if(context.vendorID() == VX_ID_KHRONOS &&
|
||||
@ -5406,7 +5406,7 @@ static bool openvx_LUT(Mat src, Mat dst, Mat _lut)
|
||||
|
||||
try
|
||||
{
|
||||
ivx::Context ctx = ivx::Context::create();
|
||||
ivx::Context ctx = ovx::getOpenVXContext();
|
||||
|
||||
ivx::Image
|
||||
ia = ivx::Image::createFromHandle(ctx, VX_DF_IMAGE_U8,
|
||||
|
||||
@ -14,6 +14,38 @@
|
||||
namespace cv
|
||||
{
|
||||
|
||||
namespace ovx
|
||||
{
|
||||
#ifdef HAVE_OPENVX
|
||||
|
||||
// Simple TLSData<ivx::Context> doesn't work, because default constructor doesn't create any OpenVX context.
|
||||
struct OpenVXTLSData
|
||||
{
|
||||
OpenVXTLSData() : ctx(ivx::Context::create()) {}
|
||||
ivx::Context ctx;
|
||||
};
|
||||
|
||||
static TLSData<OpenVXTLSData>& getOpenVXTLSData()
|
||||
{
|
||||
CV_SINGLETON_LAZY_INIT_REF(TLSData<OpenVXTLSData>, new TLSData<OpenVXTLSData>())
|
||||
}
|
||||
|
||||
struct OpenVXCleanupFunctor
|
||||
{
|
||||
~OpenVXCleanupFunctor() { getOpenVXTLSData().cleanup(); }
|
||||
};
|
||||
static OpenVXCleanupFunctor g_openvx_cleanup_functor;
|
||||
|
||||
ivx::Context& getOpenVXContext()
|
||||
{
|
||||
return getOpenVXTLSData().get()->ctx;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
} // namespace
|
||||
|
||||
|
||||
bool haveOpenVX()
|
||||
{
|
||||
#ifdef HAVE_OPENVX
|
||||
@ -22,7 +54,7 @@ bool haveOpenVX()
|
||||
{
|
||||
try
|
||||
{
|
||||
ivx::Context context = ivx::Context::create();
|
||||
ivx::Context context = ovx::getOpenVXContext();
|
||||
vx_uint16 vComp = ivx::compiledWithVersion();
|
||||
vx_uint16 vCurr = context.version();
|
||||
g_haveOpenVX =
|
||||
|
||||
@ -1665,7 +1665,7 @@ namespace cv
|
||||
|
||||
try
|
||||
{
|
||||
ivx::Context ctx = ivx::Context::create();
|
||||
ivx::Context ctx = ovx::getOpenVXContext();
|
||||
#ifndef VX_VERSION_1_1
|
||||
if (ctx.vendorID() == VX_ID_KHRONOS)
|
||||
return false; // Do not use OpenVX meanStdDev estimation for sample 1.0.1 implementation due to lack of accuracy
|
||||
@ -2312,7 +2312,7 @@ static bool openvx_minMaxIdx(Mat &src, double* minVal, double* maxVal, int* minI
|
||||
|
||||
try
|
||||
{
|
||||
ivx::Context ctx = ivx::Context::create();
|
||||
ivx::Context ctx = ovx::getOpenVXContext();
|
||||
ivx::Image
|
||||
ia = ivx::Image::createFromHandle(ctx, stype == CV_8UC1 ? VX_DF_IMAGE_U8 : VX_DF_IMAGE_S16,
|
||||
ivx::Image::createAddressing(cols, rows, stype == CV_8UC1 ? 1 : 2, (vx_int32)(src.step[0])), src.ptr());
|
||||
|
||||
@ -1086,7 +1086,7 @@ public:
|
||||
}
|
||||
|
||||
// Release TLS storage index and pass associated data to caller
|
||||
void releaseSlot(size_t slotIdx, std::vector<void*> &dataVec)
|
||||
void releaseSlot(size_t slotIdx, std::vector<void*> &dataVec, bool keepSlot = false)
|
||||
{
|
||||
AutoLock guard(mtxGlobalAccess);
|
||||
CV_Assert(tlsSlots.size() > slotIdx);
|
||||
@ -1099,12 +1099,13 @@ public:
|
||||
if (thread_slots.size() > slotIdx && thread_slots[slotIdx])
|
||||
{
|
||||
dataVec.push_back(thread_slots[slotIdx]);
|
||||
threads[i]->slots[slotIdx] = 0;
|
||||
thread_slots[slotIdx] = NULL;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
tlsSlots[slotIdx] = 0;
|
||||
if (!keepSlot)
|
||||
tlsSlots[slotIdx] = 0;
|
||||
}
|
||||
|
||||
// Get data by TLS storage index
|
||||
@ -1196,9 +1197,18 @@ void TLSDataContainer::release()
|
||||
std::vector<void*> data;
|
||||
data.reserve(32);
|
||||
getTlsStorage().releaseSlot(key_, data); // Release key and get stored data for proper destruction
|
||||
key_ = -1;
|
||||
for(size_t i = 0; i < data.size(); i++) // Delete all associated data
|
||||
deleteDataInstance(data[i]);
|
||||
}
|
||||
|
||||
void TLSDataContainer::cleanup()
|
||||
{
|
||||
std::vector<void*> data;
|
||||
data.reserve(32);
|
||||
getTlsStorage().releaseSlot(key_, data, true); // Extract stored data with removal from TLS tables
|
||||
for(size_t i = 0; i < data.size(); i++) // Delete all associated data
|
||||
deleteDataInstance(data[i]);
|
||||
key_ = -1;
|
||||
}
|
||||
|
||||
void* TLSDataContainer::getData() const
|
||||
|
||||
@ -354,7 +354,7 @@ static bool openvx_FAST(InputArray _img, std::vector<KeyPoint>& keypoints,
|
||||
|
||||
try
|
||||
{
|
||||
Context context = Context::create();
|
||||
Context context = ovx::getOpenVXContext();
|
||||
Image img = Image::createFromHandle(context, Image::matTypeToFormat(imgMat.type()),
|
||||
Image::createAddressing(imgMat), (void*)imgMat.data);
|
||||
ivx::Scalar threshold = ivx::Scalar::create<VX_TYPE_FLOAT32>(context, _threshold);
|
||||
|
||||
@ -1958,7 +1958,7 @@ static bool openvx_accumulate(InputArray _src, InputOutputArray _dst, InputArray
|
||||
|
||||
try
|
||||
{
|
||||
ivx::Context context = ivx::Context::create();
|
||||
ivx::Context context = ovx::getOpenVXContext();
|
||||
ivx::Image srcImage = ivx::Image::createFromHandle(context, ivx::Image::matTypeToFormat(srcMat.type()),
|
||||
ivx::Image::createAddressing(srcMat), srcMat.data);
|
||||
ivx::Image dstImage = ivx::Image::createFromHandle(context, ivx::Image::matTypeToFormat(dstMat.type()),
|
||||
|
||||
@ -782,7 +782,7 @@ static bool openvx_canny(const Mat& src, Mat& dst, int loVal, int hiVal, int kSi
|
||||
{
|
||||
using namespace ivx;
|
||||
|
||||
Context context = Context::create();
|
||||
Context context = ovx::getOpenVXContext();
|
||||
try
|
||||
{
|
||||
Image _src = Image::createFromHandle(
|
||||
|
||||
@ -236,7 +236,7 @@ namespace cv
|
||||
|
||||
try
|
||||
{
|
||||
ivx::Context ctx = ivx::Context::create();
|
||||
ivx::Context ctx = ovx::getOpenVXContext();
|
||||
if ((vx_size)ksize > ctx.convolutionMaxDimension())
|
||||
return false;
|
||||
|
||||
|
||||
@ -286,7 +286,7 @@ static bool openvx_harris(Mat image, OutputArray _corners,
|
||||
|
||||
try
|
||||
{
|
||||
Context context = Context::create();
|
||||
Context context = ovx::getOpenVXContext();
|
||||
|
||||
Image ovxImage = Image::createFromHandle(context, Image::matTypeToFormat(image.type()),
|
||||
Image::createAddressing(image), image.data);
|
||||
|
||||
@ -1282,7 +1282,7 @@ namespace cv
|
||||
|
||||
try
|
||||
{
|
||||
ivx::Context ctx = ivx::Context::create();
|
||||
ivx::Context ctx = ovx::getOpenVXContext();
|
||||
#if VX_VERSION <= VX_VERSION_1_0
|
||||
if (ctx.vendorID() == VX_ID_KHRONOS && (range % histSize))
|
||||
return false;
|
||||
@ -3773,7 +3773,7 @@ static bool openvx_equalize_hist(Mat srcMat, Mat dstMat)
|
||||
|
||||
try
|
||||
{
|
||||
Context context = Context::create();
|
||||
Context context = ovx::getOpenVXContext();
|
||||
Image srcImage = Image::createFromHandle(context, Image::matTypeToFormat(srcMat.type()),
|
||||
Image::createAddressing(srcMat), srcMat.data);
|
||||
Image dstImage = Image::createFromHandle(context, Image::matTypeToFormat(dstMat.type()),
|
||||
|
||||
@ -4795,7 +4795,7 @@ static bool openvx_remap(Mat src, Mat dst, Mat map1, Mat map2, int interpolation
|
||||
|
||||
try
|
||||
{
|
||||
ivx::Context ctx = ivx::Context::create();
|
||||
ivx::Context ctx = ovx::getOpenVXContext();
|
||||
|
||||
Mat a;
|
||||
if (dst.data != src.data)
|
||||
|
||||
@ -1290,7 +1290,7 @@ static bool openvx_pyrDown( InputArray _src, OutputArray _dst, const Size& _dsz,
|
||||
|
||||
try
|
||||
{
|
||||
Context context = Context::create();
|
||||
Context context = ovx::getOpenVXContext();
|
||||
if(context.vendorID() == VX_ID_KHRONOS)
|
||||
{
|
||||
// This implementation performs floor-like rounding
|
||||
|
||||
@ -1677,7 +1677,7 @@ namespace cv
|
||||
|
||||
try
|
||||
{
|
||||
ivx::Context ctx = ivx::Context::create();
|
||||
ivx::Context ctx = ovx::getOpenVXContext();
|
||||
if ((vx_size)(ksize.width) > ctx.convolutionMaxDimension() || (vx_size)(ksize.height) > ctx.convolutionMaxDimension())
|
||||
return false;
|
||||
|
||||
@ -2239,7 +2239,7 @@ static bool openvx_gaussianBlur(InputArray _src, OutputArray _dst, Size ksize,
|
||||
|
||||
try
|
||||
{
|
||||
ivx::Context ctx = ivx::Context::create();
|
||||
ivx::Context ctx = ovx::getOpenVXContext();
|
||||
if ((vx_size)(ksize.width) > ctx.convolutionMaxDimension() || (vx_size)(ksize.height) > ctx.convolutionMaxDimension())
|
||||
return false;
|
||||
|
||||
@ -3361,7 +3361,7 @@ namespace cv
|
||||
|
||||
try
|
||||
{
|
||||
ivx::Context ctx = ivx::Context::create();
|
||||
ivx::Context ctx = ovx::getOpenVXContext();
|
||||
#ifdef VX_VERSION_1_1
|
||||
if ((vx_size)ksize > ctx.nonlinearMaxDimension())
|
||||
return false;
|
||||
|
||||
@ -1301,7 +1301,7 @@ static bool openvx_threshold(Mat src, Mat dst, int thresh, int maxval, int type)
|
||||
|
||||
try
|
||||
{
|
||||
ivx::Context ctx = ivx::Context::create();
|
||||
ivx::Context ctx = ovx::getOpenVXContext();
|
||||
|
||||
ivx::Threshold thh = ivx::Threshold::createBinary(ctx, VX_TYPE_UINT8, thresh);
|
||||
thh.setValueTrue(trueVal);
|
||||
|
||||
@ -1099,7 +1099,7 @@ namespace
|
||||
|
||||
try
|
||||
{
|
||||
Context context = Context::create();
|
||||
Context context = ovx::getOpenVXContext();
|
||||
|
||||
if(context.vendorID() == VX_ID_KHRONOS)
|
||||
{
|
||||
|
||||
Loading…
Reference in New Issue
Block a user