From f4f13f63b32ac5725f056d94fb517979c28c253c Mon Sep 17 00:00:00 2001
From: Aaryaman Vasishta <jem456.vasishta@gmail.com>
Date: Tue, 7 Jul 2015 01:26:45 +0530
Subject: d3drm: Implement IDirect3DRM*::CreateDeviceFromSurface (not
 finalised)
Reply-To: wine-devel <wine-devel@winehq.org>

The only problem I see here is the segfault when you release the surface you created in the tests.
---
 dlls/d3drm/d3drm.c       | 81 ++++++++++++++++++++++++++++++++++++++++++++----
 dlls/d3drm/tests/d3drm.c | 36 ++++++++++-----------
 2 files changed, 93 insertions(+), 24 deletions(-)

diff --git a/dlls/d3drm/d3drm.c b/dlls/d3drm/d3drm.c
index 299cdb3..3f1f07f3 100644
--- a/dlls/d3drm/d3drm.c
+++ b/dlls/d3drm/d3drm.c
@@ -239,10 +239,30 @@ static HRESULT WINAPI d3drm1_CreateDevice(IDirect3DRM *iface,
 static HRESULT WINAPI d3drm1_CreateDeviceFromSurface(IDirect3DRM *iface, GUID *guid,
         IDirectDraw *ddraw, IDirectDrawSurface *backbuffer, IDirect3DRMDevice **device)
 {
-    FIXME("iface %p, guid %s, ddraw %p, backbuffer %p, device %p partial stub.\n",
+    struct d3drm *d3drm = impl_from_IDirect3DRM(iface);
+    DDSURFACEDESC desc;
+    HRESULT hr;
+
+    TRACE("iface %p, guid %s, ddraw %p, backbuffer %p, device %p.\n",
             iface, debugstr_guid(guid), ddraw, backbuffer, device);
 
-    return Direct3DRMDevice_create(&IID_IDirect3DRMDevice, (IUnknown **)device);
+    hr = Direct3DRMDevice_create(&IID_IDirect3DRMDevice, (IUnknown **)device);
+    if (FAILED(hr))
+        return hr;
+
+    desc.dwSize = sizeof(desc);
+    hr = IDirectDrawSurface_GetSurfaceDesc(backbuffer, &desc);
+    if (FAILED(hr))
+    {
+        IDirect3DRMDevice_Release(*device);
+        return hr;
+    }
+
+    hr = init_device(&d3drm->IDirect3DRM_iface, guid, ddraw, backbuffer, TRUE, NULL, desc.dwWidth, desc.dwHeight, (void **)device);
+    if (SUCCEEDED(hr))
+        IDirectDraw_AddRef(ddraw);
+
+    return hr;
 }
 
 static HRESULT WINAPI d3drm1_CreateDeviceFromD3D(IDirect3DRM *iface,
@@ -643,10 +663,22 @@ static HRESULT WINAPI d3drm2_CreateDevice(IDirect3DRM2 *iface,
 static HRESULT WINAPI d3drm2_CreateDeviceFromSurface(IDirect3DRM2 *iface, GUID *guid,
         IDirectDraw *ddraw, IDirectDrawSurface *backbuffer, IDirect3DRMDevice2 **device)
 {
-    FIXME("iface %p, guid %s, ddraw %p, backbuffer %p, device %p partial stub.\n",
+    struct d3drm *d3drm = impl_from_IDirect3DRM2(iface);
+    IDirect3DRMDevice3 *device3;
+    HRESULT hr;
+    TRACE("iface %p, guid %s, ddraw %p, backbuffer %p, device %p.\n",
             iface, debugstr_guid(guid), ddraw, backbuffer, device);
 
-    return Direct3DRMDevice_create(&IID_IDirect3DRMDevice2, (IUnknown **)device);
+    hr = IDirect3DRM3_CreateDeviceFromSurface(&d3drm->IDirect3DRM3_iface, guid, ddraw, backbuffer, 0, &device3);
+    if (FAILED(hr))
+        return hr;
+
+    hr = IDirect3DRMDevice3_QueryInterface(device3, &IID_IDirect3DRMDevice2, (void**)device);
+    IDirect3DRMDevice3_Release(device3);
+    if (FAILED(hr))
+        return hr;
+
+    return D3DRM_OK;
 }
 
 static HRESULT WINAPI d3drm2_CreateDeviceFromD3D(IDirect3DRM2 *iface,
@@ -680,6 +712,7 @@ static HRESULT WINAPI d3drm2_CreateDeviceFromClipper(IDirect3DRM2 *iface,
 
     return D3DRM_OK;
 }
+
 static HRESULT WINAPI d3drm2_CreateTextureFromSurface(IDirect3DRM2 *iface,
         IDirectDrawSurface *surface, IDirect3DRMTexture2 **texture)
 {
@@ -1029,10 +1062,46 @@ static HRESULT WINAPI d3drm3_CreateDevice(IDirect3DRM3 *iface,
 static HRESULT WINAPI d3drm3_CreateDeviceFromSurface(IDirect3DRM3 *iface, GUID *guid,
         IDirectDraw *ddraw, IDirectDrawSurface *backbuffer, DWORD flags, IDirect3DRMDevice3 **device)
 {
-    FIXME("iface %p, guid %s, ddraw %p, backbuffer %p, flags %#x, device %p partial stub.\n",
+    struct d3drm *d3drm = impl_from_IDirect3DRM3(iface);
+    IDirect3D2 *d3d2;
+    DDSURFACEDESC desc;
+    HRESULT hr;
+
+    TRACE("iface %p, guid %s, ddraw %p, backbuffer %p, flags %#x, device %p.\n",
             iface, debugstr_guid(guid), ddraw, backbuffer, flags, device);
 
-    return Direct3DRMDevice_create(&IID_IDirect3DRMDevice3, (IUnknown **)device);
+    hr = Direct3DRMDevice_create(&IID_IDirect3DRMDevice3, (IUnknown **)device);
+    if (FAILED(hr))
+        return hr;
+
+    hr = IDirectDraw_QueryInterface(ddraw, &IID_IDirect3D2, (void **)&d3d2);
+    if (FAILED(hr))
+        return hr;
+
+    desc.dwSize = sizeof(desc);
+    hr = IDirectDrawSurface_GetSurfaceDesc(backbuffer, &desc);
+    if (FAILED(hr))
+    {
+        IDirect3D2_Release(d3d2);
+        IDirect3DRMDevice3_Release(*device);
+        return hr;
+    }
+    if (!(desc.ddsCaps.dwCaps & DDSCAPS_3DDEVICE))
+    {
+        IDirect3D2_Release(d3d2);
+        IDirect3DRMDevice3_Release(*device);
+        return DDERR_INVALIDCAPS;
+    }
+
+    if (flags & D3DRMDEVICE_NOZBUFFER)
+        hr = init_device(&d3drm->IDirect3DRM_iface, guid, ddraw, backbuffer, FALSE, d3d2, desc.dwWidth, desc.dwHeight, (void **)device);
+    else
+        hr = init_device(&d3drm->IDirect3DRM_iface, guid, ddraw, backbuffer, TRUE, d3d2, desc.dwWidth, desc.dwHeight, (void **)device);
+    if (SUCCEEDED(hr))
+        IDirectDraw_AddRef(ddraw);
+    IDirect3D2_Release(d3d2);
+
+    return hr;
 }
 
 static HRESULT WINAPI d3drm3_CreateDeviceFromD3D(IDirect3DRM3 *iface,
diff --git a/dlls/d3drm/tests/d3drm.c b/dlls/d3drm/tests/d3drm.c
index c7f028f..ef70abf 100644
--- a/dlls/d3drm/tests/d3drm.c
+++ b/dlls/d3drm/tests/d3drm.c
@@ -2563,9 +2563,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,
@@ -2574,7 +2574,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;
 
@@ -2644,7 +2644,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;
 
@@ -2668,7 +2668,7 @@ cleanup:
     {
         IDirect3DRMDevice_Release(device1);
         hr = IDirectDrawSurface_GetAttachedSurface(surface, &caps, &ds);
-        todo_wine ok(hr == DD_OK, "Cannot get attached depth surface (hr = %x).\n", hr);
+        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*/
@@ -2735,7 +2735,7 @@ static void test_create_device_from_surface2(void)
     ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
 
     hr = IDirect3DRM2_CreateDeviceFromSurface(d3drm2, &driver, ddraw, surface, &device2);
-    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);
 
     desc.ddsCaps.dwCaps |= DDSCAPS_3DDEVICE;
@@ -2745,11 +2745,11 @@ static void test_create_device_from_surface2(void)
     hr = IDirect3DRM2_CreateDeviceFromSurface(d3drm2, &driver, ddraw, surface, &device2);
     ok(SUCCEEDED(hr), "Cannot create IDirect3DRMDevice2 interface (hr = %x).\n", hr);
     ref3 = get_refcount((IUnknown *)d3drm1);
-    todo_wine ok(ref3 > ref1, "expected ref3 > ref1, got ref1 = %u , ref3 = %u.\n", ref1, ref3);
+    ok(ref3 > ref1, "expected ref3 > ref1, got ref1 = %u , ref3 = %u.\n", ref1, ref3);
     ref3 = get_refcount((IUnknown *)d3drm2);
     ok(ref3 == ref2, "expected ref3 == ref2, got ref2 = %u , ref3 = %u.\n", ref2, ref3);
     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,
@@ -2758,7 +2758,7 @@ static void test_create_device_from_surface2(void)
     ok(d3drm_surface == NULL, "No primary surface should have enumerated (%p).\n", d3drm_surface);
 
     hr = IDirect3DRMDevice2_GetDirect3DDevice2(device2, &d3ddevice2);
-    todo_wine ok(hr == D3DRM_OK, "Cannot get IDirect3DDevice2 interface (hr = %x).\n", hr);
+    ok(hr == D3DRM_OK, "Cannot get IDirect3DDevice2 interface (hr = %x).\n", hr);
     if (FAILED(hr))
         goto cleanup;
 
@@ -2830,7 +2830,7 @@ static void test_create_device_from_surface2(void)
     ok(SUCCEEDED(hr), "Cannot create IDirect3DRMDevice2 interface (hr = %x).\n", hr);
 
     hr = IDirect3DRMDevice2_GetDirect3DDevice2(device2, &d3ddevice2);
-    todo_wine ok(hr == D3DRM_OK, "Cannot get IDirect3DDevice2 interface (hr = %x).\n", hr);
+    ok(hr == D3DRM_OK, "Cannot get IDirect3DDevice2 interface (hr = %x).\n", hr);
     if (FAILED(hr))
         goto cleanup;
 
@@ -2854,7 +2854,7 @@ cleanup:
     {
         IDirect3DRMDevice2_Release(device2);
         hr = IDirectDrawSurface_GetAttachedSurface(surface, &caps, &ds);
-        todo_wine ok(hr == DD_OK, "Cannot get attached depth surface (hr = %x).\n", hr);
+        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*/
@@ -2923,7 +2923,7 @@ static void test_create_device_from_surface3(void)
     ok(SUCCEEDED(hr), "Failed to create surface, hr %#x.\n", hr);
 
     hr = IDirect3DRM3_CreateDeviceFromSurface(d3drm3, &driver, ddraw, surface, 0, &device3);
-    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);
 
     desc.ddsCaps.dwCaps |= DDSCAPS_3DDEVICE;
@@ -2933,11 +2933,11 @@ static void test_create_device_from_surface3(void)
     hr = IDirect3DRM3_CreateDeviceFromSurface(d3drm3, &driver, ddraw, surface, 0, &device3);
     ok(SUCCEEDED(hr), "Cannot create IDirect3DRMDevice3 interface (hr = %x).\n", hr);
     ref3 = get_refcount((IUnknown *)d3drm1);
-    todo_wine ok(ref3 > ref1, "expected ref3 > ref1, got ref1 = %u , ref3 = %u.\n", ref1, ref3);
+    ok(ref3 > ref1, "expected ref3 > ref1, got ref1 = %u , ref3 = %u.\n", ref1, ref3);
     ref3 = get_refcount((IUnknown *)d3drm3);
     ok(ref3 == ref2, "expected ref3 == ref2, got ref2 = %u , ref3 = %u.\n", ref2, ref3);
     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,
@@ -2946,7 +2946,7 @@ static void test_create_device_from_surface3(void)
     ok(d3drm_surface == NULL, "No primary surface should have enumerated (%p).\n", d3drm_surface);
 
     hr = IDirect3DRMDevice3_GetDirect3DDevice2(device3, &d3ddevice2);
-    todo_wine ok(hr == D3DRM_OK, "Cannot get IDirect3DDevice2 interface (hr = %x).\n", hr);
+    ok(hr == D3DRM_OK, "Cannot get IDirect3DDevice2 interface (hr = %x).\n", hr);
     if (FAILED(hr))
         goto cleanup;
 
@@ -3016,7 +3016,7 @@ static void test_create_device_from_surface3(void)
     ok(SUCCEEDED(hr), "Cannot create IDirect3DRMDevice3 interface (hr = %x).\n", hr);
 
     hr = IDirect3DRMDevice3_GetDirect3DDevice2(device3, &d3ddevice2);
-    todo_wine ok(hr == D3DRM_OK, "Cannot get IDirect3DDevice2 interface (hr = %x).\n", hr);
+    ok(hr == D3DRM_OK, "Cannot get IDirect3DDevice2 interface (hr = %x).\n", hr);
     if (FAILED(hr))
         goto cleanup;
 
@@ -3035,7 +3035,7 @@ static void test_create_device_from_surface3(void)
     IDirect3DDevice2_Release(d3ddevice2);
     IDirect3DRMDevice3_Release(device3);
     hr = IDirectDrawSurface_GetAttachedSurface(surface, &caps, &ds);
-    todo_wine ok(hr == DD_OK, "Cannot get attached depth surface (hr = %x).\n", hr);
+    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*/
@@ -3060,7 +3060,7 @@ static void test_create_device_from_surface3(void)
     ok(SUCCEEDED(hr), "Cannot create IDirect3DRMDevice3 interface (hr = %x).\n", hr);
 
     hr = IDirect3DRMDevice3_GetDirect3DDevice2(device3, &d3ddevice2);
-    todo_wine ok(hr == D3DRM_OK, "Cannot get IDirect3DDevice2 interface (hr = %x).\n", hr);
+    ok(hr == D3DRM_OK, "Cannot get IDirect3DDevice2 interface (hr = %x).\n", hr);
     if (FAILED(hr))
         goto cleanup;
 
-- 
2.3.2 (Apple Git-55)

