gdiplus: Implement writing SetPageTransform records.
[wine/multimedia.git] / dlls / d3d8 / surface.c
blobf1b143ac8dc750ce9f8e6aef78185a60042689be
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 static inline struct d3d8_surface *impl_from_IDirect3DSurface8(IDirect3DSurface8 *iface)
28 return CONTAINING_RECORD(iface, struct d3d8_surface, IDirect3DSurface8_iface);
31 static HRESULT WINAPI d3d8_surface_QueryInterface(IDirect3DSurface8 *iface, REFIID riid, void **out)
33 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), out);
35 if (IsEqualGUID(riid, &IID_IDirect3DSurface8)
36 || IsEqualGUID(riid, &IID_IDirect3DResource8)
37 || IsEqualGUID(riid, &IID_IUnknown))
39 IDirect3DSurface8_AddRef(iface);
40 *out = iface;
41 return S_OK;
44 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
46 *out = NULL;
47 return E_NOINTERFACE;
50 static ULONG WINAPI d3d8_surface_AddRef(IDirect3DSurface8 *iface)
52 struct d3d8_surface *surface = impl_from_IDirect3DSurface8(iface);
54 TRACE("iface %p.\n", iface);
56 if (surface->forwardReference)
58 /* Forward refcounting */
59 TRACE("Forwarding to %p.\n", surface->forwardReference);
60 return IUnknown_AddRef(surface->forwardReference);
62 else
64 /* No container, handle our own refcounting */
65 ULONG ref = InterlockedIncrement(&surface->refcount);
67 TRACE("%p increasing refcount to %u.\n", iface, ref);
69 if (ref == 1)
71 if (surface->parent_device)
72 IDirect3DDevice8_AddRef(surface->parent_device);
73 wined3d_mutex_lock();
74 wined3d_surface_incref(surface->wined3d_surface);
75 wined3d_mutex_unlock();
78 return ref;
82 static ULONG WINAPI d3d8_surface_Release(IDirect3DSurface8 *iface)
84 struct d3d8_surface *surface = impl_from_IDirect3DSurface8(iface);
86 TRACE("iface %p.\n", iface);
88 if (surface->forwardReference)
90 /* Forward refcounting */
91 TRACE("Forwarding to %p.\n", surface->forwardReference);
92 return IUnknown_Release(surface->forwardReference);
94 else
96 /* No container, handle our own refcounting */
97 ULONG ref = InterlockedDecrement(&surface->refcount);
99 TRACE("%p decreasing refcount to %u.\n", iface, ref);
101 if (!ref)
103 IDirect3DDevice8 *parent_device = surface->parent_device;
105 /* Implicit surfaces are destroyed with the device, not if refcount reaches 0. */
106 wined3d_mutex_lock();
107 wined3d_surface_decref(surface->wined3d_surface);
108 wined3d_mutex_unlock();
110 if (parent_device)
111 IDirect3DDevice8_Release(parent_device);
114 return ref;
118 static HRESULT WINAPI d3d8_surface_GetDevice(IDirect3DSurface8 *iface, IDirect3DDevice8 **device)
120 struct d3d8_surface *surface = impl_from_IDirect3DSurface8(iface);
122 TRACE("iface %p, device %p.\n", iface, device);
124 if (surface->forwardReference)
126 IDirect3DResource8 *resource;
127 HRESULT hr;
129 hr = IUnknown_QueryInterface(surface->forwardReference, &IID_IDirect3DResource8, (void **)&resource);
130 if (SUCCEEDED(hr))
132 hr = IDirect3DResource8_GetDevice(resource, device);
133 IDirect3DResource8_Release(resource);
135 TRACE("Returning device %p.\n", *device);
138 return hr;
141 *device = surface->parent_device;
142 IDirect3DDevice8_AddRef(*device);
144 TRACE("Returning device %p.\n", *device);
146 return D3D_OK;
149 static HRESULT WINAPI d3d8_surface_SetPrivateData(IDirect3DSurface8 *iface, REFGUID guid,
150 const void *data, DWORD data_size, DWORD flags)
152 struct d3d8_surface *surface = impl_from_IDirect3DSurface8(iface);
153 struct wined3d_resource *resource;
154 HRESULT hr;
156 TRACE("iface %p, guid %s, data %p, data_size %u, flags %#x.\n",
157 iface, debugstr_guid(guid), data, data_size, flags);
159 wined3d_mutex_lock();
160 resource = wined3d_surface_get_resource(surface->wined3d_surface);
161 hr = wined3d_resource_set_private_data(resource, guid, data, data_size, flags);
162 wined3d_mutex_unlock();
164 return hr;
167 static HRESULT WINAPI d3d8_surface_GetPrivateData(IDirect3DSurface8 *iface, REFGUID guid,
168 void *data, DWORD *data_size)
170 struct d3d8_surface *surface = impl_from_IDirect3DSurface8(iface);
171 struct wined3d_resource *resource;
172 HRESULT hr;
174 TRACE("iface %p, guid %s, data %p, data_size %p.\n",
175 iface, debugstr_guid(guid), data, data_size);
177 wined3d_mutex_lock();
178 resource = wined3d_surface_get_resource(surface->wined3d_surface);
179 hr = wined3d_resource_get_private_data(resource, guid, data, data_size);
180 wined3d_mutex_unlock();
182 return hr;
185 static HRESULT WINAPI d3d8_surface_FreePrivateData(IDirect3DSurface8 *iface, REFGUID guid)
187 struct d3d8_surface *surface = impl_from_IDirect3DSurface8(iface);
188 struct wined3d_resource *resource;
189 HRESULT hr;
191 TRACE("iface %p, guid %s.\n", iface, debugstr_guid(guid));
193 wined3d_mutex_lock();
194 resource = wined3d_surface_get_resource(surface->wined3d_surface);
195 hr = wined3d_resource_free_private_data(resource, guid);
196 wined3d_mutex_unlock();
198 return hr;
201 static HRESULT WINAPI d3d8_surface_GetContainer(IDirect3DSurface8 *iface, REFIID riid, void **container)
203 struct d3d8_surface *surface = impl_from_IDirect3DSurface8(iface);
204 HRESULT hr;
206 TRACE("iface %p, riid %s, container %p.\n", iface, debugstr_guid(riid), container);
208 if (!surface->container)
209 return E_NOINTERFACE;
211 hr = IUnknown_QueryInterface(surface->container, riid, container);
213 TRACE("Returning %p.\n", *container);
215 return hr;
218 static HRESULT WINAPI d3d8_surface_GetDesc(IDirect3DSurface8 *iface, D3DSURFACE_DESC *desc)
220 struct d3d8_surface *surface = impl_from_IDirect3DSurface8(iface);
221 struct wined3d_resource_desc wined3d_desc;
222 struct wined3d_resource *wined3d_resource;
224 TRACE("iface %p, desc %p.\n", iface, desc);
226 wined3d_mutex_lock();
227 wined3d_resource = wined3d_surface_get_resource(surface->wined3d_surface);
228 wined3d_resource_get_desc(wined3d_resource, &wined3d_desc);
229 wined3d_mutex_unlock();
231 desc->Format = d3dformat_from_wined3dformat(wined3d_desc.format);
232 desc->Type = wined3d_desc.resource_type;
233 desc->Usage = wined3d_desc.usage & WINED3DUSAGE_MASK;
234 desc->Pool = wined3d_desc.pool;
235 desc->Size = wined3d_desc.size;
236 desc->MultiSampleType = wined3d_desc.multisample_type;
237 desc->Width = wined3d_desc.width;
238 desc->Height = wined3d_desc.height;
240 return D3D_OK;
243 static HRESULT WINAPI d3d8_surface_LockRect(IDirect3DSurface8 *iface,
244 D3DLOCKED_RECT *locked_rect, const RECT *rect, DWORD flags)
246 struct d3d8_surface *surface = impl_from_IDirect3DSurface8(iface);
247 struct wined3d_map_desc map_desc;
248 HRESULT hr;
250 TRACE("iface %p, locked_rect %p, rect %s, flags %#x.\n",
251 iface, locked_rect, wine_dbgstr_rect(rect), flags);
253 wined3d_mutex_lock();
254 if (rect)
256 D3DSURFACE_DESC desc;
257 IDirect3DSurface8_GetDesc(iface, &desc);
259 if ((rect->left < 0)
260 || (rect->top < 0)
261 || (rect->left >= rect->right)
262 || (rect->top >= rect->bottom)
263 || (rect->right > desc.Width)
264 || (rect->bottom > desc.Height))
266 WARN("Trying to lock an invalid rectangle, returning D3DERR_INVALIDCALL\n");
267 wined3d_mutex_unlock();
269 return D3DERR_INVALIDCALL;
273 hr = wined3d_surface_map(surface->wined3d_surface, &map_desc, rect, flags);
274 wined3d_mutex_unlock();
276 if (SUCCEEDED(hr))
278 locked_rect->Pitch = map_desc.row_pitch;
279 locked_rect->pBits = map_desc.data;
281 else
283 locked_rect->Pitch = 0;
284 locked_rect->pBits = NULL;
287 return hr;
290 static HRESULT WINAPI d3d8_surface_UnlockRect(IDirect3DSurface8 *iface)
292 struct d3d8_surface *surface = impl_from_IDirect3DSurface8(iface);
293 HRESULT hr;
295 TRACE("iface %p.\n", iface);
297 wined3d_mutex_lock();
298 hr = wined3d_surface_unmap(surface->wined3d_surface);
299 wined3d_mutex_unlock();
301 switch(hr)
303 case WINEDDERR_NOTLOCKED: return D3DERR_INVALIDCALL;
304 default: return hr;
308 static const IDirect3DSurface8Vtbl d3d8_surface_vtbl =
310 /* IUnknown */
311 d3d8_surface_QueryInterface,
312 d3d8_surface_AddRef,
313 d3d8_surface_Release,
314 /* IDirect3DResource8 */
315 d3d8_surface_GetDevice,
316 d3d8_surface_SetPrivateData,
317 d3d8_surface_GetPrivateData,
318 d3d8_surface_FreePrivateData,
319 /* IDirect3DSurface8 */
320 d3d8_surface_GetContainer,
321 d3d8_surface_GetDesc,
322 d3d8_surface_LockRect,
323 d3d8_surface_UnlockRect,
326 static void STDMETHODCALLTYPE surface_wined3d_object_destroyed(void *parent)
328 HeapFree(GetProcessHeap(), 0, parent);
331 static const struct wined3d_parent_ops d3d8_surface_wined3d_parent_ops =
333 surface_wined3d_object_destroyed,
336 void surface_init(struct d3d8_surface *surface, struct wined3d_surface *wined3d_surface,
337 struct d3d8_device *device, const struct wined3d_parent_ops **parent_ops)
339 surface->IDirect3DSurface8_iface.lpVtbl = &d3d8_surface_vtbl;
340 surface->refcount = 1;
341 wined3d_surface_incref(wined3d_surface);
342 surface->wined3d_surface = wined3d_surface;
343 surface->parent_device = &device->IDirect3DDevice8_iface;
344 IDirect3DDevice8_AddRef(surface->parent_device);
346 *parent_ops = &d3d8_surface_wined3d_parent_ops;
349 struct d3d8_surface *unsafe_impl_from_IDirect3DSurface8(IDirect3DSurface8 *iface)
351 if (!iface)
352 return NULL;
353 assert(iface->lpVtbl == &d3d8_surface_vtbl);
355 return impl_from_IDirect3DSurface8(iface);