Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/3.4' into merge-3.4
Browse files Browse the repository at this point in the history
  • Loading branch information
alalek committed Apr 2, 2020
2 parents 277f0d2 + 6216bf9 commit cf2a3c8
Show file tree
Hide file tree
Showing 14 changed files with 394 additions and 107 deletions.
135 changes: 135 additions & 0 deletions modules/core/perf/opencl/perf_matop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,141 @@ OCL_PERF_TEST_P(CopyToFixture, CopyToWithMaskUninit,
SANITY_CHECK(dst);
}



enum ROIType {
ROI_FULL,
ROI_2_RECT,
ROI_2_TOP, // contiguous memory block
ROI_2_LEFT,
ROI_4,
ROI_16,
};
static Rect getROI(enum ROIType t, const Size& sz)
{
switch (t)
{
case ROI_FULL: return Rect(0, 0, sz.width, sz.height);
case ROI_2_RECT: return Rect(0, 0, sz.width * 71 / 100, sz.height * 71 / 100); // 71 = sqrt(1/2) * 100
case ROI_2_TOP: return Rect(0, 0, sz.width, sz.height / 2); // 71 = sqrt(1/2) * 100
case ROI_2_LEFT: return Rect(0, 0, sz.width / 2, sz.height); // 71 = sqrt(1/2) * 100
case ROI_4: return Rect(0, 0, sz.width / 2, sz.height / 2);
case ROI_16: return Rect(0, 0, sz.width / 4, sz.height / 4);
}
CV_Assert(false);
}

typedef TestBaseWithParam< tuple<cv::Size, MatType, ROIType> > OpenCLBuffer;

static inline void PrintTo(const tuple<cv::Size, MatType, enum ROIType>& v, std::ostream* os)
{
*os << "(" << get<0>(v) << ", " << typeToString(get<1>(v)) << ", ";
enum ROIType roiType = get<2>(v);
if (roiType == ROI_FULL)
*os << "ROI_100_FULL";
else if (roiType == ROI_2_RECT)
*os << "ROI_050_RECT_HALF";
else if (roiType == ROI_2_TOP)
*os << "ROI_050_TOP_HALF";
else if (roiType == ROI_2_LEFT)
*os << "ROI_050_LEFT_HALF";
else if (roiType == ROI_4)
*os << "ROI_025_1/4";
else
*os << "ROI_012_1/16";
*os << ")";
}

PERF_TEST_P_(OpenCLBuffer, cpu_write)
{
const Size srcSize = get<0>(GetParam());
const int type = get<1>(GetParam());
const Rect roi = getROI(get<2>(GetParam()), srcSize);

checkDeviceMaxMemoryAllocSize(srcSize, type);

UMat src(srcSize, type);
declare.in(src(roi), WARMUP_NONE);

OCL_TEST_CYCLE()
{
Mat m = src(roi).getMat(ACCESS_WRITE);
m.setTo(Scalar(1, 2, 3, 4));
}

SANITY_CHECK_NOTHING();
}

PERF_TEST_P_(OpenCLBuffer, cpu_read)
{
const Size srcSize = get<0>(GetParam());
const int type = get<1>(GetParam());
const Rect roi = getROI(get<2>(GetParam()), srcSize);

checkDeviceMaxMemoryAllocSize(srcSize, type);

UMat src(srcSize, type, Scalar(1, 2, 3, 4));
declare.in(src(roi), WARMUP_NONE);

OCL_TEST_CYCLE()
{
unsigned counter = 0;
Mat m = src(roi).getMat(ACCESS_READ);
for (int y = 0; y < m.rows; y++)
{
uchar* ptr = m.ptr(y);
size_t width_bytes = m.cols * m.elemSize();
for (size_t x_bytes = 0; x_bytes < width_bytes; x_bytes++)
counter += (unsigned)(ptr[x_bytes]);
}
}

SANITY_CHECK_NOTHING();
}

PERF_TEST_P_(OpenCLBuffer, cpu_update)
{
const Size srcSize = get<0>(GetParam());
const int type = get<1>(GetParam());
const Rect roi = getROI(get<2>(GetParam()), srcSize);

checkDeviceMaxMemoryAllocSize(srcSize, type);

UMat src(srcSize, type, Scalar(1, 2, 3, 4));
declare.in(src(roi), WARMUP_NONE);

OCL_TEST_CYCLE()
{
Mat m = src(roi).getMat(ACCESS_READ | ACCESS_WRITE);
for (int y = 0; y < m.rows; y++)
{
uchar* ptr = m.ptr(y);
size_t width_bytes = m.cols * m.elemSize();
for (size_t x_bytes = 0; x_bytes < width_bytes; x_bytes++)
ptr[x_bytes] += 1;
}
}

SANITY_CHECK_NOTHING();
}

INSTANTIATE_TEST_CASE_P(/*FULL*/, OpenCLBuffer,
testing::Combine(
testing::Values(szVGA, sz720p, sz1080p, sz2160p),
testing::Values(CV_8UC1, CV_8UC2, CV_8UC3, CV_8UC4),
testing::Values(ROI_FULL)
)
);

INSTANTIATE_TEST_CASE_P(ROI, OpenCLBuffer,
testing::Combine(
testing::Values(sz1080p, sz2160p),
testing::Values(CV_8UC1),
testing::Values(ROI_16, ROI_4, ROI_2_RECT, ROI_2_LEFT, ROI_2_TOP, ROI_FULL)
)
);


} } // namespace opencv_test::ocl

#endif // HAVE_OPENCL
9 changes: 9 additions & 0 deletions modules/core/src/matrix_expressions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
// */

#include "precomp.hpp"
#include <opencv2/core/utils/logger.hpp>

namespace cv
{
Expand Down Expand Up @@ -1319,10 +1320,18 @@ void MatOp_AddEx::assign(const MatExpr& e, Mat& m, int _type) const
cv::add(dst, e.s, dst);
}
else
{
if (e.a.channels() > 1)
CV_LOG_ONCE_WARNING(NULL, "OpenCV/MatExpr: processing of multi-channel arrays might be changed in the future: "
"https://github.com/opencv/opencv/issues/16739");
cv::addWeighted(e.a, e.alpha, e.b, e.beta, e.s[0], dst);
}
}
else if( e.s.isReal() && (dst.data != m.data || fabs(e.alpha) != 1))
{
if (e.a.channels() > 1)
CV_LOG_ONCE_WARNING(NULL, "OpenCV/MatExpr: processing of multi-channel arrays might be changed in the future: "
"https://github.com/opencv/opencv/issues/16739");
e.a.convertTo(m, _type, e.alpha, e.s[0]);
return;
}
Expand Down
21 changes: 20 additions & 1 deletion modules/core/src/ocl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4602,14 +4602,33 @@ class OpenCLAllocator CV_FINAL : public MatAllocator
return u;
}

static bool isOpenCLMapForced() // force clEnqueueMapBuffer / clEnqueueUnmapMemObject OpenCL API
{
static bool value = cv::utils::getConfigurationParameterBool("OPENCV_OPENCL_BUFFER_FORCE_MAPPING", false);
return value;
}
static bool isOpenCLCopyingForced() // force clEnqueueReadBuffer[Rect] / clEnqueueWriteBuffer[Rect] OpenCL API
{
static bool value = cv::utils::getConfigurationParameterBool("OPENCV_OPENCL_BUFFER_FORCE_COPYING", false);
return value;
}

void getBestFlags(const Context& ctx, AccessFlag /*flags*/, UMatUsageFlags usageFlags, int& createFlags, UMatData::MemoryFlag& flags0) const
{
const Device& dev = ctx.device(0);
createFlags = 0;
if ((usageFlags & USAGE_ALLOCATE_HOST_MEMORY) != 0)
createFlags |= CL_MEM_ALLOC_HOST_PTR;

if( dev.hostUnifiedMemory() )
if (!isOpenCLCopyingForced() &&
(isOpenCLMapForced() ||
(dev.hostUnifiedMemory()
#ifndef __APPLE__
|| dev.isIntel()
#endif
)
)
)
flags0 = static_cast<UMatData::MemoryFlag>(0);
else
flags0 = UMatData::COPY_ON_MAP;
Expand Down
51 changes: 51 additions & 0 deletions modules/imgcodecs/test/test_common.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// 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
#include "test_precomp.hpp"
#include "test_common.hpp"

namespace opencv_test {

static
Mat generateTestImageBGR_()
{
Size sz(640, 480);
Mat result(sz, CV_8UC3, Scalar::all(0));

const string fname = cvtest::findDataFile("../cv/shared/baboon.png");
Mat image = imread(fname, IMREAD_COLOR);
CV_Assert(!image.empty());
CV_CheckEQ(image.size(), Size(512, 512), "");
Rect roi((640-512) / 2, 0, 512, 480);
image(Rect(0, 0, 512, 480)).copyTo(result(roi));
result(Rect(0, 0, 5, 5)).setTo(Scalar(0, 0, 255)); // R
result(Rect(5, 0, 5, 5)).setTo(Scalar(0, 255, 0)); // G
result(Rect(10, 0, 5, 5)).setTo(Scalar(255, 0, 0)); // B
result(Rect(0, 5, 5, 5)).setTo(Scalar(128, 128, 128)); // gray
//imshow("test_image", result); waitKey();
return result;
}
Mat generateTestImageBGR()
{
static Mat image = generateTestImageBGR_(); // initialize once
CV_Assert(!image.empty());
return image;
}

static
Mat generateTestImageGrayscale_()
{
Mat imageBGR = generateTestImageBGR();
CV_Assert(!imageBGR.empty());

Mat result;
cvtColor(imageBGR, result, COLOR_BGR2GRAY);
return result;
}
Mat generateTestImageGrayscale()
{
static Mat image = generateTestImageGrayscale_(); // initialize once
return image;
}

} // namespace
15 changes: 15 additions & 0 deletions modules/imgcodecs/test/test_common.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// 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

#ifndef OPENCV_TEST_IMGCODECS_COMMON_HPP
#define OPENCV_TEST_IMGCODECS_COMMON_HPP

namespace opencv_test {

Mat generateTestImageBGR();
Mat generateTestImageGrayscale();

} // namespace

#endif // OPENCV_TEST_IMGCODECS_COMMON_HPP
Loading

0 comments on commit cf2a3c8

Please sign in to comment.