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