Gate GPUCommandEncoder.writeTimestamp behind allow_unsafe_apis

This CL makes sure GPUCommandEncoder.writeTimestamp method
requires enabling allow_unsafe_apis toggle as it has been
removed from the spec while we get more experience before
adding it back.

Spec PR: https://github.com/gpuweb/gpuweb/pull/4370

Bug: dawn:1800

Change-Id: Ibc886338876ee8a9877ff4ee8ff8e3bcb65afa0a
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/159921
Reviewed-by: Corentin Wallez <cwallez@chromium.org>
Commit-Queue: Fr <beaufort.francois@gmail.com>
Kokoro: Kokoro <noreply+kokoro@google.com>
diff --git a/src/dawn/native/CommandEncoder.cpp b/src/dawn/native/CommandEncoder.cpp
index ee12458..41cf372 100644
--- a/src/dawn/native/CommandEncoder.cpp
+++ b/src/dawn/native/CommandEncoder.cpp
@@ -2004,6 +2004,9 @@
     mEncodingContext.TryEncode(
         this,
         [&](CommandAllocator* allocator) -> MaybeError {
+            DAWN_INVALID_IF(!GetDevice()->IsToggleEnabled(Toggle::AllowUnsafeAPIs),
+                            "writeTimestamp requires enabling toggle allow_unsafe_apis.");
+
             if (GetDevice()->IsValidationEnabled()) {
                 DAWN_TRY(ValidateTimestampQuery(GetDevice(), querySet, queryIndex));
             }
diff --git a/src/dawn/tests/unittests/validation/UnsafeAPIValidationTests.cpp b/src/dawn/tests/unittests/validation/UnsafeAPIValidationTests.cpp
index b30528f..eae0a95 100644
--- a/src/dawn/tests/unittests/validation/UnsafeAPIValidationTests.cpp
+++ b/src/dawn/tests/unittests/validation/UnsafeAPIValidationTests.cpp
@@ -135,5 +135,33 @@
     }
 }
 
+class TimestampQueryUnsafeAPIValidationTest : public ValidationTest {
+  protected:
+    WGPUDevice CreateTestDevice(native::Adapter dawnAdapter,
+                                wgpu::DeviceDescriptor descriptor) override {
+        wgpu::DawnTogglesDescriptor deviceTogglesDesc;
+        descriptor.nextInChain = &deviceTogglesDesc;
+        const char* toggle = "allow_unsafe_apis";
+        deviceTogglesDesc.disabledToggles = &toggle;
+        deviceTogglesDesc.disabledToggleCount = 1;
+        wgpu::FeatureName requiredFeatures[1] = {wgpu::FeatureName::TimestampQuery};
+        descriptor.requiredFeatures = requiredFeatures;
+        descriptor.requiredFeatureCount = 1;
+        return dawnAdapter.CreateDevice(&descriptor);
+    }
+};
+
+// Check write timestamp on command encoder is an unsafe API.
+TEST_F(TimestampQueryUnsafeAPIValidationTest, WriteTimestampOnCommandEncoder) {
+    wgpu::QuerySetDescriptor descriptor;
+    descriptor.type = wgpu::QueryType::Timestamp;
+    descriptor.count = 2;
+
+    wgpu::QuerySet timestampQuerySet = device.CreateQuerySet(&descriptor);
+    wgpu::CommandEncoder encoder = device.CreateCommandEncoder();
+    encoder.WriteTimestamp(timestampQuerySet, 0);
+    ASSERT_DEVICE_ERROR(encoder.Finish());
+}
+
 }  // anonymous namespace
 }  // namespace dawn