@@ -229,8 +229,15 @@ class CV_EXPORTS_W_SIMPLE Device
229
229
230
230
CV_WRAP static const Device& getDefault ();
231
231
232
- protected:
232
+ /* *
233
+ * @param d OpenCL handle (cl_device_id). clRetainDevice() is called on success.
234
+ */
235
+ static Device fromHandle (void * d);
236
+
233
237
struct Impl ;
238
+ inline Impl* getImpl () const { return (Impl*)p; }
239
+ inline bool empty () const { return !p; }
240
+ protected:
234
241
Impl* p;
235
242
};
236
243
@@ -239,33 +246,55 @@ class CV_EXPORTS Context
239
246
{
240
247
public:
241
248
Context ();
242
- explicit Context (int dtype);
249
+ explicit Context (int dtype); // !< @deprecated
243
250
~Context ();
244
251
Context (const Context& c);
245
- Context& operator = (const Context& c);
252
+ Context& operator = (const Context& c);
246
253
254
+ /* * @deprecated */
247
255
bool create ();
256
+ /* * @deprecated */
248
257
bool create (int dtype);
258
+
249
259
size_t ndevices () const ;
250
- const Device& device (size_t idx) const ;
260
+ Device& device (size_t idx) const ;
251
261
Program getProg (const ProgramSource& prog,
252
262
const String& buildopt, String& errmsg);
253
263
void unloadProg (Program& prog);
254
264
265
+
266
+ /* * Get thread-local OpenCL context (initialize if necessary) */
267
+ #if 0 // OpenCV 5.0
268
+ static Context& getDefault();
269
+ #else
255
270
static Context& getDefault (bool initialize = true );
271
+ #endif
272
+
273
+ /* * @returns cl_context value */
256
274
void * ptr () const ;
257
275
258
- friend void initializeContextFromHandle (Context& ctx, void * platform, void * context, void * device);
259
276
260
277
bool useSVM () const ;
261
278
void setUseSVM (bool enabled);
262
279
280
+ /* *
281
+ * @param context OpenCL handle (cl_context). clRetainContext() is called on success
282
+ */
283
+ static Context fromHandle (void * context);
284
+ static Context fromDevice (const ocl::Device& device);
285
+ static Context create (const std::string& configuration);
286
+
287
+ void release ();
288
+
263
289
struct Impl ;
264
290
inline Impl* getImpl () const { return (Impl*)p; }
291
+ inline bool empty () const { return !p; }
292
+ // TODO OpenCV 5.0
265
293
// protected:
266
294
Impl* p;
267
295
};
268
296
297
+ /* * @deprecated */
269
298
class CV_EXPORTS Platform
270
299
{
271
300
public:
@@ -275,11 +304,14 @@ class CV_EXPORTS Platform
275
304
Platform& operator = (const Platform& p);
276
305
277
306
void * ptr () const ;
307
+
308
+ /* * @deprecated */
278
309
static Platform& getDefault ();
279
310
280
- friend void initializeContextFromHandle (Context& ctx, void * platform, void * context, void * device);
281
- protected:
282
311
struct Impl ;
312
+ inline Impl* getImpl () const { return (Impl*)p; }
313
+ inline bool empty () const { return !p; }
314
+ protected:
283
315
Impl* p;
284
316
};
285
317
@@ -319,6 +351,7 @@ CV_EXPORTS void convertFromBuffer(void* cl_mem_buffer, size_t step, int rows, in
319
351
CV_EXPORTS void convertFromImage (void * cl_mem_image, UMat& dst);
320
352
321
353
// TODO Move to internal header
354
+ // / @deprecated
322
355
void initializeContextFromHandle (Context& ctx, void * platform, void * context, void * device);
323
356
324
357
class CV_EXPORTS Queue
@@ -340,6 +373,7 @@ class CV_EXPORTS Queue
340
373
341
374
struct Impl ; friend struct Impl ;
342
375
inline Impl* getImpl () const { return p; }
376
+ inline bool empty () const { return !p; }
343
377
protected:
344
378
Impl* p;
345
379
};
@@ -490,6 +524,7 @@ class CV_EXPORTS Program
490
524
491
525
struct Impl ; friend struct Impl ;
492
526
inline Impl* getImpl () const { return (Impl*)p; }
527
+ inline bool empty () const { return !p; }
493
528
protected:
494
529
Impl* p;
495
530
public:
@@ -571,6 +606,7 @@ class CV_EXPORTS ProgramSource
571
606
572
607
struct Impl ; friend struct Impl ;
573
608
inline Impl* getImpl () const { return (Impl*)p; }
609
+ inline bool empty () const { return !p; }
574
610
protected:
575
611
Impl* p;
576
612
};
@@ -579,6 +615,9 @@ class CV_EXPORTS PlatformInfo
579
615
{
580
616
public:
581
617
PlatformInfo ();
618
+ /* *
619
+ * @param id pointer cl_platform_id (cl_platform_id*)
620
+ */
582
621
explicit PlatformInfo (void * id);
583
622
~PlatformInfo ();
584
623
@@ -591,8 +630,9 @@ class CV_EXPORTS PlatformInfo
591
630
int deviceNumber () const ;
592
631
void getDevice (Device& device, int d) const ;
593
632
594
- protected:
595
633
struct Impl ;
634
+ bool empty () const { return !p; }
635
+ protected:
596
636
Impl* p;
597
637
};
598
638
@@ -689,6 +729,106 @@ class CV_EXPORTS Timer
689
729
CV_EXPORTS MatAllocator* getOpenCLAllocator ();
690
730
691
731
732
+ class CV_EXPORTS_W OpenCLExecutionContext
733
+ {
734
+ public:
735
+ OpenCLExecutionContext () = default ;
736
+ ~OpenCLExecutionContext () = default ;
737
+
738
+ OpenCLExecutionContext (const OpenCLExecutionContext& other) = default ;
739
+ OpenCLExecutionContext (OpenCLExecutionContext&& other) = default ;
740
+
741
+ OpenCLExecutionContext& operator =(const OpenCLExecutionContext& other) = default ;
742
+ OpenCLExecutionContext& operator =(OpenCLExecutionContext&& other) = default ;
743
+
744
+ /* * Get associated ocl::Context */
745
+ Context& getContext () const ;
746
+ /* * Get associated ocl::Device */
747
+ Device& getDevice () const ;
748
+ /* * Get associated ocl::Queue */
749
+ Queue& getQueue () const ;
750
+
751
+ bool useOpenCL () const ;
752
+ void setUseOpenCL (bool flag);
753
+
754
+ /* * Get OpenCL execution context of current thread.
755
+ *
756
+ * Initialize OpenCL execution context if it is empty
757
+ * - create new
758
+ * - reuse context of the main thread (threadID = 0)
759
+ */
760
+ static OpenCLExecutionContext& getCurrent ();
761
+
762
+ /* * Get OpenCL execution context of current thread (can be empty) */
763
+ static OpenCLExecutionContext& getCurrentRef ();
764
+
765
+ /* * Bind this OpenCL execution context to current thread.
766
+ *
767
+ * Context can't be empty.
768
+ *
769
+ * @note clFinish is not called for queue of previous execution context
770
+ */
771
+ void bind () const ;
772
+
773
+ /* * Creates new execution context with same OpenCV context and device
774
+ *
775
+ * @param q OpenCL queue
776
+ */
777
+ OpenCLExecutionContext cloneWithNewQueue (const ocl::Queue& q) const ;
778
+ /* * @overload */
779
+ OpenCLExecutionContext cloneWithNewQueue () const ;
780
+
781
+ /* * @brief Creates OpenCL execution context
782
+ * OpenCV will check if available OpenCL platform has platformName name, then assign context to
783
+ * OpenCV and call `clRetainContext` function. The deviceID device will be used as target device and
784
+ * new command queue will be created.
785
+ *
786
+ * @note Lifetime of passed handles is transferred to OpenCV wrappers on success
787
+ *
788
+ * @param platformName name of OpenCL platform to attach, this string is used to check if platform is available to OpenCV at runtime
789
+ * @param platformID ID of platform attached context was created for (cl_platform_id)
790
+ * @param context OpenCL context to be attached to OpenCV (cl_context)
791
+ * @param deviceID OpenCL device (cl_device_id)
792
+ */
793
+ static OpenCLExecutionContext create (const std::string& platformName, void * platformID, void * context, void * deviceID);
794
+
795
+ /* * @brief Creates OpenCL execution context
796
+ *
797
+ * @param context non-empty OpenCL context
798
+ * @param device non-empty OpenCL device (must be a part of context)
799
+ * @param queue non-empty OpenCL queue for provided context and device
800
+ */
801
+ static OpenCLExecutionContext create (const Context& context, const Device& device, const ocl::Queue& queue);
802
+ /* * @overload */
803
+ static OpenCLExecutionContext create (const Context& context, const Device& device);
804
+
805
+ struct Impl ;
806
+ inline bool empty () const { return !p; }
807
+ void release ();
808
+ protected:
809
+ std::shared_ptr<Impl> p;
810
+ };
811
+
812
+ class OpenCLExecutionContextScope
813
+ {
814
+ OpenCLExecutionContext ctx_;
815
+ public:
816
+ inline OpenCLExecutionContextScope (const OpenCLExecutionContext& ctx)
817
+ {
818
+ CV_Assert (!ctx.empty ());
819
+ ctx_ = OpenCLExecutionContext::getCurrentRef ();
820
+ ctx.bind ();
821
+ }
822
+
823
+ inline ~OpenCLExecutionContextScope ()
824
+ {
825
+ if (!ctx_.empty ())
826
+ {
827
+ ctx_.bind ();
828
+ }
829
+ }
830
+ };
831
+
692
832
#ifdef __OPENCV_BUILD
693
833
namespace internal {
694
834
0 commit comments