Debian Codegraph @

opencl/source/sharings/va/va_surface.cpp

raw
1 /*
2 * Copyright (C) 2018-2024 Intel Corporation
3 *
4 * SPDX-License-Identifier: MIT
5 *
6 */
7
8 #include "opencl/source/sharings/va/va_surface.h"
9
10 #include "shared/source/helpers/get_info.h"
11 #include "shared/source/helpers/hw_info.h"
12 #include "shared/source/memory_manager/allocation_properties.h"
13 #include "shared/source/memory_manager/memory_manager.h"
14 #include "shared/source/os_interface/linux/i915.h"
15
16 #include "opencl/source/cl_device/cl_device.h"
17 #include "opencl/source/context/context.h"
18 #include "opencl/source/mem_obj/image.h"
19
20 #include "drm_fourcc.h"
21
22 #include <va/va_drmcommon.h>
23
24 namespace NEO {
25
26 bool VASurface::isSupportedPlanarFormat(uint32_t imageFourcc) {
27 switch (imageFourcc) {
28 case VA_FOURCC_P010:
29 case VA_FOURCC_P016:
30 case VA_FOURCC_RGBP:
31 case VA_FOURCC_NV12:
32 return true;
33 default:
34 return false;
35 }
36 }
37
38 bool VASurface::isSupportedPackedFormat(uint32_t imageFourcc) {
39 switch (imageFourcc) {
40 case VA_FOURCC_YUY2:
41 return true;
42 case VA_FOURCC_Y210:
43 return true;
44 case VA_FOURCC_ARGB:
45 return true;
46 default:
47 return false;
48 }
49 }
50
51 VAStatus VASurface::getSurfaceDescription(SharedSurfaceInfo &surfaceInfo, VASharingFunctions *sharingFunctions, VASurfaceID *surface) {
52 VADRMPRIMESurfaceDescriptor vaDrmPrimeSurfaceDesc = {};
53 auto vaStatus = sharingFunctions->exportSurfaceHandle(*surface,
54 VA_SURFACE_ATTRIB_MEM_TYPE_DRM_PRIME_2,
55 VA_EXPORT_SURFACE_READ_WRITE | VA_EXPORT_SURFACE_SEPARATE_LAYERS,
56 &vaDrmPrimeSurfaceDesc);
57 if (VA_STATUS_SUCCESS == vaStatus) {
58 surfaceInfo.imageId = VA_INVALID_ID;
59 surfaceInfo.imgInfo.imgDesc.imageWidth = vaDrmPrimeSurfaceDesc.width;
60 surfaceInfo.imgInfo.imgDesc.imageHeight = vaDrmPrimeSurfaceDesc.height;
61 surfaceInfo.imageFourcc = vaDrmPrimeSurfaceDesc.fourcc;
62 surfaceInfo.imgInfo.linearStorage = DRM_FORMAT_MOD_LINEAR == vaDrmPrimeSurfaceDesc.objects[0].drm_format_modifier;
63 surfaceInfo.sharedHandle = vaDrmPrimeSurfaceDesc.objects[0].fd;
64
65 if (surfaceInfo.plane != 0 && !VASurface::isSupportedPlanarFormat(surfaceInfo.imageFourcc)) {
66 return VA_STATUS_ERROR_INVALID_PARAMETER;
67 }
68
69 surfaceInfo.imagePitch = vaDrmPrimeSurfaceDesc.layers[0].pitch[0];
70 surfaceInfo.imageOffset = vaDrmPrimeSurfaceDesc.layers[0].offset[0];
71
72 if (surfaceInfo.plane == 1) {
73 surfaceInfo.imageOffset = vaDrmPrimeSurfaceDesc.layers[1].offset[0];
74 surfaceInfo.imagePitch = vaDrmPrimeSurfaceDesc.layers[1].pitch[0];
75 } else if (surfaceInfo.plane == 2) {
76 surfaceInfo.imageOffset = vaDrmPrimeSurfaceDesc.layers[2].offset[0];
77 surfaceInfo.imagePitch = vaDrmPrimeSurfaceDesc.layers[2].pitch[0];
78 }
79 } else {
80 VAImage vaImage = {};
81 vaStatus = sharingFunctions->deriveImage(*surface, &vaImage);
82
83 if (vaStatus != VA_STATUS_SUCCESS) {
84 return vaStatus;
85 }
86
87 surfaceInfo.imageId = vaImage.image_id;
88 surfaceInfo.imgInfo.imgDesc.imageWidth = vaImage.width;
89 surfaceInfo.imgInfo.imgDesc.imageHeight = vaImage.height;
90 surfaceInfo.imageFourcc = vaImage.format.fourcc;
91 surfaceInfo.imgInfo.linearStorage = false;
92
93 if (surfaceInfo.plane != 0 && !VASurface::isSupportedPlanarFormat(surfaceInfo.imageFourcc)) {
94 return VA_STATUS_ERROR_INVALID_PARAMETER;
95 }
96
97 if (surfaceInfo.plane == 1) {
98 surfaceInfo.imageOffset = vaImage.offsets[1];
99 surfaceInfo.imagePitch = vaImage.pitches[0];
100 } else if (surfaceInfo.plane == 2) {
101 surfaceInfo.imageOffset = vaImage.offsets[2];
102 surfaceInfo.imagePitch = vaImage.pitches[0];
103 }
104
105 vaStatus = sharingFunctions->extGetSurfaceHandle(surface, &surfaceInfo.sharedHandle);
106 }
107
108 return vaStatus;
109 }
110
111 void VASurface::applyPlanarOptions(SharedSurfaceInfo &sharedSurfaceInfo, cl_uint plane, cl_mem_flags flags, bool supportOcl21) {
112 bool isRGBPFormat = debugManager.flags.EnableExtendedVaFormats.get() && sharedSurfaceInfo.imageFourcc == VA_FOURCC_RGBP;
113
114 if (plane == 0) {
115 sharedSurfaceInfo.imgInfo.plane = GMM_PLANE_Y;
116 sharedSurfaceInfo.channelOrder = CL_R;
117 } else if (plane == 1) {
118 sharedSurfaceInfo.imgInfo.plane = GMM_PLANE_U;
119 sharedSurfaceInfo.channelOrder = isRGBPFormat ? CL_R : CL_RG;
120 } else if (plane == 2) {
121 UNRECOVERABLE_IF(!isRGBPFormat);
122 sharedSurfaceInfo.imgInfo.plane = GMM_PLANE_V;
123 sharedSurfaceInfo.channelOrder = CL_R;
124 } else {
125 UNRECOVERABLE_IF(true);
126 }
127
128 auto gmmSurfaceFormat = Image::getSurfaceFormatFromTable(flags, &sharedSurfaceInfo.gmmImgFormat, supportOcl21); // vaImage.format.fourcc == VA_FOURCC_NV12
129
130 if (debugManager.flags.EnableExtendedVaFormats.get() && sharedSurfaceInfo.imageFourcc == VA_FOURCC_RGBP) {
131 sharedSurfaceInfo.channelType = CL_UNORM_INT8;
132 gmmSurfaceFormat = VASurface::getExtendedSurfaceFormatInfo(sharedSurfaceInfo.imageFourcc);
133 } else if (sharedSurfaceInfo.imageFourcc == VA_FOURCC_P010 || sharedSurfaceInfo.imageFourcc == VA_FOURCC_P016) {
134 sharedSurfaceInfo.channelType = CL_UNORM_INT16;
135 gmmSurfaceFormat = VASurface::getExtendedSurfaceFormatInfo(sharedSurfaceInfo.imageFourcc);
136 }
137 sharedSurfaceInfo.imgInfo.surfaceFormat = &gmmSurfaceFormat->surfaceFormat;
138 }
139
140 void VASurface::applyPlaneSettings(SharedSurfaceInfo &sharedSurfaceInfo, cl_uint plane) {
141 bool isRGBPFormat = debugManager.flags.EnableExtendedVaFormats.get() && sharedSurfaceInfo.imageFourcc == VA_FOURCC_RGBP;
142
143 sharedSurfaceInfo.imgInfo.slicePitch = 0u;
144 sharedSurfaceInfo.imgInfo.yOffset = 0;
145 sharedSurfaceInfo.imgInfo.xOffset = 0;
146 if (plane == 1) {
147 if (!isRGBPFormat) {
148 sharedSurfaceInfo.imgInfo.imgDesc.imageWidth /= 2;
149 sharedSurfaceInfo.imgInfo.imgDesc.imageHeight /= 2;
150 }
151 sharedSurfaceInfo.imgInfo.offset = sharedSurfaceInfo.imageOffset;
152 sharedSurfaceInfo.imgInfo.yOffsetForUVPlane = static_cast<uint32_t>(sharedSurfaceInfo.imageOffset / sharedSurfaceInfo.imagePitch);
153 }
154 if (isRGBPFormat && plane == 2) {
155 sharedSurfaceInfo.imgInfo.offset = sharedSurfaceInfo.imageOffset;
156 }
157 }
158
159 void VASurface::applyPackedOptions(SharedSurfaceInfo &sharedSurfaceInfo) {
160 if (sharedSurfaceInfo.imageFourcc == VA_FOURCC_Y210) {
161 sharedSurfaceInfo.channelType = CL_UNORM_INT16;
162 sharedSurfaceInfo.channelOrder = CL_RGBA;
163 } else if (sharedSurfaceInfo.imageFourcc == VA_FOURCC_ARGB) {
164 sharedSurfaceInfo.channelType = CL_UNORM_INT8;
165 sharedSurfaceInfo.channelOrder = CL_RGBA;
166 } else {
167 sharedSurfaceInfo.channelType = CL_UNORM_INT8;
168 sharedSurfaceInfo.channelOrder = CL_YUYV_INTEL;
169 }
170 sharedSurfaceInfo.imgInfo.surfaceFormat = &VASurface::getExtendedSurfaceFormatInfo(sharedSurfaceInfo.imageFourcc)->surfaceFormat;
171 }
172
173 Image *VASurface::createSharedVaSurface(Context *context, VASharingFunctions *sharingFunctions,
174 cl_mem_flags flags, cl_mem_flags_intel flagsIntel, VASurfaceID *surface,
175 cl_uint plane, cl_int *errcodeRet) {
176 ErrorCodeHelper errorCode(errcodeRet, CL_SUCCESS);
177
178 auto memoryManager = context->getMemoryManager();
179 McsSurfaceInfo mcsSurfaceInfo = {};
180
181 SharedSurfaceInfo sharedSurfaceInfo{};
182 sharedSurfaceInfo.plane = plane;
183
184 std::unique_lock<std::mutex> lock(sharingFunctions->mutex);
185
186 auto result = getSurfaceDescription(sharedSurfaceInfo, sharingFunctions, surface);
187
188 if (result != VA_STATUS_SUCCESS) {
189 *errcodeRet = static_cast<cl_int>(result);
190 return nullptr;
191 }
192
193 sharedSurfaceInfo.imgInfo.imgDesc.imageType = ImageType::image2D;
194
195 bool supportOcl21 = context->getDevice(0)->getHardwareInfo().capabilityTable.supportsOcl21Features;
196
197 if (VASurface::isSupportedPlanarFormat(sharedSurfaceInfo.imageFourcc)) {
198 applyPlanarOptions(sharedSurfaceInfo, plane, flags, supportOcl21);
199 } else if (VASurface::isSupportedPackedFormat(sharedSurfaceInfo.imageFourcc)) {
200 applyPackedOptions(sharedSurfaceInfo);
201 } else {
202 *errcodeRet = VA_STATUS_ERROR_INVALID_PARAMETER;
203 return nullptr;
204 }
205
206 AllocationProperties properties(context->getDevice(0)->getRootDeviceIndex(),
207 false, // allocateMemory
208 &sharedSurfaceInfo.imgInfo, AllocationType::sharedImage,
209 context->getDeviceBitfieldForAllocation(context->getDevice(0)->getRootDeviceIndex()));
210
211 MemoryManager::OsHandleData osHandleData{sharedSurfaceInfo.sharedHandle};
212 auto alloc = memoryManager->createGraphicsAllocationFromSharedHandle(osHandleData, properties, false, false, true, nullptr);
213
214 memoryManager->closeSharedHandle(alloc);
215
216 if (VASurface::isSupportedPlanarFormat(sharedSurfaceInfo.imageFourcc)) {
217 applyPlaneSettings(sharedSurfaceInfo, plane);
218 }
219
220 if (VA_INVALID_ID != sharedSurfaceInfo.imageId) {
221 sharingFunctions->destroyImage(sharedSurfaceInfo.imageId);
222 }
223
224 lock.unlock();
225
226 cl_image_format imgFormat = {sharedSurfaceInfo.channelOrder, sharedSurfaceInfo.channelType};
227 auto imgSurfaceFormat = Image::getSurfaceFormatFromTable(flags, &imgFormat, supportOcl21);
228 sharedSurfaceInfo.imgInfo.surfaceFormat = &imgSurfaceFormat->surfaceFormat;
229 sharedSurfaceInfo.imgInfo.imgDesc.imageRowPitch = sharedSurfaceInfo.imgInfo.rowPitch;
230
231 auto vaSurface = new VASurface(sharingFunctions, sharedSurfaceInfo.imageId, plane, surface, context->getInteropUserSyncEnabled());
232 auto multiGraphicsAllocation = MultiGraphicsAllocation(context->getDevice(0)->getRootDeviceIndex());
233 multiGraphicsAllocation.addAllocation(alloc);
234
235 auto image = Image::createSharedImage(context, vaSurface, mcsSurfaceInfo, std::move(multiGraphicsAllocation), nullptr, flags, flagsIntel, imgSurfaceFormat, sharedSurfaceInfo.imgInfo, __GMM_NO_CUBE_MAP, 0, 0);
236 image->setMediaPlaneType(plane);
237 return image;
238 }
239
240 void VASurface::synchronizeObject(UpdateData &updateData) {
241 updateData.synchronizationStatus = SynchronizeStatus::ACQUIRE_SUCCESFUL;
242 if (!interopUserSync) {
243 if (sharingFunctions->syncSurface(surfaceId) != VA_STATUS_SUCCESS) {
244 updateData.synchronizationStatus = SYNCHRONIZE_ERROR;
245 }
246 }
247 }
248
249 void VASurface::getMemObjectInfo(size_t &paramValueSize, void *&paramValue) {
250 paramValueSize = sizeof(surfaceIdPtr);
251 paramValue = &surfaceIdPtr;
252 }
253
254 bool VASurface::validate(cl_mem_flags flags, cl_uint plane) {
255 switch (flags) {
256 case CL_MEM_READ_ONLY:
257 case CL_MEM_WRITE_ONLY:
258 case CL_MEM_READ_WRITE:
259 case CL_MEM_READ_ONLY | CL_MEM_ACCESS_FLAGS_UNRESTRICTED_INTEL:
260 case CL_MEM_ACCESS_FLAGS_UNRESTRICTED_INTEL:
261 break;
262 default:
263 return false;
264 }
265 if (plane > 1 && !debugManager.flags.EnableExtendedVaFormats.get()) {
266 return false;
267 }
268 return true;
269 }
270
271 const ClSurfaceFormatInfo *VASurface::getExtendedSurfaceFormatInfo(uint32_t formatFourCC) {
272 if (formatFourCC == VA_FOURCC_P010) {
273 static const ClSurfaceFormatInfo formatInfoP010 = {{CL_NV12_INTEL, CL_UNORM_INT16},
274 {GMM_RESOURCE_FORMAT::GMM_FORMAT_P010,
275 static_cast<SurfaceFormat>(NUM_GFX3DSTATE_SURFACEFORMATS), // not used for plane images
276 0,
277 1,
278 2,
279 2}};
280 return &formatInfoP010;
281 }
282 if (formatFourCC == VA_FOURCC_P016) {
283 static const ClSurfaceFormatInfo formatInfoP016 = {{CL_NV12_INTEL, CL_UNORM_INT16},
284 {GMM_RESOURCE_FORMAT::GMM_FORMAT_P016,
285 static_cast<SurfaceFormat>(NUM_GFX3DSTATE_SURFACEFORMATS), // not used for plane images
286 0,
287 1,
288 2,
289 2}};
290 return &formatInfoP016;
291 }
292 if (formatFourCC == VA_FOURCC_RGBP) {
293 static const ClSurfaceFormatInfo formatInfoRGBP = {{CL_NV12_INTEL, CL_UNORM_INT8},
294 {GMM_RESOURCE_FORMAT::GMM_FORMAT_RGBP,
295 static_cast<SurfaceFormat>(GFX3DSTATE_SURFACEFORMAT_R8_UNORM), // not used for plane images
296 0,
297 1,
298 1,
299 1}};
300 return &formatInfoRGBP;
301 }
302 if (formatFourCC == VA_FOURCC_YUY2) {
303 static const ClSurfaceFormatInfo formatInfoYUY2 = {{CL_YUYV_INTEL, CL_UNORM_INT8},
304 {GMM_RESOURCE_FORMAT::GMM_FORMAT_YUY2,
305 static_cast<SurfaceFormat>(GFX3DSTATE_SURFACEFORMAT_YCRCB_NORMAL),
306 0,
307 2,
308 1,
309 2}};
310 return &formatInfoYUY2;
311 }
312 if (formatFourCC == VA_FOURCC_Y210) {
313 static const ClSurfaceFormatInfo formatInfoY210 = {{CL_RGBA, CL_UNORM_INT16},
314 {GMM_RESOURCE_FORMAT::GMM_FORMAT_Y210,
315 static_cast<SurfaceFormat>(GFX3DSTATE_SURFACEFORMAT_R16G16B16A16_UNORM),
316 0,
317 4,
318 2,
319 8}};
320 return &formatInfoY210;
321 }
322
323 if (formatFourCC == VA_FOURCC_ARGB) {
324 static const ClSurfaceFormatInfo formatInfoARGB = {{CL_RGBA, CL_UNORM_INT8},
325 {GMM_RESOURCE_FORMAT::GMM_FORMAT_R8G8B8A8_UNORM_TYPE,
326 static_cast<SurfaceFormat>(GFX3DSTATE_SURFACEFORMAT_R8G8B8A8_UNORM),
327 0,
328 4,
329 1,
330 4}};
331 return &formatInfoARGB;
332 }
333
334 return nullptr;
335 }
336
337 bool VASurface::isSupportedFourCCTwoPlaneFormat(int fourcc) {
338 if ((fourcc == VA_FOURCC_NV12) ||
339 (fourcc == VA_FOURCC_P010) ||
340 (fourcc == VA_FOURCC_P016)) {
341 return true;
342 }
343 return false;
344 }
345
346 bool VASurface::isSupportedFourCCThreePlaneFormat(int fourcc) {
347 if (debugManager.flags.EnableExtendedVaFormats.get() && fourcc == VA_FOURCC_RGBP) {
348 return true;
349 }
350 return false;
351 }
352
353 } // namespace NEO