From 3952a44672215b5716929e652d7c384b4c962b0d 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 10/11] d3drm: Implement
 IDirect3DRMDevice::CreateDeviceFromSurface.
Reply-To: wine-devel <wine-devel@winehq.org>

---
 dlls/d3drm/d3drm.c         | 36 ++++++++++++++++++++-----
 dlls/d3drm/d3drm_private.h |  2 +-
 dlls/d3drm/device.c        | 67 ++++++++++++++++++++++++++++++++--------------
 dlls/d3drm/tests/d3drm.c   | 56 +++++++++++++++++---------------------
 4 files changed, 103 insertions(+), 58 deletions(-)

diff --git a/dlls/d3drm/d3drm.c b/dlls/d3drm/d3drm.c
index 1f07257..ee50c53 100644
--- a/dlls/d3drm/d3drm.c
+++ b/dlls/d3drm/d3drm.c
@@ -247,18 +247,42 @@ 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;
+    DDSURFACEDESC desc;
     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);
+    desc.dwSize = sizeof(desc);
+    hr = IDirectDrawSurface_GetSurfaceDesc(backbuffer, &desc);
+    if (FAILED(hr))
+    {
+        d3drm_device_destroy(object);
+        return hr;
+    }
+    if (!(desc.ddsCaps.dwCaps & DDSCAPS_3DDEVICE))
+    {
+        d3drm_device_destroy(object);
+        return DDERR_INVALIDCAPS;
+    }
 
-    return D3DRM_OK;
+    IDirectDrawSurface_AddRef(backbuffer);
+    hr = init_device(object, 1, &d3drm->IDirect3DRM_iface, ddraw, backbuffer, TRUE, desc.dwWidth, desc.dwHeight);
+    if (SUCCEEDED(hr))
+    {
+        IDirectDraw_AddRef(ddraw);
+        *device = IDirect3DRMDevice_from_impl(object);
+    }
+    else
+        d3drm_device_destroy(object);
+    
+    return hr;
 }
 
 static HRESULT WINAPI d3drm1_CreateDeviceFromD3D(IDirect3DRM *iface,
@@ -312,7 +336,7 @@ static HRESULT WINAPI d3drm1_CreateDeviceFromClipper(IDirect3DRM *iface,
         return hr;
     }
 
-    hr = init_device(object, 1, iface, ddraw, render_target, width, height);
+    hr = init_device(object, 1, iface, ddraw, render_target, TRUE, width, height);
     if (FAILED(hr))
         d3drm_device_destroy(object);
     else
@@ -1133,7 +1157,7 @@ static HRESULT WINAPI d3drm3_CreateDeviceFromClipper(IDirect3DRM3 *iface,
         return hr;
     }
 
-    hr = init_device(object, 3, &d3drm->IDirect3DRM_iface, ddraw, render_target, width, height);
+    hr = init_device(object, 3, &d3drm->IDirect3DRM_iface, ddraw, render_target, TRUE, width, height);
     if (FAILED(hr))
         d3drm_device_destroy(object);
     else
diff --git a/dlls/d3drm/d3drm_private.h b/dlls/d3drm/d3drm_private.h
index fd9a914..071a1c6 100644
--- a/dlls/d3drm/d3drm_private.h
+++ b/dlls/d3drm/d3drm_private.h
@@ -47,7 +47,7 @@ HRESULT d3drm_device_create_surfaces_from_clipper(struct d3drm_device *object, I
             IDirectDrawSurface **surface) DECLSPEC_HIDDEN;
 
 HRESULT init_device(struct d3drm_device *device, UINT version, IDirect3DRM *d3drm, IDirectDraw *ddraw, IDirectDrawSurface *surface,
-            int width, int height) DECLSPEC_HIDDEN;
+            BOOL z_surface, int width, int height) DECLSPEC_HIDDEN;
 
 struct d3drm_file_header
 {
diff --git a/dlls/d3drm/device.c b/dlls/d3drm/device.c
index 50070a1..2506775 100644
--- a/dlls/d3drm/device.c
+++ b/dlls/d3drm/device.c
@@ -157,8 +157,9 @@ HRESULT d3drm_device_create_surfaces_from_clipper(struct d3drm_device *object, I
 }
 
 HRESULT init_device(struct d3drm_device *device, UINT version, IDirect3DRM *d3drm, IDirectDraw *ddraw, IDirectDrawSurface *surface,
-            int width, int height)
+            BOOL z_surface, int width, int height)
 {
+    DDSCAPS caps = { DDSCAPS_ZBUFFER };
     IDirectDrawSurface *ds = NULL;
     IDirect3DDevice *device1 = NULL;
     IDirect3DDevice2 *device2 = NULL;
@@ -168,35 +169,61 @@ HRESULT init_device(struct d3drm_device *device, UINT version, IDirect3DRM *d3dr
 
     device->ddraw = ddraw;
     device->d3drm = d3drm;
+    device->render_target = surface;
     IDirect3DRM_AddRef(d3drm);
     device->render_target = 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 = width;
-    surface_desc.dwHeight = height;
-    hr = IDirectDraw_CreateSurface(ddraw, &surface_desc, &ds, NULL);
-    if (FAILED(hr))
-        return hr;
+    hr = IDirectDrawSurface_GetAttachedSurface(surface, &caps, &ds);
+    if (SUCCEEDED(hr))
+    {
+        z_surface = FALSE;
+        IDirectDrawSurface_Release(ds);
+    }
 
-    hr = IDirectDrawSurface_AddAttachedSurface(surface, ds);
-    IDirectDrawSurface_Release(ds);
-    if (FAILED(hr))
-        return hr;
+    if (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 = width;
+        surface_desc.dwHeight = height;
+        hr = IDirectDraw_CreateSurface(ddraw, &surface_desc, &ds, NULL);
+        if (FAILED(hr))
+            return hr;
 
-    if (version == 1)
-        hr = IDirectDrawSurface_QueryInterface(surface, &IID_IDirect3DRGBDevice, (void **)&device1);
+        hr = IDirectDrawSurface_AddAttachedSurface(surface, ds);
+        IDirectDrawSurface_Release(ds);
+        if (FAILED(hr))
+            return hr;
+
+        if (version == 1)
+            hr = IDirectDrawSurface_QueryInterface(surface, &IID_IDirect3DRGBDevice, (void **)&device1);
+        else
+        {
+            IDirectDraw_QueryInterface(ddraw, &IID_IDirect3D2, (void**)&d3d2);
+            hr = IDirect3D2_CreateDevice(d3d2, &IID_IDirect3DRGBDevice, surface, &device2);
+        }
+        if (FAILED(hr))
+        {
+            IDirectDrawSurface_DeleteAttachedSurface(surface, 0, ds);
+            return hr;
+        }
+    }
     else
     {
-        IDirectDraw_QueryInterface(ddraw, &IID_IDirect3D2, (void**)&d3d2);
-        hr = IDirect3D2_CreateDevice(d3d2, &IID_IDirect3DRGBDevice, surface, &device2);
+        if (version == 1)
+            hr = IDirectDrawSurface_QueryInterface(surface, &IID_IDirect3DRGBDevice, (void **)&device1);
+        else
+        {
+            IDirectDraw_QueryInterface(ddraw, &IID_IDirect3D2, (void**)&d3d2);
+            hr = IDirect3D2_CreateDevice(d3d2, &IID_IDirect3DRGBDevice, surface, &device2);
+        }
     }
     if (FAILED(hr))
     {
-        IDirectDrawSurface_DeleteAttachedSurface(surface, 0, ds);
+        IDirect3D2_Release(d3d2);
         return hr;
     }
 
diff --git a/dlls/d3drm/tests/d3drm.c b/dlls/d3drm/tests/d3drm.c
index 50e7ee2..cc0b329 100644
--- a/dlls/d3drm/tests/d3drm.c
+++ b/dlls/d3drm/tests/d3drm.c
@@ -2542,10 +2542,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);
@@ -2554,9 +2552,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,
@@ -2565,7 +2563,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);
+    ok(hr == D3DRM_OK, "Cannot get IDirect3DDevice interface (hr = %x).\n", hr);
     if (FAILED(hr))
         goto cleanup;
 
@@ -2592,20 +2590,17 @@ static void test_create_device_from_surface1(void)
     IDirectDrawSurface_Release(ds);
     IDirect3DDevice_Release(d3ddevice1);
     IDirectDrawSurface_Release(d3drm_surface);
-    if (device1)
-    {
-        IDirect3DRMDevice_Release(device1);
-        ref2 = get_refcount((IUnknown *)d3drm1);
-        ok(ref1 == ref2, "expected ref1 == ref2, got ref1 = %u, ref2 = %u.\n", ref1, ref2);
-        surface_ref2 = get_refcount((IUnknown *)surface);
-        ok(surface_ref2 == surface_ref1, "Expected surface_ref2 == surface_ref1, got surface_ref1 = %u, surface_ref2 = %u.\n",
-                surface_ref1, surface_ref2);
-        hr = IDirectDrawSurface_GetAttachedSurface(surface, &caps, &ds);
-        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);
-    }
+    IDirect3DRMDevice_Release(device1);
+    ref2 = get_refcount((IUnknown *)d3drm1);
+    ok(ref1 == ref2, "expected ref1 == ref2, got ref1 = %u, ref2 = %u.\n", ref1, ref2);
+    surface_ref2 = get_refcount((IUnknown *)surface);
+    ok(surface_ref2 == surface_ref1, "Expected surface_ref2 == surface_ref1, got surface_ref1 = %u, surface_ref2 = %u.\n",
+            surface_ref1, surface_ref2);
+    hr = IDirectDrawSurface_GetAttachedSurface(surface, &caps, &ds);
+    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);
 
@@ -2635,7 +2630,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);
+    ok(hr == D3DRM_OK, "Cannot get IDirect3DDevice interface (hr = %x).\n", hr);
     if (FAILED(hr))
         goto cleanup;
 
@@ -2655,18 +2650,17 @@ static void test_create_device_from_surface1(void)
 cleanup:
     if (d3ddevice1)
         IDirect3DDevice_Release(d3ddevice1);
-    if (device1)
+
+    IDirect3DRMDevice_Release(device1);
+    hr = IDirectDrawSurface_GetAttachedSurface(surface, &caps, &ds);
+    ok(hr == DD_OK, "Cannot get attached depth surface (hr = %x).\n", hr);
+    if (SUCCEEDED(hr))
     {
-        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);
-        }
+        /*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);
-- 
2.3.2 (Apple Git-55)

