
The code touched here wasn't consistent in its use of const, but generally used it in a shallow-const fashion where even vending write access to a block of memory could be done via a const method. This causes problems when trying to enforce stricter lifetime checks on writable spans, because either the code gets confused about whether the accesses are read-only (it tries to look for the constness of the returned pointers and can't figure out what to do if things don't match everywhere) or it thinks something unsafe or non-sane is happening (write access to rvalues makes no sense, for example, but read access might in the context of a short-lived call). Instead consistently model deep constness, which is compliant with Chrome's style rules on const and fixes all these issues. Mostly, this means changing const members/ref args to non-const ones. Bug: 372381413 Change-Id: I2735c52fbf0f32b813055cbb46f7c15b09eb025f Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/5939406 Reviewed-by: Daniel Cheng <dcheng@chromium.org> Owners-Override: Daniel Cheng <dcheng@chromium.org> Auto-Submit: Peter Kasting <pkasting@chromium.org> Reviewed-by: Fred Shih <ffred@chromium.org> Reviewed-by: Alex Gough <ajgo@chromium.org> Commit-Queue: Daniel Cheng <dcheng@chromium.org> Code-Coverage: findit-for-me@appspot.gserviceaccount.com <findit-for-me@appspot.gserviceaccount.com> Cr-Commit-Position: refs/heads/main@{#1373012}
135 lines
4.4 KiB
C++
135 lines
4.4 KiB
C++
// Copyright 2014 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_COMMON_BUFFER_H_
|
|
#define GPU_COMMAND_BUFFER_COMMON_BUFFER_H_
|
|
|
|
#include <stddef.h>
|
|
#include <stdint.h>
|
|
|
|
#include <memory>
|
|
#include <utility>
|
|
|
|
#include "base/memory/raw_ptr.h"
|
|
#include "base/memory/ref_counted.h"
|
|
#include "base/memory/unsafe_shared_memory_region.h"
|
|
#include "base/trace_event/memory_allocator_dump.h"
|
|
#include "gpu/gpu_export.h"
|
|
|
|
namespace gpu {
|
|
|
|
class GPU_EXPORT BufferBacking {
|
|
public:
|
|
virtual ~BufferBacking() = default;
|
|
virtual const base::UnsafeSharedMemoryRegion& shared_memory_region() const;
|
|
virtual base::UnguessableToken GetGUID() const;
|
|
void* GetMemory() {
|
|
return const_cast<void*>(std::as_const(*this).GetMemory());
|
|
}
|
|
virtual const void* GetMemory() const = 0;
|
|
virtual uint32_t GetSize() const = 0;
|
|
};
|
|
|
|
class GPU_EXPORT MemoryBufferBacking : public BufferBacking {
|
|
public:
|
|
explicit MemoryBufferBacking(uint32_t size, uint32_t alignment = 0);
|
|
|
|
MemoryBufferBacking(const MemoryBufferBacking&) = delete;
|
|
MemoryBufferBacking& operator=(const MemoryBufferBacking&) = delete;
|
|
|
|
~MemoryBufferBacking() override;
|
|
const void* GetMemory() const override;
|
|
uint32_t GetSize() const override;
|
|
|
|
private:
|
|
std::unique_ptr<char[]> memory_;
|
|
uint32_t size_;
|
|
uint32_t alignment_;
|
|
};
|
|
|
|
|
|
class GPU_EXPORT SharedMemoryBufferBacking : public BufferBacking {
|
|
public:
|
|
SharedMemoryBufferBacking(
|
|
base::UnsafeSharedMemoryRegion shared_memory_region,
|
|
base::WritableSharedMemoryMapping shared_memory_mapping);
|
|
|
|
SharedMemoryBufferBacking(const SharedMemoryBufferBacking&) = delete;
|
|
SharedMemoryBufferBacking& operator=(const SharedMemoryBufferBacking&) =
|
|
delete;
|
|
|
|
~SharedMemoryBufferBacking() override;
|
|
const base::UnsafeSharedMemoryRegion& shared_memory_region() const override;
|
|
base::UnguessableToken GetGUID() const override;
|
|
const void* GetMemory() const override;
|
|
uint32_t GetSize() const override;
|
|
|
|
private:
|
|
base::UnsafeSharedMemoryRegion shared_memory_region_;
|
|
base::WritableSharedMemoryMapping shared_memory_mapping_;
|
|
};
|
|
|
|
// Buffer owns a piece of shared-memory of a certain size.
|
|
class GPU_EXPORT Buffer : public base::RefCountedThreadSafe<Buffer> {
|
|
public:
|
|
explicit Buffer(std::unique_ptr<BufferBacking> backing);
|
|
|
|
Buffer(const Buffer&) = delete;
|
|
Buffer& operator=(const Buffer&) = delete;
|
|
|
|
BufferBacking* backing() const { return backing_.get(); }
|
|
void* memory() { return memory_; }
|
|
const void* memory() const { return memory_; }
|
|
uint32_t size() const { return size_; }
|
|
|
|
// Returns nullptr if the address overflows the memory.
|
|
void* GetDataAddress(uint32_t data_offset, uint32_t data_size) const;
|
|
|
|
// Returns nullptr if the address overflows the memory.
|
|
void* GetDataAddressAndSize(uint32_t data_offset, uint32_t* data_size) const;
|
|
|
|
// Returns the remaining size of the buffer after an offset
|
|
uint32_t GetRemainingSize(uint32_t data_offset) const;
|
|
|
|
private:
|
|
friend class base::RefCountedThreadSafe<Buffer>;
|
|
~Buffer();
|
|
|
|
std::unique_ptr<BufferBacking> backing_;
|
|
raw_ptr<void> memory_;
|
|
uint32_t size_;
|
|
};
|
|
|
|
inline std::unique_ptr<BufferBacking> MakeBackingFromSharedMemory(
|
|
base::UnsafeSharedMemoryRegion shared_memory_region,
|
|
base::WritableSharedMemoryMapping shared_memory_mapping) {
|
|
return std::make_unique<SharedMemoryBufferBacking>(
|
|
std::move(shared_memory_region), std::move(shared_memory_mapping));
|
|
}
|
|
inline scoped_refptr<Buffer> MakeBufferFromSharedMemory(
|
|
base::UnsafeSharedMemoryRegion shared_memory_region,
|
|
base::WritableSharedMemoryMapping shared_memory_mapping) {
|
|
return base::MakeRefCounted<Buffer>(MakeBackingFromSharedMemory(
|
|
std::move(shared_memory_region), std::move(shared_memory_mapping)));
|
|
}
|
|
|
|
inline scoped_refptr<Buffer> MakeMemoryBuffer(uint32_t size,
|
|
uint32_t alignment = 0) {
|
|
return base::MakeRefCounted<Buffer>(
|
|
std::make_unique<MemoryBufferBacking>(size, alignment));
|
|
}
|
|
|
|
// Generates a process unique buffer ID which can be safely used with
|
|
// GetBufferGUIDForTracing.
|
|
GPU_EXPORT int32_t GetNextBufferId();
|
|
|
|
// Generates GUID which can be used to trace buffer using an Id.
|
|
GPU_EXPORT base::trace_event::MemoryAllocatorDumpGuid GetBufferGUIDForTracing(
|
|
uint64_t tracing_process_id,
|
|
int32_t buffer_id);
|
|
|
|
} // namespace gpu
|
|
|
|
#endif // GPU_COMMAND_BUFFER_COMMON_BUFFER_H_
|