Merge commit 'origin/master' into gallium-0.2
[mesa.git] / src / gallium / state_trackers / g3dvl / vl_surface.c
1 #define VL_INTERNAL
2 #include "vl_surface.h"
3 #include <assert.h>
4 #include <string.h>
5 #include <pipe/p_screen.h>
6 #include <pipe/p_state.h>
7 #include <pipe/p_inlines.h>
8 #include <util/u_memory.h>
9 #include <vl_winsys.h>
10 #include "vl_screen.h"
11 #include "vl_context.h"
12 #include "vl_render.h"
13 #include "vl_csc.h"
14 #include "vl_util.h"
15
16 int vlCreateSurface
17 (
18 struct vlScreen *screen,
19 unsigned int width,
20 unsigned int height,
21 enum vlFormat format,
22 struct vlSurface **surface
23 )
24 {
25 struct vlSurface *sfc;
26 struct pipe_texture template;
27
28 assert(screen);
29 assert(surface);
30
31 sfc = CALLOC_STRUCT(vlSurface);
32
33 if (!sfc)
34 return 1;
35
36 sfc->screen = screen;
37 sfc->width = width;
38 sfc->height = height;
39 sfc->format = format;
40
41 memset(&template, 0, sizeof(struct pipe_texture));
42 template.target = PIPE_TEXTURE_2D;
43 template.format = PIPE_FORMAT_A8R8G8B8_UNORM;
44 template.last_level = 0;
45 template.width[0] = vlRoundUpPOT(sfc->width);
46 template.height[0] = vlRoundUpPOT(sfc->height);
47 template.depth[0] = 1;
48 template.compressed = 0;
49 pf_get_block(template.format, &template.block);
50 template.tex_usage = PIPE_TEXTURE_USAGE_SAMPLER | PIPE_TEXTURE_USAGE_RENDER_TARGET;
51
52 sfc->texture = vlGetPipeScreen(screen)->texture_create(vlGetPipeScreen(screen), &template);
53
54 *surface = sfc;
55
56 return 0;
57 }
58
59 int vlDestroySurface
60 (
61 struct vlSurface *surface
62 )
63 {
64 assert(surface);
65
66 pipe_texture_release(&surface->texture);
67 FREE(surface);
68
69 return 0;
70 }
71
72 int vlRenderMacroBlocksMpeg2
73 (
74 struct vlMpeg2MacroBlockBatch *batch,
75 struct vlSurface *surface
76 )
77 {
78 assert(batch);
79 assert(surface);
80 assert(surface->context);
81
82 surface->context->render->vlBegin(surface->context->render);
83
84 surface->context->render->vlRenderMacroBlocksMpeg2
85 (
86 surface->context->render,
87 batch,
88 surface
89 );
90
91 surface->context->render->vlEnd(surface->context->render);
92
93 return 0;
94 }
95
96 int vlPutPicture
97 (
98 struct vlSurface *surface,
99 vlNativeDrawable drawable,
100 int srcx,
101 int srcy,
102 int srcw,
103 int srch,
104 int destx,
105 int desty,
106 int destw,
107 int desth,
108 int drawable_w,
109 int drawable_h,
110 enum vlPictureType picture_type
111 )
112 {
113 struct vlCSC *csc;
114 struct pipe_context *pipe;
115
116 assert(surface);
117 assert(surface->context);
118
119 surface->context->render->vlFlush(surface->context->render);
120
121 csc = surface->context->csc;
122 pipe = surface->context->pipe;
123
124 csc->vlResizeFrameBuffer(csc, drawable_w, drawable_h);
125
126 csc->vlBegin(csc);
127
128 csc->vlPutPicture
129 (
130 csc,
131 surface,
132 srcx,
133 srcy,
134 srcw,
135 srch,
136 destx,
137 desty,
138 destw,
139 desth,
140 picture_type
141 );
142
143 csc->vlEnd(csc);
144
145 pipe->flush(pipe, PIPE_FLUSH_RENDER_CACHE, &surface->disp_fence);
146
147 bind_pipe_drawable(pipe, drawable);
148
149 pipe->winsys->flush_frontbuffer
150 (
151 pipe->winsys,
152 csc->vlGetFrameBuffer(csc),
153 pipe->priv
154 );
155
156 return 0;
157 }
158
159 int vlSurfaceGetStatus
160 (
161 struct vlSurface *surface,
162 enum vlResourceStatus *status
163 )
164 {
165 assert(surface);
166 assert(surface->context);
167 assert(status);
168
169 if (surface->render_fence && !surface->context->pipe->winsys->fence_signalled(surface->context->pipe->winsys, surface->render_fence, 0))
170 {
171 *status = vlResourceStatusRendering;
172 return 0;
173 }
174
175 if (surface->disp_fence && !surface->context->pipe->winsys->fence_signalled(surface->context->pipe->winsys, surface->disp_fence, 0))
176 {
177 *status = vlResourceStatusDisplaying;
178 return 0;
179 }
180
181 *status = vlResourceStatusFree;
182
183 return 0;
184 }
185
186 int vlSurfaceFlush
187 (
188 struct vlSurface *surface
189 )
190 {
191 assert(surface);
192 assert(surface->context);
193
194 surface->context->render->vlFlush(surface->context->render);
195
196 return 0;
197 }
198
199 int vlSurfaceSync
200 (
201 struct vlSurface *surface
202 )
203 {
204 assert(surface);
205 assert(surface->context);
206 assert(surface->render_fence);
207
208 surface->context->pipe->winsys->fence_finish(surface->context->pipe->winsys, surface->render_fence, 0);
209
210 return 0;
211 }
212
213 struct vlScreen* vlSurfaceGetScreen
214 (
215 struct vlSurface *surface
216 )
217 {
218 assert(surface);
219
220 return surface->screen;
221 }
222
223 struct vlContext* vlBindToContext
224 (
225 struct vlSurface *surface,
226 struct vlContext *context
227 )
228 {
229 struct vlContext *old;
230
231 assert(surface);
232
233 old = surface->context;
234 surface->context = context;
235
236 return old;
237 }