From 14e4ee9d9ae4b97b0be9d556ab1115e03d834036 Mon Sep 17 00:00:00 2001 From: "Mrozek, Michal" Date: Tue, 4 Sep 2018 11:32:56 +0200 Subject: [PATCH] Do not use exceptions for input validation. Change-Id: I8a4ad230acc5360326a8655513b79153246f1eeb --- runtime/sharings/d3d/d3d_surface.cpp | 76 ++++++++++++++------------- unit_tests/d3d_sharing/d3d9_tests.cpp | 7 +++ 2 files changed, 46 insertions(+), 37 deletions(-) diff --git a/runtime/sharings/d3d/d3d_surface.cpp b/runtime/sharings/d3d/d3d_surface.cpp index 16bd3c1fc5..07c588ba63 100644 --- a/runtime/sharings/d3d/d3d_surface.cpp +++ b/runtime/sharings/d3d/d3d_surface.cpp @@ -212,48 +212,50 @@ const std::map D3DSurface::D3DFMTCLImage cl_int D3DSurface::findImgFormat(D3DFORMAT d3dFormat, cl_image_format &imgFormat, cl_uint plane, OCLPlane &oclPlane) { oclPlane = OCLPlane::NO_PLANE; static const cl_image_format unknown_format = {0, 0}; - try { - imgFormat = D3DFMTCLImageFormat.at(d3dFormat); - switch (d3dFormat) { - case static_cast(MAKEFOURCC('N', 'V', '1', '2')): - switch (plane) { - case 0: - imgFormat.image_channel_order = CL_R; - oclPlane = OCLPlane::PLANE_Y; - return CL_SUCCESS; - case 1: - imgFormat.image_channel_order = CL_RG; - oclPlane = OCLPlane::PLANE_UV; - return CL_SUCCESS; - default: - imgFormat = unknown_format; - return CL_INVALID_VALUE; - } - case static_cast(MAKEFOURCC('Y', 'V', '1', '2')): - switch (plane) { - case 0: - oclPlane = OCLPlane::PLANE_Y; - return CL_SUCCESS; - - case 1: - oclPlane = OCLPlane::PLANE_V; - return CL_SUCCESS; - - case 2: - oclPlane = OCLPlane::PLANE_U; - return CL_SUCCESS; - - default: - imgFormat = unknown_format; - return CL_INVALID_VALUE; - } - } - } catch (const std::out_of_range &) { + auto element = D3DFMTCLImageFormat.find(d3dFormat); + if (element == D3DFMTCLImageFormat.end()) { imgFormat = unknown_format; return CL_INVALID_IMAGE_FORMAT_DESCRIPTOR; } + imgFormat = element->second; + switch (d3dFormat) { + case static_cast(MAKEFOURCC('N', 'V', '1', '2')): + switch (plane) { + case 0: + imgFormat.image_channel_order = CL_R; + oclPlane = OCLPlane::PLANE_Y; + return CL_SUCCESS; + case 1: + imgFormat.image_channel_order = CL_RG; + oclPlane = OCLPlane::PLANE_UV; + return CL_SUCCESS; + default: + imgFormat = unknown_format; + return CL_INVALID_VALUE; + } + + case static_cast(MAKEFOURCC('Y', 'V', '1', '2')): + switch (plane) { + case 0: + oclPlane = OCLPlane::PLANE_Y; + return CL_SUCCESS; + + case 1: + oclPlane = OCLPlane::PLANE_V; + return CL_SUCCESS; + + case 2: + oclPlane = OCLPlane::PLANE_U; + return CL_SUCCESS; + + default: + imgFormat = unknown_format; + return CL_INVALID_VALUE; + } + } + if (plane > 0) { return CL_INVALID_VALUE; } diff --git a/unit_tests/d3d_sharing/d3d9_tests.cpp b/unit_tests/d3d_sharing/d3d9_tests.cpp index 73ebf3c251..41d62175f5 100644 --- a/unit_tests/d3d_sharing/d3d9_tests.cpp +++ b/unit_tests/d3d_sharing/d3d9_tests.cpp @@ -202,6 +202,13 @@ TEST_F(D3D9Tests, createSurface) { clReleaseMemObject(memObj); } +TEST(D3D9SimpleTests, givenWrongFormatWhenFindIsCalledThenErrorIsReturned) { + cl_image_format expectedImgFormat = {}; + OCLPlane oclPlane = OCLPlane::NO_PLANE; + auto status = D3DSurface::findImgFormat(D3DFMT_FORCE_DWORD, expectedImgFormat, 0, oclPlane); + EXPECT_EQ(CL_INVALID_IMAGE_FORMAT_DESCRIPTOR, status); +} + TEST_F(D3D9Tests, createSurfaceIntel) { cl_int retVal; cl_image_format expectedImgFormat = {};