From 73fe89c000f6b610b833d79868b4a4d7b4441926 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         | 32 +++++++++++++++++-----
 dlls/d3drm/d3drm_private.h |  2 +-
 dlls/d3drm/device.c        | 67 ++++++++++++++++++++++++++++++++--------------
 dlls/d3drm/tests/d3drm.c   | 24 +++++++----------
 4 files changed, 83 insertions(+), 42 deletions(-)

diff --git a/dlls/d3drm/d3drm.c b/dlls/d3drm/d3drm.c
index f4b240b..aa147e9 100644
--- a/dlls/d3drm/d3drm.c
+++ b/dlls/d3drm/d3drm.c
@@ -247,18 +247,38 @@ 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;
+    hr = d3drm_device_init(object, 1, &d3drm->IDirect3DRM_iface, ddraw, backbuffer, TRUE, desc.dwWidth, desc.dwHeight);
+    if (SUCCEEDED(hr))
+        *device = IDirect3DRMDevice_from_impl(object);
+    else
+        d3drm_device_destroy(object);
+
+    return hr;
 }
 
 static HRESULT WINAPI d3drm1_CreateDeviceFromD3D(IDirect3DRM *iface,
@@ -312,7 +332,7 @@ static HRESULT WINAPI d3drm1_CreateDeviceFromClipper(IDirect3DRM *iface,
         return hr;
     }
 
-    hr = d3drm_device_init(object, 1, iface, ddraw, render_target, width, height);
+    hr = d3drm_device_init(object, 1, iface, ddraw, render_target, TRUE, width, height);
     IDirectDraw_Release(ddraw);
     IDirectDrawSurface_Release(render_target);
     if (FAILED(hr))
@@ -1135,7 +1155,7 @@ static HRESULT WINAPI d3drm3_CreateDeviceFromClipper(IDirect3DRM3 *iface,
         return hr;
     }
 
-    hr = d3drm_device_init(object, 3, &d3drm->IDirect3DRM_iface, ddraw, render_target, width, height);
+    hr = d3drm_device_init(object, 3, &d3drm->IDirect3DRM_iface, ddraw, render_target, TRUE, width, height);
     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 1b364bb..a6e0f3b 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 d3drm_device_init(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 87b231e..dbbc6cc 100644
--- a/dlls/d3drm/device.c
+++ b/dlls/d3drm/device.c
@@ -160,8 +160,9 @@ HRESULT d3drm_device_create_surfaces_from_clipper(struct d3drm_device *object, I
 }
 
 HRESULT d3drm_device_init(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;
@@ -172,36 +173,62 @@ 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);
 
-    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 6d3fe41..12288c7 100644
--- a/dlls/d3drm/tests/d3drm.c
+++ b/dlls/d3drm/tests/d3drm.c
@@ -2543,10 +2543,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);
@@ -2555,9 +2553,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,
@@ -2566,7 +2564,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;
 
@@ -2605,7 +2603,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);
 
@@ -2635,7 +2632,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;
 
@@ -2657,13 +2654,10 @@ cleanup:
         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);
-    }
+    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);
-- 
2.3.2 (Apple Git-55)

