From 1c88239469491b8d10b04a1b54632f29445b38c1 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       | 42 ++++++++++++++++++++++++++++++++++--------
 dlls/d3drm/tests/d3drm.c | 42 +++++++++++++++++++-----------------------
 2 files changed, 53 insertions(+), 31 deletions(-)

diff --git a/dlls/d3drm/d3drm.c b/dlls/d3drm/d3drm.c
index aa147e9..86e587c 100644
--- a/dlls/d3drm/d3drm.c
+++ b/dlls/d3drm/d3drm.c
@@ -678,16 +678,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;
 }
@@ -1089,18 +1093,40 @@ 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;
+    hr = d3drm_device_init(object, 3, &d3drm->IDirect3DRM_iface, ddraw, backbuffer, use_z_surface, desc.dwWidth, desc.dwHeight);
+    if (SUCCEEDED(hr))
+        *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 12288c7..d7f0230 100644
--- a/dlls/d3drm/tests/d3drm.c
+++ b/dlls/d3drm/tests/d3drm.c
@@ -2717,7 +2717,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);
@@ -2729,11 +2729,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,
@@ -2742,7 +2742,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;
 
@@ -2813,7 +2813,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;
 
@@ -2835,13 +2835,10 @@ cleanup:
         IDirect3DDevice2_Release(d3ddevice2);
     IDirect3DRMDevice2_Release(device2);
     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);
-    }
+    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);
     if (surface)
     {
         ref1 = IDirectDrawSurface_Release(surface);
@@ -2903,7 +2900,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);
@@ -2915,11 +2912,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,
@@ -2928,7 +2925,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;
 
@@ -2955,8 +2952,8 @@ static void test_create_device_from_surface3(void)
     IDirectDrawSurface_Release(ds);
     IDirect3DDevice2_Release(d3ddevice2);
     IDirectDrawSurface_Release(d3drm_surface);
-
     IDirect3DRMDevice3_Release(device3);
+
     ref3 = get_refcount((IUnknown *)d3drm1);
     ok(ref1 == ref3, "expected ref1 == ref3, got ref1 = %u, ref3 = %u.\n", ref1, ref3);
     ref3 = get_refcount((IUnknown *)d3drm3);
@@ -2969,7 +2966,6 @@ static void test_create_device_from_surface3(void)
     todo_wine ok(hr == DDERR_NOTFOUND, "Expected hr == DDERR_NOTFOUND, got %x.\n", hr);
     if (SUCCEEDED(hr))
         IDirectDrawSurface_Release(ds);
-
     ref1 = IDirectDrawSurface_Release(surface);
     ok(ref1 == 0, "Expected Render target refcount == 0, got %u.\n", ref1);
 
@@ -2999,7 +2995,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;
 
@@ -3018,7 +3014,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*/
@@ -3031,7 +3027,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;
 
@@ -3049,7 +3045,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*/
@@ -3074,7 +3070,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)

