bitmap.c revision c32114460dbb7f33885c181a0d7dee07b15b8751
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 */
38VdpStatus
39vlVdpBitmapSurfaceCreate(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   vlsurface->device = dev;
71
72   memset(&res_tmpl, 0, sizeof(res_tmpl));
73   res_tmpl.target = PIPE_TEXTURE_2D;
74   res_tmpl.format = FormatRGBAToPipe(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   pipe_mutex_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   pipe_mutex_unlock(dev->mutex);
106
107   *surface = vlAddDataHTAB(vlsurface);
108   if (*surface == 0) {
109      pipe_mutex_lock(dev->mutex);
110      ret = VDP_STATUS_ERROR;
111      goto err_sampler;
112   }
113
114   return VDP_STATUS_OK;
115
116err_sampler:
117   pipe_sampler_view_reference(&vlsurface->sampler_view, NULL);
118err_unlock:
119   pipe_mutex_unlock(dev->mutex);
120   FREE(vlsurface);
121   return ret;
122}
123
124/**
125 * Destroy a VdpBitmapSurface.
126 */
127VdpStatus
128vlVdpBitmapSurfaceDestroy(VdpBitmapSurface surface)
129{
130   vlVdpBitmapSurface *vlsurface;
131
132   vlsurface = vlGetDataHTAB(surface);
133   if (!vlsurface)
134      return VDP_STATUS_INVALID_HANDLE;
135
136   pipe_mutex_lock(vlsurface->device->mutex);
137   pipe_sampler_view_reference(&vlsurface->sampler_view, NULL);
138   pipe_mutex_unlock(vlsurface->device->mutex);
139
140   vlRemoveDataHTAB(surface);
141   FREE(vlsurface);
142
143   return VDP_STATUS_OK;
144}
145
146/**
147 * Retrieve the parameters used to create a VdpBitmapSurface.
148 */
149VdpStatus
150vlVdpBitmapSurfaceGetParameters(VdpBitmapSurface surface,
151                                VdpRGBAFormat *rgba_format,
152                                uint32_t *width, uint32_t *height,
153                                VdpBool *frequently_accessed)
154{
155   vlVdpBitmapSurface *vlsurface;
156   struct pipe_resource *res;
157
158   vlsurface = vlGetDataHTAB(surface);
159   if (!vlsurface)
160      return VDP_STATUS_INVALID_HANDLE;
161
162   if (!(rgba_format && width && height && frequently_accessed))
163      return VDP_STATUS_INVALID_POINTER;
164
165   res = vlsurface->sampler_view->texture;
166   *rgba_format = PipeToFormatRGBA(res->format);
167   *width = res->width0;
168   *height = res->height0;
169   *frequently_accessed = res->usage == PIPE_USAGE_DYNAMIC;
170
171   return VDP_STATUS_OK;
172}
173
174/**
175 * Copy image data from application memory in the surface's native format to
176 * a VdpBitmapSurface.
177 */
178VdpStatus
179vlVdpBitmapSurfacePutBitsNative(VdpBitmapSurface surface,
180                                void const *const *source_data,
181                                uint32_t const *source_pitches,
182                                VdpRect const *destination_rect)
183{
184   vlVdpBitmapSurface *vlsurface;
185   struct pipe_box dst_box;
186   struct pipe_context *pipe;
187
188   vlsurface = vlGetDataHTAB(surface);
189   if (!vlsurface)
190      return VDP_STATUS_INVALID_HANDLE;
191
192   if (!(source_data && source_pitches))
193      return VDP_STATUS_INVALID_POINTER;
194
195   pipe = vlsurface->device->context;
196
197   pipe_mutex_lock(vlsurface->device->mutex);
198
199   vlVdpResolveDelayedRendering(vlsurface->device, NULL, NULL);
200
201   dst_box = RectToPipeBox(destination_rect, vlsurface->sampler_view->texture);
202   pipe->transfer_inline_write(pipe, vlsurface->sampler_view->texture, 0,
203                               PIPE_TRANSFER_WRITE, &dst_box, *source_data,
204                               *source_pitches, 0);
205
206   pipe_mutex_unlock(vlsurface->device->mutex);
207
208   return VDP_STATUS_OK;
209}
210