ReactOS 0.4.17-dev-966-gf06eace
surface.c
Go to the documentation of this file.
1/*
2 * IDirect3DSurface8 implementation
3 *
4 * Copyright 2005 Oliver Stieber
5 *
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.
10 *
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.
15 *
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
19 */
20
21#include "d3d8_private.h"
22
24
25static inline struct d3d8_surface *impl_from_IDirect3DSurface8(IDirect3DSurface8 *iface)
26{
28}
29
30static HRESULT WINAPI d3d8_surface_QueryInterface(IDirect3DSurface8 *iface, REFIID riid, void **out)
31{
32 TRACE("iface %p, riid %s, out %p.\n", iface, debugstr_guid(riid), out);
33
34 if (IsEqualGUID(riid, &IID_IDirect3DSurface8)
35 || IsEqualGUID(riid, &IID_IDirect3DResource8)
37 {
39 *out = iface;
40 return S_OK;
41 }
42
43 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
44
45 *out = NULL;
46 return E_NOINTERFACE;
47}
48
49static ULONG WINAPI d3d8_surface_AddRef(IDirect3DSurface8 *iface)
50{
51 struct d3d8_surface *surface = impl_from_IDirect3DSurface8(iface);
52 ULONG refcount;
53
54 TRACE("iface %p.\n", iface);
55
56 if (surface->texture)
57 {
58 TRACE("Forwarding to %p.\n", surface->texture);
59 return IDirect3DBaseTexture8_AddRef(&surface->texture->IDirect3DBaseTexture8_iface);
60 }
61
62 refcount = InterlockedIncrement(&surface->resource.refcount);
63 TRACE("%p increasing refcount to %lu.\n", iface, refcount);
64
65 if (refcount == 1)
66 {
67 if (surface->parent_device)
69 if (surface->wined3d_rtv)
72 if (surface->swapchain)
74 }
75
76 return refcount;
77}
78
79static ULONG WINAPI d3d8_surface_Release(IDirect3DSurface8 *iface)
80{
81 struct d3d8_surface *surface = impl_from_IDirect3DSurface8(iface);
82 ULONG refcount;
83
84 TRACE("iface %p.\n", iface);
85
86 if (surface->texture)
87 {
88 TRACE("Forwarding to %p.\n", surface->texture);
89 return IDirect3DBaseTexture8_Release(&surface->texture->IDirect3DBaseTexture8_iface);
90 }
91
92 if (!surface->resource.refcount)
93 {
94 WARN("Surface does not have any references.\n");
95 return 0;
96 }
97
98 refcount = InterlockedDecrement(&surface->resource.refcount);
99 TRACE("%p decreasing refcount to %lu.\n", iface, refcount);
100
101 if (!refcount)
102 {
103 IDirect3DDevice8 *parent_device = surface->parent_device;
104
105 if (surface->wined3d_rtv)
107 if (surface->swapchain)
109 /* Releasing the texture may free the d3d8 object, so do not access it
110 * after releasing the texture. */
112
113 if (parent_device)
115 }
116
117 return refcount;
118}
119
120static HRESULT WINAPI d3d8_surface_GetDevice(IDirect3DSurface8 *iface, IDirect3DDevice8 **device)
121{
122 struct d3d8_surface *surface = impl_from_IDirect3DSurface8(iface);
123
124 TRACE("iface %p, device %p.\n", iface, device);
125
126 if (surface->texture)
127 return IDirect3DBaseTexture8_GetDevice(&surface->texture->IDirect3DBaseTexture8_iface, device);
128
129 *device = surface->parent_device;
131
132 TRACE("Returning device %p.\n", *device);
133
134 return D3D_OK;
135}
136
137static HRESULT WINAPI d3d8_surface_SetPrivateData(IDirect3DSurface8 *iface, REFGUID guid,
138 const void *data, DWORD data_size, DWORD flags)
139{
140 struct d3d8_surface *surface = impl_from_IDirect3DSurface8(iface);
141 TRACE("iface %p, guid %s, data %p, data_size %lu, flags %#lx.\n",
142 iface, debugstr_guid(guid), data, data_size, flags);
143
144 return d3d8_resource_set_private_data(&surface->resource, guid, data, data_size, flags);
145}
146
147static HRESULT WINAPI d3d8_surface_GetPrivateData(IDirect3DSurface8 *iface, REFGUID guid,
148 void *data, DWORD *data_size)
149{
150 struct d3d8_surface *surface = impl_from_IDirect3DSurface8(iface);
151 TRACE("iface %p, guid %s, data %p, data_size %p.\n",
152 iface, debugstr_guid(guid), data, data_size);
153
154 return d3d8_resource_get_private_data(&surface->resource, guid, data, data_size);
155}
156
157static HRESULT WINAPI d3d8_surface_FreePrivateData(IDirect3DSurface8 *iface, REFGUID guid)
158{
159 struct d3d8_surface *surface = impl_from_IDirect3DSurface8(iface);
160 TRACE("iface %p, guid %s.\n", iface, debugstr_guid(guid));
161
163}
164
165static HRESULT WINAPI d3d8_surface_GetContainer(IDirect3DSurface8 *iface, REFIID riid, void **container)
166{
167 struct d3d8_surface *surface = impl_from_IDirect3DSurface8(iface);
168 HRESULT hr;
169
170 TRACE("iface %p, riid %s, container %p.\n", iface, debugstr_guid(riid), container);
171
172 if (!surface->container)
173 return E_NOINTERFACE;
174
175 hr = IUnknown_QueryInterface(surface->container, riid, container);
176
177 TRACE("Returning %p.\n", *container);
178
179 return hr;
180}
181
182static HRESULT WINAPI d3d8_surface_GetDesc(IDirect3DSurface8 *iface, D3DSURFACE_DESC *desc)
183{
184 struct d3d8_surface *surface = impl_from_IDirect3DSurface8(iface);
185 struct wined3d_sub_resource_desc wined3d_desc;
186
187 TRACE("iface %p, desc %p.\n", iface, desc);
188
192
193 desc->Format = d3dformat_from_wined3dformat(wined3d_desc.format);
194 desc->Type = D3DRTYPE_SURFACE;
195 desc->Usage = d3dusage_from_wined3dusage(wined3d_desc.usage, wined3d_desc.bind_flags);
196 desc->Pool = d3dpool_from_wined3daccess(wined3d_desc.access, wined3d_desc.usage);
197 desc->Size = wined3d_desc.size;
198 desc->MultiSampleType = d3dmultisample_type_from_wined3d(wined3d_desc.multisample_type);
199 desc->Width = wined3d_desc.width;
200 desc->Height = wined3d_desc.height;
201
202 return D3D_OK;
203}
204
205static HRESULT WINAPI d3d8_surface_LockRect(IDirect3DSurface8 *iface,
206 D3DLOCKED_RECT *locked_rect, const RECT *rect, DWORD flags)
207{
208 struct d3d8_surface *surface = impl_from_IDirect3DSurface8(iface);
209 struct wined3d_box box;
210 struct wined3d_map_desc map_desc;
211 HRESULT hr;
213
214 TRACE("iface %p, locked_rect %p, rect %s, flags %#lx.\n",
215 iface, locked_rect, wine_dbgstr_rect(rect), flags);
216
218
219 if (surface->texture)
220 type = IDirect3DBaseTexture8_GetType(&surface->texture->IDirect3DBaseTexture8_iface);
221 else
223
224 if (rect)
225 {
228
230 && ((rect->left < 0)
231 || (rect->top < 0)
232 || (rect->left >= rect->right)
233 || (rect->top >= rect->bottom)
234 || (rect->right > desc.Width)
235 || (rect->bottom > desc.Height)))
236 {
237 WARN("Trying to lock an invalid rectangle, returning D3DERR_INVALIDCALL\n");
239
240 locked_rect->Pitch = 0;
241 locked_rect->pBits = NULL;
242 return D3DERR_INVALIDCALL;
243 }
245 }
246
248 &map_desc, rect ? &box : NULL, wined3dmapflags_from_d3dmapflags(flags, 0));
250
251 if (SUCCEEDED(hr))
252 {
253 locked_rect->Pitch = map_desc.row_pitch;
254 locked_rect->pBits = map_desc.data;
255 }
256 else if (type != D3DRTYPE_TEXTURE)
257 {
258 locked_rect->Pitch = 0;
259 locked_rect->pBits = NULL;
260 }
261
262 if (hr == E_INVALIDARG)
263 return D3DERR_INVALIDCALL;
264 return hr;
265}
266
267static HRESULT WINAPI d3d8_surface_UnlockRect(IDirect3DSurface8 *iface)
268{
269 struct d3d8_surface *surface = impl_from_IDirect3DSurface8(iface);
270 HRESULT hr;
271
272 TRACE("iface %p.\n", iface);
273
275
276 if (hr == WINEDDERR_NOTLOCKED)
277 {
279 if (surface->texture)
280 type = IDirect3DBaseTexture8_GetType(&surface->texture->IDirect3DBaseTexture8_iface);
281 else
284 }
285 return hr;
286}
287
288static const IDirect3DSurface8Vtbl d3d8_surface_vtbl =
289{
290 /* IUnknown */
294 /* IDirect3DResource8 */
299 /* IDirect3DSurface8 */
304};
305
307{
308 struct d3d8_surface *surface = parent;
310 free(surface);
311}
312
314{
316};
317
319 unsigned int sub_resource_idx, IUnknown *container)
320{
321 IDirect3DBaseTexture8 *texture;
322 struct d3d8_surface *surface;
323
324 if (!(surface = calloc(1, sizeof(*surface))))
325 return NULL;
326
328 d3d8_resource_init(&surface->resource);
329 surface->resource.refcount = 0;
330 list_init(&surface->rtv_entry);
331 surface->container = container;
335
336 if (surface->container && SUCCEEDED(IUnknown_QueryInterface(surface->container,
337 &IID_IDirect3DBaseTexture8, (void **)&texture)))
338 {
341 }
342
345
346 TRACE("Created surface %p.\n", surface);
347 return surface;
348}
349
351{
352 struct d3d8_surface *surface = parent;
353
354 /* If the surface reference count drops to zero, we release our reference
355 * to the view, but don't clear the pointer yet, in case e.g. a
356 * GetRenderTarget() call brings the surface back before the view is
357 * actually destroyed. When the view is destroyed, we need to clear the
358 * pointer, or a subsequent surface AddRef() would reference it again.
359 *
360 * This is safe because as long as the view still has a reference to the
361 * texture, the surface is also still alive, and we're called before the
362 * view releases that reference. */
363 surface->wined3d_rtv = NULL;
364 list_remove(&surface->rtv_entry);
365}
366
368{
370};
371
373{
374 IDirect3DDevice8 *device;
375 device = surface->texture ? &surface->texture->parent_device->IDirect3DDevice8_iface : surface->parent_device;
377}
378
380{
381 HRESULT hr;
382
383 /* The surface reference count can be equal to 0 when this function is
384 * called. In order to properly manage the render target view reference
385 * count, we temporarily increment the surface reference count. */
387
388 if (surface->wined3d_rtv)
389 return surface->wined3d_rtv;
390
392 surface->sub_resource_idx, surface, &d3d8_view_wined3d_parent_ops, &surface->wined3d_rtv)))
393 {
394 ERR("Failed to create rendertarget view, hr %#lx.\n", hr);
396 return NULL;
397 }
398
399 if (surface->texture)
400 list_add_head(&surface->texture->rtv_list, &surface->rtv_entry);
401
402 return surface->wined3d_rtv;
403}
404
406 struct wined3d_rendertarget_view *rtv)
407{
408 if (rtv)
410}
411
412struct d3d8_surface *unsafe_impl_from_IDirect3DSurface8(IDirect3DSurface8 *iface)
413{
414 if (!iface)
415 return NULL;
416 assert(iface->lpVtbl == &d3d8_surface_vtbl);
417
418 return impl_from_IDirect3DSurface8(iface);
419}
#define InterlockedIncrement
Definition: armddk.h:53
#define InterlockedDecrement
Definition: armddk.h:52
static const char * wine_dbgstr_rect(const RECT *prc)
Definition: atltest.h:160
#define WINE_DEFAULT_DEBUG_CHANNEL(t)
Definition: precomp.h:23
static void list_remove(struct list_entry *entry)
Definition: list.h:90
static void list_add_head(struct list_entry *head, struct list_entry *entry)
Definition: list.h:76
static void list_init(struct list_entry *head)
Definition: list.h:51
#define WARN(fmt,...)
Definition: precomp.h:61
#define ERR(fmt,...)
Definition: precomp.h:57
const GUID IID_IUnknown
#define STDMETHODCALLTYPE
Definition: bdasup.h:9
RECT rect
Definition: combotst.c:67
#define D3DERR_INVALIDCALL
Definition: d3d10_private.h:42
HRESULT d3d8_resource_get_private_data(struct d3d8_resource *resource, const GUID *guid, void *data, DWORD *data_size)
Definition: d3d8_main.c:172
void d3d8_resource_cleanup(struct d3d8_resource *resource)
Definition: d3d8_main.c:149
void d3d8_resource_init(struct d3d8_resource *resource)
Definition: d3d8_main.c:210
HRESULT d3d8_resource_set_private_data(struct d3d8_resource *resource, const GUID *guid, const void *data, DWORD data_size, DWORD flags)
Definition: d3d8_main.c:216
HRESULT d3d8_resource_free_private_data(struct d3d8_resource *resource, const GUID *guid)
Definition: d3d8_main.c:154
static DWORD d3dusage_from_wined3dusage(unsigned int wined3d_usage, unsigned int bind_flags)
Definition: d3d8_private.h:305
struct d3d8_texture * unsafe_impl_from_IDirect3DBaseTexture8(IDirect3DBaseTexture8 *iface)
Definition: texture.c:1017
D3DFORMAT d3dformat_from_wined3dformat(enum wined3d_format_id format)
Definition: device.c:33
static D3DMULTISAMPLE_TYPE d3dmultisample_type_from_wined3d(enum wined3d_multisample_type type)
Definition: d3d8_private.h:387
static struct d3d8_device * impl_from_IDirect3DDevice8(IDirect3DDevice8 *iface)
Definition: d3d8_private.h:144
unsigned int wined3dmapflags_from_d3dmapflags(unsigned int flags, unsigned int usage)
Definition: device.c:135
static D3DPOOL d3dpool_from_wined3daccess(unsigned int access, unsigned int usage)
Definition: d3d8_private.h:315
enum _D3DRESOURCETYPE D3DRESOURCETYPE
@ D3DRTYPE_TEXTURE
Definition: d3d8types.h:812
@ D3DRTYPE_SURFACE
Definition: d3d8types.h:810
#define D3D_OK
Definition: d3d.h:106
#define E_INVALIDARG
Definition: ddrawi.h:101
#define free
Definition: debug_ros.c:5
HRESULT hr
Definition: delayimp.cpp:582
#define NULL
Definition: types.h:112
static void STDMETHODCALLTYPE surface_wined3d_object_destroyed(void *parent)
Definition: surface.c:306
static HRESULT WINAPI d3d8_surface_LockRect(IDirect3DSurface8 *iface, D3DLOCKED_RECT *locked_rect, const RECT *rect, DWORD flags)
Definition: surface.c:205
static ULONG WINAPI d3d8_surface_Release(IDirect3DSurface8 *iface)
Definition: surface.c:79
static HRESULT WINAPI d3d8_surface_SetPrivateData(IDirect3DSurface8 *iface, REFGUID guid, const void *data, DWORD data_size, DWORD flags)
Definition: surface.c:137
static HRESULT WINAPI d3d8_surface_GetPrivateData(IDirect3DSurface8 *iface, REFGUID guid, void *data, DWORD *data_size)
Definition: surface.c:147
static HRESULT WINAPI d3d8_surface_FreePrivateData(IDirect3DSurface8 *iface, REFGUID guid)
Definition: surface.c:157
struct d3d8_surface * unsafe_impl_from_IDirect3DSurface8(IDirect3DSurface8 *iface)
Definition: surface.c:412
struct d3d8_device * d3d8_surface_get_device(const struct d3d8_surface *surface)
Definition: surface.c:372
static const struct wined3d_parent_ops d3d8_surface_wined3d_parent_ops
Definition: surface.c:313
static HRESULT WINAPI d3d8_surface_QueryInterface(IDirect3DSurface8 *iface, REFIID riid, void **out)
Definition: surface.c:30
static HRESULT WINAPI d3d8_surface_GetDesc(IDirect3DSurface8 *iface, D3DSURFACE_DESC *desc)
Definition: surface.c:182
static ULONG WINAPI d3d8_surface_AddRef(IDirect3DSurface8 *iface)
Definition: surface.c:49
struct wined3d_rendertarget_view * d3d8_surface_acquire_rendertarget_view(struct d3d8_surface *surface)
Definition: surface.c:379
static struct d3d8_surface * impl_from_IDirect3DSurface8(IDirect3DSurface8 *iface)
Definition: surface.c:25
static HRESULT WINAPI d3d8_surface_UnlockRect(IDirect3DSurface8 *iface)
Definition: surface.c:267
static const IDirect3DSurface8Vtbl d3d8_surface_vtbl
Definition: surface.c:288
static HRESULT WINAPI d3d8_surface_GetDevice(IDirect3DSurface8 *iface, IDirect3DDevice8 **device)
Definition: surface.c:120
struct d3d8_surface * d3d8_surface_create(struct wined3d_texture *wined3d_texture, unsigned int sub_resource_idx, IUnknown *container)
Definition: surface.c:318
static const struct wined3d_parent_ops d3d8_view_wined3d_parent_ops
Definition: surface.c:367
static void STDMETHODCALLTYPE view_wined3d_object_destroyed(void *parent)
Definition: surface.c:350
static HRESULT WINAPI d3d8_surface_GetContainer(IDirect3DSurface8 *iface, REFIID riid, void **container)
Definition: surface.c:165
void d3d8_surface_release_rendertarget_view(struct d3d8_surface *surface, struct wined3d_rendertarget_view *rtv)
Definition: surface.c:405
HRESULT CDECL wined3d_resource_unmap(struct wined3d_resource *resource, unsigned int sub_resource_idx)
Definition: resource.c:304
HRESULT CDECL wined3d_resource_map(struct wined3d_resource *resource, unsigned int sub_resource_idx, struct wined3d_map_desc *map_desc, const struct wined3d_box *box, uint32_t flags)
Definition: resource.c:295
HRESULT CDECL wined3d_texture_get_sub_resource_desc(const struct wined3d_texture *texture, unsigned int sub_resource_idx, struct wined3d_sub_resource_desc *desc)
Definition: texture.c:4055
struct wined3d_swapchain *CDECL wined3d_texture_get_swapchain(struct wined3d_texture *texture)
Definition: texture.c:4028
void CDECL wined3d_texture_set_sub_resource_parent(struct wined3d_texture *texture, unsigned int sub_resource_idx, void *parent, const struct wined3d_parent_ops *parent_ops)
Definition: texture.c:4043
struct wined3d_resource *CDECL wined3d_texture_get_resource(struct wined3d_texture *texture)
Definition: texture.c:1649
ULONG CDECL wined3d_texture_decref(struct wined3d_texture *texture)
Definition: texture.c:1606
ULONG CDECL wined3d_texture_incref(struct wined3d_texture *texture)
Definition: texture.c:1516
HRESULT CDECL wined3d_rendertarget_view_create_from_sub_resource(struct wined3d_texture *texture, unsigned int sub_resource_idx, void *parent, const struct wined3d_parent_ops *parent_ops, struct wined3d_rendertarget_view **view)
Definition: view.c:946
ULONG CDECL wined3d_rendertarget_view_incref(struct wined3d_rendertarget_view *view)
Definition: view.c:365
ULONG CDECL wined3d_rendertarget_view_decref(struct wined3d_rendertarget_view *view)
Definition: view.c:379
GUID guid
Definition: version.c:147
#define assert(_expr)
Definition: assert.h:32
r parent
Definition: btrfs.c:3010
unsigned long DWORD
Definition: ntddk_ex.h:95
GLuint GLuint GLsizei GLenum type
Definition: gl.h:1545
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
Definition: gl.h:1950
GLenum GLuint texture
Definition: glext.h:6295
GLbitfield flags
Definition: glext.h:7161
REFIID riid
Definition: atlbase.h:39
#define S_OK
Definition: intsafe.h:52
#define SUCCEEDED(hr)
Definition: intsafe.h:50
#define FAILED(hr)
Definition: intsafe.h:51
#define debugstr_guid
Definition: kernel32.h:35
D3D11_SHADER_VARIABLE_DESC desc
Definition: reflection.c:1683
#define IsEqualGUID(rguid1, rguid2)
Definition: guiddef.h:147
#define REFIID
Definition: guiddef.h:118
#define calloc
Definition: rosglue.h:14
#define IDirect3DSurface8_GetDesc(p, a)
Definition: d3d8.h:313
#define IDirect3DBaseTexture8_GetDevice(p, a)
Definition: d3d8.h:544
#define IDirect3DBaseTexture8_AddRef(p)
Definition: d3d8.h:541
#define IDirect3DBaseTexture8_GetType(p)
Definition: d3d8.h:551
#define IDirect3DSurface8_AddRef(p)
Definition: d3d8.h:305
#define IDirect3DDevice8_AddRef(p)
Definition: d3d8.h:943
#define IDirect3DBaseTexture8_Release(p)
Definition: d3d8.h:542
#define IDirect3DDevice8_Release(p)
Definition: d3d8.h:944
#define TRACE(s)
Definition: solgame.cpp:4
Definition: dcommon.idl:28
long bottom
Definition: dcommon.idl:29
long right
Definition: dcommon.idl:29
long left
Definition: dcommon.idl:29
long top
Definition: dcommon.idl:29
Definition: scsiwmi.h:51
Definition: palette.c:466
IDirect3DDevice8 * parent_device
Definition: d3d8_private.h:196
struct d3d8_texture * texture
Definition: d3d8_private.h:198
unsigned int sub_resource_idx
Definition: d3d8_private.h:193
struct list rtv_entry
Definition: d3d8_private.h:194
struct wined3d_rendertarget_view * wined3d_rtv
Definition: d3d8_private.h:195
IDirect3DSurface8 IDirect3DSurface8_iface
Definition: d3d8_private.h:190
struct d3d8_resource resource
Definition: d3d8_private.h:191
struct wined3d_texture * wined3d_texture
Definition: d3d8_private.h:192
IUnknown * container
Definition: d3d8_private.h:197
struct wined3d_swapchain * swapchain
Definition: d3d8_private.h:199
Definition: devices.h:37
enum wined3d_format_id format
Definition: wined3d.h:1806
unsigned int bind_flags
Definition: wined3d.h:1810
enum wined3d_multisample_type multisample_type
Definition: wined3d.h:1807
#define CONTAINING_RECORD(address, type, field)
Definition: typedefs.h:260
uint32_t ULONG
Definition: typedefs.h:59
wchar_t tm const _CrtWcstime_Writes_and_advances_ptr_ count wchar_t ** out
Definition: wcsftime.cpp:383
#define WINAPI
Definition: msvc.h:6
ULONG CDECL wined3d_swapchain_decref(struct wined3d_swapchain *swapchain)
Definition: swapchain.c:144
ULONG CDECL wined3d_swapchain_incref(struct wined3d_swapchain *swapchain)
Definition: swapchain.c:135
#define WINEDDERR_NOTLOCKED
Definition: wined3d.h:46
static void wined3d_box_set(struct wined3d_box *box, unsigned int left, unsigned int top, unsigned int right, unsigned int bottom, unsigned int front, unsigned int back)
Definition: wined3d.h:3006
void WINAPI wined3d_mutex_unlock(void)
Definition: wined3d_main.c:544
void WINAPI wined3d_mutex_lock(void)
Definition: wined3d_main.c:539
#define E_NOINTERFACE
Definition: winerror.h:3479