|
42 | 42 | #include "precomp.hpp"
|
43 | 43 | #include <list>
|
44 | 44 | #include <map>
|
| 45 | +#include <deque> |
45 | 46 | #include <string>
|
46 | 47 | #include <sstream>
|
47 | 48 | #include <iostream> // std::cerr
|
@@ -1983,7 +1984,10 @@ struct Kernel::Impl
|
1983 | 1984 | if( u[i] )
|
1984 | 1985 | {
|
1985 | 1986 | if( CV_XADD(&u[i]->urefcount, -1) == 1 )
|
| 1987 | + { |
| 1988 | + u[i]->flags |= UMatData::ASYNC_CLEANUP; |
1986 | 1989 | u[i]->currAllocator->deallocate(u[i]);
|
| 1990 | + } |
1987 | 1991 | u[i] = 0;
|
1988 | 1992 | }
|
1989 | 1993 | nu = 0;
|
@@ -3157,6 +3161,10 @@ class OpenCLAllocator : public MatAllocator
|
3157 | 3161 |
|
3158 | 3162 | matStdAllocator = Mat::getDefaultAllocator();
|
3159 | 3163 | }
|
| 3164 | + ~OpenCLAllocator() |
| 3165 | + { |
| 3166 | + flushCleanupQueue(); |
| 3167 | + } |
3160 | 3168 |
|
3161 | 3169 | UMatData* defaultAllocate(int dims, const int* sizes, int type, void* data, size_t* step,
|
3162 | 3170 | int flags, UMatUsageFlags usageFlags) const
|
@@ -3193,6 +3201,7 @@ class OpenCLAllocator : public MatAllocator
|
3193 | 3201 | }
|
3194 | 3202 |
|
3195 | 3203 | Context& ctx = Context::getDefault();
|
| 3204 | + flushCleanupQueue(); |
3196 | 3205 |
|
3197 | 3206 | int createFlags = 0, flags0 = 0;
|
3198 | 3207 | getBestFlags(ctx, flags, usageFlags, createFlags, flags0);
|
@@ -3247,6 +3256,8 @@ class OpenCLAllocator : public MatAllocator
|
3247 | 3256 | if(!u)
|
3248 | 3257 | return false;
|
3249 | 3258 |
|
| 3259 | + flushCleanupQueue(); |
| 3260 | + |
3250 | 3261 | UMatDataAutoLock lock(u);
|
3251 | 3262 |
|
3252 | 3263 | if(u->handle == 0)
|
@@ -3381,6 +3392,15 @@ class OpenCLAllocator : public MatAllocator
|
3381 | 3392 |
|
3382 | 3393 | CV_Assert(u->handle != 0);
|
3383 | 3394 | CV_Assert(u->mapcount == 0);
|
| 3395 | + |
| 3396 | + if (u->flags & UMatData::ASYNC_CLEANUP) |
| 3397 | + addToCleanupQueue(u); |
| 3398 | + else |
| 3399 | + deallocate_(u); |
| 3400 | + } |
| 3401 | + |
| 3402 | + void deallocate_(UMatData* u) const |
| 3403 | + { |
3384 | 3404 | if(u->tempUMat())
|
3385 | 3405 | {
|
3386 | 3406 | CV_Assert(u->origdata);
|
@@ -4184,6 +4204,33 @@ class OpenCLAllocator : public MatAllocator
|
4184 | 4204 | }
|
4185 | 4205 |
|
4186 | 4206 | MatAllocator* matStdAllocator;
|
| 4207 | + |
| 4208 | + mutable cv::Mutex cleanupQueueMutex; |
| 4209 | + mutable std::deque<UMatData*> cleanupQueue; |
| 4210 | + |
| 4211 | + void flushCleanupQueue() const |
| 4212 | + { |
| 4213 | + if (!cleanupQueue.empty()) |
| 4214 | + { |
| 4215 | + std::deque<UMatData*> q; |
| 4216 | + { |
| 4217 | + cv::AutoLock lock(cleanupQueueMutex); |
| 4218 | + q.swap(cleanupQueue); |
| 4219 | + } |
| 4220 | + for (std::deque<UMatData*>::const_iterator i = q.begin(); i != q.end(); ++i) |
| 4221 | + { |
| 4222 | + deallocate_(*i); |
| 4223 | + } |
| 4224 | + } |
| 4225 | + } |
| 4226 | + void addToCleanupQueue(UMatData* u) const |
| 4227 | + { |
| 4228 | + //TODO: Validation check: CV_Assert(!u->tempUMat()); |
| 4229 | + { |
| 4230 | + cv::AutoLock lock(cleanupQueueMutex); |
| 4231 | + cleanupQueue.push_back(u); |
| 4232 | + } |
| 4233 | + } |
4187 | 4234 | };
|
4188 | 4235 |
|
4189 | 4236 | MatAllocator* getOpenCLAllocator()
|
|
0 commit comments