dri: Add DRI entrypoints to create a context for a given API
[mesa.git] / src / mesa / drivers / dri / nouveau / nouveau_context.c
index 6117f68bcf76938c91bca51d767e2697c16c0529..f481161d468e221b74af0cd2482faf16fd7a71a8 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2009 Francisco Jerez.
+ * Copyright (C) 2009-2010 Francisco Jerez.
  * All Rights Reserved.
  *
  * Permission is hereby granted, free of charge, to any person obtaining
 #include "drivers/common/meta.h"
 #include "drivers/common/driverfuncs.h"
 #include "swrast/swrast.h"
+#include "swrast/s_context.h"
 #include "vbo/vbo.h"
 #include "tnl/tnl.h"
+#include "tnl/t_context.h"
 
 #define need_GL_EXT_framebuffer_object
 #define need_GL_EXT_fog_coord
+#define need_GL_EXT_secondary_color
 
 #include "main/remap_helper.h"
 
 static const struct dri_extension nouveau_extensions[] = {
-       { "GL_EXT_framebuffer_object",  GL_EXT_framebuffer_object_functions },
        { "GL_ARB_multitexture",        NULL },
-       { "GL_EXT_texture_lod_bias",    NULL },
-       { "GL_SGIS_generate_mipmap",    NULL },
+       { "GL_ARB_texture_env_add",     NULL },
        { "GL_ARB_texture_env_combine", NULL },
        { "GL_ARB_texture_env_dot3",    NULL },
-       { "GL_ARB_texture_env_add",     NULL },
+       { "GL_ARB_texture_mirrored_repeat", NULL },
        { "GL_EXT_fog_coord",           GL_EXT_fog_coord_functions },
+       { "GL_EXT_framebuffer_blit",    NULL },
+       { "GL_EXT_framebuffer_object",  GL_EXT_framebuffer_object_functions },
+       { "GL_EXT_secondary_color",     GL_EXT_secondary_color_functions },
+       { "GL_EXT_stencil_wrap",        NULL },
+       { "GL_EXT_texture_lod_bias",    NULL },
+       { "GL_NV_blend_square",         NULL },
+       { "GL_SGIS_generate_mipmap",    NULL },
        { NULL,                         NULL }
 };
 
+static void
+nouveau_channel_flush_notify(struct nouveau_channel *chan)
+{
+       struct nouveau_context *nctx = chan->user_private;
+       GLcontext *ctx = &nctx->base;
+
+       if (nctx->fallback < SWRAST)
+               nouveau_bo_state_emit(ctx);
+}
+
 GLboolean
-nouveau_context_create(const __GLcontextModes *visual, __DRIcontext *dri_ctx,
+nouveau_context_create(gl_api api,
+                      const __GLcontextModes *visual, __DRIcontext *dri_ctx,
                       void *share_ctx)
 {
        __DRIscreen *dri_screen = dri_ctx->driScreenPriv;
@@ -82,18 +101,19 @@ nouveau_context_init(GLcontext *ctx, struct nouveau_screen *screen,
 {
        struct nouveau_context *nctx = to_nouveau_context(ctx);
        struct dd_function_table functions;
+       int ret;
 
        nctx->screen = screen;
        nctx->fallback = HWTNL;
 
-       /* Initialize the function pointers */
+       /* Initialize the function pointers. */
        _mesa_init_driver_functions(&functions);
        nouveau_driver_functions_init(&functions);
        nouveau_bufferobj_functions_init(&functions);
        nouveau_texture_functions_init(&functions);
        nouveau_fbo_functions_init(&functions);
 
-       /* Initialize the mesa context */
+       /* Initialize the mesa context. */
        _mesa_initialize_context(ctx, visual, share_ctx, &functions, NULL);
 
        nouveau_state_init(ctx);
@@ -105,42 +125,69 @@ nouveau_context_init(GLcontext *ctx, struct nouveau_screen *screen,
        nouveau_span_functions_init(ctx);
        _mesa_allow_light_in_model(ctx, GL_FALSE);
 
-       /* Enable any supported extensions */
+       /* Allocate a hardware channel. */
+       ret = nouveau_channel_alloc(context_dev(ctx), 0xbeef0201, 0xbeef0202,
+                                   &nctx->hw.chan);
+       if (ret) {
+               nouveau_error("Error initializing the FIFO.\n");
+               return GL_FALSE;
+       }
+
+       nctx->hw.chan->flush_notify = nouveau_channel_flush_notify;
+       nctx->hw.chan->user_private = nctx;
+
+       /* Enable any supported extensions. */
        driInitExtensions(ctx, nouveau_extensions, GL_TRUE);
 
        return GL_TRUE;
 }
 
+void
+nouveau_context_deinit(GLcontext *ctx)
+{
+       struct nouveau_context *nctx = to_nouveau_context(ctx);
+
+       if (TNL_CONTEXT(ctx))
+               _tnl_DestroyContext(ctx);
+
+       if (vbo_context(ctx))
+               _vbo_DestroyContext(ctx);
+
+       if (SWRAST_CONTEXT(ctx))
+               _swrast_DestroyContext(ctx);
+
+       if (ctx->Meta)
+               _mesa_meta_free(ctx);
+
+       if (nctx->hw.chan)
+               nouveau_channel_free(&nctx->hw.chan);
+
+       nouveau_bo_state_destroy(ctx);
+       _mesa_free_context_data(ctx);
+}
+
 void
 nouveau_context_destroy(__DRIcontext *dri_ctx)
 {
        struct nouveau_context *nctx = dri_ctx->driverPrivate;
        GLcontext *ctx = &nctx->base;
 
-       if (nctx->screen->context == nctx)
-               nctx->screen->context = NULL;
-
-       _tnl_DestroyContext(ctx);
-       _vbo_DestroyContext(ctx);
-       _swrast_DestroyContext(ctx);
-       _mesa_meta_free(ctx);
-       nouveau_bo_state_destroy(ctx);
        context_drv(ctx)->context_destroy(ctx);
 }
 
-static void
-nouveau_update_renderbuffers(__DRIcontext *context, __DRIdrawable *drawable,
-                            unsigned int *stamp)
+void
+nouveau_update_renderbuffers(__DRIcontext *dri_ctx, __DRIdrawable *draw)
 {
-       struct nouveau_context *nctx = context->driverPrivate;
-       GLcontext *ctx = &nctx->base;
-       __DRIscreen *screen = context->driScreenPriv;
-       struct gl_framebuffer *fb = drawable->driverPrivate;
+       GLcontext *ctx = dri_ctx->driverPrivate;
+       __DRIscreen *screen = dri_ctx->driScreenPriv;
+       struct gl_framebuffer *fb = draw->driverPrivate;
        unsigned int attachments[10];
        __DRIbuffer *buffers = NULL;
        int i = 0, count, ret;
 
-       *stamp = *drawable->pStamp;
+       if (draw->lastStamp == *draw->pStamp)
+               return;
+       draw->lastStamp = *draw->pStamp;
 
        attachments[i++] = __DRI_BUFFER_FRONT_LEFT;
        if (fb->Visual.doubleBufferMode)
@@ -152,10 +199,9 @@ nouveau_update_renderbuffers(__DRIcontext *context, __DRIdrawable *drawable,
        else if (fb->Visual.haveStencilBuffer)
                attachments[i++] = __DRI_BUFFER_STENCIL;
 
-       buffers = (*screen->dri2.loader->getBuffers)(drawable,
-                                                    &drawable->w, &drawable->h,
+       buffers = (*screen->dri2.loader->getBuffers)(draw, &draw->w, &draw->h,
                                                     attachments, i, &count,
-                                                    drawable->loaderPrivate);
+                                                    draw->loaderPrivate);
        if (buffers == NULL)
                return;
 
@@ -187,8 +233,8 @@ nouveau_update_renderbuffers(__DRIcontext *context, __DRIdrawable *drawable,
                rb = fb->Attachment[index].Renderbuffer;
                s = &to_nouveau_renderbuffer(rb)->surface;
 
-               s->width = drawable->w;
-               s->height = drawable->h;
+               s->width = draw->w;
+               s->height = draw->h;
                s->pitch = buffers[i].pitch;
                s->cpp = buffers[i].cpp;
 
@@ -204,12 +250,25 @@ nouveau_update_renderbuffers(__DRIcontext *context, __DRIdrawable *drawable,
                        ret = nouveau_bo_handle_ref(context_dev(ctx),
                                                    buffers[i].name, &s->bo);
                        assert(!ret);
-
-                       context_dirty(ctx, FRAMEBUFFER);
                }
        }
 
-       _mesa_resize_framebuffer(ctx, fb, drawable->w, drawable->h);
+       _mesa_resize_framebuffer(NULL, fb, draw->w, draw->h);
+}
+
+static void
+update_framebuffer(__DRIcontext *dri_ctx, __DRIdrawable *draw,
+                  int *stamp)
+{
+       GLcontext *ctx = dri_ctx->driverPrivate;
+       struct gl_framebuffer *fb = draw->driverPrivate;
+
+       *stamp = *draw->pStamp;
+
+       nouveau_update_renderbuffers(dri_ctx, draw);
+       _mesa_resize_framebuffer(ctx, fb, draw->w, draw->h);
+
+       context_dirty(ctx, FRAMEBUFFER);
 }
 
 GLboolean
@@ -220,18 +279,15 @@ nouveau_context_make_current(__DRIcontext *dri_ctx, __DRIdrawable *dri_draw,
                struct nouveau_context *nctx = dri_ctx->driverPrivate;
                GLcontext *ctx = &nctx->base;
 
-               if (nctx->screen->context == nctx)
-                       return GL_TRUE;
-
-               nctx->screen->context = nctx;
-               BITSET_ONES(nctx->dirty);
-
                /* Ask the X server for new renderbuffers. */
-               nouveau_update_renderbuffers(dri_ctx, dri_draw,
-                                            &nctx->drawable.d_stamp);
-               if (dri_draw != dri_read)
-                       nouveau_update_renderbuffers(dri_ctx, dri_read,
-                                                    &nctx->drawable.r_stamp);
+               if (dri_draw->driverPrivate != ctx->WinSysDrawBuffer)
+                       update_framebuffer(dri_ctx, dri_draw,
+                                          &dri_ctx->dri2.draw_stamp);
+
+               if (dri_draw != dri_read &&
+                   dri_read->driverPrivate != ctx->WinSysReadBuffer)
+                       update_framebuffer(dri_ctx, dri_read,
+                                          &dri_ctx->dri2.read_stamp);
 
                /* Pass it down to mesa. */
                _mesa_make_current(ctx, dri_draw->driverPrivate,
@@ -269,30 +325,22 @@ nouveau_fallback(GLcontext *ctx, enum nouveau_fallback mode)
 void
 nouveau_validate_framebuffer(GLcontext *ctx)
 {
-       struct nouveau_context *nctx = to_nouveau_context(ctx);
        __DRIcontext *dri_ctx = to_nouveau_context(ctx)->dri_context;
        __DRIdrawable *dri_draw = dri_ctx->driDrawablePriv;
        __DRIdrawable *dri_read = dri_ctx->driReadablePriv;
 
-       if ((ctx->DrawBuffer->Name == 0 &&
-            nctx->drawable.d_stamp != *dri_draw->pStamp) ||
-           (dri_draw != dri_read &&
-            ctx->ReadBuffer->Name == 0 &&
-            nctx->drawable.r_stamp != *dri_read->pStamp)) {
-               if (nctx->drawable.dirty)
-                       ctx->Driver.Flush(ctx);
+       if (ctx->DrawBuffer->Name == 0 &&
+           dri_ctx->dri2.draw_stamp != *dri_draw->pStamp)
+               update_framebuffer(dri_ctx, dri_draw,
+                                  &dri_ctx->dri2.draw_stamp);
 
-               /* Ask the X server for new renderbuffers. */
-               nouveau_update_renderbuffers(dri_ctx, dri_draw,
-                                            &nctx->drawable.d_stamp);
-               if (dri_draw != dri_read)
-                       nouveau_update_renderbuffers(dri_ctx, dri_read,
-                                                    &nctx->drawable.r_stamp);
-
-               if (nouveau_next_dirty_state(ctx) >= 0)
-                       FIRE_RING(context_chan(ctx));
-       }
+       if (ctx->ReadBuffer->Name == 0 && dri_draw != dri_read &&
+           dri_ctx->dri2.read_stamp != *dri_read->pStamp)
+               update_framebuffer(dri_ctx, dri_read,
+                                  &dri_ctx->dri2.read_stamp);
 
-       /* Someone's planning to draw something really soon. */
-       nctx->drawable.dirty = GL_TRUE;
+       if (nouveau_next_dirty_state(ctx) >= 0) {
+               nouveau_state_emit(ctx);
+               FIRE_RING(context_chan(ctx));
+       }
 }