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