// Copyright 2014 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. #include "gpu/command_buffer/common/buffer.h" #include #include #include "base/format_macros.h" #include "base/logging.h" #include "base/numerics/safe_math.h" #include "base/strings/stringprintf.h" namespace gpu { bool BufferBacking::is_shared() const { return false; } SharedMemoryBufferBacking::SharedMemoryBufferBacking( std::unique_ptr shared_memory, size_t size) : shared_memory_(std::move(shared_memory)), size_(size) {} SharedMemoryBufferBacking::~SharedMemoryBufferBacking() {} bool SharedMemoryBufferBacking::is_shared() const { return true; } void* SharedMemoryBufferBacking::GetMemory() const { return shared_memory_->memory(); } size_t SharedMemoryBufferBacking::GetSize() const { return size_; } Buffer::Buffer(std::unique_ptr backing) : backing_(std::move(backing)), memory_(backing_->GetMemory()), size_(backing_->GetSize()) { DCHECK(memory_) << "The memory must be mapped to create a Buffer"; } Buffer::~Buffer() {} void* Buffer::GetDataAddress(uint32_t data_offset, uint32_t data_size) const { base::CheckedNumeric end = data_offset; end += data_size; if (!end.IsValid() || end.ValueOrDie() > static_cast(size_)) return NULL; return static_cast(memory_) + data_offset; } void* Buffer::GetDataAddressAndSize(uint32_t data_offset, uint32_t* data_size) const { if (data_offset > static_cast(size_)) return NULL; *data_size = GetRemainingSize(data_offset); return static_cast(memory_) + data_offset; } uint32_t Buffer::GetRemainingSize(uint32_t data_offset) const { if (data_offset > static_cast(size_)) return 0; return static_cast(size_) - data_offset; } base::trace_event::MemoryAllocatorDumpGuid GetBufferGUIDForTracing( uint64_t tracing_process_id, int32_t buffer_id) { return base::trace_event::MemoryAllocatorDumpGuid(base::StringPrintf( "gpu-buffer-x-process/%" PRIx64 "/%d", tracing_process_id, buffer_id)); } } // namespace gpu