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