From c16b2382301f74d5fca4674059ba3cf67a116359 Mon Sep 17 00:00:00 2001
From: Aaryaman Vasishta <jem456.vasishta@gmail.com>
Date: Wed, 15 Jul 2015 02:09:53 +0530
Subject: [PATCH 3/8] d3drm: Implement
 IDirect3DRMDevice::CreateDeviceFromClipper.
Reply-To: wine-devel <wine-devel@winehq.org>

---
 dlls/d3drm/Makefile.in     |   2 +-
 dlls/d3drm/d3drm.c         |  33 +++++++++-
 dlls/d3drm/d3drm_private.h |   6 +-
 dlls/d3drm/device.c        | 149 ++++++++++++++++++++++++++++++++++++++++++---
 dlls/d3drm/tests/d3drm.c   |  12 ++--
 5 files changed, 180 insertions(+), 22 deletions(-)

diff --git a/dlls/d3drm/Makefile.in b/dlls/d3drm/Makefile.in
index 35d7af0..5db3494 100644
--- a/dlls/d3drm/Makefile.in
+++ b/dlls/d3drm/Makefile.in
@@ -1,6 +1,6 @@
 MODULE    = d3drm.dll
 IMPORTLIB = d3drm
-IMPORTS   = dxguid uuid d3dxof
+IMPORTS   = dxguid uuid d3dxof ddraw
 
 C_SRCS = \
 	d3drm.c \
diff --git a/dlls/d3drm/d3drm.c b/dlls/d3drm/d3drm.c
index 5ea991f..50d865e 100644
--- a/dlls/d3drm/d3drm.c
+++ b/dlls/d3drm/d3drm.c
@@ -283,17 +283,44 @@ static HRESULT WINAPI d3drm1_CreateDeviceFromClipper(IDirect3DRM *iface,
         IDirect3DRMDevice **device)
 {
     struct d3drm_device *object = NULL;
+    IDirectDraw *ddraw;
+    IDirectDrawSurface *render_target;
     HRESULT hr;
-    FIXME("iface %p, clipper %p, guid %s, width %d, height %d, device %p.\n",
+
+    TRACE("iface %p, clipper %p, guid %s, width %d, height %d, device %p.\n",
             iface, clipper, debugstr_guid(guid), width, height, device);
 
+    if (!clipper || !width || !height)
+        return D3DRMERR_BADVALUE;
+
+    hr = DirectDrawCreate(NULL, &ddraw, NULL);
+    if (FAILED(hr))
+        return hr;
+
     hr = Direct3DRMDevice_create(&object);
     if (FAILED(hr))
+    {
+        IDirectDraw_Release(ddraw);
         return hr;
+    }
 
-    *device = IDirect3DRMDevice_from_impl(object);
+    hr = d3drm_device_create_surfaces_from_clipper(object, ddraw, clipper, width, height, &render_target);
+    if (FAILED(hr))
+    {
+        IDirectDraw_Release(ddraw);
+        d3drm_device_destroy(object);
+        return hr;
+    }
 
-    return D3DRM_OK;
+    hr = d3drm_device_init(object, 1, iface, ddraw, render_target);
+    IDirectDraw_Release(ddraw);
+    IDirectDrawSurface_Release(render_target);
+    if (FAILED(hr))
+        d3drm_device_destroy(object);
+    else
+        *device = IDirect3DRMDevice_from_impl(object);
+
+    return hr;
 }
 
 static HRESULT WINAPI d3drm1_CreateTextureFromSurface(IDirect3DRM *iface,
diff --git a/dlls/d3drm/d3drm_private.h b/dlls/d3drm/d3drm_private.h
index 3cf9c23..06079b9 100644
--- a/dlls/d3drm/d3drm_private.h
+++ b/dlls/d3drm/d3drm_private.h
@@ -41,9 +41,13 @@ HRESULT Direct3DRMTexture_create(REFIID riid, IUnknown** ret_iface) DECLSPEC_HID
 
 HRESULT load_mesh_data(IDirect3DRMMeshBuilder3 *iface, IDirectXFileData *data,
                        D3DRMLOADTEXTURECALLBACK load_texture_proc, void *arg) DECLSPEC_HIDDEN;
-HRESULT init_device(struct d3drm_device *device, REFIID riid, IUnknown **out) DECLSPEC_HIDDEN;
 void d3drm_device_destroy(struct d3drm_device *device) DECLSPEC_HIDDEN;
 
+HRESULT d3drm_device_create_surfaces_from_clipper(struct d3drm_device *object, IDirectDraw *ddraw, IDirectDrawClipper *clipper, int width, int height,
+            IDirectDrawSurface **surface) DECLSPEC_HIDDEN;
+
+HRESULT d3drm_device_init(struct d3drm_device *device, UINT version, IDirect3DRM *d3drm, IDirectDraw *ddraw, IDirectDrawSurface *surface) DECLSPEC_HIDDEN;
+
 struct d3drm_file_header
 {
     WORD major;
diff --git a/dlls/d3drm/device.c b/dlls/d3drm/device.c
index 2325d45..b824e41 100644
--- a/dlls/d3drm/device.c
+++ b/dlls/d3drm/device.c
@@ -37,6 +37,11 @@ struct d3drm_device
     IDirect3DRMDevice2 IDirect3DRMDevice2_iface;
     IDirect3DRMDevice3 IDirect3DRMDevice3_iface;
     IDirect3DRMWinDevice IDirect3DRMWinDevice_iface;
+    IDirect3DRM *d3drm;
+    IDirectDraw *ddraw;
+    IDirectDrawSurface *primary_surface, *render_target;
+    IDirectDrawClipper *clipper;
+    IDirect3DDevice *device;
     LONG ref;
     BOOL dither;
     D3DRMRENDERQUALITY quality;
@@ -75,17 +80,26 @@ IDirect3DRMDevice3 *IDirect3DRMDevice3_from_impl(struct d3drm_device *device)
     return &device->IDirect3DRMDevice3_iface;
 }
 
-HRESULT init_device(struct d3drm_device *device, REFIID riid, IUnknown **out)
-{
-    HRESULT hr = Direct3DRMDevice_create(&device);
-    if (FAILED(hr))
-        return hr;
-
-    return IDirect3DRMDevice3_QueryInterface(&device->IDirect3DRMDevice3_iface, riid, (void **)out);
-}
-
 void d3drm_device_destroy(struct d3drm_device *device)
 {
+    if (device->device)
+    {
+        TRACE("Releasing attached ddraw interfaces.\n");
+        IDirect3DDevice_Release(device->device);
+    }
+    if (device->render_target)
+        IDirectDrawSurface_Release(device->render_target);
+    if (device->primary_surface)
+    {
+        TRACE("Releasing primary surface and attached clipper.\n");
+        IDirectDrawSurface_Release(device->primary_surface);
+        IDirectDrawClipper_Release(device->clipper);
+    }
+    if (device->ddraw)
+    {
+        IDirectDraw_Release(device->ddraw);
+        IDirect3DRM_Release(device->d3drm);
+    }
     HeapFree(GetProcessHeap(), 0, device);
 }
 
@@ -94,6 +108,123 @@ static inline struct d3drm_device *impl_from_IDirect3DRMWinDevice(IDirect3DRMWin
     return CONTAINING_RECORD(iface, struct d3drm_device, IDirect3DRMWinDevice_iface);
 }
 
+HRESULT d3drm_device_create_surfaces_from_clipper(struct d3drm_device *object, IDirectDraw *ddraw, IDirectDrawClipper *clipper, int width, int height, IDirectDrawSurface **surface)
+{
+    DDSURFACEDESC surface_desc;
+    IDirectDrawSurface *primary_surface, *render_target;
+    HWND window;
+    HRESULT hr;
+
+    hr = IDirectDrawClipper_GetHWnd(clipper, &window);
+    if (FAILED(hr))
+        return hr;
+
+    hr = IDirectDraw_SetCooperativeLevel(ddraw, window, DDSCL_NORMAL);
+    if (FAILED(hr))
+        return hr;
+
+    memset(&surface_desc, 0, sizeof(surface_desc));
+    surface_desc.dwSize = sizeof(surface_desc);
+    surface_desc.dwFlags = DDSD_CAPS;
+    surface_desc.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
+    hr = IDirectDraw_CreateSurface(ddraw, &surface_desc, &primary_surface, NULL);
+    if (FAILED(hr))
+        return hr;
+    hr = IDirectDrawSurface_SetClipper(primary_surface, clipper);
+    if (FAILED(hr))
+    {
+        IDirectDrawSurface_Release(primary_surface);
+        return hr;
+    }
+
+    memset(&surface_desc, 0, sizeof(surface_desc));
+    surface_desc.dwSize = sizeof(surface_desc);
+    surface_desc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
+    surface_desc.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN | DDSCAPS_3DDEVICE;
+    surface_desc.dwWidth = width;
+    surface_desc.dwHeight = height;
+
+    hr = IDirectDraw_CreateSurface(ddraw, &surface_desc, &render_target, NULL);
+    if (FAILED(hr))
+    {
+        IDirectDrawSurface_Release(primary_surface);
+        return hr;
+    }
+
+    object->primary_surface = primary_surface;
+    object->clipper = clipper;
+    IDirectDrawClipper_AddRef(clipper);
+    *surface = render_target;
+
+    return D3DRM_OK;
+}
+
+HRESULT d3drm_device_init(struct d3drm_device *device, UINT version, IDirect3DRM *d3drm, IDirectDraw *ddraw, IDirectDrawSurface *surface)
+{
+    IDirectDrawSurface *ds = NULL;
+    IDirect3DDevice *device1 = NULL;
+    IDirect3DDevice2 *device2 = NULL;
+    IDirect3D2 *d3d2 = NULL;
+    DDSURFACEDESC desc, surface_desc;
+    HRESULT hr;
+
+    device->ddraw = ddraw;
+    IDirectDraw_AddRef(ddraw);
+    device->d3drm = d3drm;
+    IDirect3DRM_AddRef(d3drm);
+    device->render_target = surface;
+    IDirectDrawSurface_AddRef(surface);
+
+    desc.dwSize = sizeof(desc);
+    hr = IDirectDrawSurface_GetSurfaceDesc(surface, &desc);
+    if (FAILED(hr))
+        return hr;
+
+    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 = desc.dwWidth;
+    surface_desc.dwHeight = desc.dwHeight;
+    hr = IDirectDraw_CreateSurface(ddraw, &surface_desc, &ds, NULL);
+    if (FAILED(hr))
+        return hr;
+
+    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;
+    }
+
+    if (version != 1)
+    {
+        hr = IDirect3DDevice2_QueryInterface(device2, &IID_IDirect3DDevice, (void**)&device1);
+        IDirect3DDevice2_Release(device2);
+        IDirect3D2_Release(d3d2);
+        if (FAILED(hr))
+        {
+            IDirectDrawSurface_DeleteAttachedSurface(surface, 0, ds);
+            return hr;
+        }
+    }
+    device->device = device1;
+
+    return hr;
+}
+
 static HRESULT WINAPI d3drm_device1_QueryInterface(IDirect3DRMDevice *iface, REFIID riid, void **out)
 {
     struct d3drm_device *device = impl_from_IDirect3DRMDevice(iface);
diff --git a/dlls/d3drm/tests/d3drm.c b/dlls/d3drm/tests/d3drm.c
index d360591..b427014 100644
--- a/dlls/d3drm/tests/d3drm.c
+++ b/dlls/d3drm/tests/d3drm.c
@@ -1993,22 +1993,18 @@ static void test_create_device_from_clipper1(void)
     cref1 = get_refcount((IUnknown *)clipper);
 
     hr = IDirect3DRM_CreateDeviceFromClipper(d3drm1, clipper, &driver, 0, 0, &device1);
-    todo_wine ok(hr == D3DRMERR_BADVALUE, "Expected hr == D3DRMERR_BADVALUE, got %x.\n", hr);
-    if (SUCCEEDED(hr))
-        IDirect3DRMDevice_Release(device1);
+    ok(hr == D3DRMERR_BADVALUE, "Expected hr == D3DRMERR_BADVALUE, got %x.\n", hr);
 
     /* If NULL is passed for clipper, CreateDeviceFromClipper returns D3DRMERR_BADVALUE */
     hr = IDirect3DRM_CreateDeviceFromClipper(d3drm1, NULL, &driver, 300, 200, &device1);
-    todo_wine ok(hr == D3DRMERR_BADVALUE, "Expected hr == D3DRMERR_BADVALUE, got %x.\n", hr);
-    if (SUCCEEDED(hr))
-        IDirect3DRMDevice_Release(device1);
+    ok(hr == D3DRMERR_BADVALUE, "Expected hr == D3DRMERR_BADVALUE, got %x.\n", hr);
 
     hr = IDirect3DRM_CreateDeviceFromClipper(d3drm1, clipper, &driver, 300, 200, &device1);
     ok(hr == D3DRM_OK, "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);
     cref2 = get_refcount((IUnknown *)clipper);
-    todo_wine ok(cref2 > cref1, "expected cref2 > cref1, got cref1 = %u , cref2 = %u.\n", cref1, cref2);
+    ok(cref2 > cref1, "expected cref2 > cref1, got cref1 = %u , cref2 = %u.\n", cref1, cref2);
 
     /* Fetch immediate mode device in order to access render target */
     hr = IDirect3DRMDevice_GetDirect3DDevice(device1, &d3ddevice1);
-- 
2.3.2 (Apple Git-55)

