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