xref: /reactos/dll/directx/wine/d3dx9_36/core.c (revision 6760065e)
1 #ifdef __REACTOS__
2 #include "precomp.h"
3 #else
4 /*
5  *
6  * Copyright 2002 Raphael Junqueira
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 "wine/port.h"
25 
26 #include "d3dx9_private.h"
27 #endif /* __REACTOS__ */
28 
29 WINE_DEFAULT_DEBUG_CHANNEL(d3dx);
30 
31 struct ID3DXBufferImpl
32 {
33     ID3DXBuffer ID3DXBuffer_iface;
34     LONG ref;
35 
36     void *buffer;
37     DWORD size;
38 };
39 
40 static inline struct ID3DXBufferImpl *impl_from_ID3DXBuffer(ID3DXBuffer *iface)
41 {
42     return CONTAINING_RECORD(iface, struct ID3DXBufferImpl, ID3DXBuffer_iface);
43 }
44 
45 static HRESULT WINAPI ID3DXBufferImpl_QueryInterface(ID3DXBuffer *iface, REFIID riid, void **ppobj)
46 {
47     TRACE("iface %p, riid %s, object %p\n", iface, debugstr_guid(riid), ppobj);
48 
49     if (IsEqualGUID(riid, &IID_IUnknown)
50       || IsEqualGUID(riid, &IID_ID3DXBuffer))
51     {
52         IUnknown_AddRef(iface);
53         *ppobj = iface;
54         return D3D_OK;
55     }
56 
57     WARN("%s not implemented, returning E_NOINTERFACE\n", debugstr_guid(riid));
58 
59     return E_NOINTERFACE;
60 }
61 
62 static ULONG WINAPI ID3DXBufferImpl_AddRef(ID3DXBuffer *iface)
63 {
64     struct ID3DXBufferImpl *This = impl_from_ID3DXBuffer(iface);
65     ULONG ref = InterlockedIncrement(&This->ref);
66 
67     TRACE("%p increasing refcount to %u\n", This, ref);
68 
69     return ref;
70 }
71 
72 static ULONG WINAPI ID3DXBufferImpl_Release(ID3DXBuffer *iface)
73 {
74     struct ID3DXBufferImpl *This = impl_from_ID3DXBuffer(iface);
75     ULONG ref = InterlockedDecrement(&This->ref);
76 
77     TRACE("%p decreasing refcount to %u\n", This, ref);
78 
79     if (ref == 0)
80     {
81         HeapFree(GetProcessHeap(), 0, This->buffer);
82         HeapFree(GetProcessHeap(), 0, This);
83     }
84 
85     return ref;
86 }
87 
88 static void * WINAPI ID3DXBufferImpl_GetBufferPointer(ID3DXBuffer *iface)
89 {
90     struct ID3DXBufferImpl *This = impl_from_ID3DXBuffer(iface);
91 
92     TRACE("iface %p\n", iface);
93 
94     return This->buffer;
95 }
96 
97 static DWORD WINAPI ID3DXBufferImpl_GetBufferSize(ID3DXBuffer *iface)
98 {
99     struct ID3DXBufferImpl *This = impl_from_ID3DXBuffer(iface);
100 
101     TRACE("iface %p\n", iface);
102 
103     return This->size;
104 }
105 
106 static const struct ID3DXBufferVtbl ID3DXBufferImpl_Vtbl =
107 {
108     /* IUnknown methods */
109     ID3DXBufferImpl_QueryInterface,
110     ID3DXBufferImpl_AddRef,
111     ID3DXBufferImpl_Release,
112     /* ID3DXBuffer methods */
113     ID3DXBufferImpl_GetBufferPointer,
114     ID3DXBufferImpl_GetBufferSize
115 };
116 
117 static HRESULT d3dx9_buffer_init(struct ID3DXBufferImpl *buffer, DWORD size)
118 {
119     buffer->ID3DXBuffer_iface.lpVtbl = &ID3DXBufferImpl_Vtbl;
120     buffer->ref = 1;
121     buffer->size = size;
122 
123     buffer->buffer = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, size);
124     if (!buffer->buffer)
125     {
126         ERR("Failed to allocate buffer memory\n");
127         return E_OUTOFMEMORY;
128     }
129 
130     return D3D_OK;
131 }
132 
133 HRESULT WINAPI D3DXCreateBuffer(DWORD size, ID3DXBuffer **buffer)
134 {
135     struct ID3DXBufferImpl *object;
136     HRESULT hr;
137 
138     if (!buffer)
139     {
140         WARN("Invalid buffer specified.\n");
141         return D3DERR_INVALIDCALL;
142     }
143 
144     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
145     if (!object)
146         return E_OUTOFMEMORY;
147 
148     hr = d3dx9_buffer_init(object, size);
149     if (FAILED(hr))
150     {
151         WARN("Failed to initialize buffer, hr %#x.\n", hr);
152         HeapFree(GetProcessHeap(), 0, object);
153         return hr;
154     }
155 
156     *buffer = &object->ID3DXBuffer_iface;
157 
158     TRACE("Created ID3DXBuffer %p.\n", *buffer);
159 
160     return D3D_OK;
161 }
162