mirror of
https://github.com/opencv/opencv.git
synced 2026-07-29 15:23:05 +04:00
fixed many bugs related to Mat::getUMat(), asynchronous kernel execution etc. Also, played a bit with ocl::cvtColor vs cv::cvtColor performance
This commit is contained in:
+135
-116
@@ -48,147 +48,166 @@
|
||||
|
||||
namespace cv {
|
||||
|
||||
void MatAllocator::sync(UMatData*) const
|
||||
{
|
||||
}
|
||||
|
||||
void MatAllocator::map(UMatData*, int) const
|
||||
{
|
||||
}
|
||||
|
||||
void MatAllocator::unmap(UMatData* u) const
|
||||
{
|
||||
if(u->urefcount == 0 && u->refcount == 0)
|
||||
deallocate(u);
|
||||
}
|
||||
|
||||
void MatAllocator::download(UMatData* u, void* dstptr,
|
||||
int dims, const size_t sz[],
|
||||
const size_t srcofs[], const size_t srcstep[],
|
||||
const size_t dststep[]) const
|
||||
{
|
||||
if(!u)
|
||||
return;
|
||||
int isz[CV_MAX_DIM];
|
||||
uchar* srcptr = u->data;
|
||||
for( int i = 0; i < dims; i++ )
|
||||
{
|
||||
CV_Assert( sz[i] <= (size_t)INT_MAX );
|
||||
if( sz[i] == 0 )
|
||||
return;
|
||||
if( srcofs )
|
||||
srcptr += srcofs[i]*(i <= dims-2 ? srcstep[i] : 1);
|
||||
isz[i] = (int)sz[i];
|
||||
}
|
||||
|
||||
Mat src(dims, isz, CV_8U, srcptr, srcstep);
|
||||
Mat dst(dims, isz, CV_8U, dstptr, dststep);
|
||||
|
||||
const Mat* arrays[] = { &src, &dst };
|
||||
uchar* ptrs[2];
|
||||
NAryMatIterator it(arrays, ptrs, 2);
|
||||
size_t j, planesz = it.size;
|
||||
|
||||
for( j = 0; j < it.nplanes; j++, ++it )
|
||||
memcpy(ptrs[1], ptrs[0], planesz);
|
||||
}
|
||||
|
||||
|
||||
void MatAllocator::upload(UMatData* u, const void* srcptr, int dims, const size_t sz[],
|
||||
const size_t dstofs[], const size_t dststep[],
|
||||
const size_t srcstep[]) const
|
||||
{
|
||||
if(!u)
|
||||
return;
|
||||
int isz[CV_MAX_DIM];
|
||||
uchar* dstptr = u->data;
|
||||
for( int i = 0; i < dims; i++ )
|
||||
{
|
||||
CV_Assert( sz[i] <= (size_t)INT_MAX );
|
||||
if( sz[i] == 0 )
|
||||
return;
|
||||
if( dstofs )
|
||||
dstptr += dstofs[i]*(i <= dims-2 ? dststep[i] : 1);
|
||||
isz[i] = (int)sz[i];
|
||||
}
|
||||
|
||||
Mat src(dims, isz, CV_8U, (void*)srcptr, srcstep);
|
||||
Mat dst(dims, isz, CV_8U, dstptr, dststep);
|
||||
|
||||
const Mat* arrays[] = { &src, &dst };
|
||||
uchar* ptrs[2];
|
||||
NAryMatIterator it(arrays, ptrs, 2);
|
||||
size_t j, planesz = it.size;
|
||||
|
||||
for( j = 0; j < it.nplanes; j++, ++it )
|
||||
memcpy(ptrs[1], ptrs[0], planesz);
|
||||
}
|
||||
|
||||
void MatAllocator::copy(UMatData* usrc, UMatData* udst, int dims, const size_t sz[],
|
||||
const size_t srcofs[], const size_t srcstep[],
|
||||
const size_t dstofs[], const size_t dststep[], bool sync) const
|
||||
{
|
||||
if(!usrc || !udst)
|
||||
return;
|
||||
int isz[CV_MAX_DIM];
|
||||
uchar* srcptr = usrc->data;
|
||||
uchar* dstptr = udst->data;
|
||||
for( int i = 0; i < dims; i++ )
|
||||
{
|
||||
CV_Assert( sz[i] <= (size_t)INT_MAX );
|
||||
if( sz[i] == 0 )
|
||||
return;
|
||||
if( srcofs )
|
||||
srcptr += srcofs[i]*(i <= dims-2 ? srcstep[i] : 1);
|
||||
if( dstofs )
|
||||
dstptr += dstofs[i]*(i <= dims-2 ? dststep[i] : 1);
|
||||
isz[i] = (int)sz[i];
|
||||
}
|
||||
|
||||
Mat src(dims, isz, CV_8U, srcptr, srcstep);
|
||||
Mat dst(dims, isz, CV_8U, dstptr, dststep);
|
||||
|
||||
const Mat* arrays[] = { &src, &dst };
|
||||
uchar* ptrs[2];
|
||||
NAryMatIterator it(arrays, ptrs, 2);
|
||||
size_t j, planesz = it.size;
|
||||
|
||||
for( j = 0; j < it.nplanes; j++, ++it )
|
||||
memcpy(ptrs[1], ptrs[0], planesz);
|
||||
}
|
||||
|
||||
class StdMatAllocator : public MatAllocator
|
||||
{
|
||||
public:
|
||||
UMatData* allocate(int dims, const int* sizes, int type, size_t* step) const
|
||||
UMatData* allocate(int dims, const int* sizes, int type,
|
||||
void* data0, size_t* step, int /*flags*/) const
|
||||
{
|
||||
size_t total = CV_ELEM_SIZE(type);
|
||||
for( int i = dims-1; i >= 0; i-- )
|
||||
{
|
||||
if( step )
|
||||
step[i] = total;
|
||||
{
|
||||
if( data0 && step[i] != CV_AUTOSTEP )
|
||||
{
|
||||
CV_Assert(total <= step[i]);
|
||||
total = step[i];
|
||||
}
|
||||
else
|
||||
step[i] = total;
|
||||
}
|
||||
total *= sizes[i];
|
||||
}
|
||||
uchar* data = (uchar*)fastMalloc(total);
|
||||
uchar* data = data0 ? (uchar*)data0 : (uchar*)fastMalloc(total);
|
||||
UMatData* u = new UMatData(this);
|
||||
u->data = u->origdata = data;
|
||||
u->size = total;
|
||||
u->refcount = 1;
|
||||
u->refcount = data0 == 0;
|
||||
if(data0)
|
||||
u->flags |= UMatData::USER_ALLOCATED;
|
||||
|
||||
return u;
|
||||
}
|
||||
|
||||
bool allocate(UMatData* u, int accessFlags) const
|
||||
bool allocate(UMatData* u, int /*accessFlags*/) const
|
||||
{
|
||||
if(!u) return false;
|
||||
if(u->handle != 0)
|
||||
return true;
|
||||
return UMat::getStdAllocator()->allocate(u, accessFlags);
|
||||
CV_XADD(&u->urefcount, 1);
|
||||
return true;
|
||||
}
|
||||
|
||||
void deallocate(UMatData* u) const
|
||||
{
|
||||
if(u)
|
||||
if(u && u->refcount == 0)
|
||||
{
|
||||
fastFree(u->origdata);
|
||||
if( !(u->flags & UMatData::USER_ALLOCATED) )
|
||||
{
|
||||
fastFree(u->origdata);
|
||||
u->origdata = 0;
|
||||
}
|
||||
delete u;
|
||||
}
|
||||
}
|
||||
|
||||
void map(UMatData*, int) const
|
||||
{
|
||||
}
|
||||
|
||||
void unmap(UMatData* u) const
|
||||
{
|
||||
if(u->urefcount == 0)
|
||||
deallocate(u);
|
||||
}
|
||||
|
||||
void download(UMatData* u, void* dstptr,
|
||||
int dims, const size_t sz[],
|
||||
const size_t srcofs[], const size_t srcstep[],
|
||||
const size_t dststep[]) const
|
||||
{
|
||||
if(!u)
|
||||
return;
|
||||
int isz[CV_MAX_DIM];
|
||||
uchar* srcptr = u->data;
|
||||
for( int i = 0; i < dims; i++ )
|
||||
{
|
||||
CV_Assert( sz[i] <= (size_t)INT_MAX );
|
||||
if( sz[i] == 0 )
|
||||
return;
|
||||
if( srcofs )
|
||||
srcptr += srcofs[i]*(i <= dims-2 ? srcstep[i] : 1);
|
||||
isz[i] = (int)sz[i];
|
||||
}
|
||||
|
||||
Mat src(dims, isz, CV_8U, srcptr, srcstep);
|
||||
Mat dst(dims, isz, CV_8U, dstptr, dststep);
|
||||
|
||||
const Mat* arrays[] = { &src, &dst };
|
||||
uchar* ptrs[2];
|
||||
NAryMatIterator it(arrays, ptrs, 2);
|
||||
size_t j, planesz = it.size;
|
||||
|
||||
for( j = 0; j < it.nplanes; j++, ++it )
|
||||
memcpy(ptrs[1], ptrs[0], planesz);
|
||||
}
|
||||
|
||||
void upload(UMatData* u, const void* srcptr, int dims, const size_t sz[],
|
||||
const size_t dstofs[], const size_t dststep[],
|
||||
const size_t srcstep[]) const
|
||||
{
|
||||
if(!u)
|
||||
return;
|
||||
int isz[CV_MAX_DIM];
|
||||
uchar* dstptr = u->data;
|
||||
for( int i = 0; i < dims; i++ )
|
||||
{
|
||||
CV_Assert( sz[i] <= (size_t)INT_MAX );
|
||||
if( sz[i] == 0 )
|
||||
return;
|
||||
if( dstofs )
|
||||
dstptr += dstofs[i]*(i <= dims-2 ? dststep[i] : 1);
|
||||
isz[i] = (int)sz[i];
|
||||
}
|
||||
|
||||
Mat src(dims, isz, CV_8U, (void*)srcptr, srcstep);
|
||||
Mat dst(dims, isz, CV_8U, dstptr, dststep);
|
||||
|
||||
const Mat* arrays[] = { &src, &dst };
|
||||
uchar* ptrs[2];
|
||||
NAryMatIterator it(arrays, ptrs, 2);
|
||||
size_t j, planesz = it.size;
|
||||
|
||||
for( j = 0; j < it.nplanes; j++, ++it )
|
||||
memcpy(ptrs[1], ptrs[0], planesz);
|
||||
}
|
||||
|
||||
void copy(UMatData* usrc, UMatData* udst, int dims, const size_t sz[],
|
||||
const size_t srcofs[], const size_t srcstep[],
|
||||
const size_t dstofs[], const size_t dststep[], bool) const
|
||||
{
|
||||
if(!usrc || !udst)
|
||||
return;
|
||||
int isz[CV_MAX_DIM];
|
||||
uchar* srcptr = usrc->data;
|
||||
uchar* dstptr = udst->data;
|
||||
for( int i = 0; i < dims; i++ )
|
||||
{
|
||||
CV_Assert( sz[i] <= (size_t)INT_MAX );
|
||||
if( sz[i] == 0 )
|
||||
return;
|
||||
if( srcofs )
|
||||
srcptr += srcofs[i]*(i <= dims-2 ? srcstep[i] : 1);
|
||||
if( dstofs )
|
||||
dstptr += dstofs[i]*(i <= dims-2 ? dststep[i] : 1);
|
||||
isz[i] = (int)sz[i];
|
||||
}
|
||||
|
||||
Mat src(dims, isz, CV_8U, srcptr, srcstep);
|
||||
Mat dst(dims, isz, CV_8U, dstptr, dststep);
|
||||
|
||||
const Mat* arrays[] = { &src, &dst };
|
||||
uchar* ptrs[2];
|
||||
NAryMatIterator it(arrays, ptrs, 2);
|
||||
size_t j, planesz = it.size;
|
||||
|
||||
for( j = 0; j < it.nplanes; j++, ++it )
|
||||
memcpy(ptrs[1], ptrs[0], planesz);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -364,13 +383,13 @@ void Mat::create(int d, const int* _sizes, int _type)
|
||||
a = a0;
|
||||
try
|
||||
{
|
||||
u = a->allocate(dims, size, _type, step.p);
|
||||
u = a->allocate(dims, size, _type, 0, step.p, 0);
|
||||
CV_Assert(u != 0);
|
||||
}
|
||||
catch(...)
|
||||
{
|
||||
if(a != a0)
|
||||
u = a0->allocate(dims, size, _type, step.p);
|
||||
u = a0->allocate(dims, size, _type, 0, step.p, 0);
|
||||
CV_Assert(u != 0);
|
||||
}
|
||||
CV_Assert( step[dims-1] == (size_t)CV_ELEM_SIZE(flags) );
|
||||
|
||||
Reference in New Issue
Block a user