gallium/targets: Break haiku state_tracker out to own directory
[mesa.git] / src / gallium / targets / haiku-softpipe / GalliumContext.cpp
index ff420b9af269ee21341222ee14c07d386e3bce87..c740458bc7253ed371c8d9c6fce688788e094fed 100644 (file)
@@ -18,6 +18,7 @@ extern "C" {
 #include "main/context.h"
 #include "main/framebuffer.h"
 #include "main/renderbuffer.h"
+#include "main/viewport.h"
 #include "pipe/p_format.h"
 #include "state_tracker/st_cb_fbo.h"
 #include "state_tracker/st_cb_flush.h"
@@ -25,6 +26,7 @@ extern "C" {
 #include "state_tracker/st_gl_api.h"
 #include "state_tracker/st_manager.h"
 #include "state_tracker/sw_winsys.h"
+#include "util/u_memory.h"
 #include "hgl_sw_winsys.h"
 
 #include "target-helpers/inline_sw_helper.h"
@@ -43,17 +45,23 @@ extern "C" {
 
 
 static void
-hgl_viewport(struct gl_context* glContext, GLint x, GLint y,
-       GLsizei width, GLsizei height)
+hgl_viewport(struct gl_context* glContext)
 {
+       // TODO: We should try to eliminate this function
+
+       GLint x = glContext->ViewportArray[0].X;
+       GLint y = glContext->ViewportArray[0].Y;
+       GLint width = glContext->ViewportArray[0].Width;
+       GLint height = glContext->ViewportArray[0].Height;
+
        TRACE("%s(glContext: %p, x: %d, y: %d, w: %d, h: %d\n", __func__,
                glContext, x, y, width, height);
 
+       _mesa_check_init_viewport(glContext, width, height);
+
        struct gl_framebuffer *draw = glContext->WinSysDrawBuffer;
        struct gl_framebuffer *read = glContext->WinSysReadBuffer;
 
-       // TODO: SLOW! We need to check for changes in bitmap vs gl_framebuffer
-       // size before doing a _mesa_resize_framebuffer.
        if (draw)
                _mesa_resize_framebuffer(glContext, draw, width, height);
        if (read)
@@ -61,70 +69,6 @@ hgl_viewport(struct gl_context* glContext, GLint x, GLint y,
 }
 
 
-static st_visual*
-hgl_fill_st_visual(gl_config* glVisual)
-{
-       struct st_visual* stVisual = CALLOC_STRUCT(st_visual);
-       if (!stVisual) {
-               ERROR("%s: Couldn't allocate st_visual\n", __func__);
-               return NULL;
-       }
-
-       // Determine color format
-       if (glVisual->redBits == 8) {
-               if (glVisual->alphaBits == 8)
-                       stVisual->color_format = PIPE_FORMAT_A8R8G8B8_UNORM;
-               else
-                       stVisual->color_format = PIPE_FORMAT_X8R8G8B8_UNORM;
-       } else {
-               // TODO: I think this should be RGB vs BGR
-               stVisual->color_format = PIPE_FORMAT_B5G6R5_UNORM;
-       }
-
-       // Determine depth stencil format
-       switch (glVisual->depthBits) {
-               default:
-               case 0:
-                       stVisual->depth_stencil_format = PIPE_FORMAT_NONE;
-                       break;
-               case 16:
-                       stVisual->depth_stencil_format = PIPE_FORMAT_Z16_UNORM;
-                       break;
-               case 24:
-                       if (glVisual->stencilBits == 0) {
-                               stVisual->depth_stencil_format = PIPE_FORMAT_X8Z24_UNORM;
-                       } else {
-                               stVisual->depth_stencil_format = PIPE_FORMAT_S8_UINT_Z24_UNORM;
-                       }
-                       break;
-               case 32:
-                       stVisual->depth_stencil_format = PIPE_FORMAT_Z32_UNORM;
-                       break;
-       }
-
-       stVisual->accum_format = (glVisual->haveAccumBuffer)
-               ? PIPE_FORMAT_R16G16B16A16_SNORM : PIPE_FORMAT_NONE;
-
-       stVisual->buffer_mask |= ST_ATTACHMENT_FRONT_LEFT_MASK;
-       stVisual->render_buffer = ST_ATTACHMENT_FRONT_LEFT;
-       if (glVisual->doubleBufferMode) {
-               stVisual->buffer_mask |= ST_ATTACHMENT_BACK_LEFT_MASK;
-               stVisual->render_buffer = ST_ATTACHMENT_BACK_LEFT;
-       }
-
-       if (glVisual->stereoMode) {
-               stVisual->buffer_mask |= ST_ATTACHMENT_FRONT_RIGHT_MASK;
-               if (glVisual->doubleBufferMode)
-                       stVisual->buffer_mask |= ST_ATTACHMENT_BACK_RIGHT_MASK;
-       }
-
-       if (glVisual->haveDepthBuffer || glVisual->haveStencilBuffer)
-               stVisual->buffer_mask |= ST_ATTACHMENT_DEPTH_STENCIL_MASK;
-
-       return stVisual;
-}
-
-
 static int
 hook_stm_get_param(struct st_manager *smapi, enum st_manager_param param)
 {
@@ -145,8 +89,8 @@ hook_stm_get_param(struct st_manager *smapi, enum st_manager_param param)
 GalliumContext::GalliumContext(ulong options)
        :
        fOptions(options),
-       fCurrentContext(0),
-       fScreen(NULL)
+       fScreen(NULL),
+       fCurrentContext(0)
 {
        CALLED();
 
@@ -165,10 +109,10 @@ GalliumContext::~GalliumContext()
        CALLED();
 
        // Destroy our contexts
-       pipe_mutex_lock(fMutex);
+       Lock();
        for (context_id i = 0; i < CONTEXT_MAX; i++)
                DestroyContext(i);
-       pipe_mutex_unlock(fMutex);
+       Unlock();
 
        pipe_mutex_destroy(fMutex);
 
@@ -176,6 +120,95 @@ GalliumContext::~GalliumContext()
 }
 
 
+struct st_visual*
+GalliumContext::CreateVisual()
+{
+       struct st_visual* visual = CALLOC_STRUCT(st_visual);
+       if (!visual) {
+               ERROR("%s: Couldn't allocate st_visual\n", __func__);
+               return NULL;
+       }
+
+       // Calculate visual configuration
+       const GLboolean rgbFlag         = ((fOptions & BGL_INDEX) == 0);
+       const GLboolean alphaFlag       = ((fOptions & BGL_ALPHA) == BGL_ALPHA);
+       const GLboolean dblFlag         = ((fOptions & BGL_DOUBLE) == BGL_DOUBLE);
+       const GLboolean stereoFlag      = false;
+       const GLint depth                       = (fOptions & BGL_DEPTH) ? 24 : 0;
+       const GLint stencil                     = (fOptions & BGL_STENCIL) ? 8 : 0;
+       const GLint accum                       = (fOptions & BGL_ACCUM) ? 16 : 0;
+       const GLint red                         = rgbFlag ? 8 : 5;
+       const GLint green                       = rgbFlag ? 8 : 5;
+       const GLint blue                        = rgbFlag ? 8 : 5;
+       const GLint alpha                       = alphaFlag ? 8 : 0;
+
+       TRACE("rgb      :\t%d\n", (bool)rgbFlag);
+       TRACE("alpha    :\t%d\n", (bool)alphaFlag);
+       TRACE("dbl      :\t%d\n", (bool)dblFlag);
+       TRACE("stereo   :\t%d\n", (bool)stereoFlag);
+       TRACE("depth    :\t%d\n", depth);
+       TRACE("stencil  :\t%d\n", stencil);
+       TRACE("accum    :\t%d\n", accum);
+       TRACE("red      :\t%d\n", red);
+       TRACE("green    :\t%d\n", green);
+       TRACE("blue     :\t%d\n", blue);
+       TRACE("alpha    :\t%d\n", alpha);
+
+       // Determine color format
+       if (red == 8) {
+               if (alpha == 8)
+                       visual->color_format = PIPE_FORMAT_A8R8G8B8_UNORM;
+               else
+                       visual->color_format = PIPE_FORMAT_X8R8G8B8_UNORM;
+       } else {
+               // TODO: I think this should be RGB vs BGR
+               visual->color_format = PIPE_FORMAT_B5G6R5_UNORM;
+       }
+
+       // Determine depth stencil format
+       switch (depth) {
+               default:
+               case 0:
+                       visual->depth_stencil_format = PIPE_FORMAT_NONE;
+                       break;
+               case 16:
+                       visual->depth_stencil_format = PIPE_FORMAT_Z16_UNORM;
+                       break;
+               case 24:
+                       if ((fOptions & BGL_STENCIL) != 0)
+                               visual->depth_stencil_format = PIPE_FORMAT_S8_UINT_Z24_UNORM;
+                       else
+                               visual->depth_stencil_format = PIPE_FORMAT_X8Z24_UNORM;
+                       break;
+               case 32:
+                       visual->depth_stencil_format = PIPE_FORMAT_Z32_UNORM;
+                       break;
+       }
+
+       visual->accum_format = (fOptions & BGL_ACCUM)
+               ? PIPE_FORMAT_R16G16B16A16_SNORM : PIPE_FORMAT_NONE;
+
+       visual->buffer_mask |= ST_ATTACHMENT_FRONT_LEFT_MASK;
+       visual->render_buffer = ST_ATTACHMENT_FRONT_LEFT;
+
+       if (dblFlag) {
+               visual->buffer_mask |= ST_ATTACHMENT_BACK_LEFT_MASK;
+               visual->render_buffer = ST_ATTACHMENT_BACK_LEFT;
+       }
+
+       if (stereoFlag) {
+               visual->buffer_mask |= ST_ATTACHMENT_FRONT_RIGHT_MASK;
+               if (dblFlag)
+                       visual->buffer_mask |= ST_ATTACHMENT_BACK_RIGHT_MASK;
+       }
+
+       if ((fOptions & BGL_DEPTH) || (fOptions & BGL_STENCIL)) 
+               visual->buffer_mask |= ST_ATTACHMENT_DEPTH_STENCIL_MASK;
+
+       return visual;
+}
+
+
 status_t
 GalliumContext::CreateScreen()
 {
@@ -238,51 +271,19 @@ GalliumContext::CreateContext(Bitmap *bitmap)
        }
        context->manager->get_param = hook_stm_get_param;
 
-       // Calculate visual configuration
-       const GLboolean rgbFlag         = ((fOptions & BGL_INDEX) == 0);
-       const GLboolean alphaFlag       = ((fOptions & BGL_ALPHA) == BGL_ALPHA);
-       const GLboolean dblFlag         = ((fOptions & BGL_DOUBLE) == BGL_DOUBLE);
-       const GLboolean stereoFlag      = false;
-       const GLint depth                       = (fOptions & BGL_DEPTH) ? 24 : 0;
-       const GLint stencil                     = (fOptions & BGL_STENCIL) ? 8 : 0;
-       const GLint accum                       = (fOptions & BGL_ACCUM) ? 16 : 0;
-       const GLint red                         = rgbFlag ? 8 : 5;
-       const GLint green                       = rgbFlag ? 8 : 5;
-       const GLint blue                        = rgbFlag ? 8 : 5;
-       const GLint alpha                       = alphaFlag ? 8 : 0;
-
-       TRACE("rgb      :\t%d\n", (bool)rgbFlag);
-       TRACE("alpha    :\t%d\n", (bool)alphaFlag);
-       TRACE("dbl      :\t%d\n", (bool)dblFlag);
-       TRACE("stereo   :\t%d\n", (bool)stereoFlag);
-       TRACE("depth    :\t%d\n", depth);
-       TRACE("stencil  :\t%d\n", stencil);
-       TRACE("accum    :\t%d\n", accum);
-       TRACE("red      :\t%d\n", red);
-       TRACE("green    :\t%d\n", green);
-       TRACE("blue     :\t%d\n", blue);
-       TRACE("alpha    :\t%d\n", alpha);
-
-       gl_config* glVisual = _mesa_create_visual(dblFlag, stereoFlag, red, green,
-               blue, alpha, depth, stencil, accum, accum, accum, alpha ? accum : 0, 1);
-
-       if (!glVisual) {
-               ERROR("%s: Couldn't create Mesa visual!\n", __func__);
+       // Create state tracker visual
+       context->stVisual = CreateVisual();
+       if (context->stVisual == NULL) {
+               ERROR("%s: Couldn't create state_tracker visual!\n", __func__);
                return -1;
        }
 
-       TRACE("depthBits   :\t%d\n", glVisual->depthBits);
-       TRACE("stencilBits :\t%d\n", glVisual->stencilBits);
-
-       // Convert Mesa calculated visual into state tracker visual
-       context->stVisual = hgl_fill_st_visual(glVisual);
-
-       context->draw = new GalliumFramebuffer(context->stVisual, (void*)this);
-       context->read = new GalliumFramebuffer(context->stVisual, (void*)this);
+       context->draw = hgl_create_st_framebuffer(context);
+       context->read = hgl_create_st_framebuffer(context);
 
        if (!context->draw || !context->read) {
                ERROR("%s: Problem allocating framebuffer!\n", __func__);
-               _mesa_destroy_visual(glVisual);
+               FREE(context->stVisual);
                return -1;
        }
 
@@ -333,24 +334,25 @@ GalliumContext::CreateContext(Bitmap *bitmap)
                                break;
                }
 
+               FREE(context->stVisual);
                FREE(context);
                return -1;
        }
 
-       // Init Gallium3D Post Processing
-       //context->postProcess = pp_init(fScreen, context->postProcessEnable);
-
        assert(!context->st->st_manager_private);
-       context->st->st_manager_private = (void*)this;
+       context->st->st_manager_private = (void*)context;
 
        struct st_context *stContext = (struct st_context*)context->st;
        
        stContext->ctx->Driver.Viewport = hgl_viewport;
 
-       // TODO: Closely review this next context logic...
-       context_id contextNext = -1;
+       // Init Gallium3D Post Processing
+       // TODO: no pp filters are enabled yet through postProcessEnable
+       context->postProcess = pp_init(stContext->pipe, context->postProcessEnable,
+               stContext->cso_context);
 
-       pipe_mutex_lock(fMutex);
+       context_id contextNext = -1;
+       Lock();
        for (context_id i = 0; i < CONTEXT_MAX; i++) {
                if (fContext[i] == NULL) {
                        fContext[i] = context;
@@ -358,14 +360,14 @@ GalliumContext::CreateContext(Bitmap *bitmap)
                        break;
                }
        }
-       pipe_mutex_unlock(fMutex);
+       Unlock();
 
        if (contextNext < 0) {
                ERROR("%s: The next context is invalid... something went wrong!\n",
                        __func__);
                //st_destroy_context(context->st);
+               FREE(context->stVisual);
                FREE(context);
-               _mesa_destroy_visual(glVisual);
                return -1;
        }
 
@@ -419,10 +421,10 @@ GalliumContext::SetCurrentContext(Bitmap *bitmap, context_id contextID)
                return B_ERROR;
        }
 
-       pipe_mutex_lock(fMutex);
+       Lock();
        context_id oldContextID = fCurrentContext;
        struct hgl_context* context = fContext[contextID];
-       pipe_mutex_unlock(fMutex);
+       Unlock();
 
        if (!context) {
                ERROR("%s: Invalid context provided (#%" B_PRIu64 ")!\n",
@@ -446,19 +448,17 @@ GalliumContext::SetCurrentContext(Bitmap *bitmap, context_id contextID)
        }
 
        // We need to lock and unlock framebuffers before accessing them
-       context->draw->Lock();
-       context->read->Lock();
-       api->make_current(context->api, context->st, context->draw->fBuffer,
-               context->read->fBuffer);
-       context->draw->Unlock();
-       context->read->Unlock();
-
-       // TODO: Init textures before post-processing them
-       #if 0
-       pp_init_fbos(context->postProcess,
-               context->textures[ST_ATTACHMENT_BACK_LEFT]->width0,
-               context->textures[ST_ATTACHMENT_BACK_LEFT]->height0);
-       #endif
+       api->make_current(context->api, context->st, context->draw->stfbi,
+               context->read->stfbi);
+
+       if (context->textures[ST_ATTACHMENT_BACK_LEFT]
+               && context->textures[ST_ATTACHMENT_DEPTH_STENCIL]
+               && context->postProcess) {
+               TRACE("Postprocessing textures...\n");
+               pp_init_fbos(context->postProcess,
+                       context->textures[ST_ATTACHMENT_BACK_LEFT]->width0,
+                       context->textures[ST_ATTACHMENT_BACK_LEFT]->height0);
+       }
 
        context->bitmap = bitmap;
        //context->st->pipe->priv = context;
@@ -472,9 +472,9 @@ GalliumContext::SwapBuffers(context_id contextID)
 {
        CALLED();
 
-       pipe_mutex_lock(fMutex);
+       Lock();
        struct hgl_context *context = fContext[contextID];
-       pipe_mutex_unlock(fMutex);
+       Unlock();
 
        if (!context) {
                ERROR("%s: context not found\n", __func__);
@@ -482,7 +482,7 @@ GalliumContext::SwapBuffers(context_id contextID)
        }
 
        // TODO: Where did st_notify_swapbuffers go?
-       //st_notify_swapbuffers(context->draw->stfb);
+       //st_notify_swapbuffers(context->draw->stfbi);
 
        context->st->flush(context->st, ST_FLUSH_FRONT, NULL);
 
@@ -501,16 +501,46 @@ GalliumContext::SwapBuffers(context_id contextID)
                // We pass our destination bitmap to flush_fronbuffer which passes it
                // to the private winsys display call.
                fScreen->flush_frontbuffer(fScreen, surface->texture, 0, 0,
-                       context->bitmap);
+                       context->bitmap, NULL);
        }
 
        #if 0
        // TODO... should we flush the z stencil buffer?
        pipe_surface* zSurface = stContext->state.framebuffer.zsbuf;
-       fScreen->flush_frontbuffer(fScreen, surface->texture, 0, 0,
-               context->bitmap);
+       fScreen->flush_frontbuffer(fScreen, zSurface->texture, 0, 0,
+               context->bitmap, NULL);
        #endif
 
        return B_OK;
 }
+
+
+void
+GalliumContext::ResizeViewport(int32 width, int32 height)
+{
+       CALLED();
+       for (context_id i = 0; i < CONTEXT_MAX; i++) {
+               if (fContext[i] && fContext[i]->st) {
+                       struct st_context *stContext = (struct st_context*)fContext[i]->st;
+                       _mesa_set_viewport(stContext->ctx, 0, 0, 0, width, height);
+                       st_manager_validate_framebuffers(stContext);
+               }
+       }
+}
+
+
+void
+GalliumContext::Lock()
+{
+       CALLED();
+       pipe_mutex_lock(fMutex);
+}
+
+
+void
+GalliumContext::Unlock()
+{
+       CALLED();
+       pipe_mutex_unlock(fMutex);
+}
 /* vim: set tabstop=4: */