rpcrt4: Don't try to bind to a null handle.
[wine] / dlls / d3d9 / stateblock.c
1 /*
2  * IDirect3DStateBlock9 implementation
3  *
4  * Copyright 2002-2003 Raphael Junqueira
5  * Copyright 2002-2003 Jason Edmeades
6  * Copyright 2005 Oliver Stieber
7  *
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.
12  *
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.
17  *
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
21  */
22
23 #include "config.h"
24 #include "d3d9_private.h"
25
26 WINE_DEFAULT_DEBUG_CHANNEL(d3d9);
27
28 static inline IDirect3DStateBlock9Impl *impl_from_IDirect3DStateBlock9(IDirect3DStateBlock9 *iface)
29 {
30     return CONTAINING_RECORD(iface, IDirect3DStateBlock9Impl, IDirect3DStateBlock9_iface);
31 }
32
33 static HRESULT WINAPI IDirect3DStateBlock9Impl_QueryInterface(IDirect3DStateBlock9 *iface,
34         REFIID riid, void **ppobj)
35 {
36     IDirect3DStateBlock9Impl *This = impl_from_IDirect3DStateBlock9(iface);
37
38     TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), ppobj);
39
40     if (IsEqualGUID(riid, &IID_IUnknown)
41         || IsEqualGUID(riid, &IID_IDirect3DStateBlock9)) {
42         IDirect3DStateBlock9_AddRef(iface);
43         *ppobj = This;
44         return S_OK;
45     }
46
47     WARN("(%p)->(%s,%p),not found\n", This, debugstr_guid(riid), ppobj);
48     *ppobj = NULL;
49     return E_NOINTERFACE;
50 }
51
52 static ULONG WINAPI IDirect3DStateBlock9Impl_AddRef(IDirect3DStateBlock9 *iface)
53 {
54     IDirect3DStateBlock9Impl *This = impl_from_IDirect3DStateBlock9(iface);
55     ULONG ref = InterlockedIncrement(&This->ref);
56
57     TRACE("%p increasing refcount to %u.\n", iface, ref);
58
59     return ref;
60 }
61
62 static ULONG WINAPI IDirect3DStateBlock9Impl_Release(IDirect3DStateBlock9 *iface)
63 {
64     IDirect3DStateBlock9Impl *This = impl_from_IDirect3DStateBlock9(iface);
65     ULONG ref = InterlockedDecrement(&This->ref);
66
67     TRACE("%p decreasing refcount to %u.\n", iface, ref);
68
69     if (ref == 0) {
70         wined3d_mutex_lock();
71         wined3d_stateblock_decref(This->wined3d_stateblock);
72         wined3d_mutex_unlock();
73
74         IDirect3DDevice9Ex_Release(This->parentDevice);
75         HeapFree(GetProcessHeap(), 0, This);
76     }
77     return ref;
78 }
79
80 /* IDirect3DStateBlock9 Interface follow: */
81 static HRESULT WINAPI IDirect3DStateBlock9Impl_GetDevice(IDirect3DStateBlock9 *iface,
82         IDirect3DDevice9 **device)
83 {
84     IDirect3DStateBlock9Impl *This = impl_from_IDirect3DStateBlock9(iface);
85
86     TRACE("iface %p, device %p.\n", iface, device);
87
88     *device = (IDirect3DDevice9 *)This->parentDevice;
89     IDirect3DDevice9_AddRef(*device);
90
91     TRACE("Returning device %p.\n", *device);
92
93     return D3D_OK;
94 }
95
96 static HRESULT WINAPI IDirect3DStateBlock9Impl_Capture(IDirect3DStateBlock9 *iface)
97 {
98     IDirect3DStateBlock9Impl *This = impl_from_IDirect3DStateBlock9(iface);
99     HRESULT hr;
100
101     TRACE("iface %p.\n", iface);
102
103     wined3d_mutex_lock();
104     hr = wined3d_stateblock_capture(This->wined3d_stateblock);
105     wined3d_mutex_unlock();
106
107     return hr;
108 }
109
110 static HRESULT WINAPI IDirect3DStateBlock9Impl_Apply(IDirect3DStateBlock9 *iface)
111 {
112     IDirect3DStateBlock9Impl *This = impl_from_IDirect3DStateBlock9(iface);
113     HRESULT hr;
114
115     TRACE("iface %p.\n", iface);
116
117     wined3d_mutex_lock();
118     hr = wined3d_stateblock_apply(This->wined3d_stateblock);
119     wined3d_mutex_unlock();
120
121     return hr;
122 }
123
124
125 static const IDirect3DStateBlock9Vtbl Direct3DStateBlock9_Vtbl =
126 {
127     /* IUnknown */
128     IDirect3DStateBlock9Impl_QueryInterface,
129     IDirect3DStateBlock9Impl_AddRef,
130     IDirect3DStateBlock9Impl_Release,
131     /* IDirect3DStateBlock9 */
132     IDirect3DStateBlock9Impl_GetDevice,
133     IDirect3DStateBlock9Impl_Capture,
134     IDirect3DStateBlock9Impl_Apply
135 };
136
137 HRESULT stateblock_init(IDirect3DStateBlock9Impl *stateblock, IDirect3DDevice9Impl *device,
138         D3DSTATEBLOCKTYPE type, struct wined3d_stateblock *wined3d_stateblock)
139 {
140     HRESULT hr;
141
142     stateblock->IDirect3DStateBlock9_iface.lpVtbl = &Direct3DStateBlock9_Vtbl;
143     stateblock->ref = 1;
144
145     if (wined3d_stateblock)
146     {
147         stateblock->wined3d_stateblock = wined3d_stateblock;
148     }
149     else
150     {
151         wined3d_mutex_lock();
152         hr = wined3d_stateblock_create(device->wined3d_device,
153                 (WINED3DSTATEBLOCKTYPE)type, &stateblock->wined3d_stateblock);
154         wined3d_mutex_unlock();
155         if (FAILED(hr))
156         {
157             WARN("Failed to create wined3d stateblock, hr %#x.\n", hr);
158             return hr;
159         }
160     }
161
162     stateblock->parentDevice = &device->IDirect3DDevice9Ex_iface;
163     IDirect3DDevice9Ex_AddRef(stateblock->parentDevice);
164
165     return D3D_OK;
166 }