mmdevapi/tests: Add tests for IAudioClient::GetCurrentPadding.
[wine] / dlls / wined3d / surface.c
1 /*
2  * IWineD3DSurface Implementation
3  *
4  * Copyright 1997-2000 Marcus Meissner
5  * Copyright 1998-2000 Lionel Ulmer
6  * Copyright 2000-2001 TransGaming Technologies Inc.
7  * Copyright 2002-2005 Jason Edmeades
8  * Copyright 2002-2003 Raphael Junqueira
9  * Copyright 2004 Christian Costa
10  * Copyright 2005 Oliver Stieber
11  * Copyright 2006-2008 Stefan Dösinger for CodeWeavers
12  * Copyright 2007-2008 Henri Verbeet
13  * Copyright 2006-2008 Roderick Colenbrander
14  * Copyright 2009-2011 Henri Verbeet for CodeWeavers
15  *
16  * This library is free software; you can redistribute it and/or
17  * modify it under the terms of the GNU Lesser General Public
18  * License as published by the Free Software Foundation; either
19  * version 2.1 of the License, or (at your option) any later version.
20  *
21  * This library is distributed in the hope that it will be useful,
22  * but WITHOUT ANY WARRANTY; without even the implied warranty of
23  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
24  * Lesser General Public License for more details.
25  *
26  * You should have received a copy of the GNU Lesser General Public
27  * License along with this library; if not, write to the Free Software
28  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
29  */
30
31 #include "config.h"
32 #include "wine/port.h"
33 #include "wined3d_private.h"
34
35 WINE_DEFAULT_DEBUG_CHANNEL(d3d_surface);
36 WINE_DECLARE_DEBUG_CHANNEL(d3d);
37
38 static void surface_cleanup(IWineD3DSurfaceImpl *This)
39 {
40     TRACE("(%p) : Cleaning up.\n", This);
41
42     if (This->texture_name || (This->flags & SFLAG_PBO) || !list_empty(&This->renderbuffers))
43     {
44         const struct wined3d_gl_info *gl_info;
45         renderbuffer_entry_t *entry, *entry2;
46         struct wined3d_context *context;
47
48         context = context_acquire(This->resource.device, NULL);
49         gl_info = context->gl_info;
50
51         ENTER_GL();
52
53         if (This->texture_name)
54         {
55             TRACE("Deleting texture %u.\n", This->texture_name);
56             glDeleteTextures(1, &This->texture_name);
57         }
58
59         if (This->flags & SFLAG_PBO)
60         {
61             TRACE("Deleting PBO %u.\n", This->pbo);
62             GL_EXTCALL(glDeleteBuffersARB(1, &This->pbo));
63         }
64
65         LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, &This->renderbuffers, renderbuffer_entry_t, entry)
66         {
67             TRACE("Deleting renderbuffer %u.\n", entry->id);
68             gl_info->fbo_ops.glDeleteRenderbuffers(1, &entry->id);
69             HeapFree(GetProcessHeap(), 0, entry);
70         }
71
72         LEAVE_GL();
73
74         context_release(context);
75     }
76
77     if (This->flags & SFLAG_DIBSECTION)
78     {
79         /* Release the DC. */
80         SelectObject(This->hDC, This->dib.holdbitmap);
81         DeleteDC(This->hDC);
82         /* Release the DIB section. */
83         DeleteObject(This->dib.DIBsection);
84         This->dib.bitmap_data = NULL;
85         This->resource.allocatedMemory = NULL;
86     }
87
88     if (This->flags & SFLAG_USERPTR) IWineD3DSurface_SetMem((IWineD3DSurface *)This, NULL);
89     if (This->overlay_dest) list_remove(&This->overlay_entry);
90
91     HeapFree(GetProcessHeap(), 0, This->palette9);
92
93     resource_cleanup(&This->resource);
94 }
95
96 void surface_set_container(IWineD3DSurfaceImpl *surface, enum wined3d_container_type type, void *container)
97 {
98     TRACE("surface %p, container %p.\n", surface, container);
99
100     if (!container && type != WINED3D_CONTAINER_NONE)
101         ERR("Setting NULL container of type %#x.\n", type);
102
103     if (type == WINED3D_CONTAINER_SWAPCHAIN)
104     {
105         surface->get_drawable_size = get_drawable_size_swapchain;
106     }
107     else
108     {
109         switch (wined3d_settings.offscreen_rendering_mode)
110         {
111             case ORM_FBO:
112                 surface->get_drawable_size = get_drawable_size_fbo;
113                 break;
114
115             case ORM_BACKBUFFER:
116                 surface->get_drawable_size = get_drawable_size_backbuffer;
117                 break;
118
119             default:
120                 ERR("Unhandled offscreen rendering mode %#x.\n", wined3d_settings.offscreen_rendering_mode);
121                 return;
122         }
123     }
124
125     surface->container.type = type;
126     surface->container.u.base = container;
127 }
128
129 struct blt_info
130 {
131     GLenum binding;
132     GLenum bind_target;
133     enum tex_types tex_type;
134     GLfloat coords[4][3];
135 };
136
137 struct float_rect
138 {
139     float l;
140     float t;
141     float r;
142     float b;
143 };
144
145 static inline void cube_coords_float(const RECT *r, UINT w, UINT h, struct float_rect *f)
146 {
147     f->l = ((r->left * 2.0f) / w) - 1.0f;
148     f->t = ((r->top * 2.0f) / h) - 1.0f;
149     f->r = ((r->right * 2.0f) / w) - 1.0f;
150     f->b = ((r->bottom * 2.0f) / h) - 1.0f;
151 }
152
153 static void surface_get_blt_info(GLenum target, const RECT *rect, GLsizei w, GLsizei h, struct blt_info *info)
154 {
155     GLfloat (*coords)[3] = info->coords;
156     struct float_rect f;
157
158     switch (target)
159     {
160         default:
161             FIXME("Unsupported texture target %#x\n", target);
162             /* Fall back to GL_TEXTURE_2D */
163         case GL_TEXTURE_2D:
164             info->binding = GL_TEXTURE_BINDING_2D;
165             info->bind_target = GL_TEXTURE_2D;
166             info->tex_type = tex_2d;
167             coords[0][0] = (float)rect->left / w;
168             coords[0][1] = (float)rect->top / h;
169             coords[0][2] = 0.0f;
170
171             coords[1][0] = (float)rect->right / w;
172             coords[1][1] = (float)rect->top / h;
173             coords[1][2] = 0.0f;
174
175             coords[2][0] = (float)rect->left / w;
176             coords[2][1] = (float)rect->bottom / h;
177             coords[2][2] = 0.0f;
178
179             coords[3][0] = (float)rect->right / w;
180             coords[3][1] = (float)rect->bottom / h;
181             coords[3][2] = 0.0f;
182             break;
183
184         case GL_TEXTURE_RECTANGLE_ARB:
185             info->binding = GL_TEXTURE_BINDING_RECTANGLE_ARB;
186             info->bind_target = GL_TEXTURE_RECTANGLE_ARB;
187             info->tex_type = tex_rect;
188             coords[0][0] = rect->left;  coords[0][1] = rect->top;       coords[0][2] = 0.0f;
189             coords[1][0] = rect->right; coords[1][1] = rect->top;       coords[1][2] = 0.0f;
190             coords[2][0] = rect->left;  coords[2][1] = rect->bottom;    coords[2][2] = 0.0f;
191             coords[3][0] = rect->right; coords[3][1] = rect->bottom;    coords[3][2] = 0.0f;
192             break;
193
194         case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
195             info->binding = GL_TEXTURE_BINDING_CUBE_MAP_ARB;
196             info->bind_target = GL_TEXTURE_CUBE_MAP_ARB;
197             info->tex_type = tex_cube;
198             cube_coords_float(rect, w, h, &f);
199
200             coords[0][0] =  1.0f;   coords[0][1] = -f.t;   coords[0][2] = -f.l;
201             coords[1][0] =  1.0f;   coords[1][1] = -f.t;   coords[1][2] = -f.r;
202             coords[2][0] =  1.0f;   coords[2][1] = -f.b;   coords[2][2] = -f.l;
203             coords[3][0] =  1.0f;   coords[3][1] = -f.b;   coords[3][2] = -f.r;
204             break;
205
206         case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
207             info->binding = GL_TEXTURE_BINDING_CUBE_MAP_ARB;
208             info->bind_target = GL_TEXTURE_CUBE_MAP_ARB;
209             info->tex_type = tex_cube;
210             cube_coords_float(rect, w, h, &f);
211
212             coords[0][0] = -1.0f;   coords[0][1] = -f.t;   coords[0][2] = f.l;
213             coords[1][0] = -1.0f;   coords[1][1] = -f.t;   coords[1][2] = f.r;
214             coords[2][0] = -1.0f;   coords[2][1] = -f.b;   coords[2][2] = f.l;
215             coords[3][0] = -1.0f;   coords[3][1] = -f.b;   coords[3][2] = f.r;
216             break;
217
218         case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
219             info->binding = GL_TEXTURE_BINDING_CUBE_MAP_ARB;
220             info->bind_target = GL_TEXTURE_CUBE_MAP_ARB;
221             info->tex_type = tex_cube;
222             cube_coords_float(rect, w, h, &f);
223
224             coords[0][0] = f.l;   coords[0][1] =  1.0f;   coords[0][2] = f.t;
225             coords[1][0] = f.r;   coords[1][1] =  1.0f;   coords[1][2] = f.t;
226             coords[2][0] = f.l;   coords[2][1] =  1.0f;   coords[2][2] = f.b;
227             coords[3][0] = f.r;   coords[3][1] =  1.0f;   coords[3][2] = f.b;
228             break;
229
230         case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
231             info->binding = GL_TEXTURE_BINDING_CUBE_MAP_ARB;
232             info->bind_target = GL_TEXTURE_CUBE_MAP_ARB;
233             info->tex_type = tex_cube;
234             cube_coords_float(rect, w, h, &f);
235
236             coords[0][0] = f.l;   coords[0][1] = -1.0f;   coords[0][2] = -f.t;
237             coords[1][0] = f.r;   coords[1][1] = -1.0f;   coords[1][2] = -f.t;
238             coords[2][0] = f.l;   coords[2][1] = -1.0f;   coords[2][2] = -f.b;
239             coords[3][0] = f.r;   coords[3][1] = -1.0f;   coords[3][2] = -f.b;
240             break;
241
242         case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
243             info->binding = GL_TEXTURE_BINDING_CUBE_MAP_ARB;
244             info->bind_target = GL_TEXTURE_CUBE_MAP_ARB;
245             info->tex_type = tex_cube;
246             cube_coords_float(rect, w, h, &f);
247
248             coords[0][0] = f.l;   coords[0][1] = -f.t;   coords[0][2] =  1.0f;
249             coords[1][0] = f.r;   coords[1][1] = -f.t;   coords[1][2] =  1.0f;
250             coords[2][0] = f.l;   coords[2][1] = -f.b;   coords[2][2] =  1.0f;
251             coords[3][0] = f.r;   coords[3][1] = -f.b;   coords[3][2] =  1.0f;
252             break;
253
254         case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
255             info->binding = GL_TEXTURE_BINDING_CUBE_MAP_ARB;
256             info->bind_target = GL_TEXTURE_CUBE_MAP_ARB;
257             info->tex_type = tex_cube;
258             cube_coords_float(rect, w, h, &f);
259
260             coords[0][0] = -f.l;   coords[0][1] = -f.t;   coords[0][2] = -1.0f;
261             coords[1][0] = -f.r;   coords[1][1] = -f.t;   coords[1][2] = -1.0f;
262             coords[2][0] = -f.l;   coords[2][1] = -f.b;   coords[2][2] = -1.0f;
263             coords[3][0] = -f.r;   coords[3][1] = -f.b;   coords[3][2] = -1.0f;
264             break;
265     }
266 }
267
268 static inline void surface_get_rect(IWineD3DSurfaceImpl *This, const RECT *rect_in, RECT *rect_out)
269 {
270     if (rect_in)
271         *rect_out = *rect_in;
272     else
273     {
274         rect_out->left = 0;
275         rect_out->top = 0;
276         rect_out->right = This->resource.width;
277         rect_out->bottom = This->resource.height;
278     }
279 }
280
281 /* GL locking and context activation is done by the caller */
282 void draw_textured_quad(IWineD3DSurfaceImpl *src_surface, const RECT *src_rect, const RECT *dst_rect, WINED3DTEXTUREFILTERTYPE Filter)
283 {
284     struct blt_info info;
285
286     surface_get_blt_info(src_surface->texture_target, src_rect, src_surface->pow2Width, src_surface->pow2Height, &info);
287
288     glEnable(info.bind_target);
289     checkGLcall("glEnable(bind_target)");
290
291     /* Bind the texture */
292     glBindTexture(info.bind_target, src_surface->texture_name);
293     checkGLcall("glBindTexture");
294
295     /* Filtering for StretchRect */
296     glTexParameteri(info.bind_target, GL_TEXTURE_MAG_FILTER,
297             wined3d_gl_mag_filter(magLookup, Filter));
298     checkGLcall("glTexParameteri");
299     glTexParameteri(info.bind_target, GL_TEXTURE_MIN_FILTER,
300             wined3d_gl_min_mip_filter(minMipLookup, Filter, WINED3DTEXF_NONE));
301     checkGLcall("glTexParameteri");
302     glTexParameteri(info.bind_target, GL_TEXTURE_WRAP_S, GL_CLAMP);
303     glTexParameteri(info.bind_target, GL_TEXTURE_WRAP_T, GL_CLAMP);
304     glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
305     checkGLcall("glTexEnvi");
306
307     /* Draw a quad */
308     glBegin(GL_TRIANGLE_STRIP);
309     glTexCoord3fv(info.coords[0]);
310     glVertex2i(dst_rect->left, dst_rect->top);
311
312     glTexCoord3fv(info.coords[1]);
313     glVertex2i(dst_rect->right, dst_rect->top);
314
315     glTexCoord3fv(info.coords[2]);
316     glVertex2i(dst_rect->left, dst_rect->bottom);
317
318     glTexCoord3fv(info.coords[3]);
319     glVertex2i(dst_rect->right, dst_rect->bottom);
320     glEnd();
321
322     /* Unbind the texture */
323     glBindTexture(info.bind_target, 0);
324     checkGLcall("glBindTexture(info->bind_target, 0)");
325
326     /* We changed the filtering settings on the texture. Inform the
327      * container about this to get the filters reset properly next draw. */
328     if (src_surface->container.type == WINED3D_CONTAINER_TEXTURE)
329     {
330         struct wined3d_texture *texture = src_surface->container.u.texture;
331         texture->texture_rgb.states[WINED3DTEXSTA_MAGFILTER] = WINED3DTEXF_POINT;
332         texture->texture_rgb.states[WINED3DTEXSTA_MINFILTER] = WINED3DTEXF_POINT;
333         texture->texture_rgb.states[WINED3DTEXSTA_MIPFILTER] = WINED3DTEXF_NONE;
334     }
335 }
336
337 static void surface_realize_palette(IWineD3DSurfaceImpl *surface)
338 {
339     struct wined3d_palette *palette = surface->palette;
340
341     TRACE("surface %p.\n", surface);
342
343     if (!palette) return;
344
345     if (surface->resource.format->id == WINED3DFMT_P8_UINT
346             || surface->resource.format->id == WINED3DFMT_P8_UINT_A8_UNORM)
347     {
348         if (surface->resource.usage & WINED3DUSAGE_RENDERTARGET)
349         {
350             /* Make sure the texture is up to date. This call doesn't do
351              * anything if the texture is already up to date. */
352             surface_load_location(surface, SFLAG_INTEXTURE, NULL);
353
354             /* We want to force a palette refresh, so mark the drawable as not being up to date */
355             surface_modify_location(surface, SFLAG_INDRAWABLE, FALSE);
356         }
357         else
358         {
359             if (!(surface->flags & SFLAG_INSYSMEM))
360             {
361                 TRACE("Palette changed with surface that does not have an up to date system memory copy.\n");
362                 surface_load_location(surface, SFLAG_INSYSMEM, NULL);
363             }
364             surface_modify_location(surface, SFLAG_INSYSMEM, TRUE);
365         }
366     }
367
368     if (surface->flags & SFLAG_DIBSECTION)
369     {
370         RGBQUAD col[256];
371         unsigned int i;
372
373         TRACE("Updating the DC's palette.\n");
374
375         for (i = 0; i < 256; ++i)
376         {
377             col[i].rgbRed   = palette->palents[i].peRed;
378             col[i].rgbGreen = palette->palents[i].peGreen;
379             col[i].rgbBlue  = palette->palents[i].peBlue;
380             col[i].rgbReserved = 0;
381         }
382         SetDIBColorTable(surface->hDC, 0, 256, col);
383     }
384
385     /* Propagate the changes to the drawable when we have a palette. */
386     if (surface->resource.usage & WINED3DUSAGE_RENDERTARGET)
387         surface_load_location(surface, SFLAG_INDRAWABLE, NULL);
388 }
389
390 static HRESULT surface_draw_overlay(IWineD3DSurfaceImpl *surface)
391 {
392     HRESULT hr;
393
394     /* If there's no destination surface there is nothing to do. */
395     if (!surface->overlay_dest)
396         return WINED3D_OK;
397
398     /* Blt calls ModifyLocation on the dest surface, which in turn calls
399      * DrawOverlay to update the overlay. Prevent an endless recursion. */
400     if (surface->overlay_dest->flags & SFLAG_INOVERLAYDRAW)
401         return WINED3D_OK;
402
403     surface->overlay_dest->flags |= SFLAG_INOVERLAYDRAW;
404     hr = IWineD3DSurface_Blt((IWineD3DSurface *)surface->overlay_dest,
405             &surface->overlay_destrect, (IWineD3DSurface *)surface, &surface->overlay_srcrect,
406             WINEDDBLT_WAIT, NULL, WINED3DTEXF_LINEAR);
407     surface->overlay_dest->flags &= ~SFLAG_INOVERLAYDRAW;
408
409     return hr;
410 }
411
412 /* Context activation is done by the caller. */
413 static void surface_remove_pbo(IWineD3DSurfaceImpl *surface, const struct wined3d_gl_info *gl_info)
414 {
415     if (!surface->resource.heapMemory)
416     {
417         surface->resource.heapMemory = HeapAlloc(GetProcessHeap(), 0, surface->resource.size + RESOURCE_ALIGNMENT);
418         surface->resource.allocatedMemory = (BYTE *)(((ULONG_PTR)surface->resource.heapMemory
419                 + (RESOURCE_ALIGNMENT - 1)) & ~(RESOURCE_ALIGNMENT - 1));
420     }
421
422     ENTER_GL();
423     GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, surface->pbo));
424     checkGLcall("glBindBufferARB(GL_PIXEL_UNPACK_BUFFER, surface->pbo)");
425     GL_EXTCALL(glGetBufferSubDataARB(GL_PIXEL_UNPACK_BUFFER_ARB, 0,
426             surface->resource.size, surface->resource.allocatedMemory));
427     checkGLcall("glGetBufferSubDataARB");
428     GL_EXTCALL(glDeleteBuffersARB(1, &surface->pbo));
429     checkGLcall("glDeleteBuffersARB");
430     LEAVE_GL();
431
432     surface->pbo = 0;
433     surface->flags &= ~SFLAG_PBO;
434 }
435
436 /* Do not call while under the GL lock. */
437 static void surface_unload(struct wined3d_resource *resource)
438 {
439     IWineD3DSurfaceImpl *surface = surface_from_resource(resource);
440     IWineD3DDeviceImpl *device = resource->device;
441     const struct wined3d_gl_info *gl_info;
442     renderbuffer_entry_t *entry, *entry2;
443     struct wined3d_context *context;
444
445     TRACE("surface %p.\n", surface);
446
447     if (resource->pool == WINED3DPOOL_DEFAULT)
448     {
449         /* Default pool resources are supposed to be destroyed before Reset is called.
450          * Implicit resources stay however. So this means we have an implicit render target
451          * or depth stencil. The content may be destroyed, but we still have to tear down
452          * opengl resources, so we cannot leave early.
453          *
454          * Put the surfaces into sysmem, and reset the content. The D3D content is undefined,
455          * but we can't set the sysmem INDRAWABLE because when we're rendering the swapchain
456          * or the depth stencil into an FBO the texture or render buffer will be removed
457          * and all flags get lost
458          */
459         surface_init_sysmem(surface);
460     }
461     else
462     {
463         /* Load the surface into system memory */
464         surface_load_location(surface, SFLAG_INSYSMEM, NULL);
465         surface_modify_location(surface, SFLAG_INDRAWABLE, FALSE);
466     }
467     surface_modify_location(surface, SFLAG_INTEXTURE, FALSE);
468     surface_modify_location(surface, SFLAG_INSRGBTEX, FALSE);
469     surface->flags &= ~(SFLAG_ALLOCATED | SFLAG_SRGBALLOCATED);
470
471     context = context_acquire(device, NULL);
472     gl_info = context->gl_info;
473
474     /* Destroy PBOs, but load them into real sysmem before */
475     if (surface->flags & SFLAG_PBO)
476         surface_remove_pbo(surface, gl_info);
477
478     /* Destroy fbo render buffers. This is needed for implicit render targets, for
479      * all application-created targets the application has to release the surface
480      * before calling _Reset
481      */
482     LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, &surface->renderbuffers, renderbuffer_entry_t, entry)
483     {
484         ENTER_GL();
485         gl_info->fbo_ops.glDeleteRenderbuffers(1, &entry->id);
486         LEAVE_GL();
487         list_remove(&entry->entry);
488         HeapFree(GetProcessHeap(), 0, entry);
489     }
490     list_init(&surface->renderbuffers);
491     surface->current_renderbuffer = NULL;
492
493     /* If we're in a texture, the texture name belongs to the texture.
494      * Otherwise, destroy it. */
495     if (surface->container.type != WINED3D_CONTAINER_TEXTURE)
496     {
497         ENTER_GL();
498         glDeleteTextures(1, &surface->texture_name);
499         surface->texture_name = 0;
500         glDeleteTextures(1, &surface->texture_name_srgb);
501         surface->texture_name_srgb = 0;
502         LEAVE_GL();
503     }
504
505     context_release(context);
506
507     resource_unload(resource);
508 }
509
510 static const struct wined3d_resource_ops surface_resource_ops =
511 {
512     surface_unload,
513 };
514
515 static const struct wined3d_surface_ops surface_ops =
516 {
517     surface_realize_palette,
518     surface_draw_overlay,
519 };
520
521 static void surface_gdi_cleanup(IWineD3DSurfaceImpl *surface)
522 {
523     TRACE("surface %p.\n", surface);
524
525     if (surface->flags & SFLAG_DIBSECTION)
526     {
527         /* Release the DC. */
528         SelectObject(surface->hDC, surface->dib.holdbitmap);
529         DeleteDC(surface->hDC);
530         /* Release the DIB section. */
531         DeleteObject(surface->dib.DIBsection);
532         surface->dib.bitmap_data = NULL;
533         surface->resource.allocatedMemory = NULL;
534     }
535
536     if (surface->flags & SFLAG_USERPTR)
537         IWineD3DSurface_SetMem((IWineD3DSurface *)surface, NULL);
538     if (surface->overlay_dest)
539         list_remove(&surface->overlay_entry);
540
541     HeapFree(GetProcessHeap(), 0, surface->palette9);
542
543     resource_cleanup(&surface->resource);
544 }
545
546 static void gdi_surface_realize_palette(IWineD3DSurfaceImpl *surface)
547 {
548     struct wined3d_palette *palette = surface->palette;
549
550     TRACE("surface %p.\n", surface);
551
552     if (!palette) return;
553
554     if (surface->flags & SFLAG_DIBSECTION)
555     {
556         RGBQUAD col[256];
557         unsigned int i;
558
559         TRACE("Updating the DC's palette.\n");
560
561         for (i = 0; i < 256; ++i)
562         {
563             col[i].rgbRed = palette->palents[i].peRed;
564             col[i].rgbGreen = palette->palents[i].peGreen;
565             col[i].rgbBlue = palette->palents[i].peBlue;
566             col[i].rgbReserved = 0;
567         }
568         SetDIBColorTable(surface->hDC, 0, 256, col);
569     }
570
571     /* Update the image because of the palette change. Some games like e.g.
572      * Red Alert call SetEntries a lot to implement fading. */
573     /* Tell the swapchain to update the screen. */
574     if (surface->container.type == WINED3D_CONTAINER_SWAPCHAIN)
575     {
576         struct wined3d_swapchain *swapchain = surface->container.u.swapchain;
577         if (surface == swapchain->front_buffer)
578         {
579             x11_copy_to_screen(swapchain, NULL);
580         }
581     }
582 }
583
584 static HRESULT gdi_surface_draw_overlay(IWineD3DSurfaceImpl *surface)
585 {
586     FIXME("GDI surfaces can't draw overlays yet.\n");
587     return E_FAIL;
588 }
589
590 static const struct wined3d_surface_ops gdi_surface_ops =
591 {
592     gdi_surface_realize_palette,
593     gdi_surface_draw_overlay,
594 };
595
596 static void surface_force_reload(IWineD3DSurfaceImpl *surface)
597 {
598     surface->flags &= ~(SFLAG_ALLOCATED | SFLAG_SRGBALLOCATED);
599 }
600
601 void surface_set_texture_name(IWineD3DSurfaceImpl *surface, GLuint new_name, BOOL srgb)
602 {
603     GLuint *name;
604     DWORD flag;
605
606     TRACE("surface %p, new_name %u, srgb %#x.\n", surface, new_name, srgb);
607
608     if(srgb)
609     {
610         name = &surface->texture_name_srgb;
611         flag = SFLAG_INSRGBTEX;
612     }
613     else
614     {
615         name = &surface->texture_name;
616         flag = SFLAG_INTEXTURE;
617     }
618
619     if (!*name && new_name)
620     {
621         /* FIXME: We shouldn't need to remove SFLAG_INTEXTURE if the
622          * surface has no texture name yet. See if we can get rid of this. */
623         if (surface->flags & flag)
624             ERR("Surface has %s set, but no texture name.\n", debug_surflocation(flag));
625         surface_modify_location(surface, flag, FALSE);
626     }
627
628     *name = new_name;
629     surface_force_reload(surface);
630 }
631
632 void surface_set_texture_target(IWineD3DSurfaceImpl *surface, GLenum target)
633 {
634     TRACE("surface %p, target %#x.\n", surface, target);
635
636     if (surface->texture_target != target)
637     {
638         if (target == GL_TEXTURE_RECTANGLE_ARB)
639         {
640             surface->flags &= ~SFLAG_NORMCOORD;
641         }
642         else if (surface->texture_target == GL_TEXTURE_RECTANGLE_ARB)
643         {
644             surface->flags |= SFLAG_NORMCOORD;
645         }
646     }
647     surface->texture_target = target;
648     surface_force_reload(surface);
649 }
650
651 /* Context activation is done by the caller. */
652 void surface_bind(IWineD3DSurfaceImpl *surface, const struct wined3d_gl_info *gl_info, BOOL srgb)
653 {
654     TRACE("surface %p, gl_info %p, srgb %#x.\n", surface, gl_info, srgb);
655
656     if (surface->container.type == WINED3D_CONTAINER_TEXTURE)
657     {
658         struct wined3d_texture *texture = surface->container.u.texture;
659
660         TRACE("Passing to container (%p).\n", texture);
661         texture->texture_ops->texture_bind(texture, gl_info, srgb);
662     }
663     else
664     {
665         if (surface->texture_level)
666         {
667             ERR("Standalone surface %p is non-zero texture level %u.\n",
668                     surface, surface->texture_level);
669         }
670
671         if (srgb)
672             ERR("Trying to bind standalone surface %p as sRGB.\n", surface);
673
674         ENTER_GL();
675
676         if (!surface->texture_name)
677         {
678             glGenTextures(1, &surface->texture_name);
679             checkGLcall("glGenTextures");
680
681             TRACE("Surface %p given name %u.\n", surface, surface->texture_name);
682
683             glBindTexture(surface->texture_target, surface->texture_name);
684             checkGLcall("glBindTexture");
685             glTexParameteri(surface->texture_target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
686             glTexParameteri(surface->texture_target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
687             glTexParameteri(surface->texture_target, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
688             glTexParameteri(surface->texture_target, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
689             glTexParameteri(surface->texture_target, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
690             checkGLcall("glTexParameteri");
691         }
692         else
693         {
694             glBindTexture(surface->texture_target, surface->texture_name);
695             checkGLcall("glBindTexture");
696         }
697
698         LEAVE_GL();
699     }
700 }
701
702 /* Context activation is done by the caller. */
703 static void surface_bind_and_dirtify(IWineD3DSurfaceImpl *surface,
704         const struct wined3d_gl_info *gl_info, BOOL srgb)
705 {
706     IWineD3DDeviceImpl *device = surface->resource.device;
707     DWORD active_sampler;
708
709     /* We don't need a specific texture unit, but after binding the texture the current unit is dirty.
710      * Read the unit back instead of switching to 0, this avoids messing around with the state manager's
711      * gl states. The current texture unit should always be a valid one.
712      *
713      * To be more specific, this is tricky because we can implicitly be called
714      * from sampler() in state.c. This means we can't touch anything other than
715      * whatever happens to be the currently active texture, or we would risk
716      * marking already applied sampler states dirty again.
717      *
718      * TODO: Track the current active texture per GL context instead of using glGet
719      */
720     GLint active_texture;
721     ENTER_GL();
722     glGetIntegerv(GL_ACTIVE_TEXTURE, &active_texture);
723     LEAVE_GL();
724     active_sampler = device->rev_tex_unit_map[active_texture - GL_TEXTURE0_ARB];
725
726     if (active_sampler != WINED3D_UNMAPPED_STAGE)
727     {
728         IWineD3DDeviceImpl_MarkStateDirty(device, STATE_SAMPLER(active_sampler));
729     }
730     surface_bind(surface, gl_info, srgb);
731 }
732
733 /* This function checks if the primary render target uses the 8bit paletted format. */
734 static BOOL primary_render_target_is_p8(IWineD3DDeviceImpl *device)
735 {
736     if (device->render_targets && device->render_targets[0])
737     {
738         IWineD3DSurfaceImpl *render_target = device->render_targets[0];
739         if ((render_target->resource.usage & WINED3DUSAGE_RENDERTARGET)
740                 && (render_target->resource.format->id == WINED3DFMT_P8_UINT))
741             return TRUE;
742     }
743     return FALSE;
744 }
745
746 /* This call just downloads data, the caller is responsible for binding the
747  * correct texture. */
748 /* Context activation is done by the caller. */
749 static void surface_download_data(IWineD3DSurfaceImpl *This, const struct wined3d_gl_info *gl_info)
750 {
751     const struct wined3d_format *format = This->resource.format;
752
753     /* Only support read back of converted P8 surfaces */
754     if (This->flags & SFLAG_CONVERTED && format->id != WINED3DFMT_P8_UINT)
755     {
756         FIXME("Readback conversion not supported for format %s.\n", debug_d3dformat(format->id));
757         return;
758     }
759
760     ENTER_GL();
761
762     if (format->flags & WINED3DFMT_FLAG_COMPRESSED)
763     {
764         TRACE("(%p) : Calling glGetCompressedTexImageARB level %d, format %#x, type %#x, data %p.\n",
765                 This, This->texture_level, format->glFormat, format->glType,
766                 This->resource.allocatedMemory);
767
768         if (This->flags & SFLAG_PBO)
769         {
770             GL_EXTCALL(glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, This->pbo));
771             checkGLcall("glBindBufferARB");
772             GL_EXTCALL(glGetCompressedTexImageARB(This->texture_target, This->texture_level, NULL));
773             checkGLcall("glGetCompressedTexImageARB");
774             GL_EXTCALL(glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, 0));
775             checkGLcall("glBindBufferARB");
776         }
777         else
778         {
779             GL_EXTCALL(glGetCompressedTexImageARB(This->texture_target,
780                     This->texture_level, This->resource.allocatedMemory));
781             checkGLcall("glGetCompressedTexImageARB");
782         }
783
784         LEAVE_GL();
785     } else {
786         void *mem;
787         GLenum gl_format = format->glFormat;
788         GLenum gl_type = format->glType;
789         int src_pitch = 0;
790         int dst_pitch = 0;
791
792         /* In case of P8 the index is stored in the alpha component if the primary render target uses P8 */
793         if (format->id == WINED3DFMT_P8_UINT && primary_render_target_is_p8(This->resource.device))
794         {
795             gl_format = GL_ALPHA;
796             gl_type = GL_UNSIGNED_BYTE;
797         }
798
799         if (This->flags & SFLAG_NONPOW2)
800         {
801             unsigned char alignment = This->resource.device->surface_alignment;
802             src_pitch = format->byte_count * This->pow2Width;
803             dst_pitch = IWineD3DSurface_GetPitch((IWineD3DSurface *) This);
804             src_pitch = (src_pitch + alignment - 1) & ~(alignment - 1);
805             mem = HeapAlloc(GetProcessHeap(), 0, src_pitch * This->pow2Height);
806         } else {
807             mem = This->resource.allocatedMemory;
808         }
809
810         TRACE("(%p) : Calling glGetTexImage level %d, format %#x, type %#x, data %p\n",
811                 This, This->texture_level, gl_format, gl_type, mem);
812
813         if (This->flags & SFLAG_PBO)
814         {
815             GL_EXTCALL(glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, This->pbo));
816             checkGLcall("glBindBufferARB");
817
818             glGetTexImage(This->texture_target, This->texture_level, gl_format, gl_type, NULL);
819             checkGLcall("glGetTexImage");
820
821             GL_EXTCALL(glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, 0));
822             checkGLcall("glBindBufferARB");
823         } else {
824             glGetTexImage(This->texture_target, This->texture_level, gl_format, gl_type, mem);
825             checkGLcall("glGetTexImage");
826         }
827         LEAVE_GL();
828
829         if (This->flags & SFLAG_NONPOW2)
830         {
831             const BYTE *src_data;
832             BYTE *dst_data;
833             UINT y;
834             /*
835              * Some games (e.g. warhammer 40k) don't work properly with the odd pitches, preventing
836              * the surface pitch from being used to box non-power2 textures. Instead we have to use a hack to
837              * repack the texture so that the bpp * width pitch can be used instead of bpp * pow2width.
838              *
839              * We're doing this...
840              *
841              * instead of boxing the texture :
842              * |<-texture width ->|  -->pow2width|   /\
843              * |111111111111111111|              |   |
844              * |222 Texture 222222| boxed empty  | texture height
845              * |3333 Data 33333333|              |   |
846              * |444444444444444444|              |   \/
847              * -----------------------------------   |
848              * |     boxed  empty | boxed empty  | pow2height
849              * |                  |              |   \/
850              * -----------------------------------
851              *
852              *
853              * we're repacking the data to the expected texture width
854              *
855              * |<-texture width ->|  -->pow2width|   /\
856              * |111111111111111111222222222222222|   |
857              * |222333333333333333333444444444444| texture height
858              * |444444                           |   |
859              * |                                 |   \/
860              * |                                 |   |
861              * |            empty                | pow2height
862              * |                                 |   \/
863              * -----------------------------------
864              *
865              * == is the same as
866              *
867              * |<-texture width ->|    /\
868              * |111111111111111111|
869              * |222222222222222222|texture height
870              * |333333333333333333|
871              * |444444444444444444|    \/
872              * --------------------
873              *
874              * this also means that any references to allocatedMemory should work with the data as if were a
875              * standard texture with a non-power2 width instead of texture boxed up to be a power2 texture.
876              *
877              * internally the texture is still stored in a boxed format so any references to textureName will
878              * get a boxed texture with width pow2width and not a texture of width resource.width.
879              *
880              * Performance should not be an issue, because applications normally do not lock the surfaces when
881              * rendering. If an app does, the SFLAG_DYNLOCK flag will kick in and the memory copy won't be released,
882              * and doesn't have to be re-read.
883              */
884             src_data = mem;
885             dst_data = This->resource.allocatedMemory;
886             TRACE("(%p) : Repacking the surface data from pitch %d to pitch %d\n", This, src_pitch, dst_pitch);
887             for (y = 1; y < This->resource.height; ++y)
888             {
889                 /* skip the first row */
890                 src_data += src_pitch;
891                 dst_data += dst_pitch;
892                 memcpy(dst_data, src_data, dst_pitch);
893             }
894
895             HeapFree(GetProcessHeap(), 0, mem);
896         }
897     }
898
899     /* Surface has now been downloaded */
900     This->flags |= SFLAG_INSYSMEM;
901 }
902
903 /* This call just uploads data, the caller is responsible for binding the
904  * correct texture. */
905 /* Context activation is done by the caller. */
906 static void surface_upload_data(IWineD3DSurfaceImpl *This, const struct wined3d_gl_info *gl_info,
907         const struct wined3d_format *format, BOOL srgb, const GLvoid *data)
908 {
909     GLsizei width = This->resource.width;
910     GLsizei height = This->resource.height;
911     GLenum internal;
912
913     if (srgb)
914     {
915         internal = format->glGammaInternal;
916     }
917     else if (This->resource.usage & WINED3DUSAGE_RENDERTARGET && surface_is_offscreen(This))
918     {
919         internal = format->rtInternal;
920     }
921     else
922     {
923         internal = format->glInternal;
924     }
925
926     TRACE("This %p, internal %#x, width %d, height %d, format %#x, type %#x, data %p.\n",
927             This, internal, width, height, format->glFormat, format->glType, data);
928     TRACE("target %#x, level %u, resource size %u.\n",
929             This->texture_target, This->texture_level, This->resource.size);
930
931     if (format->heightscale != 1.0f && format->heightscale != 0.0f) height *= format->heightscale;
932
933     ENTER_GL();
934
935     if (This->flags & SFLAG_PBO)
936     {
937         GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, This->pbo));
938         checkGLcall("glBindBufferARB");
939
940         TRACE("(%p) pbo: %#x, data: %p.\n", This, This->pbo, data);
941         data = NULL;
942     }
943
944     if (format->flags & WINED3DFMT_FLAG_COMPRESSED)
945     {
946         TRACE("Calling glCompressedTexSubImage2DARB.\n");
947
948         GL_EXTCALL(glCompressedTexSubImage2DARB(This->texture_target, This->texture_level,
949                 0, 0, width, height, internal, This->resource.size, data));
950         checkGLcall("glCompressedTexSubImage2DARB");
951     }
952     else
953     {
954         TRACE("Calling glTexSubImage2D.\n");
955
956         glTexSubImage2D(This->texture_target, This->texture_level,
957                 0, 0, width, height, format->glFormat, format->glType, data);
958         checkGLcall("glTexSubImage2D");
959     }
960
961     if (This->flags & SFLAG_PBO)
962     {
963         GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, 0));
964         checkGLcall("glBindBufferARB");
965     }
966
967     LEAVE_GL();
968
969     if (gl_info->quirks & WINED3D_QUIRK_FBO_TEX_UPDATE)
970     {
971         IWineD3DDeviceImpl *device = This->resource.device;
972         unsigned int i;
973
974         for (i = 0; i < device->context_count; ++i)
975         {
976             context_surface_update(device->contexts[i], This);
977         }
978     }
979 }
980
981 /* This call just allocates the texture, the caller is responsible for binding
982  * the correct texture. */
983 /* Context activation is done by the caller. */
984 static void surface_allocate_surface(IWineD3DSurfaceImpl *This, const struct wined3d_gl_info *gl_info,
985         const struct wined3d_format *format, BOOL srgb)
986 {
987     BOOL enable_client_storage = FALSE;
988     GLsizei width = This->pow2Width;
989     GLsizei height = This->pow2Height;
990     const BYTE *mem = NULL;
991     GLenum internal;
992
993     if (srgb)
994     {
995         internal = format->glGammaInternal;
996     }
997     else if (This->resource.usage & WINED3DUSAGE_RENDERTARGET && surface_is_offscreen(This))
998     {
999         internal = format->rtInternal;
1000     }
1001     else
1002     {
1003         internal = format->glInternal;
1004     }
1005
1006     if (format->heightscale != 1.0f && format->heightscale != 0.0f) height *= format->heightscale;
1007
1008     TRACE("(%p) : Creating surface (target %#x)  level %d, d3d format %s, internal format %#x, width %d, height %d, gl format %#x, gl type=%#x\n",
1009             This, This->texture_target, This->texture_level, debug_d3dformat(format->id),
1010             internal, width, height, format->glFormat, format->glType);
1011
1012     ENTER_GL();
1013
1014     if (gl_info->supported[APPLE_CLIENT_STORAGE])
1015     {
1016         if (This->flags & (SFLAG_NONPOW2 | SFLAG_DIBSECTION | SFLAG_CONVERTED)
1017                 || !This->resource.allocatedMemory)
1018         {
1019             /* In some cases we want to disable client storage.
1020              * SFLAG_NONPOW2 has a bigger opengl texture than the client memory, and different pitches
1021              * SFLAG_DIBSECTION: Dibsections may have read / write protections on the memory. Avoid issues...
1022              * SFLAG_CONVERTED: The conversion destination memory is freed after loading the surface
1023              * allocatedMemory == NULL: Not defined in the extension. Seems to disable client storage effectively
1024              */
1025             glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_FALSE);
1026             checkGLcall("glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_FALSE)");
1027             This->flags &= ~SFLAG_CLIENT;
1028             enable_client_storage = TRUE;
1029         }
1030         else
1031         {
1032             This->flags |= SFLAG_CLIENT;
1033
1034             /* Point opengl to our allocated texture memory. Do not use resource.allocatedMemory here because
1035              * it might point into a pbo. Instead use heapMemory, but get the alignment right.
1036              */
1037             mem = (BYTE *)(((ULONG_PTR) This->resource.heapMemory + (RESOURCE_ALIGNMENT - 1)) & ~(RESOURCE_ALIGNMENT - 1));
1038         }
1039     }
1040
1041     if (format->flags & WINED3DFMT_FLAG_COMPRESSED && mem)
1042     {
1043         GL_EXTCALL(glCompressedTexImage2DARB(This->texture_target, This->texture_level,
1044                 internal, width, height, 0, This->resource.size, mem));
1045         checkGLcall("glCompressedTexImage2DARB");
1046     }
1047     else
1048     {
1049         glTexImage2D(This->texture_target, This->texture_level,
1050                 internal, width, height, 0, format->glFormat, format->glType, mem);
1051         checkGLcall("glTexImage2D");
1052     }
1053
1054     if(enable_client_storage) {
1055         glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE);
1056         checkGLcall("glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE)");
1057     }
1058     LEAVE_GL();
1059 }
1060
1061 /* In D3D the depth stencil dimensions have to be greater than or equal to the
1062  * render target dimensions. With FBOs, the dimensions have to be an exact match. */
1063 /* TODO: We should synchronize the renderbuffer's content with the texture's content. */
1064 /* GL locking is done by the caller */
1065 void surface_set_compatible_renderbuffer(IWineD3DSurfaceImpl *surface, IWineD3DSurfaceImpl *rt)
1066 {
1067     const struct wined3d_gl_info *gl_info = &surface->resource.device->adapter->gl_info;
1068     renderbuffer_entry_t *entry;
1069     GLuint renderbuffer = 0;
1070     unsigned int src_width, src_height;
1071     unsigned int width, height;
1072
1073     if (rt && rt->resource.format->id != WINED3DFMT_NULL)
1074     {
1075         width = rt->pow2Width;
1076         height = rt->pow2Height;
1077     }
1078     else
1079     {
1080         width = surface->pow2Width;
1081         height = surface->pow2Height;
1082     }
1083
1084     src_width = surface->pow2Width;
1085     src_height = surface->pow2Height;
1086
1087     /* A depth stencil smaller than the render target is not valid */
1088     if (width > src_width || height > src_height) return;
1089
1090     /* Remove any renderbuffer set if the sizes match */
1091     if (gl_info->supported[ARB_FRAMEBUFFER_OBJECT]
1092             || (width == src_width && height == src_height))
1093     {
1094         surface->current_renderbuffer = NULL;
1095         return;
1096     }
1097
1098     /* Look if we've already got a renderbuffer of the correct dimensions */
1099     LIST_FOR_EACH_ENTRY(entry, &surface->renderbuffers, renderbuffer_entry_t, entry)
1100     {
1101         if (entry->width == width && entry->height == height)
1102         {
1103             renderbuffer = entry->id;
1104             surface->current_renderbuffer = entry;
1105             break;
1106         }
1107     }
1108
1109     if (!renderbuffer)
1110     {
1111         gl_info->fbo_ops.glGenRenderbuffers(1, &renderbuffer);
1112         gl_info->fbo_ops.glBindRenderbuffer(GL_RENDERBUFFER, renderbuffer);
1113         gl_info->fbo_ops.glRenderbufferStorage(GL_RENDERBUFFER,
1114                 surface->resource.format->glInternal, width, height);
1115
1116         entry = HeapAlloc(GetProcessHeap(), 0, sizeof(renderbuffer_entry_t));
1117         entry->width = width;
1118         entry->height = height;
1119         entry->id = renderbuffer;
1120         list_add_head(&surface->renderbuffers, &entry->entry);
1121
1122         surface->current_renderbuffer = entry;
1123     }
1124
1125     checkGLcall("set_compatible_renderbuffer");
1126 }
1127
1128 GLenum surface_get_gl_buffer(IWineD3DSurfaceImpl *surface)
1129 {
1130     struct wined3d_swapchain *swapchain = surface->container.u.swapchain;
1131
1132     TRACE("surface %p.\n", surface);
1133
1134     if (surface->container.type != WINED3D_CONTAINER_SWAPCHAIN)
1135     {
1136         ERR("Surface %p is not on a swapchain.\n", surface);
1137         return GL_NONE;
1138     }
1139
1140     if (swapchain->back_buffers && swapchain->back_buffers[0] == surface)
1141     {
1142         if (swapchain->render_to_fbo)
1143         {
1144             TRACE("Returning GL_COLOR_ATTACHMENT0\n");
1145             return GL_COLOR_ATTACHMENT0;
1146         }
1147         TRACE("Returning GL_BACK\n");
1148         return GL_BACK;
1149     }
1150     else if (surface == swapchain->front_buffer)
1151     {
1152         TRACE("Returning GL_FRONT\n");
1153         return GL_FRONT;
1154     }
1155
1156     FIXME("Higher back buffer, returning GL_BACK\n");
1157     return GL_BACK;
1158 }
1159
1160 /* Slightly inefficient way to handle multiple dirty rects but it works :) */
1161 void surface_add_dirty_rect(IWineD3DSurfaceImpl *surface, const WINED3DBOX *dirty_rect)
1162 {
1163     TRACE("surface %p, dirty_rect %p.\n", surface, dirty_rect);
1164
1165     if (!(surface->flags & SFLAG_INSYSMEM) && (surface->flags & SFLAG_INTEXTURE))
1166         /* No partial locking for textures yet. */
1167         surface_load_location(surface, SFLAG_INSYSMEM, NULL);
1168
1169     surface_modify_location(surface, SFLAG_INSYSMEM, TRUE);
1170     if (dirty_rect)
1171     {
1172         surface->dirtyRect.left = min(surface->dirtyRect.left, dirty_rect->Left);
1173         surface->dirtyRect.top = min(surface->dirtyRect.top, dirty_rect->Top);
1174         surface->dirtyRect.right = max(surface->dirtyRect.right, dirty_rect->Right);
1175         surface->dirtyRect.bottom = max(surface->dirtyRect.bottom, dirty_rect->Bottom);
1176     }
1177     else
1178     {
1179         surface->dirtyRect.left = 0;
1180         surface->dirtyRect.top = 0;
1181         surface->dirtyRect.right = surface->resource.width;
1182         surface->dirtyRect.bottom = surface->resource.height;
1183     }
1184
1185     /* if the container is a texture then mark it dirty. */
1186     if (surface->container.type == WINED3D_CONTAINER_TEXTURE)
1187     {
1188         TRACE("Passing to container.\n");
1189         wined3d_texture_set_dirty(surface->container.u.texture, TRUE);
1190     }
1191 }
1192
1193 static BOOL surface_convert_color_to_float(IWineD3DSurfaceImpl *surface, DWORD color, WINED3DCOLORVALUE *float_color)
1194 {
1195     const struct wined3d_format *format = surface->resource.format;
1196     IWineD3DDeviceImpl *device = surface->resource.device;
1197
1198     switch (format->id)
1199     {
1200         case WINED3DFMT_P8_UINT:
1201             if (surface->palette)
1202             {
1203                 float_color->r = surface->palette->palents[color].peRed / 255.0f;
1204                 float_color->g = surface->palette->palents[color].peGreen / 255.0f;
1205                 float_color->b = surface->palette->palents[color].peBlue / 255.0f;
1206             }
1207             else
1208             {
1209                 float_color->r = 0.0f;
1210                 float_color->g = 0.0f;
1211                 float_color->b = 0.0f;
1212             }
1213             float_color->a = primary_render_target_is_p8(device) ? color / 255.0f : 1.0f;
1214             break;
1215
1216         case WINED3DFMT_B5G6R5_UNORM:
1217             float_color->r = ((color >> 11) & 0x1f) / 31.0f;
1218             float_color->g = ((color >> 5) & 0x3f) / 63.0f;
1219             float_color->b = (color & 0x1f) / 31.0f;
1220             float_color->a = 1.0f;
1221             break;
1222
1223         case WINED3DFMT_B8G8R8_UNORM:
1224         case WINED3DFMT_B8G8R8X8_UNORM:
1225             float_color->r = D3DCOLOR_R(color);
1226             float_color->g = D3DCOLOR_G(color);
1227             float_color->b = D3DCOLOR_B(color);
1228             float_color->a = 1.0f;
1229             break;
1230
1231         case WINED3DFMT_B8G8R8A8_UNORM:
1232             float_color->r = D3DCOLOR_R(color);
1233             float_color->g = D3DCOLOR_G(color);
1234             float_color->b = D3DCOLOR_B(color);
1235             float_color->a = D3DCOLOR_A(color);
1236             break;
1237
1238         default:
1239             ERR("Unhandled conversion from %s to floating point.\n", debug_d3dformat(format->id));
1240             return FALSE;
1241     }
1242
1243     return TRUE;
1244 }
1245
1246 static BOOL surface_convert_depth_to_float(IWineD3DSurfaceImpl *surface, DWORD depth, float *float_depth)
1247 {
1248     const struct wined3d_format *format = surface->resource.format;
1249
1250     switch (format->id)
1251     {
1252         case WINED3DFMT_S1_UINT_D15_UNORM:
1253             *float_depth = depth / (float)0x00007fff;
1254             break;
1255
1256         case WINED3DFMT_D16_UNORM:
1257             *float_depth = depth / (float)0x0000ffff;
1258             break;
1259
1260         case WINED3DFMT_D24_UNORM_S8_UINT:
1261         case WINED3DFMT_X8D24_UNORM:
1262             *float_depth = depth / (float)0x00ffffff;
1263             break;
1264
1265         case WINED3DFMT_D32_UNORM:
1266             *float_depth = depth / (float)0xffffffff;
1267             break;
1268
1269         default:
1270             ERR("Unhandled conversion from %s to floating point.\n", debug_d3dformat(format->id));
1271             return FALSE;
1272     }
1273
1274     return TRUE;
1275 }
1276
1277 static void surface_evict_sysmem(IWineD3DSurfaceImpl *surface)
1278 {
1279     if (surface->flags & SFLAG_DONOTFREE)
1280         return;
1281
1282     HeapFree(GetProcessHeap(), 0, surface->resource.heapMemory);
1283     surface->resource.allocatedMemory = NULL;
1284     surface->resource.heapMemory = NULL;
1285     surface_modify_location(surface, SFLAG_INSYSMEM, FALSE);
1286 }
1287
1288 HRESULT surface_load(IWineD3DSurfaceImpl *surface, BOOL srgb)
1289 {
1290     DWORD flag = srgb ? SFLAG_INSRGBTEX : SFLAG_INTEXTURE;
1291
1292     TRACE("surface %p, srgb %#x.\n", surface, srgb);
1293
1294     if (surface->resource.pool == WINED3DPOOL_SCRATCH)
1295     {
1296         ERR("Not supported on scratch surfaces.\n");
1297         return WINED3DERR_INVALIDCALL;
1298     }
1299
1300     if (!(surface->flags & flag))
1301     {
1302         TRACE("Reloading because surface is dirty\n");
1303     }
1304     /* Reload if either the texture and sysmem have different ideas about the
1305      * color key, or the actual key values changed. */
1306     else if (!(surface->flags & SFLAG_GLCKEY) != !(surface->CKeyFlags & WINEDDSD_CKSRCBLT)
1307             || ((surface->CKeyFlags & WINEDDSD_CKSRCBLT)
1308             && (surface->glCKey.dwColorSpaceLowValue != surface->SrcBltCKey.dwColorSpaceLowValue
1309             || surface->glCKey.dwColorSpaceHighValue != surface->SrcBltCKey.dwColorSpaceHighValue)))
1310     {
1311         TRACE("Reloading because of color keying\n");
1312         /* To perform the color key conversion we need a sysmem copy of
1313          * the surface. Make sure we have it. */
1314
1315         surface_load_location(surface, SFLAG_INSYSMEM, NULL);
1316         /* Make sure the texture is reloaded because of the color key change,
1317          * this kills performance though :( */
1318         /* TODO: This is not necessarily needed with hw palettized texture support. */
1319         surface_modify_location(surface, SFLAG_INSYSMEM, TRUE);
1320     }
1321     else
1322     {
1323         TRACE("surface is already in texture\n");
1324         return WINED3D_OK;
1325     }
1326
1327     /* No partial locking for textures yet. */
1328     surface_load_location(surface, flag, NULL);
1329     surface_evict_sysmem(surface);
1330
1331     return WINED3D_OK;
1332 }
1333
1334 /* Do not call while under the GL lock. */
1335 static ULONG WINAPI IWineD3DSurfaceImpl_Release(IWineD3DSurface *iface)
1336 {
1337     IWineD3DSurfaceImpl *surface = (IWineD3DSurfaceImpl *)iface;
1338     ULONG refcount;
1339
1340     TRACE("Surface %p, container %p of type %#x.\n",
1341             surface, surface->container.u.base, surface->container.type);
1342
1343     switch (surface->container.type)
1344     {
1345         case WINED3D_CONTAINER_TEXTURE:
1346             return wined3d_texture_decref(surface->container.u.texture);
1347
1348         case WINED3D_CONTAINER_SWAPCHAIN:
1349             return wined3d_swapchain_decref(surface->container.u.swapchain);
1350
1351         default:
1352             ERR("Unhandled container type %#x.\n", surface->container.type);
1353         case WINED3D_CONTAINER_NONE:
1354             break;
1355     }
1356
1357     refcount = InterlockedDecrement(&surface->resource.ref);
1358     TRACE("%p decreasing refcount to %u.\n", surface, refcount);
1359
1360     if (!refcount)
1361     {
1362         surface_cleanup(surface);
1363         surface->resource.parent_ops->wined3d_object_destroyed(surface->resource.parent);
1364
1365         TRACE("Destroyed surface %p.\n", surface);
1366         HeapFree(GetProcessHeap(), 0, surface);
1367     }
1368
1369     return refcount;
1370 }
1371
1372 /* ****************************************************
1373    IWineD3DSurface IWineD3DResource parts follow
1374    **************************************************** */
1375
1376 /* Do not call while under the GL lock. */
1377 void surface_internal_preload(IWineD3DSurfaceImpl *surface, enum WINED3DSRGB srgb)
1378 {
1379     IWineD3DDeviceImpl *device = surface->resource.device;
1380
1381     TRACE("iface %p, srgb %#x.\n", surface, srgb);
1382
1383     if (surface->container.type == WINED3D_CONTAINER_TEXTURE)
1384     {
1385         struct wined3d_texture *texture = surface->container.u.texture;
1386
1387         TRACE("Passing to container (%p).\n", texture);
1388         texture->texture_ops->texture_preload(texture, srgb);
1389     }
1390     else
1391     {
1392         struct wined3d_context *context = NULL;
1393
1394         TRACE("(%p) : About to load surface\n", surface);
1395
1396         if (!device->isInDraw) context = context_acquire(device, NULL);
1397
1398         if (surface->resource.format->id == WINED3DFMT_P8_UINT
1399                 || surface->resource.format->id == WINED3DFMT_P8_UINT_A8_UNORM)
1400         {
1401             if (palette9_changed(surface))
1402             {
1403                 TRACE("Reloading surface because the d3d8/9 palette was changed\n");
1404                 /* TODO: This is not necessarily needed with hw palettized texture support */
1405                 surface_load_location(surface, SFLAG_INSYSMEM, NULL);
1406                 /* Make sure the texture is reloaded because of the palette change, this kills performance though :( */
1407                 surface_modify_location(surface, SFLAG_INTEXTURE, FALSE);
1408             }
1409         }
1410
1411         surface_load(surface, srgb == SRGB_SRGB ? TRUE : FALSE);
1412
1413         if (surface->resource.pool == WINED3DPOOL_DEFAULT)
1414         {
1415             /* Tell opengl to try and keep this texture in video ram (well mostly) */
1416             GLclampf tmp;
1417             tmp = 0.9f;
1418             ENTER_GL();
1419             glPrioritizeTextures(1, &surface->texture_name, &tmp);
1420             LEAVE_GL();
1421         }
1422
1423         if (context) context_release(context);
1424     }
1425 }
1426
1427 static void WINAPI IWineD3DSurfaceImpl_PreLoad(IWineD3DSurface *iface)
1428 {
1429     surface_internal_preload((IWineD3DSurfaceImpl *)iface, SRGB_ANY);
1430 }
1431
1432 BOOL surface_init_sysmem(IWineD3DSurfaceImpl *surface)
1433 {
1434     if (!surface->resource.allocatedMemory)
1435     {
1436         surface->resource.heapMemory = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
1437                 surface->resource.size + RESOURCE_ALIGNMENT);
1438         if (!surface->resource.heapMemory)
1439         {
1440             ERR("Out of memory\n");
1441             return FALSE;
1442         }
1443         surface->resource.allocatedMemory =
1444             (BYTE *)(((ULONG_PTR)surface->resource.heapMemory + (RESOURCE_ALIGNMENT - 1)) & ~(RESOURCE_ALIGNMENT - 1));
1445     }
1446     else
1447     {
1448         memset(surface->resource.allocatedMemory, 0, surface->resource.size);
1449     }
1450
1451     surface_modify_location(surface, SFLAG_INSYSMEM, TRUE);
1452
1453     return TRUE;
1454 }
1455
1456 /* ******************************************************
1457    IWineD3DSurface IWineD3DSurface parts follow
1458    ****************************************************** */
1459
1460 /* Read the framebuffer back into the surface */
1461 static void read_from_framebuffer(IWineD3DSurfaceImpl *This, const RECT *rect, void *dest, UINT pitch)
1462 {
1463     IWineD3DDeviceImpl *device = This->resource.device;
1464     const struct wined3d_gl_info *gl_info;
1465     struct wined3d_context *context;
1466     BYTE *mem;
1467     GLint fmt;
1468     GLint type;
1469     BYTE *row, *top, *bottom;
1470     int i;
1471     BOOL bpp;
1472     RECT local_rect;
1473     BOOL srcIsUpsideDown;
1474     GLint rowLen = 0;
1475     GLint skipPix = 0;
1476     GLint skipRow = 0;
1477
1478     if(wined3d_settings.rendertargetlock_mode == RTL_DISABLE) {
1479         static BOOL warned = FALSE;
1480         if(!warned) {
1481             ERR("The application tries to lock the render target, but render target locking is disabled\n");
1482             warned = TRUE;
1483         }
1484         return;
1485     }
1486
1487     context = context_acquire(device, This);
1488     context_apply_blit_state(context, device);
1489     gl_info = context->gl_info;
1490
1491     ENTER_GL();
1492
1493     /* Select the correct read buffer, and give some debug output.
1494      * There is no need to keep track of the current read buffer or reset it, every part of the code
1495      * that reads sets the read buffer as desired.
1496      */
1497     if (surface_is_offscreen(This))
1498     {
1499         /* Mapping the primary render target which is not on a swapchain.
1500          * Read from the back buffer. */
1501         TRACE("Mapping offscreen render target.\n");
1502         glReadBuffer(device->offscreenBuffer);
1503         srcIsUpsideDown = TRUE;
1504     }
1505     else
1506     {
1507         /* Onscreen surfaces are always part of a swapchain */
1508         GLenum buffer = surface_get_gl_buffer(This);
1509         TRACE("Mapping %#x buffer.\n", buffer);
1510         glReadBuffer(buffer);
1511         checkGLcall("glReadBuffer");
1512         srcIsUpsideDown = FALSE;
1513     }
1514
1515     /* TODO: Get rid of the extra rectangle comparison and construction of a full surface rectangle */
1516     if(!rect) {
1517         local_rect.left = 0;
1518         local_rect.top = 0;
1519         local_rect.right = This->resource.width;
1520         local_rect.bottom = This->resource.height;
1521     }
1522     else
1523     {
1524         local_rect = *rect;
1525     }
1526     /* TODO: Get rid of the extra GetPitch call, LockRect does that too. Cache the pitch */
1527
1528     switch (This->resource.format->id)
1529     {
1530         case WINED3DFMT_P8_UINT:
1531         {
1532             if (primary_render_target_is_p8(device))
1533             {
1534                 /* In case of P8 render targets the index is stored in the alpha component */
1535                 fmt = GL_ALPHA;
1536                 type = GL_UNSIGNED_BYTE;
1537                 mem = dest;
1538                 bpp = This->resource.format->byte_count;
1539             } else {
1540                 /* GL can't return palettized data, so read ARGB pixels into a
1541                  * separate block of memory and convert them into palettized format
1542                  * in software. Slow, but if the app means to use palettized render
1543                  * targets and locks it...
1544                  *
1545                  * Use GL_RGB, GL_UNSIGNED_BYTE to read the surface for performance reasons
1546                  * Don't use GL_BGR as in the WINED3DFMT_R8G8B8 case, instead watch out
1547                  * for the color channels when palettizing the colors.
1548                  */
1549                 fmt = GL_RGB;
1550                 type = GL_UNSIGNED_BYTE;
1551                 pitch *= 3;
1552                 mem = HeapAlloc(GetProcessHeap(), 0, This->resource.size * 3);
1553                 if(!mem) {
1554                     ERR("Out of memory\n");
1555                     LEAVE_GL();
1556                     return;
1557                 }
1558                 bpp = This->resource.format->byte_count * 3;
1559             }
1560         }
1561         break;
1562
1563         default:
1564             mem = dest;
1565             fmt = This->resource.format->glFormat;
1566             type = This->resource.format->glType;
1567             bpp = This->resource.format->byte_count;
1568     }
1569
1570     if (This->flags & SFLAG_PBO)
1571     {
1572         GL_EXTCALL(glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, This->pbo));
1573         checkGLcall("glBindBufferARB");
1574         if (mem)
1575         {
1576             ERR("mem not null for pbo -- unexpected\n");
1577             mem = NULL;
1578         }
1579     }
1580
1581     /* Save old pixel store pack state */
1582     glGetIntegerv(GL_PACK_ROW_LENGTH, &rowLen);
1583     checkGLcall("glGetIntegerv");
1584     glGetIntegerv(GL_PACK_SKIP_PIXELS, &skipPix);
1585     checkGLcall("glGetIntegerv");
1586     glGetIntegerv(GL_PACK_SKIP_ROWS, &skipRow);
1587     checkGLcall("glGetIntegerv");
1588
1589     /* Setup pixel store pack state -- to glReadPixels into the correct place */
1590     glPixelStorei(GL_PACK_ROW_LENGTH, This->resource.width);
1591     checkGLcall("glPixelStorei");
1592     glPixelStorei(GL_PACK_SKIP_PIXELS, local_rect.left);
1593     checkGLcall("glPixelStorei");
1594     glPixelStorei(GL_PACK_SKIP_ROWS, local_rect.top);
1595     checkGLcall("glPixelStorei");
1596
1597     glReadPixels(local_rect.left, !srcIsUpsideDown ? (This->resource.height - local_rect.bottom) : local_rect.top,
1598                  local_rect.right - local_rect.left,
1599                  local_rect.bottom - local_rect.top,
1600                  fmt, type, mem);
1601     checkGLcall("glReadPixels");
1602
1603     /* Reset previous pixel store pack state */
1604     glPixelStorei(GL_PACK_ROW_LENGTH, rowLen);
1605     checkGLcall("glPixelStorei");
1606     glPixelStorei(GL_PACK_SKIP_PIXELS, skipPix);
1607     checkGLcall("glPixelStorei");
1608     glPixelStorei(GL_PACK_SKIP_ROWS, skipRow);
1609     checkGLcall("glPixelStorei");
1610
1611     if (This->flags & SFLAG_PBO)
1612     {
1613         GL_EXTCALL(glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, 0));
1614         checkGLcall("glBindBufferARB");
1615
1616         /* Check if we need to flip the image. If we need to flip use glMapBufferARB
1617          * to get a pointer to it and perform the flipping in software. This is a lot
1618          * faster than calling glReadPixels for each line. In case we want more speed
1619          * we should rerender it flipped in a FBO and read the data back from the FBO. */
1620         if(!srcIsUpsideDown) {
1621             GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, This->pbo));
1622             checkGLcall("glBindBufferARB");
1623
1624             mem = GL_EXTCALL(glMapBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, GL_READ_WRITE_ARB));
1625             checkGLcall("glMapBufferARB");
1626         }
1627     }
1628
1629     /* TODO: Merge this with the palettization loop below for P8 targets */
1630     if(!srcIsUpsideDown) {
1631         UINT len, off;
1632         /* glReadPixels returns the image upside down, and there is no way to prevent this.
1633             Flip the lines in software */
1634         len = (local_rect.right - local_rect.left) * bpp;
1635         off = local_rect.left * bpp;
1636
1637         row = HeapAlloc(GetProcessHeap(), 0, len);
1638         if(!row) {
1639             ERR("Out of memory\n");
1640             if (This->resource.format->id == WINED3DFMT_P8_UINT) HeapFree(GetProcessHeap(), 0, mem);
1641             LEAVE_GL();
1642             return;
1643         }
1644
1645         top = mem + pitch * local_rect.top;
1646         bottom = mem + pitch * (local_rect.bottom - 1);
1647         for(i = 0; i < (local_rect.bottom - local_rect.top) / 2; i++) {
1648             memcpy(row, top + off, len);
1649             memcpy(top + off, bottom + off, len);
1650             memcpy(bottom + off, row, len);
1651             top += pitch;
1652             bottom -= pitch;
1653         }
1654         HeapFree(GetProcessHeap(), 0, row);
1655
1656         /* Unmap the temp PBO buffer */
1657         if (This->flags & SFLAG_PBO)
1658         {
1659             GL_EXTCALL(glUnmapBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB));
1660             GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, 0));
1661         }
1662     }
1663
1664     LEAVE_GL();
1665     context_release(context);
1666
1667     /* For P8 textures we need to perform an inverse palette lookup. This is done by searching for a palette
1668      * index which matches the RGB value. Note this isn't guaranteed to work when there are multiple entries for
1669      * the same color but we have no choice.
1670      * In case of P8 render targets, the index is stored in the alpha component so no conversion is needed.
1671      */
1672     if (This->resource.format->id == WINED3DFMT_P8_UINT && !primary_render_target_is_p8(device))
1673     {
1674         const PALETTEENTRY *pal = NULL;
1675         DWORD width = pitch / 3;
1676         int x, y, c;
1677
1678         if(This->palette) {
1679             pal = This->palette->palents;
1680         } else {
1681             ERR("Palette is missing, cannot perform inverse palette lookup\n");
1682             HeapFree(GetProcessHeap(), 0, mem);
1683             return ;
1684         }
1685
1686         for(y = local_rect.top; y < local_rect.bottom; y++) {
1687             for(x = local_rect.left; x < local_rect.right; x++) {
1688                 /*                      start              lines            pixels      */
1689                 const BYTE *blue = mem + y * pitch + x * (sizeof(BYTE) * 3);
1690                 const BYTE *green = blue  + 1;
1691                 const BYTE *red = green + 1;
1692
1693                 for(c = 0; c < 256; c++) {
1694                     if(*red   == pal[c].peRed   &&
1695                        *green == pal[c].peGreen &&
1696                        *blue  == pal[c].peBlue)
1697                     {
1698                         *((BYTE *) dest + y * width + x) = c;
1699                         break;
1700                     }
1701                 }
1702             }
1703         }
1704         HeapFree(GetProcessHeap(), 0, mem);
1705     }
1706 }
1707
1708 /* Read the framebuffer contents into a texture */
1709 static void read_from_framebuffer_texture(IWineD3DSurfaceImpl *This, BOOL srgb)
1710 {
1711     IWineD3DDeviceImpl *device = This->resource.device;
1712     const struct wined3d_gl_info *gl_info;
1713     struct wined3d_context *context;
1714
1715     if (!surface_is_offscreen(This))
1716     {
1717         /* We would need to flip onscreen surfaces, but there's no efficient
1718          * way to do that here. It makes more sense for the caller to
1719          * explicitly go through sysmem. */
1720         ERR("Not supported for onscreen targets.\n");
1721         return;
1722     }
1723
1724     /* Activate the surface to read from. In some situations it isn't the currently active target(e.g. backbuffer
1725      * locking during offscreen rendering). RESOURCELOAD is ok because glCopyTexSubImage2D isn't affected by any
1726      * states in the stateblock, and no driver was found yet that had bugs in that regard.
1727      */
1728     context = context_acquire(device, This);
1729     gl_info = context->gl_info;
1730
1731     surface_prepare_texture(This, gl_info, srgb);
1732     surface_bind_and_dirtify(This, gl_info, srgb);
1733
1734     TRACE("Reading back offscreen render target %p.\n", This);
1735
1736     ENTER_GL();
1737
1738     glReadBuffer(device->offscreenBuffer);
1739     checkGLcall("glReadBuffer");
1740
1741     glCopyTexSubImage2D(This->texture_target, This->texture_level,
1742             0, 0, 0, 0, This->resource.width, This->resource.height);
1743     checkGLcall("glCopyTexSubImage2D");
1744
1745     LEAVE_GL();
1746
1747     context_release(context);
1748 }
1749
1750 /* Context activation is done by the caller. */
1751 static void surface_prepare_texture_internal(IWineD3DSurfaceImpl *surface,
1752         const struct wined3d_gl_info *gl_info, BOOL srgb)
1753 {
1754     DWORD alloc_flag = srgb ? SFLAG_SRGBALLOCATED : SFLAG_ALLOCATED;
1755     CONVERT_TYPES convert;
1756     struct wined3d_format format;
1757
1758     if (surface->flags & alloc_flag) return;
1759
1760     d3dfmt_get_conv(surface, TRUE, TRUE, &format, &convert);
1761     if (convert != NO_CONVERSION || format.convert) surface->flags |= SFLAG_CONVERTED;
1762     else surface->flags &= ~SFLAG_CONVERTED;
1763
1764     surface_bind_and_dirtify(surface, gl_info, srgb);
1765     surface_allocate_surface(surface, gl_info, &format, srgb);
1766     surface->flags |= alloc_flag;
1767 }
1768
1769 /* Context activation is done by the caller. */
1770 void surface_prepare_texture(IWineD3DSurfaceImpl *surface, const struct wined3d_gl_info *gl_info, BOOL srgb)
1771 {
1772     if (surface->container.type == WINED3D_CONTAINER_TEXTURE)
1773     {
1774         struct wined3d_texture *texture = surface->container.u.texture;
1775         UINT sub_count = texture->level_count * texture->layer_count;
1776         UINT i;
1777
1778         TRACE("surface %p is a subresource of texture %p.\n", surface, texture);
1779
1780         for (i = 0; i < sub_count; ++i)
1781         {
1782             IWineD3DSurfaceImpl *s = surface_from_resource(texture->sub_resources[i]);
1783             surface_prepare_texture_internal(s, gl_info, srgb);
1784         }
1785
1786         return;
1787     }
1788
1789     surface_prepare_texture_internal(surface, gl_info, srgb);
1790 }
1791
1792 static void surface_prepare_system_memory(IWineD3DSurfaceImpl *This)
1793 {
1794     IWineD3DDeviceImpl *device = This->resource.device;
1795     const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
1796
1797     /* Performance optimization: Count how often a surface is locked, if it is locked regularly do not throw away the system memory copy.
1798      * This avoids the need to download the surface from opengl all the time. The surface is still downloaded if the opengl texture is
1799      * changed
1800      */
1801     if (!(This->flags & SFLAG_DYNLOCK))
1802     {
1803         This->lockCount++;
1804         /* MAXLOCKCOUNT is defined in wined3d_private.h */
1805         if(This->lockCount > MAXLOCKCOUNT) {
1806             TRACE("Surface is locked regularly, not freeing the system memory copy any more\n");
1807             This->flags |= SFLAG_DYNLOCK;
1808         }
1809     }
1810
1811     /* Create a PBO for dynamically locked surfaces but don't do it for converted or non-pow2 surfaces.
1812      * Also don't create a PBO for systemmem surfaces.
1813      */
1814     if (gl_info->supported[ARB_PIXEL_BUFFER_OBJECT] && (This->flags & SFLAG_DYNLOCK)
1815             && !(This->flags & (SFLAG_PBO | SFLAG_CONVERTED | SFLAG_NONPOW2))
1816             && (This->resource.pool != WINED3DPOOL_SYSTEMMEM))
1817     {
1818         GLenum error;
1819         struct wined3d_context *context;
1820
1821         context = context_acquire(device, NULL);
1822         ENTER_GL();
1823
1824         GL_EXTCALL(glGenBuffersARB(1, &This->pbo));
1825         error = glGetError();
1826         if (!This->pbo || error != GL_NO_ERROR)
1827             ERR("Failed to bind the PBO with error %s (%#x)\n", debug_glerror(error), error);
1828
1829         TRACE("Attaching pbo=%#x to (%p)\n", This->pbo, This);
1830
1831         GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, This->pbo));
1832         checkGLcall("glBindBufferARB");
1833
1834         GL_EXTCALL(glBufferDataARB(GL_PIXEL_UNPACK_BUFFER_ARB, This->resource.size + 4, This->resource.allocatedMemory, GL_STREAM_DRAW_ARB));
1835         checkGLcall("glBufferDataARB");
1836
1837         GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, 0));
1838         checkGLcall("glBindBufferARB");
1839
1840         /* We don't need the system memory anymore and we can't even use it for PBOs */
1841         if (!(This->flags & SFLAG_CLIENT))
1842         {
1843             HeapFree(GetProcessHeap(), 0, This->resource.heapMemory);
1844             This->resource.heapMemory = NULL;
1845         }
1846         This->resource.allocatedMemory = NULL;
1847         This->flags |= SFLAG_PBO;
1848         LEAVE_GL();
1849         context_release(context);
1850     }
1851     else if (!(This->resource.allocatedMemory || This->flags & SFLAG_PBO))
1852     {
1853         /* Whatever surface we have, make sure that there is memory allocated for the downloaded copy,
1854          * or a pbo to map
1855          */
1856         if(!This->resource.heapMemory) {
1857             This->resource.heapMemory = HeapAlloc(GetProcessHeap() ,0 , This->resource.size + RESOURCE_ALIGNMENT);
1858         }
1859         This->resource.allocatedMemory =
1860                 (BYTE *)(((ULONG_PTR) This->resource.heapMemory + (RESOURCE_ALIGNMENT - 1)) & ~(RESOURCE_ALIGNMENT - 1));
1861         if (This->flags & SFLAG_INSYSMEM)
1862         {
1863             ERR("Surface without memory or pbo has SFLAG_INSYSMEM set!\n");
1864         }
1865     }
1866 }
1867
1868 static HRESULT WINAPI IWineD3DSurfaceImpl_Map(IWineD3DSurface *iface,
1869         WINED3DLOCKED_RECT *pLockedRect, const RECT *pRect, DWORD flags)
1870 {
1871     IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
1872     IWineD3DDeviceImpl *device = This->resource.device;
1873     const RECT *pass_rect = pRect;
1874
1875     TRACE("iface %p, locked_rect %p, rect %s, flags %#x.\n",
1876             iface, pLockedRect, wine_dbgstr_rect(pRect), flags);
1877
1878     /* This is also done in the base class, but we have to verify this before loading any data from
1879      * gl into the sysmem copy. The PBO may be mapped, a different rectangle locked, the discard flag
1880      * may interfere, and all other bad things may happen
1881      */
1882     if (This->flags & SFLAG_LOCKED)
1883     {
1884         WARN("Surface is already locked, returning D3DERR_INVALIDCALL\n");
1885         return WINED3DERR_INVALIDCALL;
1886     }
1887     This->flags |= SFLAG_LOCKED;
1888
1889     if (!(This->flags & SFLAG_LOCKABLE))
1890     {
1891         TRACE("Warning: trying to lock unlockable surf@%p\n", This);
1892     }
1893
1894     if (flags & WINED3DLOCK_DISCARD)
1895     {
1896         TRACE("WINED3DLOCK_DISCARD flag passed, marking SYSMEM as up to date.\n");
1897         surface_prepare_system_memory(This);
1898         surface_modify_location(This, SFLAG_INSYSMEM, TRUE);
1899         goto lock_end;
1900     }
1901
1902     /* surface_load_location() does not check if the rectangle specifies
1903      * the full surface. Most callers don't need that, so do it here. */
1904     if (pRect && !pRect->top && !pRect->left
1905             && pRect->right == This->resource.width
1906             && pRect->bottom == This->resource.height)
1907     {
1908         pass_rect = NULL;
1909     }
1910
1911     if (!(wined3d_settings.rendertargetlock_mode == RTL_DISABLE
1912             && ((This->container.type == WINED3D_CONTAINER_SWAPCHAIN) || This == device->render_targets[0])))
1913     {
1914         surface_load_location(This, SFLAG_INSYSMEM, pass_rect);
1915     }
1916
1917 lock_end:
1918     if (This->flags & SFLAG_PBO)
1919     {
1920         const struct wined3d_gl_info *gl_info;
1921         struct wined3d_context *context;
1922
1923         context = context_acquire(device, NULL);
1924         gl_info = context->gl_info;
1925
1926         ENTER_GL();
1927         GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, This->pbo));
1928         checkGLcall("glBindBufferARB");
1929
1930         /* This shouldn't happen but could occur if some other function didn't handle the PBO properly */
1931         if(This->resource.allocatedMemory) {
1932             ERR("The surface already has PBO memory allocated!\n");
1933         }
1934
1935         This->resource.allocatedMemory = GL_EXTCALL(glMapBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, GL_READ_WRITE_ARB));
1936         checkGLcall("glMapBufferARB");
1937
1938         /* Make sure the pbo isn't set anymore in order not to break non-pbo calls */
1939         GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, 0));
1940         checkGLcall("glBindBufferARB");
1941
1942         LEAVE_GL();
1943         context_release(context);
1944     }
1945
1946     if (!(flags & (WINED3DLOCK_NO_DIRTY_UPDATE | WINED3DLOCK_READONLY)))
1947     {
1948         if (!pRect)
1949             surface_add_dirty_rect(This, NULL);
1950         else
1951         {
1952             WINED3DBOX b;
1953
1954             b.Left = pRect->left;
1955             b.Top = pRect->top;
1956             b.Right = pRect->right;
1957             b.Bottom = pRect->bottom;
1958             b.Front = 0;
1959             b.Back = 1;
1960             surface_add_dirty_rect(This, &b);
1961         }
1962     }
1963
1964     return IWineD3DBaseSurfaceImpl_Map(iface, pLockedRect, pRect, flags);
1965 }
1966
1967 static void flush_to_framebuffer_drawpixels(IWineD3DSurfaceImpl *surface,
1968         const RECT *rect, GLenum fmt, GLenum type, UINT bpp, const BYTE *mem)
1969 {
1970     UINT pitch = IWineD3DSurface_GetPitch((IWineD3DSurface *)surface);    /* target is argb, 4 byte */
1971     IWineD3DDeviceImpl *device = surface->resource.device;
1972     const struct wined3d_gl_info *gl_info;
1973     struct wined3d_context *context;
1974     RECT local_rect;
1975     UINT w, h;
1976
1977     surface_get_rect(surface, rect, &local_rect);
1978
1979     mem += local_rect.top * pitch + local_rect.left * bpp;
1980     w = local_rect.right - local_rect.left;
1981     h = local_rect.bottom - local_rect.top;
1982
1983     /* Activate the correct context for the render target */
1984     context = context_acquire(device, surface);
1985     context_apply_blit_state(context, device);
1986     gl_info = context->gl_info;
1987
1988     ENTER_GL();
1989
1990     if (!surface_is_offscreen(surface))
1991     {
1992         GLenum buffer = surface_get_gl_buffer(surface);
1993         TRACE("Unlocking %#x buffer.\n", buffer);
1994         context_set_draw_buffer(context, buffer);
1995
1996         surface_translate_drawable_coords(surface, context->win_handle, &local_rect);
1997         glPixelZoom(1.0f, -1.0f);
1998     }
1999     else
2000     {
2001         /* Primary offscreen render target */
2002         TRACE("Offscreen render target.\n");
2003         context_set_draw_buffer(context, device->offscreenBuffer);
2004
2005         glPixelZoom(1.0f, 1.0f);
2006     }
2007
2008     glRasterPos3i(local_rect.left, local_rect.top, 1);
2009     checkGLcall("glRasterPos3i");
2010
2011     /* If not fullscreen, we need to skip a number of bytes to find the next row of data */
2012     glPixelStorei(GL_UNPACK_ROW_LENGTH, surface->resource.width);
2013
2014     if (surface->flags & SFLAG_PBO)
2015     {
2016         GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, surface->pbo));
2017         checkGLcall("glBindBufferARB");
2018     }
2019
2020     glDrawPixels(w, h, fmt, type, mem);
2021     checkGLcall("glDrawPixels");
2022
2023     if (surface->flags & SFLAG_PBO)
2024     {
2025         GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, 0));
2026         checkGLcall("glBindBufferARB");
2027     }
2028
2029     glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
2030     checkGLcall("glPixelStorei(GL_UNPACK_ROW_LENGTH, 0)");
2031
2032     LEAVE_GL();
2033
2034     if (wined3d_settings.strict_draw_ordering
2035             || (surface->container.type == WINED3D_CONTAINER_SWAPCHAIN
2036             && surface->container.u.swapchain->front_buffer == surface))
2037         wglFlush();
2038
2039     context_release(context);
2040 }
2041
2042 static HRESULT WINAPI IWineD3DSurfaceImpl_Unmap(IWineD3DSurface *iface)
2043 {
2044     IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
2045     IWineD3DDeviceImpl *device = This->resource.device;
2046     BOOL fullsurface;
2047
2048     if (!(This->flags & SFLAG_LOCKED))
2049     {
2050         WARN("trying to Unlock an unlocked surf@%p\n", This);
2051         return WINEDDERR_NOTLOCKED;
2052     }
2053
2054     This->flags &= ~SFLAG_LOCKED;
2055     memset(&This->lockedRect, 0, sizeof(This->lockedRect));
2056
2057     if (This->flags & SFLAG_PBO)
2058     {
2059         const struct wined3d_gl_info *gl_info;
2060         struct wined3d_context *context;
2061
2062         TRACE("Freeing PBO memory\n");
2063
2064         context = context_acquire(device, NULL);
2065         gl_info = context->gl_info;
2066
2067         ENTER_GL();
2068         GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, This->pbo));
2069         GL_EXTCALL(glUnmapBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB));
2070         GL_EXTCALL(glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, 0));
2071         checkGLcall("glUnmapBufferARB");
2072         LEAVE_GL();
2073         context_release(context);
2074
2075         This->resource.allocatedMemory = NULL;
2076     }
2077
2078     TRACE("(%p) : dirtyfied(%d)\n", This, This->flags & (SFLAG_INDRAWABLE | SFLAG_INTEXTURE) ? 0 : 1);
2079
2080     if (This->flags & (SFLAG_INDRAWABLE | SFLAG_INTEXTURE))
2081     {
2082         TRACE("(%p) : Not Dirtified so nothing to do, return now\n", This);
2083         goto unlock_end;
2084     }
2085
2086     if (This->container.type == WINED3D_CONTAINER_SWAPCHAIN
2087             || (device->render_targets && This == device->render_targets[0]))
2088     {
2089         if(wined3d_settings.rendertargetlock_mode == RTL_DISABLE) {
2090             static BOOL warned = FALSE;
2091             if(!warned) {
2092                 ERR("The application tries to write to the render target, but render target locking is disabled\n");
2093                 warned = TRUE;
2094             }
2095             goto unlock_end;
2096         }
2097
2098         if (!This->dirtyRect.left && !This->dirtyRect.top
2099                 && This->dirtyRect.right == This->resource.width
2100                 && This->dirtyRect.bottom == This->resource.height)
2101         {
2102             fullsurface = TRUE;
2103         } else {
2104             /* TODO: Proper partial rectangle tracking */
2105             fullsurface = FALSE;
2106             This->flags |= SFLAG_INSYSMEM;
2107         }
2108
2109         surface_load_location(This, SFLAG_INDRAWABLE, fullsurface ? NULL : &This->dirtyRect);
2110
2111         /* Partial rectangle tracking is not commonly implemented, it is only
2112          * done for render targets. INSYSMEM was set before to tell
2113          * surface_load_location() where to read the rectangle from.
2114          * Indrawable is set because all modifications from the partial
2115          * sysmem copy are written back to the drawable, thus the surface is
2116          * merged again in the drawable. The sysmem copy is not fully up to
2117          * date because only a subrectangle was read in Map(). */
2118         if (!fullsurface)
2119         {
2120             surface_modify_location(This, SFLAG_INDRAWABLE, TRUE);
2121             surface_evict_sysmem(This);
2122         }
2123
2124         This->dirtyRect.left   = This->resource.width;
2125         This->dirtyRect.top    = This->resource.height;
2126         This->dirtyRect.right  = 0;
2127         This->dirtyRect.bottom = 0;
2128     }
2129     else if (This->resource.format->flags & (WINED3DFMT_FLAG_DEPTH | WINED3DFMT_FLAG_STENCIL))
2130     {
2131         FIXME("Depth Stencil buffer locking is not implemented\n");
2132     }
2133
2134 unlock_end:
2135     /* Overlays have to be redrawn manually after changes with the GL implementation */
2136     if (This->overlay_dest)
2137         This->surface_ops->surface_draw_overlay(This);
2138
2139     return WINED3D_OK;
2140 }
2141
2142 static void surface_release_client_storage(IWineD3DSurfaceImpl *surface)
2143 {
2144     struct wined3d_context *context;
2145
2146     context = context_acquire(surface->resource.device, NULL);
2147
2148     ENTER_GL();
2149     glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_FALSE);
2150     if (surface->texture_name)
2151     {
2152         surface_bind_and_dirtify(surface, context->gl_info, FALSE);
2153         glTexImage2D(surface->texture_target, surface->texture_level,
2154                 GL_RGB, 1, 1, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL);
2155     }
2156     if (surface->texture_name_srgb)
2157     {
2158         surface_bind_and_dirtify(surface, context->gl_info, TRUE);
2159         glTexImage2D(surface->texture_target, surface->texture_level,
2160                 GL_RGB, 1, 1, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL);
2161     }
2162     glPixelStorei(GL_UNPACK_CLIENT_STORAGE_APPLE, GL_TRUE);
2163
2164     LEAVE_GL();
2165     context_release(context);
2166
2167     surface_modify_location(surface, SFLAG_INSRGBTEX, FALSE);
2168     surface_modify_location(surface, SFLAG_INTEXTURE, FALSE);
2169     surface_force_reload(surface);
2170 }
2171
2172 static HRESULT WINAPI IWineD3DSurfaceImpl_GetDC(IWineD3DSurface *iface, HDC *pHDC)
2173 {
2174     IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
2175     WINED3DLOCKED_RECT lock;
2176     HRESULT hr;
2177     RGBQUAD col[256];
2178
2179     TRACE("(%p)->(%p)\n",This,pHDC);
2180
2181     if (This->flags & SFLAG_USERPTR)
2182     {
2183         ERR("Not supported on surfaces with an application-provided surfaces\n");
2184         return WINEDDERR_NODC;
2185     }
2186
2187     /* Give more detailed info for ddraw */
2188     if (This->flags & SFLAG_DCINUSE)
2189         return WINEDDERR_DCALREADYCREATED;
2190
2191     /* Can't GetDC if the surface is locked */
2192     if (This->flags & SFLAG_LOCKED)
2193         return WINED3DERR_INVALIDCALL;
2194
2195     memset(&lock, 0, sizeof(lock)); /* To be sure */
2196
2197     /* Create a DIB section if there isn't a hdc yet */
2198     if (!This->hDC)
2199     {
2200         if (This->flags & SFLAG_CLIENT)
2201         {
2202             surface_load_location(This, SFLAG_INSYSMEM, NULL);
2203             surface_release_client_storage(This);
2204         }
2205         hr = IWineD3DBaseSurfaceImpl_CreateDIBSection(iface);
2206         if(FAILED(hr)) return WINED3DERR_INVALIDCALL;
2207
2208         /* Use the dib section from now on if we are not using a PBO */
2209         if (!(This->flags & SFLAG_PBO))
2210             This->resource.allocatedMemory = This->dib.bitmap_data;
2211     }
2212
2213     /* Map the surface */
2214     hr = IWineD3DSurface_Map(iface, &lock, NULL, 0);
2215
2216     /* Sync the DIB with the PBO. This can't be done earlier because Map()
2217      * activates the allocatedMemory. */
2218     if (This->flags & SFLAG_PBO)
2219         memcpy(This->dib.bitmap_data, This->resource.allocatedMemory, This->dib.bitmap_size);
2220
2221     if (FAILED(hr))
2222     {
2223         ERR("IWineD3DSurface_Map failed, hr %#x.\n", hr);
2224         /* keep the dib section */
2225         return hr;
2226     }
2227
2228     if (This->resource.format->id == WINED3DFMT_P8_UINT
2229             || This->resource.format->id == WINED3DFMT_P8_UINT_A8_UNORM)
2230     {
2231         /* GetDC on palettized formats is unsupported in D3D9, and the method is missing in
2232             D3D8, so this should only be used for DX <=7 surfaces (with non-device palettes) */
2233         unsigned int n;
2234         const PALETTEENTRY *pal = NULL;
2235
2236         if(This->palette) {
2237             pal = This->palette->palents;
2238         } else {
2239             IWineD3DSurfaceImpl *dds_primary;
2240             struct wined3d_swapchain *swapchain;
2241             swapchain = This->resource.device->swapchains[0];
2242             dds_primary = swapchain->front_buffer;
2243             if (dds_primary && dds_primary->palette)
2244                 pal = dds_primary->palette->palents;
2245         }
2246
2247         if (pal) {
2248             for (n=0; n<256; n++) {
2249                 col[n].rgbRed   = pal[n].peRed;
2250                 col[n].rgbGreen = pal[n].peGreen;
2251                 col[n].rgbBlue  = pal[n].peBlue;
2252                 col[n].rgbReserved = 0;
2253             }
2254             SetDIBColorTable(This->hDC, 0, 256, col);
2255         }
2256     }
2257
2258     *pHDC = This->hDC;
2259     TRACE("returning %p\n",*pHDC);
2260     This->flags |= SFLAG_DCINUSE;
2261
2262     return WINED3D_OK;
2263 }
2264
2265 static HRESULT WINAPI IWineD3DSurfaceImpl_ReleaseDC(IWineD3DSurface *iface, HDC hDC)
2266 {
2267     IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
2268
2269     TRACE("(%p)->(%p)\n",This,hDC);
2270
2271     if (!(This->flags & SFLAG_DCINUSE))
2272         return WINEDDERR_NODC;
2273
2274     if (This->hDC !=hDC) {
2275         WARN("Application tries to release an invalid DC(%p), surface dc is %p\n", hDC, This->hDC);
2276         return WINEDDERR_NODC;
2277     }
2278
2279     if ((This->flags & SFLAG_PBO) && This->resource.allocatedMemory)
2280     {
2281         /* Copy the contents of the DIB over to the PBO */
2282         memcpy(This->resource.allocatedMemory, This->dib.bitmap_data, This->dib.bitmap_size);
2283     }
2284
2285     /* we locked first, so unlock now */
2286     IWineD3DSurface_Unmap(iface);
2287
2288     This->flags &= ~SFLAG_DCINUSE;
2289
2290     return WINED3D_OK;
2291 }
2292
2293 /* ******************************************************
2294    IWineD3DSurface Internal (No mapping to directx api) parts follow
2295    ****************************************************** */
2296
2297 HRESULT d3dfmt_get_conv(IWineD3DSurfaceImpl *This, BOOL need_alpha_ck,
2298         BOOL use_texturing, struct wined3d_format *format, CONVERT_TYPES *convert)
2299 {
2300     BOOL colorkey_active = need_alpha_ck && (This->CKeyFlags & WINEDDSD_CKSRCBLT);
2301     IWineD3DDeviceImpl *device = This->resource.device;
2302     const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
2303     BOOL blit_supported = FALSE;
2304
2305     /* Copy the default values from the surface. Below we might perform fixups */
2306     /* TODO: get rid of color keying desc fixups by using e.g. a table. */
2307     *format = *This->resource.format;
2308     *convert = NO_CONVERSION;
2309
2310     /* Ok, now look if we have to do any conversion */
2311     switch (This->resource.format->id)
2312     {
2313         case WINED3DFMT_P8_UINT:
2314             /* Below the call to blit_supported is disabled for Wine 1.2
2315              * because the function isn't operating correctly yet. At the
2316              * moment 8-bit blits are handled in software and if certain GL
2317              * extensions are around, surface conversion is performed at
2318              * upload time. The blit_supported call recognizes it as a
2319              * destination fixup. This type of upload 'fixup' and 8-bit to
2320              * 8-bit blits need to be handled by the blit_shader.
2321              * TODO: get rid of this #if 0. */
2322 #if 0
2323             blit_supported = device->blitter->blit_supported(&device->adapter->gl_info, WINED3D_BLIT_OP_COLOR_BLIT,
2324                     &rect, This->resource.usage, This->resource.pool, This->resource.format,
2325                     &rect, This->resource.usage, This->resource.pool, This->resource.format);
2326 #endif
2327             blit_supported = gl_info->supported[EXT_PALETTED_TEXTURE] || gl_info->supported[ARB_FRAGMENT_PROGRAM];
2328
2329             /* Use conversion when the blit_shader backend supports it. It only supports this in case of
2330              * texturing. Further also use conversion in case of color keying.
2331              * Paletted textures can be emulated using shaders but only do that for 2D purposes e.g. situations
2332              * in which the main render target uses p8. Some games like GTA Vice City use P8 for texturing which
2333              * conflicts with this.
2334              */
2335             if (!((blit_supported && device->render_targets && This == device->render_targets[0]))
2336                     || colorkey_active || !use_texturing)
2337             {
2338                 format->glFormat = GL_RGBA;
2339                 format->glInternal = GL_RGBA;
2340                 format->glType = GL_UNSIGNED_BYTE;
2341                 format->conv_byte_count = 4;
2342                 if (colorkey_active)
2343                     *convert = CONVERT_PALETTED_CK;
2344                 else
2345                     *convert = CONVERT_PALETTED;
2346             }
2347             break;
2348
2349         case WINED3DFMT_B2G3R3_UNORM:
2350             /* **********************
2351                 GL_UNSIGNED_BYTE_3_3_2
2352                 ********************** */
2353             if (colorkey_active) {
2354                 /* This texture format will never be used.. So do not care about color keying
2355                     up until the point in time it will be needed :-) */
2356                 FIXME(" ColorKeying not supported in the RGB 332 format !\n");
2357             }
2358             break;
2359
2360         case WINED3DFMT_B5G6R5_UNORM:
2361             if (colorkey_active)
2362             {
2363                 *convert = CONVERT_CK_565;
2364                 format->glFormat = GL_RGBA;
2365                 format->glInternal = GL_RGB5_A1;
2366                 format->glType = GL_UNSIGNED_SHORT_5_5_5_1;
2367                 format->conv_byte_count = 2;
2368             }
2369             break;
2370
2371         case WINED3DFMT_B5G5R5X1_UNORM:
2372             if (colorkey_active)
2373             {
2374                 *convert = CONVERT_CK_5551;
2375                 format->glFormat = GL_BGRA;
2376                 format->glInternal = GL_RGB5_A1;
2377                 format->glType = GL_UNSIGNED_SHORT_1_5_5_5_REV;
2378                 format->conv_byte_count = 2;
2379             }
2380             break;
2381
2382         case WINED3DFMT_B8G8R8_UNORM:
2383             if (colorkey_active)
2384             {
2385                 *convert = CONVERT_CK_RGB24;
2386                 format->glFormat = GL_RGBA;
2387                 format->glInternal = GL_RGBA8;
2388                 format->glType = GL_UNSIGNED_INT_8_8_8_8;
2389                 format->conv_byte_count = 4;
2390             }
2391             break;
2392
2393         case WINED3DFMT_B8G8R8X8_UNORM:
2394             if (colorkey_active)
2395             {
2396                 *convert = CONVERT_RGB32_888;
2397                 format->glFormat = GL_RGBA;
2398                 format->glInternal = GL_RGBA8;
2399                 format->glType = GL_UNSIGNED_INT_8_8_8_8;
2400                 format->conv_byte_count = 4;
2401             }
2402             break;
2403
2404         default:
2405             break;
2406     }
2407
2408     return WINED3D_OK;
2409 }
2410
2411 void d3dfmt_p8_init_palette(IWineD3DSurfaceImpl *This, BYTE table[256][4], BOOL colorkey)
2412 {
2413     IWineD3DDeviceImpl *device = This->resource.device;
2414     struct wined3d_palette *pal = This->palette;
2415     BOOL index_in_alpha = FALSE;
2416     unsigned int i;
2417
2418     /* Old games like StarCraft, C&C, Red Alert and others use P8 render targets.
2419      * Reading back the RGB output each lockrect (each frame as they lock the whole screen)
2420      * is slow. Further RGB->P8 conversion is not possible because palettes can have
2421      * duplicate entries. Store the color key in the unused alpha component to speed the
2422      * download up and to make conversion unneeded. */
2423     index_in_alpha = primary_render_target_is_p8(device);
2424
2425     if (!pal)
2426     {
2427         UINT dxVersion = device->wined3d->dxVersion;
2428
2429         /* In DirectDraw the palette is a property of the surface, there are no such things as device palettes. */
2430         if (dxVersion <= 7)
2431         {
2432             ERR("This code should never get entered for DirectDraw!, expect problems\n");
2433             if (index_in_alpha)
2434             {
2435                 /* Guarantees that memory representation remains correct after sysmem<->texture transfers even if
2436                  * there's no palette at this time. */
2437                 for (i = 0; i < 256; i++) table[i][3] = i;
2438             }
2439         }
2440         else
2441         {
2442             /* Direct3D >= 8 palette usage style: P8 textures use device palettes, palette entry format is A8R8G8B8,
2443              * alpha is stored in peFlags and may be used by the app if D3DPTEXTURECAPS_ALPHAPALETTE device
2444              * capability flag is present (wine does advertise this capability) */
2445             for (i = 0; i < 256; ++i)
2446             {
2447                 table[i][0] = device->palettes[device->currentPalette][i].peRed;
2448                 table[i][1] = device->palettes[device->currentPalette][i].peGreen;
2449                 table[i][2] = device->palettes[device->currentPalette][i].peBlue;
2450                 table[i][3] = device->palettes[device->currentPalette][i].peFlags;
2451             }
2452         }
2453     }
2454     else
2455     {
2456         TRACE("Using surface palette %p\n", pal);
2457         /* Get the surface's palette */
2458         for (i = 0; i < 256; ++i)
2459         {
2460             table[i][0] = pal->palents[i].peRed;
2461             table[i][1] = pal->palents[i].peGreen;
2462             table[i][2] = pal->palents[i].peBlue;
2463
2464             /* When index_in_alpha is set the palette index is stored in the
2465              * alpha component. In case of a readback we can then read
2466              * GL_ALPHA. Color keying is handled in BltOverride using a
2467              * GL_ALPHA_TEST using GL_NOT_EQUAL. In case of index_in_alpha the
2468              * color key itself is passed to glAlphaFunc in other cases the
2469              * alpha component of pixels that should be masked away is set to 0. */
2470             if (index_in_alpha)
2471             {
2472                 table[i][3] = i;
2473             }
2474             else if (colorkey && (i >= This->SrcBltCKey.dwColorSpaceLowValue)
2475                     && (i <= This->SrcBltCKey.dwColorSpaceHighValue))
2476             {
2477                 table[i][3] = 0x00;
2478             }
2479             else if (pal->flags & WINEDDPCAPS_ALPHA)
2480             {
2481                 table[i][3] = pal->palents[i].peFlags;
2482             }
2483             else
2484             {
2485                 table[i][3] = 0xFF;
2486             }
2487         }
2488     }
2489 }
2490
2491 static HRESULT d3dfmt_convert_surface(const BYTE *src, BYTE *dst, UINT pitch, UINT width,
2492         UINT height, UINT outpitch, CONVERT_TYPES convert, IWineD3DSurfaceImpl *This)
2493 {
2494     const BYTE *source;
2495     BYTE *dest;
2496     TRACE("(%p)->(%p),(%d,%d,%d,%d,%p)\n", src, dst, pitch, height, outpitch, convert,This);
2497
2498     switch (convert) {
2499         case NO_CONVERSION:
2500         {
2501             memcpy(dst, src, pitch * height);
2502             break;
2503         }
2504         case CONVERT_PALETTED:
2505         case CONVERT_PALETTED_CK:
2506         {
2507             BYTE table[256][4];
2508             unsigned int x, y;
2509
2510             d3dfmt_p8_init_palette(This, table, (convert == CONVERT_PALETTED_CK));
2511
2512             for (y = 0; y < height; y++)
2513             {
2514                 source = src + pitch * y;
2515                 dest = dst + outpitch * y;
2516                 /* This is an 1 bpp format, using the width here is fine */
2517                 for (x = 0; x < width; x++) {
2518                     BYTE color = *source++;
2519                     *dest++ = table[color][0];
2520                     *dest++ = table[color][1];
2521                     *dest++ = table[color][2];
2522                     *dest++ = table[color][3];
2523                 }
2524             }
2525         }
2526         break;
2527
2528         case CONVERT_CK_565:
2529         {
2530             /* Converting the 565 format in 5551 packed to emulate color-keying.
2531
2532               Note : in all these conversion, it would be best to average the averaging
2533                       pixels to get the color of the pixel that will be color-keyed to
2534                       prevent 'color bleeding'. This will be done later on if ever it is
2535                       too visible.
2536
2537               Note2: Nvidia documents say that their driver does not support alpha + color keying
2538                      on the same surface and disables color keying in such a case
2539             */
2540             unsigned int x, y;
2541             const WORD *Source;
2542             WORD *Dest;
2543
2544             TRACE("Color keyed 565\n");
2545
2546             for (y = 0; y < height; y++) {
2547                 Source = (const WORD *)(src + y * pitch);
2548                 Dest = (WORD *) (dst + y * outpitch);
2549                 for (x = 0; x < width; x++ ) {
2550                     WORD color = *Source++;
2551                     *Dest = ((color & 0xFFC0) | ((color & 0x1F) << 1));
2552                     if ((color < This->SrcBltCKey.dwColorSpaceLowValue) ||
2553                         (color > This->SrcBltCKey.dwColorSpaceHighValue)) {
2554                         *Dest |= 0x0001;
2555                     }
2556                     Dest++;
2557                 }
2558             }
2559         }
2560         break;
2561
2562         case CONVERT_CK_5551:
2563         {
2564             /* Converting X1R5G5B5 format to R5G5B5A1 to emulate color-keying. */
2565             unsigned int x, y;
2566             const WORD *Source;
2567             WORD *Dest;
2568             TRACE("Color keyed 5551\n");
2569             for (y = 0; y < height; y++) {
2570                 Source = (const WORD *)(src + y * pitch);
2571                 Dest = (WORD *) (dst + y * outpitch);
2572                 for (x = 0; x < width; x++ ) {
2573                     WORD color = *Source++;
2574                     *Dest = color;
2575                     if ((color < This->SrcBltCKey.dwColorSpaceLowValue) ||
2576                         (color > This->SrcBltCKey.dwColorSpaceHighValue)) {
2577                         *Dest |= (1 << 15);
2578                     }
2579                     else {
2580                         *Dest &= ~(1 << 15);
2581                     }
2582                     Dest++;
2583                 }
2584             }
2585         }
2586         break;
2587
2588         case CONVERT_CK_RGB24:
2589         {
2590             /* Converting R8G8B8 format to R8G8B8A8 with color-keying. */
2591             unsigned int x, y;
2592             for (y = 0; y < height; y++)
2593             {
2594                 source = src + pitch * y;
2595                 dest = dst + outpitch * y;
2596                 for (x = 0; x < width; x++) {
2597                     DWORD color = ((DWORD)source[0] << 16) + ((DWORD)source[1] << 8) + (DWORD)source[2] ;
2598                     DWORD dstcolor = color << 8;
2599                     if ((color < This->SrcBltCKey.dwColorSpaceLowValue) ||
2600                         (color > This->SrcBltCKey.dwColorSpaceHighValue)) {
2601                         dstcolor |= 0xff;
2602                     }
2603                     *(DWORD*)dest = dstcolor;
2604                     source += 3;
2605                     dest += 4;
2606                 }
2607             }
2608         }
2609         break;
2610
2611         case CONVERT_RGB32_888:
2612         {
2613             /* Converting X8R8G8B8 format to R8G8B8A8 with color-keying. */
2614             unsigned int x, y;
2615             for (y = 0; y < height; y++)
2616             {
2617                 source = src + pitch * y;
2618                 dest = dst + outpitch * y;
2619                 for (x = 0; x < width; x++) {
2620                     DWORD color = 0xffffff & *(const DWORD*)source;
2621                     DWORD dstcolor = color << 8;
2622                     if ((color < This->SrcBltCKey.dwColorSpaceLowValue) ||
2623                         (color > This->SrcBltCKey.dwColorSpaceHighValue)) {
2624                         dstcolor |= 0xff;
2625                     }
2626                     *(DWORD*)dest = dstcolor;
2627                     source += 4;
2628                     dest += 4;
2629                 }
2630             }
2631         }
2632         break;
2633
2634         default:
2635             ERR("Unsupported conversion type %#x.\n", convert);
2636     }
2637     return WINED3D_OK;
2638 }
2639
2640 BOOL palette9_changed(IWineD3DSurfaceImpl *This)
2641 {
2642     IWineD3DDeviceImpl *device = This->resource.device;
2643
2644     if (This->palette || (This->resource.format->id != WINED3DFMT_P8_UINT
2645             && This->resource.format->id != WINED3DFMT_P8_UINT_A8_UNORM))
2646     {
2647         /* If a ddraw-style palette is attached assume no d3d9 palette change.
2648          * Also the palette isn't interesting if the surface format isn't P8 or A8P8
2649          */
2650         return FALSE;
2651     }
2652
2653     if (This->palette9)
2654     {
2655         if (!memcmp(This->palette9, device->palettes[device->currentPalette], sizeof(PALETTEENTRY) * 256))
2656         {
2657             return FALSE;
2658         }
2659     } else {
2660         This->palette9 = HeapAlloc(GetProcessHeap(), 0, sizeof(PALETTEENTRY) * 256);
2661     }
2662     memcpy(This->palette9, device->palettes[device->currentPalette], sizeof(PALETTEENTRY) * 256);
2663     return TRUE;
2664 }
2665
2666 static HRESULT WINAPI IWineD3DSurfaceImpl_SetFormat(IWineD3DSurface *iface, enum wined3d_format_id format)
2667 {
2668     IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
2669     HRESULT hr;
2670
2671     TRACE("(%p) : Calling base function first\n", This);
2672     hr = IWineD3DBaseSurfaceImpl_SetFormat(iface, format);
2673     if (SUCCEEDED(hr))
2674     {
2675         This->flags &= ~(SFLAG_ALLOCATED | SFLAG_SRGBALLOCATED);
2676         TRACE("(%p) : glFormat %d, glFormatInternal %d, glType %d\n", This, This->resource.format->glFormat,
2677                 This->resource.format->glInternal, This->resource.format->glType);
2678     }
2679     return hr;
2680 }
2681
2682 static HRESULT WINAPI IWineD3DSurfaceImpl_SetMem(IWineD3DSurface *iface, void *Mem) {
2683     IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *) iface;
2684
2685     TRACE("iface %p, mem %p.\n", iface, Mem);
2686
2687     if (This->flags & (SFLAG_LOCKED | SFLAG_DCINUSE))
2688     {
2689         WARN("Surface is locked or the HDC is in use\n");
2690         return WINED3DERR_INVALIDCALL;
2691     }
2692
2693     if(Mem && Mem != This->resource.allocatedMemory) {
2694         void *release = NULL;
2695
2696         /* Do I have to copy the old surface content? */
2697         if (This->flags & SFLAG_DIBSECTION)
2698         {
2699             SelectObject(This->hDC, This->dib.holdbitmap);
2700             DeleteDC(This->hDC);
2701             /* Release the DIB section */
2702             DeleteObject(This->dib.DIBsection);
2703             This->dib.bitmap_data = NULL;
2704             This->resource.allocatedMemory = NULL;
2705             This->hDC = NULL;
2706             This->flags &= ~SFLAG_DIBSECTION;
2707         }
2708         else if (!(This->flags & SFLAG_USERPTR))
2709         {
2710             release = This->resource.heapMemory;
2711             This->resource.heapMemory = NULL;
2712         }
2713         This->resource.allocatedMemory = Mem;
2714         This->flags |= SFLAG_USERPTR;
2715
2716         /* Now the surface memory is most up do date. Invalidate drawable and texture */
2717         surface_modify_location(This, SFLAG_INSYSMEM, TRUE);
2718
2719         /* For client textures opengl has to be notified */
2720         if (This->flags & SFLAG_CLIENT)
2721             surface_release_client_storage(This);
2722
2723         /* Now free the old memory if any */
2724         HeapFree(GetProcessHeap(), 0, release);
2725     }
2726     else if (This->flags & SFLAG_USERPTR)
2727     {
2728         /* Map and GetDC will re-create the dib section and allocated memory. */
2729         This->resource.allocatedMemory = NULL;
2730         /* HeapMemory should be NULL already */
2731         if (This->resource.heapMemory)
2732             ERR("User pointer surface has heap memory allocated.\n");
2733         This->flags &= ~(SFLAG_USERPTR | SFLAG_INSYSMEM);
2734
2735         if (This->flags & SFLAG_CLIENT)
2736             surface_release_client_storage(This);
2737
2738         surface_prepare_system_memory(This);
2739         surface_modify_location(This, SFLAG_INSYSMEM, TRUE);
2740     }
2741     return WINED3D_OK;
2742 }
2743
2744 void flip_surface(IWineD3DSurfaceImpl *front, IWineD3DSurfaceImpl *back) {
2745
2746     /* Flip the surface contents */
2747     /* Flip the DC */
2748     {
2749         HDC tmp;
2750         tmp = front->hDC;
2751         front->hDC = back->hDC;
2752         back->hDC = tmp;
2753     }
2754
2755     /* Flip the DIBsection */
2756     {
2757         HBITMAP tmp;
2758         BOOL hasDib = front->flags & SFLAG_DIBSECTION;
2759         tmp = front->dib.DIBsection;
2760         front->dib.DIBsection = back->dib.DIBsection;
2761         back->dib.DIBsection = tmp;
2762
2763         if (back->flags & SFLAG_DIBSECTION) front->flags |= SFLAG_DIBSECTION;
2764         else front->flags &= ~SFLAG_DIBSECTION;
2765         if (hasDib) back->flags |= SFLAG_DIBSECTION;
2766         else back->flags &= ~SFLAG_DIBSECTION;
2767     }
2768
2769     /* Flip the surface data */
2770     {
2771         void* tmp;
2772
2773         tmp = front->dib.bitmap_data;
2774         front->dib.bitmap_data = back->dib.bitmap_data;
2775         back->dib.bitmap_data = tmp;
2776
2777         tmp = front->resource.allocatedMemory;
2778         front->resource.allocatedMemory = back->resource.allocatedMemory;
2779         back->resource.allocatedMemory = tmp;
2780
2781         tmp = front->resource.heapMemory;
2782         front->resource.heapMemory = back->resource.heapMemory;
2783         back->resource.heapMemory = tmp;
2784     }
2785
2786     /* Flip the PBO */
2787     {
2788         GLuint tmp_pbo = front->pbo;
2789         front->pbo = back->pbo;
2790         back->pbo = tmp_pbo;
2791     }
2792
2793     /* client_memory should not be different, but just in case */
2794     {
2795         BOOL tmp;
2796         tmp = front->dib.client_memory;
2797         front->dib.client_memory = back->dib.client_memory;
2798         back->dib.client_memory = tmp;
2799     }
2800
2801     /* Flip the opengl texture */
2802     {
2803         GLuint tmp;
2804
2805         tmp = back->texture_name;
2806         back->texture_name = front->texture_name;
2807         front->texture_name = tmp;
2808
2809         tmp = back->texture_name_srgb;
2810         back->texture_name_srgb = front->texture_name_srgb;
2811         front->texture_name_srgb = tmp;
2812
2813         resource_unload(&back->resource);
2814         resource_unload(&front->resource);
2815     }
2816
2817     {
2818         DWORD tmp_flags = back->flags;
2819         back->flags = front->flags;
2820         front->flags = tmp_flags;
2821     }
2822 }
2823
2824 static HRESULT WINAPI IWineD3DSurfaceImpl_Flip(IWineD3DSurface *iface, IWineD3DSurface *override, DWORD flags)
2825 {
2826     IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
2827     struct wined3d_swapchain *swapchain = NULL;
2828
2829     TRACE("iface %p, override %p, flags %#x.\n", iface, override, flags);
2830
2831     /* Flipping is only supported on RenderTargets and overlays*/
2832     if( !(This->resource.usage & (WINED3DUSAGE_RENDERTARGET | WINED3DUSAGE_OVERLAY)) ) {
2833         WARN("Tried to flip a non-render target, non-overlay surface\n");
2834         return WINEDDERR_NOTFLIPPABLE;
2835     }
2836
2837     if(This->resource.usage & WINED3DUSAGE_OVERLAY) {
2838         flip_surface(This, (IWineD3DSurfaceImpl *) override);
2839
2840         /* Update the overlay if it is visible */
2841         if (This->overlay_dest)
2842             return This->surface_ops->surface_draw_overlay(This);
2843         else
2844             return WINED3D_OK;
2845     }
2846
2847     if(override) {
2848         /* DDraw sets this for the X11 surfaces, so don't confuse the user
2849          * FIXME("(%p) Target override is not supported by now\n", This);
2850          * Additionally, it isn't really possible to support triple-buffering
2851          * properly on opengl at all
2852          */
2853     }
2854
2855     if (This->container.type != WINED3D_CONTAINER_SWAPCHAIN)
2856     {
2857         ERR("Flipped surface is not on a swapchain\n");
2858         return WINEDDERR_NOTFLIPPABLE;
2859     }
2860     swapchain = This->container.u.swapchain;
2861
2862     /* Just overwrite the swapchain presentation interval. This is ok because only ddraw apps can call Flip,
2863      * and only d3d8 and d3d9 apps specify the presentation interval
2864      */
2865     if (!(flags & (WINEDDFLIP_NOVSYNC | WINEDDFLIP_INTERVAL2 | WINEDDFLIP_INTERVAL3 | WINEDDFLIP_INTERVAL4)))
2866         /* Most common case first to avoid wasting time on all the other cases */
2867         swapchain->presentParms.PresentationInterval = WINED3DPRESENT_INTERVAL_ONE;
2868     else if (flags & WINEDDFLIP_NOVSYNC)
2869         swapchain->presentParms.PresentationInterval = WINED3DPRESENT_INTERVAL_IMMEDIATE;
2870     else if (flags & WINEDDFLIP_INTERVAL2)
2871         swapchain->presentParms.PresentationInterval = WINED3DPRESENT_INTERVAL_TWO;
2872     else if (flags & WINEDDFLIP_INTERVAL3)
2873         swapchain->presentParms.PresentationInterval = WINED3DPRESENT_INTERVAL_THREE;
2874     else
2875         swapchain->presentParms.PresentationInterval = WINED3DPRESENT_INTERVAL_FOUR;
2876
2877     /* Flipping a OpenGL surface -> present. */
2878     return wined3d_swapchain_present(swapchain, NULL, NULL, swapchain->win_handle, NULL, 0);
2879 }
2880
2881 /* Does a direct frame buffer -> texture copy. Stretching is done
2882  * with single pixel copy calls
2883  */
2884 static void fb_copy_to_texture_direct(IWineD3DSurfaceImpl *dst_surface, IWineD3DSurfaceImpl *src_surface,
2885         const RECT *src_rect, const RECT *dst_rect_in, WINED3DTEXTUREFILTERTYPE Filter)
2886 {
2887     IWineD3DDeviceImpl *device = dst_surface->resource.device;
2888     float xrel, yrel;
2889     UINT row;
2890     struct wined3d_context *context;
2891     BOOL upsidedown = FALSE;
2892     RECT dst_rect = *dst_rect_in;
2893
2894     /* Make sure that the top pixel is always above the bottom pixel, and keep a separate upside down flag
2895      * glCopyTexSubImage is a bit picky about the parameters we pass to it
2896      */
2897     if(dst_rect.top > dst_rect.bottom) {
2898         UINT tmp = dst_rect.bottom;
2899         dst_rect.bottom = dst_rect.top;
2900         dst_rect.top = tmp;
2901         upsidedown = TRUE;
2902     }
2903
2904     context = context_acquire(device, src_surface);
2905     context_apply_blit_state(context, device);
2906     surface_internal_preload(dst_surface, SRGB_RGB);
2907     ENTER_GL();
2908
2909     /* Bind the target texture */
2910     glBindTexture(dst_surface->texture_target, dst_surface->texture_name);
2911     checkGLcall("glBindTexture");
2912     if (surface_is_offscreen(src_surface))
2913     {
2914         TRACE("Reading from an offscreen target\n");
2915         upsidedown = !upsidedown;
2916         glReadBuffer(device->offscreenBuffer);
2917     }
2918     else
2919     {
2920         glReadBuffer(surface_get_gl_buffer(src_surface));
2921     }
2922     checkGLcall("glReadBuffer");
2923
2924     xrel = (float) (src_rect->right - src_rect->left) / (float) (dst_rect.right - dst_rect.left);
2925     yrel = (float) (src_rect->bottom - src_rect->top) / (float) (dst_rect.bottom - dst_rect.top);
2926
2927     if ((xrel - 1.0f < -eps) || (xrel - 1.0f > eps))
2928     {
2929         FIXME("Doing a pixel by pixel copy from the framebuffer to a texture, expect major performance issues\n");
2930
2931         if(Filter != WINED3DTEXF_NONE && Filter != WINED3DTEXF_POINT) {
2932             ERR("Texture filtering not supported in direct blit\n");
2933         }
2934     }
2935     else if ((Filter != WINED3DTEXF_NONE && Filter != WINED3DTEXF_POINT)
2936             && ((yrel - 1.0f < -eps) || (yrel - 1.0f > eps)))
2937     {
2938         ERR("Texture filtering not supported in direct blit\n");
2939     }
2940
2941     if (upsidedown
2942             && !((xrel - 1.0f < -eps) || (xrel - 1.0f > eps))
2943             && !((yrel - 1.0f < -eps) || (yrel - 1.0f > eps)))
2944     {
2945         /* Upside down copy without stretching is nice, one glCopyTexSubImage call will do */
2946
2947         glCopyTexSubImage2D(dst_surface->texture_target, dst_surface->texture_level,
2948                 dst_rect.left /*xoffset */, dst_rect.top /* y offset */,
2949                 src_rect->left, src_surface->resource.height - src_rect->bottom,
2950                 dst_rect.right - dst_rect.left, dst_rect.bottom - dst_rect.top);
2951     }
2952     else
2953     {
2954         UINT yoffset = src_surface->resource.height - src_rect->top + dst_rect.top - 1;
2955         /* I have to process this row by row to swap the image,
2956          * otherwise it would be upside down, so stretching in y direction
2957          * doesn't cost extra time
2958          *
2959          * However, stretching in x direction can be avoided if not necessary
2960          */
2961         for(row = dst_rect.top; row < dst_rect.bottom; row++) {
2962             if ((xrel - 1.0f < -eps) || (xrel - 1.0f > eps))
2963             {
2964                 /* Well, that stuff works, but it's very slow.
2965                  * find a better way instead
2966                  */
2967                 UINT col;
2968
2969                 for (col = dst_rect.left; col < dst_rect.right; ++col)
2970                 {
2971                     glCopyTexSubImage2D(dst_surface->texture_target, dst_surface->texture_level,
2972                             dst_rect.left + col /* x offset */, row /* y offset */,
2973                             src_rect->left + col * xrel, yoffset - (int) (row * yrel), 1, 1);
2974                 }
2975             }
2976             else
2977             {
2978                 glCopyTexSubImage2D(dst_surface->texture_target, dst_surface->texture_level,
2979                         dst_rect.left /* x offset */, row /* y offset */,
2980                         src_rect->left, yoffset - (int) (row * yrel), dst_rect.right - dst_rect.left, 1);
2981             }
2982         }
2983     }
2984     checkGLcall("glCopyTexSubImage2D");
2985
2986     LEAVE_GL();
2987     context_release(context);
2988
2989     /* The texture is now most up to date - If the surface is a render target and has a drawable, this
2990      * path is never entered
2991      */
2992     surface_modify_location(dst_surface, SFLAG_INTEXTURE, TRUE);
2993 }
2994
2995 /* Uses the hardware to stretch and flip the image */
2996 static void fb_copy_to_texture_hwstretch(IWineD3DSurfaceImpl *dst_surface, IWineD3DSurfaceImpl *src_surface,
2997         const RECT *src_rect, const RECT *dst_rect_in, WINED3DTEXTUREFILTERTYPE Filter)
2998 {
2999     IWineD3DDeviceImpl *device = dst_surface->resource.device;
3000     struct wined3d_swapchain *src_swapchain = NULL;
3001     GLuint src, backup = 0;
3002     float left, right, top, bottom; /* Texture coordinates */
3003     UINT fbwidth = src_surface->resource.width;
3004     UINT fbheight = src_surface->resource.height;
3005     struct wined3d_context *context;
3006     GLenum drawBuffer = GL_BACK;
3007     GLenum texture_target;
3008     BOOL noBackBufferBackup;
3009     BOOL src_offscreen;
3010     BOOL upsidedown = FALSE;
3011     RECT dst_rect = *dst_rect_in;
3012
3013     TRACE("Using hwstretch blit\n");
3014     /* Activate the Proper context for reading from the source surface, set it up for blitting */
3015     context = context_acquire(device, src_surface);
3016     context_apply_blit_state(context, device);
3017     surface_internal_preload(dst_surface, SRGB_RGB);
3018
3019     src_offscreen = surface_is_offscreen(src_surface);
3020     noBackBufferBackup = src_offscreen && wined3d_settings.offscreen_rendering_mode == ORM_FBO;
3021     if (!noBackBufferBackup && !src_surface->texture_name)
3022     {
3023         /* Get it a description */
3024         surface_internal_preload(src_surface, SRGB_RGB);
3025     }
3026     ENTER_GL();
3027
3028     /* Try to use an aux buffer for drawing the rectangle. This way it doesn't need restoring.
3029      * This way we don't have to wait for the 2nd readback to finish to leave this function.
3030      */
3031     if (context->aux_buffers >= 2)
3032     {
3033         /* Got more than one aux buffer? Use the 2nd aux buffer */
3034         drawBuffer = GL_AUX1;
3035     }
3036     else if ((!src_offscreen || device->offscreenBuffer == GL_BACK) && context->aux_buffers >= 1)
3037     {
3038         /* Only one aux buffer, but it isn't used (Onscreen rendering, or non-aux orm)? Use it! */
3039         drawBuffer = GL_AUX0;
3040     }
3041
3042     if(noBackBufferBackup) {
3043         glGenTextures(1, &backup);
3044         checkGLcall("glGenTextures");
3045         glBindTexture(GL_TEXTURE_2D, backup);
3046         checkGLcall("glBindTexture(GL_TEXTURE_2D, backup)");
3047         texture_target = GL_TEXTURE_2D;
3048     } else {
3049         /* Backup the back buffer and copy the source buffer into a texture to draw an upside down stretched quad. If
3050          * we are reading from the back buffer, the backup can be used as source texture
3051          */
3052         texture_target = src_surface->texture_target;
3053         glBindTexture(texture_target, src_surface->texture_name);
3054         checkGLcall("glBindTexture(texture_target, src_surface->texture_name)");
3055         glEnable(texture_target);
3056         checkGLcall("glEnable(texture_target)");
3057
3058         /* For now invalidate the texture copy of the back buffer. Drawable and sysmem copy are untouched */
3059         src_surface->flags &= ~SFLAG_INTEXTURE;
3060     }
3061
3062     /* Make sure that the top pixel is always above the bottom pixel, and keep a separate upside down flag
3063      * glCopyTexSubImage is a bit picky about the parameters we pass to it
3064      */
3065     if(dst_rect.top > dst_rect.bottom) {
3066         UINT tmp = dst_rect.bottom;
3067         dst_rect.bottom = dst_rect.top;
3068         dst_rect.top = tmp;
3069         upsidedown = TRUE;
3070     }
3071
3072     if (src_offscreen)
3073     {
3074         TRACE("Reading from an offscreen target\n");
3075         upsidedown = !upsidedown;
3076         glReadBuffer(device->offscreenBuffer);
3077     }
3078     else
3079     {
3080         glReadBuffer(surface_get_gl_buffer(src_surface));
3081     }
3082
3083     /* TODO: Only back up the part that will be overwritten */
3084     glCopyTexSubImage2D(texture_target, 0,
3085                         0, 0 /* read offsets */,
3086                         0, 0,
3087                         fbwidth,
3088                         fbheight);
3089
3090     checkGLcall("glCopyTexSubImage2D");
3091
3092     /* No issue with overriding these - the sampler is dirty due to blit usage */
3093     glTexParameteri(texture_target, GL_TEXTURE_MAG_FILTER,
3094             wined3d_gl_mag_filter(magLookup, Filter));
3095     checkGLcall("glTexParameteri");
3096     glTexParameteri(texture_target, GL_TEXTURE_MIN_FILTER,
3097             wined3d_gl_min_mip_filter(minMipLookup, Filter, WINED3DTEXF_NONE));
3098     checkGLcall("glTexParameteri");
3099
3100     if (src_surface->container.type == WINED3D_CONTAINER_SWAPCHAIN)
3101         src_swapchain = src_surface->container.u.swapchain;
3102     if (!src_swapchain || src_surface == src_swapchain->back_buffers[0])
3103     {
3104         src = backup ? backup : src_surface->texture_name;
3105     }
3106     else
3107     {
3108         glReadBuffer(GL_FRONT);
3109         checkGLcall("glReadBuffer(GL_FRONT)");
3110
3111         glGenTextures(1, &src);
3112         checkGLcall("glGenTextures(1, &src)");
3113         glBindTexture(GL_TEXTURE_2D, src);
3114         checkGLcall("glBindTexture(GL_TEXTURE_2D, src)");
3115
3116         /* TODO: Only copy the part that will be read. Use src_rect->left, src_rect->bottom as origin, but with the width watch
3117          * out for power of 2 sizes
3118          */
3119         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, src_surface->pow2Width,
3120                 src_surface->pow2Height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
3121         checkGLcall("glTexImage2D");
3122         glCopyTexSubImage2D(GL_TEXTURE_2D, 0,
3123                             0, 0 /* read offsets */,
3124                             0, 0,
3125                             fbwidth,
3126                             fbheight);
3127
3128         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
3129         checkGLcall("glTexParameteri");
3130         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
3131         checkGLcall("glTexParameteri");
3132
3133         glReadBuffer(GL_BACK);
3134         checkGLcall("glReadBuffer(GL_BACK)");
3135
3136         if(texture_target != GL_TEXTURE_2D) {
3137             glDisable(texture_target);
3138             glEnable(GL_TEXTURE_2D);
3139             texture_target = GL_TEXTURE_2D;
3140         }
3141     }
3142     checkGLcall("glEnd and previous");
3143
3144     left = src_rect->left;
3145     right = src_rect->right;
3146
3147     if (!upsidedown)
3148     {
3149         top = src_surface->resource.height - src_rect->top;
3150         bottom = src_surface->resource.height - src_rect->bottom;
3151     }
3152     else
3153     {
3154         top = src_surface->resource.height - src_rect->bottom;
3155         bottom = src_surface->resource.height - src_rect->top;
3156     }
3157
3158     if (src_surface->flags & SFLAG_NORMCOORD)
3159     {
3160         left /= src_surface->pow2Width;
3161         right /= src_surface->pow2Width;
3162         top /= src_surface->pow2Height;
3163         bottom /= src_surface->pow2Height;
3164     }
3165
3166     /* draw the source texture stretched and upside down. The correct surface is bound already */
3167     glTexParameteri(texture_target, GL_TEXTURE_WRAP_S, GL_CLAMP);
3168     glTexParameteri(texture_target, GL_TEXTURE_WRAP_T, GL_CLAMP);
3169
3170     context_set_draw_buffer(context, drawBuffer);
3171     glReadBuffer(drawBuffer);
3172
3173     glBegin(GL_QUADS);
3174         /* bottom left */
3175         glTexCoord2f(left, bottom);
3176         glVertex2i(0, 0);
3177
3178         /* top left */
3179         glTexCoord2f(left, top);
3180         glVertex2i(0, dst_rect.bottom - dst_rect.top);
3181
3182         /* top right */
3183         glTexCoord2f(right, top);
3184         glVertex2i(dst_rect.right - dst_rect.left, dst_rect.bottom - dst_rect.top);
3185
3186         /* bottom right */
3187         glTexCoord2f(right, bottom);
3188         glVertex2i(dst_rect.right - dst_rect.left, 0);
3189     glEnd();
3190     checkGLcall("glEnd and previous");
3191
3192     if (texture_target != dst_surface->texture_target)
3193     {
3194         glDisable(texture_target);
3195         glEnable(dst_surface->texture_target);
3196         texture_target = dst_surface->texture_target;
3197     }
3198
3199     /* Now read the stretched and upside down image into the destination texture */
3200     glBindTexture(texture_target, dst_surface->texture_name);
3201     checkGLcall("glBindTexture");
3202     glCopyTexSubImage2D(texture_target,
3203                         0,
3204                         dst_rect.left, dst_rect.top, /* xoffset, yoffset */
3205                         0, 0, /* We blitted the image to the origin */
3206                         dst_rect.right - dst_rect.left, dst_rect.bottom - dst_rect.top);
3207     checkGLcall("glCopyTexSubImage2D");
3208
3209     if(drawBuffer == GL_BACK) {
3210         /* Write the back buffer backup back */
3211         if(backup) {
3212             if(texture_target != GL_TEXTURE_2D) {
3213                 glDisable(texture_target);
3214                 glEnable(GL_TEXTURE_2D);
3215                 texture_target = GL_TEXTURE_2D;
3216             }
3217             glBindTexture(GL_TEXTURE_2D, backup);
3218             checkGLcall("glBindTexture(GL_TEXTURE_2D, backup)");
3219         }
3220         else
3221         {
3222             if (texture_target != src_surface->texture_target)
3223             {
3224                 glDisable(texture_target);
3225                 glEnable(src_surface->texture_target);
3226                 texture_target = src_surface->texture_target;
3227             }
3228             glBindTexture(src_surface->texture_target, src_surface->texture_name);
3229             checkGLcall("glBindTexture(src_surface->texture_target, src_surface->texture_name)");
3230         }
3231
3232         glBegin(GL_QUADS);
3233             /* top left */
3234             glTexCoord2f(0.0f, 0.0f);
3235             glVertex2i(0, fbheight);
3236
3237             /* bottom left */
3238             glTexCoord2f(0.0f, (float)fbheight / (float)src_surface->pow2Height);
3239             glVertex2i(0, 0);
3240
3241             /* bottom right */
3242             glTexCoord2f((float)fbwidth / (float)src_surface->pow2Width,
3243                     (float)fbheight / (float)src_surface->pow2Height);
3244             glVertex2i(fbwidth, 0);
3245
3246             /* top right */
3247             glTexCoord2f((float)fbwidth / (float)src_surface->pow2Width, 0.0f);
3248             glVertex2i(fbwidth, fbheight);
3249         glEnd();
3250     }
3251     glDisable(texture_target);
3252     checkGLcall("glDisable(texture_target)");
3253
3254     /* Cleanup */
3255     if (src != src_surface->texture_name && src != backup)
3256     {
3257         glDeleteTextures(1, &src);
3258         checkGLcall("glDeleteTextures(1, &src)");
3259     }
3260     if(backup) {
3261         glDeleteTextures(1, &backup);
3262         checkGLcall("glDeleteTextures(1, &backup)");
3263     }
3264
3265     LEAVE_GL();
3266
3267     if (wined3d_settings.strict_draw_ordering) wglFlush(); /* Flush to ensure ordering across contexts. */
3268
3269     context_release(context);
3270
3271     /* The texture is now most up to date - If the surface is a render target and has a drawable, this
3272      * path is never entered
3273      */
3274     surface_modify_location(dst_surface, SFLAG_INTEXTURE, TRUE);
3275 }
3276
3277 /* Until the blit_shader is ready, define some prototypes here. */
3278 static BOOL fbo_blit_supported(const struct wined3d_gl_info *gl_info, enum wined3d_blit_op blit_op,
3279         const RECT *src_rect, DWORD src_usage, WINED3DPOOL src_pool, const struct wined3d_format *src_format,
3280         const RECT *dst_rect, DWORD dst_usage, WINED3DPOOL dst_pool, const struct wined3d_format *dst_format);
3281
3282 /* Front buffer coordinates are always full screen coordinates, but our GL
3283  * drawable is limited to the window's client area. The sysmem and texture
3284  * copies do have the full screen size. Note that GL has a bottom-left
3285  * origin, while D3D has a top-left origin. */
3286 void surface_translate_drawable_coords(IWineD3DSurfaceImpl *surface, HWND window, RECT *rect)
3287 {
3288     UINT drawable_height;
3289
3290     if (surface->container.type == WINED3D_CONTAINER_SWAPCHAIN
3291             && surface == surface->container.u.swapchain->front_buffer)
3292     {
3293         POINT offset = {0, 0};
3294         RECT windowsize;
3295
3296         ScreenToClient(window, &offset);
3297         OffsetRect(rect, offset.x, offset.y);
3298
3299         GetClientRect(window, &windowsize);
3300         drawable_height = windowsize.bottom - windowsize.top;
3301     }
3302     else
3303     {
3304         drawable_height = surface->resource.height;
3305     }
3306
3307     rect->top = drawable_height - rect->top;
3308     rect->bottom = drawable_height - rect->bottom;
3309 }
3310
3311 static BOOL surface_is_full_rect(IWineD3DSurfaceImpl *surface, const RECT *r)
3312 {
3313     if ((r->left && r->right) || abs(r->right - r->left) != surface->resource.width)
3314         return FALSE;
3315     if ((r->top && r->bottom) || abs(r->bottom - r->top) != surface->resource.height)
3316         return FALSE;
3317     return TRUE;
3318 }
3319
3320 /* blit between surface locations. onscreen on different swapchains is not supported.
3321  * depth / stencil is not supported. */
3322 static void surface_blt_fbo(IWineD3DDeviceImpl *device, const WINED3DTEXTUREFILTERTYPE filter,
3323         IWineD3DSurfaceImpl *src_surface, DWORD src_location, const RECT *src_rect_in,
3324         IWineD3DSurfaceImpl *dst_surface, DWORD dst_location, const RECT *dst_rect_in)
3325 {
3326     const struct wined3d_gl_info *gl_info;
3327     struct wined3d_context *context;
3328     RECT src_rect, dst_rect;
3329     GLenum gl_filter;
3330
3331     TRACE("device %p, filter %s,\n", device, debug_d3dtexturefiltertype(filter));
3332     TRACE("src_surface %p, src_location %s, src_rect %s,\n",
3333             src_surface, debug_surflocation(src_location), wine_dbgstr_rect(src_rect_in));
3334     TRACE("dst_surface %p, dst_location %s, dst_rect %s.\n",
3335             dst_surface, debug_surflocation(dst_location), wine_dbgstr_rect(dst_rect_in));
3336
3337     src_rect = *src_rect_in;
3338     dst_rect = *dst_rect_in;
3339
3340     switch (filter)
3341     {
3342         case WINED3DTEXF_LINEAR:
3343             gl_filter = GL_LINEAR;
3344             break;
3345
3346         default:
3347             FIXME("Unsupported filter mode %s (%#x).\n", debug_d3dtexturefiltertype(filter), filter);
3348         case WINED3DTEXF_NONE:
3349         case WINED3DTEXF_POINT:
3350             gl_filter = GL_NEAREST;
3351             break;
3352     }
3353
3354     if (src_location == SFLAG_INDRAWABLE && surface_is_offscreen(src_surface))
3355         src_location = SFLAG_INTEXTURE;
3356     if (dst_location == SFLAG_INDRAWABLE && surface_is_offscreen(dst_surface))
3357         dst_location = SFLAG_INTEXTURE;
3358
3359     /* Make sure the locations are up-to-date. Loading the destination
3360      * surface isn't required if the entire surface is overwritten. (And is
3361      * in fact harmful if we're being called by surface_load_location() with
3362      * the purpose of loading the destination surface.) */
3363     surface_load_location(src_surface, src_location, NULL);
3364     if (!surface_is_full_rect(dst_surface, &dst_rect))
3365         surface_load_location(dst_surface, dst_location, NULL);
3366
3367     if (src_location == SFLAG_INDRAWABLE) context = context_acquire(device, src_surface);
3368     else if (dst_location == SFLAG_INDRAWABLE) context = context_acquire(device, dst_surface);
3369     else context = context_acquire(device, NULL);
3370
3371     if (!context->valid)
3372     {
3373         context_release(context);
3374         WARN("Invalid context, skipping blit.\n");
3375         return;
3376     }
3377
3378     gl_info = context->gl_info;
3379
3380     if (src_location == SFLAG_INDRAWABLE)
3381     {
3382         GLenum buffer = surface_get_gl_buffer(src_surface);
3383
3384         TRACE("Source surface %p is onscreen.\n", src_surface);
3385
3386         surface_translate_drawable_coords(src_surface, context->win_handle, &src_rect);
3387
3388         ENTER_GL();
3389         context_bind_fbo(context, GL_READ_FRAMEBUFFER, NULL);
3390         glReadBuffer(buffer);
3391         checkGLcall("glReadBuffer()");
3392     }
3393     else
3394     {
3395         TRACE("Source surface %p is offscreen.\n", src_surface);
3396         ENTER_GL();
3397         context_apply_fbo_state_blit(context, GL_READ_FRAMEBUFFER, src_surface, NULL, src_location);
3398         glReadBuffer(GL_COLOR_ATTACHMENT0);
3399         checkGLcall("glReadBuffer()");
3400     }
3401     context_check_fbo_status(context, GL_READ_FRAMEBUFFER);
3402     LEAVE_GL();
3403
3404     if (dst_location == SFLAG_INDRAWABLE)
3405     {
3406         GLenum buffer = surface_get_gl_buffer(dst_surface);
3407
3408         TRACE("Destination surface %p is onscreen.\n", dst_surface);
3409
3410         surface_translate_drawable_coords(dst_surface, context->win_handle, &dst_rect);
3411
3412         ENTER_GL();
3413         context_bind_fbo(context, GL_DRAW_FRAMEBUFFER, NULL);
3414         context_set_draw_buffer(context, buffer);
3415     }
3416     else
3417     {
3418         TRACE("Destination surface %p is offscreen.\n", dst_surface);
3419
3420         ENTER_GL();
3421         context_apply_fbo_state_blit(context, GL_DRAW_FRAMEBUFFER, dst_surface, NULL, dst_location);
3422         context_set_draw_buffer(context, GL_COLOR_ATTACHMENT0);
3423     }
3424     context_check_fbo_status(context, GL_DRAW_FRAMEBUFFER);
3425
3426     glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
3427     IWineD3DDeviceImpl_MarkStateDirty(device, STATE_RENDER(WINED3DRS_COLORWRITEENABLE));
3428     IWineD3DDeviceImpl_MarkStateDirty(device, STATE_RENDER(WINED3DRS_COLORWRITEENABLE1));
3429     IWineD3DDeviceImpl_MarkStateDirty(device, STATE_RENDER(WINED3DRS_COLORWRITEENABLE2));
3430     IWineD3DDeviceImpl_MarkStateDirty(device, STATE_RENDER(WINED3DRS_COLORWRITEENABLE3));
3431
3432     glDisable(GL_SCISSOR_TEST);
3433     IWineD3DDeviceImpl_MarkStateDirty(device, STATE_RENDER(WINED3DRS_SCISSORTESTENABLE));
3434
3435     gl_info->fbo_ops.glBlitFramebuffer(src_rect.left, src_rect.top, src_rect.right, src_rect.bottom,
3436             dst_rect.left, dst_rect.top, dst_rect.right, dst_rect.bottom, GL_COLOR_BUFFER_BIT, gl_filter);
3437     checkGLcall("glBlitFramebuffer()");
3438
3439     LEAVE_GL();
3440
3441     if (wined3d_settings.strict_draw_ordering
3442             || (dst_location == SFLAG_INDRAWABLE
3443             && dst_surface->container.u.swapchain->front_buffer == dst_surface))
3444         wglFlush();
3445
3446     context_release(context);
3447 }
3448
3449 static void wined3d_surface_depth_blt_fbo(IWineD3DDeviceImpl *device, IWineD3DSurfaceImpl *src_surface,
3450         const RECT *src_rect, IWineD3DSurfaceImpl *dst_surface, const RECT *dst_rect)
3451 {
3452     const struct wined3d_gl_info *gl_info;
3453     struct wined3d_context *context;
3454     DWORD src_mask, dst_mask;
3455     GLbitfield gl_mask;
3456
3457     TRACE("device %p, src_surface %p, src_rect %s, dst_surface %p, dst_rect %s.\n",
3458             device, src_surface, wine_dbgstr_rect(src_rect),
3459             dst_surface, wine_dbgstr_rect(dst_rect));
3460
3461     src_mask = src_surface->resource.format->flags & (WINED3DFMT_FLAG_DEPTH | WINED3DFMT_FLAG_STENCIL);
3462     dst_mask = dst_surface->resource.format->flags & (WINED3DFMT_FLAG_DEPTH | WINED3DFMT_FLAG_STENCIL);
3463
3464     if (src_mask != dst_mask)
3465     {
3466         ERR("Incompatible formats %s and %s.\n",
3467                 debug_d3dformat(src_surface->resource.format->id),
3468                 debug_d3dformat(dst_surface->resource.format->id));
3469         return;
3470     }
3471
3472     if (!src_mask)
3473     {
3474         ERR("Not a depth / stencil format: %s.\n",
3475                 debug_d3dformat(src_surface->resource.format->id));
3476         return;
3477     }
3478
3479     gl_mask = 0;
3480     if (src_mask & WINED3DFMT_FLAG_DEPTH)
3481         gl_mask |= GL_DEPTH_BUFFER_BIT;
3482     if (src_mask & WINED3DFMT_FLAG_STENCIL)
3483         gl_mask |= GL_STENCIL_BUFFER_BIT;
3484
3485     /* Make sure the locations are up-to-date. Loading the destination
3486      * surface isn't required if the entire surface is overwritten. */
3487     surface_load_location(src_surface, SFLAG_INTEXTURE, NULL);
3488     if (!surface_is_full_rect(dst_surface, dst_rect))
3489         surface_load_location(dst_surface, SFLAG_INTEXTURE, NULL);
3490
3491     context = context_acquire(device, NULL);
3492     if (!context->valid)
3493     {
3494         context_release(context);
3495         WARN("Invalid context, skipping blit.\n");
3496         return;
3497     }
3498
3499     gl_info = context->gl_info;
3500
3501     ENTER_GL();
3502
3503     context_apply_fbo_state_blit(context, GL_READ_FRAMEBUFFER, NULL, src_surface, SFLAG_INTEXTURE);
3504     glReadBuffer(GL_NONE);
3505     checkGLcall("glReadBuffer()");
3506     context_check_fbo_status(context, GL_READ_FRAMEBUFFER);
3507
3508     context_apply_fbo_state_blit(context, GL_DRAW_FRAMEBUFFER, NULL, dst_surface, SFLAG_INTEXTURE);
3509     context_set_draw_buffer(context, GL_NONE);
3510     context_check_fbo_status(context, GL_DRAW_FRAMEBUFFER);
3511
3512     if (gl_mask & GL_DEPTH_BUFFER_BIT)
3513     {
3514         glDepthMask(GL_TRUE);
3515         IWineD3DDeviceImpl_MarkStateDirty(device, STATE_RENDER(WINED3DRS_ZWRITEENABLE));
3516     }
3517     if (gl_mask & GL_STENCIL_BUFFER_BIT)
3518     {
3519         if (context->gl_info->supported[EXT_STENCIL_TWO_SIDE])
3520         {
3521             glDisable(GL_STENCIL_TEST_TWO_SIDE_EXT);
3522             IWineD3DDeviceImpl_MarkStateDirty(device, STATE_RENDER(WINED3DRS_TWOSIDEDSTENCILMODE));
3523         }
3524         glStencilMask(~0U);
3525         IWineD3DDeviceImpl_MarkStateDirty(device, STATE_RENDER(WINED3DRS_STENCILWRITEMASK));
3526     }
3527
3528     glDisable(GL_SCISSOR_TEST);
3529     IWineD3DDeviceImpl_MarkStateDirty(device, STATE_RENDER(WINED3DRS_SCISSORTESTENABLE));
3530
3531     gl_info->fbo_ops.glBlitFramebuffer(src_rect->left, src_rect->top, src_rect->right, src_rect->bottom,
3532             dst_rect->left, dst_rect->top, dst_rect->right, dst_rect->bottom, gl_mask, GL_NEAREST);
3533     checkGLcall("glBlitFramebuffer()");
3534
3535     LEAVE_GL();
3536
3537     if (wined3d_settings.strict_draw_ordering)
3538         wglFlush(); /* Flush to ensure ordering across contexts. */
3539
3540     context_release(context);
3541 }
3542
3543 static void surface_blt_to_drawable(IWineD3DDeviceImpl *device,
3544         WINED3DTEXTUREFILTERTYPE filter, BOOL color_key,
3545         IWineD3DSurfaceImpl *src_surface, const RECT *src_rect_in,
3546         IWineD3DSurfaceImpl *dst_surface, const RECT *dst_rect_in)
3547 {
3548     struct wined3d_context *context;
3549     RECT src_rect, dst_rect;
3550
3551     src_rect = *src_rect_in;
3552     dst_rect = *dst_rect_in;
3553
3554     /* Make sure the surface is up-to-date. This should probably use
3555      * surface_load_location() and worry about the destination surface too,
3556      * unless we're overwriting it completely. */
3557     surface_internal_preload(src_surface, SRGB_RGB);
3558
3559     /* Activate the destination context, set it up for blitting */
3560     context = context_acquire(device, dst_surface);
3561     context_apply_blit_state(context, device);
3562
3563     if (!surface_is_offscreen(dst_surface))
3564         surface_translate_drawable_coords(dst_surface, context->win_handle, &dst_rect);
3565
3566     device->blitter->set_shader(device->blit_priv, context->gl_info, src_surface);
3567
3568     ENTER_GL();
3569
3570     if (color_key)
3571     {
3572         glEnable(GL_ALPHA_TEST);
3573         checkGLcall("glEnable(GL_ALPHA_TEST)");
3574
3575         /* When the primary render target uses P8, the alpha component
3576          * contains the palette index. Which means that the colorkey is one of
3577          * the palette entries. In other cases pixels that should be masked
3578          * away have alpha set to 0. */
3579         if (primary_render_target_is_p8(device))
3580             glAlphaFunc(GL_NOTEQUAL, (float)src_surface->SrcBltCKey.dwColorSpaceLowValue / 256.0f);
3581         else
3582             glAlphaFunc(GL_NOTEQUAL, 0.0f);
3583         checkGLcall("glAlphaFunc");
3584     }
3585     else
3586     {
3587         glDisable(GL_ALPHA_TEST);
3588         checkGLcall("glDisable(GL_ALPHA_TEST)");
3589     }
3590
3591     draw_textured_quad(src_surface, &src_rect, &dst_rect, filter);
3592
3593     if (color_key)
3594     {
3595         glDisable(GL_ALPHA_TEST);
3596         checkGLcall("glDisable(GL_ALPHA_TEST)");
3597     }
3598
3599     LEAVE_GL();
3600
3601     /* Leave the opengl state valid for blitting */
3602     device->blitter->unset_shader(context->gl_info);
3603
3604     if (wined3d_settings.strict_draw_ordering
3605             || (dst_surface->container.type == WINED3D_CONTAINER_SWAPCHAIN
3606             && (dst_surface->container.u.swapchain->front_buffer == dst_surface)))
3607         wglFlush(); /* Flush to ensure ordering across contexts. */
3608
3609     context_release(context);
3610 }
3611
3612 /* Do not call while under the GL lock. */
3613 HRESULT surface_color_fill(IWineD3DSurfaceImpl *s, const RECT *rect, const WINED3DCOLORVALUE *color)
3614 {
3615     IWineD3DDeviceImpl *device = s->resource.device;
3616     const struct blit_shader *blitter;
3617
3618     blitter = wined3d_select_blitter(&device->adapter->gl_info, WINED3D_BLIT_OP_COLOR_FILL,
3619             NULL, 0, 0, NULL, rect, s->resource.usage, s->resource.pool, s->resource.format);
3620     if (!blitter)
3621     {
3622         FIXME("No blitter is capable of performing the requested color fill operation.\n");
3623         return WINED3DERR_INVALIDCALL;
3624     }
3625
3626     return blitter->color_fill(device, s, rect, color);
3627 }
3628
3629 /* Not called from the VTable */
3630 /* Do not call while under the GL lock. */
3631 static HRESULT IWineD3DSurfaceImpl_BltOverride(IWineD3DSurfaceImpl *dst_surface, const RECT *DestRect,
3632         IWineD3DSurfaceImpl *src_surface, const RECT *SrcRect, DWORD flags, const WINEDDBLTFX *DDBltFx,
3633         WINED3DTEXTUREFILTERTYPE Filter)
3634 {
3635     IWineD3DDeviceImpl *device = dst_surface->resource.device;
3636     const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
3637     struct wined3d_swapchain *srcSwapchain = NULL, *dstSwapchain = NULL;
3638     RECT dst_rect, src_rect;
3639
3640     TRACE("dst_surface %p, dst_rect %s, src_surface %p, src_rect %s, flags %#x, blt_fx %p, filter %s.\n",
3641             dst_surface, wine_dbgstr_rect(DestRect), src_surface, wine_dbgstr_rect(SrcRect),
3642             flags, DDBltFx, debug_d3dtexturefiltertype(Filter));
3643
3644     /* Get the swapchain. One of the surfaces has to be a primary surface */
3645     if (dst_surface->resource.pool == WINED3DPOOL_SYSTEMMEM)
3646     {
3647         WARN("Destination is in sysmem, rejecting gl blt\n");
3648         return WINED3DERR_INVALIDCALL;
3649     }
3650
3651     if (dst_surface->container.type == WINED3D_CONTAINER_SWAPCHAIN)
3652         dstSwapchain = dst_surface->container.u.swapchain;
3653
3654     if (src_surface)
3655     {
3656         if (src_surface->resource.pool == WINED3DPOOL_SYSTEMMEM)
3657         {
3658             WARN("Src is in sysmem, rejecting gl blt\n");
3659             return WINED3DERR_INVALIDCALL;
3660         }
3661
3662         if (src_surface->container.type == WINED3D_CONTAINER_SWAPCHAIN)
3663             srcSwapchain = src_surface->container.u.swapchain;
3664     }
3665
3666     /* Early sort out of cases where no render target is used */
3667     if (!dstSwapchain && !srcSwapchain
3668             && src_surface != device->render_targets[0]
3669             && dst_surface != device->render_targets[0])
3670     {
3671         TRACE("No surface is render target, not using hardware blit.\n");
3672         return WINED3DERR_INVALIDCALL;
3673     }
3674
3675     /* No destination color keying supported */
3676     if (flags & (WINEDDBLT_KEYDEST | WINEDDBLT_KEYDESTOVERRIDE))
3677     {
3678         /* Can we support that with glBlendFunc if blitting to the frame buffer? */
3679         TRACE("Destination color key not supported in accelerated Blit, falling back to software\n");
3680         return WINED3DERR_INVALIDCALL;
3681     }
3682
3683     surface_get_rect(dst_surface, DestRect, &dst_rect);
3684     if (src_surface) surface_get_rect(src_surface, SrcRect, &src_rect);
3685
3686     /* The only case where both surfaces on a swapchain are supported is a back buffer -> front buffer blit on the same swapchain */
3687     if (dstSwapchain && dstSwapchain == srcSwapchain && dstSwapchain->back_buffers
3688             && dst_surface == dstSwapchain->front_buffer
3689             && src_surface == dstSwapchain->back_buffers[0])
3690     {
3691         /* Half-Life does a Blt from the back buffer to the front buffer,
3692          * Full surface size, no flags... Use present instead
3693          *
3694          * This path will only be entered for d3d7 and ddraw apps, because d3d8/9 offer no way to blit TO the front buffer
3695          */
3696
3697         /* Check rects - IWineD3DDevice_Present doesn't handle them */
3698         while(1)
3699         {
3700             TRACE("Looking if a Present can be done...\n");
3701             /* Source Rectangle must be full surface */
3702             if (src_rect.left || src_rect.top
3703                     || src_rect.right != src_surface->resource.width
3704                     || src_rect.bottom != src_surface->resource.height)
3705             {
3706                 TRACE("No, Source rectangle doesn't match\n");
3707                 break;
3708             }
3709
3710             /* No stretching may occur */
3711             if(src_rect.right != dst_rect.right - dst_rect.left ||
3712                src_rect.bottom != dst_rect.bottom - dst_rect.top) {
3713                 TRACE("No, stretching is done\n");
3714                 break;
3715             }
3716
3717             /* Destination must be full surface or match the clipping rectangle */
3718             if (dst_surface->clipper && dst_surface->clipper->hWnd)
3719             {
3720                 RECT cliprect;
3721                 POINT pos[2];
3722                 GetClientRect(dst_surface->clipper->hWnd, &cliprect);
3723                 pos[0].x = dst_rect.left;
3724                 pos[0].y = dst_rect.top;
3725                 pos[1].x = dst_rect.right;
3726                 pos[1].y = dst_rect.bottom;
3727                 MapWindowPoints(GetDesktopWindow(), dst_surface->clipper->hWnd, pos, 2);
3728
3729                 if(pos[0].x != cliprect.left  || pos[0].y != cliprect.top   ||
3730                    pos[1].x != cliprect.right || pos[1].y != cliprect.bottom)
3731                 {
3732                     TRACE("No, dest rectangle doesn't match(clipper)\n");
3733                     TRACE("Clip rect at %s\n", wine_dbgstr_rect(&cliprect));
3734                     TRACE("Blt dest: %s\n", wine_dbgstr_rect(&dst_rect));
3735                     break;
3736                 }
3737             }
3738             else if (dst_rect.left || dst_rect.top
3739                     || dst_rect.right != dst_surface->resource.width
3740                     || dst_rect.bottom != dst_surface->resource.height)
3741             {
3742                 TRACE("No, dest rectangle doesn't match(surface size)\n");
3743                 break;
3744             }
3745
3746             TRACE("Yes\n");
3747
3748             /* These flags are unimportant for the flag check, remove them */
3749             if (!(flags & ~(WINEDDBLT_DONOTWAIT | WINEDDBLT_WAIT)))
3750             {
3751                 WINED3DSWAPEFFECT orig_swap = dstSwapchain->presentParms.SwapEffect;
3752
3753                 /* The idea behind this is that a glReadPixels and a glDrawPixels call
3754                     * take very long, while a flip is fast.
3755                     * This applies to Half-Life, which does such Blts every time it finished
3756                     * a frame, and to Prince of Persia 3D, which uses this to draw at least the main
3757                     * menu. This is also used by all apps when they do windowed rendering
3758                     *
3759                     * The problem is that flipping is not really the same as copying. After a
3760                     * Blt the front buffer is a copy of the back buffer, and the back buffer is
3761                     * untouched. Therefore it's necessary to override the swap effect
3762                     * and to set it back after the flip.
3763                     *
3764                     * Windowed Direct3D < 7 apps do the same. The D3D7 sdk demos are nice
3765                     * testcases.
3766                     */
3767
3768                 dstSwapchain->presentParms.SwapEffect = WINED3DSWAPEFFECT_COPY;
3769                 dstSwapchain->presentParms.PresentationInterval = WINED3DPRESENT_INTERVAL_IMMEDIATE;
3770
3771                 TRACE("Full screen back buffer -> front buffer blt, performing a flip instead.\n");
3772                 wined3d_swapchain_present(dstSwapchain, NULL, NULL, dstSwapchain->win_handle, NULL, 0);
3773
3774                 dstSwapchain->presentParms.SwapEffect = orig_swap;
3775
3776                 return WINED3D_OK;
3777             }
3778             break;
3779         }
3780
3781         TRACE("Unsupported blit between buffers on the same swapchain\n");
3782         return WINED3DERR_INVALIDCALL;
3783     } else if(dstSwapchain && dstSwapchain == srcSwapchain) {
3784         FIXME("Implement hardware blit between two surfaces on the same swapchain\n");
3785         return WINED3DERR_INVALIDCALL;
3786     } else if(dstSwapchain && srcSwapchain) {
3787         FIXME("Implement hardware blit between two different swapchains\n");
3788         return WINED3DERR_INVALIDCALL;
3789     }
3790     else if (dstSwapchain)
3791     {
3792         /* Handled with regular texture -> swapchain blit */
3793         if (src_surface == device->render_targets[0])
3794             TRACE("Blit from active render target to a swapchain\n");
3795     }
3796     else if (srcSwapchain && dst_surface == device->render_targets[0])
3797     {
3798         FIXME("Implement blit from a swapchain to the active render target\n");
3799         return WINED3DERR_INVALIDCALL;
3800     }
3801
3802     if ((srcSwapchain || src_surface == device->render_targets[0]) && !dstSwapchain)
3803     {
3804         /* Blit from render target to texture */
3805         BOOL stretchx;
3806
3807         /* P8 read back is not implemented */
3808         if (src_surface->resource.format->id == WINED3DFMT_P8_UINT
3809                 || dst_surface->resource.format->id == WINED3DFMT_P8_UINT)
3810         {
3811             TRACE("P8 read back not supported by frame buffer to texture blit\n");
3812             return WINED3DERR_INVALIDCALL;
3813         }
3814
3815         if (flags & (WINEDDBLT_KEYSRC | WINEDDBLT_KEYSRCOVERRIDE))
3816         {
3817             TRACE("Color keying not supported by frame buffer to texture blit\n");
3818             return WINED3DERR_INVALIDCALL;
3819             /* Destination color key is checked above */
3820         }
3821
3822         if(dst_rect.right - dst_rect.left != src_rect.right - src_rect.left) {
3823             stretchx = TRUE;
3824         } else {
3825             stretchx = FALSE;
3826         }
3827
3828         /* Blt is a pretty powerful call, while glCopyTexSubImage2D is not. glCopyTexSubImage cannot
3829          * flip the image nor scale it.
3830          *
3831          * -> If the app asks for a unscaled, upside down copy, just perform one glCopyTexSubImage2D call
3832          * -> If the app wants a image width an unscaled width, copy it line per line
3833          * -> If the app wants a image that is scaled on the x axis, and the destination rectangle is smaller
3834          *    than the frame buffer, draw an upside down scaled image onto the fb, read it back and restore the
3835          *    back buffer. This is slower than reading line per line, thus not used for flipping
3836          * -> If the app wants a scaled image with a dest rect that is bigger than the fb, it has to be copied
3837          *    pixel by pixel
3838          *
3839          * If EXT_framebuffer_blit is supported that can be used instead. Note that EXT_framebuffer_blit implies
3840          * FBO support, so it doesn't really make sense to try and make it work with different offscreen rendering
3841          * backends. */
3842         if (fbo_blit_supported(gl_info, WINED3D_BLIT_OP_COLOR_BLIT,
3843                 &src_rect, src_surface->resource.usage, src_surface->resource.pool, src_surface->resource.format,
3844                 &dst_rect, dst_surface->resource.usage, dst_surface->resource.pool, dst_surface->resource.format))
3845         {
3846             surface_blt_fbo(device, Filter,
3847                     src_surface, SFLAG_INDRAWABLE, &src_rect,
3848                     dst_surface, SFLAG_INDRAWABLE, &dst_rect);
3849             surface_modify_location(dst_surface, SFLAG_INDRAWABLE, TRUE);
3850         }
3851         else if (!stretchx || dst_rect.right - dst_rect.left > src_surface->resource.width
3852                 || dst_rect.bottom - dst_rect.top > src_surface->resource.height)
3853         {
3854             TRACE("No stretching in x direction, using direct framebuffer -> texture copy\n");
3855             fb_copy_to_texture_direct(dst_surface, src_surface, &src_rect, &dst_rect, Filter);
3856         } else {
3857             TRACE("Using hardware stretching to flip / stretch the texture\n");
3858             fb_copy_to_texture_hwstretch(dst_surface, src_surface, &src_rect, &dst_rect, Filter);
3859         }
3860
3861         if (!(dst_surface->flags & SFLAG_DONOTFREE))
3862         {
3863             HeapFree(GetProcessHeap(), 0, dst_surface->resource.heapMemory);
3864             dst_surface->resource.allocatedMemory = NULL;
3865             dst_surface->resource.heapMemory = NULL;
3866         }
3867         else
3868         {
3869             dst_surface->flags &= ~SFLAG_INSYSMEM;
3870         }
3871
3872         return WINED3D_OK;
3873     }
3874     else if (src_surface)
3875     {
3876         /* Blit from offscreen surface to render target */
3877         DWORD oldCKeyFlags = src_surface->CKeyFlags;
3878         WINEDDCOLORKEY oldBltCKey = src_surface->SrcBltCKey;
3879
3880         TRACE("Blt from surface %p to rendertarget %p\n", src_surface, dst_surface);
3881
3882         if (!(flags & (WINEDDBLT_KEYSRC | WINEDDBLT_KEYSRCOVERRIDE))
3883                 && fbo_blit_supported(gl_info, WINED3D_BLIT_OP_COLOR_BLIT,
3884                         &src_rect, src_surface->resource.usage, src_surface->resource.pool,
3885                         src_surface->resource.format,
3886                         &dst_rect, dst_surface->resource.usage, dst_surface->resource.pool,
3887                         dst_surface->resource.format))
3888         {
3889             TRACE("Using surface_blt_fbo.\n");
3890             /* The source is always a texture, but never the currently active render target, and the texture
3891              * contents are never upside down. */
3892             surface_blt_fbo(device, Filter,
3893                     src_surface, SFLAG_INDRAWABLE, &src_rect,
3894                     dst_surface, SFLAG_INDRAWABLE, &dst_rect);
3895             surface_modify_location(dst_surface, SFLAG_INDRAWABLE, TRUE);
3896             return WINED3D_OK;
3897         }
3898
3899         if (!(flags & (WINEDDBLT_KEYSRC | WINEDDBLT_KEYSRCOVERRIDE))
3900                 && arbfp_blit.blit_supported(gl_info, WINED3D_BLIT_OP_COLOR_BLIT,
3901                         &src_rect, src_surface->resource.usage, src_surface->resource.pool,
3902                         src_surface->resource.format,
3903                         &dst_rect, dst_surface->resource.usage, dst_surface->resource.pool,
3904                         dst_surface->resource.format))
3905         {
3906             return arbfp_blit_surface(device, src_surface, &src_rect, dst_surface, &dst_rect,
3907                     WINED3D_BLIT_OP_COLOR_BLIT, Filter);
3908         }
3909
3910         if (!device->blitter->blit_supported(gl_info, WINED3D_BLIT_OP_COLOR_BLIT,
3911                 &src_rect, src_surface->resource.usage, src_surface->resource.pool, src_surface->resource.format,
3912                 &dst_rect, dst_surface->resource.usage, dst_surface->resource.pool, dst_surface->resource.format))
3913         {
3914             FIXME("Unsupported blit operation falling back to software\n");
3915             return WINED3DERR_INVALIDCALL;
3916         }
3917
3918         /* Color keying: Check if we have to do a color keyed blt,
3919          * and if not check if a color key is activated.
3920          *
3921          * Just modify the color keying parameters in the surface and restore them afterwards
3922          * The surface keeps track of the color key last used to load the opengl surface.
3923          * PreLoad will catch the change to the flags and color key and reload if necessary.
3924          */
3925         if (flags & WINEDDBLT_KEYSRC)
3926         {
3927             /* Use color key from surface */
3928         }
3929         else if (flags & WINEDDBLT_KEYSRCOVERRIDE)
3930         {
3931             /* Use color key from DDBltFx */
3932             src_surface->CKeyFlags |= WINEDDSD_CKSRCBLT;
3933             src_surface->SrcBltCKey = DDBltFx->ddckSrcColorkey;
3934         }
3935         else
3936         {
3937             /* Do not use color key */
3938             src_surface->CKeyFlags &= ~WINEDDSD_CKSRCBLT;
3939         }
3940
3941         surface_blt_to_drawable(device, Filter, flags & (WINEDDBLT_KEYSRC | WINEDDBLT_KEYSRCOVERRIDE),
3942                 src_surface, &src_rect, dst_surface, &dst_rect);
3943
3944         /* Restore the color key parameters */
3945         src_surface->CKeyFlags = oldCKeyFlags;
3946         src_surface->SrcBltCKey = oldBltCKey;
3947
3948         surface_modify_location(dst_surface, SFLAG_INDRAWABLE, TRUE);
3949
3950         return WINED3D_OK;
3951     }
3952     else
3953     {
3954         /* Source-Less Blit to render target */
3955         if (flags & WINEDDBLT_COLORFILL)
3956         {
3957             WINED3DCOLORVALUE color;
3958
3959             TRACE("Colorfill\n");
3960
3961             /* The color as given in the Blt function is in the surface format. */
3962             if (!surface_convert_color_to_float(dst_surface, DDBltFx->u5.dwFillColor, &color))
3963                 return WINED3DERR_INVALIDCALL;
3964
3965             return surface_color_fill(dst_surface, &dst_rect, &color);
3966         }
3967     }
3968
3969     /* Default: Fall back to the generic blt. Not an error, a TRACE is enough */
3970     TRACE("Didn't find any usable render target setup for hw blit, falling back to software\n");
3971     return WINED3DERR_INVALIDCALL;
3972 }
3973
3974 /* Do not call while under the GL lock. */
3975 static HRESULT wined3d_surface_depth_fill(IWineD3DSurfaceImpl *surface, const RECT *rect, float depth)
3976 {
3977     const struct wined3d_resource *resource = &surface->resource;
3978     IWineD3DDeviceImpl *device = resource->device;
3979     const struct blit_shader *blitter;
3980
3981     blitter = wined3d_select_blitter(&device->adapter->gl_info, WINED3D_BLIT_OP_DEPTH_FILL,
3982             NULL, 0, 0, NULL, rect, resource->usage, resource->pool, resource->format);
3983     if (!blitter)
3984     {
3985         FIXME("No blitter is capable of performing the requested depth fill operation.\n");
3986         return WINED3DERR_INVALIDCALL;
3987     }
3988
3989     return blitter->depth_fill(device, surface, rect, depth);
3990 }
3991
3992 static HRESULT wined3d_surface_depth_blt(IWineD3DSurfaceImpl *src_surface, const RECT *src_rect,
3993         IWineD3DSurfaceImpl *dst_surface, const RECT *dst_rect)
3994 {
3995     IWineD3DDeviceImpl *device = src_surface->resource.device;
3996
3997     if (!fbo_blit_supported(&device->adapter->gl_info, WINED3D_BLIT_OP_DEPTH_BLIT,
3998             src_rect, src_surface->resource.usage, src_surface->resource.pool, src_surface->resource.format,
3999             dst_rect, dst_surface->resource.usage, dst_surface->resource.pool, dst_surface->resource.format))
4000         return WINED3DERR_INVALIDCALL;
4001
4002     wined3d_surface_depth_blt_fbo(device, src_surface, src_rect, dst_surface, dst_rect);
4003
4004     surface_modify_ds_location(dst_surface, SFLAG_DS_OFFSCREEN,
4005             dst_surface->ds_current_size.cx, dst_surface->ds_current_size.cy);
4006     surface_modify_location(dst_surface, SFLAG_INDRAWABLE, TRUE);
4007
4008     return WINED3D_OK;
4009 }
4010
4011 /* Do not call while under the GL lock. */
4012 static HRESULT WINAPI IWineD3DSurfaceImpl_Blt(IWineD3DSurface *iface, const RECT *DestRect,
4013         IWineD3DSurface *src_surface, const RECT *SrcRect, DWORD flags,
4014         const WINEDDBLTFX *DDBltFx, WINED3DTEXTUREFILTERTYPE Filter)
4015 {
4016     IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
4017     IWineD3DSurfaceImpl *src = (IWineD3DSurfaceImpl *)src_surface;
4018     IWineD3DDeviceImpl *device = This->resource.device;
4019     DWORD src_ds_flags, dst_ds_flags;
4020
4021     TRACE("iface %p, dst_rect %s, src_surface %p, src_rect %s, flags %#x, fx %p, filter %s.\n",
4022             iface, wine_dbgstr_rect(DestRect), src_surface, wine_dbgstr_rect(SrcRect),
4023             flags, DDBltFx, debug_d3dtexturefiltertype(Filter));
4024     TRACE("Usage is %s.\n", debug_d3dusage(This->resource.usage));
4025
4026     if ((This->flags & SFLAG_LOCKED) || (src && (src->flags & SFLAG_LOCKED)))
4027     {
4028         WARN(" Surface is busy, returning DDERR_SURFACEBUSY\n");
4029         return WINEDDERR_SURFACEBUSY;
4030     }
4031
4032     dst_ds_flags = This->resource.format->flags & (WINED3DFMT_FLAG_DEPTH | WINED3DFMT_FLAG_STENCIL);
4033     if (src)
4034         src_ds_flags = src->resource.format->flags & (WINED3DFMT_FLAG_DEPTH | WINED3DFMT_FLAG_STENCIL);
4035     else
4036         src_ds_flags = 0;
4037
4038     if (src_ds_flags || dst_ds_flags)
4039     {
4040         if (flags & WINEDDBLT_DEPTHFILL)
4041         {
4042             float depth;
4043             RECT rect;
4044
4045             TRACE("Depth fill.\n");
4046
4047             surface_get_rect(This, DestRect, &rect);
4048
4049             if (!surface_convert_depth_to_float(This, DDBltFx->u5.dwFillDepth, &depth))
4050                 return WINED3DERR_INVALIDCALL;
4051
4052             if (SUCCEEDED(wined3d_surface_depth_fill(This, &rect, depth)))
4053                 return WINED3D_OK;
4054         }
4055         else
4056         {
4057             RECT src_rect, dst_rect;
4058
4059             /* Accessing depth / stencil surfaces is supposed to fail while in
4060              * a scene, except for fills, which seem to work. */
4061             if (device->inScene)
4062             {
4063                 WARN("Rejecting depth / stencil access while in scene.\n");
4064                 return WINED3DERR_INVALIDCALL;
4065             }
4066
4067             if (src_ds_flags != dst_ds_flags)
4068             {
4069                 WARN("Rejecting depth / stencil blit between incompatible formats.\n");
4070                 return WINED3DERR_INVALIDCALL;
4071             }
4072
4073             if (SrcRect && (SrcRect->top || SrcRect->left
4074                     || SrcRect->bottom != src->resource.height
4075                     || SrcRect->right != src->resource.width))
4076             {
4077                 WARN("Rejecting depth / stencil blit with invalid source rect %s.\n",
4078                         wine_dbgstr_rect(SrcRect));
4079                 return WINED3DERR_INVALIDCALL;
4080             }
4081
4082             if (DestRect && (DestRect->top || DestRect->left
4083                     || DestRect->bottom != This->resource.height
4084                     || DestRect->right != This->resource.width))
4085             {
4086                 WARN("Rejecting depth / stencil blit with invalid destination rect %s.\n",
4087                         wine_dbgstr_rect(SrcRect));
4088                 return WINED3DERR_INVALIDCALL;
4089             }
4090
4091             if (src->resource.height != This->resource.height
4092                     || src->resource.width != This->resource.width)
4093             {
4094                 WARN("Rejecting depth / stencil blit with mismatched surface sizes.\n");
4095                 return WINED3DERR_INVALIDCALL;
4096             }
4097
4098             surface_get_rect(src, SrcRect, &src_rect);
4099             surface_get_rect(This, DestRect, &dst_rect);
4100
4101             if (SUCCEEDED(wined3d_surface_depth_blt(src, &src_rect, This, &dst_rect)))
4102                 return WINED3D_OK;
4103         }
4104     }
4105
4106     /* Special cases for RenderTargets */
4107     if ((This->resource.usage & WINED3DUSAGE_RENDERTARGET)
4108             || (src && (src->resource.usage & WINED3DUSAGE_RENDERTARGET)))
4109     {
4110         if (SUCCEEDED(IWineD3DSurfaceImpl_BltOverride(This, DestRect, src, SrcRect, flags, DDBltFx, Filter)))
4111             return WINED3D_OK;
4112     }
4113
4114     /* For the rest call the X11 surface implementation.
4115      * For RenderTargets this should be implemented OpenGL accelerated in BltOverride,
4116      * other Blts are rather rare. */
4117     return IWineD3DBaseSurfaceImpl_Blt(iface, DestRect, src_surface, SrcRect, flags, DDBltFx, Filter);
4118 }
4119
4120 static HRESULT WINAPI IWineD3DSurfaceImpl_BltFast(IWineD3DSurface *iface, DWORD dstx, DWORD dsty,
4121         IWineD3DSurface *src_surface, const RECT *rsrc, DWORD trans)
4122 {
4123     IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *)iface;
4124     IWineD3DSurfaceImpl *src = (IWineD3DSurfaceImpl *)src_surface;
4125     IWineD3DDeviceImpl *device = This->resource.device;
4126
4127     TRACE("iface %p, dst_x %u, dst_y %u, src_surface %p, src_rect %s, flags %#x.\n",
4128             iface, dstx, dsty, src_surface, wine_dbgstr_rect(rsrc), trans);
4129
4130     if ((This->flags & SFLAG_LOCKED) || (src->flags & SFLAG_LOCKED))
4131     {
4132         WARN(" Surface is busy, returning DDERR_SURFACEBUSY\n");
4133         return WINEDDERR_SURFACEBUSY;
4134     }
4135
4136     if (device->inScene && (This == device->depth_stencil || src == device->depth_stencil))
4137     {
4138         TRACE("Attempt to access the depth stencil surface in a BeginScene / EndScene pair, returning WINED3DERR_INVALIDCALL\n");
4139         return WINED3DERR_INVALIDCALL;
4140     }
4141
4142     /* Special cases for RenderTargets */
4143     if ((This->resource.usage & WINED3DUSAGE_RENDERTARGET)
4144             || (src->resource.usage & WINED3DUSAGE_RENDERTARGET))
4145     {
4146
4147         RECT SrcRect, DstRect;
4148         DWORD flags = 0;
4149
4150         surface_get_rect(src, rsrc, &SrcRect);
4151
4152         DstRect.left = dstx;
4153         DstRect.top=dsty;
4154         DstRect.right = dstx + SrcRect.right - SrcRect.left;
4155         DstRect.bottom = dsty + SrcRect.bottom - SrcRect.top;
4156
4157         /* Convert BltFast flags into Btl ones because it is called from SurfaceImpl_Blt as well */
4158         if (trans & WINEDDBLTFAST_SRCCOLORKEY)
4159             flags |= WINEDDBLT_KEYSRC;
4160         if (trans & WINEDDBLTFAST_DESTCOLORKEY)
4161             flags |= WINEDDBLT_KEYDEST;
4162         if (trans & WINEDDBLTFAST_WAIT)
4163             flags |= WINEDDBLT_WAIT;
4164         if (trans & WINEDDBLTFAST_DONOTWAIT)
4165             flags |= WINEDDBLT_DONOTWAIT;
4166
4167         if (SUCCEEDED(IWineD3DSurfaceImpl_BltOverride(This,
4168                 &DstRect, src, &SrcRect, flags, NULL, WINED3DTEXF_POINT)))
4169             return WINED3D_OK;
4170     }
4171
4172     return IWineD3DBaseSurfaceImpl_BltFast(iface, dstx, dsty, src_surface, rsrc, trans);
4173 }
4174
4175 static HRESULT WINAPI IWineD3DSurfaceImpl_PrivateSetup(IWineD3DSurface *iface) {
4176     /** Check against the maximum texture sizes supported by the video card **/
4177     IWineD3DSurfaceImpl *This = (IWineD3DSurfaceImpl *) iface;
4178     const struct wined3d_gl_info *gl_info = &This->resource.device->adapter->gl_info;
4179     unsigned int pow2Width, pow2Height;
4180
4181     This->surface_ops = &surface_ops;
4182
4183     This->texture_name = 0;
4184     This->texture_target = GL_TEXTURE_2D;
4185
4186     /* Non-power2 support */
4187     if (gl_info->supported[ARB_TEXTURE_NON_POWER_OF_TWO] || gl_info->supported[WINED3D_GL_NORMALIZED_TEXRECT])
4188     {
4189         pow2Width = This->resource.width;
4190         pow2Height = This->resource.height;
4191     }
4192     else
4193     {
4194         /* Find the nearest pow2 match */
4195         pow2Width = pow2Height = 1;
4196         while (pow2Width < This->resource.width) pow2Width <<= 1;
4197         while (pow2Height < This->resource.height) pow2Height <<= 1;
4198     }
4199     This->pow2Width  = pow2Width;
4200     This->pow2Height = pow2Height;
4201
4202     if (pow2Width > This->resource.width || pow2Height > This->resource.height)
4203     {
4204         /* TODO: Add support for non power two compressed textures. */
4205         if (This->resource.format->flags & WINED3DFMT_FLAG_COMPRESSED)
4206         {
4207             FIXME("(%p) Compressed non-power-two textures are not supported w(%d) h(%d)\n",
4208                   This, This->resource.width, This->resource.height);
4209             return WINED3DERR_NOTAVAILABLE;
4210         }
4211     }
4212
4213     if (pow2Width != This->resource.width
4214             || pow2Height != This->resource.height)
4215     {
4216         This->flags |= SFLAG_NONPOW2;
4217     }
4218
4219     TRACE("%p\n", This);
4220     if ((This->pow2Width > gl_info->limits.texture_size || This->pow2Height > gl_info->limits.texture_size)
4221             && !(This->resource.usage & (WINED3DUSAGE_RENDERTARGET | WINED3DUSAGE_DEPTHSTENCIL)))
4222     {
4223         /* one of three options
4224         1: Do the same as we do with nonpow 2 and scale the texture, (any texture ops would require the texture to be scaled which is potentially slow)
4225         2: Set the texture to the maximum size (bad idea)
4226         3:    WARN and return WINED3DERR_NOTAVAILABLE;
4227         4: Create the surface, but allow it to be used only for DirectDraw Blts. Some apps(e.g. Swat 3) create textures with a Height of 16 and a Width > 3000 and blt 16x16 letter areas from them to the render target.
4228         */
4229         if(This->resource.pool == WINED3DPOOL_DEFAULT || This->resource.pool == WINED3DPOOL_MANAGED)
4230         {
4231             WARN("(%p) Unable to allocate a surface which exceeds the maximum OpenGL texture size\n", This);
4232             return WINED3DERR_NOTAVAILABLE;
4233         }
4234
4235         /* We should never use this surface in combination with OpenGL! */
4236         TRACE("(%p) Creating an oversized surface: %ux%u\n", This, This->pow2Width, This->pow2Height);
4237     }
4238     else
4239     {
4240         /* Don't use ARB_TEXTURE_RECTANGLE in case the surface format is P8 and EXT_PALETTED_TEXTURE
4241            is used in combination with texture uploads (RTL_READTEX/RTL_TEXTEX). The reason is that EXT_PALETTED_TEXTURE
4242            doesn't work in combination with ARB_TEXTURE_RECTANGLE.
4243         */
4244         if (This->flags & SFLAG_NONPOW2 && gl_info->supported[ARB_TEXTURE_RECTANGLE]
4245                 && !(This->resource.format->id == WINED3DFMT_P8_UINT
4246                 && gl_info->supported[EXT_PALETTED_TEXTURE]
4247                 && wined3d_settings.rendertargetlock_mode == RTL_READTEX))
4248         {
4249             This->texture_target = GL_TEXTURE_RECTANGLE_ARB;
4250             This->pow2Width  = This->resource.width;
4251             This->pow2Height = This->resource.height;
4252             This->flags &= ~(SFLAG_NONPOW2 | SFLAG_NORMCOORD);
4253         }
4254     }
4255
4256     switch (wined3d_settings.offscreen_rendering_mode)
4257     {
4258         case ORM_FBO:
4259             This->get_drawable_size = get_drawable_size_fbo;
4260             break;
4261
4262         case ORM_BACKBUFFER:
4263             This->get_drawable_size = get_drawable_size_backbuffer;
4264             break;
4265
4266         default:
4267             ERR("Unhandled offscreen rendering mode %#x.\n", wined3d_settings.offscreen_rendering_mode);
4268             return WINED3DERR_INVALIDCALL;
4269     }
4270
4271     This->flags |= SFLAG_INSYSMEM;
4272
4273     return WINED3D_OK;
4274 }
4275
4276 /* GL locking is done by the caller */
4277 static void surface_depth_blt(IWineD3DSurfaceImpl *This, const struct wined3d_gl_info *gl_info,
4278         GLuint texture, GLsizei w, GLsizei h, GLenum target)
4279 {
4280     IWineD3DDeviceImpl *device = This->resource.device;
4281     GLint compare_mode = GL_NONE;
4282     struct blt_info info;
4283     GLint old_binding = 0;
4284     RECT rect;
4285
4286     glPushAttrib(GL_ENABLE_BIT | GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT | GL_VIEWPORT_BIT);
4287
4288     glDisable(GL_CULL_FACE);
4289     glDisable(GL_BLEND);
4290     glDisable(GL_ALPHA_TEST);
4291     glDisable(GL_SCISSOR_TEST);
4292     glDisable(GL_STENCIL_TEST);
4293     glEnable(GL_DEPTH_TEST);
4294     glDepthFunc(GL_ALWAYS);
4295     glDepthMask(GL_TRUE);
4296     glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
4297     glViewport(0, This->pow2Height - h, w, h);
4298
4299     SetRect(&rect, 0, h, w, 0);
4300     surface_get_blt_info(target, &rect, This->pow2Width, This->pow2Height, &info);
4301     GL_EXTCALL(glActiveTextureARB(GL_TEXTURE0_ARB));
4302     glGetIntegerv(info.binding, &old_binding);
4303     glBindTexture(info.bind_target, texture);
4304     if (gl_info->supported[ARB_SHADOW])
4305     {
4306         glGetTexParameteriv(info.bind_target, GL_TEXTURE_COMPARE_MODE_ARB, &compare_mode);
4307         if (compare_mode != GL_NONE) glTexParameteri(info.bind_target, GL_TEXTURE_COMPARE_MODE_ARB, GL_NONE);
4308     }
4309
4310     device->shader_backend->shader_select_depth_blt(device->shader_priv,
4311             gl_info, info.tex_type, &This->ds_current_size);
4312
4313     glBegin(GL_TRIANGLE_STRIP);
4314     glTexCoord3fv(info.coords[0]);
4315     glVertex2f(-1.0f, -1.0f);
4316     glTexCoord3fv(info.coords[1]);
4317     glVertex2f(1.0f, -1.0f);
4318     glTexCoord3fv(info.coords[2]);
4319     glVertex2f(-1.0f, 1.0f);
4320     glTexCoord3fv(info.coords[3]);
4321     glVertex2f(1.0f, 1.0f);
4322     glEnd();
4323
4324     if (compare_mode != GL_NONE) glTexParameteri(info.bind_target, GL_TEXTURE_COMPARE_MODE_ARB, compare_mode);
4325     glBindTexture(info.bind_target, old_binding);
4326
4327     glPopAttrib();
4328
4329     device->shader_backend->shader_deselect_depth_blt(device->shader_priv, gl_info);
4330 }
4331
4332 void surface_modify_ds_location(IWineD3DSurfaceImpl *surface,
4333         DWORD location, UINT w, UINT h)
4334 {
4335     TRACE("surface %p, new location %#x, w %u, h %u.\n", surface, location, w, h);
4336
4337     if (location & ~SFLAG_DS_LOCATIONS)
4338         FIXME("Invalid location (%#x) specified.\n", location);
4339
4340     surface->ds_current_size.cx = w;
4341     surface->ds_current_size.cy = h;
4342     surface->flags &= ~SFLAG_DS_LOCATIONS;
4343     surface->flags |= location;
4344 }
4345
4346 /* Context activation is done by the caller. */
4347 void surface_load_ds_location(IWineD3DSurfaceImpl *surface, struct wined3d_context *context, DWORD location)
4348 {
4349     IWineD3DDeviceImpl *device = surface->resource.device;
4350     const struct wined3d_gl_info *gl_info = context->gl_info;
4351     GLsizei w, h;
4352
4353     TRACE("surface %p, new location %#x.\n", surface, location);
4354
4355     /* TODO: Make this work for modes other than FBO */
4356     if (wined3d_settings.offscreen_rendering_mode != ORM_FBO) return;
4357
4358     if (!(surface->flags & location))
4359     {
4360         w = surface->ds_current_size.cx;
4361         h = surface->ds_current_size.cy;
4362         surface->ds_current_size.cx = 0;
4363         surface->ds_current_size.cy = 0;
4364     }
4365     else
4366     {
4367         w = surface->resource.width;
4368         h = surface->resource.height;
4369     }
4370
4371     if (surface->ds_current_size.cx == surface->resource.width
4372             && surface->ds_current_size.cy == surface->resource.height)
4373     {
4374         TRACE("Location (%#x) is already up to date.\n", location);
4375         return;
4376     }
4377
4378     if (surface->current_renderbuffer)
4379     {
4380         FIXME("Not supported with fixed up depth stencil.\n");
4381         return;
4382     }
4383
4384     if (!(surface->flags & SFLAG_DS_LOCATIONS))
4385     {
4386         /* This mostly happens when a depth / stencil is used without being
4387          * cleared first. In principle we could upload from sysmem, or
4388          * explicitly clear before first usage. For the moment there don't
4389          * appear to be a lot of applications depending on this, so a FIXME
4390          * should do. */
4391         FIXME("No up to date depth stencil location.\n");
4392         surface->flags |= location;
4393         surface->ds_current_size.cx = surface->resource.width;
4394         surface->ds_current_size.cy = surface->resource.height;
4395         return;
4396     }
4397
4398     if (location == SFLAG_DS_OFFSCREEN)
4399     {
4400         GLint old_binding = 0;
4401         GLenum bind_target;
4402
4403         /* The render target is allowed to be smaller than the depth/stencil
4404          * buffer, so the onscreen depth/stencil buffer is potentially smaller
4405          * than the offscreen surface. Don't overwrite the offscreen surface
4406          * with undefined data. */
4407         w = min(w, context->swapchain->presentParms.BackBufferWidth);
4408         h = min(h, context->swapchain->presentParms.BackBufferHeight);
4409
4410         TRACE("Copying onscreen depth buffer to depth texture.\n");
4411
4412         ENTER_GL();
4413
4414         if (!device->depth_blt_texture)
4415         {
4416             glGenTextures(1, &device->depth_blt_texture);
4417         }
4418
4419         /* Note that we use depth_blt here as well, rather than glCopyTexImage2D
4420          * directly on the FBO texture. That's because we need to flip. */
4421         context_bind_fbo(context, GL_FRAMEBUFFER, NULL);
4422         if (surface->texture_target == GL_TEXTURE_RECTANGLE_ARB)
4423         {
4424             glGetIntegerv(GL_TEXTURE_BINDING_RECTANGLE_ARB, &old_binding);
4425             bind_target = GL_TEXTURE_RECTANGLE_ARB;
4426         }
4427         else
4428         {
4429             glGetIntegerv(GL_TEXTURE_BINDING_2D, &old_binding);
4430             bind_target = GL_TEXTURE_2D;
4431         }
4432         glBindTexture(bind_target, device->depth_blt_texture);
4433         glCopyTexImage2D(bind_target, surface->texture_level, surface->resource.format->glInternal, 0, 0, w, h, 0);
4434         glTexParameteri(bind_target, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
4435         glTexParameteri(bind_target, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
4436         glTexParameteri(bind_target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
4437         glTexParameteri(bind_target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
4438         glTexParameteri(bind_target, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
4439         glTexParameteri(bind_target, GL_DEPTH_TEXTURE_MODE_ARB, GL_LUMINANCE);
4440         glBindTexture(bind_target, old_binding);
4441
4442         /* Setup the destination */
4443         if (!device->depth_blt_rb)
4444         {
4445             gl_info->fbo_ops.glGenRenderbuffers(1, &device->depth_blt_rb);
4446             checkGLcall("glGenRenderbuffersEXT");
4447         }
4448         if (device->depth_blt_rb_w != w || device->depth_blt_rb_h != h)
4449         {
4450             gl_info->fbo_ops.glBindRenderbuffer(GL_RENDERBUFFER, device->depth_blt_rb);
4451             checkGLcall("glBindRenderbufferEXT");
4452             gl_info->fbo_ops.glRenderbufferStorage(GL_RENDERBUFFER, GL_RGBA8, w, h);
4453             checkGLcall("glRenderbufferStorageEXT");
4454             device->depth_blt_rb_w = w;
4455             device->depth_blt_rb_h = h;
4456         }
4457
4458         context_bind_fbo(context, GL_FRAMEBUFFER, &context->dst_fbo);
4459         gl_info->fbo_ops.glFramebufferRenderbuffer(GL_FRAMEBUFFER,
4460                 GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, device->depth_blt_rb);
4461         checkGLcall("glFramebufferRenderbufferEXT");
4462         context_attach_depth_stencil_fbo(context, GL_FRAMEBUFFER, surface, FALSE);
4463
4464         /* Do the actual blit */
4465         surface_depth_blt(surface, gl_info, device->depth_blt_texture, w, h, bind_target);
4466         checkGLcall("depth_blt");
4467
4468         if (context->current_fbo) context_bind_fbo(context, GL_FRAMEBUFFER, &context->current_fbo->id);
4469         else context_bind_fbo(context, GL_FRAMEBUFFER, NULL);
4470
4471         LEAVE_GL();
4472
4473         if (wined3d_settings.strict_draw_ordering) wglFlush(); /* Flush to ensure ordering across contexts. */
4474     }
4475     else if (location == SFLAG_DS_ONSCREEN)
4476     {
4477         TRACE("Copying depth texture to onscreen depth buffer.\n");
4478
4479         ENTER_GL();
4480
4481         context_bind_fbo(context, GL_FRAMEBUFFER, NULL);
4482         surface_depth_blt(surface, gl_info, surface->texture_name,
4483                 w, h, surface->texture_target);
4484         checkGLcall("depth_blt");
4485
4486         if (context->current_fbo) context_bind_fbo(context, GL_FRAMEBUFFER, &context->current_fbo->id);
4487
4488         LEAVE_GL();
4489
4490         if (wined3d_settings.strict_draw_ordering) wglFlush(); /* Flush to ensure ordering across contexts. */
4491     }
4492     else
4493     {
4494         ERR("Invalid location (%#x) specified.\n", location);
4495     }
4496
4497     surface->flags |= location;
4498     surface->ds_current_size.cx = surface->resource.width;
4499     surface->ds_current_size.cy = surface->resource.height;
4500 }
4501
4502 void surface_modify_location(IWineD3DSurfaceImpl *surface, DWORD flag, BOOL persistent)
4503 {
4504     const struct wined3d_gl_info *gl_info = &surface->resource.device->adapter->gl_info;
4505     IWineD3DSurfaceImpl *overlay;
4506
4507     TRACE("surface %p, location %s, persistent %#x.\n",
4508             surface, debug_surflocation(flag), persistent);
4509
4510     if (wined3d_settings.offscreen_rendering_mode == ORM_FBO)
4511     {
4512         if (surface_is_offscreen(surface))
4513         {
4514             /* With ORM_FBO, SFLAG_INTEXTURE and SFLAG_INDRAWABLE are the same for offscreen targets. */
4515             if (flag & (SFLAG_INTEXTURE | SFLAG_INDRAWABLE)) flag |= (SFLAG_INTEXTURE | SFLAG_INDRAWABLE);
4516         }
4517         else
4518         {
4519             TRACE("Surface %p is an onscreen surface.\n", surface);
4520         }
4521     }
4522
4523     if (flag & (SFLAG_INTEXTURE | SFLAG_INSRGBTEX)
4524             && gl_info->supported[EXT_TEXTURE_SRGB_DECODE])
4525     {
4526         flag |= (SFLAG_INTEXTURE | SFLAG_INSRGBTEX);
4527     }
4528
4529     if (persistent)
4530     {
4531         if (((surface->flags & SFLAG_INTEXTURE) && !(flag & SFLAG_INTEXTURE))
4532                 || ((surface->flags & SFLAG_INSRGBTEX) && !(flag & SFLAG_INSRGBTEX)))
4533         {
4534             if (surface->container.type == WINED3D_CONTAINER_TEXTURE)
4535             {
4536                 TRACE("Passing to container.\n");
4537                 wined3d_texture_set_dirty(surface->container.u.texture, TRUE);
4538             }
4539         }
4540         surface->flags &= ~SFLAG_LOCATIONS;
4541         surface->flags |= flag;
4542
4543         /* Redraw emulated overlays, if any */
4544         if (flag & SFLAG_INDRAWABLE && !list_empty(&surface->overlays))
4545         {
4546             LIST_FOR_EACH_ENTRY(overlay, &surface->overlays, IWineD3DSurfaceImpl, overlay_entry)
4547             {
4548                 overlay->surface_ops->surface_draw_overlay(overlay);
4549             }
4550         }
4551     }
4552     else
4553     {
4554         if ((surface->flags & (SFLAG_INTEXTURE | SFLAG_INSRGBTEX)) && (flag & (SFLAG_INTEXTURE | SFLAG_INSRGBTEX)))
4555         {
4556             if (surface->container.type == WINED3D_CONTAINER_TEXTURE)
4557             {
4558                 TRACE("Passing to container\n");
4559                 wined3d_texture_set_dirty(surface->container.u.texture, TRUE);
4560             }
4561         }
4562         surface->flags &= ~flag;
4563     }
4564
4565     if (!(surface->flags & SFLAG_LOCATIONS))
4566     {
4567         ERR("Surface %p does not have any up to date location.\n", surface);
4568     }
4569 }
4570
4571 HRESULT surface_load_location(IWineD3DSurfaceImpl *surface, DWORD flag, const RECT *rect)
4572 {
4573     IWineD3DDeviceImpl *device = surface->resource.device;
4574     const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
4575     BOOL drawable_read_ok = surface_is_offscreen(surface);
4576     struct wined3d_format format;
4577     CONVERT_TYPES convert;
4578     int width, pitch, outpitch;
4579     BYTE *mem;
4580     BOOL in_fbo = FALSE;
4581
4582     TRACE("surface %p, location %s, rect %s.\n", surface, debug_surflocation(flag), wine_dbgstr_rect(rect));
4583
4584     if (surface->resource.usage & WINED3DUSAGE_DEPTHSTENCIL)
4585     {
4586         if (flag == SFLAG_INTEXTURE)
4587         {
4588             struct wined3d_context *context = context_acquire(device, NULL);
4589             surface_load_ds_location(surface, context, SFLAG_DS_OFFSCREEN);
4590             context_release(context);
4591             return WINED3D_OK;
4592         }
4593         else
4594         {
4595             FIXME("Unimplemented location %s for depth/stencil buffers.\n", debug_surflocation(flag));
4596             return WINED3DERR_INVALIDCALL;
4597         }
4598     }
4599
4600     if (wined3d_settings.offscreen_rendering_mode == ORM_FBO)
4601     {
4602         if (surface_is_offscreen(surface))
4603         {
4604             /* With ORM_FBO, SFLAG_INTEXTURE and SFLAG_INDRAWABLE are the same for offscreen targets.
4605              * Prefer SFLAG_INTEXTURE. */
4606             if (flag == SFLAG_INDRAWABLE) flag = SFLAG_INTEXTURE;
4607             drawable_read_ok = FALSE;
4608             in_fbo = TRUE;
4609         }
4610         else
4611         {
4612             TRACE("Surface %p is an onscreen surface.\n", surface);
4613         }
4614     }
4615
4616     if (flag == SFLAG_INSRGBTEX && gl_info->supported[EXT_TEXTURE_SRGB_DECODE])
4617     {
4618         flag = SFLAG_INTEXTURE;
4619     }
4620
4621     if (surface->flags & flag)
4622     {
4623         TRACE("Location already up to date\n");
4624         return WINED3D_OK;
4625     }
4626
4627     if (!(surface->flags & SFLAG_LOCATIONS))
4628     {
4629         ERR("Surface %p does not have any up to date location.\n", surface);
4630         surface->flags |= SFLAG_LOST;
4631         return WINED3DERR_DEVICELOST;
4632     }
4633
4634     if (flag == SFLAG_INSYSMEM)
4635     {
4636         surface_prepare_system_memory(surface);
4637
4638         /* Download the surface to system memory */
4639         if (surface->flags & (SFLAG_INTEXTURE | SFLAG_INSRGBTEX))
4640         {
4641             struct wined3d_context *context = NULL;
4642
4643             if (!device->isInDraw) context = context_acquire(device, NULL);
4644
4645             surface_bind_and_dirtify(surface, gl_info, !(surface->flags & SFLAG_INTEXTURE));
4646             surface_download_data(surface, gl_info);
4647
4648             if (context) context_release(context);
4649         }
4650         else
4651         {
4652             /* Note: It might be faster to download into a texture first. */
4653             read_from_framebuffer(surface, rect, surface->resource.allocatedMemory,
4654                     IWineD3DSurface_GetPitch((IWineD3DSurface *)surface));
4655         }
4656     }
4657     else if (flag == SFLAG_INDRAWABLE)
4658     {
4659         if (wined3d_settings.rendertargetlock_mode == RTL_READTEX)
4660             surface_load_location(surface, SFLAG_INTEXTURE, NULL);
4661
4662         if (surface->flags & SFLAG_INTEXTURE)
4663         {
4664             RECT r;
4665
4666             surface_get_rect(surface, rect, &r);
4667             surface_blt_to_drawable(device, WINED3DTEXF_POINT, FALSE, surface, &r, surface, &r);
4668         }
4669         else
4670         {
4671             int byte_count;
4672             if ((surface->flags & SFLAG_LOCATIONS) == SFLAG_INSRGBTEX)
4673             {
4674                 /* This needs a shader to convert the srgb data sampled from the GL texture into RGB
4675                  * values, otherwise we get incorrect values in the target. For now go the slow way
4676                  * via a system memory copy
4677                  */
4678                 surface_load_location(surface, SFLAG_INSYSMEM, rect);
4679             }
4680
4681             d3dfmt_get_conv(surface, FALSE /* We need color keying */,
4682                     FALSE /* We won't use textures */, &format, &convert);
4683
4684             /* The width is in 'length' not in bytes */
4685             width = surface->resource.width;
4686             pitch = IWineD3DSurface_GetPitch((IWineD3DSurface *)surface);
4687
4688             /* Don't use PBOs for converted surfaces. During PBO conversion we look at SFLAG_CONVERTED
4689              * but it isn't set (yet) in all cases it is getting called. */
4690             if ((convert != NO_CONVERSION) && (surface->flags & SFLAG_PBO))
4691             {
4692                 struct wined3d_context *context = NULL;
4693
4694                 TRACE("Removing the pbo attached to surface %p.\n", surface);
4695
4696                 if (!device->isInDraw) context = context_acquire(device, NULL);
4697                 surface_remove_pbo(surface, gl_info);
4698                 if (context) context_release(context);
4699             }
4700
4701             if ((convert != NO_CONVERSION) && surface->resource.allocatedMemory)
4702             {
4703                 int height = surface->resource.height;
4704                 byte_count = format.conv_byte_count;
4705
4706                 /* Stick to the alignment for the converted surface too, makes it easier to load the surface */
4707                 outpitch = width * byte_count;
4708                 outpitch = (outpitch + device->surface_alignment - 1) & ~(device->surface_alignment - 1);
4709
4710                 mem = HeapAlloc(GetProcessHeap(), 0, outpitch * height);
4711                 if(!mem) {
4712                     ERR("Out of memory %d, %d!\n", outpitch, height);
4713                     return WINED3DERR_OUTOFVIDEOMEMORY;
4714                 }
4715                 d3dfmt_convert_surface(surface->resource.allocatedMemory, mem, pitch,
4716                         width, height, outpitch, convert, surface);
4717
4718                 surface->flags |= SFLAG_CONVERTED;
4719             }
4720             else
4721             {
4722                 surface->flags &= ~SFLAG_CONVERTED;
4723                 mem = surface->resource.allocatedMemory;
4724                 byte_count = format.byte_count;
4725             }
4726
4727             flush_to_framebuffer_drawpixels(surface, rect, format.glFormat, format.glType, byte_count, mem);
4728
4729             /* Don't delete PBO memory */
4730             if ((mem != surface->resource.allocatedMemory) && !(surface->flags & SFLAG_PBO))
4731                 HeapFree(GetProcessHeap(), 0, mem);
4732         }
4733     }
4734     else /* if(flag & (SFLAG_INTEXTURE | SFLAG_INSRGBTEX)) */
4735     {
4736         const DWORD attach_flags = WINED3DFMT_FLAG_FBO_ATTACHABLE | WINED3DFMT_FLAG_FBO_ATTACHABLE_SRGB;
4737
4738         if (drawable_read_ok && (surface->flags & SFLAG_INDRAWABLE))
4739         {
4740             read_from_framebuffer_texture(surface, flag == SFLAG_INSRGBTEX);
4741         }
4742         else if (surface->flags & (SFLAG_INSRGBTEX | SFLAG_INTEXTURE)
4743                 && (surface->resource.format->flags & attach_flags) == attach_flags
4744                 && fbo_blit_supported(gl_info, WINED3D_BLIT_OP_COLOR_BLIT,
4745                         NULL, surface->resource.usage, surface->resource.pool, surface->resource.format,
4746                         NULL, surface->resource.usage, surface->resource.pool, surface->resource.format))
4747         {
4748             DWORD src_location = flag == SFLAG_INSRGBTEX ? SFLAG_INTEXTURE : SFLAG_INSRGBTEX;
4749             RECT rect = {0, 0, surface->resource.width, surface->resource.height};
4750
4751             surface_blt_fbo(surface->resource.device, WINED3DTEXF_POINT,
4752                     surface, src_location, &rect, surface, flag, &rect);
4753         }
4754         else
4755         {
4756             /* Upload from system memory */
4757             BOOL srgb = flag == SFLAG_INSRGBTEX;
4758             struct wined3d_context *context = NULL;
4759
4760             d3dfmt_get_conv(surface, TRUE /* We need color keying */,
4761                     TRUE /* We will use textures */, &format, &convert);
4762
4763             if (srgb)
4764             {
4765                 if ((surface->flags & (SFLAG_INTEXTURE | SFLAG_INSYSMEM)) == SFLAG_INTEXTURE)
4766                 {
4767                     /* Performance warning... */
4768                     FIXME("Downloading RGB surface %p to reload it as sRGB.\n", surface);
4769                     surface_load_location(surface, SFLAG_INSYSMEM, rect);
4770                 }
4771             }
4772             else
4773             {
4774                 if ((surface->flags & (SFLAG_INSRGBTEX | SFLAG_INSYSMEM)) == SFLAG_INSRGBTEX)
4775                 {
4776                     /* Performance warning... */
4777                     FIXME("Downloading sRGB surface %p to reload it as RGB.\n", surface);
4778                     surface_load_location(surface, SFLAG_INSYSMEM, rect);
4779                 }
4780             }
4781             if (!(surface->flags & SFLAG_INSYSMEM))
4782             {
4783                 WARN("Trying to load a texture from sysmem, but SFLAG_INSYSMEM is not set.\n");
4784                 /* Lets hope we get it from somewhere... */
4785                 surface_load_location(surface, SFLAG_INSYSMEM, rect);
4786             }
4787
4788             if (!device->isInDraw) context = context_acquire(device, NULL);
4789
4790             surface_prepare_texture(surface, gl_info, srgb);
4791             surface_bind_and_dirtify(surface, gl_info, srgb);
4792
4793             if (surface->CKeyFlags & WINEDDSD_CKSRCBLT)
4794             {
4795                 surface->flags |= SFLAG_GLCKEY;
4796                 surface->glCKey = surface->SrcBltCKey;
4797             }
4798             else surface->flags &= ~SFLAG_GLCKEY;
4799
4800             /* The width is in 'length' not in bytes */
4801             width = surface->resource.width;
4802             pitch = IWineD3DSurface_GetPitch((IWineD3DSurface *)surface);
4803
4804             /* Don't use PBOs for converted surfaces. During PBO conversion we look at SFLAG_CONVERTED
4805              * but it isn't set (yet) in all cases it is getting called. */
4806             if ((convert != NO_CONVERSION || format.convert) && (surface->flags & SFLAG_PBO))
4807             {
4808                 TRACE("Removing the pbo attached to surface %p.\n", surface);
4809                 surface_remove_pbo(surface, gl_info);
4810             }
4811
4812             if (format.convert)
4813             {
4814                 /* This code is entered for texture formats which need a fixup. */
4815                 UINT height = surface->resource.height;
4816
4817                 /* Stick to the alignment for the converted surface too, makes it easier to load the surface */
4818                 outpitch = width * format.conv_byte_count;
4819                 outpitch = (outpitch + device->surface_alignment - 1) & ~(device->surface_alignment - 1);
4820
4821                 mem = HeapAlloc(GetProcessHeap(), 0, outpitch * height);
4822                 if(!mem) {
4823                     ERR("Out of memory %d, %d!\n", outpitch, height);
4824                     if (context) context_release(context);
4825                     return WINED3DERR_OUTOFVIDEOMEMORY;
4826                 }
4827                 format.convert(surface->resource.allocatedMemory, mem, pitch, width, height);
4828             }
4829             else if (convert != NO_CONVERSION && surface->resource.allocatedMemory)
4830             {
4831                 /* This code is only entered for color keying fixups */
4832                 UINT height = surface->resource.height;
4833
4834                 /* Stick to the alignment for the converted surface too, makes it easier to load the surface */
4835                 outpitch = width * format.conv_byte_count;
4836                 outpitch = (outpitch + device->surface_alignment - 1) & ~(device->surface_alignment - 1);
4837
4838                 mem = HeapAlloc(GetProcessHeap(), 0, outpitch * height);
4839                 if(!mem) {
4840                     ERR("Out of memory %d, %d!\n", outpitch, height);
4841                     if (context) context_release(context);
4842                     return WINED3DERR_OUTOFVIDEOMEMORY;
4843                 }
4844                 d3dfmt_convert_surface(surface->resource.allocatedMemory, mem, pitch,
4845                         width, height, outpitch, convert, surface);
4846             }
4847             else
4848             {
4849                 mem = surface->resource.allocatedMemory;
4850             }
4851
4852             /* Make sure the correct pitch is used */
4853             ENTER_GL();
4854             glPixelStorei(GL_UNPACK_ROW_LENGTH, width);
4855             LEAVE_GL();
4856
4857             if (mem || (surface->flags & SFLAG_PBO))
4858                 surface_upload_data(surface, gl_info, &format, srgb, mem);
4859
4860             /* Restore the default pitch */
4861             ENTER_GL();
4862             glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
4863             LEAVE_GL();
4864
4865             if (context) context_release(context);
4866
4867             /* Don't delete PBO memory */
4868             if ((mem != surface->resource.allocatedMemory) && !(surface->flags & SFLAG_PBO))
4869                 HeapFree(GetProcessHeap(), 0, mem);
4870         }
4871     }
4872
4873     if (!rect)
4874     {
4875         surface->flags |= flag;
4876
4877         if (flag != SFLAG_INSYSMEM && (surface->flags & SFLAG_INSYSMEM))
4878             surface_evict_sysmem(surface);
4879     }
4880
4881     if (in_fbo && (surface->flags & (SFLAG_INTEXTURE | SFLAG_INDRAWABLE)))
4882     {
4883         /* With ORM_FBO, SFLAG_INTEXTURE and SFLAG_INDRAWABLE are the same for offscreen targets. */
4884         surface->flags |= (SFLAG_INTEXTURE | SFLAG_INDRAWABLE);
4885     }
4886
4887     if (surface->flags & (SFLAG_INTEXTURE | SFLAG_INSRGBTEX)
4888             && gl_info->supported[EXT_TEXTURE_SRGB_DECODE])
4889     {
4890         surface->flags |= (SFLAG_INTEXTURE | SFLAG_INSRGBTEX);
4891     }
4892
4893     return WINED3D_OK;
4894 }
4895
4896 BOOL surface_is_offscreen(IWineD3DSurfaceImpl *surface)
4897 {
4898     struct wined3d_swapchain *swapchain = surface->container.u.swapchain;
4899
4900     /* Not on a swapchain - must be offscreen */
4901     if (surface->container.type != WINED3D_CONTAINER_SWAPCHAIN) return TRUE;
4902
4903     /* The front buffer is always onscreen */
4904     if (surface == swapchain->front_buffer) return FALSE;
4905
4906     /* If the swapchain is rendered to an FBO, the backbuffer is
4907      * offscreen, otherwise onscreen */
4908     return swapchain->render_to_fbo;
4909 }
4910
4911 const IWineD3DSurfaceVtbl IWineD3DSurface_Vtbl =
4912 {
4913     /* IUnknown */
4914     IWineD3DBaseSurfaceImpl_QueryInterface,
4915     IWineD3DBaseSurfaceImpl_AddRef,
4916     IWineD3DSurfaceImpl_Release,
4917     /* IWineD3DResource */
4918     IWineD3DBaseSurfaceImpl_GetParent,
4919     IWineD3DBaseSurfaceImpl_SetPrivateData,
4920     IWineD3DBaseSurfaceImpl_GetPrivateData,
4921     IWineD3DBaseSurfaceImpl_FreePrivateData,
4922     IWineD3DBaseSurfaceImpl_SetPriority,
4923     IWineD3DBaseSurfaceImpl_GetPriority,
4924     IWineD3DSurfaceImpl_PreLoad,
4925     /* IWineD3DSurface */
4926     IWineD3DBaseSurfaceImpl_GetResource,
4927     IWineD3DSurfaceImpl_Map,
4928     IWineD3DSurfaceImpl_Unmap,
4929     IWineD3DSurfaceImpl_GetDC,
4930     IWineD3DSurfaceImpl_ReleaseDC,
4931     IWineD3DSurfaceImpl_Flip,
4932     IWineD3DSurfaceImpl_Blt,
4933     IWineD3DBaseSurfaceImpl_GetBltStatus,
4934     IWineD3DBaseSurfaceImpl_GetFlipStatus,
4935     IWineD3DBaseSurfaceImpl_IsLost,
4936     IWineD3DBaseSurfaceImpl_Restore,
4937     IWineD3DSurfaceImpl_BltFast,
4938     IWineD3DBaseSurfaceImpl_GetPalette,
4939     IWineD3DBaseSurfaceImpl_SetPalette,
4940     IWineD3DBaseSurfaceImpl_SetColorKey,
4941     IWineD3DBaseSurfaceImpl_GetPitch,
4942     IWineD3DSurfaceImpl_SetMem,
4943     IWineD3DBaseSurfaceImpl_SetOverlayPosition,
4944     IWineD3DBaseSurfaceImpl_GetOverlayPosition,
4945     IWineD3DBaseSurfaceImpl_UpdateOverlayZOrder,
4946     IWineD3DBaseSurfaceImpl_UpdateOverlay,
4947     IWineD3DBaseSurfaceImpl_SetClipper,
4948     IWineD3DBaseSurfaceImpl_GetClipper,
4949     /* Internal use: */
4950     IWineD3DSurfaceImpl_SetFormat,
4951     IWineD3DSurfaceImpl_PrivateSetup,
4952 };
4953
4954 static HRESULT ffp_blit_alloc(IWineD3DDeviceImpl *device) { return WINED3D_OK; }
4955 /* Context activation is done by the caller. */
4956 static void ffp_blit_free(IWineD3DDeviceImpl *device) { }
4957
4958 /* This function is used in case of 8bit paletted textures using GL_EXT_paletted_texture */
4959 /* Context activation is done by the caller. */
4960 static void ffp_blit_p8_upload_palette(IWineD3DSurfaceImpl *surface, const struct wined3d_gl_info *gl_info)
4961 {
4962     BYTE table[256][4];
4963     BOOL colorkey_active = (surface->CKeyFlags & WINEDDSD_CKSRCBLT) ? TRUE : FALSE;
4964
4965     d3dfmt_p8_init_palette(surface, table, colorkey_active);
4966
4967     TRACE("Using GL_EXT_PALETTED_TEXTURE for 8-bit paletted texture support\n");
4968     ENTER_GL();
4969     GL_EXTCALL(glColorTableEXT(surface->texture_target, GL_RGBA, 256, GL_RGBA, GL_UNSIGNED_BYTE, table));
4970     LEAVE_GL();
4971 }
4972
4973 /* Context activation is done by the caller. */
4974 static HRESULT ffp_blit_set(void *blit_priv, const struct wined3d_gl_info *gl_info, IWineD3DSurfaceImpl *surface)
4975 {
4976     enum complex_fixup fixup = get_complex_fixup(surface->resource.format->color_fixup);
4977
4978     /* When EXT_PALETTED_TEXTURE is around, palette conversion is done by the GPU
4979      * else the surface is converted in software at upload time in LoadLocation.
4980      */
4981     if(fixup == COMPLEX_FIXUP_P8 && gl_info->supported[EXT_PALETTED_TEXTURE])
4982         ffp_blit_p8_upload_palette(surface, gl_info);
4983
4984     ENTER_GL();
4985     glEnable(surface->texture_target);
4986     checkGLcall("glEnable(surface->texture_target)");
4987     LEAVE_GL();
4988     return WINED3D_OK;
4989 }
4990
4991 /* Context activation is done by the caller. */
4992 static void ffp_blit_unset(const struct wined3d_gl_info *gl_info)
4993 {
4994     ENTER_GL();
4995     glDisable(GL_TEXTURE_2D);
4996     checkGLcall("glDisable(GL_TEXTURE_2D)");
4997     if (gl_info->supported[ARB_TEXTURE_CUBE_MAP])
4998     {
4999         glDisable(GL_TEXTURE_CUBE_MAP_ARB);
5000         checkGLcall("glDisable(GL_TEXTURE_CUBE_MAP_ARB)");
5001     }
5002     if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
5003     {
5004         glDisable(GL_TEXTURE_RECTANGLE_ARB);
5005         checkGLcall("glDisable(GL_TEXTURE_RECTANGLE_ARB)");
5006     }
5007     LEAVE_GL();
5008 }
5009
5010 static BOOL ffp_blit_supported(const struct wined3d_gl_info *gl_info, enum wined3d_blit_op blit_op,
5011         const RECT *src_rect, DWORD src_usage, WINED3DPOOL src_pool, const struct wined3d_format *src_format,
5012         const RECT *dst_rect, DWORD dst_usage, WINED3DPOOL dst_pool, const struct wined3d_format *dst_format)
5013 {
5014     enum complex_fixup src_fixup;
5015
5016     switch (blit_op)
5017     {
5018         case WINED3D_BLIT_OP_COLOR_BLIT:
5019             src_fixup = get_complex_fixup(src_format->color_fixup);
5020             if (TRACE_ON(d3d_surface) && TRACE_ON(d3d))
5021             {
5022                 TRACE("Checking support for fixup:\n");
5023                 dump_color_fixup_desc(src_format->color_fixup);
5024             }
5025
5026             if (!is_identity_fixup(dst_format->color_fixup))
5027             {
5028                 TRACE("Destination fixups are not supported\n");
5029                 return FALSE;
5030             }
5031
5032             if (src_fixup == COMPLEX_FIXUP_P8 && gl_info->supported[EXT_PALETTED_TEXTURE])
5033             {
5034                 TRACE("P8 fixup supported\n");
5035                 return TRUE;
5036             }
5037
5038             /* We only support identity conversions. */
5039             if (is_identity_fixup(src_format->color_fixup))
5040             {
5041                 TRACE("[OK]\n");
5042                 return TRUE;
5043             }
5044
5045             TRACE("[FAILED]\n");
5046             return FALSE;
5047
5048         case WINED3D_BLIT_OP_COLOR_FILL:
5049             if (!(dst_usage & WINED3DUSAGE_RENDERTARGET))
5050             {
5051                 TRACE("Color fill not supported\n");
5052                 return FALSE;
5053             }
5054
5055             return TRUE;
5056
5057         case WINED3D_BLIT_OP_DEPTH_FILL:
5058             return TRUE;
5059
5060         default:
5061             TRACE("Unsupported blit_op=%d\n", blit_op);
5062             return FALSE;
5063     }
5064 }
5065
5066 /* Do not call while under the GL lock. */
5067 static HRESULT ffp_blit_color_fill(IWineD3DDeviceImpl *device, IWineD3DSurfaceImpl *dst_surface,
5068         const RECT *dst_rect, const WINED3DCOLORVALUE *color)
5069 {
5070     const RECT draw_rect = {0, 0, dst_surface->resource.width, dst_surface->resource.height};
5071
5072     return device_clear_render_targets(device, 1, &dst_surface, NULL,
5073             1, dst_rect, &draw_rect, WINED3DCLEAR_TARGET, color, 0.0f, 0);
5074 }
5075
5076 /* Do not call while under the GL lock. */
5077 static HRESULT ffp_blit_depth_fill(IWineD3DDeviceImpl *device,
5078         IWineD3DSurfaceImpl *surface, const RECT *rect, float depth)
5079 {
5080     const RECT draw_rect = {0, 0, surface->resource.width, surface->resource.height};
5081
5082     return device_clear_render_targets(device, 0, NULL, surface,
5083             1, rect, &draw_rect, WINED3DCLEAR_ZBUFFER, 0, depth, 0);
5084 }
5085
5086 const struct blit_shader ffp_blit =  {
5087     ffp_blit_alloc,
5088     ffp_blit_free,
5089     ffp_blit_set,
5090     ffp_blit_unset,
5091     ffp_blit_supported,
5092     ffp_blit_color_fill,
5093     ffp_blit_depth_fill,
5094 };
5095
5096 static HRESULT cpu_blit_alloc(IWineD3DDeviceImpl *device)
5097 {
5098     return WINED3D_OK;
5099 }
5100
5101 /* Context activation is done by the caller. */
5102 static void cpu_blit_free(IWineD3DDeviceImpl *device)
5103 {
5104 }
5105
5106 /* Context activation is done by the caller. */
5107 static HRESULT cpu_blit_set(void *blit_priv, const struct wined3d_gl_info *gl_info, IWineD3DSurfaceImpl *surface)
5108 {
5109     return WINED3D_OK;
5110 }
5111
5112 /* Context activation is done by the caller. */
5113 static void cpu_blit_unset(const struct wined3d_gl_info *gl_info)
5114 {
5115 }
5116
5117 static BOOL cpu_blit_supported(const struct wined3d_gl_info *gl_info, enum wined3d_blit_op blit_op,
5118         const RECT *src_rect, DWORD src_usage, WINED3DPOOL src_pool, const struct wined3d_format *src_format,
5119         const RECT *dst_rect, DWORD dst_usage, WINED3DPOOL dst_pool, const struct wined3d_format *dst_format)
5120 {
5121     if (blit_op == WINED3D_BLIT_OP_COLOR_FILL)
5122     {
5123         return TRUE;
5124     }
5125
5126     return FALSE;
5127 }
5128
5129 /* Do not call while under the GL lock. */
5130 static HRESULT cpu_blit_color_fill(IWineD3DDeviceImpl *device, IWineD3DSurfaceImpl *dst_surface,
5131         const RECT *dst_rect, const WINED3DCOLORVALUE *color)
5132 {
5133     WINEDDBLTFX BltFx;
5134
5135     memset(&BltFx, 0, sizeof(BltFx));
5136     BltFx.dwSize = sizeof(BltFx);
5137     BltFx.u5.dwFillColor = wined3d_format_convert_from_float(dst_surface->resource.format, color);
5138     return IWineD3DBaseSurfaceImpl_Blt((IWineD3DSurface*)dst_surface, dst_rect,
5139             NULL, NULL, WINEDDBLT_COLORFILL, &BltFx, WINED3DTEXF_POINT);
5140 }
5141
5142 /* Do not call while under the GL lock. */
5143 static HRESULT cpu_blit_depth_fill(IWineD3DDeviceImpl *device,
5144         IWineD3DSurfaceImpl *surface, const RECT *rect, float depth)
5145 {
5146     FIXME("Depth filling not implemented by cpu_blit.\n");
5147     return WINED3DERR_INVALIDCALL;
5148 }
5149
5150 const struct blit_shader cpu_blit =  {
5151     cpu_blit_alloc,
5152     cpu_blit_free,
5153     cpu_blit_set,
5154     cpu_blit_unset,
5155     cpu_blit_supported,
5156     cpu_blit_color_fill,
5157     cpu_blit_depth_fill,
5158 };
5159
5160 static BOOL fbo_blit_supported(const struct wined3d_gl_info *gl_info, enum wined3d_blit_op blit_op,
5161         const RECT *src_rect, DWORD src_usage, WINED3DPOOL src_pool, const struct wined3d_format *src_format,
5162         const RECT *dst_rect, DWORD dst_usage, WINED3DPOOL dst_pool, const struct wined3d_format *dst_format)
5163 {
5164     if ((wined3d_settings.offscreen_rendering_mode != ORM_FBO) || !gl_info->fbo_ops.glBlitFramebuffer)
5165         return FALSE;
5166
5167     /* Source and/or destination need to be on the GL side */
5168     if (src_pool == WINED3DPOOL_SYSTEMMEM || dst_pool == WINED3DPOOL_SYSTEMMEM)
5169         return FALSE;
5170
5171     switch (blit_op)
5172     {
5173         case WINED3D_BLIT_OP_COLOR_BLIT:
5174             if (!((src_format->flags & WINED3DFMT_FLAG_FBO_ATTACHABLE) || (src_usage & WINED3DUSAGE_RENDERTARGET)))
5175                 return FALSE;
5176             if (!((dst_format->flags & WINED3DFMT_FLAG_FBO_ATTACHABLE) || (dst_usage & WINED3DUSAGE_RENDERTARGET)))
5177                 return FALSE;
5178             break;
5179
5180         case WINED3D_BLIT_OP_DEPTH_BLIT:
5181             if (!(src_format->flags & (WINED3DFMT_FLAG_DEPTH | WINED3DFMT_FLAG_STENCIL)))
5182                 return FALSE;
5183             if (!(dst_format->flags & (WINED3DFMT_FLAG_DEPTH | WINED3DFMT_FLAG_STENCIL)))
5184                 return FALSE;
5185             break;
5186
5187         default:
5188             return FALSE;
5189     }
5190
5191     if (!(src_format->id == dst_format->id
5192             || (is_identity_fixup(src_format->color_fixup)
5193             && is_identity_fixup(dst_format->color_fixup))))
5194         return FALSE;
5195
5196     return TRUE;
5197 }
5198
5199 static ULONG WINAPI IWineGDISurfaceImpl_Release(IWineD3DSurface *iface)
5200 {
5201     IWineD3DSurfaceImpl *surface = (IWineD3DSurfaceImpl *)iface;
5202     ULONG refcount;
5203
5204     TRACE("Surface %p, container %p of type %#x.\n",
5205             surface, surface->container.u.base, surface->container.type);
5206
5207     switch (surface->container.type)
5208     {
5209         case WINED3D_CONTAINER_TEXTURE:
5210             return wined3d_texture_decref(surface->container.u.texture);
5211
5212         case WINED3D_CONTAINER_SWAPCHAIN:
5213             return wined3d_swapchain_decref(surface->container.u.swapchain);
5214
5215         default:
5216             ERR("Unhandled container type %#x.\n", surface->container.type);
5217         case WINED3D_CONTAINER_NONE:
5218             break;
5219     }
5220
5221     refcount = InterlockedDecrement(&surface->resource.ref);
5222     TRACE("%p decreasing refcount to %u.\n", surface, refcount);
5223
5224     if (!refcount)
5225     {
5226         surface_gdi_cleanup(surface);
5227         surface->resource.parent_ops->wined3d_object_destroyed(surface->resource.parent);
5228
5229         TRACE("Destroyed surface %p.\n", surface);
5230         HeapFree(GetProcessHeap(), 0, surface);
5231     }
5232
5233     return refcount;
5234 }
5235
5236 static void WINAPI IWineGDISurfaceImpl_PreLoad(IWineD3DSurface *iface)
5237 {
5238     ERR("(%p): PreLoad is not supported on X11 surfaces!\n", iface);
5239     ERR("(%p): Most likely the parent library did something wrong.\n", iface);
5240     ERR("(%p): Please report to wine-devel\n", iface);
5241 }
5242
5243 static HRESULT WINAPI IWineGDISurfaceImpl_Map(IWineD3DSurface *iface,
5244         WINED3DLOCKED_RECT *locked_rect, const RECT *rect, DWORD flags)
5245 {
5246     IWineD3DSurfaceImpl *surface = (IWineD3DSurfaceImpl *)iface;
5247
5248     TRACE("iface %p, locked_rect %p, rect %s, flags %#x.\n",
5249             iface, locked_rect, wine_dbgstr_rect(rect), flags);
5250
5251     /* Already locked? */
5252     if (surface->flags & SFLAG_LOCKED)
5253     {
5254         WARN("Surface already mapped.\n");
5255         /* What should I return here? */
5256         return WINED3DERR_INVALIDCALL;
5257     }
5258     surface->flags |= SFLAG_LOCKED;
5259
5260     if (!surface->resource.allocatedMemory)
5261     {
5262         /* This happens on gdi surfaces if the application set a user pointer
5263          * and resets it. Recreate the DIB section. */
5264         IWineD3DBaseSurfaceImpl_CreateDIBSection(iface);
5265         surface->resource.allocatedMemory = surface->dib.bitmap_data;
5266     }
5267
5268     return IWineD3DBaseSurfaceImpl_Map(iface, locked_rect, rect, flags);
5269 }
5270
5271 static HRESULT WINAPI IWineGDISurfaceImpl_Unmap(IWineD3DSurface *iface)
5272 {
5273     IWineD3DSurfaceImpl *surface = (IWineD3DSurfaceImpl *)iface;
5274
5275     TRACE("iface %p.\n", iface);
5276
5277     if (!(surface->flags & SFLAG_LOCKED))
5278     {
5279         WARN("Trying to unmap unmapped surface.\n");
5280         return WINEDDERR_NOTLOCKED;
5281     }
5282
5283     /* Tell the swapchain to update the screen. */
5284     if (surface->container.type == WINED3D_CONTAINER_SWAPCHAIN)
5285     {
5286         struct wined3d_swapchain *swapchain = surface->container.u.swapchain;
5287         if (surface == swapchain->front_buffer)
5288         {
5289             x11_copy_to_screen(swapchain, &surface->lockedRect);
5290         }
5291     }
5292
5293     surface->flags &= ~SFLAG_LOCKED;
5294     memset(&surface->lockedRect, 0, sizeof(RECT));
5295
5296     return WINED3D_OK;
5297 }
5298
5299 /*****************************************************************************
5300  * IWineD3DSurface::Flip, GDI version
5301  *
5302  * Flips 2 flipping enabled surfaces. Determining the 2 targets is done by
5303  * the parent library. This implementation changes the data pointers of the
5304  * surfaces and copies the new front buffer content to the screen
5305  *
5306  * Params:
5307  *  override: Flipping target(e.g. back buffer)
5308  *
5309  * Returns:
5310  *  WINED3D_OK on success
5311  *
5312  *****************************************************************************/
5313 static HRESULT WINAPI IWineGDISurfaceImpl_Flip(IWineD3DSurface *iface, IWineD3DSurface *override, DWORD flags)
5314 {
5315     IWineD3DSurfaceImpl *surface = (IWineD3DSurfaceImpl *)iface;
5316     struct wined3d_swapchain *swapchain;
5317     HRESULT hr;
5318
5319     if (surface->container.type != WINED3D_CONTAINER_SWAPCHAIN)
5320     {
5321         ERR("Flipped surface is not on a swapchain\n");
5322         return WINEDDERR_NOTFLIPPABLE;
5323     }
5324
5325     swapchain = surface->container.u.swapchain;
5326     hr = wined3d_swapchain_present(swapchain, NULL, NULL, swapchain->win_handle, NULL, 0);
5327
5328     return hr;
5329 }
5330
5331 static HRESULT WINAPI IWineGDISurfaceImpl_GetDC(IWineD3DSurface *iface, HDC *dc)
5332 {
5333     IWineD3DSurfaceImpl *surface = (IWineD3DSurfaceImpl *)iface;
5334     WINED3DLOCKED_RECT lock;
5335     HRESULT hr;
5336     RGBQUAD col[256];
5337
5338     TRACE("iface %p, dc %p.\n", iface, dc);
5339
5340     if (!(surface->flags & SFLAG_DIBSECTION))
5341     {
5342         WARN("DC not supported on this surface\n");
5343         return WINED3DERR_INVALIDCALL;
5344     }
5345
5346     if (surface->flags & SFLAG_USERPTR)
5347     {
5348         ERR("Not supported on surfaces with application-provided memory.\n");
5349         return WINEDDERR_NODC;
5350     }
5351
5352     /* Give more detailed info for ddraw. */
5353     if (surface->flags & SFLAG_DCINUSE)
5354         return WINEDDERR_DCALREADYCREATED;
5355
5356     /* Can't GetDC if the surface is locked. */
5357     if (surface->flags & SFLAG_LOCKED)
5358         return WINED3DERR_INVALIDCALL;
5359
5360     memset(&lock, 0, sizeof(lock)); /* To be sure */
5361
5362     /* Should have a DIB section already. */
5363
5364     /* Map the surface. */
5365     hr = IWineD3DSurface_Map(iface, &lock, NULL, 0);
5366     if (FAILED(hr))
5367     {
5368         ERR("IWineD3DSurface_Map failed, hr %#x.\n", hr);
5369         /* keep the dib section */
5370         return hr;
5371     }
5372
5373     if (surface->resource.format->id == WINED3DFMT_P8_UINT
5374             || surface->resource.format->id == WINED3DFMT_P8_UINT_A8_UNORM)
5375     {
5376         const PALETTEENTRY *pal = NULL;
5377
5378         if (surface->palette)
5379         {
5380             pal = surface->palette->palents;
5381         }
5382         else
5383         {
5384             struct wined3d_swapchain *swapchain = surface->resource.device->swapchains[0];
5385             IWineD3DSurfaceImpl *dds_primary = swapchain->front_buffer;
5386
5387             if (dds_primary && dds_primary->palette)
5388                 pal = dds_primary->palette->palents;
5389         }
5390
5391         if (pal)
5392         {
5393             unsigned int i;
5394
5395             for (i = 0; i < 256; ++i)
5396             {
5397                 col[i].rgbRed = pal[i].peRed;
5398                 col[i].rgbGreen = pal[i].peGreen;
5399                 col[i].rgbBlue = pal[i].peBlue;
5400                 col[i].rgbReserved = 0;
5401             }
5402             SetDIBColorTable(surface->hDC, 0, 256, col);
5403         }
5404     }
5405
5406     surface->flags |= SFLAG_DCINUSE;
5407
5408     *dc = surface->hDC;
5409     TRACE("Returning dc %p.\n", *dc);
5410
5411     return WINED3D_OK;
5412 }
5413
5414 static HRESULT WINAPI IWineGDISurfaceImpl_ReleaseDC(IWineD3DSurface *iface, HDC dc)
5415 {
5416     IWineD3DSurfaceImpl *surface = (IWineD3DSurfaceImpl *)iface;
5417
5418     TRACE("iface %p, dc %p.\n", iface, dc);
5419
5420     if (!(surface->flags & SFLAG_DCINUSE))
5421         return WINEDDERR_NODC;
5422
5423     if (surface->hDC != dc)
5424     {
5425         WARN("Application tries to release invalid DC %p, surface DC is %p.\n",
5426                 dc, surface->hDC);
5427         return WINEDDERR_NODC;
5428     }
5429
5430     /* We locked first, so unlock now. */
5431     IWineD3DSurface_Unmap(iface);
5432
5433     surface->flags &= ~SFLAG_DCINUSE;
5434
5435     return WINED3D_OK;
5436 }
5437
5438 /*****************************************************************************
5439  * IWineD3DSurface::PrivateSetup, GDI version
5440  *
5441  * Initializes the GDI surface, aka creates the DIB section we render to
5442  * The DIB section creation is done by calling GetDC, which will create the
5443  * section and releasing the dc to allow the app to use it. The dib section
5444  * will stay until the surface is released
5445  *
5446  * GDI surfaces do not need to be a power of 2 in size, so the pow2 sizes
5447  * are set to the real sizes to save memory. The NONPOW2 flag is unset to
5448  * avoid confusion in the shared surface code.
5449  *
5450  * Returns:
5451  *  WINED3D_OK on success
5452  *  The return values of called methods on failure
5453  *
5454  *****************************************************************************/
5455 static HRESULT WINAPI IWineGDISurfaceImpl_PrivateSetup(IWineD3DSurface *iface)
5456 {
5457     IWineD3DSurfaceImpl *surface = (IWineD3DSurfaceImpl *)iface;
5458     HRESULT hr;
5459
5460     surface->surface_ops = &gdi_surface_ops;
5461
5462     if (surface->resource.usage & WINED3DUSAGE_OVERLAY)
5463     {
5464         ERR("Overlays not yet supported by GDI surfaces.\n");
5465         return WINED3DERR_INVALIDCALL;
5466     }
5467
5468     /* Sysmem textures have memory already allocated - release it,
5469      * this avoids an unnecessary memcpy. */
5470     hr = IWineD3DBaseSurfaceImpl_CreateDIBSection(iface);
5471     if (SUCCEEDED(hr))
5472     {
5473         HeapFree(GetProcessHeap(), 0, surface->resource.heapMemory);
5474         surface->resource.heapMemory = NULL;
5475         surface->resource.allocatedMemory = surface->dib.bitmap_data;
5476     }
5477
5478     /* We don't mind the nonpow2 stuff in GDI */
5479     surface->pow2Width = surface->resource.width;
5480     surface->pow2Height = surface->resource.height;
5481
5482     return WINED3D_OK;
5483 }
5484
5485 static HRESULT WINAPI IWineGDISurfaceImpl_SetMem(IWineD3DSurface *iface, void *mem)
5486 {
5487     IWineD3DSurfaceImpl *surface = (IWineD3DSurfaceImpl *)iface;
5488
5489     /* Render targets depend on their hdc, and we can't create an hdc on a user pointer. */
5490     if (surface->resource.usage & WINED3DUSAGE_RENDERTARGET)
5491     {
5492         ERR("Not supported on render targets.\n");
5493         return WINED3DERR_INVALIDCALL;
5494     }
5495
5496     if (surface->flags & (SFLAG_LOCKED | SFLAG_DCINUSE))
5497     {
5498         WARN("Surface is locked or the DC is in use.\n");
5499         return WINED3DERR_INVALIDCALL;
5500     }
5501
5502     if (mem && mem != surface->resource.allocatedMemory)
5503     {
5504         void *release = NULL;
5505
5506         /* Do I have to copy the old surface content? */
5507         if (surface->flags & SFLAG_DIBSECTION)
5508         {
5509             SelectObject(surface->hDC, surface->dib.holdbitmap);
5510             DeleteDC(surface->hDC);
5511             /* Release the DIB section. */
5512             DeleteObject(surface->dib.DIBsection);
5513             surface->dib.bitmap_data = NULL;
5514             surface->resource.allocatedMemory = NULL;
5515             surface->hDC = NULL;
5516             surface->flags &= ~SFLAG_DIBSECTION;
5517         }
5518         else if (!(surface->flags & SFLAG_USERPTR))
5519         {
5520             release = surface->resource.allocatedMemory;
5521         }
5522         surface->resource.allocatedMemory = mem;
5523         surface->flags |= SFLAG_USERPTR | SFLAG_INSYSMEM;
5524
5525         /* Now free the old memory, if any. */
5526         HeapFree(GetProcessHeap(), 0, release);
5527     }
5528     else if (surface->flags & SFLAG_USERPTR)
5529     {
5530         /* Map() and GetDC() will re-create the dib section and allocated memory. */
5531         surface->resource.allocatedMemory = NULL;
5532         surface->flags &= ~SFLAG_USERPTR;
5533     }
5534
5535     return WINED3D_OK;
5536 }
5537
5538 static const IWineD3DSurfaceVtbl IWineGDISurface_Vtbl =
5539 {
5540     /* IUnknown */
5541     IWineD3DBaseSurfaceImpl_QueryInterface,
5542     IWineD3DBaseSurfaceImpl_AddRef,
5543     IWineGDISurfaceImpl_Release,
5544     /* IWineD3DResource */
5545     IWineD3DBaseSurfaceImpl_GetParent,
5546     IWineD3DBaseSurfaceImpl_SetPrivateData,
5547     IWineD3DBaseSurfaceImpl_GetPrivateData,
5548     IWineD3DBaseSurfaceImpl_FreePrivateData,
5549     IWineD3DBaseSurfaceImpl_SetPriority,
5550     IWineD3DBaseSurfaceImpl_GetPriority,
5551     IWineGDISurfaceImpl_PreLoad,
5552     /* IWineD3DSurface */
5553     IWineD3DBaseSurfaceImpl_GetResource,
5554     IWineGDISurfaceImpl_Map,
5555     IWineGDISurfaceImpl_Unmap,
5556     IWineGDISurfaceImpl_GetDC,
5557     IWineGDISurfaceImpl_ReleaseDC,
5558     IWineGDISurfaceImpl_Flip,
5559     IWineD3DBaseSurfaceImpl_Blt,
5560     IWineD3DBaseSurfaceImpl_GetBltStatus,
5561     IWineD3DBaseSurfaceImpl_GetFlipStatus,
5562     IWineD3DBaseSurfaceImpl_IsLost,
5563     IWineD3DBaseSurfaceImpl_Restore,
5564     IWineD3DBaseSurfaceImpl_BltFast,
5565     IWineD3DBaseSurfaceImpl_GetPalette,
5566     IWineD3DBaseSurfaceImpl_SetPalette,
5567     IWineD3DBaseSurfaceImpl_SetColorKey,
5568     IWineD3DBaseSurfaceImpl_GetPitch,
5569     IWineGDISurfaceImpl_SetMem,
5570     IWineD3DBaseSurfaceImpl_SetOverlayPosition,
5571     IWineD3DBaseSurfaceImpl_GetOverlayPosition,
5572     IWineD3DBaseSurfaceImpl_UpdateOverlayZOrder,
5573     IWineD3DBaseSurfaceImpl_UpdateOverlay,
5574     IWineD3DBaseSurfaceImpl_SetClipper,
5575     IWineD3DBaseSurfaceImpl_GetClipper,
5576     /* Internal use: */
5577     IWineD3DBaseSurfaceImpl_SetFormat,
5578     IWineGDISurfaceImpl_PrivateSetup,
5579 };
5580
5581 HRESULT surface_init(IWineD3DSurfaceImpl *surface, WINED3DSURFTYPE surface_type, UINT alignment,
5582         UINT width, UINT height, UINT level, BOOL lockable, BOOL discard, WINED3DMULTISAMPLE_TYPE multisample_type,
5583         UINT multisample_quality, IWineD3DDeviceImpl *device, DWORD usage, enum wined3d_format_id format_id,
5584         WINED3DPOOL pool, void *parent, const struct wined3d_parent_ops *parent_ops)
5585 {
5586     const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
5587     const struct wined3d_format *format = wined3d_get_format(gl_info, format_id);
5588     void (*cleanup)(IWineD3DSurfaceImpl *This);
5589     unsigned int resource_size;
5590     HRESULT hr;
5591
5592     if (multisample_quality > 0)
5593     {
5594         FIXME("multisample_quality set to %u, substituting 0.\n", multisample_quality);
5595         multisample_quality = 0;
5596     }
5597
5598     /* Quick lockable sanity check.
5599      * TODO: remove this after surfaces, usage and lockability have been debugged properly
5600      * this function is too deep to need to care about things like this.
5601      * Levels need to be checked too, since they all affect what can be done. */
5602     switch (pool)
5603     {
5604         case WINED3DPOOL_SCRATCH:
5605             if (!lockable)
5606             {
5607                 FIXME("Called with a pool of SCRATCH and a lockable of FALSE "
5608                         "which are mutually exclusive, setting lockable to TRUE.\n");
5609                 lockable = TRUE;
5610             }
5611             break;
5612
5613         case WINED3DPOOL_SYSTEMMEM:
5614             if (!lockable)
5615                 FIXME("Called with a pool of SYSTEMMEM and a lockable of FALSE, this is acceptable but unexpected.\n");
5616             break;
5617
5618         case WINED3DPOOL_MANAGED:
5619             if (usage & WINED3DUSAGE_DYNAMIC)
5620                 FIXME("Called with a pool of MANAGED and a usage of DYNAMIC which are mutually exclusive.\n");
5621             break;
5622
5623         case WINED3DPOOL_DEFAULT:
5624             if (lockable && !(usage & (WINED3DUSAGE_DYNAMIC | WINED3DUSAGE_RENDERTARGET | WINED3DUSAGE_DEPTHSTENCIL)))
5625                 WARN("Creating a lockable surface with a POOL of DEFAULT, that doesn't specify DYNAMIC usage.\n");
5626             break;
5627
5628         default:
5629             FIXME("Unknown pool %#x.\n", pool);
5630             break;
5631     };
5632
5633     if (usage & WINED3DUSAGE_RENDERTARGET && pool != WINED3DPOOL_DEFAULT)
5634         FIXME("Trying to create a render target that isn't in the default pool.\n");
5635
5636     /* FIXME: Check that the format is supported by the device. */
5637
5638     resource_size = wined3d_format_calculate_size(format, alignment, width, height);
5639     if (!resource_size)
5640         return WINED3DERR_INVALIDCALL;
5641
5642     surface->surface_type = surface_type;
5643
5644     /* Look at the implementation and set the correct Vtable. */
5645     switch (surface_type)
5646     {
5647         case SURFACE_OPENGL:
5648             surface->lpVtbl = &IWineD3DSurface_Vtbl;
5649             cleanup = surface_cleanup;
5650             break;
5651
5652         case SURFACE_GDI:
5653             surface->lpVtbl = &IWineGDISurface_Vtbl;
5654             cleanup = surface_gdi_cleanup;
5655             break;
5656
5657         default:
5658             ERR("Requested unknown surface implementation %#x.\n", surface_type);
5659             return WINED3DERR_INVALIDCALL;
5660     }
5661
5662     hr = resource_init(&surface->resource, device, WINED3DRTYPE_SURFACE, format,
5663             multisample_type, multisample_quality, usage, pool, width, height, 1,
5664             resource_size, parent, parent_ops, &surface_resource_ops);
5665     if (FAILED(hr))
5666     {
5667         WARN("Failed to initialize resource, returning %#x.\n", hr);
5668         return hr;
5669     }
5670
5671     /* "Standalone" surface. */
5672     surface_set_container(surface, WINED3D_CONTAINER_NONE, NULL);
5673
5674     surface->texture_level = level;
5675     list_init(&surface->overlays);
5676
5677     /* Flags */
5678     surface->flags = SFLAG_NORMCOORD; /* Default to normalized coords. */
5679     if (discard)
5680         surface->flags |= SFLAG_DISCARD;
5681     if (lockable || format_id == WINED3DFMT_D16_LOCKABLE)
5682         surface->flags |= SFLAG_LOCKABLE;
5683
5684     /* Mark the texture as dirty so that it gets loaded first time around. */
5685     surface_add_dirty_rect(surface, NULL);
5686     list_init(&surface->renderbuffers);
5687
5688     TRACE("surface %p, memory %p, size %u\n",
5689             surface, surface->resource.allocatedMemory, surface->resource.size);
5690
5691     /* Call the private setup routine */
5692     hr = IWineD3DSurface_PrivateSetup((IWineD3DSurface *)surface);
5693     if (FAILED(hr))
5694     {
5695         ERR("Private setup failed, returning %#x\n", hr);
5696         cleanup(surface);
5697         return hr;
5698     }
5699
5700     return hr;
5701 }