From 3a263c6326d2e46ef85474a0eb991160d483031c Mon Sep 17 00:00:00 2001 From: Alexander Karsakov Date: Thu, 23 Oct 2014 14:23:37 +0300 Subject: [PATCH 1/2] Added tests for Image2D --- modules/core/src/ocl.cpp | 3 ++ modules/core/test/ocl/test_image2d.cpp | 43 ++++++++++++++++++++++++++ 2 files changed, 46 insertions(+) create mode 100644 modules/core/test/ocl/test_image2d.cpp diff --git a/modules/core/src/ocl.cpp b/modules/core/src/ocl.cpp index c333b08c40..708730ee50 100644 --- a/modules/core/src/ocl.cpp +++ b/modules/core/src/ocl.cpp @@ -4624,6 +4624,9 @@ struct Image2D::Impl static bool isFormatSupported(cl_image_format format) { + if (!haveOpenCL()) + CV_Error(Error::OpenCLApiCallError, "OpenCL runtime not found!"); + cl_context context = (cl_context)Context::getDefault().ptr(); // Figure out how many formats are supported by this context. cl_uint numFormats = 0; diff --git a/modules/core/test/ocl/test_image2d.cpp b/modules/core/test/ocl/test_image2d.cpp new file mode 100644 index 0000000000..412761a7f8 --- /dev/null +++ b/modules/core/test/ocl/test_image2d.cpp @@ -0,0 +1,43 @@ +// This file is part of OpenCV project. +// It is subject to the license terms in the LICENSE file found in the top-level directory +// of this distribution and at http://opencv.org/license.html. + +// Copyright (C) 2014, Itseez, Inc., all rights reserved. +// Third party copyrights are property of their respective owners. + +#include "../test_precomp.hpp" +#include "opencv2/ts/ocl_test.hpp" + +#ifdef HAVE_OPENCL + +namespace cvtest { +namespace ocl { + +PARAM_TEST_CASE(Image2DBasicTest, int, int) +{ + int depth, ch; + + +}; + +TEST(Image2D, turnOffOpenCL) +{ + if (cv::ocl::haveOpenCL()) + { + // save the current state + bool useOCL = cv::ocl::useOpenCL(); + + cv::ocl::setUseOpenCL(true); + UMat um(128, 128, CV_8UC1); + + cv::ocl::setUseOpenCL(false); + cv::ocl::Image2D image(um); + + // reset state to the previous one + cv::ocl::setUseOpenCL(useOCL); + } +} + +} } // namespace cvtest::ocl + +#endif // HAVE_OPENCL \ No newline at end of file From 237cb9314378f0819e10a9c95eb41ec61e2c3ebe Mon Sep 17 00:00:00 2001 From: Alexander Karsakov Date: Fri, 24 Oct 2014 13:55:16 +0300 Subject: [PATCH 2/2] Added extra checks to ocl::Image2D --- modules/core/src/ocl.cpp | 9 +++- modules/core/test/ocl/test_image2d.cpp | 61 ++++++++++++++++++++++++-- 2 files changed, 65 insertions(+), 5 deletions(-) diff --git a/modules/core/src/ocl.cpp b/modules/core/src/ocl.cpp index 708730ee50..9200c29f55 100644 --- a/modules/core/src/ocl.cpp +++ b/modules/core/src/ocl.cpp @@ -4650,6 +4650,10 @@ struct Image2D::Impl void init(const UMat &src, bool norm, bool alias) { + if (!haveOpenCL()) + CV_Error(Error::OpenCLApiCallError, "OpenCL runtime not found!"); + + CV_Assert(!src.empty()); CV_Assert(ocl::Device::getDefault().imageSupport()); int err, depth = src.depth(), cn = src.channels(); @@ -4659,6 +4663,9 @@ struct Image2D::Impl if (!isFormatSupported(format)) CV_Error(Error::OpenCLApiCallError, "Image format is not supported"); + if (alias && !src.handle(ACCESS_RW)) + CV_Error(Error::OpenCLApiCallError, "Incorrect UMat, handle is null"); + cl_context context = (cl_context)Context::getDefault().ptr(); cl_command_queue queue = (cl_command_queue)Queue::getDefault().ptr(); @@ -4743,7 +4750,7 @@ bool Image2D::canCreateAlias(const UMat &m) { bool ret = false; const Device & d = ocl::Device::getDefault(); - if (d.imageFromBufferSupport()) + if (d.imageFromBufferSupport() && !m.empty()) { // This is the required pitch alignment in pixels uint pitchAlign = d.imagePitchAlignment(); diff --git a/modules/core/test/ocl/test_image2d.cpp b/modules/core/test/ocl/test_image2d.cpp index 412761a7f8..dcfc701f11 100644 --- a/modules/core/test/ocl/test_image2d.cpp +++ b/modules/core/test/ocl/test_image2d.cpp @@ -13,12 +13,55 @@ namespace cvtest { namespace ocl { -PARAM_TEST_CASE(Image2DBasicTest, int, int) +TEST(Image2D, createAliasEmptyUMat) { - int depth, ch; + if (cv::ocl::haveOpenCL()) + { + UMat um; + EXPECT_FALSE(cv::ocl::Image2D::canCreateAlias(um)); + } + else + std::cout << "OpenCL runtime not found. Test skipped." << std::endl; +} + +TEST(Image2D, createImage2DWithEmptyUMat) +{ + if (cv::ocl::haveOpenCL()) + { + UMat um; + EXPECT_ANY_THROW(cv::ocl::Image2D image(um)); + } + else + std::cout << "OpenCL runtime not found. Test skipped." << std::endl; +} + +TEST(Image2D, createAlias) +{ + if (cv::ocl::haveOpenCL()) + { + const cv::ocl::Device & d = cv::ocl::Device::getDefault(); + int minor = d.deviceVersionMinor(), major = d.deviceVersionMajor(); + // aliases is OpenCL 1.2 extension + if (1 < major || (1 == major && 2 <= minor)) + { + UMat um(128, 128, CV_8UC1); + bool isFormatSupported = false, canCreateAlias = false; -}; + EXPECT_NO_THROW(isFormatSupported = cv::ocl::Image2D::isFormatSupported(CV_8U, 1, false)); + EXPECT_NO_THROW(canCreateAlias = cv::ocl::Image2D::canCreateAlias(um)); + + if (isFormatSupported && canCreateAlias) + { + EXPECT_NO_THROW(cv::ocl::Image2D image(um, false, true)); + } + else + std::cout << "Impossible to create alias for selected image. Test skipped." << std::endl; + } + } + else + std::cout << "OpenCL runtime not found. Test skipped" << std::endl; +} TEST(Image2D, turnOffOpenCL) { @@ -26,16 +69,26 @@ TEST(Image2D, turnOffOpenCL) { // save the current state bool useOCL = cv::ocl::useOpenCL(); + bool isFormatSupported = false; cv::ocl::setUseOpenCL(true); UMat um(128, 128, CV_8UC1); cv::ocl::setUseOpenCL(false); - cv::ocl::Image2D image(um); + EXPECT_NO_THROW(isFormatSupported = cv::ocl::Image2D::isFormatSupported(CV_8U, 1, true)); + + if (isFormatSupported) + { + EXPECT_NO_THROW(cv::ocl::Image2D image(um)); + } + else + std::cout << "CV_8UC1 is not supported for OpenCL images. Test skipped." << std::endl; // reset state to the previous one cv::ocl::setUseOpenCL(useOCL); } + else + std::cout << "OpenCL runtime not found. Test skipped." << std::endl; } } } // namespace cvtest::ocl