d3d9: Add reference to the parent device for surfaces.

Vitaliy Margolen wine-patch at kievinfo.com
Mon May 29 16:01:04 CDT 2006


ChangeLog:
d3d9: Add reference to the parent device for surfaces.
Allow some objects to have no parent device.
Tighten refcount tests and test refound decrement on Release.

It seems that if a surface being created from the device it should
reference it's parent. But if it's created as a part of the texture
then it's using different mechanism for refcounting.

 dlls/d3d9/d3d9_private.h |    2 +
 dlls/d3d9/device.c       |   12 ++++--
 dlls/d3d9/directx.c      |   10 ++++-
 dlls/d3d9/surface.c      |    1 
 dlls/d3d9/swapchain.c    |    2 -
 dlls/d3d9/tests/device.c |   95 ++++++++++++++++++++++------------------------
 6 files changed, 66 insertions(+), 56 deletions(-)
-------------- next part --------------
6a6e275ed38593b3cc433bb561c3e44ce47b38a3
diff --git a/dlls/d3d9/d3d9_private.h b/dlls/d3d9/d3d9_private.h
index f4f0583..03b23a0 100644
--- a/dlls/d3d9/d3d9_private.h
+++ b/dlls/d3d9/d3d9_private.h
@@ -333,6 +333,8 @@ typedef struct IDirect3DSurface9Impl
     /* IDirect3DResource9 fields */
     IWineD3DSurface        *wineD3DSurface;
 
+    /* Parent reference */
+    LPDIRECT3DDEVICE9       parentDevice;
 } IDirect3DSurface9Impl;
 
 /* ---------------------- */
diff --git a/dlls/d3d9/device.c b/dlls/d3d9/device.c
index e26c0e8..c6f0527 100644
--- a/dlls/d3d9/device.c
+++ b/dlls/d3d9/device.c
@@ -277,6 +277,8 @@ HRESULT  WINAPI IDirect3DDevice9Impl_Cre
         FIXME("(%p) call to IWineD3DDevice_CreateSurface failed\n", This);
         HeapFree(GetProcessHeap(), 0, object);
     } else {
+        IUnknown_AddRef(iface);
+        object->parentDevice = iface;
         TRACE("(%p) : Created surface %p\n", This, object);
         *ppSurface = (LPDIRECT3DSURFACE9) object;
     }
@@ -951,11 +953,15 @@ HRESULT WINAPI D3D9CB_CreateSurface(IUnk
         Lockable = FALSE;
         
     TRACE("relay\n");
-    res = IDirect3DDevice9Impl_CreateSurface((IDirect3DDevice9 *)device, Width, Height, (D3DFORMAT)Format, Lockable, FALSE/*Discard*/, Level,  (IDirect3DSurface9 **)&d3dSurface, D3DRTYPE_SURFACE, Usage, (D3DPOOL) Pool, D3DMULTISAMPLE_NONE, 0 /* MultisampleQuality */, pSharedHandle);  
+    res = IDirect3DDevice9Impl_CreateSurface((IDirect3DDevice9 *)device, Width, Height, (D3DFORMAT)Format,
+                Lockable, FALSE/*Discard*/, Level,  (IDirect3DSurface9 **)&d3dSurface, D3DRTYPE_SURFACE,
+                Usage, (D3DPOOL) Pool, D3DMULTISAMPLE_NONE, 0 /* MultisampleQuality */, pSharedHandle);  
 
-    if (res == D3D_OK) {
+    if (SUCCEEDED(res)) {
         *ppSurface = d3dSurface->wineD3DSurface;
-    }else{
+        IUnknown_Release(d3dSurface->parentDevice);
+        d3dSurface->parentDevice = NULL;
+    } else {
         FIXME("(%p) IDirect3DDevice9_CreateSurface failed\n", device);
     }
     return res;
diff --git a/dlls/d3d9/directx.c b/dlls/d3d9/directx.c
index 2a385e9..808af0a 100644
--- a/dlls/d3d9/directx.c
+++ b/dlls/d3d9/directx.c
@@ -188,6 +188,8 @@ HRESULT WINAPI D3D9CB_CreateRenderTarget
 
     if (SUCCEEDED(res)) {
         *ppSurface = d3dSurface->wineD3DSurface;
+        IUnknown_Release(d3dSurface->parentDevice);
+        d3dSurface->parentDevice = NULL;
     } else {
         *ppSurface = NULL;
     }
@@ -220,8 +222,10 @@ HRESULT WINAPI D3D9CB_CreateAdditionalSw
     /*copy the presentation parameters*/
     res = IDirect3DDevice9_CreateAdditionalSwapChain((IDirect3DDevice9 *)device, &localParameters, (IDirect3DSwapChain9 **)&d3dSwapChain);
 
-    if (res == D3D_OK && d3dSwapChain != NULL) {
+    if (SUCCEEDED(res)) {
         *ppSwapChain = d3dSwapChain->wineD3DSwapChain;
+        IUnknown_Release(d3dSwapChain->parentDevice);
+        d3dSwapChain->parentDevice = NULL;
     } else {
         *ppSwapChain = NULL;
     }
@@ -256,8 +260,10 @@ HRESULT WINAPI D3D9CB_CreateDepthStencil
     res = IDirect3DDevice9_CreateDepthStencilSurface((IDirect3DDevice9 *)device, Width, Height, 
                                          (D3DFORMAT)Format, MultiSample, MultisampleQuality, Discard, 
                                          (IDirect3DSurface9 **)&d3dSurface, pSharedHandle);
-    if (res == D3D_OK) {
+    if (SUCCEEDED(res)) {
         *ppSurface = d3dSurface->wineD3DSurface;
+        IUnknown_Release(d3dSurface->parentDevice);
+        d3dSurface->parentDevice = NULL;
     }
     return res;
 }
diff --git a/dlls/d3d9/surface.c b/dlls/d3d9/surface.c
index 2752792..6fb6daa 100644
--- a/dlls/d3d9/surface.c
+++ b/dlls/d3d9/surface.c
@@ -79,6 +79,7 @@ ULONG WINAPI IDirect3DSurface9Impl_Relea
 
         if (ref == 0) {
             IWineD3DSurface_Release(This->wineD3DSurface);
+            if (This->parentDevice) IUnknown_Release(This->parentDevice);
             HeapFree(GetProcessHeap(), 0, This);
         }
 
diff --git a/dlls/d3d9/swapchain.c b/dlls/d3d9/swapchain.c
index 5f60cba..95c8096 100644
--- a/dlls/d3d9/swapchain.c
+++ b/dlls/d3d9/swapchain.c
@@ -58,7 +58,7 @@ ULONG WINAPI IDirect3DSwapChain9Impl_Rel
 
     if (ref == 0) {
         IWineD3DSwapChain_Release(This->wineD3DSwapChain);
-        IUnknown_Release(This->parentDevice);
+        if (This->parentDevice) IUnknown_Release(This->parentDevice);
         HeapFree(GetProcessHeap(), 0, This);
     }
     return ref;
diff --git a/dlls/d3d9/tests/device.c b/dlls/d3d9/tests/device.c
index 36e84de..4bb287e 100644
--- a/dlls/d3d9/tests/device.c
+++ b/dlls/d3d9/tests/device.c
@@ -33,10 +33,18 @@ static int get_refcount(IUnknown *object
 #define CHECK_CALL(r,c,d,rc) \
     if (SUCCEEDED(r)) {\
         int tmp1 = get_refcount( (IUnknown *)d ); \
-        ok(rc == tmp1, "Invalid refcount. Expected %d got %d\n", rc, tmp1); \
+        int rc_new = rc; \
+        ok(tmp1 == rc_new, "Invalid refcount. Expected %d got %d\n", rc_new, tmp1); \
     } else {\
         trace("%s failed: %s\n", c, DXGetErrorString9(r)); \
     }
+#define CHECK_RELEASE(obj,d,rc) \
+    if (obj) { \
+        int tmp1, rc_new = rc; \
+        IUnknown_Release( obj ); \
+        tmp1 = get_refcount( (IUnknown *)d ); \
+        ok(tmp1 == rc_new, "Invalid refcount. Expected %d got %d\n", rc_new, tmp1); \
+    }
 
 void test_swapchain(void)
 {
@@ -245,95 +253,82 @@ void test_refcount(void)
     ok(SUCCEEDED(hr), "Failed to create IDirect3D9Device (%s)\n", DXGetErrorString9(hr));
     if (FAILED(hr)) goto cleanup;
 
+    refcount = get_refcount( (IUnknown *)pDevice );
+    ok(refcount == 1, "Invalid device RefCount %d\n", refcount);
 
     /* Buffers */
-    refcount = get_refcount( (IUnknown *)pDevice );
     hr = IDirect3DDevice9_CreateIndexBuffer( pDevice, 16, 0, D3DFMT_INDEX32, D3DPOOL_DEFAULT, &pIndexBuffer, NULL );
-    CHECK_CALL( hr, "CreateIndexBuffer", pDevice, refcount+1 );
-    refcount = get_refcount( (IUnknown *)pDevice );
+    CHECK_CALL( hr, "CreateIndexBuffer", pDevice, ++refcount );
     hr = IDirect3DDevice9_CreateVertexBuffer( pDevice, 16, 0, D3DFVF_XYZ, D3DPOOL_DEFAULT, &pVertexBuffer, NULL );
-    CHECK_CALL( hr, "CreateVertexBuffer", pDevice, refcount+1 );
+    CHECK_CALL( hr, "CreateVertexBuffer", pDevice, ++refcount );
     /* Shaders */
-    refcount = get_refcount( (IUnknown *)pDevice );
     hr = IDirect3DDevice9_CreateVertexDeclaration( pDevice, decl, &pVertexDeclaration );
-    CHECK_CALL( hr, "CreateVertexDeclaration", pDevice, refcount+1 );
-    refcount = get_refcount( (IUnknown *)pDevice );
+    CHECK_CALL( hr, "CreateVertexDeclaration", pDevice, ++refcount );
     hr = IDirect3DDevice9_CreateVertexShader( pDevice, simple_vs, &pVertexShader );
-    CHECK_CALL( hr, "CreateVertexShader", pDevice, refcount+1 );
-    refcount = get_refcount( (IUnknown *)pDevice );
+    CHECK_CALL( hr, "CreateVertexShader", pDevice, ++refcount );
     hr = IDirect3DDevice9_CreatePixelShader( pDevice, simple_ps, &pPixelShader );
-    CHECK_CALL( hr, "CreatePixelShader", pDevice, refcount+1 );
+    CHECK_CALL( hr, "CreatePixelShader", pDevice, ++refcount );
     /* Textures */
-    refcount = get_refcount( (IUnknown *)pDevice );
     hr = IDirect3DDevice9_CreateTexture( pDevice, 32, 32, 3, 0, D3DFMT_X8R8G8B8, D3DPOOL_DEFAULT, &pTexture, NULL );
-    CHECK_CALL( hr, "CreateTexture", pDevice, refcount+1 );
+    CHECK_CALL( hr, "CreateTexture", pDevice, ++refcount );
     if (pTexture)
     {
         tmp = get_refcount( (IUnknown *)pTexture );
         /* This should not increment device refcount */
         hr = IDirect3DTexture9_GetSurfaceLevel( pTexture, 1, &pTextureLevel );
-        CHECK_CALL( hr, "GetSurfaceLevel", pDevice, refcount+1 );
+        CHECK_CALL( hr, "GetSurfaceLevel", pDevice, refcount );
         /* But should increment texture's refcount */
         CHECK_CALL( hr, "GetSurfaceLevel", pTexture, tmp+1 );
     }
-    refcount = get_refcount( (IUnknown *)pDevice );
     hr = IDirect3DDevice9_CreateCubeTexture( pDevice, 32, 0, 0, D3DFMT_X8R8G8B8, D3DPOOL_DEFAULT, &pCubeTexture, NULL );
-    CHECK_CALL( hr, "CreateCubeTexture", pDevice, refcount+1 );
-    refcount = get_refcount( (IUnknown *)pDevice );
+    CHECK_CALL( hr, "CreateCubeTexture", pDevice, ++refcount );
     hr = IDirect3DDevice9_CreateVolumeTexture( pDevice, 32, 32, 2, 0, 0, D3DFMT_X8R8G8B8, D3DPOOL_DEFAULT, &pVolumeTexture, NULL );
-    CHECK_CALL( hr, "CreateVolumeTexture", pDevice, refcount+1 );
+    CHECK_CALL( hr, "CreateVolumeTexture", pDevice, ++refcount );
     /* Surfaces */
-    refcount = get_refcount( (IUnknown *)pDevice );
     hr = IDirect3DDevice9_CreateDepthStencilSurface( pDevice, 32, 32, D3DFMT_D24S8, D3DMULTISAMPLE_NONE, 0, TRUE, &pStencilSurface, NULL );
-    todo_wine {CHECK_CALL( hr, "CreateDepthStencilSurface", pDevice, refcount+1 );}
-    refcount = get_refcount( (IUnknown *)pDevice );
+    CHECK_CALL( hr, "CreateDepthStencilSurface", pDevice, ++refcount );
     hr = IDirect3DDevice9_CreateOffscreenPlainSurface( pDevice, 32, 32, D3DFMT_X8R8G8B8, D3DPOOL_DEFAULT, &pOffscreenSurface, NULL );
-    todo_wine {CHECK_CALL( hr, "CreateOffscreenPlainSurface", pDevice, refcount+1 );}
-    refcount = get_refcount( (IUnknown *)pDevice );
+    CHECK_CALL( hr, "CreateOffscreenPlainSurface", pDevice, ++refcount );
     hr = IDirect3DDevice9_CreateRenderTarget( pDevice, 32, 32, D3DFMT_X8R8G8B8, D3DMULTISAMPLE_NONE, 0, TRUE, &pRenderTarget, NULL );
-    todo_wine {CHECK_CALL( hr, "CreateRenderTarget", pDevice, refcount+1 );}
+    CHECK_CALL( hr, "CreateRenderTarget", pDevice, ++refcount );
     /* Misc */
-    refcount = get_refcount( (IUnknown *)pDevice );
     hr = IDirect3DDevice9_CreateStateBlock( pDevice, D3DSBT_ALL, &pStateBlock );
-    CHECK_CALL( hr, "CreateStateBlock", pDevice, refcount+1 );
-    refcount = get_refcount( (IUnknown *)pDevice );
+    CHECK_CALL( hr, "CreateStateBlock", pDevice, ++refcount );
     hr = IDirect3DDevice9_CreateAdditionalSwapChain( pDevice, &d3dpp, &pSwapChain );
-    CHECK_CALL( hr, "CreateAdditionalSwapChain", pDevice, refcount+1 );
-    refcount = get_refcount( (IUnknown *)pDevice );
+    CHECK_CALL( hr, "CreateAdditionalSwapChain", pDevice, ++refcount );
     hr = IDirect3DDevice9_CreateQuery( pDevice, D3DQUERYTYPE_EVENT, &pQuery );
-    CHECK_CALL( hr, "CreateQuery", pDevice, refcount+1 );
+    CHECK_CALL( hr, "CreateQuery", pDevice, ++refcount );
 
-    refcount = get_refcount( (IUnknown *)pDevice );
     hr = IDirect3DDevice9_BeginStateBlock( pDevice );
     CHECK_CALL( hr, "BeginStateBlock", pDevice, refcount );
     hr = IDirect3DDevice9_EndStateBlock( pDevice, &pStateBlock1 );
-    CHECK_CALL( hr, "EndStateBlock", pDevice, refcount+1 );
+    CHECK_CALL( hr, "EndStateBlock", pDevice, ++refcount );
 
 cleanup:
-    if (pDevice)              IUnknown_Release( pDevice );
+    CHECK_RELEASE(pDevice,              pDevice, --refcount);
 
     /* Buffers */
-    if (pVertexBuffer)        IUnknown_Release( pVertexBuffer );
-    if (pIndexBuffer)         IUnknown_Release( pIndexBuffer );
+    CHECK_RELEASE(pVertexBuffer,        pDevice, --refcount);
+    CHECK_RELEASE(pIndexBuffer,         pDevice, --refcount);
     /* Shaders */
-    if (pVertexDeclaration)   IUnknown_Release( pVertexDeclaration );
-    if (pVertexShader)        IUnknown_Release( pVertexShader );
-    if (pPixelShader)         IUnknown_Release( pPixelShader );
+    CHECK_RELEASE(pVertexDeclaration,   pDevice, --refcount);
+    CHECK_RELEASE(pVertexShader,        pDevice, --refcount);
+    CHECK_RELEASE(pPixelShader,         pDevice, --refcount);
     /* Textures */
-    if (pTexture)             IUnknown_Release( pTexture );
-    if (pTextureLevel)        IUnknown_Release( pTextureLevel );
-    if (pCubeTexture)         IUnknown_Release( pCubeTexture );
-    if (pVolumeTexture)       IUnknown_Release( pVolumeTexture );
+    /* pTextureLevel is holding a reference to the pTexture */
+    CHECK_RELEASE(pTexture,             pDevice,   refcount);
+    CHECK_RELEASE(pTextureLevel,        pDevice, --refcount);
+    CHECK_RELEASE(pCubeTexture,         pDevice, --refcount);
+    CHECK_RELEASE(pVolumeTexture,       pDevice, --refcount);
     /* Surfaces */
-    if (pStencilSurface)      IUnknown_Release( pStencilSurface );
-    if (pOffscreenSurface)    IUnknown_Release( pOffscreenSurface );
-    if (pRenderTarget)        IUnknown_Release( pRenderTarget );
+    CHECK_RELEASE(pStencilSurface,      pDevice, --refcount);
+    CHECK_RELEASE(pOffscreenSurface,    pDevice, --refcount);
+    CHECK_RELEASE(pRenderTarget,        pDevice, --refcount);
     /* Misc */
-    if (pStateBlock)          IUnknown_Release( pStateBlock );
-    /* Avoid crash for now.
-    if (pSwapChain)           IUnknown_Release( pSwapChain );
-    */
-    if (pQuery)               IUnknown_Release( pQuery );
+    CHECK_RELEASE(pStateBlock,          pDevice, --refcount);
+    CHECK_RELEASE(pSwapChain,           pDevice, --refcount);
+    CHECK_RELEASE(pQuery,               pDevice, --refcount);
+    /* This will destroy device - can not check the refcount here */
     if (pStateBlock1)         IUnknown_Release( pStateBlock1 );
 
     if (pD3d)                 IUnknown_Release( pD3d );


More information about the wine-patches mailing list