msctf: Implement ITfRange::GetContext.
[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 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,
172                 wined3dformat_from_dxgi_format(desc->Format), usage, WINED3DPOOL_DEFAULT, 0,
173                 WINED3DCUBEMAP_FACE_POSITIVE_X, &wined3d_surface);
174         if (FAILED(hr))
175         {
176             ERR("CreateSurface failed, returning %#x\n", hr);
177             goto fail;
178         }
179
180         hr = IWineD3DSurface_GetParent(wined3d_surface, &parent);
181         IWineD3DSurface_Release(wined3d_surface);
182         if (FAILED(hr))
183         {
184             ERR("GetParent failed, returning %#x\n", hr);
185             goto fail;
186         }
187
188         hr = IUnknown_QueryInterface(parent, &IID_IDXGISurface, (void **)&surface[i]);
189         IUnknown_Release(parent);
190         if (FAILED(hr))
191         {
192             ERR("Surface should implement IDXGISurface\n");
193             goto fail;
194         }
195
196         TRACE("Created IDXGISurface %p (%u/%u)\n", surface[i], i + 1, surface_count);
197     }
198     IWineD3DDeviceParent_Release(device_parent);
199
200     return S_OK;
201
202 fail:
203     for (j = 0; j < i; ++j)
204     {
205         IDXGISurface_Release(surface[i]);
206     }
207     IWineD3DDeviceParent_Release(device_parent);
208     return hr;
209 }
210
211 static HRESULT STDMETHODCALLTYPE dxgi_device_QueryResourceResidency(IWineDXGIDevice *iface,
212         IUnknown *const *resources, DXGI_RESIDENCY *residency, UINT resource_count)
213 {
214     FIXME("iface %p, resources %p, residency %p, resource_count %u stub!\n",
215             iface, resources, residency, resource_count);
216
217     return E_NOTIMPL;
218 }
219
220 static HRESULT STDMETHODCALLTYPE dxgi_device_SetGPUThreadPriority(IWineDXGIDevice *iface, INT priority)
221 {
222     FIXME("iface %p, priority %d stub!\n", iface, priority);
223
224     return E_NOTIMPL;
225 }
226
227 static HRESULT STDMETHODCALLTYPE dxgi_device_GetGPUThreadPriority(IWineDXGIDevice *iface, INT *priority)
228 {
229     FIXME("iface %p, priority %p stub!\n", iface, priority);
230
231     return E_NOTIMPL;
232 }
233
234 /* IWineDXGIDevice methods */
235
236 static IWineD3DDevice * STDMETHODCALLTYPE dxgi_device_get_wined3d_device(IWineDXGIDevice *iface)
237 {
238     struct dxgi_device *This = (struct dxgi_device *)iface;
239
240     TRACE("iface %p\n", iface);
241
242     EnterCriticalSection(&dxgi_cs);
243     IWineD3DDevice_AddRef(This->wined3d_device);
244     LeaveCriticalSection(&dxgi_cs);
245     return This->wined3d_device;
246 }
247
248 static HRESULT STDMETHODCALLTYPE dxgi_device_create_surface(IWineDXGIDevice *iface, const DXGI_SURFACE_DESC *desc,
249         DXGI_USAGE usage, const DXGI_SHARED_RESOURCE *shared_resource, IUnknown *outer, void **surface)
250 {
251     struct dxgi_surface *object;
252
253     FIXME("iface %p, desc %p, usage %#x, shared_resource %p, outer %p, surface %p partial stub!\n",
254             iface, desc, usage, shared_resource, outer, surface);
255
256     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
257     if (!object)
258     {
259         ERR("Failed to allocate DXGI surface object memory\n");
260         return E_OUTOFMEMORY;
261     }
262
263     object->vtbl = &dxgi_surface_vtbl;
264     object->inner_unknown_vtbl = &dxgi_surface_inner_unknown_vtbl;
265     object->refcount = 1;
266
267     if (outer)
268     {
269         object->outer_unknown = outer;
270         *surface = &object->inner_unknown_vtbl;
271     }
272     else
273     {
274         object->outer_unknown = (IUnknown *)&object->inner_unknown_vtbl;
275         *surface = object;
276     }
277
278     TRACE("Created IDXGISurface %p\n", object);
279
280     return S_OK;
281 }
282
283 static HRESULT STDMETHODCALLTYPE dxgi_device_create_swapchain(IWineDXGIDevice *iface,
284         WINED3DPRESENT_PARAMETERS *present_parameters, IWineD3DSwapChain **wined3d_swapchain)
285 {
286     struct dxgi_device *This = (struct dxgi_device *)iface;
287     struct dxgi_swapchain *object;
288     HRESULT hr;
289
290     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
291     if (!object)
292     {
293         ERR("Failed to allocate DXGI swapchain object memory\n");
294         return E_OUTOFMEMORY;
295     }
296
297     object->vtbl = &dxgi_swapchain_vtbl;
298     object->refcount = 1;
299
300     hr = IWineD3DDevice_CreateSwapChain(This->wined3d_device, present_parameters,
301             &object->wined3d_swapchain, (IUnknown *)object, SURFACE_OPENGL);
302     if (FAILED(hr))
303     {
304         WARN("Failed to create a swapchain, returning %#x\n", hr);
305         HeapFree(GetProcessHeap(), 0, object);
306         return hr;
307     }
308     *wined3d_swapchain = object->wined3d_swapchain;
309
310     TRACE("Created IDXGISwapChain %p\n", object);
311
312     return S_OK;
313 }
314
315 const struct IWineDXGIDeviceVtbl dxgi_device_vtbl =
316 {
317     /* IUnknown methods */
318     dxgi_device_QueryInterface,
319     dxgi_device_AddRef,
320     dxgi_device_Release,
321     /* IDXGIObject methods */
322     dxgi_device_SetPrivateData,
323     dxgi_device_SetPrivateDataInterface,
324     dxgi_device_GetPrivateData,
325     dxgi_device_GetParent,
326     /* IDXGIDevice methods */
327     dxgi_device_GetAdapter,
328     dxgi_device_CreateSurface,
329     dxgi_device_QueryResourceResidency,
330     dxgi_device_SetGPUThreadPriority,
331     dxgi_device_GetGPUThreadPriority,
332     /* IWineDXGIAdapter methods */
333     dxgi_device_get_wined3d_device,
334     dxgi_device_create_surface,
335     dxgi_device_create_swapchain,
336 };