From 4ca448b9c9fb4ed0e94eaa82b98d7677d486a92c Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Stefan=20D=C3=B6singer?= <stefan@codeweavers.com>
Date: Thu, 19 Sep 2013 18:00:23 +0200
Subject: wined3d: Use resource_map for surface_map.

---
 dlls/wined3d/resource.c |  2 +-
 dlls/wined3d/surface.c  | 99 ++++++-------------------------------------------
 dlls/wined3d/volume.c   |  7 +++-
 3 files changed, 18 insertions(+), 90 deletions(-)

diff --git a/dlls/wined3d/resource.c b/dlls/wined3d/resource.c
index 7743a40..74cb6c1 100644
--- a/dlls/wined3d/resource.c
+++ b/dlls/wined3d/resource.c
@@ -883,7 +883,7 @@ HRESULT wined3d_resource_unmap(struct wined3d_resource *resource)
     if (!resource->map_count)
     {
         WARN("Trying to unlock an unlocked resource %p.\n", resource);
-        return WINED3DERR_INVALIDCALL;
+        return WINEDDERR_NOTLOCKED;
     }
 
     if (device->d3d_initialized)
diff --git a/dlls/wined3d/surface.c b/dlls/wined3d/surface.c
index f00f6c3..3f7f54c 100644
--- a/dlls/wined3d/surface.c
+++ b/dlls/wined3d/surface.c
@@ -2428,28 +2428,18 @@ 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;
+    HRESULT hr;
     TRACE("surface %p.\n", surface);
 
-    if (!surface->resource.map_count)
-    {
-        WARN("Trying to unmap unmapped surface.\n");
-        return WINEDDERR_NOTLOCKED;
-    }
-    --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);
+    hr = wined3d_resource_unmap(&surface->resource);
+    if (FAILED(hr))
+        return hr;
 
     if (surface->container->swapchain && surface->container == surface->container->swapchain->front_buffer)
         surface->surface_ops->surface_frontbuffer_updated(surface);
     memset(&surface->lockedRect, 0, sizeof(surface->lockedRect));
 
-    return WINED3D_OK;
+    return hr;
 }
 
 HRESULT CDECL wined3d_surface_map(struct wined3d_surface *surface,
@@ -2457,18 +2447,6 @@ 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 = NULL;
-    BYTE *base_memory;
-
-    TRACE("surface %p, map_desc %p, box %p, flags %#x.\n",
-            surface, map_desc, box, flags);
-
-    if (surface->resource.map_count)
-    {
-        WARN("Surface is already mapped.\n");
-        return WINED3DERR_INVALIDCALL;
-    }
 
     if ((fmt_flags & WINED3DFMT_FLAG_BLOCKS) && box
             && !surface_check_block_align(surface, box))
@@ -2480,11 +2458,6 @@ HRESULT CDECL wined3d_surface_map(struct wined3d_surface *surface,
             return WINED3DERR_INVALIDCALL;
     }
 
-    ++surface->resource.map_count;
-
-    if (!(surface->resource.access_flags & WINED3D_RESOURCE_ACCESS_CPU))
-        WARN("Trying to lock unlockable surface.\n");
-
     /* Performance optimization: Count how often a surface is mapped, if it is
      * mapped regularly do not throw away the system memory copy. This avoids
      * the need to download the surface from OpenGL all the time. The surface
@@ -2500,72 +2473,22 @@ HRESULT CDECL wined3d_surface_map(struct wined3d_surface *surface,
         }
     }
 
-    if (device->d3d_initialized)
-        context = context_acquire(device, NULL);
-
-    wined3d_resource_prepare_map_memory(&surface->resource, context);
-    if (flags & WINED3D_MAP_DISCARD)
-    {
-        TRACE("WINED3D_MAP_DISCARD flag passed, marking %s as up to date.\n",
-                wined3d_debug_location(surface->resource.map_binding));
-        wined3d_resource_validate_location(&surface->resource, surface->resource.map_binding);
-    }
-    else
+    if (box)
     {
-        if (surface->resource.usage & WINED3DUSAGE_DYNAMIC)
-            WARN_(d3d_perf)("Mapping a dynamic surface without WINED3D_MAP_DISCARD.\n");
-
-        wined3d_resource_load_location(&surface->resource, context, surface->resource.map_binding);
+        surface->lockedRect.left = box->left;
+        surface->lockedRect.top = box->top;
+        surface->lockedRect.right = box->right;
+        surface->lockedRect.bottom = box->bottom;
     }
-
-    if (!(flags & (WINED3D_MAP_NO_DIRTY_UPDATE | WINED3D_MAP_READONLY)))
-        wined3d_resource_invalidate_location(&surface->resource, ~surface->resource.map_binding);
-
-    base_memory = wined3d_resource_get_map_ptr(&surface->resource, context, flags);
-
-    if (context)
-        context_release(context);
-
-    if (fmt_flags & WINED3DFMT_FLAG_BROKEN_PITCH)
-        map_desc->row_pitch = surface->resource.width * format->byte_count;
     else
-        wined3d_resource_get_pitch(&surface->resource, &map_desc->row_pitch, &map_desc->slice_pitch);
-    map_desc->slice_pitch = surface->resource.height * map_desc->row_pitch;
-
-    if (!box)
     {
-        map_desc->data = base_memory;
         surface->lockedRect.left = 0;
         surface->lockedRect.top = 0;
         surface->lockedRect.right = surface->resource.width;
         surface->lockedRect.bottom = surface->resource.height;
     }
-    else
-    {
-        if ((fmt_flags & (WINED3DFMT_FLAG_BLOCKS | WINED3DFMT_FLAG_BROKEN_PITCH)) == WINED3DFMT_FLAG_BLOCKS)
-        {
-            /* Compressed textures are block based, so calculate the offset of
-             * the block that contains the top-left pixel of the locked rectangle. */
-            map_desc->data = base_memory
-                    + ((box->top / format->block_height) * map_desc->row_pitch)
-                    + ((box->left / format->block_width) * format->block_byte_count);
-        }
-        else
-        {
-            map_desc->data = base_memory
-                    + (map_desc->row_pitch * box->top)
-                    + (box->left * format->byte_count);
-        }
-        surface->lockedRect.left = box->left;
-        surface->lockedRect.top = box->top;
-        surface->lockedRect.right = box->right;
-        surface->lockedRect.bottom = box->bottom;
-    }
-
-    TRACE("Locked rect %s.\n", wine_dbgstr_rect(&surface->lockedRect));
-    TRACE("Returning memory %p, pitch %u.\n", map_desc->data, map_desc->row_pitch);
 
-    return WINED3D_OK;
+    return wined3d_resource_map(&surface->resource, map_desc, box, flags);
 }
 
 HRESULT CDECL wined3d_surface_getdc(struct wined3d_surface *surface, HDC *dc)
diff --git a/dlls/wined3d/volume.c b/dlls/wined3d/volume.c
index a237bf6..0e97cd1 100644
--- a/dlls/wined3d/volume.c
+++ b/dlls/wined3d/volume.c
@@ -402,7 +402,12 @@ HRESULT wined3d_volume_map(struct wined3d_volume *volume,
 
 HRESULT wined3d_volume_unmap(struct wined3d_volume *volume)
 {
-    return wined3d_resource_unmap(&volume->resource);
+    HRESULT hr;
+
+    hr = wined3d_resource_unmap(&volume->resource);
+    if (hr == WINEDDERR_NOTLOCKED)
+        return WINED3DERR_INVALIDCALL;
+    return hr;
 }
 
 static ULONG volume_resource_incref(struct wined3d_resource *resource)
-- 
2.6.2

