From 0c74d61fdaa16fbb70f443f6863d864024b8e615 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Stefan=20D=C3=B6singer?= <stefan@codeweavers.com>
Date: Tue, 21 Jan 2014 13:30:59 +0100
Subject: wined3d: Use resource buffer mapping facilities in surfaces.

---
 dlls/wined3d/resource.c |  8 +++++
 dlls/wined3d/surface.c  | 78 ++++++++++---------------------------------------
 2 files changed, 23 insertions(+), 63 deletions(-)

diff --git a/dlls/wined3d/resource.c b/dlls/wined3d/resource.c
index 5f0a98e..dda77f0 100644
--- a/dlls/wined3d/resource.c
+++ b/dlls/wined3d/resource.c
@@ -544,6 +544,12 @@ BYTE *wined3d_resource_get_map_ptr(const struct wined3d_resource *resource,
         case WINED3D_LOCATION_SYSMEM:
             return resource->heap_memory;
 
+        case WINED3D_LOCATION_DIB:
+            return resource->bitmap_data;
+
+        case WINED3D_LOCATION_USER_MEMORY:
+            return resource->user_memory;
+
         default:
             ERR("Unexpected map binding %s.\n", wined3d_debug_location(resource->map_binding));
             return NULL;
@@ -566,6 +572,8 @@ void wined3d_resource_release_map_ptr(const struct wined3d_resource *resource,
             return;
 
         case WINED3D_LOCATION_SYSMEM:
+        case WINED3D_LOCATION_DIB:
+        case WINED3D_LOCATION_USER_MEMORY:
             return;
 
         default:
diff --git a/dlls/wined3d/surface.c b/dlls/wined3d/surface.c
index 62f74bd..583fa59 100644
--- a/dlls/wined3d/surface.c
+++ b/dlls/wined3d/surface.c
@@ -716,36 +716,10 @@ static HRESULT surface_private_setup(struct wined3d_surface *surface)
 
 static void surface_unmap(struct wined3d_surface *surface)
 {
-    struct wined3d_device *device = surface->resource.device;
-    const struct wined3d_gl_info *gl_info;
-    struct wined3d_context *context;
-
     TRACE("surface %p.\n", surface);
 
     memset(&surface->lockedRect, 0, sizeof(surface->lockedRect));
 
-    switch (surface->resource.map_binding)
-    {
-        case WINED3D_LOCATION_SYSMEM:
-        case WINED3D_LOCATION_USER_MEMORY:
-        case WINED3D_LOCATION_DIB:
-            break;
-
-        case WINED3D_LOCATION_BUFFER:
-            context = context_acquire(device, NULL);
-            gl_info = context->gl_info;
-
-            GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, surface->resource.buffer_object));
-            GL_EXTCALL(glUnmapBuffer(GL_PIXEL_UNPACK_BUFFER));
-            GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0));
-            checkGLcall("glUnmapBuffer");
-            context_release(context);
-            break;
-
-        default:
-            ERR("Unexpected map binding %s.\n", wined3d_debug_location(surface->resource.map_binding));
-    }
-
     if (surface->resource.locations & (WINED3D_LOCATION_DRAWABLE | WINED3D_LOCATION_TEXTURE_RGB))
     {
         TRACE("Not dirtified, nothing to do.\n");
@@ -2623,6 +2597,8 @@ struct wined3d_surface * CDECL wined3d_surface_from_resource(struct wined3d_reso
 
 HRESULT CDECL wined3d_surface_unmap(struct wined3d_surface *surface)
 {
+    struct wined3d_device *device = surface->resource.device;
+    struct wined3d_context *context = NULL;
     TRACE("surface %p.\n", surface);
 
     if (!surface->resource.map_count)
@@ -2632,6 +2608,12 @@ HRESULT CDECL wined3d_surface_unmap(struct wined3d_surface *surface)
     }
     --surface->resource.map_count;
 
+    if (device->d3d_initialized)
+        context = context_acquire(device, NULL);
+    wined3d_resource_release_map_ptr(&surface->resource, context);
+    if (context)
+        context_release(context);
+
     surface->surface_ops->surface_unmap(surface);
 
     return WINED3D_OK;
@@ -2643,8 +2625,7 @@ HRESULT CDECL wined3d_surface_map(struct wined3d_surface *surface,
     const struct wined3d_format *format = surface->resource.format;
     unsigned int fmt_flags = surface->container->resource.format_flags;
     struct wined3d_device *device = surface->resource.device;
-    struct wined3d_context *context;
-    const struct wined3d_gl_info *gl_info;
+    struct wined3d_context *context = NULL;
     BYTE *base_memory;
 
     TRACE("surface %p, map_desc %p, box %p, flags %#x.\n",
@@ -2686,6 +2667,9 @@ HRESULT CDECL wined3d_surface_map(struct wined3d_surface *surface,
         }
     }
 
+    if (device->d3d_initialized)
+        context = context_acquire(device, NULL);
+
     surface_prepare_map_memory(surface);
     if (flags & WINED3D_MAP_DISCARD)
     {
@@ -2695,51 +2679,19 @@ HRESULT CDECL wined3d_surface_map(struct wined3d_surface *surface,
     }
     else
     {
-        struct wined3d_context *context = NULL;
-
         if (surface->resource.usage & WINED3DUSAGE_DYNAMIC)
             WARN_(d3d_perf)("Mapping a dynamic surface without WINED3D_MAP_DISCARD.\n");
 
-        if (surface->resource.device->d3d_initialized)
-            context = context_acquire(surface->resource.device, NULL);
         wined3d_resource_load_location(&surface->resource, context, surface->resource.map_binding);
-        if (context)
-            context_release(context);
     }
 
     if (!(flags & (WINED3D_MAP_NO_DIRTY_UPDATE | WINED3D_MAP_READONLY)))
         wined3d_resource_invalidate_location(&surface->resource, ~surface->resource.map_binding);
 
-    switch (surface->resource.map_binding)
-    {
-        case WINED3D_LOCATION_SYSMEM:
-            base_memory = surface->resource.heap_memory;
-            break;
-
-        case WINED3D_LOCATION_USER_MEMORY:
-            base_memory = surface->resource.user_memory;
-            break;
-
-        case WINED3D_LOCATION_DIB:
-            base_memory = surface->resource.bitmap_data;
-            break;
-
-        case WINED3D_LOCATION_BUFFER:
-            context = context_acquire(device, NULL);
-            gl_info = context->gl_info;
+    base_memory = wined3d_resource_get_map_ptr(&surface->resource, context, flags);
 
-            GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, surface->resource.buffer_object));
-            base_memory = GL_EXTCALL(glMapBuffer(GL_PIXEL_UNPACK_BUFFER, GL_READ_WRITE));
-            GL_EXTCALL(glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0));
-            checkGLcall("map PBO");
-
-            context_release(context);
-            break;
-
-        default:
-            ERR("Unexpected map binding %s.\n", wined3d_debug_location(surface->resource.map_binding));
-            base_memory = NULL;
-    }
+    if (context)
+        context_release(context);
 
     if (fmt_flags & WINED3DFMT_FLAG_BROKEN_PITCH)
         map_desc->row_pitch = surface->resource.width * format->byte_count;
-- 
2.6.0

