surface.c revision 8f54929c5d9a07a999cb0a55c406062fd8cdfb74
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 */
42VdpStatus
43vlVdpVideoSurfaceCreate(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   VDPAU_MSG(VDPAU_TRACE, "[VDPAU] Creating a surface\n");
52
53   if (!(width && height)) {
54      ret = VDP_STATUS_INVALID_SIZE;
55      goto inv_size;
56   }
57
58   if (!vlCreateHTAB()) {
59      ret = VDP_STATUS_RESOURCES;
60      goto no_htab;
61   }
62
63   p_surf = CALLOC(1, sizeof(vlVdpSurface));
64   if (!p_surf) {
65      ret = VDP_STATUS_RESOURCES;
66      goto no_res;
67   }
68
69   vlVdpDevice *dev = vlGetDataHTAB(device);
70   if (!dev) {
71      ret = VDP_STATUS_INVALID_HANDLE;
72      goto inv_device;
73   }
74
75   p_surf->device = dev;
76   pipe = dev->context->pipe;
77
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
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
104no_handle:
105   p_surf->video_buffer->destroy(p_surf->video_buffer);
106
107inv_device:
108   FREE(p_surf);
109
110no_res:
111no_htab:
112inv_size:
113   return ret;
114}
115
116/**
117 * Destroy a VdpVideoSurface.
118 */
119VdpStatus
120vlVdpVideoSurfaceDestroy(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   if (p_surf->video_buffer)
129      p_surf->video_buffer->destroy(p_surf->video_buffer);
130
131   FREE(p_surf);
132   return VDP_STATUS_OK;
133}
134
135/**
136 * Retrieve the parameters used to create a VdpVideoSurface.
137 */
138VdpStatus
139vlVdpVideoSurfaceGetParameters(VdpVideoSurface surface,
140                               VdpChromaType *chroma_type,
141                               uint32_t *width, uint32_t *height)
142{
143   if (!(width && height && chroma_type))
144      return VDP_STATUS_INVALID_POINTER;
145
146   vlVdpSurface *p_surf = vlGetDataHTAB(surface);
147   if (!p_surf)
148      return VDP_STATUS_INVALID_HANDLE;
149
150   if (p_surf->video_buffer) {
151      *width = p_surf->video_buffer->width;
152      *height = p_surf->video_buffer->height;
153      *chroma_type = PipeToChroma(p_surf->video_buffer->chroma_format);
154   } else {
155      *width = p_surf->templat.width;
156      *height = p_surf->templat.height;
157      *chroma_type = PipeToChroma(p_surf->templat.chroma_format);
158   }
159
160   return VDP_STATUS_OK;
161}
162
163/**
164 * Copy image data from a VdpVideoSurface to application memory in a specified
165 * YCbCr format.
166 */
167VdpStatus
168vlVdpVideoSurfaceGetBitsYCbCr(VdpVideoSurface surface,
169                              VdpYCbCrFormat destination_ycbcr_format,
170                              void *const *destination_data,
171                              uint32_t const *destination_pitches)
172{
173   if (!vlCreateHTAB())
174      return VDP_STATUS_RESOURCES;
175
176   vlVdpSurface *p_surf = vlGetDataHTAB(surface);
177   if (!p_surf)
178      return VDP_STATUS_INVALID_HANDLE;
179
180   //if (!p_surf->psurface)
181   //   return VDP_STATUS_RESOURCES;
182
183   //return VDP_STATUS_OK;
184   return VDP_STATUS_NO_IMPLEMENTATION;
185}
186
187/**
188 * Copy image data from application memory in a specific YCbCr format to
189 * a VdpVideoSurface.
190 */
191VdpStatus
192vlVdpVideoSurfacePutBitsYCbCr(VdpVideoSurface surface,
193                              VdpYCbCrFormat source_ycbcr_format,
194                              void const *const *source_data,
195                              uint32_t const *source_pitches)
196{
197   enum pipe_format pformat = FormatYCBCRToPipe(source_ycbcr_format);
198   struct pipe_context *pipe;
199   struct pipe_sampler_view **sampler_views;
200   unsigned i, j;
201
202   if (!vlCreateHTAB())
203      return VDP_STATUS_RESOURCES;
204
205   vlVdpSurface *p_surf = vlGetDataHTAB(surface);
206   if (!p_surf)
207      return VDP_STATUS_INVALID_HANDLE;
208
209   pipe = p_surf->device->context->pipe;
210   if (!pipe)
211      return VDP_STATUS_INVALID_HANDLE;
212
213   if (p_surf->video_buffer == NULL || pformat != p_surf->video_buffer->buffer_format) {
214
215      /* destroy the old one */
216      if (p_surf->video_buffer)
217         p_surf->video_buffer->destroy(p_surf->video_buffer);
218
219      /* adjust the template parameters */
220      p_surf->templat.buffer_format = pformat;
221
222      /* and try to create the video buffer with the new format */
223      p_surf->video_buffer = pipe->create_video_buffer(pipe, &p_surf->templat);
224
225      /* stil no luck? ok forget it we don't support it */
226      if (!p_surf->video_buffer)
227         return VDP_STATUS_NO_IMPLEMENTATION;
228   }
229
230   sampler_views = p_surf->video_buffer->get_sampler_view_planes(p_surf->video_buffer);
231   if (!sampler_views)
232      return VDP_STATUS_RESOURCES;
233
234   for (i = 0; i < 3; ++i) {
235      struct pipe_sampler_view *sv = sampler_views[i];
236      if (!sv) continue;
237
238      for (j = 0; j < sv->texture->depth0; ++j) {
239         struct pipe_box dst_box = {
240            0, 0, j,
241            sv->texture->width0, sv->texture->height0, 1
242         };
243
244         struct pipe_transfer *transfer;
245         void *map;
246
247         transfer = pipe->get_transfer(pipe, sv->texture, 0, PIPE_TRANSFER_WRITE, &dst_box);
248         if (!transfer)
249            return VDP_STATUS_RESOURCES;
250
251         map = pipe->transfer_map(pipe, transfer);
252         if (map) {
253            util_copy_rect(map, sv->texture->format, transfer->stride, 0, 0,
254                           dst_box.width, dst_box.height,
255                           source_data[i] + source_pitches[i] * j,
256                           source_pitches[i] * sv->texture->depth0,
257                           0, 0);
258         }
259
260         pipe->transfer_unmap(pipe, transfer);
261         pipe->transfer_destroy(pipe, transfer);
262      }
263   }
264
265   return VDP_STATUS_OK;
266}
267