Skip to content

Commit 212815a

Browse files
committed
core(ocl): fix lifetime handling of Image kernel args
1 parent 68fb8dd commit 212815a

File tree

1 file changed

+16
-6
lines changed

1 file changed

+16
-6
lines changed

modules/core/src/ocl.cpp

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2802,16 +2802,24 @@ struct Kernel::Impl
28022802
haveTempSrcUMats = true; // UMat is created on RAW memory (without proper lifetime management, even from Mat)
28032803
}
28042804

2805-
void addImage(const Image2D& image)
2805+
/// Preserve image lifetime (while it is specified as Kernel argument)
2806+
void registerImageArgument(int arg, const Image2D& image)
28062807
{
2807-
images.push_back(image);
2808+
CV_CheckGE(arg, 0, "");
2809+
CV_CheckLT(arg, (int)MAX_ARRS, "");
2810+
if (arg < (int)shadow_images.size() && shadow_images[arg].ptr() != image.ptr()) // TODO future: replace ptr => impl (more strong check)
2811+
{
2812+
CV_Check(arg, !isInProgress, "ocl::Kernel: clearing of pending Image2D arguments is not allowed");
2813+
}
2814+
shadow_images.reserve(MAX_ARRS);
2815+
shadow_images.resize(std::max(shadow_images.size(), (size_t)arg + 1));
2816+
shadow_images[arg] = image;
28082817
}
28092818

28102819
void finit(cl_event e)
28112820
{
28122821
CV_UNUSED(e);
28132822
cleanupUMats();
2814-
images.clear();
28152823
isInProgress = false;
28162824
release();
28172825
}
@@ -2836,7 +2844,7 @@ struct Kernel::Impl
28362844
bool isInProgress;
28372845
bool isAsyncRun; // true if kernel was scheduled in async mode
28382846
int nu;
2839-
std::list<Image2D> images;
2847+
std::vector<Image2D> shadow_images;
28402848
bool haveTempDstUMats;
28412849
bool haveTempSrcUMats;
28422850
};
@@ -2978,9 +2986,11 @@ int Kernel::set(int i, const void* value, size_t sz)
29782986

29792987
int Kernel::set(int i, const Image2D& image2D)
29802988
{
2981-
p->addImage(image2D);
29822989
cl_mem h = (cl_mem)image2D.ptr();
2983-
return set(i, &h, sizeof(h));
2990+
int res = set(i, &h, sizeof(h));
2991+
if (res >= 0)
2992+
p->registerImageArgument(i, image2D);
2993+
return res;
29842994
}
29852995

29862996
int Kernel::set(int i, const UMat& m)

0 commit comments

Comments
 (0)