From 5fd9f4b04024eeb7f0e1a8a16659a126ad6d9d2e Mon Sep 17 00:00:00 2001
From: Aaryaman Vasishta <jem456.vasishta@gmail.com>
Date: Wed, 29 Jul 2015 14:15:41 +0530
Subject: [PATCH 7/8] d3drm: Implement
 IDirect3DRMDevice::CreateDeviceFromSurface.
Reply-To: wine-devel <wine-devel@winehq.org>

---
 dlls/d3drm/d3drm.c         | 18 +++++++++++------
 dlls/d3drm/d3drm_private.h |  3 ++-
 dlls/d3drm/device.c        | 49 +++++++++++++++++++++++++++++++---------------
 dlls/d3drm/tests/d3drm.c   | 45 +++++++++++++-----------------------------
 4 files changed, 61 insertions(+), 54 deletions(-)

diff --git a/dlls/d3drm/d3drm.c b/dlls/d3drm/d3drm.c
index 59d4486..a1ea4c0 100644
--- a/dlls/d3drm/d3drm.c
+++ b/dlls/d3drm/d3drm.c
@@ -247,18 +247,24 @@ static HRESULT WINAPI d3drm1_CreateDevice(IDirect3DRM *iface,
 static HRESULT WINAPI d3drm1_CreateDeviceFromSurface(IDirect3DRM *iface, GUID *guid,
         IDirectDraw *ddraw, IDirectDrawSurface *backbuffer, IDirect3DRMDevice **device)
 {
-    struct d3drm_device *object = NULL;
+    struct d3drm *d3drm = impl_from_IDirect3DRM(iface);
+    struct d3drm_device *object;
     HRESULT hr;
-    FIXME("iface %p, guid %s, ddraw %p, backbuffer %p, device %p partial stub.\n",
+
+    TRACE("iface %p, guid %s, ddraw %p, backbuffer %p, device %p.\n",
             iface, debugstr_guid(guid), ddraw, backbuffer, device);
 
     hr = Direct3DRMDevice_create(&object);
     if (FAILED(hr))
         return hr;
 
-    *device = IDirect3DRMDevice_from_impl(object);
+    hr = d3drm_device_init(object, 1, &d3drm->IDirect3DRM_iface, ddraw, backbuffer, TRUE);
+    if (SUCCEEDED(hr))
+        *device = IDirect3DRMDevice_from_impl(object);
+    else
+        d3drm_device_destroy(object);
 
-    return D3DRM_OK;
+    return hr;
 }
 
 static HRESULT WINAPI d3drm1_CreateDeviceFromD3D(IDirect3DRM *iface,
@@ -312,7 +318,7 @@ static HRESULT WINAPI d3drm1_CreateDeviceFromClipper(IDirect3DRM *iface,
         return hr;
     }
 
-    hr = d3drm_device_init(object, 1, iface, ddraw, render_target);
+    hr = d3drm_device_init(object, 1, iface, ddraw, render_target, TRUE);
     IDirectDraw_Release(ddraw);
     IDirectDrawSurface_Release(render_target);
     if (FAILED(hr))
@@ -1133,7 +1139,7 @@ static HRESULT WINAPI d3drm3_CreateDeviceFromClipper(IDirect3DRM3 *iface,
         return hr;
     }
 
-    hr = d3drm_device_init(object, 3, &d3drm->IDirect3DRM_iface, ddraw, render_target);
+    hr = d3drm_device_init(object, 3, &d3drm->IDirect3DRM_iface, ddraw, render_target, TRUE);
     IDirectDraw_Release(ddraw);
     IDirectDrawSurface_Release(render_target);
     if (FAILED(hr))
diff --git a/dlls/d3drm/d3drm_private.h b/dlls/d3drm/d3drm_private.h
index 06079b9..bf131f3 100644
--- a/dlls/d3drm/d3drm_private.h
+++ b/dlls/d3drm/d3drm_private.h
@@ -46,7 +46,8 @@ void d3drm_device_destroy(struct d3drm_device *device) DECLSPEC_HIDDEN;
 HRESULT d3drm_device_create_surfaces_from_clipper(struct d3drm_device *object, IDirectDraw *ddraw, IDirectDrawClipper *clipper, int width, int height,
             IDirectDrawSurface **surface) DECLSPEC_HIDDEN;
 
-HRESULT d3drm_device_init(struct d3drm_device *device, UINT version, IDirect3DRM *d3drm, IDirectDraw *ddraw, IDirectDrawSurface *surface) DECLSPEC_HIDDEN;
+HRESULT d3drm_device_init(struct d3drm_device *device, UINT version, IDirect3DRM *d3drm, IDirectDraw *ddraw, IDirectDrawSurface *surface,
+            BOOL create_z_surface) DECLSPEC_HIDDEN;
 
 struct d3drm_file_header
 {
diff --git a/dlls/d3drm/device.c b/dlls/d3drm/device.c
index 62e2c6f..17f2b58 100644
--- a/dlls/d3drm/device.c
+++ b/dlls/d3drm/device.c
@@ -159,8 +159,10 @@ HRESULT d3drm_device_create_surfaces_from_clipper(struct d3drm_device *object, I
     return D3DRM_OK;
 }
 
-HRESULT d3drm_device_init(struct d3drm_device *device, UINT version, IDirect3DRM *d3drm, IDirectDraw *ddraw, IDirectDrawSurface *surface)
+HRESULT d3drm_device_init(struct d3drm_device *device, UINT version, IDirect3DRM *d3drm, IDirectDraw *ddraw, IDirectDrawSurface *surface,
+            BOOL create_z_surface)
 {
+    DDSCAPS caps = { DDSCAPS_ZBUFFER };
     IDirectDrawSurface *ds = NULL;
     IDirect3DDevice *device1 = NULL;
     IDirect3DDevice2 *device2 = NULL;
@@ -171,6 +173,7 @@ HRESULT d3drm_device_init(struct d3drm_device *device, UINT version, IDirect3DRM
     device->ddraw = ddraw;
     IDirectDraw_AddRef(ddraw);
     device->d3drm = d3drm;
+    device->render_target = surface;
     IDirect3DRM_AddRef(d3drm);
     device->render_target = surface;
     IDirectDrawSurface_AddRef(surface);
@@ -180,21 +183,34 @@ HRESULT d3drm_device_init(struct d3drm_device *device, UINT version, IDirect3DRM
     if (FAILED(hr))
         return hr;
 
-    memset(&surface_desc, 0, sizeof(surface_desc));
-    surface_desc.dwSize = sizeof(surface_desc);
-    surface_desc.dwFlags = DDSD_CAPS | DDSD_ZBUFFERBITDEPTH | DDSD_WIDTH | DDSD_HEIGHT;
-    surface_desc.ddsCaps.dwCaps = DDSCAPS_ZBUFFER;
-    surface_desc.dwZBufferBitDepth = 16;
-    surface_desc.dwWidth = desc.dwWidth;
-    surface_desc.dwHeight = desc.dwHeight;
-    hr = IDirectDraw_CreateSurface(ddraw, &surface_desc, &ds, NULL);
-    if (FAILED(hr))
-        return hr;
+    if (!(desc.ddsCaps.dwCaps & DDSCAPS_3DDEVICE))
+        return DDERR_INVALIDCAPS;
 
-    hr = IDirectDrawSurface_AddAttachedSurface(surface, ds);
-    IDirectDrawSurface_Release(ds);
-    if (FAILED(hr))
-        return hr;
+    hr = IDirectDrawSurface_GetAttachedSurface(surface, &caps, &ds);
+    if (SUCCEEDED(hr))
+    {
+        create_z_surface = FALSE;
+        IDirectDrawSurface_Release(ds);
+    }
+
+    if (create_z_surface)
+    {
+        memset(&surface_desc, 0, sizeof(surface_desc));
+        surface_desc.dwSize = sizeof(surface_desc);
+        surface_desc.dwFlags = DDSD_CAPS | DDSD_ZBUFFERBITDEPTH | DDSD_WIDTH | DDSD_HEIGHT;
+        surface_desc.ddsCaps.dwCaps = DDSCAPS_ZBUFFER;
+        surface_desc.dwZBufferBitDepth = 16;
+        surface_desc.dwWidth = desc.dwWidth;
+        surface_desc.dwHeight = desc.dwHeight;
+        hr = IDirectDraw_CreateSurface(ddraw, &surface_desc, &ds, NULL);
+        if (FAILED(hr))
+            return hr;
+
+        hr = IDirectDrawSurface_AddAttachedSurface(surface, ds);
+        IDirectDrawSurface_Release(ds);
+        if (FAILED(hr))
+            return hr;
+    }
 
     if (version == 1)
         hr = IDirectDrawSurface_QueryInterface(surface, &IID_IDirect3DRGBDevice, (void **)&device1);
@@ -206,7 +222,8 @@ HRESULT d3drm_device_init(struct d3drm_device *device, UINT version, IDirect3DRM
     }
     if (FAILED(hr))
     {
-        IDirectDrawSurface_DeleteAttachedSurface(surface, 0, ds);
+        if (ds)
+            IDirectDrawSurface_DeleteAttachedSurface(surface, 0, ds);
         return hr;
     }
 
diff --git a/dlls/d3drm/tests/d3drm.c b/dlls/d3drm/tests/d3drm.c
index 7963b2a..621a4f4 100644
--- a/dlls/d3drm/tests/d3drm.c
+++ b/dlls/d3drm/tests/d3drm.c
@@ -2505,10 +2505,8 @@ static void test_create_device_from_surface1(void)
     ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
 
     hr = IDirect3DRM_CreateDeviceFromSurface(d3drm1, &driver, ddraw, surface, &device1);
-    todo_wine ok(hr == DDERR_INVALIDCAPS, "Expected hr == DDERR_INVALIDCAPS, got %x.\n", hr);
+    ok(hr == DDERR_INVALIDCAPS, "Expected hr == DDERR_INVALIDCAPS, got %x.\n", hr);
     IDirectDrawSurface_Release(surface);
-    if (SUCCEEDED(hr))
-        IDirect3DRMDevice_Release(device1);
 
     desc.ddsCaps.dwCaps |= DDSCAPS_3DDEVICE;
     hr = IDirectDraw_CreateSurface(ddraw, &desc, &surface, NULL);
@@ -2517,9 +2515,9 @@ static void test_create_device_from_surface1(void)
     hr = IDirect3DRM_CreateDeviceFromSurface(d3drm1, &driver, ddraw, surface, &device1);
     ok(SUCCEEDED(hr), "Cannot create IDirect3DRMDevice interface (hr = %x).\n", hr);
     ref2 = get_refcount((IUnknown *)d3drm1);
-    todo_wine ok(ref2 > ref1, "expected ref2 > ref1, got ref1 = %u , ref2 = %u.\n", ref1, ref2);
+    ok(ref2 > ref1, "expected ref2 > ref1, got ref1 = %u , ref2 = %u.\n", ref1, ref2);
     surface_ref2 = get_refcount((IUnknown *)surface);
-    todo_wine ok(surface_ref2 > surface_ref1, "Expected surface_ref2 > surface_ref1, got surface_ref1 = %u, surface_ref2 = %u.\n", surface_ref1, surface_ref2);
+    ok(surface_ref2 > surface_ref1, "Expected surface_ref2 > surface_ref1, got surface_ref1 = %u, surface_ref2 = %u.\n", surface_ref1, surface_ref2);
 
     /* Check if CreateDeviceFromSurface creates a primary surface */
     hr = IDirectDraw_EnumSurfaces(ddraw, DDENUMSURFACES_ALL | DDENUMSURFACES_DOESEXIST,
@@ -2528,9 +2526,7 @@ static void test_create_device_from_surface1(void)
     ok(d3drm_surface == NULL, "No primary surface should have enumerated (%p).\n", d3drm_surface);
 
     hr = IDirect3DRMDevice_GetDirect3DDevice(device1, &d3ddevice1);
-    todo_wine ok(hr == D3DRM_OK, "Cannot get IDirect3DDevice interface (hr = %x).\n", hr);
-    if (FAILED(hr))
-        goto cleanup;
+    ok(hr == D3DRM_OK, "Cannot get IDirect3DDevice interface (hr = %x).\n", hr);
 
     hr = IDirect3DDevice_QueryInterface(d3ddevice1, &IID_IDirectDrawSurface, (void **)&d3drm_surface);
     ok(hr == DD_OK, "Cannot get surface to the render target (hr = %x).\n", hr);
@@ -2567,7 +2563,6 @@ static void test_create_device_from_surface1(void)
     /*The render target still holds a reference to ds as the depth surface remains attached to it, so refcount will be 1*/
     ref1 = IDirectDrawSurface_Release(ds);
     ok(ref1 == 1, "Expected ref1 == 1, got %u.\n", ref1);
-
     ref1 = IDirectDrawSurface_Release(surface);
     ok(ref1 == 0, "Expected Render target refcount == 0, got %u.\n", ref1);
 
@@ -2597,9 +2592,7 @@ static void test_create_device_from_surface1(void)
     ok(SUCCEEDED(hr), "Cannot create IDirect3DRMDevice interface (hr = %x).\n", hr);
 
     hr = IDirect3DRMDevice2_GetDirect3DDevice(device1, &d3ddevice1);
-    todo_wine ok(hr == D3DRM_OK, "Cannot get IDirect3DDevice interface (hr = %x).\n", hr);
-    if (FAILED(hr))
-        goto cleanup;
+    ok(hr == D3DRM_OK, "Cannot get IDirect3DDevice interface (hr = %x).\n", hr);
 
     hr = IDirect3DDevice_QueryInterface(d3ddevice1, &IID_IDirectDrawSurface, (void **)&d3drm_surface);
     ok(hr == DD_OK, "Cannot get surface to the render target (hr = %x).\n", hr);
@@ -2614,27 +2607,17 @@ static void test_create_device_from_surface1(void)
     IDirectDrawSurface_Release(d3drm_surface);
     IDirectDrawSurface_Release(ds);
 
-cleanup:
-    if (d3ddevice1)
-        IDirect3DDevice_Release(d3ddevice1);
+    IDirect3DDevice_Release(d3ddevice1);
     IDirect3DRMDevice_Release(device1);
     hr = IDirectDrawSurface_GetAttachedSurface(surface, &caps, &ds);
-    todo_wine ok(hr == DD_OK, "Cannot get attached depth surface (hr = %x).\n", hr);
-    if (SUCCEEDED(hr))
-    {
-        /*The render target still holds a reference to ds as the depth surface remains attached to it, so refcount will be 1*/
-        ref1 = IDirectDrawSurface_Release(ds);
-        ok(ref1 == 1, "Expected ref1 == 1, got %u.\n", ref1);
-    }
-    if (surface)
-    {
-        ref1 = IDirectDrawSurface_Release(surface);
-        ok(ref1 == 0, "Expected Render target refcount == 0, got %u.\n", ref1);
-    }
-    if (d3drm1)
-        IDirect3DRM_Release(d3drm1);
-    if (ddraw)
-        IDirectDraw_Release(ddraw);
+    ok(hr == DD_OK, "Cannot get attached depth surface (hr = %x).\n", hr);
+    /*The render target still holds a reference to ds as the depth surface remains attached to it, so refcount will be 1*/
+    ref1 = IDirectDrawSurface_Release(ds);
+    ok(ref1 == 1, "Expected ref1 == 1, got %u.\n", ref1);
+    ref1 = IDirectDrawSurface_Release(surface);
+    ok(ref1 == 0, "Expected Render target refcount == 0, got %u.\n", ref1);
+    IDirect3DRM_Release(d3drm1);
+    IDirectDraw_Release(ddraw);
     DestroyWindow(window);
 }
 
-- 
2.3.2 (Apple Git-55)

