{
struct stw_context *old_ctx = NULL;
struct stw_context *ctx = NULL;
- struct stw_framebuffer *fb = NULL;
BOOL ret = FALSE;
if (!stw_dev)
}
if (dhglrc) {
+ struct stw_framebuffer *fb = NULL;
stw_lock_contexts(stw_dev);
ctx = stw_lookup_context_locked( dhglrc );
stw_unlock_contexts(stw_dev);
goto fail;
}
+ /* This call locks fb's mutex */
fb = stw_framebuffer_from_hdc( hdc );
if (fb) {
stw_framebuffer_update(fb);
}
if (fb->iPixelFormat != ctx->iPixelFormat) {
+ stw_framebuffer_unlock(fb);
SetLastError(ERROR_INVALID_PIXEL_FORMAT);
goto fail;
}
/* Bind the new framebuffer */
ctx->hdc = hdc;
+ struct stw_framebuffer *old_fb = ctx->current_framebuffer;
+ if (old_fb != fb) {
+ stw_framebuffer_reference_locked(fb);
+ ctx->current_framebuffer = fb;
+ }
+ stw_framebuffer_unlock(fb);
+
/* Note: when we call this function we will wind up in the
* stw_st_framebuffer_validate_locked() function which will incur
* a recursive fb->mutex lock.
*/
ret = stw_dev->stapi->make_current(stw_dev->stapi, ctx->st,
fb->stfb, fb->stfb);
- stw_framebuffer_reference(&ctx->current_framebuffer, fb);
- } else {
- ret = stw_dev->stapi->make_current(stw_dev->stapi, NULL, NULL, NULL);
- }
-fail:
+ if (old_fb && old_fb != fb) {
+ stw_lock_framebuffers(stw_dev);
+ stw_framebuffer_lock(old_fb);
+ stw_framebuffer_release_locked(old_fb);
+ stw_unlock_framebuffers(stw_dev);
+ }
- if (fb) {
- stw_framebuffer_unlock(fb);
- }
+fail:
+ /* fb must be unlocked at this point. */
+ assert(!stw_own_mutex(&fb->mutex));
- /* On failure, make the thread's current rendering context not current
- * before returning.
- */
- if (!ret) {
- stw_dev->stapi->make_current(stw_dev->stapi, NULL, NULL, NULL);
- ctx = NULL;
+ /* On failure, make the thread's current rendering context not current
+ * before returning.
+ */
+ if (!ret) {
+ stw_make_current(NULL, 0);
+ }
+ } else {
+ ret = stw_dev->stapi->make_current(stw_dev->stapi, NULL, NULL, NULL);
}
/* Unreference the previous framebuffer if any. It must be done after
* make_current, as it can be referenced inside.
*/
if (old_ctx && old_ctx != ctx) {
- stw_framebuffer_reference(&old_ctx->current_framebuffer, NULL);
+ struct stw_framebuffer *old_fb = old_ctx->current_framebuffer;
+ if (old_fb) {
+ old_ctx->current_framebuffer = NULL;
+ stw_lock_framebuffers(stw_dev);
+ stw_framebuffer_lock(old_fb);
+ stw_framebuffer_release_locked(old_fb);
+ stw_unlock_framebuffers(stw_dev);
+ }
}
return ret;
* Decrement the reference count on the given stw_framebuffer object.
* If the reference count hits zero, destroy the object.
*
- * Note: Both stw_dev::fb_mutex and stw_framebuffer::mutex must already
- * be locked.
+ * Note: Both stw_dev::fb_mutex and stw_framebuffer::mutex must already be
+ * locked. After this function completes, the fb's mutex will be unlocked.
*/
-static void
-stw_framebuffer_destroy_locked(struct stw_framebuffer *fb)
+void
+stw_framebuffer_release_locked(struct stw_framebuffer *fb)
{
struct stw_framebuffer **link;
+ assert(fb);
+ assert(stw_own_mutex(&fb->mutex));
+ assert(stw_own_mutex(&stw_dev->fb_mutex));
+
/* check the reference count */
fb->refcnt--;
if (fb->refcnt) {
stw_lock_framebuffers(stw_dev);
fb = stw_framebuffer_from_hwnd_locked( pParams->hwnd );
if (fb)
- stw_framebuffer_destroy_locked(fb);
+ stw_framebuffer_release_locked(fb);
stw_unlock_framebuffers(stw_dev);
}
}
-/**
- * Have ptr reference fb. The referenced framebuffer should be locked.
- */
-void
-stw_framebuffer_reference(struct stw_framebuffer **ptr,
- struct stw_framebuffer *fb)
-{
- struct stw_framebuffer *old_fb = *ptr;
-
- if (old_fb == fb)
- return;
-
- if (fb)
- fb->refcnt++;
- if (old_fb) {
- stw_lock_framebuffers(stw_dev);
-
- stw_framebuffer_lock(old_fb);
- stw_framebuffer_destroy_locked(old_fb);
-
- stw_unlock_framebuffers(stw_dev);
- }
-
- *ptr = fb;
-}
-
-
/**
* Update the framebuffer's size if necessary.
*/
next = fb->next;
stw_framebuffer_lock(fb);
- stw_framebuffer_destroy_locked(fb);
+ stw_framebuffer_release_locked(fb);
fb = next;
}
#include <GL/wglext.h>
#include "util/u_debug.h"
+#include "stw_st.h"
struct pipe_resource;
struct stw_framebuffer *
stw_framebuffer_create(HDC hdc, int iPixelFormat);
+
+/**
+ * Increase fb reference count. The referenced framebuffer should be locked.
+ *
+ * It's not necessary to hold stw_dev::fb_mutex global lock.
+ */
+static inline void
+stw_framebuffer_reference_locked(struct stw_framebuffer *fb)
+{
+ if (fb) {
+ assert(stw_own_mutex(&fb->mutex));
+ fb->refcnt++;
+ }
+}
+
+
void
-stw_framebuffer_reference(struct stw_framebuffer **ptr,
- struct stw_framebuffer *fb);
+stw_framebuffer_release_locked(struct stw_framebuffer *fb);
/**
* Search a framebuffer with a matching HWND.
stw_framebuffer_unlock(struct stw_framebuffer *fb)
{
assert(fb);
+ assert(stw_own_mutex(&fb->mutex));
LeaveCriticalSection(&fb->mutex);
}