Buffer: Always use MappedAtCreation state when applicable.

In a follow-up CL we need to know if the buffer is in the
MappedAtCreation state for validation of GetMappedRange. Having the
state not 100% reflect the state in the spec meant that validation if
GetMappedRange would fail for buffers mapped at creation for which
IsMapWritable is true.

Bug: dawn:445
Change-Id: I4a64335a708b838526da8d65f907b21c782816e0
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/23981
Commit-Queue: Corentin Wallez <cwallez@chromium.org>
Reviewed-by: Kai Ninomiya <kainino@chromium.org>
diff --git a/src/dawn_native/Buffer.cpp b/src/dawn_native/Buffer.cpp
index 3c97655..21a5e43 100644
--- a/src/dawn_native/Buffer.cpp
+++ b/src/dawn_native/Buffer.cpp
@@ -163,16 +163,15 @@
         ASSERT(!IsError());
         ASSERT(mappedPointer != nullptr);
 
+        mState = BufferState::MappedAtCreation;
+
         // Mappable buffers don't use a staging buffer and are just as if mapped through MapAsync.
         if (IsMapWritable()) {
             DAWN_TRY(MapAtCreationImpl(mappedPointer));
-            mState = BufferState::Mapped;
             ASSERT(*mappedPointer != nullptr);
             return {};
         }
 
-        mState = BufferState::MappedAtCreation;
-
         // 0-sized buffers are not supposed to be written to, Return back any non-null pointer.
         if (mSize == 0) {
             *mappedPointer = reinterpret_cast<uint8_t*>(intptr_t(0xCAFED00D));
@@ -322,8 +321,9 @@
         } else if (mState == BufferState::MappedAtCreation) {
             if (mStagingBuffer != nullptr) {
                 mStagingBuffer.reset();
-            } else {
-                ASSERT(mSize == 0);
+            } else if (mSize != 0) {
+                ASSERT(IsMapWritable());
+                Unmap();
             }
         }
 
@@ -371,8 +371,9 @@
         } else if (mState == BufferState::MappedAtCreation) {
             if (mStagingBuffer != nullptr) {
                 GetDevice()->ConsumedError(CopyFromStagingBuffer());
-            } else {
-                ASSERT(mSize == 0);
+            } else if (mSize != 0) {
+                ASSERT(IsMapWritable());
+                UnmapImpl();
             }
         }