winedbg: Don't dereference possibly NULL thread pointer.
[wine/zf.git] / dlls / d3d8 / swapchain.c
blob78db4fcaf248dcfcef8cb7091ff1a10e0bc34ef4
1 /*
2 * IDirect3DSwapChain8 implementation
4 * Copyright 2005 Oliver Stieber
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "d3d8_private.h"
23 WINE_DEFAULT_DEBUG_CHANNEL(d3d8);
25 static inline struct d3d8_swapchain *impl_from_IDirect3DSwapChain8(IDirect3DSwapChain8 *iface)
27 return CONTAINING_RECORD(iface, struct d3d8_swapchain, IDirect3DSwapChain8_iface);
30 static HRESULT WINAPI d3d8_swapchain_QueryInterface(IDirect3DSwapChain8 *iface, REFIID riid, void **out)
32 TRACE("iface %p, riid %s, out %p.\n", iface, debugstr_guid(riid), out);
34 if (IsEqualGUID(riid, &IID_IDirect3DSwapChain8)
35 || IsEqualGUID(riid, &IID_IUnknown))
37 IDirect3DSwapChain8_AddRef(iface);
38 *out = iface;
39 return S_OK;
42 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
44 *out = NULL;
45 return E_NOINTERFACE;
48 static ULONG WINAPI d3d8_swapchain_AddRef(IDirect3DSwapChain8 *iface)
50 struct d3d8_swapchain *swapchain = impl_from_IDirect3DSwapChain8(iface);
51 ULONG ref = InterlockedIncrement(&swapchain->refcount);
53 TRACE("%p increasing refcount to %u.\n", iface, ref);
55 if (ref == 1)
57 if (swapchain->parent_device)
58 IDirect3DDevice8_AddRef(swapchain->parent_device);
59 wined3d_swapchain_incref(swapchain->wined3d_swapchain);
62 return ref;
65 static ULONG WINAPI d3d8_swapchain_Release(IDirect3DSwapChain8 *iface)
67 struct d3d8_swapchain *swapchain = impl_from_IDirect3DSwapChain8(iface);
68 ULONG ref = InterlockedDecrement(&swapchain->refcount);
70 TRACE("%p decreasing refcount to %u.\n", iface, ref);
72 if (!ref)
74 IDirect3DDevice8 *parent_device = swapchain->parent_device;
76 wined3d_swapchain_decref(swapchain->wined3d_swapchain);
78 if (parent_device)
79 IDirect3DDevice8_Release(parent_device);
81 return ref;
84 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d8_swapchain_Present(IDirect3DSwapChain8 *iface,
85 const RECT *src_rect, const RECT *dst_rect, HWND dst_window_override,
86 const RGNDATA *dirty_region)
88 struct d3d8_swapchain *swapchain = impl_from_IDirect3DSwapChain8(iface);
89 struct d3d8_device *device = impl_from_IDirect3DDevice8(swapchain->parent_device);
91 TRACE("iface %p, src_rect %s, dst_rect %s, dst_window_override %p, dirty_region %p.\n",
92 iface, wine_dbgstr_rect(src_rect), wine_dbgstr_rect(dst_rect), dst_window_override, dirty_region);
94 if (device->device_state != D3D8_DEVICE_STATE_OK)
95 return D3DERR_DEVICELOST;
97 if (dirty_region)
98 FIXME("Ignoring dirty_region %p.\n", dirty_region);
100 return wined3d_swapchain_present(swapchain->wined3d_swapchain,
101 src_rect, dst_rect, dst_window_override, swapchain->swap_interval, 0);
104 static HRESULT WINAPI d3d8_swapchain_GetBackBuffer(IDirect3DSwapChain8 *iface,
105 UINT backbuffer_idx, D3DBACKBUFFER_TYPE backbuffer_type, IDirect3DSurface8 **backbuffer)
107 struct d3d8_swapchain *swapchain = impl_from_IDirect3DSwapChain8(iface);
108 struct wined3d_texture *wined3d_texture;
109 struct d3d8_surface *surface_impl;
110 HRESULT hr = D3D_OK;
112 TRACE("iface %p, backbuffer_idx %u, backbuffer_type %#x, backbuffer %p.\n",
113 iface, backbuffer_idx, backbuffer_type, backbuffer);
115 /* backbuffer_type is ignored by native. */
117 if (!backbuffer)
119 WARN("The output pointer is NULL, returning D3DERR_INVALIDCALL.\n");
120 return D3DERR_INVALIDCALL;
123 wined3d_mutex_lock();
124 if ((wined3d_texture = wined3d_swapchain_get_back_buffer(swapchain->wined3d_swapchain, backbuffer_idx)))
126 surface_impl = wined3d_texture_get_sub_resource_parent(wined3d_texture, 0);
127 *backbuffer = &surface_impl->IDirect3DSurface8_iface;
128 IDirect3DSurface8_AddRef(*backbuffer);
130 else
132 /* Do not set *backbuffer = NULL, see tests/device.c, test_swapchain(). */
133 hr = D3DERR_INVALIDCALL;
135 wined3d_mutex_unlock();
137 return hr;
140 static const IDirect3DSwapChain8Vtbl d3d8_swapchain_vtbl =
142 d3d8_swapchain_QueryInterface,
143 d3d8_swapchain_AddRef,
144 d3d8_swapchain_Release,
145 d3d8_swapchain_Present,
146 d3d8_swapchain_GetBackBuffer
149 static void STDMETHODCALLTYPE d3d8_swapchain_wined3d_object_released(void *parent)
151 heap_free(parent);
154 static const struct wined3d_parent_ops d3d8_swapchain_wined3d_parent_ops =
156 d3d8_swapchain_wined3d_object_released,
159 static void CDECL d3d8_swapchain_windowed_state_changed(struct wined3d_swapchain_state_parent *parent,
160 BOOL windowed)
162 TRACE("parent %p, windowed %d.\n", parent, windowed);
165 static const struct wined3d_swapchain_state_parent_ops d3d8_swapchain_state_parent_ops =
167 d3d8_swapchain_windowed_state_changed,
170 static HRESULT swapchain_init(struct d3d8_swapchain *swapchain, struct d3d8_device *device,
171 struct wined3d_swapchain_desc *desc, unsigned int swap_interval)
173 HRESULT hr;
175 swapchain->refcount = 1;
176 swapchain->IDirect3DSwapChain8_iface.lpVtbl = &d3d8_swapchain_vtbl;
177 swapchain->state_parent.ops = &d3d8_swapchain_state_parent_ops;
178 swapchain->swap_interval = swap_interval;
180 if (FAILED(hr = wined3d_swapchain_create(device->wined3d_device, desc, &swapchain->state_parent,
181 swapchain, &d3d8_swapchain_wined3d_parent_ops, &swapchain->wined3d_swapchain)))
183 WARN("Failed to create wined3d swapchain, hr %#x.\n", hr);
184 return hr;
187 swapchain->parent_device = &device->IDirect3DDevice8_iface;
188 IDirect3DDevice8_AddRef(swapchain->parent_device);
190 return D3D_OK;
193 HRESULT d3d8_swapchain_create(struct d3d8_device *device, struct wined3d_swapchain_desc *desc,
194 unsigned int swap_interval, struct d3d8_swapchain **swapchain)
196 struct d3d8_swapchain *object;
197 HRESULT hr;
199 if (!(object = heap_alloc_zero(sizeof(*object))))
200 return E_OUTOFMEMORY;
202 if (FAILED(hr = swapchain_init(object, device, desc, swap_interval)))
204 WARN("Failed to initialize swapchain, hr %#x.\n", hr);
205 heap_free(object);
206 return hr;
209 TRACE("Created swapchain %p.\n", object);
210 *swapchain = object;
212 return D3D_OK;