Tizen 2.1 base
[sdk/emulator/qemu.git] / gl / mesa / src / mesa / drivers / dri / nouveau / nouveau_context.c
1 /*
2  * Copyright (C) 2009-2010 Francisco Jerez.
3  * All Rights Reserved.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining
6  * a 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, sublicense, 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
15  * portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20  * IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
21  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24  *
25  */
26
27 #include <stdbool.h>
28 #include "nouveau_driver.h"
29 #include "nouveau_context.h"
30 #include "nouveau_bufferobj.h"
31 #include "nouveau_fbo.h"
32
33 #include "main/dd.h"
34 #include "main/framebuffer.h"
35 #include "main/light.h"
36 #include "main/state.h"
37 #include "main/version.h"
38 #include "drivers/common/meta.h"
39 #include "drivers/common/driverfuncs.h"
40 #include "swrast/swrast.h"
41 #include "swrast/s_context.h"
42 #include "vbo/vbo.h"
43 #include "tnl/tnl.h"
44 #include "tnl/t_context.h"
45
46 static void
47 nouveau_channel_flush_notify(struct nouveau_channel *chan)
48 {
49         struct nouveau_context *nctx = chan->user_private;
50         struct gl_context *ctx = &nctx->base;
51
52         if (nctx->fallback < SWRAST)
53                 nouveau_bo_state_emit(ctx);
54 }
55
56 GLboolean
57 nouveau_context_create(gl_api api,
58                        const struct gl_config *visual, __DRIcontext *dri_ctx,
59                        unsigned major_version,
60                        unsigned minor_version,
61                        uint32_t flags,
62                        unsigned *error,
63                        void *share_ctx)
64 {
65         __DRIscreen *dri_screen = dri_ctx->driScreenPriv;
66         struct nouveau_screen *screen = dri_screen->driverPrivate;
67         struct nouveau_context *nctx;
68         struct gl_context *ctx;
69
70         /* API and flag filtering is handled in dri2CreateContextAttribs.
71          */
72         (void) api;
73         (void) flags;
74
75         ctx = screen->driver->context_create(screen, visual, share_ctx);
76         if (!ctx) {
77                 *error = __DRI_CTX_ERROR_NO_MEMORY;
78                 return GL_FALSE;
79         }
80
81         nctx = to_nouveau_context(ctx);
82         nctx->dri_context = dri_ctx;
83         dri_ctx->driverPrivate = ctx;
84
85         _mesa_compute_version(ctx);
86         if (ctx->VersionMajor < major_version
87             || (ctx->VersionMajor == major_version
88                 && ctx->VersionMinor < minor_version)) {
89            nouveau_context_destroy(dri_ctx);
90            *error = __DRI_CTX_ERROR_BAD_VERSION;
91            return GL_FALSE;
92         }
93
94         *error = __DRI_CTX_ERROR_SUCCESS;
95         return GL_TRUE;
96 }
97
98 GLboolean
99 nouveau_context_init(struct gl_context *ctx, struct nouveau_screen *screen,
100                      const struct gl_config *visual, struct gl_context *share_ctx)
101 {
102         struct nouveau_context *nctx = to_nouveau_context(ctx);
103         struct dd_function_table functions;
104         int ret;
105
106         nctx->screen = screen;
107         nctx->fallback = HWTNL;
108
109         /* Initialize the function pointers. */
110         _mesa_init_driver_functions(&functions);
111         nouveau_driver_functions_init(&functions);
112         nouveau_bufferobj_functions_init(&functions);
113         nouveau_texture_functions_init(&functions);
114         nouveau_fbo_functions_init(&functions);
115
116         /* Initialize the mesa context. */
117         _mesa_initialize_context(ctx, API_OPENGL, visual,
118                                  share_ctx, &functions, NULL);
119
120         nouveau_state_init(ctx);
121         nouveau_bo_state_init(ctx);
122         nouveau_scratch_init(ctx);
123         _mesa_meta_init(ctx);
124         _swrast_CreateContext(ctx);
125         _vbo_CreateContext(ctx);
126         _tnl_CreateContext(ctx);
127         nouveau_span_functions_init(ctx);
128         _mesa_allow_light_in_model(ctx, GL_FALSE);
129
130         /* Allocate a hardware channel. */
131         ret = nouveau_channel_alloc(context_dev(ctx), 0xbeef0201, 0xbeef0202,
132                                     512*1024, &nctx->hw.chan);
133         if (ret) {
134                 nouveau_error("Error initializing the FIFO.\n");
135                 return GL_FALSE;
136         }
137
138         nctx->hw.chan->flush_notify = nouveau_channel_flush_notify;
139         nctx->hw.chan->user_private = nctx;
140
141         /* Enable any supported extensions. */
142         ctx->Extensions.EXT_blend_color = true;
143         ctx->Extensions.EXT_blend_minmax = true;
144         ctx->Extensions.EXT_fog_coord = true;
145         ctx->Extensions.EXT_framebuffer_blit = true;
146         ctx->Extensions.EXT_framebuffer_object = true;
147         ctx->Extensions.EXT_packed_depth_stencil = true;
148         ctx->Extensions.EXT_secondary_color = true;
149         ctx->Extensions.EXT_texture_filter_anisotropic = true;
150         ctx->Extensions.NV_blend_square = true;
151         ctx->Extensions.NV_texture_env_combine4 = true;
152
153         return GL_TRUE;
154 }
155
156 void
157 nouveau_context_deinit(struct gl_context *ctx)
158 {
159         struct nouveau_context *nctx = to_nouveau_context(ctx);
160
161         if (TNL_CONTEXT(ctx))
162                 _tnl_DestroyContext(ctx);
163
164         if (vbo_context(ctx))
165                 _vbo_DestroyContext(ctx);
166
167         if (SWRAST_CONTEXT(ctx))
168                 _swrast_DestroyContext(ctx);
169
170         if (ctx->Meta)
171                 _mesa_meta_free(ctx);
172
173         if (nctx->hw.chan)
174                 nouveau_channel_free(&nctx->hw.chan);
175
176         nouveau_scratch_destroy(ctx);
177         nouveau_bo_state_destroy(ctx);
178         _mesa_free_context_data(ctx);
179 }
180
181 void
182 nouveau_context_destroy(__DRIcontext *dri_ctx)
183 {
184         struct nouveau_context *nctx = dri_ctx->driverPrivate;
185         struct gl_context *ctx = &nctx->base;
186
187         context_drv(ctx)->context_destroy(ctx);
188 }
189
190 void
191 nouveau_update_renderbuffers(__DRIcontext *dri_ctx, __DRIdrawable *draw)
192 {
193         struct gl_context *ctx = dri_ctx->driverPrivate;
194         struct nouveau_context *nctx = to_nouveau_context(ctx);
195         __DRIscreen *screen = dri_ctx->driScreenPriv;
196         struct gl_framebuffer *fb = draw->driverPrivate;
197         struct nouveau_framebuffer *nfb = to_nouveau_framebuffer(fb);
198         unsigned int attachments[10];
199         __DRIbuffer *buffers = NULL;
200         int i = 0, count, ret;
201
202         if (draw->lastStamp == draw->dri2.stamp)
203                 return;
204         draw->lastStamp = draw->dri2.stamp;
205
206         if (nfb->need_front)
207                 attachments[i++] = __DRI_BUFFER_FRONT_LEFT;
208         if (fb->Visual.doubleBufferMode)
209                 attachments[i++] = __DRI_BUFFER_BACK_LEFT;
210         if (fb->Visual.haveDepthBuffer && fb->Visual.haveStencilBuffer)
211                 attachments[i++] = __DRI_BUFFER_DEPTH_STENCIL;
212         else if (fb->Visual.haveDepthBuffer)
213                 attachments[i++] = __DRI_BUFFER_DEPTH;
214         else if (fb->Visual.haveStencilBuffer)
215                 attachments[i++] = __DRI_BUFFER_STENCIL;
216
217         buffers = (*screen->dri2.loader->getBuffers)(draw, &draw->w, &draw->h,
218                                                      attachments, i, &count,
219                                                      draw->loaderPrivate);
220         if (buffers == NULL)
221                 return;
222
223         for (i = 0; i < count; i++) {
224                 struct gl_renderbuffer *rb;
225                 struct nouveau_surface *s;
226                 uint32_t old_name;
227                 int index;
228
229                 switch (buffers[i].attachment) {
230                 case __DRI_BUFFER_FRONT_LEFT:
231                 case __DRI_BUFFER_FAKE_FRONT_LEFT:
232                         index = BUFFER_FRONT_LEFT;
233                         break;
234                 case __DRI_BUFFER_BACK_LEFT:
235                         index = BUFFER_BACK_LEFT;
236                         break;
237                 case __DRI_BUFFER_DEPTH:
238                 case __DRI_BUFFER_DEPTH_STENCIL:
239                         index = BUFFER_DEPTH;
240                         break;
241                 case __DRI_BUFFER_STENCIL:
242                         index = BUFFER_STENCIL;
243                         break;
244                 default:
245                         assert(0);
246                 }
247
248                 rb = fb->Attachment[index].Renderbuffer;
249                 s = &to_nouveau_renderbuffer(rb)->surface;
250
251                 s->width = draw->w;
252                 s->height = draw->h;
253                 s->pitch = buffers[i].pitch;
254                 s->cpp = buffers[i].cpp;
255
256                 if (index == BUFFER_DEPTH && s->bo) {
257                         ret = nouveau_bo_handle_get(s->bo, &old_name);
258                         /*
259                          * Disable fast Z clears in the next frame, the
260                          * depth buffer contents are undefined.
261                          */
262                         if (!ret && old_name != buffers[i].name)
263                                 nctx->hierz.clear_seq = 0;
264                 }
265
266                 nouveau_bo_ref(NULL, &s->bo);
267                 ret = nouveau_bo_handle_ref(context_dev(ctx),
268                                             buffers[i].name, &s->bo);
269                 assert(!ret);
270         }
271
272         _mesa_resize_framebuffer(ctx, fb, draw->w, draw->h);
273 }
274
275 static void
276 update_framebuffer(__DRIcontext *dri_ctx, __DRIdrawable *draw,
277                    int *stamp)
278 {
279         struct gl_context *ctx = dri_ctx->driverPrivate;
280         struct gl_framebuffer *fb = draw->driverPrivate;
281
282         *stamp = draw->dri2.stamp;
283
284         nouveau_update_renderbuffers(dri_ctx, draw);
285         _mesa_resize_framebuffer(ctx, fb, draw->w, draw->h);
286
287         /* Clean up references to the old framebuffer objects. */
288         context_dirty(ctx, FRAMEBUFFER);
289         context_bctx(ctx, FRAMEBUFFER);
290         FIRE_RING(context_chan(ctx));
291 }
292
293 GLboolean
294 nouveau_context_make_current(__DRIcontext *dri_ctx, __DRIdrawable *dri_draw,
295                              __DRIdrawable *dri_read)
296 {
297         if (dri_ctx) {
298                 struct nouveau_context *nctx = dri_ctx->driverPrivate;
299                 struct gl_context *ctx = &nctx->base;
300
301                 /* Ask the X server for new renderbuffers. */
302                 if (dri_draw->driverPrivate != ctx->WinSysDrawBuffer)
303                         update_framebuffer(dri_ctx, dri_draw,
304                                            &dri_ctx->dri2.draw_stamp);
305
306                 if (dri_draw != dri_read &&
307                     dri_read->driverPrivate != ctx->WinSysReadBuffer)
308                         update_framebuffer(dri_ctx, dri_read,
309                                            &dri_ctx->dri2.read_stamp);
310
311                 /* Pass it down to mesa. */
312                 _mesa_make_current(ctx, dri_draw->driverPrivate,
313                                    dri_read->driverPrivate);
314                 _mesa_update_state(ctx);
315
316         } else {
317                 _mesa_make_current(NULL, NULL, NULL);
318         }
319
320         return GL_TRUE;
321 }
322
323 GLboolean
324 nouveau_context_unbind(__DRIcontext *dri_ctx)
325 {
326         /* Unset current context and dispatch table */
327         _mesa_make_current(NULL, NULL, NULL);
328
329         return GL_TRUE;
330 }
331
332 void
333 nouveau_fallback(struct gl_context *ctx, enum nouveau_fallback mode)
334 {
335         struct nouveau_context *nctx = to_nouveau_context(ctx);
336
337         nctx->fallback = MAX2(HWTNL, mode);
338
339         if (mode < SWRAST) {
340                 nouveau_state_emit(ctx);
341                 nouveau_bo_state_emit(ctx);
342         } else {
343                 FIRE_RING(context_chan(ctx));
344         }
345 }
346
347 static void
348 validate_framebuffer(__DRIcontext *dri_ctx, __DRIdrawable *draw,
349                      int *stamp)
350 {
351         struct gl_framebuffer *fb = draw->driverPrivate;
352         struct nouveau_framebuffer *nfb = to_nouveau_framebuffer(fb);
353         GLboolean need_front =
354                 (fb->_ColorDrawBufferIndexes[0] == BUFFER_FRONT_LEFT ||
355                  fb->_ColorReadBufferIndex == BUFFER_FRONT_LEFT);
356
357         if (nfb->need_front != need_front) {
358                 nfb->need_front = need_front;
359                 dri2InvalidateDrawable(draw);
360         }
361
362         if (draw->dri2.stamp != *stamp)
363                 update_framebuffer(dri_ctx, draw, stamp);
364 }
365
366 void
367 nouveau_validate_framebuffer(struct gl_context *ctx)
368 {
369         __DRIcontext *dri_ctx = to_nouveau_context(ctx)->dri_context;
370         __DRIdrawable *dri_draw = dri_ctx->driDrawablePriv;
371         __DRIdrawable *dri_read = dri_ctx->driReadablePriv;
372
373         if (ctx->DrawBuffer->Name == 0)
374                 validate_framebuffer(dri_ctx, dri_draw,
375                                      &dri_ctx->dri2.draw_stamp);
376
377         if (ctx->ReadBuffer->Name == 0)
378                 validate_framebuffer(dri_ctx, dri_read,
379                                      &dri_ctx->dri2.read_stamp);
380
381         if (ctx->NewState & _NEW_BUFFERS)
382                 _mesa_update_state(ctx);
383 }