#ifndef GPU_COMMAND_BUFFER_SERVICE_DAWN_CACHING_INTERFACE_H_
#define GPU_COMMAND_BUFFER_SERVICE_DAWN_CACHING_INTERFACE_H_
#include <dawn/platform/DawnPlatform.h>
#include <memory>
#include <string>
#include "base/containers/flat_map.h"
#include "base/containers/flat_set.h"
#include "base/containers/linked_list.h"
#include "base/functional/callback.h"
#include "base/memory/memory_pressure_listener.h"
#include "base/memory/ref_counted.h"
#include "base/synchronization/lock.h"
#include "base/thread_annotations.h"
#include "base/trace_event/memory_dump_provider.h"
#include "components/persistent_cache/persistent_cache.h"
#include "gpu/command_buffer/common/shm_count.h"
#include "gpu/command_buffer/service/memory_cache.h"
#include "gpu/gpu_gles2_export.h"
#include "gpu/ipc/common/gpu_disk_cache_type.h"
namespace gpu {
class GpuPersistentCache;
namespace webgpu {
class DawnCachingInterfaceFactory;
class GPU_GLES2_EXPORT DawnCachingInterface
: public dawn::platform::CachingInterface {
public:
using CacheBlobCallback =
base::RepeatingCallback<void(const std::string& key,
const std::string& blob)>;
~DawnCachingInterface() override;
void InitializePersistentCache(
persistent_cache::PendingBackend pending_backend,
scoped_refptr<RefCountedGpuProcessShmCount> use_shader_cache_shm_count);
size_t LoadData(const void* key,
size_t key_size,
void* value_out,
size_t value_size) override;
void StoreData(const void* key,
size_t key_size,
const void* value,
size_t value_size) override;
private:
friend class DawnCachingInterfaceFactory;
MemoryCache* memory_cache() { return memory_cache_backend_.get(); }
explicit DawnCachingInterface(scoped_refptr<MemoryCache> backend,
CacheBlobCallback callback = {});
explicit DawnCachingInterface(
scoped_refptr<MemoryCache> backend,
scoped_refptr<GpuPersistentCache> persistent_cache);
scoped_refptr<MemoryCache> memory_cache_backend_ = nullptr;
CacheBlobCallback cache_blob_callback_;
scoped_refptr<GpuPersistentCache> persistent_cache_;
};
class GPU_GLES2_EXPORT DawnCachingInterfaceFactory
: public base::trace_event::MemoryDumpProvider {
public:
using BackendFactory = base::RepeatingCallback<scoped_refptr<MemoryCache>()>;
explicit DawnCachingInterfaceFactory(BackendFactory factory);
DawnCachingInterfaceFactory();
~DawnCachingInterfaceFactory() override;
std::unique_ptr<DawnCachingInterface> CreateInstance(
const gpu::GpuDiskCacheHandle& handle,
DawnCachingInterface::CacheBlobCallback callback = {});
std::unique_ptr<DawnCachingInterface> CreateInstance(
const gpu::GpuDiskCacheHandle& handle,
scoped_refptr<GpuPersistentCache> persistent_cache);
std::unique_ptr<DawnCachingInterface> CreateInstance();
void ReleaseHandle(const gpu::GpuDiskCacheHandle& handle);
void PurgeMemory(base::MemoryPressureLevel memory_pressure_level);
bool OnMemoryDump(const base::trace_event::MemoryDumpArgs& args,
base::trace_event::ProcessMemoryDump* pmd) override;
private:
scoped_refptr<MemoryCache> GetOrCreateMemoryCache(
const gpu::GpuDiskCacheHandle& handle);
static scoped_refptr<MemoryCache> CreateDefaultInMemoryBackend();
BackendFactory backend_factory_;
base::flat_map<gpu::GpuDiskCacheHandle, scoped_refptr<MemoryCache>> backends_;
};
}
}
#endif