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