chromium/third_party/blink/renderer/modules/webgpu/dawn_object.h

// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef THIRD_PARTY_BLINK_RENDERER_MODULES_WEBGPU_DAWN_OBJECT_H_
#define THIRD_PARTY_BLINK_RENDERER_MODULES_WEBGPU_DAWN_OBJECT_H_

#include "base/memory/scoped_refptr.h"
#include "third_party/blink/renderer/bindings/core/v8/script_value.h"
#include "third_party/blink/renderer/platform/bindings/script_wrappable.h"
#include "third_party/blink/renderer/platform/graphics/gpu/dawn_control_client_holder.h"
#include "third_party/blink/renderer/platform/graphics/gpu/webgpu_cpp.h"
#include "third_party/blink/renderer/platform/heap/member.h"
#include "third_party/blink/renderer/platform/heap/visitor.h"
#include "third_party/blink/renderer/platform/wtf/text/wtf_string.h"

namespace gpu {
namespace webgpu {

class WebGPUInterface;

}  // namespace webgpu
}  // namespace gpu

namespace blink {

namespace scheduler {
class EventLoop;
}  // namespace scheduler

class GPUDevice;

// RAII tracker for known memory allocations outside of V8, informing V8 using
// AdjustAmountOfExternalAllocatedMemory. This causes V8 to collect garbage
// more often when it knows the footprint of V8-managed objects is large.
//
// - It is OK for the tracked size to be an estimate.
// - It is OK to update the tracked size dynamically/asynchronously.
// - It is OK to use this for CPU memory allocated in another process.
// - It is NOT OK to use this for VRAM allocations. This may cause GC to
//   trigger too often: GC is trying to manage CPU memory pressure, but freeing
//   VRAM allocations may or may not reduce CPU memory pressure.
class ExternalMemoryTracker final {};

// This class allows objects to hold onto a DawnControlClientHolder.
// The DawnControlClientHolder is used to hold the WebGPUInterface and keep
// track of whether or not the client has been destroyed. If the client is
// destroyed, we should not call any Dawn functions.
class DawnObjectBase {};

class DawnObjectImpl : public ScriptWrappable, public DawnObjectBase {};

template <typename Obj>
class DawnObject : public DawnObjectImpl {};

template <>
class DawnObject<wgpu::Device> : public DawnObjectBase {};

template <>
class DawnObject<wgpu::Adapter> : public DawnObjectBase {};

}  // namespace blink

#endif  // THIRD_PARTY_BLINK_RENDERER_MODULES_WEBGPU_DAWN_OBJECT_H_