• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * cv_context.cpp - used to init_opencv_ocl once
3  *
4  *  Copyright (c) 2016-2017 Intel Corporation
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *   http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * Author: Andrey Parfenov <a1994ndrey@gmail.com>
19  * Author: Wind Yuan <feng.yuan@intel.com>
20  */
21 
22 #include "cv_context.h"
23 #include "cl_device.h"
24 #include "cl_memory.h"
25 
26 namespace XCam {
27 
28 Mutex CVContext::_init_mutex;
29 SmartPtr<CVContext> CVContext::_instance;
30 
31 
32 SmartPtr<CVContext>
instance()33 CVContext::instance ()
34 {
35     SmartLock locker (_init_mutex);
36     if (_instance.ptr())
37         return _instance;
38 
39     _instance = new CVContext ();
40     _instance->init_opencv_ocl ();
41     return _instance;
42 }
43 
44 void
init_opencv_ocl()45 CVContext::init_opencv_ocl ()
46 {
47     _context = CLDevice::instance()->get_context();
48     cl_platform_id platform_id = CLDevice::instance()->get_platform_id ();
49     char *platform_name = CLDevice::instance()->get_platform_name ();
50     cl_device_id device_id = CLDevice::instance()->get_device_id ();
51     cl_context _context_id = _context->get_context_id ();
52     cv::ocl::attachContext (platform_name, platform_id, _context_id, device_id);
53     cv::ocl::setUseOpenCL (cv::ocl::haveOpenCL());
54     XCAM_LOG_DEBUG("Use OpenCL is:  %s", cv::ocl::haveOpenCL() ? "true" : "false");
55 }
56 
57 bool
enable_ocl(bool flag)58 CVContext::enable_ocl (bool flag)
59 {
60     if (flag && !cv::ocl::haveOpenCL()) {
61         return false;
62     }
63     cv::ocl::setUseOpenCL (flag);
64     return true;
65 }
66 
67 bool
is_ocl_enabled() const68 CVContext::is_ocl_enabled () const
69 {
70     return cv::ocl::useOpenCL ();
71 }
72 
CVContext()73 CVContext::CVContext ()
74 {
75 
76 }
77 
~CVContext()78 CVContext::~CVContext () {
79 
80 }
81 
82 }
83