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