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 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.
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    VdpStatus ret;
49 
50    vlVdpBitmapSurface *vlsurface = NULL;
51 
52    if (!(width && height))
53       return VDP_STATUS_INVALID_SIZE;
54 
55    vlVdpDevice *dev = vlGetDataHTAB(device);
56    if (!dev)
57       return VDP_STATUS_INVALID_HANDLE;
58 
59    pipe = dev->context;
60    if (!pipe)
61       return VDP_STATUS_INVALID_HANDLE;
62 
63    if (!surface)
64       return VDP_STATUS_INVALID_POINTER;
65 
66    vlsurface = CALLOC(1, sizeof(vlVdpBitmapSurface));
67    if (!vlsurface)
68       return VDP_STATUS_RESOURCES;
69 
70    DeviceReference(&vlsurface->device, dev);
71 
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;
77    res_tmpl.depth0 = 1;
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;
81 
82    mtx_lock(&dev->mutex);
83 
84    if (!CheckSurfaceParams(pipe->screen, &res_tmpl)) {
85       ret = VDP_STATUS_RESOURCES;
86       goto err_unlock;
87    }
88 
89    res = pipe->screen->resource_create(pipe->screen, &res_tmpl);
90    if (!res) {
91       ret = VDP_STATUS_RESOURCES;
92       goto err_unlock;
93    }
94 
95    vlVdpDefaultSamplerViewTemplate(&sv_templ, res);
96    vlsurface->sampler_view = pipe->create_sampler_view(pipe, res, &sv_templ);
97 
98    pipe_resource_reference(&res, NULL);
99 
100    if (!vlsurface->sampler_view) {
101       ret = VDP_STATUS_RESOURCES;
102       goto err_unlock;
103    }
104 
105    mtx_unlock(&dev->mutex);
106 
107    *surface = vlAddDataHTAB(vlsurface);
108    if (*surface == 0) {
109       mtx_lock(&dev->mutex);
110       ret = VDP_STATUS_ERROR;
111       goto err_sampler;
112    }
113 
114    return VDP_STATUS_OK;
115 
116 err_sampler:
117    pipe_sampler_view_reference(&vlsurface->sampler_view, NULL);
118 err_unlock:
119    mtx_unlock(&dev->mutex);
120    DeviceReference(&vlsurface->device, NULL);
121    FREE(vlsurface);
122    return ret;
123 }
124 
125 /**
126  * Destroy a VdpBitmapSurface.
127  */
128 VdpStatus
vlVdpBitmapSurfaceDestroy(VdpBitmapSurface surface)129 vlVdpBitmapSurfaceDestroy(VdpBitmapSurface surface)
130 {
131    vlVdpBitmapSurface *vlsurface;
132 
133    vlsurface = vlGetDataHTAB(surface);
134    if (!vlsurface)
135       return VDP_STATUS_INVALID_HANDLE;
136 
137    mtx_lock(&vlsurface->device->mutex);
138    pipe_sampler_view_reference(&vlsurface->sampler_view, NULL);
139    mtx_unlock(&vlsurface->device->mutex);
140 
141    vlRemoveDataHTAB(surface);
142    DeviceReference(&vlsurface->device, NULL);
143    FREE(vlsurface);
144 
145    return VDP_STATUS_OK;
146 }
147 
148 /**
149  * Retrieve the parameters used to create a VdpBitmapSurface.
150  */
151 VdpStatus
vlVdpBitmapSurfaceGetParameters(VdpBitmapSurface surface,VdpRGBAFormat * rgba_format,uint32_t * width,uint32_t * height,VdpBool * frequently_accessed)152 vlVdpBitmapSurfaceGetParameters(VdpBitmapSurface surface,
153                                 VdpRGBAFormat *rgba_format,
154                                 uint32_t *width, uint32_t *height,
155                                 VdpBool *frequently_accessed)
156 {
157    vlVdpBitmapSurface *vlsurface;
158    struct pipe_resource *res;
159 
160    vlsurface = vlGetDataHTAB(surface);
161    if (!vlsurface)
162       return VDP_STATUS_INVALID_HANDLE;
163 
164    if (!(rgba_format && width && height && frequently_accessed))
165       return VDP_STATUS_INVALID_POINTER;
166 
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;
172 
173    return VDP_STATUS_OK;
174 }
175 
176 /**
177  * Copy image data from application memory in the surface's native format to
178  * a VdpBitmapSurface.
179  */
180 VdpStatus
vlVdpBitmapSurfacePutBitsNative(VdpBitmapSurface surface,void const * const * source_data,uint32_t const * source_pitches,VdpRect const * destination_rect)181 vlVdpBitmapSurfacePutBitsNative(VdpBitmapSurface surface,
182                                 void const *const *source_data,
183                                 uint32_t const *source_pitches,
184                                 VdpRect const *destination_rect)
185 {
186    vlVdpBitmapSurface *vlsurface;
187    struct pipe_box dst_box;
188    struct pipe_context *pipe;
189 
190    vlsurface = vlGetDataHTAB(surface);
191    if (!vlsurface)
192       return VDP_STATUS_INVALID_HANDLE;
193 
194    if (!(source_data && source_pitches))
195       return VDP_STATUS_INVALID_POINTER;
196 
197    pipe = vlsurface->device->context;
198 
199    mtx_lock(&vlsurface->device->mutex);
200 
201    dst_box = RectToPipeBox(destination_rect, vlsurface->sampler_view->texture);
202    pipe->texture_subdata(pipe, vlsurface->sampler_view->texture, 0,
203                          PIPE_MAP_WRITE, &dst_box, *source_data,
204                          *source_pitches, 0);
205 
206    mtx_unlock(&vlsurface->device->mutex);
207 
208    return VDP_STATUS_OK;
209 }
210