Switch global error menu icon to vectorized MD asset
[chromium-blink-merge.git] / cc / test / test_gpu_memory_buffer_manager.cc
blob0192b40211b7e1a1bee5ee64931dc95c853eb399
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "cc/test/test_gpu_memory_buffer_manager.h"
7 #include "base/logging.h"
8 #include "base/numerics/safe_conversions.h"
9 #include "ui/gfx/buffer_format_util.h"
10 #include "ui/gfx/gpu_memory_buffer.h"
12 namespace cc {
13 namespace {
15 class GpuMemoryBufferImpl : public gfx::GpuMemoryBuffer {
16 public:
17 GpuMemoryBufferImpl(const gfx::Size& size,
18 gfx::BufferFormat format,
19 scoped_ptr<base::SharedMemory> shared_memory)
20 : size_(size),
21 format_(format),
22 shared_memory_(shared_memory.Pass()),
23 mapped_(false) {}
25 // Overridden from gfx::GpuMemoryBuffer:
26 bool Map(void** data) override {
27 DCHECK(!mapped_);
28 if (!shared_memory_->Map(gfx::BufferSizeForBufferFormat(size_, format_)))
29 return false;
30 mapped_ = true;
31 size_t offset = 0;
32 int num_planes =
33 static_cast<int>(gfx::NumberOfPlanesForBufferFormat(format_));
34 for (int i = 0; i < num_planes; ++i) {
35 data[i] = reinterpret_cast<uint8*>(shared_memory_->memory()) + offset;
36 offset +=
37 gfx::RowSizeForBufferFormat(size_.width(), format_, i) *
38 (size_.height() / gfx::SubsamplingFactorForBufferFormat(format_, i));
40 return true;
42 void Unmap() override {
43 DCHECK(mapped_);
44 shared_memory_->Unmap();
45 mapped_ = false;
47 bool IsMapped() const override { return mapped_; }
48 gfx::BufferFormat GetFormat() const override { return format_; }
49 void GetStride(int* stride) const override {
50 int num_planes =
51 static_cast<int>(gfx::NumberOfPlanesForBufferFormat(format_));
52 for (int i = 0; i < num_planes; ++i)
53 stride[i] = base::checked_cast<int>(
54 gfx::RowSizeForBufferFormat(size_.width(), format_, i));
56 gfx::GpuMemoryBufferId GetId() const override {
57 NOTREACHED();
58 return gfx::GpuMemoryBufferId(0);
60 gfx::GpuMemoryBufferHandle GetHandle() const override {
61 gfx::GpuMemoryBufferHandle handle;
62 handle.type = gfx::SHARED_MEMORY_BUFFER;
63 handle.handle = shared_memory_->handle();
64 return handle;
66 ClientBuffer AsClientBuffer() override {
67 return reinterpret_cast<ClientBuffer>(this);
70 private:
71 const gfx::Size size_;
72 gfx::BufferFormat format_;
73 scoped_ptr<base::SharedMemory> shared_memory_;
74 bool mapped_;
77 } // namespace
79 TestGpuMemoryBufferManager::TestGpuMemoryBufferManager() {
82 TestGpuMemoryBufferManager::~TestGpuMemoryBufferManager() {
85 scoped_ptr<gfx::GpuMemoryBuffer>
86 TestGpuMemoryBufferManager::AllocateGpuMemoryBuffer(const gfx::Size& size,
87 gfx::BufferFormat format,
88 gfx::BufferUsage usage) {
89 scoped_ptr<base::SharedMemory> shared_memory(new base::SharedMemory);
90 const size_t buffer_size = gfx::BufferSizeForBufferFormat(size, format);
91 if (!shared_memory->CreateAnonymous(buffer_size))
92 return nullptr;
93 return make_scoped_ptr<gfx::GpuMemoryBuffer>(
94 new GpuMemoryBufferImpl(size, format, shared_memory.Pass()));
97 gfx::GpuMemoryBuffer*
98 TestGpuMemoryBufferManager::GpuMemoryBufferFromClientBuffer(
99 ClientBuffer buffer) {
100 return reinterpret_cast<gfx::GpuMemoryBuffer*>(buffer);
103 void TestGpuMemoryBufferManager::SetDestructionSyncPoint(
104 gfx::GpuMemoryBuffer* buffer,
105 uint32 sync_point) {
108 } // namespace cc