1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
|
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#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 "gpu/gpu_gles2_export.h"
#include "gpu/ipc/common/gpu_disk_cache_type.h"
namespace gpu {
namespace webgpu {
class DawnCachingInterfaceFactory;
namespace detail {
// In memory caching backend that is just a thread-safe wrapper around a map
// with a simple LRU eviction algorithm implemented on top. This is the actual
// backing cache for instances of DawnCachingInterface. The eviction queue is
// set up so that the entries in the front are the first entries to be deleted.
class GPU_GLES2_EXPORT DawnCachingBackend
: public base::RefCounted<DawnCachingBackend> {
public:
explicit DawnCachingBackend(size_t max_size);
size_t LoadData(const std::string& key, void* value_out, size_t value_size);
void StoreData(const std::string& key, const void* value, size_t value_size);
void PurgeMemory(
base::MemoryPressureListener::MemoryPressureLevel memory_pressure_level);
void OnMemoryDump(const std::string& dump_name,
base::trace_event::ProcessMemoryDump* pmd);
private:
// Internal entry class for LRU tracking and holding key/value pair.
class Entry : public base::LinkNode<Entry> {
public:
Entry(const std::string& key, const void* value, size_t value_size);
const std::string& Key() const;
size_t TotalSize() const;
size_t DataSize() const;
size_t ReadData(void* value_out, size_t value_size) const;
private:
const std::string key_;
const std::string data_;
};
// Overrides for transparent flat_set lookups using a string.
friend bool operator<(const std::unique_ptr<Entry>& lhs,
const std::unique_ptr<Entry>& rhs);
friend bool operator<(const std::unique_ptr<Entry>& lhs,
const std::string& rhs);
friend bool operator<(const std::string& lhs,
const std::unique_ptr<Entry>& rhs);
friend class base::RefCounted<DawnCachingBackend>;
~DawnCachingBackend();
void EvictEntry(Entry* entry) EXCLUSIVE_LOCKS_REQUIRED(mutex_);
base::Lock mutex_;
base::flat_set<std::unique_ptr<Entry>> entries_ GUARDED_BY(mutex_);
base::LinkedList<Entry> lru_ GUARDED_BY(mutex_);
size_t max_size_ GUARDED_BY(mutex_);
size_t current_size_ GUARDED_BY(mutex_) = 0;
};
} // namespace detail
// Provides a wrapper class around an in-memory DawnCachingBackend. This class
// was originally designed to handle both disk and in-memory cache backends, but
// because it lives on the GPU process and does not have permissions (due to
// sandbox restrictions) to disk, the disk functionality was removed. Should it
// become necessary to provide interfaces over a disk level disk_cache::Backend,
// please refer to the file history for reference. Note that the big difference
// between in-memory and disk backends are the sync vs async nature of the two
// respectively. Because we are only handling in-memory backends now, the logic
// can be simplified to handle everything synchronously.
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;
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;
// Simplified accessor to the backend.
detail::DawnCachingBackend* backend() { return backend_.get(); }
// Constructor is private because creation of interfaces should be deferred to
// the factory.
explicit DawnCachingInterface(
scoped_refptr<detail::DawnCachingBackend> backend,
CacheBlobCallback callback = {});
// Caching interface owns a reference to the backend.
scoped_refptr<detail::DawnCachingBackend> backend_ = nullptr;
// The callback provides ability to store cache entries to persistent disk.
CacheBlobCallback cache_blob_callback_;
};
// Factory class for producing and managing DawnCachingInterfaces.
// Creating/using caching interfaces through the factory guarantees that we will
// not run into issues where backends are being initialized with the same
// parameters leading to blockage.
class GPU_GLES2_EXPORT DawnCachingInterfaceFactory
: public base::trace_event::MemoryDumpProvider {
public:
// Factory for backend creation, especially for testing.
using BackendFactory =
base::RepeatingCallback<scoped_refptr<detail::DawnCachingBackend>()>;
explicit DawnCachingInterfaceFactory(BackendFactory factory);
DawnCachingInterfaceFactory();
~DawnCachingInterfaceFactory() override;
// Returns a pointer to a DawnCachingInterface, creating a backend for it if
// necessary. For handle based instances, the factory keeps a reference to the
// backend until ReleaseHandle below is called.
std::unique_ptr<DawnCachingInterface> CreateInstance(
const gpu::GpuDiskCacheHandle& handle,
DawnCachingInterface::CacheBlobCallback callback = {});
// Returns a pointer to a DawnCachingInterface that owns the in memory
// backend. This is used for incognito cases where the cache should not be
// persisted to disk.
std::unique_ptr<DawnCachingInterface> CreateInstance();
// Releases the factory held reference of the handle's backend. Generally this
// is the last reference which means that the in-memory disk cache will be
// destroyed and the resources reclaimed. The factory needs to hold an extra
// reference in order to avoid potential races where the browser may be about
// to reuse the same handle, but the last reference on the GPU side has just
// been released causing us to clear the in-memory disk cache too early. When
// that happens, the disk cache entries are not re-sent over to the GPU
// process. To avoid this, when the browser's last reference goes away, it
// notifies the GPU process, and the last reference held by the factory is
// released.
void ReleaseHandle(const gpu::GpuDiskCacheHandle& handle);
void PurgeMemory(
base::MemoryPressureListener::MemoryPressureLevel memory_pressure_level);
// base::trace_event::MemoryDumpProvider implementation.
bool OnMemoryDump(const base::trace_event::MemoryDumpArgs& args,
base::trace_event::ProcessMemoryDump* pmd) override;
private:
// Creates a default backend for assignment.
static scoped_refptr<detail::DawnCachingBackend>
CreateDefaultInMemoryBackend();
// Factory to create backends.
BackendFactory backend_factory_;
// Map that holds existing backends.
base::flat_map<gpu::GpuDiskCacheHandle,
scoped_refptr<detail::DawnCachingBackend>>
backends_;
};
} // namespace webgpu
} // namespace gpu
#endif // GPU_COMMAND_BUFFER_SERVICE_DAWN_CACHING_INTERFACE_H_
|