st/wgl: Use st_context_iface::share for DrvShareLists.
[mesa.git] / src / gallium / state_trackers / wgl / stw_framebuffer.c
index 8a3e11b6b4075cd71b0765c537a81c0164e41902..259b22f22c73a1a13ab435fd31cd45428ede5f12 100644 (file)
 
 #include <windows.h>
 
-#include "main/context.h"
 #include "pipe/p_format.h"
 #include "pipe/p_screen.h"
-#include "state_tracker/st_context.h"
-#include "state_tracker/st_public.h"
-
-#ifdef DEBUG
-#include "trace/tr_screen.h"
-#include "trace/tr_texture.h"
-#endif
+#include "util/u_format.h"
+#include "util/u_memory.h"
+#include "state_tracker/st_api.h"
 
 #include "stw_icd.h"
 #include "stw_framebuffer.h"
 #include "stw_device.h"
 #include "stw_winsys.h"
 #include "stw_tls.h"
+#include "stw_context.h"
+#include "stw_st.h"
 
 
 /**
@@ -67,8 +64,8 @@ stw_framebuffer_from_hwnd_locked(
 
 /**
  * Destroy this framebuffer. Both stw_dev::fb_mutex and stw_framebuffer::mutex
- * must be held, by this order. Obviously no further access to fb can be done 
- * after this.
+ * must be held, by this order.  If there are still references to the
+ * framebuffer, nothing will happen.
  */
 static INLINE void
 stw_framebuffer_destroy_locked(
@@ -76,6 +73,13 @@ stw_framebuffer_destroy_locked(
 {
    struct stw_framebuffer **link;
 
+   /* check the reference count */
+   fb->refcnt--;
+   if (fb->refcnt) {
+      pipe_mutex_unlock( fb->mutex );
+      return;
+   }
+
    link = &stw_dev->fb_head;
    while (*link != fb)
       link = &(*link)->next;
@@ -86,7 +90,7 @@ stw_framebuffer_destroy_locked(
    if(fb->shared_surface)
       stw_dev->stw_winsys->shared_surface_close(stw_dev->screen, fb->shared_surface);
 
-   st_unreference_framebuffer(fb->stfb);
+   stw_st_destroy_framebuffer_locked(fb->stfb);
    
    pipe_mutex_unlock( fb->mutex );
 
@@ -178,7 +182,7 @@ stw_call_window_proc(
    if(!tls_data)
       return 0;
    
-   if (nCode < 0)
+   if (nCode < 0 || !stw_dev)
        return CallNextHookEx(tls_data->hCallWndProcHook, nCode, wParam, lParam);
 
    if (pParams->message == WM_WINDOWPOSCHANGED) {
@@ -234,9 +238,14 @@ stw_framebuffer_create(
    fb->iPixelFormat = iPixelFormat;
 
    fb->pfi = pfi = stw_pixelformat_get_info( iPixelFormat - 1 );
+   fb->stfb = stw_st_create_framebuffer( fb );
+   if (!fb->stfb) {
+      FREE( fb );
+      return NULL;
+   }
+
+   fb->refcnt = 1;
 
-   stw_pixelformat_visual(&fb->visual, pfi);
-   
    stw_framebuffer_get_size(fb);
 
    pipe_mutex_init( fb->mutex );
@@ -255,49 +264,31 @@ stw_framebuffer_create(
    return fb;
 }
 
-
-BOOL
-stw_framebuffer_allocate(
+/**
+ * Have ptr reference fb.  The referenced framebuffer should be locked.
+ */
+void
+stw_framebuffer_reference(
+   struct stw_framebuffer **ptr,
    struct stw_framebuffer *fb)
 {
-   assert(fb);
-   
-   if(!fb->stfb) {
-      const struct stw_pixelformat_info *pfi = fb->pfi;
-      enum pipe_format colorFormat, depthFormat, stencilFormat;
+   struct stw_framebuffer *old_fb = *ptr;
 
-      colorFormat = pfi->color_format;
-      
-      assert(pf_layout( pfi->depth_stencil_format ) == PIPE_FORMAT_LAYOUT_RGBAZS );
-   
-      if(pf_get_component_bits( pfi->depth_stencil_format, PIPE_FORMAT_COMP_Z ))
-         depthFormat = pfi->depth_stencil_format;
-      else
-         depthFormat = PIPE_FORMAT_NONE;
-   
-      if(pf_get_component_bits( pfi->depth_stencil_format, PIPE_FORMAT_COMP_S ))
-         stencilFormat = pfi->depth_stencil_format;
-      else
-         stencilFormat = PIPE_FORMAT_NONE;
-   
-      assert(fb->must_resize);
-      assert(fb->width);
-      assert(fb->height);
-
-      fb->stfb = st_create_framebuffer(
-         &fb->visual,
-         colorFormat,
-         depthFormat,
-         stencilFormat,
-         fb->width,
-         fb->height,
-         (void *) fb );
-      
-      // to notify the context
-      fb->must_resize = TRUE;
+   if (old_fb == fb)
+      return;
+
+   if (fb)
+      fb->refcnt++;
+   if (old_fb) {
+      pipe_mutex_lock(stw_dev->fb_mutex);
+
+      pipe_mutex_lock(old_fb->mutex);
+      stw_framebuffer_destroy_locked(old_fb);
+
+      pipe_mutex_unlock(stw_dev->fb_mutex);
    }
-   
-   return fb->stfb ? TRUE : FALSE;
+
+   *ptr = fb;
 }
 
 
@@ -319,11 +310,6 @@ stw_framebuffer_update(
     * to know of their existing without using the not very portable PSAPI.
     */
    stw_framebuffer_get_size(fb);
-   
-   if(fb->must_resize) {
-      st_resize_framebuffer(fb->stfb, fb->width, fb->height);
-      fb->must_resize = FALSE;
-   }
 }                      
 
 
@@ -333,6 +319,9 @@ stw_framebuffer_cleanup( void )
    struct stw_framebuffer *fb;
    struct stw_framebuffer *next;
 
+   if (!stw_dev)
+      return;
+
    pipe_mutex_lock( stw_dev->fb_mutex );
 
    fb = stw_dev->fb_head;
@@ -388,6 +377,9 @@ stw_framebuffer_from_hdc(
 {
    struct stw_framebuffer *fb;
 
+   if (!stw_dev)
+      return NULL;
+
    pipe_mutex_lock( stw_dev->fb_mutex );
    fb = stw_framebuffer_from_hdc_locked(hdc);
    pipe_mutex_unlock( stw_dev->fb_mutex );
@@ -422,6 +414,9 @@ DrvSetPixelFormat(
    uint index;
    struct stw_framebuffer *fb;
 
+   if (!stw_dev)
+      return FALSE;
+
    index = (uint) iPixelFormat - 1;
    count = stw_pixelformat_get_extended_count();
    if (index >= count)
@@ -475,8 +470,9 @@ DrvPresentBuffers(HDC hdc, PGLPRESENTBUFFERSDATA data)
    struct stw_framebuffer *fb;
    struct pipe_screen *screen;
    struct pipe_surface *surface;
-   unsigned surface_index;
-   BOOL ret = FALSE;
+
+   if (!stw_dev)
+      return FALSE;
 
    fb = stw_framebuffer_from_hdc( hdc );
    if (fb == NULL)
@@ -484,16 +480,7 @@ DrvPresentBuffers(HDC hdc, PGLPRESENTBUFFERSDATA data)
 
    screen = stw_dev->screen;
 
-   surface_index = (unsigned)(uintptr_t)data->pPrivateData;
-   if(!st_get_framebuffer_surface( fb->stfb, surface_index, &surface ))
-      goto fail;
-
-#ifdef DEBUG
-   if(stw_dev->trace_running) {
-      screen = trace_screen(screen)->screen;
-      surface = trace_surface(surface)->surface;
-   }
-#endif
+   surface = (struct pipe_surface *)data->pPrivateData;
 
    if(data->hSharedSurface != fb->hSharedSurface) {
       if(fb->shared_surface) {
@@ -520,15 +507,12 @@ DrvPresentBuffers(HDC hdc, PGLPRESENTBUFFERSDATA data)
       stw_dev->stw_winsys->present( screen, surface, hdc );
    }
 
-   ret = TRUE;
-
-fail:
-
    stw_framebuffer_update(fb);
+   stw_notify_current_locked(fb);
 
    stw_framebuffer_release(fb);
 
-   return ret;
+   return TRUE;
 }
 
 
@@ -540,7 +524,7 @@ fail:
 BOOL
 stw_framebuffer_present_locked(HDC hdc,
                                struct stw_framebuffer *fb,
-                               unsigned surface_index)
+                               struct pipe_surface *surface)
 {
    if(stw_dev->callbacks.wglCbPresentBuffers &&
       stw_dev->stw_winsys->compose) {
@@ -551,33 +535,20 @@ stw_framebuffer_present_locked(HDC hdc,
       data.magic2 = 0;
       data.AdapterLuid = stw_dev->AdapterLuid;
       data.rect = fb->client_rect;
-      data.pPrivateData = (void *)(uintptr_t)surface_index;
+      data.pPrivateData = (void *)surface;
 
+      stw_notify_current_locked(fb);
       stw_framebuffer_release(fb);
 
       return stw_dev->callbacks.wglCbPresentBuffers(hdc, &data);
    }
    else {
       struct pipe_screen *screen = stw_dev->screen;
-      struct pipe_surface *surface;
-
-      if(!st_get_framebuffer_surface( fb->stfb, surface_index, &surface )) {
-         /* FIXME: this shouldn't happen, but does on glean */
-         stw_framebuffer_release(fb);
-         return FALSE;
-      }
-
-#ifdef DEBUG
-      if(stw_dev->trace_running) {
-         screen = trace_screen(screen)->screen;
-         surface = trace_surface(surface)->surface;
-      }
-#endif
 
       stw_dev->stw_winsys->present( screen, surface, hdc );
 
       stw_framebuffer_update(fb);
-
+      stw_notify_current_locked(fb);
       stw_framebuffer_release(fb);
 
       return TRUE;
@@ -591,6 +562,9 @@ DrvSwapBuffers(
 {
    struct stw_framebuffer *fb;
 
+   if (!stw_dev)
+      return FALSE;
+
    fb = stw_framebuffer_from_hdc( hdc );
    if (fb == NULL)
       return FALSE;
@@ -600,12 +574,9 @@ DrvSwapBuffers(
       return TRUE;
    }
 
-   /* If we're swapping the buffer associated with the current context
-    * we have to flush any pending rendering commands first.
-    */
-   st_notify_swapbuffers( fb->stfb );
+   stw_flush_current_locked(fb);
 
-   return stw_framebuffer_present_locked(hdc, fb, ST_SURFACE_BACK_LEFT);
+   return stw_st_swap_framebuffer_locked(fb->stfb);
 }