surface.c revision 10fd45114d4a7bbac4093755305ea5e4ba3ab6a5
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_video_context.h>
32#include <pipe/p_state.h>
33
34#include <util/u_memory.h>
35#include <util/u_debug.h>
36#include <util/u_rect.h>
37
38#include "vdpau_private.h"
39
40VdpStatus
41vlVdpVideoSurfaceCreate(VdpDevice device, VdpChromaType chroma_type,
42                        uint32_t width, uint32_t height,
43                        VdpVideoSurface *surface)
44{
45   vlVdpSurface *p_surf;
46   VdpStatus ret;
47
48   VDPAU_MSG(VDPAU_TRACE, "[VDPAU] Creating a surface\n");
49
50   if (!(width && height)) {
51      ret = VDP_STATUS_INVALID_SIZE;
52      goto inv_size;
53   }
54
55   if (!vlCreateHTAB()) {
56      ret = VDP_STATUS_RESOURCES;
57      goto no_htab;
58   }
59
60   p_surf = CALLOC(1, sizeof(vlVdpSurface));
61   if (!p_surf) {
62      ret = VDP_STATUS_RESOURCES;
63      goto no_res;
64   }
65
66   vlVdpDevice *dev = vlGetDataHTAB(device);
67   if (!dev) {
68      ret = VDP_STATUS_INVALID_HANDLE;
69      goto inv_device;
70   }
71
72   p_surf->device = dev;
73   p_surf->video_buffer = dev->context->vpipe->create_buffer
74   (
75      dev->context->vpipe,
76      PIPE_FORMAT_YV12, // most common used
77      ChromaToPipe(chroma_type),
78      width, height
79   );
80
81   *surface = vlAddDataHTAB(p_surf);
82   if (*surface == 0) {
83      ret = VDP_STATUS_ERROR;
84      goto no_handle;
85   }
86
87   return VDP_STATUS_OK;
88
89no_handle:
90   p_surf->video_buffer->destroy(p_surf->video_buffer);
91
92inv_device:
93   FREE(p_surf);
94
95no_res:
96no_htab:
97inv_size:
98   return ret;
99}
100
101VdpStatus
102vlVdpVideoSurfaceDestroy(VdpVideoSurface surface)
103{
104   vlVdpSurface *p_surf;
105
106   p_surf = (vlVdpSurface *)vlGetDataHTAB((vlHandle)surface);
107   if (!p_surf)
108      return VDP_STATUS_INVALID_HANDLE;
109
110   if (p_surf->video_buffer)
111      p_surf->video_buffer->destroy(p_surf->video_buffer);
112
113   FREE(p_surf);
114   return VDP_STATUS_OK;
115}
116
117VdpStatus
118vlVdpVideoSurfaceGetParameters(VdpVideoSurface surface,
119                               VdpChromaType *chroma_type,
120                               uint32_t *width, uint32_t *height)
121{
122   if (!(width && height && chroma_type))
123      return VDP_STATUS_INVALID_POINTER;
124
125   vlVdpSurface *p_surf = vlGetDataHTAB(surface);
126   if (!p_surf)
127      return VDP_STATUS_INVALID_HANDLE;
128
129   *width = p_surf->video_buffer->width;
130   *height = p_surf->video_buffer->height;
131   *chroma_type = PipeToChroma(p_surf->video_buffer->chroma_format);
132
133   return VDP_STATUS_OK;
134}
135
136VdpStatus
137vlVdpVideoSurfaceGetBitsYCbCr(VdpVideoSurface surface,
138                              VdpYCbCrFormat destination_ycbcr_format,
139                              void *const *destination_data,
140                              uint32_t const *destination_pitches)
141{
142   if (!vlCreateHTAB())
143      return VDP_STATUS_RESOURCES;
144
145   vlVdpSurface *p_surf = vlGetDataHTAB(surface);
146   if (!p_surf)
147      return VDP_STATUS_INVALID_HANDLE;
148
149   //if (!p_surf->psurface)
150   //   return VDP_STATUS_RESOURCES;
151
152   //return VDP_STATUS_OK;
153   return VDP_STATUS_NO_IMPLEMENTATION;
154}
155
156VdpStatus
157vlVdpVideoSurfacePutBitsYCbCr(VdpVideoSurface surface,
158                              VdpYCbCrFormat source_ycbcr_format,
159                              void const *const *source_data,
160                              uint32_t const *source_pitches)
161{
162   enum pipe_format pformat = FormatToPipe(source_ycbcr_format);
163   struct pipe_context *pipe;
164   struct pipe_video_context *context;
165   struct pipe_sampler_view **sampler_views;
166   unsigned i;
167
168   if (!vlCreateHTAB())
169      return VDP_STATUS_RESOURCES;
170
171   vlVdpSurface *p_surf = vlGetDataHTAB(surface);
172   if (!p_surf)
173      return VDP_STATUS_INVALID_HANDLE;
174
175   pipe = p_surf->device->context->pipe;
176   context = p_surf->device->context->vpipe;
177   if (!pipe && !context)
178      return VDP_STATUS_INVALID_HANDLE;
179
180   if (p_surf->video_buffer == NULL || pformat != p_surf->video_buffer->buffer_format) {
181      assert(0); // TODO Recreate resource
182      return VDP_STATUS_NO_IMPLEMENTATION;
183   }
184
185   sampler_views = p_surf->video_buffer->get_sampler_view_planes(p_surf->video_buffer);
186   if (!sampler_views)
187      return VDP_STATUS_RESOURCES;
188
189   for (i = 0; i < 3; ++i) { //TODO put nr of planes into util format
190      struct pipe_sampler_view *sv = sampler_views[i ? i ^ 3 : 0];
191      struct pipe_box dst_box = { 0, 0, 0, sv->texture->width0, sv->texture->height0, 1 };
192
193      struct pipe_transfer *transfer;
194      void *map;
195
196      transfer = pipe->get_transfer(pipe, sv->texture, 0, PIPE_TRANSFER_WRITE, &dst_box);
197      if (!transfer)
198         return VDP_STATUS_RESOURCES;
199
200      map = pipe->transfer_map(pipe, transfer);
201      if (map) {
202         util_copy_rect(map, sv->texture->format, transfer->stride, 0, 0,
203                        dst_box.width, dst_box.height,
204                        source_data[i], source_pitches[i], 0, 0);
205
206         pipe->transfer_unmap(pipe, transfer);
207      }
208
209      pipe->transfer_destroy(pipe, transfer);
210   }
211
212   return VDP_STATUS_OK;
213}
214