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