d3dx9/tests: Remove d3d9 test from d3dx9 tests.
[wine] / dlls / d3dx9_36 / core.c
1 /*
2  *
3  * Copyright 2002 Raphael Junqueira
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
18  */
19
20 #include <stdarg.h>
21
22 #define COBJMACROS
23 #include "windef.h"
24 #include "winbase.h"
25 #include "wingdi.h"
26 #include "wine/debug.h"
27 #include "wine/unicode.h"
28
29 #include "d3dx9_36_private.h"
30
31 WINE_DEFAULT_DEBUG_CHANNEL(d3dx);
32
33 struct ID3DXBufferImpl
34 {
35     ID3DXBuffer ID3DXBuffer_iface;
36     LONG ref;
37
38     void *buffer;
39     DWORD size;
40 };
41
42 static inline struct ID3DXBufferImpl *impl_from_ID3DXBuffer(ID3DXBuffer *iface)
43 {
44     return CONTAINING_RECORD(iface, struct ID3DXBufferImpl, ID3DXBuffer_iface);
45 }
46
47 static HRESULT WINAPI ID3DXBufferImpl_QueryInterface(ID3DXBuffer *iface, REFIID riid, void **ppobj)
48 {
49     TRACE("iface %p, riid %s, object %p\n", iface, debugstr_guid(riid), ppobj);
50
51     if (IsEqualGUID(riid, &IID_IUnknown)
52       || IsEqualGUID(riid, &IID_ID3DXBuffer))
53     {
54         IUnknown_AddRef(iface);
55         *ppobj = iface;
56         return D3D_OK;
57     }
58
59     WARN("%s not implemented, returning E_NOINTERFACE\n", debugstr_guid(riid));
60
61     return E_NOINTERFACE;
62 }
63
64 static ULONG WINAPI ID3DXBufferImpl_AddRef(ID3DXBuffer *iface)
65 {
66     struct ID3DXBufferImpl *This = impl_from_ID3DXBuffer(iface);
67     ULONG ref = InterlockedIncrement(&This->ref);
68
69     TRACE("%p increasing refcount to %u\n", This, ref);
70
71     return ref;
72 }
73
74 static ULONG WINAPI ID3DXBufferImpl_Release(ID3DXBuffer *iface)
75 {
76     struct ID3DXBufferImpl *This = impl_from_ID3DXBuffer(iface);
77     ULONG ref = InterlockedDecrement(&This->ref);
78
79     TRACE("%p decreasing refcount to %u\n", This, ref);
80
81     if (ref == 0)
82     {
83         HeapFree(GetProcessHeap(), 0, This->buffer);
84         HeapFree(GetProcessHeap(), 0, This);
85     }
86
87     return ref;
88 }
89
90 static LPVOID WINAPI ID3DXBufferImpl_GetBufferPointer(ID3DXBuffer *iface)
91 {
92     struct ID3DXBufferImpl *This = impl_from_ID3DXBuffer(iface);
93
94     TRACE("iface %p\n", iface);
95
96     return This->buffer;
97 }
98
99 static DWORD WINAPI ID3DXBufferImpl_GetBufferSize(ID3DXBuffer *iface)
100 {
101     struct ID3DXBufferImpl *This = impl_from_ID3DXBuffer(iface);
102
103     TRACE("iface %p\n", iface);
104
105     return This->size;
106 }
107
108 static const struct ID3DXBufferVtbl ID3DXBufferImpl_Vtbl =
109 {
110     /* IUnknown methods */
111     ID3DXBufferImpl_QueryInterface,
112     ID3DXBufferImpl_AddRef,
113     ID3DXBufferImpl_Release,
114     /* ID3DXBuffer methods */
115     ID3DXBufferImpl_GetBufferPointer,
116     ID3DXBufferImpl_GetBufferSize
117 };
118
119 static HRESULT d3dx9_buffer_init(struct ID3DXBufferImpl *buffer, DWORD size)
120 {
121     buffer->ID3DXBuffer_iface.lpVtbl = &ID3DXBufferImpl_Vtbl;
122     buffer->ref = 1;
123     buffer->size = size;
124
125     buffer->buffer = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, size);
126     if (!buffer->buffer)
127     {
128         ERR("Failed to allocate buffer memory\n");
129         return E_OUTOFMEMORY;
130     }
131
132     return D3D_OK;
133 }
134
135 HRESULT WINAPI D3DXCreateBuffer(DWORD size, LPD3DXBUFFER *buffer)
136 {
137     struct ID3DXBufferImpl *object;
138     HRESULT hr;
139
140     if (!buffer)
141     {
142         WARN("Invalid buffer specified.\n");
143         return D3DERR_INVALIDCALL;
144     }
145
146     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
147     if (!object)
148     {
149         ERR("Failed to allocate buffer memory\n");
150         return E_OUTOFMEMORY;
151     }
152
153     hr = d3dx9_buffer_init(object, size);
154     if (FAILED(hr))
155     {
156         WARN("Failed to initialize buffer, hr %#x.\n", hr);
157         HeapFree(GetProcessHeap(), 0, object);
158         return hr;
159     }
160
161     *buffer = &object->ID3DXBuffer_iface;
162
163     TRACE("Created ID3DBuffer %p\n", *buffer);
164
165     return D3D_OK;
166 }