d3d9: Don't return a pointer to the implementation in IDirect3DSwapChain9Impl_QueryIn...
[wine/multimedia.git] / dlls / d3d9 / swapchain.c
blob7a138488a1f9b0240040794d2147fedd86b716b8
1 /*
2 * IDirect3DSwapChain9 implementation
4 * Copyright 2002-2003 Jason Edmeades
5 * Raphael Junqueira
6 * Copyright 2005 Oliver Stieber
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #include "config.h"
24 #include "d3d9_private.h"
26 WINE_DEFAULT_DEBUG_CHANNEL(d3d9);
28 /* IDirect3DSwapChain IUnknown parts follow: */
29 static HRESULT WINAPI IDirect3DSwapChain9Impl_QueryInterface(LPDIRECT3DSWAPCHAIN9 iface, REFIID riid, LPVOID* ppobj)
31 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), ppobj);
33 if (IsEqualGUID(riid, &IID_IDirect3DSwapChain9)
34 || IsEqualGUID(riid, &IID_IUnknown))
36 IDirect3DSwapChain9_AddRef(iface);
37 *ppobj = iface;
38 return S_OK;
41 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
43 *ppobj = NULL;
44 return E_NOINTERFACE;
47 static ULONG WINAPI IDirect3DSwapChain9Impl_AddRef(LPDIRECT3DSWAPCHAIN9 iface) {
48 IDirect3DSwapChain9Impl *This = (IDirect3DSwapChain9Impl *)iface;
49 ULONG ref = InterlockedIncrement(&This->ref);
51 TRACE("%p increasing refcount to %u.\n", iface, ref);
53 if (ref == 1)
55 if (This->parentDevice)
56 IDirect3DDevice9Ex_AddRef(This->parentDevice);
58 wined3d_mutex_lock();
59 wined3d_swapchain_incref(This->wined3d_swapchain);
60 wined3d_mutex_unlock();
63 return ref;
66 static ULONG WINAPI IDirect3DSwapChain9Impl_Release(LPDIRECT3DSWAPCHAIN9 iface) {
67 IDirect3DSwapChain9Impl *This = (IDirect3DSwapChain9Impl *)iface;
68 ULONG ref = InterlockedDecrement(&This->ref);
70 TRACE("%p decreasing refcount to %u.\n", iface, ref);
72 if (ref == 0) {
73 IDirect3DDevice9Ex *parentDevice = This->parentDevice;
75 wined3d_mutex_lock();
76 wined3d_swapchain_decref(This->wined3d_swapchain);
77 wined3d_mutex_unlock();
79 /* Release the device last, as it may cause the device to be destroyed. */
80 if (parentDevice) IDirect3DDevice9Ex_Release(parentDevice);
82 return ref;
85 /* IDirect3DSwapChain9 parts follow: */
86 static HRESULT WINAPI DECLSPEC_HOTPATCH IDirect3DSwapChain9Impl_Present(LPDIRECT3DSWAPCHAIN9 iface, CONST RECT* pSourceRect, CONST RECT* pDestRect, HWND hDestWindowOverride, CONST RGNDATA* pDirtyRegion, DWORD dwFlags) {
87 IDirect3DSwapChain9Impl *This = (IDirect3DSwapChain9Impl *)iface;
88 HRESULT hr;
90 TRACE("iface %p, src_rect %p, dst_rect %p, dst_window_override %p, dirty_region %p, flags %#x.\n",
91 iface, pSourceRect, pDestRect, hDestWindowOverride, pDirtyRegion, dwFlags);
93 wined3d_mutex_lock();
94 hr = wined3d_swapchain_present(This->wined3d_swapchain, pSourceRect,
95 pDestRect, hDestWindowOverride, pDirtyRegion, dwFlags);
96 wined3d_mutex_unlock();
98 return hr;
101 static HRESULT WINAPI IDirect3DSwapChain9Impl_GetFrontBufferData(IDirect3DSwapChain9 *iface,
102 IDirect3DSurface9 *pDestSurface)
104 IDirect3DSwapChain9Impl *This = (IDirect3DSwapChain9Impl *)iface;
105 IDirect3DSurface9Impl *dst = unsafe_impl_from_IDirect3DSurface9(pDestSurface);
106 HRESULT hr;
108 TRACE("iface %p, surface %p.\n", iface, pDestSurface);
110 wined3d_mutex_lock();
111 hr = wined3d_swapchain_get_front_buffer_data(This->wined3d_swapchain, dst->wined3d_surface);
112 wined3d_mutex_unlock();
114 return hr;
117 static HRESULT WINAPI IDirect3DSwapChain9Impl_GetBackBuffer(IDirect3DSwapChain9 *iface,
118 UINT iBackBuffer, D3DBACKBUFFER_TYPE Type, IDirect3DSurface9 **ppBackBuffer)
120 IDirect3DSwapChain9Impl *This = (IDirect3DSwapChain9Impl *)iface;
121 struct wined3d_surface *wined3d_surface = NULL;
122 HRESULT hr;
124 TRACE("iface %p, backbuffer_idx %u, backbuffer_type %#x, backbuffer %p.\n",
125 iface, iBackBuffer, Type, ppBackBuffer);
127 wined3d_mutex_lock();
128 hr = wined3d_swapchain_get_back_buffer(This->wined3d_swapchain,
129 iBackBuffer, (enum wined3d_backbuffer_type)Type, &wined3d_surface);
130 if (SUCCEEDED(hr) && wined3d_surface)
132 *ppBackBuffer = wined3d_surface_get_parent(wined3d_surface);
133 IDirect3DSurface9_AddRef(*ppBackBuffer);
134 wined3d_surface_decref(wined3d_surface);
136 wined3d_mutex_unlock();
138 /* Do not touch the **ppBackBuffer pointer otherwise! (see device test) */
139 return hr;
142 static HRESULT WINAPI IDirect3DSwapChain9Impl_GetRasterStatus(LPDIRECT3DSWAPCHAIN9 iface, D3DRASTER_STATUS* pRasterStatus) {
143 IDirect3DSwapChain9Impl *This = (IDirect3DSwapChain9Impl *)iface;
144 HRESULT hr;
146 TRACE("iface %p, raster_status %p.\n", iface, pRasterStatus);
148 wined3d_mutex_lock();
149 hr = wined3d_swapchain_get_raster_status(This->wined3d_swapchain, (struct wined3d_raster_status *)pRasterStatus);
150 wined3d_mutex_unlock();
152 return hr;
155 static HRESULT WINAPI IDirect3DSwapChain9Impl_GetDisplayMode(LPDIRECT3DSWAPCHAIN9 iface, D3DDISPLAYMODE* pMode) {
156 IDirect3DSwapChain9Impl *This = (IDirect3DSwapChain9Impl *)iface;
157 HRESULT hr;
159 TRACE("iface %p, mode %p.\n", iface, pMode);
161 wined3d_mutex_lock();
162 hr = wined3d_swapchain_get_display_mode(This->wined3d_swapchain, (struct wined3d_display_mode *)pMode);
163 wined3d_mutex_unlock();
165 if (SUCCEEDED(hr)) pMode->Format = d3dformat_from_wined3dformat(pMode->Format);
167 return hr;
170 static HRESULT WINAPI IDirect3DSwapChain9Impl_GetDevice(IDirect3DSwapChain9 *iface, IDirect3DDevice9 **device)
172 IDirect3DSwapChain9Impl *This = (IDirect3DSwapChain9Impl *)iface;
174 TRACE("iface %p, device %p.\n", iface, device);
176 *device = (IDirect3DDevice9 *)This->parentDevice;
177 IDirect3DDevice9_AddRef(*device);
179 TRACE("Returning device %p.\n", *device);
181 return D3D_OK;
184 static HRESULT WINAPI IDirect3DSwapChain9Impl_GetPresentParameters(IDirect3DSwapChain9 *iface,
185 D3DPRESENT_PARAMETERS *pPresentationParameters)
187 IDirect3DSwapChain9Impl *This = (IDirect3DSwapChain9Impl *)iface;
188 struct wined3d_swapchain_desc desc;
189 HRESULT hr;
191 TRACE("iface %p, parameters %p.\n", iface, pPresentationParameters);
193 wined3d_mutex_lock();
194 hr = wined3d_swapchain_get_desc(This->wined3d_swapchain, &desc);
195 wined3d_mutex_unlock();
197 pPresentationParameters->BackBufferWidth = desc.backbuffer_width;
198 pPresentationParameters->BackBufferHeight = desc.backbuffer_height;
199 pPresentationParameters->BackBufferFormat = d3dformat_from_wined3dformat(desc.backbuffer_format);
200 pPresentationParameters->BackBufferCount = desc.backbuffer_count;
201 pPresentationParameters->MultiSampleType = desc.multisample_type;
202 pPresentationParameters->MultiSampleQuality = desc.multisample_quality;
203 pPresentationParameters->SwapEffect = desc.swap_effect;
204 pPresentationParameters->hDeviceWindow = desc.device_window;
205 pPresentationParameters->Windowed = desc.windowed;
206 pPresentationParameters->EnableAutoDepthStencil = desc.enable_auto_depth_stencil;
207 pPresentationParameters->AutoDepthStencilFormat = d3dformat_from_wined3dformat(desc.auto_depth_stencil_format);
208 pPresentationParameters->Flags = desc.flags;
209 pPresentationParameters->FullScreen_RefreshRateInHz = desc.refresh_rate;
210 pPresentationParameters->PresentationInterval = desc.swap_interval;
212 return hr;
216 static const IDirect3DSwapChain9Vtbl Direct3DSwapChain9_Vtbl =
218 IDirect3DSwapChain9Impl_QueryInterface,
219 IDirect3DSwapChain9Impl_AddRef,
220 IDirect3DSwapChain9Impl_Release,
221 IDirect3DSwapChain9Impl_Present,
222 IDirect3DSwapChain9Impl_GetFrontBufferData,
223 IDirect3DSwapChain9Impl_GetBackBuffer,
224 IDirect3DSwapChain9Impl_GetRasterStatus,
225 IDirect3DSwapChain9Impl_GetDisplayMode,
226 IDirect3DSwapChain9Impl_GetDevice,
227 IDirect3DSwapChain9Impl_GetPresentParameters
230 static void STDMETHODCALLTYPE d3d9_swapchain_wined3d_object_released(void *parent)
232 HeapFree(GetProcessHeap(), 0, parent);
235 static const struct wined3d_parent_ops d3d9_swapchain_wined3d_parent_ops =
237 d3d9_swapchain_wined3d_object_released,
240 HRESULT swapchain_init(IDirect3DSwapChain9Impl *swapchain, IDirect3DDevice9Impl *device,
241 D3DPRESENT_PARAMETERS *present_parameters)
243 struct wined3d_swapchain_desc desc;
244 HRESULT hr;
246 swapchain->ref = 1;
247 swapchain->lpVtbl = &Direct3DSwapChain9_Vtbl;
249 desc.backbuffer_width = present_parameters->BackBufferWidth;
250 desc.backbuffer_height = present_parameters->BackBufferHeight;
251 desc.backbuffer_format = wined3dformat_from_d3dformat(present_parameters->BackBufferFormat);
252 desc.backbuffer_count = max(1, present_parameters->BackBufferCount);
253 desc.multisample_type = present_parameters->MultiSampleType;
254 desc.multisample_quality = present_parameters->MultiSampleQuality;
255 desc.swap_effect = present_parameters->SwapEffect;
256 desc.device_window = present_parameters->hDeviceWindow;
257 desc.windowed = present_parameters->Windowed;
258 desc.enable_auto_depth_stencil = present_parameters->EnableAutoDepthStencil;
259 desc.auto_depth_stencil_format = wined3dformat_from_d3dformat(present_parameters->AutoDepthStencilFormat);
260 desc.flags = present_parameters->Flags;
261 desc.refresh_rate = present_parameters->FullScreen_RefreshRateInHz;
262 desc.swap_interval = present_parameters->PresentationInterval;
263 desc.auto_restore_display_mode = TRUE;
265 wined3d_mutex_lock();
266 hr = wined3d_swapchain_create(device->wined3d_device, &desc,
267 WINED3D_SURFACE_TYPE_OPENGL, swapchain, &d3d9_swapchain_wined3d_parent_ops,
268 &swapchain->wined3d_swapchain);
269 wined3d_mutex_unlock();
271 present_parameters->BackBufferWidth = desc.backbuffer_width;
272 present_parameters->BackBufferHeight = desc.backbuffer_height;
273 present_parameters->BackBufferFormat = d3dformat_from_wined3dformat(desc.backbuffer_format);
274 present_parameters->BackBufferCount = desc.backbuffer_count;
275 present_parameters->MultiSampleType = desc.multisample_type;
276 present_parameters->MultiSampleQuality = desc.multisample_quality;
277 present_parameters->SwapEffect = desc.swap_effect;
278 present_parameters->hDeviceWindow = desc.device_window;
279 present_parameters->Windowed = desc.windowed;
280 present_parameters->EnableAutoDepthStencil = desc.enable_auto_depth_stencil;
281 present_parameters->AutoDepthStencilFormat = d3dformat_from_wined3dformat(desc.auto_depth_stencil_format);
282 present_parameters->Flags = desc.flags;
283 present_parameters->FullScreen_RefreshRateInHz = desc.refresh_rate;
284 present_parameters->PresentationInterval = desc.swap_interval;
286 if (FAILED(hr))
288 WARN("Failed to create wined3d swapchain, hr %#x.\n", hr);
289 return hr;
292 swapchain->parentDevice = &device->IDirect3DDevice9Ex_iface;
293 IDirect3DDevice9Ex_AddRef(swapchain->parentDevice);
295 return D3D_OK;