wine.desktop: Fix the Norwegian translations.
[wine/hacks.git] / dlls / d3d8 / surface.c
blobcf72e3e7d22ebff4b5be2ec7574e474e1a678391
1 /*
2 * IDirect3DSurface8 implementation
4 * Copyright 2005 Oliver Stieber
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "config.h"
22 #include "d3d8_private.h"
24 WINE_DEFAULT_DEBUG_CHANNEL(d3d8);
26 /* IDirect3DSurface8 IUnknown parts follow: */
27 static HRESULT WINAPI IDirect3DSurface8Impl_QueryInterface(LPDIRECT3DSURFACE8 iface, REFIID riid, LPVOID *ppobj) {
28 IDirect3DSurface8Impl *This = (IDirect3DSurface8Impl *)iface;
30 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), ppobj);
32 if (IsEqualGUID(riid, &IID_IUnknown)
33 || IsEqualGUID(riid, &IID_IDirect3DResource8)
34 || IsEqualGUID(riid, &IID_IDirect3DSurface8)) {
35 IUnknown_AddRef(iface);
36 *ppobj = This;
37 return S_OK;
40 WARN("(%p)->(%s,%p),not found\n", This, debugstr_guid(riid), ppobj);
41 *ppobj = NULL;
42 return E_NOINTERFACE;
45 static ULONG WINAPI IDirect3DSurface8Impl_AddRef(LPDIRECT3DSURFACE8 iface) {
46 IDirect3DSurface8Impl *This = (IDirect3DSurface8Impl *)iface;
48 TRACE("iface %p.\n", iface);
50 if (This->forwardReference) {
51 /* Forward refcounting */
52 TRACE("(%p) : Forwarding to %p\n", This, This->forwardReference);
53 return IUnknown_AddRef(This->forwardReference);
54 } else {
55 /* No container, handle our own refcounting */
56 ULONG ref = InterlockedIncrement(&This->ref);
58 TRACE("%p increasing refcount to %u.\n", iface, ref);
60 if (ref == 1)
62 if (This->parentDevice) IUnknown_AddRef(This->parentDevice);
63 wined3d_mutex_lock();
64 IUnknown_AddRef(This->wineD3DSurface);
65 wined3d_mutex_unlock();
68 return ref;
72 static ULONG WINAPI IDirect3DSurface8Impl_Release(LPDIRECT3DSURFACE8 iface) {
73 IDirect3DSurface8Impl *This = (IDirect3DSurface8Impl *)iface;
75 TRACE("iface %p.\n", iface);
77 if (This->forwardReference) {
78 /* Forward refcounting */
79 TRACE("(%p) : Forwarding to %p\n", This, This->forwardReference);
80 return IUnknown_Release(This->forwardReference);
81 } else {
82 /* No container, handle our own refcounting */
83 ULONG ref = InterlockedDecrement(&This->ref);
85 TRACE("%p decreasing refcount to %u.\n", iface, ref);
87 if (ref == 0) {
88 IDirect3DDevice8 *parentDevice = This->parentDevice;
90 /* Implicit surfaces are destroyed with the device, not if refcount reaches 0. */
91 wined3d_mutex_lock();
92 IWineD3DSurface_Release(This->wineD3DSurface);
93 wined3d_mutex_unlock();
95 if (parentDevice) IDirect3DDevice8_Release(parentDevice);
98 return ref;
102 /* IDirect3DSurface8 IDirect3DResource8 Interface follow: */
103 static HRESULT WINAPI IDirect3DSurface8Impl_GetDevice(IDirect3DSurface8 *iface, IDirect3DDevice8 **device)
105 IDirect3DSurface8Impl *This = (IDirect3DSurface8Impl *)iface;
107 TRACE("iface %p, device %p.\n", iface, device);
109 if (This->forwardReference)
111 IDirect3DResource8 *resource;
112 HRESULT hr;
114 hr = IUnknown_QueryInterface(This->forwardReference, &IID_IDirect3DResource8, (void **)&resource);
115 if (SUCCEEDED(hr))
117 hr = IDirect3DResource8_GetDevice(resource, device);
118 IDirect3DResource8_Release(resource);
120 TRACE("Returning device %p.\n", *device);
123 return hr;
126 *device = (IDirect3DDevice8 *)This->parentDevice;
127 IDirect3DDevice8_AddRef(*device);
129 TRACE("Returning device %p.\n", *device);
131 return D3D_OK;
134 static HRESULT WINAPI IDirect3DSurface8Impl_SetPrivateData(LPDIRECT3DSURFACE8 iface, REFGUID refguid, CONST void *pData, DWORD SizeOfData, DWORD Flags) {
135 IDirect3DSurface8Impl *This = (IDirect3DSurface8Impl *)iface;
136 HRESULT hr;
138 TRACE("iface %p, guid %s, data %p, data_size %u, flags %#x.\n",
139 iface, debugstr_guid(refguid), pData, SizeOfData, Flags);
141 wined3d_mutex_lock();
142 hr = IWineD3DSurface_SetPrivateData(This->wineD3DSurface, refguid, pData, SizeOfData, Flags);
143 wined3d_mutex_unlock();
145 return hr;
148 static HRESULT WINAPI IDirect3DSurface8Impl_GetPrivateData(LPDIRECT3DSURFACE8 iface, REFGUID refguid, void *pData, DWORD *pSizeOfData) {
149 IDirect3DSurface8Impl *This = (IDirect3DSurface8Impl *)iface;
150 HRESULT hr;
152 TRACE("iface %p, guid %s, data %p, data_size %p.\n",
153 iface, debugstr_guid(refguid), pData, pSizeOfData);
155 wined3d_mutex_lock();
156 hr = IWineD3DSurface_GetPrivateData(This->wineD3DSurface, refguid, pData, pSizeOfData);
157 wined3d_mutex_unlock();
159 return hr;
162 static HRESULT WINAPI IDirect3DSurface8Impl_FreePrivateData(LPDIRECT3DSURFACE8 iface, REFGUID refguid) {
163 IDirect3DSurface8Impl *This = (IDirect3DSurface8Impl *)iface;
164 HRESULT hr;
166 TRACE("iface %p, guid %s.\n", iface, debugstr_guid(refguid));
168 wined3d_mutex_lock();
169 hr = IWineD3DSurface_FreePrivateData(This->wineD3DSurface, refguid);
170 wined3d_mutex_unlock();
172 return hr;
175 /* IDirect3DSurface8 Interface follow: */
176 static HRESULT WINAPI IDirect3DSurface8Impl_GetContainer(LPDIRECT3DSURFACE8 iface, REFIID riid, void **ppContainer) {
177 IDirect3DSurface8Impl *This = (IDirect3DSurface8Impl *)iface;
178 HRESULT res;
180 TRACE("iface %p, riid %s, container %p.\n", iface, debugstr_guid(riid), ppContainer);
182 if (!This->container) return E_NOINTERFACE;
184 res = IUnknown_QueryInterface(This->container, riid, ppContainer);
186 TRACE("(%p) : returning %p\n", This, *ppContainer);
187 return res;
190 static HRESULT WINAPI IDirect3DSurface8Impl_GetDesc(LPDIRECT3DSURFACE8 iface, D3DSURFACE_DESC *pDesc) {
191 IDirect3DSurface8Impl *This = (IDirect3DSurface8Impl *)iface;
192 WINED3DSURFACE_DESC wined3ddesc;
193 HRESULT hr;
195 TRACE("iface %p, desc %p.\n", iface, pDesc);
197 wined3d_mutex_lock();
198 hr = IWineD3DSurface_GetDesc(This->wineD3DSurface, &wined3ddesc);
199 wined3d_mutex_unlock();
201 if (SUCCEEDED(hr))
203 pDesc->Format = d3dformat_from_wined3dformat(wined3ddesc.format);
204 pDesc->Type = wined3ddesc.resource_type;
205 pDesc->Usage = wined3ddesc.usage;
206 pDesc->Pool = wined3ddesc.pool;
207 pDesc->Size = wined3ddesc.size;
208 pDesc->MultiSampleType = wined3ddesc.multisample_type;
209 pDesc->Width = wined3ddesc.width;
210 pDesc->Height = wined3ddesc.height;
213 return hr;
216 static HRESULT WINAPI IDirect3DSurface8Impl_LockRect(LPDIRECT3DSURFACE8 iface, D3DLOCKED_RECT *pLockedRect, CONST RECT *pRect, DWORD Flags) {
217 IDirect3DSurface8Impl *This = (IDirect3DSurface8Impl *)iface;
218 HRESULT hr;
220 TRACE("iface %p, locked_rect %p, rect %p, flags %#x.\n", iface, pLockedRect, pRect, Flags);
222 wined3d_mutex_lock();
223 if (pRect) {
224 D3DSURFACE_DESC desc;
225 IDirect3DSurface8_GetDesc(iface, &desc);
227 if ((pRect->left < 0)
228 || (pRect->top < 0)
229 || (pRect->left >= pRect->right)
230 || (pRect->top >= pRect->bottom)
231 || (pRect->right > desc.Width)
232 || (pRect->bottom > desc.Height)) {
233 WARN("Trying to lock an invalid rectangle, returning D3DERR_INVALIDCALL\n");
234 wined3d_mutex_unlock();
236 return D3DERR_INVALIDCALL;
240 hr = IWineD3DSurface_LockRect(This->wineD3DSurface, (WINED3DLOCKED_RECT *) pLockedRect, pRect, Flags);
241 wined3d_mutex_unlock();
243 return hr;
246 static HRESULT WINAPI IDirect3DSurface8Impl_UnlockRect(LPDIRECT3DSURFACE8 iface) {
247 IDirect3DSurface8Impl *This = (IDirect3DSurface8Impl *)iface;
248 HRESULT hr;
250 TRACE("iface %p.\n", iface);
252 wined3d_mutex_lock();
253 hr = IWineD3DSurface_UnlockRect(This->wineD3DSurface);
254 wined3d_mutex_unlock();
256 switch(hr)
258 case WINEDDERR_NOTLOCKED: return D3DERR_INVALIDCALL;
259 default: return hr;
263 static const IDirect3DSurface8Vtbl Direct3DSurface8_Vtbl =
265 /* IUnknown */
266 IDirect3DSurface8Impl_QueryInterface,
267 IDirect3DSurface8Impl_AddRef,
268 IDirect3DSurface8Impl_Release,
269 /* IDirect3DResource8 */
270 IDirect3DSurface8Impl_GetDevice,
271 IDirect3DSurface8Impl_SetPrivateData,
272 IDirect3DSurface8Impl_GetPrivateData,
273 IDirect3DSurface8Impl_FreePrivateData,
274 /* IDirect3DSurface8 */
275 IDirect3DSurface8Impl_GetContainer,
276 IDirect3DSurface8Impl_GetDesc,
277 IDirect3DSurface8Impl_LockRect,
278 IDirect3DSurface8Impl_UnlockRect
281 static void STDMETHODCALLTYPE surface_wined3d_object_destroyed(void *parent)
283 HeapFree(GetProcessHeap(), 0, parent);
286 static const struct wined3d_parent_ops d3d8_surface_wined3d_parent_ops =
288 surface_wined3d_object_destroyed,
291 HRESULT surface_init(IDirect3DSurface8Impl *surface, IDirect3DDevice8Impl *device,
292 UINT width, UINT height, D3DFORMAT format, BOOL lockable, BOOL discard, UINT level,
293 DWORD usage, D3DPOOL pool, D3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality)
295 HRESULT hr;
297 surface->lpVtbl = &Direct3DSurface8_Vtbl;
298 surface->ref = 1;
300 /* FIXME: Check MAX bounds of MultisampleQuality. */
301 if (multisample_quality > 0)
303 FIXME("Multisample quality set to %u, substituting 0.\n", multisample_quality);
304 multisample_quality = 0;
307 wined3d_mutex_lock();
308 hr = IWineD3DDevice_CreateSurface(device->WineD3DDevice, width, height, wined3dformat_from_d3dformat(format),
309 lockable, discard, level, &surface->wineD3DSurface, usage & WINED3DUSAGE_MASK, (WINED3DPOOL)pool,
310 multisample_type, multisample_quality, SURFACE_OPENGL, (IUnknown *)surface,
311 &d3d8_surface_wined3d_parent_ops);
312 wined3d_mutex_unlock();
313 if (FAILED(hr))
315 WARN("Failed to create wined3d surface, hr %#x.\n", hr);
316 return hr;
319 surface->parentDevice = (IDirect3DDevice8 *)device;
320 IUnknown_AddRef(surface->parentDevice);
322 return D3D_OK;