gdiplus: Select the font into the appropriate hdc in GdipMeasureString.
[wine] / dlls / wined3d / volume.c
1 /*
2  * IWineD3DVolume implementation
3  *
4  * Copyright 2002-2005 Jason Edmeades
5  * Copyright 2002-2005 Raphael Junqueira
6  * Copyright 2005 Oliver Stieber
7  * Copyright 2009-2010 Henri Verbeet for CodeWeavers
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22  */
23
24 #include "config.h"
25 #include "wined3d_private.h"
26
27 WINE_DEFAULT_DEBUG_CHANNEL(d3d_surface);
28
29 /* Context activation is done by the caller. */
30 static void volume_bind_and_dirtify(struct IWineD3DVolumeImpl *volume, const struct wined3d_gl_info *gl_info)
31 {
32     IWineD3DBaseTextureImpl *container = (IWineD3DBaseTextureImpl *)volume->container;
33     DWORD active_sampler;
34
35     /* We don't need a specific texture unit, but after binding the texture the current unit is dirty.
36      * Read the unit back instead of switching to 0, this avoids messing around with the state manager's
37      * gl states. The current texture unit should always be a valid one.
38      *
39      * To be more specific, this is tricky because we can implicitly be called
40      * from sampler() in state.c. This means we can't touch anything other than
41      * whatever happens to be the currently active texture, or we would risk
42      * marking already applied sampler states dirty again.
43      *
44      * TODO: Track the current active texture per GL context instead of using glGet
45      */
46     if (gl_info->supported[ARB_MULTITEXTURE])
47     {
48         GLint active_texture;
49         ENTER_GL();
50         glGetIntegerv(GL_ACTIVE_TEXTURE, &active_texture);
51         LEAVE_GL();
52         active_sampler = volume->resource.device->rev_tex_unit_map[active_texture - GL_TEXTURE0_ARB];
53     } else {
54         active_sampler = 0;
55     }
56
57     if (active_sampler != WINED3D_UNMAPPED_STAGE)
58     {
59         IWineD3DDeviceImpl_MarkStateDirty(volume->resource.device, STATE_SAMPLER(active_sampler));
60     }
61
62     container->baseTexture.texture_ops->texture_bind(container, gl_info, FALSE);
63 }
64
65 void volume_add_dirty_box(struct IWineD3DVolumeImpl *volume, const WINED3DBOX *dirty_box)
66 {
67     volume->dirty = TRUE;
68     if (dirty_box)
69     {
70         volume->lockedBox.Left = min(volume->lockedBox.Left, dirty_box->Left);
71         volume->lockedBox.Top = min(volume->lockedBox.Top, dirty_box->Top);
72         volume->lockedBox.Front = min(volume->lockedBox.Front, dirty_box->Front);
73         volume->lockedBox.Right = max(volume->lockedBox.Right, dirty_box->Right);
74         volume->lockedBox.Bottom = max(volume->lockedBox.Bottom, dirty_box->Bottom);
75         volume->lockedBox.Back = max(volume->lockedBox.Back, dirty_box->Back);
76     }
77     else
78     {
79         volume->lockedBox.Left = 0;
80         volume->lockedBox.Top = 0;
81         volume->lockedBox.Front = 0;
82         volume->lockedBox.Right = volume->resource.width;
83         volume->lockedBox.Bottom = volume->resource.height;
84         volume->lockedBox.Back = volume->resource.depth;
85     }
86 }
87
88 void volume_set_container(IWineD3DVolumeImpl *volume, struct IWineD3DBaseTextureImpl *container)
89 {
90     TRACE("volume %p, container %p.\n", volume, container);
91
92     volume->container = container;
93 }
94
95 /* Context activation is done by the caller. */
96 void volume_load(IWineD3DVolumeImpl *volume, UINT level, BOOL srgb_mode)
97 {
98     const struct wined3d_gl_info *gl_info = &volume->resource.device->adapter->gl_info;
99     const struct wined3d_format *format = volume->resource.format;
100
101     TRACE("volume %p, level %u, srgb %#x, format %s (%#x).\n",
102             volume, level, srgb_mode, debug_d3dformat(format->id), format->id);
103
104     volume_bind_and_dirtify(volume, gl_info);
105
106     ENTER_GL();
107     GL_EXTCALL(glTexImage3DEXT(GL_TEXTURE_3D, level, format->glInternal,
108             volume->resource.width, volume->resource.height, volume->resource.depth,
109             0, format->glFormat, format->glType, volume->resource.allocatedMemory));
110     checkGLcall("glTexImage3D");
111     LEAVE_GL();
112
113     /* When adding code releasing volume->resource.allocatedMemory to save
114      * data keep in mind that GL_UNPACK_CLIENT_STORAGE_APPLE is enabled by
115      * default if supported(GL_APPLE_client_storage). Thus do not release
116      * volume->resource.allocatedMemory if GL_APPLE_client_storage is
117      * supported. */
118 }
119
120 /* Do not call while under the GL lock. */
121 static void volume_unload(struct wined3d_resource *resource)
122 {
123     TRACE("texture %p.\n", resource);
124
125     /* The whole content is shadowed on This->resource.allocatedMemory, and
126      * the texture name is managed by the VolumeTexture container. */
127
128     resource_unload(resource);
129 }
130
131 /* *******************************************
132    IWineD3DVolume IUnknown parts follow
133    ******************************************* */
134 static HRESULT WINAPI IWineD3DVolumeImpl_QueryInterface(IWineD3DVolume *iface, REFIID riid, void **object)
135 {
136     TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
137
138     if (IsEqualGUID(riid, &IID_IWineD3DVolume)
139             || IsEqualGUID(riid, &IID_IWineD3DResource)
140             || IsEqualGUID(riid, &IID_IWineD3DBase)
141             || IsEqualGUID(riid, &IID_IUnknown))
142     {
143         IUnknown_AddRef(iface);
144         *object = iface;
145         return S_OK;
146     }
147
148     WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
149
150     *object = NULL;
151     return E_NOINTERFACE;
152 }
153
154 static ULONG WINAPI IWineD3DVolumeImpl_AddRef(IWineD3DVolume *iface) {
155     IWineD3DVolumeImpl *This = (IWineD3DVolumeImpl *)iface;
156     TRACE("(%p) : AddRef increasing from %d\n", This, This->resource.ref);
157     return InterlockedIncrement(&This->resource.ref);
158 }
159
160 /* Do not call while under the GL lock. */
161 static ULONG WINAPI IWineD3DVolumeImpl_Release(IWineD3DVolume *iface) {
162     IWineD3DVolumeImpl *This = (IWineD3DVolumeImpl *)iface;
163     ULONG ref;
164     TRACE("(%p) : Releasing from %d\n", This, This->resource.ref);
165     ref = InterlockedDecrement(&This->resource.ref);
166
167     if (!ref)
168     {
169         resource_cleanup(&This->resource);
170         This->resource.parent_ops->wined3d_object_destroyed(This->resource.parent);
171         HeapFree(GetProcessHeap(), 0, This);
172     }
173     return ref;
174 }
175
176 /* ****************************************************
177    IWineD3DVolume IWineD3DResource parts follow
178    **************************************************** */
179 static void * WINAPI IWineD3DVolumeImpl_GetParent(IWineD3DVolume *iface)
180 {
181     TRACE("iface %p.\n", iface);
182
183     return ((IWineD3DVolumeImpl *)iface)->resource.parent;
184 }
185
186 static HRESULT WINAPI IWineD3DVolumeImpl_SetPrivateData(IWineD3DVolume *iface,
187         REFGUID riid, const void *data, DWORD data_size, DWORD flags)
188 {
189     return resource_set_private_data(&((IWineD3DVolumeImpl *)iface)->resource, riid, data, data_size, flags);
190 }
191
192 static HRESULT WINAPI IWineD3DVolumeImpl_GetPrivateData(IWineD3DVolume *iface,
193         REFGUID guid, void *data, DWORD *data_size)
194 {
195     return resource_get_private_data(&((IWineD3DVolumeImpl *)iface)->resource, guid, data, data_size);
196 }
197
198 static HRESULT WINAPI IWineD3DVolumeImpl_FreePrivateData(IWineD3DVolume *iface, REFGUID refguid)
199 {
200     return resource_free_private_data(&((IWineD3DVolumeImpl *)iface)->resource, refguid);
201 }
202
203 static DWORD WINAPI IWineD3DVolumeImpl_SetPriority(IWineD3DVolume *iface, DWORD priority)
204 {
205     return resource_set_priority(&((IWineD3DVolumeImpl *)iface)->resource, priority);
206 }
207
208 static DWORD WINAPI IWineD3DVolumeImpl_GetPriority(IWineD3DVolume *iface)
209 {
210     return resource_get_priority(&((IWineD3DVolumeImpl *)iface)->resource);
211 }
212
213 /* Do not call while under the GL lock. */
214 static void WINAPI IWineD3DVolumeImpl_PreLoad(IWineD3DVolume *iface) {
215     FIXME("iface %p stub!\n", iface);
216 }
217
218 static WINED3DRESOURCETYPE WINAPI IWineD3DVolumeImpl_GetType(IWineD3DVolume *iface)
219 {
220     return resource_get_type(&((IWineD3DVolumeImpl *)iface)->resource);
221 }
222
223 static struct wined3d_resource * WINAPI IWineD3DVolumeImpl_GetResource(IWineD3DVolume *iface)
224 {
225     TRACE("iface %p.\n", iface);
226
227     return &((IWineD3DVolumeImpl *)iface)->resource;
228 }
229
230 static HRESULT WINAPI IWineD3DVolumeImpl_Map(IWineD3DVolume *iface,
231         WINED3DLOCKED_BOX *pLockedVolume, const WINED3DBOX *pBox, DWORD flags)
232 {
233     IWineD3DVolumeImpl *This = (IWineD3DVolumeImpl *)iface;
234     FIXME("(%p) : pBox=%p stub\n", This, pBox);
235
236     if(!This->resource.allocatedMemory) {
237         This->resource.allocatedMemory = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, This->resource.size);
238     }
239
240     /* fixme: should we really lock as such? */
241     TRACE("(%p) : box=%p, output pbox=%p, allMem=%p\n", This, pBox, pLockedVolume, This->resource.allocatedMemory);
242
243     pLockedVolume->RowPitch = This->resource.format->byte_count * This->resource.width; /* Bytes / row */
244     pLockedVolume->SlicePitch = This->resource.format->byte_count
245             * This->resource.width * This->resource.height; /* Bytes / slice */
246     if (!pBox) {
247         TRACE("No box supplied - all is ok\n");
248         pLockedVolume->pBits = This->resource.allocatedMemory;
249         This->lockedBox.Left   = 0;
250         This->lockedBox.Top    = 0;
251         This->lockedBox.Front  = 0;
252         This->lockedBox.Right  = This->resource.width;
253         This->lockedBox.Bottom = This->resource.height;
254         This->lockedBox.Back   = This->resource.depth;
255     } else {
256         TRACE("Lock Box (%p) = l %d, t %d, r %d, b %d, fr %d, ba %d\n", pBox, pBox->Left, pBox->Top, pBox->Right, pBox->Bottom, pBox->Front, pBox->Back);
257         pLockedVolume->pBits = This->resource.allocatedMemory
258                 + (pLockedVolume->SlicePitch * pBox->Front)     /* FIXME: is front < back or vica versa? */
259                 + (pLockedVolume->RowPitch * pBox->Top)
260                 + (pBox->Left * This->resource.format->byte_count);
261         This->lockedBox.Left   = pBox->Left;
262         This->lockedBox.Top    = pBox->Top;
263         This->lockedBox.Front  = pBox->Front;
264         This->lockedBox.Right  = pBox->Right;
265         This->lockedBox.Bottom = pBox->Bottom;
266         This->lockedBox.Back   = pBox->Back;
267     }
268
269     if (!(flags & (WINED3DLOCK_NO_DIRTY_UPDATE | WINED3DLOCK_READONLY)))
270     {
271         volume_add_dirty_box(This, &This->lockedBox);
272         basetexture_set_dirty((IWineD3DBaseTextureImpl *)This->container, TRUE);
273     }
274
275     This->locked = TRUE;
276     TRACE("returning memory@%p rpitch(%d) spitch(%d)\n", pLockedVolume->pBits, pLockedVolume->RowPitch, pLockedVolume->SlicePitch);
277     return WINED3D_OK;
278 }
279
280 IWineD3DVolume * CDECL wined3d_volume_from_resource(struct wined3d_resource *resource)
281 {
282     return (IWineD3DVolume *)volume_from_resource(resource);
283 }
284
285 static HRESULT WINAPI IWineD3DVolumeImpl_Unmap(IWineD3DVolume *iface)
286 {
287     IWineD3DVolumeImpl *This = (IWineD3DVolumeImpl *)iface;
288     if (!This->locked)
289     {
290         WARN("Trying to unlock unlocked volume %p.\n", iface);
291         return WINED3DERR_INVALIDCALL;
292     }
293     TRACE("(%p) : unlocking volume\n", This);
294     This->locked = FALSE;
295     memset(&This->lockedBox, 0, sizeof(This->lockedBox));
296     return WINED3D_OK;
297 }
298
299 static const IWineD3DVolumeVtbl IWineD3DVolume_Vtbl =
300 {
301     /* IUnknown */
302     IWineD3DVolumeImpl_QueryInterface,
303     IWineD3DVolumeImpl_AddRef,
304     IWineD3DVolumeImpl_Release,
305     /* IWineD3DResource */
306     IWineD3DVolumeImpl_GetParent,
307     IWineD3DVolumeImpl_SetPrivateData,
308     IWineD3DVolumeImpl_GetPrivateData,
309     IWineD3DVolumeImpl_FreePrivateData,
310     IWineD3DVolumeImpl_SetPriority,
311     IWineD3DVolumeImpl_GetPriority,
312     IWineD3DVolumeImpl_PreLoad,
313     IWineD3DVolumeImpl_GetType,
314     /* IWineD3DVolume */
315     IWineD3DVolumeImpl_GetResource,
316     IWineD3DVolumeImpl_Map,
317     IWineD3DVolumeImpl_Unmap,
318 };
319
320 static const struct wined3d_resource_ops volume_resource_ops =
321 {
322     volume_unload,
323 };
324
325 HRESULT volume_init(IWineD3DVolumeImpl *volume, IWineD3DDeviceImpl *device, UINT width,
326         UINT height, UINT depth, DWORD usage, enum wined3d_format_id format_id, WINED3DPOOL pool,
327         void *parent, const struct wined3d_parent_ops *parent_ops)
328 {
329     const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
330     const struct wined3d_format *format = wined3d_get_format(gl_info, format_id);
331     HRESULT hr;
332
333     if (!gl_info->supported[EXT_TEXTURE3D])
334     {
335         WARN("Volume cannot be created - no volume texture support.\n");
336         return WINED3DERR_INVALIDCALL;
337     }
338
339     volume->lpVtbl = &IWineD3DVolume_Vtbl;
340
341     hr = resource_init(&volume->resource, device, WINED3DRTYPE_VOLUME, format,
342             WINED3DMULTISAMPLE_NONE, 0, usage, pool, width, height, depth,
343             width * height * depth * format->byte_count, parent, parent_ops,
344             &volume_resource_ops);
345     if (FAILED(hr))
346     {
347         WARN("Failed to initialize resource, returning %#x.\n", hr);
348         return hr;
349     }
350
351     volume->lockable = TRUE;
352     volume->locked = FALSE;
353     memset(&volume->lockedBox, 0, sizeof(volume->lockedBox));
354     volume->dirty = TRUE;
355
356     volume_add_dirty_box(volume, NULL);
357
358     return WINED3D_OK;
359 }