Skip to content

Commit 8e351a4

Browse files
Cleanup VA sharing implementation
- validate flags and plane - allow plane 0 and 1 only - do not allow full NV12 image creation - remove redundant unit tests Related-To: NEO-3049 Change-Id: I0a2820cdeb9e4b56fe1800490b89c99d05ba7f87 Signed-off-by: Mateusz Hoppe <mateusz.hoppe@intel.com>
1 parent 2f497ad commit 8e351a4

File tree

4 files changed

+56
-75
lines changed

4 files changed

+56
-75
lines changed

runtime/sharings/va/cl_va_api.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,13 @@ clCreateFromVA_APIMediaSurfaceINTEL(cl_context context, cl_mem_flags flags, VASu
4141
if (returnCode != CL_SUCCESS) {
4242
return nullptr;
4343
}
44+
45+
if (!VASurface::validate(flags, plane)) {
46+
returnCode = CL_INVALID_VALUE;
47+
err.set(returnCode);
48+
return nullptr;
49+
}
50+
4451
image = VASurface::createSharedVaSurface(pContext, pContext->getSharing<VASharingFunctions>(), flags, surface, plane, errcodeRet);
4552
DBG_LOG_INPUTS("image", image);
4653
return image;

runtime/sharings/va/va_surface.cpp

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,7 @@ Image *VASurface::createSharedVaSurface(Context *context, VASharingFunctions *sh
5050
imgInfo.plane = GMM_PLANE_U;
5151
imgFormat = {CL_RG, CL_UNORM_INT8};
5252
} else {
53-
imgFormat = {CL_NV12_INTEL, CL_UNORM_INT8};
54-
imgInfo.plane = GMM_NO_PLANE;
53+
UNRECOVERABLE_IF(true);
5554
}
5655

5756
imgSurfaceFormat = Image::getSurfaceFormatFromTable(flags, &imgFormat);
@@ -94,4 +93,18 @@ void VASurface::getMemObjectInfo(size_t &paramValueSize, void *&paramValue) {
9493
paramValue = &surfaceId;
9594
}
9695

96+
bool VASurface::validate(cl_mem_flags flags, cl_uint plane) {
97+
switch (flags) {
98+
case CL_MEM_READ_ONLY:
99+
case CL_MEM_WRITE_ONLY:
100+
case CL_MEM_READ_WRITE:
101+
break;
102+
default:
103+
return false;
104+
}
105+
if (plane > 1) {
106+
return false;
107+
}
108+
return true;
109+
}
97110
} // namespace NEO

runtime/sharings/va/va_surface.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ class VASurface : VASharing {
2323

2424
void getMemObjectInfo(size_t &paramValueSize, void *&paramValue) override;
2525

26+
static bool validate(cl_mem_flags flags, cl_uint plane);
27+
2628
protected:
2729
VASurface(VASharingFunctions *sharingFunctions, VAImageID imageId,
2830
cl_uint plane, VASurfaceID *surfaceId, bool interopUserSync)

unit_tests/sharings/va/va_sharing_tests.cpp

Lines changed: 32 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,18 @@ TEST_F(VaSharingTests, givenMockVaWhenVaSurfaceIsCreatedThenMemObjectHasVaHandle
173173
delete vaSurface;
174174
}
175175

176+
TEST_F(VaSharingTests, givenInvalidPlaneWhenVaSurfaceIsCreatedThenUnrecoverableIsCalled) {
177+
EXPECT_THROW(VASurface::createSharedVaSurface(&context, &vaSharing->sharingFunctions,
178+
CL_MEM_READ_WRITE, &vaSurfaceId, 2, &errCode),
179+
std::exception);
180+
}
181+
182+
TEST_F(VaSharingTests, givenInvalidPlaneInputWhenVaSurfaceIsCreatedThenInvalidValueErrorIsReturned) {
183+
sharedClMem = clCreateFromVA_APIMediaSurfaceINTEL(&context, CL_MEM_READ_WRITE, &vaSurfaceId, 2, &errCode);
184+
EXPECT_EQ(nullptr, sharedClMem);
185+
EXPECT_EQ(CL_INVALID_VALUE, errCode);
186+
}
187+
176188
TEST_F(VaSharingTests, givenMockVaWhenVaSurfaceIsCreatedWithNotAlignedWidthAndHeightThenSurfaceOffsetsUseAlignedValues) {
177189
vaSharing->sharingFunctions.derivedImageWidth = 256 + 16;
178190
vaSharing->sharingFunctions.derivedImageHeight = 512 + 16;
@@ -316,10 +328,10 @@ TEST_F(VaSharingTests, givenVaMediaSurfaceWhenGetImageInfoIsCalledThenPlaneIsRet
316328
}
317329

318330
TEST_F(VaSharingTests, givenPlaneWhenCreateSurfaceIsCalledThenSetPlaneFields) {
319-
cl_uint planes[4] = {0, 1, 2, 3};
331+
cl_uint planes[2] = {0, 1};
320332
updateAcquiredHandle(2);
321333

322-
for (int i = 0; i < 4; i++) {
334+
for (int i = 0; i < 2; i++) {
323335
createMediaSurface(planes[i]);
324336

325337
EXPECT_TRUE(sharedImg->getSurfaceFormatInfo().OCLImageFormat.image_channel_data_type == CL_UNORM_INT8);
@@ -329,8 +341,6 @@ TEST_F(VaSharingTests, givenPlaneWhenCreateSurfaceIsCalledThenSetPlaneFields) {
329341
EXPECT_TRUE(sharedImg->getSurfaceFormatInfo().OCLImageFormat.image_channel_order == CL_R);
330342
} else if (planes[i] == 1) {
331343
EXPECT_TRUE(sharedImg->getSurfaceFormatInfo().OCLImageFormat.image_channel_order == CL_RG);
332-
} else {
333-
EXPECT_TRUE(sharedImg->getSurfaceFormatInfo().OCLImageFormat.image_channel_order == CL_NV12_INTEL);
334344
}
335345

336346
delete sharedImg;
@@ -341,7 +351,7 @@ TEST_F(VaSharingTests, givenPlaneWhenCreateSurfaceIsCalledThenSetPlaneFields) {
341351
TEST_F(VaSharingTests, givenSimpleParamsWhenCreateSurfaceIsCalledThenSetImgObject) {
342352
updateAcquiredHandle(2);
343353

344-
createMediaSurface(3u);
354+
createMediaSurface(0u);
345355

346356
EXPECT_TRUE(sharedImg->getImageDesc().buffer == nullptr);
347357
EXPECT_EQ(0u, sharedImg->getImageDesc().image_array_size);
@@ -382,15 +392,14 @@ TEST_F(VaSharingTests, givenInteropUserSyncContextWhenAcquireIsCalledThenDontSyn
382392
}
383393

384394
TEST_F(VaSharingTests, givenYuvPlaneWhenCreateIsCalledThenChangeWidthAndHeight) {
385-
cl_uint planeTypes[4] = {
395+
cl_uint planeTypes[] = {
386396
0, //Y
387-
1, //U
388-
2, //no-plane
397+
1 //U
389398
};
390399

391400
context.setInteropUserSyncEnabled(true);
392401

393-
for (int i = 0; i < 3; i++) {
402+
for (int i = 0; i < 2; i++) {
394403
createMediaSurface(planeTypes[i]);
395404
size_t retParam;
396405

@@ -414,70 +423,6 @@ TEST_F(VaSharingTests, givenYuvPlaneWhenCreateIsCalledThenChangeWidthAndHeight)
414423
}
415424
}
416425

417-
TEST_F(VaSharingTests, givenVaSurfaceWhenCreateImageFromParentThenShareHandler) {
418-
context.setInteropUserSyncEnabled(true);
419-
420-
createMediaSurface(2u);
421-
EXPECT_TRUE(sharedImg->getSurfaceFormatInfo().OCLImageFormat.image_channel_order == CL_NV12_INTEL);
422-
423-
cl_image_desc imgDesc = {};
424-
imgDesc.image_type = CL_MEM_OBJECT_IMAGE2D;
425-
imgDesc.image_width = 256;
426-
imgDesc.image_height = 256;
427-
imgDesc.mem_object = sharedClMem;
428-
429-
cl_image_format imgFormat = {CL_R, CL_UNORM_INT8};
430-
431-
auto childImg = clCreateImage(&context, CL_MEM_READ_WRITE, &imgFormat, &imgDesc, nullptr, &errCode);
432-
EXPECT_EQ(CL_SUCCESS, errCode);
433-
434-
auto childImgObj = castToObject<Image>(childImg);
435-
EXPECT_FALSE(childImgObj->getIsObjectRedescribed());
436-
437-
EXPECT_EQ(sharedImg->peekSharingHandler(), childImgObj->peekSharingHandler());
438-
439-
errCode = clReleaseMemObject(childImg);
440-
EXPECT_EQ(CL_SUCCESS, errCode);
441-
}
442-
443-
TEST_F(VaSharingTests, givenVaSurfaceWhenCreateImageFromParentThenSetMediaPlaneType) {
444-
context.setInteropUserSyncEnabled(true);
445-
446-
createMediaSurface(2u);
447-
EXPECT_TRUE(sharedImg->getSurfaceFormatInfo().OCLImageFormat.image_channel_order == CL_NV12_INTEL);
448-
449-
cl_image_desc imgDesc = {};
450-
imgDesc.image_type = CL_MEM_OBJECT_IMAGE2D;
451-
imgDesc.image_width = 256;
452-
imgDesc.image_height = 256;
453-
imgDesc.image_depth = 0; // Y plane
454-
imgDesc.mem_object = sharedClMem;
455-
456-
cl_image_format imgFormat = {CL_R, CL_UNORM_INT8};
457-
458-
auto childImg = clCreateImage(&context, CL_MEM_READ_WRITE, &imgFormat, &imgDesc, nullptr, &errCode);
459-
EXPECT_EQ(CL_SUCCESS, errCode);
460-
461-
auto childImgObj = castToObject<Image>(childImg);
462-
463-
EXPECT_EQ(childImgObj->getMediaPlaneType(), 0u);
464-
465-
errCode = clReleaseMemObject(childImg);
466-
EXPECT_EQ(CL_SUCCESS, errCode);
467-
468-
imgDesc.image_depth = 1; // U plane
469-
imgFormat.image_channel_order = CL_RG;
470-
childImg = clCreateImage(&context, CL_MEM_READ_WRITE, &imgFormat, &imgDesc, nullptr, &errCode);
471-
EXPECT_EQ(CL_SUCCESS, errCode);
472-
473-
childImgObj = castToObject<Image>(childImg);
474-
475-
EXPECT_EQ(childImgObj->getMediaPlaneType(), 1u);
476-
477-
errCode = clReleaseMemObject(childImg);
478-
EXPECT_EQ(CL_SUCCESS, errCode);
479-
}
480-
481426
TEST_F(VaSharingTests, givenContextWhenSharingTableEmptyThenReturnsNullptr) {
482427
MockContext context;
483428
context.clearSharingFunctions();
@@ -507,3 +452,17 @@ TEST_F(VaSharingTests, givenInValidPlatformWhenGetDeviceIdsFromVaApiMediaAdapter
507452
EXPECT_EQ(0u, numDevices);
508453
EXPECT_EQ(0u, devices);
509454
}
455+
456+
TEST(VaSurface, givenValidPlaneAndFlagsWhenValidatingInputsThenTrueIsReturned) {
457+
for (cl_uint plane = 0; plane <= 1; plane++) {
458+
EXPECT_TRUE(VASurface::validate(CL_MEM_READ_ONLY, plane));
459+
EXPECT_TRUE(VASurface::validate(CL_MEM_WRITE_ONLY, plane));
460+
EXPECT_TRUE(VASurface::validate(CL_MEM_READ_WRITE, plane));
461+
}
462+
}
463+
464+
TEST(VaSurface, givenInValidPlaneOrFlagsWhenValidatingInputsThenTrueIsReturned) {
465+
cl_uint plane = 2;
466+
EXPECT_FALSE(VASurface::validate(CL_MEM_READ_ONLY, plane));
467+
EXPECT_FALSE(VASurface::validate(CL_MEM_USE_HOST_PTR, 0));
468+
}

0 commit comments

Comments
 (0)