winejoystick.drv: Use CP_UNIXCP instead of CP_ACP when converting a string that comes...
[wine] / dlls / dxgi / device.c
1 /*
2  * Copyright 2008 Henri Verbeet for CodeWeavers
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
17  *
18  */
19
20 #include "config.h"
21 #include "wine/port.h"
22
23 #include "dxgi_private.h"
24
25 WINE_DEFAULT_DEBUG_CHANNEL(dxgi);
26
27 /* IUnknown methods */
28
29 static HRESULT STDMETHODCALLTYPE dxgi_device_QueryInterface(IWineDXGIDevice *iface, REFIID riid, void **object)
30 {
31     struct dxgi_device *This = (struct dxgi_device *)iface;
32
33     TRACE("iface %p, riid %s, object %p\n", iface, debugstr_guid(riid), object);
34
35     if (IsEqualGUID(riid, &IID_IUnknown)
36             || IsEqualGUID(riid, &IID_IDXGIObject)
37             || IsEqualGUID(riid, &IID_IDXGIDevice)
38             || IsEqualGUID(riid, &IID_IWineDXGIDevice))
39     {
40         IUnknown_AddRef(iface);
41         *object = iface;
42         return S_OK;
43     }
44
45     if (This->child_layer)
46     {
47         TRACE("forwarding to child layer %p\n", This->child_layer);
48         return IUnknown_QueryInterface(This->child_layer, riid, object);
49     }
50
51     WARN("%s not implemented, returning E_NOINTERFACE\n", debugstr_guid(riid));
52
53     *object = NULL;
54     return E_NOINTERFACE;
55 }
56
57 static ULONG STDMETHODCALLTYPE dxgi_device_AddRef(IWineDXGIDevice *iface)
58 {
59     struct dxgi_device *This = (struct dxgi_device *)iface;
60     ULONG refcount = InterlockedIncrement(&This->refcount);
61
62     TRACE("%p increasing refcount to %u\n", This, refcount);
63
64     return refcount;
65 }
66
67 static ULONG STDMETHODCALLTYPE dxgi_device_Release(IWineDXGIDevice *iface)
68 {
69     struct dxgi_device *This = (struct dxgi_device *)iface;
70     ULONG refcount = InterlockedDecrement(&This->refcount);
71
72     TRACE("%p decreasing refcount to %u\n", This, refcount);
73
74     if (!refcount)
75     {
76         if (This->child_layer) IUnknown_Release(This->child_layer);
77         EnterCriticalSection(&dxgi_cs);
78         IWineD3DDevice_Release(This->wined3d_device);
79         LeaveCriticalSection(&dxgi_cs);
80         IWineDXGIFactory_Release(This->factory);
81         HeapFree(GetProcessHeap(), 0, This);
82     }
83
84     return refcount;
85 }
86
87 /* IDXGIObject methods */
88
89 static HRESULT STDMETHODCALLTYPE dxgi_device_SetPrivateData(IWineDXGIDevice *iface,
90         REFGUID guid, UINT data_size, const void *data)
91 {
92     FIXME("iface %p, guid %s, data_size %u, data %p stub!\n", iface, debugstr_guid(guid), data_size, data);
93
94     return E_NOTIMPL;
95 }
96
97 static HRESULT STDMETHODCALLTYPE dxgi_device_SetPrivateDataInterface(IWineDXGIDevice *iface,
98         REFGUID guid, const IUnknown *object)
99 {
100     FIXME("iface %p, guid %s, object %p stub!\n", iface, debugstr_guid(guid), object);
101
102     return E_NOTIMPL;
103 }
104
105 static HRESULT STDMETHODCALLTYPE dxgi_device_GetPrivateData(IWineDXGIDevice *iface,
106         REFGUID guid, UINT *data_size, void *data)
107 {
108     FIXME("iface %p, guid %s, data_size %p, data %p stub!\n", iface, debugstr_guid(guid), data_size, data);
109
110     return E_NOTIMPL;
111 }
112
113 static HRESULT STDMETHODCALLTYPE dxgi_device_GetParent(IWineDXGIDevice *iface, REFIID riid, void **parent)
114 {
115     FIXME("iface %p, riid %s, parent %p stub!\n", iface, debugstr_guid(riid), parent);
116
117     return E_NOTIMPL;
118 }
119
120 /* IDXGIDevice methods */
121
122 static HRESULT STDMETHODCALLTYPE dxgi_device_GetAdapter(IWineDXGIDevice *iface, IDXGIAdapter **adapter)
123 {
124     struct dxgi_device *This = (struct dxgi_device *)iface;
125     WINED3DDEVICE_CREATION_PARAMETERS create_parameters;
126     HRESULT hr;
127
128     TRACE("iface %p, adapter %p\n", iface, adapter);
129
130     EnterCriticalSection(&dxgi_cs);
131
132     hr = IWineD3DDevice_GetCreationParameters(This->wined3d_device, &create_parameters);
133     if (FAILED(hr))
134     {
135         LeaveCriticalSection(&dxgi_cs);
136         return hr;
137     }
138
139     LeaveCriticalSection(&dxgi_cs);
140
141     return IWineDXGIFactory_EnumAdapters(This->factory, create_parameters.AdapterOrdinal, adapter);
142 }
143
144 static HRESULT STDMETHODCALLTYPE dxgi_device_CreateSurface(IWineDXGIDevice *iface,
145         const DXGI_SURFACE_DESC *desc, UINT surface_count, DXGI_USAGE usage,
146         const DXGI_SHARED_RESOURCE *shared_resource, IDXGISurface **surface)
147 {
148     IWineD3DDeviceParent *device_parent;
149     HRESULT hr;
150     UINT i;
151     UINT j;
152
153     TRACE("iface %p, desc %p, surface_count %u, usage %#x, shared_resource %p, surface %p\n",
154             iface, desc, surface_count, usage, shared_resource, surface);
155
156     hr = IWineDXGIDevice_QueryInterface(iface, &IID_IWineD3DDeviceParent, (void **)&device_parent);
157     if (FAILED(hr))
158     {
159         ERR("Device should implement IWineD3DDeviceParent\n");
160         return E_FAIL;
161     }
162
163     FIXME("Implement DXGI<->wined3d format and usage conversion\n");
164
165     memset(surface, 0, surface_count * sizeof(*surface));
166     for (i = 0; i < surface_count; ++i)
167     {
168         IWineD3DSurface *wined3d_surface;
169         IUnknown *parent;
170
171         hr = IWineD3DDeviceParent_CreateSurface(device_parent, NULL, desc->Width, desc->Height, desc->Format,
172                 usage, WINED3DPOOL_DEFAULT, 0, WINED3DCUBEMAP_FACE_POSITIVE_X, &wined3d_surface);
173         if (FAILED(hr))
174         {
175             ERR("CreateSurface failed, returning %#x\n", hr);
176             goto fail;
177         }
178
179         hr = IWineD3DSurface_GetParent(wined3d_surface, &parent);
180         IWineD3DSurface_Release(wined3d_surface);
181         if (FAILED(hr))
182         {
183             ERR("GetParent failed, returning %#x\n", hr);
184             goto fail;
185         }
186
187         hr = IUnknown_QueryInterface(parent, &IID_IDXGISurface, (void **)&surface[i]);
188         IUnknown_Release(parent);
189         if (FAILED(hr))
190         {
191             ERR("Surface should implement IDXGISurface\n");
192             goto fail;
193         }
194
195         TRACE("Created IDXGISurface %p (%u/%u)\n", surface[i], i + 1, surface_count);
196     }
197     IWineD3DDeviceParent_Release(device_parent);
198
199     return S_OK;
200
201 fail:
202     for (j = 0; j < i; ++j)
203     {
204         IDXGISurface_Release(surface[i]);
205     }
206     IWineD3DDeviceParent_Release(device_parent);
207     return hr;
208 }
209
210 static HRESULT STDMETHODCALLTYPE dxgi_device_QueryResourceResidency(IWineDXGIDevice *iface,
211         IUnknown *const *resources, DXGI_RESIDENCY *residency, UINT resource_count)
212 {
213     FIXME("iface %p, resources %p, residency %p, resource_count %u stub!\n",
214             iface, resources, residency, resource_count);
215
216     return E_NOTIMPL;
217 }
218
219 static HRESULT STDMETHODCALLTYPE dxgi_device_SetGPUThreadPriority(IWineDXGIDevice *iface, INT priority)
220 {
221     FIXME("iface %p, priority %d stub!\n", iface, priority);
222
223     return E_NOTIMPL;
224 }
225
226 static HRESULT STDMETHODCALLTYPE dxgi_device_GetGPUThreadPriority(IWineDXGIDevice *iface, INT *priority)
227 {
228     FIXME("iface %p, priority %p stub!\n", iface, priority);
229
230     return E_NOTIMPL;
231 }
232
233 /* IWineDXGIDevice methods */
234
235 static IWineD3DDevice * STDMETHODCALLTYPE dxgi_device_get_wined3d_device(IWineDXGIDevice *iface)
236 {
237     struct dxgi_device *This = (struct dxgi_device *)iface;
238
239     TRACE("iface %p\n", iface);
240
241     EnterCriticalSection(&dxgi_cs);
242     IWineD3DDevice_AddRef(This->wined3d_device);
243     LeaveCriticalSection(&dxgi_cs);
244     return This->wined3d_device;
245 }
246
247 static HRESULT STDMETHODCALLTYPE dxgi_device_create_surface(IWineDXGIDevice *iface, const DXGI_SURFACE_DESC *desc,
248         DXGI_USAGE usage, const DXGI_SHARED_RESOURCE *shared_resource, IUnknown *outer, void **surface)
249 {
250     struct dxgi_surface *object;
251
252     FIXME("iface %p, desc %p, usage %#x, shared_resource %p, outer %p, surface %p partial stub!\n",
253             iface, desc, usage, shared_resource, outer, surface);
254
255     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
256     if (!object)
257     {
258         ERR("Failed to allocate DXGI surface object memory\n");
259         return E_OUTOFMEMORY;
260     }
261
262     object->vtbl = &dxgi_surface_vtbl;
263     object->inner_unknown_vtbl = &dxgi_surface_inner_unknown_vtbl;
264     object->refcount = 1;
265
266     if (outer)
267     {
268         object->outer_unknown = outer;
269         *surface = &object->inner_unknown_vtbl;
270     }
271     else
272     {
273         object->outer_unknown = (IUnknown *)&object->inner_unknown_vtbl;
274         *surface = object;
275     }
276
277     TRACE("Created IDXGISurface %p\n", object);
278
279     return S_OK;
280 }
281
282 const struct IWineDXGIDeviceVtbl dxgi_device_vtbl =
283 {
284     /* IUnknown methods */
285     dxgi_device_QueryInterface,
286     dxgi_device_AddRef,
287     dxgi_device_Release,
288     /* IDXGIObject methods */
289     dxgi_device_SetPrivateData,
290     dxgi_device_SetPrivateDataInterface,
291     dxgi_device_GetPrivateData,
292     dxgi_device_GetParent,
293     /* IDXGIDevice methods */
294     dxgi_device_GetAdapter,
295     dxgi_device_CreateSurface,
296     dxgi_device_QueryResourceResidency,
297     dxgi_device_SetGPUThreadPriority,
298     dxgi_device_GetGPUThreadPriority,
299     /* IWineDXGIAdapter methods */
300     dxgi_device_get_wined3d_device,
301     dxgi_device_create_surface,
302 };