st/vdpau: correct null check
[mesa.git] / src / gallium / state_trackers / vdpau / surface.c
1 /**************************************************************************
2 *
3 * Copyright 2010 Thomas Balling Sørensen.
4 * Copyright 2011 Christian König.
5 * All Rights Reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the
9 * "Software"), to deal in the Software without restriction, including
10 * without limitation the rights to use, copy, modify, merge, publish,
11 * distribute, sub license, and/or sell copies of the Software, and to
12 * permit persons to whom the Software is furnished to do so, subject to
13 * the following conditions:
14 *
15 * The above copyright notice and this permission notice (including the
16 * next paragraph) shall be included in all copies or substantial portions
17 * of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
22 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
23 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
24 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
25 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 *
27 **************************************************************************/
28
29 #include <assert.h>
30
31 #include "pipe/p_state.h"
32
33 #include "util/u_memory.h"
34 #include "util/u_debug.h"
35 #include "util/u_rect.h"
36 #include "util/u_surface.h"
37 #include "util/u_video.h"
38 #include "vl/vl_defines.h"
39
40 #include "state_tracker/drm_driver.h"
41
42 #include "vdpau_private.h"
43
44 enum getbits_conversion {
45 CONVERSION_NONE,
46 CONVERSION_NV12_TO_YV12,
47 CONVERSION_YV12_TO_NV12,
48 CONVERSION_SWAP_YUYV_UYVY,
49 };
50
51 /**
52 * Create a VdpVideoSurface.
53 */
54 VdpStatus
55 vlVdpVideoSurfaceCreate(VdpDevice device, VdpChromaType chroma_type,
56 uint32_t width, uint32_t height,
57 VdpVideoSurface *surface)
58 {
59 struct pipe_context *pipe;
60 vlVdpSurface *p_surf;
61 VdpStatus ret;
62
63 if (!(width && height)) {
64 ret = VDP_STATUS_INVALID_SIZE;
65 goto inv_size;
66 }
67
68 p_surf = CALLOC(1, sizeof(vlVdpSurface));
69 if (!p_surf) {
70 ret = VDP_STATUS_RESOURCES;
71 goto no_res;
72 }
73
74 vlVdpDevice *dev = vlGetDataHTAB(device);
75 if (!dev) {
76 ret = VDP_STATUS_INVALID_HANDLE;
77 goto inv_device;
78 }
79
80 DeviceReference(&p_surf->device, dev);
81 pipe = dev->context;
82
83 pipe_mutex_lock(dev->mutex);
84 memset(&p_surf->templat, 0, sizeof(p_surf->templat));
85 p_surf->templat.buffer_format = pipe->screen->get_video_param
86 (
87 pipe->screen,
88 PIPE_VIDEO_PROFILE_UNKNOWN,
89 PIPE_VIDEO_ENTRYPOINT_BITSTREAM,
90 PIPE_VIDEO_CAP_PREFERED_FORMAT
91 );
92 p_surf->templat.chroma_format = ChromaToPipe(chroma_type);
93 p_surf->templat.width = width;
94 p_surf->templat.height = height;
95 p_surf->templat.interlaced = pipe->screen->get_video_param
96 (
97 pipe->screen,
98 PIPE_VIDEO_PROFILE_UNKNOWN,
99 PIPE_VIDEO_ENTRYPOINT_BITSTREAM,
100 PIPE_VIDEO_CAP_PREFERS_INTERLACED
101 );
102 if (p_surf->templat.buffer_format != PIPE_FORMAT_NONE)
103 p_surf->video_buffer = pipe->create_video_buffer(pipe, &p_surf->templat);
104
105 /* do not mandate early allocation of a video buffer */
106 vlVdpVideoSurfaceClear(p_surf);
107 pipe_mutex_unlock(dev->mutex);
108
109 *surface = vlAddDataHTAB(p_surf);
110 if (*surface == 0) {
111 ret = VDP_STATUS_ERROR;
112 goto no_handle;
113 }
114
115 return VDP_STATUS_OK;
116
117 no_handle:
118 p_surf->video_buffer->destroy(p_surf->video_buffer);
119
120 inv_device:
121 DeviceReference(&p_surf->device, NULL);
122 FREE(p_surf);
123
124 no_res:
125 inv_size:
126 return ret;
127 }
128
129 /**
130 * Destroy a VdpVideoSurface.
131 */
132 VdpStatus
133 vlVdpVideoSurfaceDestroy(VdpVideoSurface surface)
134 {
135 vlVdpSurface *p_surf;
136
137 p_surf = (vlVdpSurface *)vlGetDataHTAB((vlHandle)surface);
138 if (!p_surf)
139 return VDP_STATUS_INVALID_HANDLE;
140
141 pipe_mutex_lock(p_surf->device->mutex);
142 if (p_surf->video_buffer)
143 p_surf->video_buffer->destroy(p_surf->video_buffer);
144 pipe_mutex_unlock(p_surf->device->mutex);
145
146 vlRemoveDataHTAB(surface);
147 DeviceReference(&p_surf->device, NULL);
148 FREE(p_surf);
149
150 return VDP_STATUS_OK;
151 }
152
153 /**
154 * Retrieve the parameters used to create a VdpVideoSurface.
155 */
156 VdpStatus
157 vlVdpVideoSurfaceGetParameters(VdpVideoSurface surface,
158 VdpChromaType *chroma_type,
159 uint32_t *width, uint32_t *height)
160 {
161 if (!(width && height && chroma_type))
162 return VDP_STATUS_INVALID_POINTER;
163
164 vlVdpSurface *p_surf = vlGetDataHTAB(surface);
165 if (!p_surf)
166 return VDP_STATUS_INVALID_HANDLE;
167
168 if (p_surf->video_buffer) {
169 *width = p_surf->video_buffer->width;
170 *height = p_surf->video_buffer->height;
171 *chroma_type = PipeToChroma(p_surf->video_buffer->chroma_format);
172 } else {
173 *width = p_surf->templat.width;
174 *height = p_surf->templat.height;
175 *chroma_type = PipeToChroma(p_surf->templat.chroma_format);
176 }
177
178 return VDP_STATUS_OK;
179 }
180
181 static void
182 vlVdpVideoSurfaceSize(vlVdpSurface *p_surf, int component,
183 unsigned *width, unsigned *height)
184 {
185 *width = p_surf->templat.width;
186 *height = p_surf->templat.height;
187
188 vl_video_buffer_adjust_size(width, height, component,
189 p_surf->templat.chroma_format,
190 p_surf->templat.interlaced);
191 }
192
193 /**
194 * Copy image data from a VdpVideoSurface to application memory in a specified
195 * YCbCr format.
196 */
197 VdpStatus
198 vlVdpVideoSurfaceGetBitsYCbCr(VdpVideoSurface surface,
199 VdpYCbCrFormat destination_ycbcr_format,
200 void *const *destination_data,
201 uint32_t const *destination_pitches)
202 {
203 vlVdpSurface *vlsurface;
204 struct pipe_context *pipe;
205 enum pipe_format format, buffer_format;
206 struct pipe_sampler_view **sampler_views;
207 enum getbits_conversion conversion = CONVERSION_NONE;
208 unsigned i, j;
209
210 vlsurface = vlGetDataHTAB(surface);
211 if (!vlsurface)
212 return VDP_STATUS_INVALID_HANDLE;
213
214 pipe = vlsurface->device->context;
215 if (!pipe)
216 return VDP_STATUS_INVALID_HANDLE;
217
218 format = FormatYCBCRToPipe(destination_ycbcr_format);
219 if (format == PIPE_FORMAT_NONE)
220 return VDP_STATUS_INVALID_Y_CB_CR_FORMAT;
221
222 if (vlsurface->video_buffer == NULL)
223 return VDP_STATUS_INVALID_VALUE;
224
225 buffer_format = vlsurface->video_buffer->buffer_format;
226 if (format != buffer_format) {
227 if (format == PIPE_FORMAT_YV12 && buffer_format == PIPE_FORMAT_NV12)
228 conversion = CONVERSION_NV12_TO_YV12;
229 else if (format == PIPE_FORMAT_NV12 && buffer_format == PIPE_FORMAT_YV12)
230 conversion = CONVERSION_YV12_TO_NV12;
231 else if ((format == PIPE_FORMAT_YUYV && buffer_format == PIPE_FORMAT_UYVY) ||
232 (format == PIPE_FORMAT_UYVY && buffer_format == PIPE_FORMAT_YUYV))
233 conversion = CONVERSION_SWAP_YUYV_UYVY;
234 else
235 return VDP_STATUS_NO_IMPLEMENTATION;
236 }
237
238 pipe_mutex_lock(vlsurface->device->mutex);
239 sampler_views = vlsurface->video_buffer->get_sampler_view_planes(vlsurface->video_buffer);
240 if (!sampler_views) {
241 pipe_mutex_unlock(vlsurface->device->mutex);
242 return VDP_STATUS_RESOURCES;
243 }
244
245 for (i = 0; i < 3; ++i) {
246 unsigned width, height;
247 struct pipe_sampler_view *sv = sampler_views[i];
248 if (!sv) continue;
249
250 vlVdpVideoSurfaceSize(vlsurface, i, &width, &height);
251
252 for (j = 0; j < sv->texture->array_size; ++j) {
253 struct pipe_box box = {
254 0, 0, j,
255 width, height, 1
256 };
257 struct pipe_transfer *transfer;
258 uint8_t *map;
259
260 map = pipe->transfer_map(pipe, sv->texture, 0,
261 PIPE_TRANSFER_READ, &box, &transfer);
262 if (!map) {
263 pipe_mutex_unlock(vlsurface->device->mutex);
264 return VDP_STATUS_RESOURCES;
265 }
266
267 if (conversion == CONVERSION_NV12_TO_YV12 && i == 1) {
268 u_copy_nv12_to_yv12(destination_data, destination_pitches,
269 i, j, transfer->stride, sv->texture->array_size,
270 map, box.width, box.height);
271 } else if (conversion == CONVERSION_YV12_TO_NV12 && i > 0) {
272 u_copy_yv12_to_nv12(destination_data, destination_pitches,
273 i, j, transfer->stride, sv->texture->array_size,
274 map, box.width, box.height);
275 } else if (conversion == CONVERSION_SWAP_YUYV_UYVY) {
276 u_copy_swap422_packed(destination_data, destination_pitches,
277 i, j, transfer->stride, sv->texture->array_size,
278 map, box.width, box.height);
279 } else {
280 util_copy_rect(destination_data[i] + destination_pitches[i] * j, sv->texture->format,
281 destination_pitches[i] * sv->texture->array_size, 0, 0,
282 box.width, box.height, map, transfer->stride, 0, 0);
283 }
284
285 pipe_transfer_unmap(pipe, transfer);
286 }
287 }
288 pipe_mutex_unlock(vlsurface->device->mutex);
289
290 return VDP_STATUS_OK;
291 }
292
293 /**
294 * Copy image data from application memory in a specific YCbCr format to
295 * a VdpVideoSurface.
296 */
297 VdpStatus
298 vlVdpVideoSurfacePutBitsYCbCr(VdpVideoSurface surface,
299 VdpYCbCrFormat source_ycbcr_format,
300 void const *const *source_data,
301 uint32_t const *source_pitches)
302 {
303 enum pipe_format pformat = FormatYCBCRToPipe(source_ycbcr_format);
304 struct pipe_context *pipe;
305 struct pipe_sampler_view **sampler_views;
306 unsigned i, j;
307
308 vlVdpSurface *p_surf = vlGetDataHTAB(surface);
309 if (!p_surf)
310 return VDP_STATUS_INVALID_HANDLE;
311
312 pipe = p_surf->device->context;
313 if (!pipe)
314 return VDP_STATUS_INVALID_HANDLE;
315
316 pipe_mutex_lock(p_surf->device->mutex);
317 if (p_surf->video_buffer == NULL || pformat != p_surf->video_buffer->buffer_format) {
318
319 /* destroy the old one */
320 if (p_surf->video_buffer)
321 p_surf->video_buffer->destroy(p_surf->video_buffer);
322
323 /* adjust the template parameters */
324 p_surf->templat.buffer_format = pformat;
325
326 /* and try to create the video buffer with the new format */
327 p_surf->video_buffer = pipe->create_video_buffer(pipe, &p_surf->templat);
328
329 /* stil no luck? ok forget it we don't support it */
330 if (!p_surf->video_buffer) {
331 pipe_mutex_unlock(p_surf->device->mutex);
332 return VDP_STATUS_NO_IMPLEMENTATION;
333 }
334 vlVdpVideoSurfaceClear(p_surf);
335 }
336
337 sampler_views = p_surf->video_buffer->get_sampler_view_planes(p_surf->video_buffer);
338 if (!sampler_views) {
339 pipe_mutex_unlock(p_surf->device->mutex);
340 return VDP_STATUS_RESOURCES;
341 }
342
343 for (i = 0; i < 3; ++i) {
344 unsigned width, height;
345 struct pipe_sampler_view *sv = sampler_views[i];
346 if (!sv || !source_pitches[i]) continue;
347
348 vlVdpVideoSurfaceSize(p_surf, i, &width, &height);
349
350 for (j = 0; j < sv->texture->array_size; ++j) {
351 struct pipe_box dst_box = {
352 0, 0, j,
353 width, height, 1
354 };
355
356 pipe->transfer_inline_write(pipe, sv->texture, 0,
357 PIPE_TRANSFER_WRITE, &dst_box,
358 source_data[i] + source_pitches[i] * j,
359 source_pitches[i] * sv->texture->array_size,
360 0);
361 }
362 }
363 pipe_mutex_unlock(p_surf->device->mutex);
364
365 return VDP_STATUS_OK;
366 }
367
368 /**
369 * Helper function to initially clear the VideoSurface after (re-)creation
370 */
371 void
372 vlVdpVideoSurfaceClear(vlVdpSurface *vlsurf)
373 {
374 struct pipe_context *pipe = vlsurf->device->context;
375 struct pipe_surface **surfaces;
376 unsigned i;
377
378 if (!vlsurf->video_buffer)
379 return;
380
381 surfaces = vlsurf->video_buffer->get_surfaces(vlsurf->video_buffer);
382 for (i = 0; i < VL_MAX_SURFACES; ++i) {
383 union pipe_color_union c = {};
384
385 if (!surfaces[i])
386 continue;
387
388 if (i > !!vlsurf->templat.interlaced)
389 c.f[0] = c.f[1] = c.f[2] = c.f[3] = 0.5f;
390
391 pipe->clear_render_target(pipe, surfaces[i], &c, 0, 0,
392 surfaces[i]->width, surfaces[i]->height);
393 }
394 pipe->flush(pipe, NULL, 0);
395 }
396
397 /**
398 * Interop to mesa state tracker
399 */
400 struct pipe_video_buffer *vlVdpVideoSurfaceGallium(VdpVideoSurface surface)
401 {
402 vlVdpSurface *p_surf = vlGetDataHTAB(surface);
403 if (!p_surf)
404 return NULL;
405
406 pipe_mutex_lock(p_surf->device->mutex);
407 if (p_surf->video_buffer == NULL) {
408 struct pipe_context *pipe = p_surf->device->context;
409
410 /* try to create a video buffer if we don't already have one */
411 p_surf->video_buffer = pipe->create_video_buffer(pipe, &p_surf->templat);
412 }
413 pipe_mutex_unlock(p_surf->device->mutex);
414
415 return p_surf->video_buffer;
416 }
417
418 VdpStatus vlVdpVideoSurfaceDMABuf(VdpVideoSurface surface,
419 VdpVideoSurfacePlane plane,
420 struct VdpSurfaceDMABufDesc *result)
421 {
422 vlVdpSurface *p_surf = vlGetDataHTAB(surface);
423
424 struct pipe_screen *pscreen;
425 struct winsys_handle whandle;
426
427 struct pipe_surface *surf;
428
429 if (!p_surf)
430 return VDP_STATUS_INVALID_HANDLE;
431
432 if (plane > 3)
433 return VDP_STATUS_INVALID_VALUE;
434
435 if (!result)
436 return VDP_STATUS_INVALID_POINTER;
437
438 memset(result, 0, sizeof(*result));
439 result->handle = -1;
440
441 pipe_mutex_lock(p_surf->device->mutex);
442 if (p_surf->video_buffer == NULL) {
443 struct pipe_context *pipe = p_surf->device->context;
444
445 /* try to create a video buffer if we don't already have one */
446 p_surf->video_buffer = pipe->create_video_buffer(pipe, &p_surf->templat);
447 }
448
449 /* Check if surface match interop requirements */
450 if (p_surf->video_buffer == NULL || !p_surf->video_buffer->interlaced ||
451 p_surf->video_buffer->buffer_format != PIPE_FORMAT_NV12) {
452 pipe_mutex_unlock(p_surf->device->mutex);
453 return VDP_STATUS_NO_IMPLEMENTATION;
454 }
455
456 surf = p_surf->video_buffer->get_surfaces(p_surf->video_buffer)[plane];
457 pipe_mutex_unlock(p_surf->device->mutex);
458
459 if (!surf)
460 return VDP_STATUS_RESOURCES;
461
462 memset(&whandle, 0, sizeof(struct winsys_handle));
463 whandle.type = DRM_API_HANDLE_TYPE_FD;
464 whandle.layer = surf->u.tex.first_layer;
465
466 pscreen = surf->texture->screen;
467 if (!pscreen->resource_get_handle(pscreen, surf->texture, &whandle,
468 PIPE_HANDLE_USAGE_READ_WRITE))
469 return VDP_STATUS_NO_IMPLEMENTATION;
470
471 result->handle = whandle.handle;
472 result->width = surf->width;
473 result->height = surf->height;
474 result->offset = whandle.offset;
475 result->stride = whandle.stride;
476
477 if (surf->format == PIPE_FORMAT_R8_UNORM)
478 result->format = VDP_RGBA_FORMAT_R8;
479 else
480 result->format = VDP_RGBA_FORMAT_R8G8;
481
482 return VDP_STATUS_OK;
483 }