56cc60aebe1889eabd775869b44291a92932565a
[mesa.git] / src / gallium / state_trackers / vega / vg_tracker.c
1 /**************************************************************************
2 *
3 * Copyright 2009 VMware, Inc. All Rights Reserved.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sub license, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
12 *
13 * The above copyright notice and this permission notice (including the
14 * next paragraph) shall be included in all copies or substantial portions
15 * of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
20 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
21 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
22 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
23 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 *
25 **************************************************************************/
26
27 #include "vg_context.h"
28 #include "vg_tracker.h"
29 #include "mask.h"
30
31 #include "pipe/p_context.h"
32 #include "pipe/p_inlines.h"
33 #include "pipe/p_screen.h"
34 #include "util/u_memory.h"
35 #include "util/u_math.h"
36
37 static struct pipe_texture *
38 create_texture(struct pipe_context *pipe, enum pipe_format format,
39 VGint width, VGint height)
40 {
41 struct pipe_texture templ;
42
43 memset(&templ, 0, sizeof(templ));
44
45 if (format != PIPE_FORMAT_NONE) {
46 templ.format = format;
47 }
48 else {
49 templ.format = PIPE_FORMAT_A8R8G8B8_UNORM;
50 }
51
52 templ.target = PIPE_TEXTURE_2D;
53 pf_get_block(templ.format, &templ.block);
54 templ.width[0] = width;
55 templ.height[0] = height;
56 templ.depth[0] = 1;
57 templ.last_level = 0;
58
59 if (pf_get_component_bits(format, PIPE_FORMAT_COMP_S)) {
60 templ.tex_usage = PIPE_TEXTURE_USAGE_DEPTH_STENCIL;
61 } else {
62 templ.tex_usage = (PIPE_TEXTURE_USAGE_DISPLAY_TARGET |
63 PIPE_TEXTURE_USAGE_RENDER_TARGET |
64 PIPE_TEXTURE_USAGE_SAMPLER);
65 }
66
67 return pipe->screen->texture_create(pipe->screen, &templ);
68 }
69
70 /**
71 * Allocate a renderbuffer for a an on-screen window (not a user-created
72 * renderbuffer). The window system code determines the format.
73 */
74 static struct st_renderbuffer *
75 st_new_renderbuffer_fb(enum pipe_format format)
76 {
77 struct st_renderbuffer *strb;
78
79 strb = CALLOC_STRUCT(st_renderbuffer);
80 if (!strb) {
81 /*_vega_error(NULL, VG_OUT_OF_MEMORY, "creating renderbuffer");*/
82 return NULL;
83 }
84
85 strb->format = format;
86
87 return strb;
88 }
89
90
91 /**
92 * This is called to allocate the original drawing surface, and
93 * during window resize.
94 */
95 static VGboolean
96 st_renderbuffer_alloc_storage(struct vg_context * ctx,
97 struct st_renderbuffer *strb,
98 VGuint width, VGuint height)
99 {
100 struct pipe_context *pipe = ctx->pipe;
101 unsigned surface_usage;
102
103 /* Free the old surface and texture
104 */
105 pipe_surface_reference(&strb->surface, NULL);
106 pipe_texture_reference(&strb->texture, NULL);
107
108
109 /* Probably need dedicated flags for surface usage too:
110 */
111 surface_usage = (PIPE_BUFFER_USAGE_GPU_READ |
112 PIPE_BUFFER_USAGE_GPU_WRITE);
113
114 strb->texture = create_texture(pipe, strb->format,
115 width, height);
116
117 if (!strb->texture)
118 return FALSE;
119
120 strb->surface = pipe->screen->get_tex_surface(pipe->screen,
121 strb->texture,
122 0, 0, 0,
123 surface_usage);
124 strb->width = width;
125 strb->height = height;
126
127 assert(strb->surface->width == width);
128 assert(strb->surface->height == height);
129
130 return strb->surface != NULL;
131 }
132
133 struct vg_context * st_create_context(struct pipe_context *pipe,
134 const void *visual,
135 struct vg_context *share)
136 {
137 struct vg_context *ctx = vg_create_context(pipe, visual, share);
138 /*debug_printf("--------- CREATE CONTEXT %p\n", ctx);*/
139 return ctx;
140 }
141
142 void st_destroy_context(struct vg_context *st)
143 {
144 /*debug_printf("--------- DESTROY CONTEXT %p\n", st);*/
145 vg_destroy_context(st);
146 }
147
148 void st_copy_context_state(struct vg_context *dst, struct vg_context *src,
149 uint mask)
150 {
151 fprintf(stderr, "FIXME: %s\n", __FUNCTION__);
152 }
153
154 void st_get_framebuffer_dimensions(struct st_framebuffer *stfb,
155 uint *width,
156 uint *height)
157 {
158 *width = stfb->strb->width;
159 *height = stfb->strb->height;
160 }
161
162 struct st_framebuffer * st_create_framebuffer(const void *visual,
163 enum pipe_format colorFormat,
164 enum pipe_format depthFormat,
165 enum pipe_format stencilFormat,
166 uint width, uint height,
167 void *privateData)
168 {
169 struct st_framebuffer *stfb = CALLOC_STRUCT(st_framebuffer);
170 if (stfb) {
171 struct st_renderbuffer *rb =
172 st_new_renderbuffer_fb(colorFormat);
173 stfb->strb = rb;
174 #if 0
175 if (doubleBuffer) {
176 struct st_renderbuffer *rb =
177 st_new_renderbuffer_fb(colorFormat);
178 }
179 #endif
180
181 /* we want to combine the depth/stencil */
182 if (stencilFormat == depthFormat)
183 stfb->dsrb = st_new_renderbuffer_fb(stencilFormat);
184 else
185 stfb->dsrb = st_new_renderbuffer_fb(PIPE_FORMAT_S8Z24_UNORM);
186
187 /*### currently we always allocate it but it's possible it's
188 not necessary if EGL_ALPHA_MASK_SIZE was 0
189 */
190 stfb->alpha_mask = 0;
191
192 stfb->init_width = width;
193 stfb->init_height = height;
194 stfb->privateData = privateData;
195 }
196
197 return stfb;
198 }
199
200 static void setup_new_alpha_mask(struct vg_context *ctx,
201 struct st_framebuffer *stfb,
202 uint width, uint height)
203 {
204 struct pipe_context *pipe = ctx->pipe;
205 struct pipe_texture *old_texture = stfb->alpha_mask;
206
207 /*
208 we use PIPE_FORMAT_A8R8G8B8_UNORM because we want to render to
209 this texture and use it as a sampler, so while this wastes some
210 space it makes both of those a lot simpler
211 */
212 stfb->alpha_mask =
213 create_texture(pipe, PIPE_FORMAT_A8R8G8B8_UNORM, width, height);
214
215 if (!stfb->alpha_mask) {
216 if (old_texture)
217 pipe_texture_reference(&old_texture, NULL);
218 return;
219 }
220
221 vg_validate_state(ctx);
222
223 /* alpha mask starts with 1.f alpha */
224 mask_fill(0, 0, width, height, 1.f);
225
226 /* if we had an old surface copy it over */
227 if (old_texture) {
228 struct pipe_surface *surface = pipe->screen->get_tex_surface(
229 pipe->screen,
230 stfb->alpha_mask,
231 0, 0, 0,
232 PIPE_BUFFER_USAGE_GPU_WRITE);
233 struct pipe_surface *old_surface = pipe->screen->get_tex_surface(
234 pipe->screen,
235 old_texture,
236 0, 0, 0,
237 PIPE_BUFFER_USAGE_GPU_READ);
238 pipe->surface_copy(pipe,
239 surface,
240 0, 0,
241 old_surface,
242 0, 0,
243 MIN2(old_surface->width, width),
244 MIN2(old_surface->height, height));
245 if (surface)
246 pipe_surface_reference(&surface, NULL);
247 if (old_surface)
248 pipe_surface_reference(&old_surface, NULL);
249 }
250
251 /* Free the old texture
252 */
253 if (old_texture)
254 pipe_texture_reference(&old_texture, NULL);
255 }
256
257 void st_resize_framebuffer(struct st_framebuffer *stfb,
258 uint width, uint height)
259 {
260 struct vg_context *ctx = vg_current_context();
261 struct st_renderbuffer *strb = stfb->strb;
262 struct pipe_framebuffer_state *state;
263
264 if (!ctx)
265 return;
266
267 state = &ctx->state.g3d.fb;
268
269 /* If this is a noop, exit early and don't do the clear, etc below.
270 */
271 if (strb->width == width &&
272 strb->height == height &&
273 state->zsbuf)
274 return;
275
276 if (strb->width != width || strb->height != height)
277 st_renderbuffer_alloc_storage(ctx, strb,
278 width, height);
279
280 if (stfb->dsrb->width != width || stfb->dsrb->height != height)
281 st_renderbuffer_alloc_storage(ctx, stfb->dsrb,
282 width, height);
283
284 {
285 VGuint i;
286
287 memset(state, 0, sizeof(struct pipe_framebuffer_state));
288
289 state->width = width;
290 state->height = height;
291
292 state->nr_cbufs = 1;
293 state->cbufs[0] = strb->surface;
294 for (i = 1; i < PIPE_MAX_COLOR_BUFS; ++i)
295 state->cbufs[i] = 0;
296
297 state->zsbuf = stfb->dsrb->surface;
298
299 cso_set_framebuffer(ctx->cso_context, state);
300 }
301
302 ctx->state.dirty |= VIEWPORT_DIRTY;
303 ctx->state.dirty |= DEPTH_STENCIL_DIRTY;/*to reset the scissors*/
304
305 ctx->pipe->clear(ctx->pipe, PIPE_CLEAR_DEPTHSTENCIL,
306 NULL, 0.0, 0);
307
308 /* we need all the other state already set */
309
310 setup_new_alpha_mask(ctx, stfb, width, height);
311
312 pipe_texture_reference( &stfb->blend_texture, NULL );
313 stfb->blend_texture = create_texture(ctx->pipe, PIPE_FORMAT_A8R8G8B8_UNORM,
314 width, height);
315 }
316
317 void st_set_framebuffer_surface(struct st_framebuffer *stfb,
318 uint surfIndex, struct pipe_surface *surf)
319 {
320 struct st_renderbuffer *rb = stfb->strb;
321
322 /* unreference existing surfaces */
323 pipe_surface_reference( &rb->surface, NULL );
324 pipe_texture_reference( &rb->texture, NULL );
325
326 /* reference new ones */
327 pipe_surface_reference( &rb->surface, surf );
328 pipe_texture_reference( &rb->texture, surf->texture );
329
330 rb->width = surf->width;
331 rb->height = surf->height;
332 }
333
334 int st_get_framebuffer_surface(struct st_framebuffer *stfb,
335 uint surfIndex, struct pipe_surface **surf)
336 {
337 struct st_renderbuffer *rb = stfb->strb;
338 *surf = rb->surface;
339 return VG_TRUE;
340 }
341
342 int st_get_framebuffer_texture(struct st_framebuffer *stfb,
343 uint surfIndex, struct pipe_texture **tex)
344 {
345 struct st_renderbuffer *rb = stfb->strb;
346 *tex = rb->texture;
347 return VG_TRUE;
348 }
349
350 void * st_framebuffer_private(struct st_framebuffer *stfb)
351 {
352 return stfb->privateData;
353 }
354
355 void st_unreference_framebuffer(struct st_framebuffer *stfb)
356 {
357 /* FIXME */
358 }
359
360 void st_make_current(struct vg_context *st,
361 struct st_framebuffer *draw,
362 struct st_framebuffer *read)
363 {
364 vg_set_current_context(st);
365 if (st) {
366 st->draw_buffer = draw;
367 }
368 }
369
370 struct vg_context *st_get_current(void)
371 {
372 return vg_current_context();
373 }
374
375 void st_flush(struct vg_context *st, uint pipeFlushFlags,
376 struct pipe_fence_handle **fence)
377 {
378 st->pipe->flush(st->pipe, pipeFlushFlags, fence);
379 }
380
381 void st_finish(struct vg_context *st)
382 {
383 struct pipe_fence_handle *fence = NULL;
384
385 st_flush(st, PIPE_FLUSH_RENDER_CACHE, &fence);
386
387 st->pipe->screen->fence_finish(st->pipe->screen, fence, 0);
388 st->pipe->screen->fence_reference(st->pipe->screen, &fence, NULL);
389 }
390
391 void st_notify_swapbuffers(struct st_framebuffer *stfb)
392 {
393 struct vg_context *ctx = vg_current_context();
394 if (ctx && ctx->draw_buffer == stfb) {
395 st_flush(ctx,
396 PIPE_FLUSH_RENDER_CACHE |
397 PIPE_FLUSH_SWAPBUFFERS |
398 PIPE_FLUSH_FRAME,
399 NULL);
400 }
401 }
402
403 void st_notify_swapbuffers_complete(struct st_framebuffer *stfb)
404 {
405 }
406
407 int st_bind_texture_surface(struct pipe_surface *ps, int target, int level,
408 enum pipe_format format)
409 {
410 return 0;
411 }
412
413 int st_unbind_texture_surface(struct pipe_surface *ps, int target, int level)
414 {
415 return 0;
416 }