Slab-allocate OpenGL bind groups

Now that all backends use slab-allocated bind groups, this patch also
moves the BindGroup implementation with owned-data into the Null backend.

Bug: dawn:340
Change-Id: I08a952075b382008fb82f1fbab3f779cc05bc2a3
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/16747
Commit-Queue: Austin Eng <enga@chromium.org>
Reviewed-by: Kai Ninomiya <kainino@chromium.org>
diff --git a/BUILD.gn b/BUILD.gn
index c5dfa5d..d1ee8cf 100644
--- a/BUILD.gn
+++ b/BUILD.gn
@@ -412,6 +412,10 @@
     sources += [
       "src/dawn_native/opengl/BackendGL.cpp",
       "src/dawn_native/opengl/BackendGL.h",
+      "src/dawn_native/opengl/BindGroupGL.cpp",
+      "src/dawn_native/opengl/BindGroupGL.h",
+      "src/dawn_native/opengl/BindGroupLayoutGL.cpp",
+      "src/dawn_native/opengl/BindGroupLayoutGL.h",
       "src/dawn_native/opengl/BufferGL.cpp",
       "src/dawn_native/opengl/BufferGL.h",
       "src/dawn_native/opengl/CommandBufferGL.cpp",
diff --git a/src/dawn_native/BindGroup.cpp b/src/dawn_native/BindGroup.cpp
index 491d8f2..9e64c46 100644
--- a/src/dawn_native/BindGroup.cpp
+++ b/src/dawn_native/BindGroup.cpp
@@ -181,26 +181,6 @@
         return {};
     }
 
-    // OwnBindingDataHolder
-
-    OwnBindingDataHolder::OwnBindingDataHolder(size_t size)
-        : mBindingDataAllocation(malloc(size))  // malloc is guaranteed to return a
-                                                // pointer aligned enough for the allocation
-    {
-    }
-
-    OwnBindingDataHolder::~OwnBindingDataHolder() {
-        free(mBindingDataAllocation);
-    }
-
-    // BindGroupBaseOwnBindingData
-
-    BindGroupBaseOwnBindingData::BindGroupBaseOwnBindingData(DeviceBase* device,
-                                                             const BindGroupDescriptor* descriptor)
-        : OwnBindingDataHolder(descriptor->layout->GetBindingDataSize()),
-          BindGroupBase(device, descriptor, mBindingDataAllocation) {
-    }
-
     // BindGroup
 
     BindGroupBase::BindGroupBase(DeviceBase* device,
diff --git a/src/dawn_native/BindGroup.h b/src/dawn_native/BindGroup.h
index 3255171..68322b2 100644
--- a/src/dawn_native/BindGroup.h
+++ b/src/dawn_native/BindGroup.h
@@ -77,24 +77,6 @@
         BindGroupLayoutBase::BindingDataPointers mBindingData;
     };
 
-    // Helper class so |BindGroupBaseOwnBindingData| can allocate memory for its binding data,
-    // before calling the BindGroupBase base class constructor.
-    class OwnBindingDataHolder {
-      protected:
-        explicit OwnBindingDataHolder(size_t size);
-        ~OwnBindingDataHolder();
-
-        void* mBindingDataAllocation;
-    };
-
-    // We don't have the complexity of placement-allocation of bind group data in
-    // the Null backend. This class, keeps the binding data in a separate allocation for simplicity.
-    class BindGroupBaseOwnBindingData : private OwnBindingDataHolder, public BindGroupBase {
-      public:
-        BindGroupBaseOwnBindingData(DeviceBase* device, const BindGroupDescriptor* descriptor);
-        ~BindGroupBaseOwnBindingData() override = default;
-    };
-
 }  // namespace dawn_native
 
 #endif  // DAWNNATIVE_BINDGROUP_H_
diff --git a/src/dawn_native/CMakeLists.txt b/src/dawn_native/CMakeLists.txt
index 4b45307..2bab3e5 100644
--- a/src/dawn_native/CMakeLists.txt
+++ b/src/dawn_native/CMakeLists.txt
@@ -295,6 +295,10 @@
         ${DAWN_NATIVE_OPENGL_AUTOGEN_SOURCES}
         "opengl/BackendGL.cpp"
         "opengl/BackendGL.h"
+        "opengl/BindGroupGL.cpp"
+        "opengl/BindGroupGL.h"
+        "opengl/BindGroupLayoutGL.cpp"
+        "opengl/BindGroupLayoutGL.h"
         "opengl/BufferGL.cpp"
         "opengl/BufferGL.h"
         "opengl/CommandBufferGL.cpp"
diff --git a/src/dawn_native/null/DeviceNull.cpp b/src/dawn_native/null/DeviceNull.cpp
index 64bde46..43b38bf 100644
--- a/src/dawn_native/null/DeviceNull.cpp
+++ b/src/dawn_native/null/DeviceNull.cpp
@@ -252,6 +252,25 @@
         mLastSubmittedSerial++;
     }
 
+    // BindGroupDataHolder
+
+    BindGroupDataHolder::BindGroupDataHolder(size_t size)
+        : mBindingDataAllocation(malloc(size))  // malloc is guaranteed to return a
+                                                // pointer aligned enough for the allocation
+    {
+    }
+
+    BindGroupDataHolder::~BindGroupDataHolder() {
+        free(mBindingDataAllocation);
+    }
+
+    // BindGroup
+
+    BindGroup::BindGroup(DeviceBase* device, const BindGroupDescriptor* descriptor)
+        : BindGroupDataHolder(descriptor->layout->GetBindingDataSize()),
+          BindGroupBase(device, descriptor, mBindingDataAllocation) {
+    }
+
     // Buffer
 
     struct BufferMapOperation : PendingOperation {
diff --git a/src/dawn_native/null/DeviceNull.h b/src/dawn_native/null/DeviceNull.h
index cd52971..b41061e 100644
--- a/src/dawn_native/null/DeviceNull.h
+++ b/src/dawn_native/null/DeviceNull.h
@@ -38,7 +38,7 @@
 namespace dawn_native { namespace null {
 
     class Adapter;
-    using BindGroup = BindGroupBaseOwnBindingData;
+    class BindGroup;
     using BindGroupLayout = BindGroupLayoutBase;
     class Buffer;
     class CommandBuffer;
@@ -157,6 +157,24 @@
         ResultOrError<DeviceBase*> CreateDeviceImpl(const DeviceDescriptor* descriptor) override;
     };
 
+    // Helper class so |BindGroup| can allocate memory for its binding data,
+    // before calling the BindGroupBase base class constructor.
+    class BindGroupDataHolder {
+      protected:
+        explicit BindGroupDataHolder(size_t size);
+        ~BindGroupDataHolder();
+
+        void* mBindingDataAllocation;
+    };
+
+    // We don't have the complexity of placement-allocation of bind group data in
+    // the Null backend. This class, keeps the binding data in a separate allocation for simplicity.
+    class BindGroup : private BindGroupDataHolder, public BindGroupBase {
+      public:
+        BindGroup(DeviceBase* device, const BindGroupDescriptor* descriptor);
+        ~BindGroup() override = default;
+    };
+
     class Buffer : public BufferBase {
       public:
         Buffer(Device* device, const BufferDescriptor* descriptor);
diff --git a/src/dawn_native/opengl/BindGroupGL.cpp b/src/dawn_native/opengl/BindGroupGL.cpp
new file mode 100644
index 0000000..383607b
--- /dev/null
+++ b/src/dawn_native/opengl/BindGroupGL.cpp
@@ -0,0 +1,35 @@
+// Copyright 2020 The Dawn Authors
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#include "dawn_native/opengl/BindGroupGL.h"
+
+#include "dawn_native/opengl/BindGroupLayoutGL.h"
+#include "dawn_native/opengl/DeviceGL.h"
+
+namespace dawn_native { namespace opengl {
+
+    BindGroup::BindGroup(Device* device, const BindGroupDescriptor* descriptor)
+        : BindGroupBase(this, device, descriptor) {
+    }
+
+    BindGroup::~BindGroup() {
+        ToBackend(GetLayout())->DeallocateBindGroup(this);
+    }
+
+    // static
+    BindGroup* BindGroup::Create(Device* device, const BindGroupDescriptor* descriptor) {
+        return ToBackend(descriptor->layout)->AllocateBindGroup(device, descriptor);
+    }
+
+}}  // namespace dawn_native::opengl
diff --git a/src/dawn_native/opengl/BindGroupGL.h b/src/dawn_native/opengl/BindGroupGL.h
new file mode 100644
index 0000000..9ce8ed7
--- /dev/null
+++ b/src/dawn_native/opengl/BindGroupGL.h
@@ -0,0 +1,36 @@
+// Copyright 2020 The Dawn Authors
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#ifndef DAWNNATIVE_OPENGL_BINDGROUPGL_H_
+#define DAWNNATIVE_OPENGL_BINDGROUPGL_H_
+
+#include "common/PlacementAllocated.h"
+#include "dawn_native/BindGroup.h"
+
+namespace dawn_native { namespace opengl {
+
+    class BindGroupLayout;
+    class Device;
+
+    class BindGroup : public BindGroupBase, public PlacementAllocated {
+      public:
+        BindGroup(Device* device, const BindGroupDescriptor* descriptor);
+        ~BindGroup() override;
+
+        static BindGroup* Create(Device* device, const BindGroupDescriptor* descriptor);
+    };
+
+}}  // namespace dawn_native::opengl
+
+#endif  // DAWNNATIVE_OPENGL_BINDGROUPGL_H_
diff --git a/src/dawn_native/opengl/BindGroupLayoutGL.cpp b/src/dawn_native/opengl/BindGroupLayoutGL.cpp
new file mode 100644
index 0000000..7c098c8
--- /dev/null
+++ b/src/dawn_native/opengl/BindGroupLayoutGL.cpp
@@ -0,0 +1,36 @@
+// Copyright 2020 The Dawn Authors
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#include "dawn_native/opengl/BindGroupLayoutGL.h"
+
+#include "dawn_native/opengl/BindGroupGL.h"
+
+namespace dawn_native { namespace opengl {
+
+    BindGroupLayout::BindGroupLayout(DeviceBase* device,
+                                     const BindGroupLayoutDescriptor* descriptor)
+        : BindGroupLayoutBase(device, descriptor),
+          mBindGroupAllocator(MakeFrontendBindGroupAllocator<BindGroup>(4096)) {
+    }
+
+    BindGroup* BindGroupLayout::AllocateBindGroup(Device* device,
+                                                  const BindGroupDescriptor* descriptor) {
+        return mBindGroupAllocator.Allocate(device, descriptor);
+    }
+
+    void BindGroupLayout::DeallocateBindGroup(BindGroup* bindGroup) {
+        mBindGroupAllocator.Deallocate(bindGroup);
+    }
+
+}}  // namespace dawn_native::opengl
diff --git a/src/dawn_native/opengl/BindGroupLayoutGL.h b/src/dawn_native/opengl/BindGroupLayoutGL.h
new file mode 100644
index 0000000..ab8dab4
--- /dev/null
+++ b/src/dawn_native/opengl/BindGroupLayoutGL.h
@@ -0,0 +1,39 @@
+// Copyright 2020 The Dawn Authors
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#ifndef DAWNNATIVE_OPENGL_BINDGROUPLAYOUTGL_H_
+#define DAWNNATIVE_OPENGL_BINDGROUPLAYOUTGL_H_
+
+#include "common/SlabAllocator.h"
+#include "dawn_native/BindGroupLayout.h"
+
+namespace dawn_native { namespace opengl {
+
+    class BindGroup;
+    class Device;
+
+    class BindGroupLayout : public BindGroupLayoutBase {
+      public:
+        BindGroupLayout(DeviceBase* device, const BindGroupLayoutDescriptor* descriptor);
+
+        BindGroup* AllocateBindGroup(Device* device, const BindGroupDescriptor* descriptor);
+        void DeallocateBindGroup(BindGroup* bindGroup);
+
+      private:
+        SlabAllocator<BindGroup> mBindGroupAllocator;
+    };
+
+}}  // namespace dawn_native::opengl
+
+#endif  // DAWNNATIVE_OPENGL_BINDGROUPLAYOUTGL_H_
diff --git a/src/dawn_native/opengl/DeviceGL.cpp b/src/dawn_native/opengl/DeviceGL.cpp
index 26d2c33..7cf41a0 100644
--- a/src/dawn_native/opengl/DeviceGL.cpp
+++ b/src/dawn_native/opengl/DeviceGL.cpp
@@ -15,10 +15,11 @@
 #include "dawn_native/opengl/DeviceGL.h"
 
 #include "dawn_native/BackendConnection.h"
-#include "dawn_native/BindGroup.h"
 #include "dawn_native/BindGroupLayout.h"
 #include "dawn_native/DynamicUploader.h"
 #include "dawn_native/ErrorData.h"
+#include "dawn_native/opengl/BindGroupGL.h"
+#include "dawn_native/opengl/BindGroupLayoutGL.h"
 #include "dawn_native/opengl/BufferGL.h"
 #include "dawn_native/opengl/CommandBufferGL.h"
 #include "dawn_native/opengl/ComputePipelineGL.h"
@@ -82,7 +83,7 @@
 
     ResultOrError<BindGroupBase*> Device::CreateBindGroupImpl(
         const BindGroupDescriptor* descriptor) {
-        return new BindGroup(this, descriptor);
+        return BindGroup::Create(this, descriptor);
     }
     ResultOrError<BindGroupLayoutBase*> Device::CreateBindGroupLayoutImpl(
         const BindGroupLayoutDescriptor* descriptor) {
diff --git a/src/dawn_native/opengl/Forward.h b/src/dawn_native/opengl/Forward.h
index 8b4f20b..bd2cc76 100644
--- a/src/dawn_native/opengl/Forward.h
+++ b/src/dawn_native/opengl/Forward.h
@@ -17,17 +17,11 @@
 
 #include "dawn_native/ToBackend.h"
 
-namespace dawn_native {
-    class BindGroupBaseOwnBindingData;
-    class BindGroupLayoutBase;
-    struct RenderPassDescriptor;
-}  // namespace dawn_native
-
 namespace dawn_native { namespace opengl {
 
     class Adapter;
-    using BindGroup = BindGroupBaseOwnBindingData;
-    using BindGroupLayout = BindGroupLayoutBase;
+    class BindGroup;
+    class BindGroupLayout;
     class Buffer;
     class CommandBuffer;
     class ComputePipeline;