From c181738d97a5b74d77d817db5b9ae0f621aa7a33 Mon Sep 17 00:00:00 2001
From: Aaryaman Vasishta <jem456.vasishta@gmail.com>
Date: Wed, 29 Jul 2015 19:23:07 +0530
Subject: [PATCH 11/11] d3drm: Implement
 IDirect3DRM{2-3}::CreateDeviceFromSurface.
Reply-To: wine-devel <wine-devel@winehq.org>

---
 dlls/d3drm/d3drm.c       | 46 ++++++++++++++++++++++++++++++++++++++--------
 dlls/d3drm/tests/d3drm.c | 32 ++++++++++++++++----------------
 2 files changed, 54 insertions(+), 24 deletions(-)

diff --git a/dlls/d3drm/d3drm.c b/dlls/d3drm/d3drm.c
index ee50c53..8271a63 100644
--- a/dlls/d3drm/d3drm.c
+++ b/dlls/d3drm/d3drm.c
@@ -680,16 +680,20 @@ static HRESULT WINAPI d3drm2_CreateDevice(IDirect3DRM2 *iface,
 static HRESULT WINAPI d3drm2_CreateDeviceFromSurface(IDirect3DRM2 *iface, GUID *guid,
         IDirectDraw *ddraw, IDirectDrawSurface *backbuffer, IDirect3DRMDevice2 **device)
 {
-    struct d3drm_device *object = NULL;
+    struct d3drm *d3drm = impl_from_IDirect3DRM2(iface);
+    IDirect3DRMDevice3 *device3;
     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);
+    hr = IDirect3DRM3_CreateDeviceFromSurface(&d3drm->IDirect3DRM3_iface, guid, ddraw, backbuffer, 0, &device3);
     if (FAILED(hr))
         return hr;
 
-    *device = IDirect3DRMDevice2_from_impl(object);
+    hr = IDirect3DRMDevice3_QueryInterface(device3, &IID_IDirect3DRMDevice2, (void**)device);
+    IDirect3DRMDevice3_Release(device3);
+    if (FAILED(hr))
+        return hr;
 
     return D3DRM_OK;
 }
@@ -1091,18 +1095,44 @@ static HRESULT WINAPI d3drm3_CreateDevice(IDirect3DRM3 *iface,
 static HRESULT WINAPI d3drm3_CreateDeviceFromSurface(IDirect3DRM3 *iface, GUID *guid,
         IDirectDraw *ddraw, IDirectDrawSurface *backbuffer, DWORD flags, IDirect3DRMDevice3 **device)
 {
-    struct d3drm_device *object = NULL;
+    struct d3drm *d3drm = impl_from_IDirect3DRM3(iface);
+    struct d3drm_device *object;
+    DDSURFACEDESC desc;
+    BOOL use_z_surface;
     HRESULT hr;
-    FIXME("iface %p, guid %s, ddraw %p, backbuffer %p, flags %#x, device %p partial stub.\n",
+
+    TRACE("iface %p, guid %s, ddraw %p, backbuffer %p, flags %#x, device %p.\n",
             iface, debugstr_guid(guid), ddraw, backbuffer, flags, device);
 
     hr = Direct3DRMDevice_create(&object);
     if (FAILED(hr))
         return hr;
 
-    *device = IDirect3DRMDevice3_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;
+    }
+    use_z_surface = !(flags & D3DRMDEVICE_NOZBUFFER);
 
-    return D3DRM_OK;
+    IDirectDrawSurface_AddRef(backbuffer);
+    hr = init_device(object, 3, &d3drm->IDirect3DRM_iface, ddraw, backbuffer, use_z_surface, desc.dwWidth, desc.dwHeight);
+    if (SUCCEEDED(hr))
+    {
+        IDirectDraw_AddRef(ddraw);
+        *device = IDirect3DRMDevice3_from_impl(object);
+    }
+    else
+        d3drm_device_destroy(object);
+
+    return hr;
 }
 
 static HRESULT WINAPI d3drm3_CreateDeviceFromD3D(IDirect3DRM3 *iface,
diff --git a/dlls/d3drm/tests/d3drm.c b/dlls/d3drm/tests/d3drm.c
index cc0b329..12a4b87 100644
--- a/dlls/d3drm/tests/d3drm.c
+++ b/dlls/d3drm/tests/d3drm.c
@@ -2720,7 +2720,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);
     if (SUCCEEDED(hr))
         IDirect3DRMDevice2_Release(device2);
@@ -2732,11 +2732,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,
@@ -2745,7 +2745,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;
 
@@ -2817,7 +2817,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;
 
@@ -2841,7 +2841,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*/
@@ -2910,7 +2910,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);
     if (SUCCEEDED(hr))
         IDirect3DRMDevice3_Release(device3);
@@ -2922,11 +2922,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,
@@ -2935,7 +2935,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;
 
@@ -2974,7 +2974,7 @@ static void test_create_device_from_surface3(void)
                 surface_ref1, surface_ref2);
         /* In version 3, d3drm will destroy all references of the depth surface it created internally. */
         hr = IDirectDrawSurface_GetAttachedSurface(surface, &caps, &ds);
-        ok(hr == DDERR_NOTFOUND, "Expected hr == DDERR_NOTFOUND, got %x.\n", hr);
+        todo_wine ok(hr == DDERR_NOTFOUND, "Expected hr == DDERR_NOTFOUND, got %x.\n", hr);
     }
     ref1 = IDirectDrawSurface_Release(surface);
     ok(ref1 == 0, "Expected Render target refcount == 0, got %u.\n", ref1);
@@ -3005,7 +3005,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;
 
@@ -3024,7 +3024,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*/
@@ -3037,7 +3037,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;
 
@@ -3055,7 +3055,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*/
@@ -3080,7 +3080,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)

