xref: /reactos/dll/directx/wine/d3d9/stateblock.c (revision c2c66aff)
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 "d3d9_private.h"
24 
25 static inline struct d3d9_stateblock *impl_from_IDirect3DStateBlock9(IDirect3DStateBlock9 *iface)
26 {
27     return CONTAINING_RECORD(iface, struct d3d9_stateblock, IDirect3DStateBlock9_iface);
28 }
29 
30 static HRESULT WINAPI d3d9_stateblock_QueryInterface(IDirect3DStateBlock9 *iface, REFIID riid, void **out)
31 {
32     TRACE("iface %p, riid %s, out %p.\n", iface, debugstr_guid(riid), out);
33 
34     if (IsEqualGUID(riid, &IID_IDirect3DStateBlock9)
35             || IsEqualGUID(riid, &IID_IUnknown))
36     {
37         IDirect3DStateBlock9_AddRef(iface);
38         *out = iface;
39         return S_OK;
40     }
41 
42     WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
43 
44     *out = NULL;
45     return E_NOINTERFACE;
46 }
47 
48 static ULONG WINAPI d3d9_stateblock_AddRef(IDirect3DStateBlock9 *iface)
49 {
50     struct d3d9_stateblock *stateblock = impl_from_IDirect3DStateBlock9(iface);
51     ULONG refcount = InterlockedIncrement(&stateblock->refcount);
52 
53     TRACE("%p increasing refcount to %u.\n", iface, refcount);
54 
55     return refcount;
56 }
57 
58 static ULONG WINAPI d3d9_stateblock_Release(IDirect3DStateBlock9 *iface)
59 {
60     struct d3d9_stateblock *stateblock = impl_from_IDirect3DStateBlock9(iface);
61     ULONG refcount = InterlockedDecrement(&stateblock->refcount);
62 
63     TRACE("%p decreasing refcount to %u.\n", iface, refcount);
64 
65     if (!refcount)
66     {
67         wined3d_mutex_lock();
68         wined3d_stateblock_decref(stateblock->wined3d_stateblock);
69         wined3d_mutex_unlock();
70 
71         IDirect3DDevice9Ex_Release(stateblock->parent_device);
72         HeapFree(GetProcessHeap(), 0, stateblock);
73     }
74 
75     return refcount;
76 }
77 
78 static HRESULT WINAPI d3d9_stateblock_GetDevice(IDirect3DStateBlock9 *iface, IDirect3DDevice9 **device)
79 {
80     struct d3d9_stateblock *stateblock = impl_from_IDirect3DStateBlock9(iface);
81 
82     TRACE("iface %p, device %p.\n", iface, device);
83 
84     *device = (IDirect3DDevice9 *)stateblock->parent_device;
85     IDirect3DDevice9_AddRef(*device);
86 
87     TRACE("Returning device %p.\n", *device);
88 
89     return D3D_OK;
90 }
91 
92 static HRESULT WINAPI d3d9_stateblock_Capture(IDirect3DStateBlock9 *iface)
93 {
94     struct d3d9_stateblock *stateblock = impl_from_IDirect3DStateBlock9(iface);
95 
96     TRACE("iface %p.\n", iface);
97 
98     wined3d_mutex_lock();
99     wined3d_stateblock_capture(stateblock->wined3d_stateblock);
100     wined3d_mutex_unlock();
101 
102     return D3D_OK;
103 }
104 
105 static HRESULT WINAPI d3d9_stateblock_Apply(IDirect3DStateBlock9 *iface)
106 {
107     struct d3d9_stateblock *stateblock = impl_from_IDirect3DStateBlock9(iface);
108 
109     TRACE("iface %p.\n", iface);
110 
111     wined3d_mutex_lock();
112     wined3d_stateblock_apply(stateblock->wined3d_stateblock);
113     wined3d_mutex_unlock();
114 
115     return D3D_OK;
116 }
117 
118 
119 static const struct IDirect3DStateBlock9Vtbl d3d9_stateblock_vtbl =
120 {
121     /* IUnknown */
122     d3d9_stateblock_QueryInterface,
123     d3d9_stateblock_AddRef,
124     d3d9_stateblock_Release,
125     /* IDirect3DStateBlock9 */
126     d3d9_stateblock_GetDevice,
127     d3d9_stateblock_Capture,
128     d3d9_stateblock_Apply,
129 };
130 
131 HRESULT stateblock_init(struct d3d9_stateblock *stateblock, struct d3d9_device *device,
132         D3DSTATEBLOCKTYPE type, struct wined3d_stateblock *wined3d_stateblock)
133 {
134     HRESULT hr;
135 
136     stateblock->IDirect3DStateBlock9_iface.lpVtbl = &d3d9_stateblock_vtbl;
137     stateblock->refcount = 1;
138 
139     if (wined3d_stateblock)
140     {
141         stateblock->wined3d_stateblock = wined3d_stateblock;
142     }
143     else
144     {
145         wined3d_mutex_lock();
146         hr = wined3d_stateblock_create(device->wined3d_device,
147                 (enum wined3d_stateblock_type)type, &stateblock->wined3d_stateblock);
148         wined3d_mutex_unlock();
149         if (FAILED(hr))
150         {
151             WARN("Failed to create wined3d stateblock, hr %#x.\n", hr);
152             return hr;
153         }
154     }
155 
156     stateblock->parent_device = &device->IDirect3DDevice9Ex_iface;
157     IDirect3DDevice9Ex_AddRef(stateblock->parent_device);
158 
159     return D3D_OK;
160 }
161