1 /**************************************************************************
2 *
3 * Copyright 2010 Thomas Balling Sørensen.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28 #include <vdpau/vdpau.h>
29
30 #include "util/u_memory.h"
31 #include "util/u_sampler.h"
32
33 #include "vdpau_private.h"
34
35 /**
36 * Create a VdpBitmapSurface.
37 */
38 VdpStatus
vlVdpBitmapSurfaceCreate(VdpDevice device,VdpRGBAFormat rgba_format,uint32_t width,uint32_t height,VdpBool frequently_accessed,VdpBitmapSurface * surface)39 vlVdpBitmapSurfaceCreate(VdpDevice device,
40 VdpRGBAFormat rgba_format,
41 uint32_t width, uint32_t height,
42 VdpBool frequently_accessed,
43 VdpBitmapSurface *surface)
44 {
45 struct pipe_context *pipe;
46 struct pipe_resource res_tmpl, *res;
47 struct pipe_sampler_view sv_templ;
48
49 vlVdpBitmapSurface *vlsurface = NULL;
50
51 if (!(width && height))
52 return VDP_STATUS_INVALID_SIZE;
53
54 vlVdpDevice *dev = vlGetDataHTAB(device);
55 if (!dev)
56 return VDP_STATUS_INVALID_HANDLE;
57
58 pipe = dev->context;
59 if (!pipe)
60 return VDP_STATUS_INVALID_HANDLE;
61
62 if (!surface)
63 return VDP_STATUS_INVALID_POINTER;
64
65 vlsurface = CALLOC(1, sizeof(vlVdpBitmapSurface));
66 if (!vlsurface)
67 return VDP_STATUS_RESOURCES;
68
69 vlsurface->device = dev;
70
71 memset(&res_tmpl, 0, sizeof(res_tmpl));
72 res_tmpl.target = PIPE_TEXTURE_2D;
73 res_tmpl.format = FormatRGBAToPipe(rgba_format);
74 res_tmpl.width0 = width;
75 res_tmpl.height0 = height;
76 res_tmpl.depth0 = 1;
77 res_tmpl.array_size = 1;
78 res_tmpl.bind = PIPE_BIND_SAMPLER_VIEW | PIPE_BIND_RENDER_TARGET;
79 res_tmpl.usage = frequently_accessed ? PIPE_USAGE_DYNAMIC : PIPE_USAGE_STATIC;
80
81 pipe_mutex_lock(dev->mutex);
82 res = pipe->screen->resource_create(pipe->screen, &res_tmpl);
83 if (!res) {
84 pipe_mutex_unlock(dev->mutex);
85 FREE(dev);
86 return VDP_STATUS_RESOURCES;
87 }
88
89 vlVdpDefaultSamplerViewTemplate(&sv_templ, res);
90 vlsurface->sampler_view = pipe->create_sampler_view(pipe, res, &sv_templ);
91
92 pipe_resource_reference(&res, NULL);
93 pipe_mutex_unlock(dev->mutex);
94
95 if (!vlsurface->sampler_view) {
96 FREE(dev);
97 return VDP_STATUS_RESOURCES;
98 }
99
100 *surface = vlAddDataHTAB(vlsurface);
101 if (*surface == 0) {
102 FREE(dev);
103 return VDP_STATUS_ERROR;
104 }
105
106 return VDP_STATUS_OK;
107 }
108
109 /**
110 * Destroy a VdpBitmapSurface.
111 */
112 VdpStatus
vlVdpBitmapSurfaceDestroy(VdpBitmapSurface surface)113 vlVdpBitmapSurfaceDestroy(VdpBitmapSurface surface)
114 {
115 vlVdpBitmapSurface *vlsurface;
116
117 vlsurface = vlGetDataHTAB(surface);
118 if (!vlsurface)
119 return VDP_STATUS_INVALID_HANDLE;
120
121 pipe_mutex_lock(vlsurface->device->mutex);
122 pipe_sampler_view_reference(&vlsurface->sampler_view, NULL);
123 pipe_mutex_unlock(vlsurface->device->mutex);
124
125 vlRemoveDataHTAB(surface);
126 FREE(vlsurface);
127
128 return VDP_STATUS_OK;
129 }
130
131 /**
132 * Retrieve the parameters used to create a VdpBitmapSurface.
133 */
134 VdpStatus
vlVdpBitmapSurfaceGetParameters(VdpBitmapSurface surface,VdpRGBAFormat * rgba_format,uint32_t * width,uint32_t * height,VdpBool * frequently_accessed)135 vlVdpBitmapSurfaceGetParameters(VdpBitmapSurface surface,
136 VdpRGBAFormat *rgba_format,
137 uint32_t *width, uint32_t *height,
138 VdpBool *frequently_accessed)
139 {
140 vlVdpBitmapSurface *vlsurface;
141 struct pipe_resource *res;
142
143 vlsurface = vlGetDataHTAB(surface);
144 if (!vlsurface)
145 return VDP_STATUS_INVALID_HANDLE;
146
147 if (!(rgba_format && width && height && frequently_accessed))
148 return VDP_STATUS_INVALID_POINTER;
149
150 res = vlsurface->sampler_view->texture;
151 *rgba_format = PipeToFormatRGBA(res->format);
152 *width = res->width0;
153 *height = res->height0;
154 *frequently_accessed = res->usage == PIPE_USAGE_DYNAMIC;
155
156 return VDP_STATUS_OK;
157 }
158
159 /**
160 * Copy image data from application memory in the surface's native format to
161 * a VdpBitmapSurface.
162 */
163 VdpStatus
vlVdpBitmapSurfacePutBitsNative(VdpBitmapSurface surface,void const * const * source_data,uint32_t const * source_pitches,VdpRect const * destination_rect)164 vlVdpBitmapSurfacePutBitsNative(VdpBitmapSurface surface,
165 void const *const *source_data,
166 uint32_t const *source_pitches,
167 VdpRect const *destination_rect)
168 {
169 vlVdpBitmapSurface *vlsurface;
170 struct pipe_box dst_box;
171 struct pipe_context *pipe;
172
173 vlsurface = vlGetDataHTAB(surface);
174 if (!vlsurface)
175 return VDP_STATUS_INVALID_HANDLE;
176
177 if (!(source_data && source_pitches))
178 return VDP_STATUS_INVALID_POINTER;
179
180 pipe = vlsurface->device->context;
181
182 pipe_mutex_lock(vlsurface->device->mutex);
183
184 vlVdpResolveDelayedRendering(vlsurface->device, NULL, NULL);
185
186 dst_box = RectToPipeBox(destination_rect, vlsurface->sampler_view->texture);
187 pipe->transfer_inline_write(pipe, vlsurface->sampler_view->texture, 0,
188 PIPE_TRANSFER_WRITE, &dst_box, *source_data,
189 *source_pitches, 0);
190
191 pipe_mutex_unlock(vlsurface->device->mutex);
192
193 return VDP_STATUS_OK;
194 }
195