1 /**************************************************************************
3 * Copyright 2010 Thomas Balling Sørensen.
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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 VMWARE 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.
26 **************************************************************************/
28 #include <vdpau/vdpau.h>
30 #include "util/u_memory.h"
31 #include "util/u_sampler.h"
33 #include "vdpau_private.h"
36 * Create a VdpBitmapSurface.
39 vlVdpBitmapSurfaceCreate(VdpDevice device
,
40 VdpRGBAFormat rgba_format
,
41 uint32_t width
, uint32_t height
,
42 VdpBool frequently_accessed
,
43 VdpBitmapSurface
*surface
)
45 struct pipe_context
*pipe
;
46 struct pipe_resource res_tmpl
, *res
;
47 struct pipe_sampler_view sv_templ
;
50 vlVdpBitmapSurface
*vlsurface
= NULL
;
52 if (!(width
&& height
))
53 return VDP_STATUS_INVALID_SIZE
;
55 vlVdpDevice
*dev
= vlGetDataHTAB(device
);
57 return VDP_STATUS_INVALID_HANDLE
;
61 return VDP_STATUS_INVALID_HANDLE
;
64 return VDP_STATUS_INVALID_POINTER
;
66 vlsurface
= CALLOC(1, sizeof(vlVdpBitmapSurface
));
68 return VDP_STATUS_RESOURCES
;
70 DeviceReference(&vlsurface
->device
, dev
);
72 memset(&res_tmpl
, 0, sizeof(res_tmpl
));
73 res_tmpl
.target
= PIPE_TEXTURE_2D
;
74 res_tmpl
.format
= VdpFormatRGBAToPipe(rgba_format
);
75 res_tmpl
.width0
= width
;
76 res_tmpl
.height0
= height
;
78 res_tmpl
.array_size
= 1;
79 res_tmpl
.bind
= PIPE_BIND_SAMPLER_VIEW
| PIPE_BIND_RENDER_TARGET
;
80 res_tmpl
.usage
= frequently_accessed
? PIPE_USAGE_DYNAMIC
: PIPE_USAGE_DEFAULT
;
82 pipe_mutex_lock(dev
->mutex
);
84 if (!CheckSurfaceParams(pipe
->screen
, &res_tmpl
)) {
85 ret
= VDP_STATUS_RESOURCES
;
89 res
= pipe
->screen
->resource_create(pipe
->screen
, &res_tmpl
);
91 ret
= VDP_STATUS_RESOURCES
;
95 vlVdpDefaultSamplerViewTemplate(&sv_templ
, res
);
96 vlsurface
->sampler_view
= pipe
->create_sampler_view(pipe
, res
, &sv_templ
);
98 pipe_resource_reference(&res
, NULL
);
100 if (!vlsurface
->sampler_view
) {
101 ret
= VDP_STATUS_RESOURCES
;
105 pipe_mutex_unlock(dev
->mutex
);
107 *surface
= vlAddDataHTAB(vlsurface
);
109 pipe_mutex_lock(dev
->mutex
);
110 ret
= VDP_STATUS_ERROR
;
114 return VDP_STATUS_OK
;
117 pipe_sampler_view_reference(&vlsurface
->sampler_view
, NULL
);
119 pipe_mutex_unlock(dev
->mutex
);
120 DeviceReference(&vlsurface
->device
, NULL
);
126 * Destroy a VdpBitmapSurface.
129 vlVdpBitmapSurfaceDestroy(VdpBitmapSurface surface
)
131 vlVdpBitmapSurface
*vlsurface
;
133 vlsurface
= vlGetDataHTAB(surface
);
135 return VDP_STATUS_INVALID_HANDLE
;
137 pipe_mutex_lock(vlsurface
->device
->mutex
);
138 pipe_sampler_view_reference(&vlsurface
->sampler_view
, NULL
);
139 pipe_mutex_unlock(vlsurface
->device
->mutex
);
141 vlRemoveDataHTAB(surface
);
142 DeviceReference(&vlsurface
->device
, NULL
);
145 return VDP_STATUS_OK
;
149 * Retrieve the parameters used to create a VdpBitmapSurface.
152 vlVdpBitmapSurfaceGetParameters(VdpBitmapSurface surface
,
153 VdpRGBAFormat
*rgba_format
,
154 uint32_t *width
, uint32_t *height
,
155 VdpBool
*frequently_accessed
)
157 vlVdpBitmapSurface
*vlsurface
;
158 struct pipe_resource
*res
;
160 vlsurface
= vlGetDataHTAB(surface
);
162 return VDP_STATUS_INVALID_HANDLE
;
164 if (!(rgba_format
&& width
&& height
&& frequently_accessed
))
165 return VDP_STATUS_INVALID_POINTER
;
167 res
= vlsurface
->sampler_view
->texture
;
168 *rgba_format
= PipeToFormatRGBA(res
->format
);
169 *width
= res
->width0
;
170 *height
= res
->height0
;
171 *frequently_accessed
= res
->usage
== PIPE_USAGE_DYNAMIC
;
173 return VDP_STATUS_OK
;
177 * Copy image data from application memory in the surface's native format to
178 * a VdpBitmapSurface.
181 vlVdpBitmapSurfacePutBitsNative(VdpBitmapSurface surface
,
182 void const *const *source_data
,
183 uint32_t const *source_pitches
,
184 VdpRect
const *destination_rect
)
186 vlVdpBitmapSurface
*vlsurface
;
187 struct pipe_box dst_box
;
188 struct pipe_context
*pipe
;
190 vlsurface
= vlGetDataHTAB(surface
);
192 return VDP_STATUS_INVALID_HANDLE
;
194 if (!(source_data
&& source_pitches
))
195 return VDP_STATUS_INVALID_POINTER
;
197 pipe
= vlsurface
->device
->context
;
199 pipe_mutex_lock(vlsurface
->device
->mutex
);
201 vlVdpResolveDelayedRendering(vlsurface
->device
, NULL
, NULL
);
203 dst_box
= RectToPipeBox(destination_rect
, vlsurface
->sampler_view
->texture
);
204 pipe
->texture_subdata(pipe
, vlsurface
->sampler_view
->texture
, 0,
205 PIPE_TRANSFER_WRITE
, &dst_box
, *source_data
,
208 pipe_mutex_unlock(vlsurface
->device
->mutex
);
210 return VDP_STATUS_OK
;