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