st/vdpau: use vl_video_buffer_adjust_size
[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 "vdpau_private.h"
41
42 enum getbits_conversion {
43 CONVERSION_NONE,
44 CONVERSION_NV12_TO_YV12,
45 CONVERSION_YV12_TO_NV12,
46 CONVERSION_SWAP_YUYV_UYVY,
47 };
48
49 /**
50 * Create a VdpVideoSurface.
51 */
52 VdpStatus
53 vlVdpVideoSurfaceCreate(VdpDevice device, VdpChromaType chroma_type,
54 uint32_t width, uint32_t height,
55 VdpVideoSurface *surface)
56 {
57 struct pipe_context *pipe;
58 vlVdpSurface *p_surf;
59 VdpStatus ret;
60
61 if (!(width && height)) {
62 ret = VDP_STATUS_INVALID_SIZE;
63 goto inv_size;
64 }
65
66 p_surf = CALLOC(1, sizeof(vlVdpSurface));
67 if (!p_surf) {
68 ret = VDP_STATUS_RESOURCES;
69 goto no_res;
70 }
71
72 vlVdpDevice *dev = vlGetDataHTAB(device);
73 if (!dev) {
74 ret = VDP_STATUS_INVALID_HANDLE;
75 goto inv_device;
76 }
77
78 DeviceReference(&p_surf->device, dev);
79 pipe = dev->context;
80
81 pipe_mutex_lock(dev->mutex);
82 memset(&p_surf->templat, 0, sizeof(p_surf->templat));
83 p_surf->templat.buffer_format = pipe->screen->get_video_param
84 (
85 pipe->screen,
86 PIPE_VIDEO_PROFILE_UNKNOWN,
87 PIPE_VIDEO_ENTRYPOINT_BITSTREAM,
88 PIPE_VIDEO_CAP_PREFERED_FORMAT
89 );
90 p_surf->templat.chroma_format = ChromaToPipe(chroma_type);
91 p_surf->templat.width = width;
92 p_surf->templat.height = height;
93 p_surf->templat.interlaced = pipe->screen->get_video_param
94 (
95 pipe->screen,
96 PIPE_VIDEO_PROFILE_UNKNOWN,
97 PIPE_VIDEO_ENTRYPOINT_BITSTREAM,
98 PIPE_VIDEO_CAP_PREFERS_INTERLACED
99 );
100 if (p_surf->templat.buffer_format != PIPE_FORMAT_NONE)
101 p_surf->video_buffer = pipe->create_video_buffer(pipe, &p_surf->templat);
102
103 /* do not mandate early allocation of a video buffer */
104 vlVdpVideoSurfaceClear(p_surf);
105 pipe_mutex_unlock(dev->mutex);
106
107 *surface = vlAddDataHTAB(p_surf);
108 if (*surface == 0) {
109 ret = VDP_STATUS_ERROR;
110 goto no_handle;
111 }
112
113 return VDP_STATUS_OK;
114
115 no_handle:
116 p_surf->video_buffer->destroy(p_surf->video_buffer);
117
118 inv_device:
119 DeviceReference(&p_surf->device, NULL);
120 FREE(p_surf);
121
122 no_res:
123 inv_size:
124 return ret;
125 }
126
127 /**
128 * Destroy a VdpVideoSurface.
129 */
130 VdpStatus
131 vlVdpVideoSurfaceDestroy(VdpVideoSurface surface)
132 {
133 vlVdpSurface *p_surf;
134
135 p_surf = (vlVdpSurface *)vlGetDataHTAB((vlHandle)surface);
136 if (!p_surf)
137 return VDP_STATUS_INVALID_HANDLE;
138
139 pipe_mutex_lock(p_surf->device->mutex);
140 if (p_surf->video_buffer)
141 p_surf->video_buffer->destroy(p_surf->video_buffer);
142 pipe_mutex_unlock(p_surf->device->mutex);
143
144 vlRemoveDataHTAB(surface);
145 DeviceReference(&p_surf->device, NULL);
146 FREE(p_surf);
147
148 return VDP_STATUS_OK;
149 }
150
151 /**
152 * Retrieve the parameters used to create a VdpVideoSurface.
153 */
154 VdpStatus
155 vlVdpVideoSurfaceGetParameters(VdpVideoSurface surface,
156 VdpChromaType *chroma_type,
157 uint32_t *width, uint32_t *height)
158 {
159 if (!(width && height && chroma_type))
160 return VDP_STATUS_INVALID_POINTER;
161
162 vlVdpSurface *p_surf = vlGetDataHTAB(surface);
163 if (!p_surf)
164 return VDP_STATUS_INVALID_HANDLE;
165
166 if (p_surf->video_buffer) {
167 *width = p_surf->video_buffer->width;
168 *height = p_surf->video_buffer->height;
169 *chroma_type = PipeToChroma(p_surf->video_buffer->chroma_format);
170 } else {
171 *width = p_surf->templat.width;
172 *height = p_surf->templat.height;
173 *chroma_type = PipeToChroma(p_surf->templat.chroma_format);
174 }
175
176 return VDP_STATUS_OK;
177 }
178
179 static void
180 vlVdpVideoSurfaceSize(vlVdpSurface *p_surf, int component,
181 unsigned *width, unsigned *height)
182 {
183 *width = p_surf->templat.width;
184 *height = p_surf->templat.height;
185
186 vl_video_buffer_adjust_size(width, height, component,
187 p_surf->templat.chroma_format,
188 p_surf->templat.interlaced);
189 }
190
191 /**
192 * Copy image data from a VdpVideoSurface to application memory in a specified
193 * YCbCr format.
194 */
195 VdpStatus
196 vlVdpVideoSurfaceGetBitsYCbCr(VdpVideoSurface surface,
197 VdpYCbCrFormat destination_ycbcr_format,
198 void *const *destination_data,
199 uint32_t const *destination_pitches)
200 {
201 vlVdpSurface *vlsurface;
202 struct pipe_context *pipe;
203 enum pipe_format format, buffer_format;
204 struct pipe_sampler_view **sampler_views;
205 enum getbits_conversion conversion = CONVERSION_NONE;
206 unsigned i, j;
207
208 vlsurface = vlGetDataHTAB(surface);
209 if (!vlsurface)
210 return VDP_STATUS_INVALID_HANDLE;
211
212 pipe = vlsurface->device->context;
213 if (!pipe)
214 return VDP_STATUS_INVALID_HANDLE;
215
216 format = FormatYCBCRToPipe(destination_ycbcr_format);
217 if (format == PIPE_FORMAT_NONE)
218 return VDP_STATUS_INVALID_Y_CB_CR_FORMAT;
219
220 if (vlsurface->video_buffer == NULL)
221 return VDP_STATUS_INVALID_VALUE;
222
223 buffer_format = vlsurface->video_buffer->buffer_format;
224 if (format != buffer_format) {
225 if (format == PIPE_FORMAT_YV12 && buffer_format == PIPE_FORMAT_NV12)
226 conversion = CONVERSION_NV12_TO_YV12;
227 else if (format == PIPE_FORMAT_NV12 && buffer_format == PIPE_FORMAT_YV12)
228 conversion = CONVERSION_YV12_TO_NV12;
229 else if ((format == PIPE_FORMAT_YUYV && buffer_format == PIPE_FORMAT_UYVY) ||
230 (format == PIPE_FORMAT_UYVY && buffer_format == PIPE_FORMAT_YUYV))
231 conversion = CONVERSION_SWAP_YUYV_UYVY;
232 else
233 return VDP_STATUS_NO_IMPLEMENTATION;
234 }
235
236 pipe_mutex_lock(vlsurface->device->mutex);
237 sampler_views = vlsurface->video_buffer->get_sampler_view_planes(vlsurface->video_buffer);
238 if (!sampler_views) {
239 pipe_mutex_unlock(vlsurface->device->mutex);
240 return VDP_STATUS_RESOURCES;
241 }
242
243 for (i = 0; i < 3; ++i) {
244 unsigned width, height;
245 struct pipe_sampler_view *sv = sampler_views[i];
246 if (!sv) continue;
247
248 vlVdpVideoSurfaceSize(vlsurface, i, &width, &height);
249
250 for (j = 0; j < sv->texture->array_size; ++j) {
251 struct pipe_box box = {
252 0, 0, j,
253 width, height, 1
254 };
255 struct pipe_transfer *transfer;
256 uint8_t *map;
257
258 map = pipe->transfer_map(pipe, sv->texture, 0,
259 PIPE_TRANSFER_READ, &box, &transfer);
260 if (!map) {
261 pipe_mutex_unlock(vlsurface->device->mutex);
262 return VDP_STATUS_RESOURCES;
263 }
264
265 if (conversion == CONVERSION_NV12_TO_YV12 && i == 1) {
266 u_copy_nv12_to_yv12(destination_data, destination_pitches,
267 i, j, transfer->stride, sv->texture->array_size,
268 map, box.width, box.height);
269 } else if (conversion == CONVERSION_YV12_TO_NV12 && i > 0) {
270 u_copy_yv12_to_nv12(destination_data, destination_pitches,
271 i, j, transfer->stride, sv->texture->array_size,
272 map, box.width, box.height);
273 } else if (conversion == CONVERSION_SWAP_YUYV_UYVY) {
274 u_copy_swap422_packed(destination_data, destination_pitches,
275 i, j, transfer->stride, sv->texture->array_size,
276 map, box.width, box.height);
277 } else {
278 util_copy_rect(destination_data[i] + destination_pitches[i] * j, sv->texture->format,
279 destination_pitches[i] * sv->texture->array_size, 0, 0,
280 box.width, box.height, map, transfer->stride, 0, 0);
281 }
282
283 pipe_transfer_unmap(pipe, transfer);
284 }
285 }
286 pipe_mutex_unlock(vlsurface->device->mutex);
287
288 return VDP_STATUS_OK;
289 }
290
291 /**
292 * Copy image data from application memory in a specific YCbCr format to
293 * a VdpVideoSurface.
294 */
295 VdpStatus
296 vlVdpVideoSurfacePutBitsYCbCr(VdpVideoSurface surface,
297 VdpYCbCrFormat source_ycbcr_format,
298 void const *const *source_data,
299 uint32_t const *source_pitches)
300 {
301 enum pipe_format pformat = FormatYCBCRToPipe(source_ycbcr_format);
302 struct pipe_context *pipe;
303 struct pipe_sampler_view **sampler_views;
304 unsigned i, j;
305
306 vlVdpSurface *p_surf = vlGetDataHTAB(surface);
307 if (!p_surf)
308 return VDP_STATUS_INVALID_HANDLE;
309
310 pipe = p_surf->device->context;
311 if (!pipe)
312 return VDP_STATUS_INVALID_HANDLE;
313
314 pipe_mutex_lock(p_surf->device->mutex);
315 if (p_surf->video_buffer == NULL || pformat != p_surf->video_buffer->buffer_format) {
316
317 /* destroy the old one */
318 if (p_surf->video_buffer)
319 p_surf->video_buffer->destroy(p_surf->video_buffer);
320
321 /* adjust the template parameters */
322 p_surf->templat.buffer_format = pformat;
323
324 /* and try to create the video buffer with the new format */
325 p_surf->video_buffer = pipe->create_video_buffer(pipe, &p_surf->templat);
326
327 /* stil no luck? ok forget it we don't support it */
328 if (!p_surf->video_buffer) {
329 pipe_mutex_unlock(p_surf->device->mutex);
330 return VDP_STATUS_NO_IMPLEMENTATION;
331 }
332 vlVdpVideoSurfaceClear(p_surf);
333 }
334
335 sampler_views = p_surf->video_buffer->get_sampler_view_planes(p_surf->video_buffer);
336 if (!sampler_views) {
337 pipe_mutex_unlock(p_surf->device->mutex);
338 return VDP_STATUS_RESOURCES;
339 }
340
341 for (i = 0; i < 3; ++i) {
342 unsigned width, height;
343 struct pipe_sampler_view *sv = sampler_views[i];
344 if (!sv || !source_pitches[i]) continue;
345
346 vlVdpVideoSurfaceSize(p_surf, i, &width, &height);
347
348 for (j = 0; j < sv->texture->array_size; ++j) {
349 struct pipe_box dst_box = {
350 0, 0, j,
351 width, height, 1
352 };
353
354 pipe->transfer_inline_write(pipe, sv->texture, 0,
355 PIPE_TRANSFER_WRITE, &dst_box,
356 source_data[i] + source_pitches[i] * j,
357 source_pitches[i] * sv->texture->array_size,
358 0);
359 }
360 }
361 pipe_mutex_unlock(p_surf->device->mutex);
362
363 return VDP_STATUS_OK;
364 }
365
366 /**
367 * Helper function to initially clear the VideoSurface after (re-)creation
368 */
369 void
370 vlVdpVideoSurfaceClear(vlVdpSurface *vlsurf)
371 {
372 struct pipe_context *pipe = vlsurf->device->context;
373 struct pipe_surface **surfaces;
374 unsigned i;
375
376 if (!vlsurf->video_buffer)
377 return;
378
379 surfaces = vlsurf->video_buffer->get_surfaces(vlsurf->video_buffer);
380 for (i = 0; i < VL_MAX_SURFACES; ++i) {
381 union pipe_color_union c = {};
382
383 if (!surfaces[i])
384 continue;
385
386 if (i > !!vlsurf->templat.interlaced)
387 c.f[0] = c.f[1] = c.f[2] = c.f[3] = 0.5f;
388
389 pipe->clear_render_target(pipe, surfaces[i], &c, 0, 0,
390 surfaces[i]->width, surfaces[i]->height);
391 }
392 pipe->flush(pipe, NULL, 0);
393 }
394
395 /**
396 * Interop to mesa state tracker
397 */
398 struct pipe_video_buffer *vlVdpVideoSurfaceGallium(VdpVideoSurface surface)
399 {
400 vlVdpSurface *p_surf = vlGetDataHTAB(surface);
401 if (!p_surf)
402 return NULL;
403
404 pipe_mutex_lock(p_surf->device->mutex);
405 if (p_surf->video_buffer == NULL) {
406 struct pipe_context *pipe = p_surf->device->context;
407
408 /* try to create a video buffer if we don't already have one */
409 p_surf->video_buffer = pipe->create_video_buffer(pipe, &p_surf->templat);
410 }
411 pipe_mutex_unlock(p_surf->device->mutex);
412
413 return p_surf->video_buffer;
414 }