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