[Eraser strings] Remove unused Supervised User infobar and corresponding strings
[chromium-blink-merge.git] / cc / test / test_gpu_memory_buffer_manager.cc
blob6648823b29cd546dee0142ad8d289c4497dfedf5
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 size_t SubsamplingFactor(gfx::BufferFormat format, int plane) {
16 switch (format) {
17 case gfx::BufferFormat::ATC:
18 case gfx::BufferFormat::ATCIA:
19 case gfx::BufferFormat::DXT1:
20 case gfx::BufferFormat::DXT5:
21 case gfx::BufferFormat::ETC1:
22 case gfx::BufferFormat::R_8:
23 case gfx::BufferFormat::RGBA_4444:
24 case gfx::BufferFormat::RGBA_8888:
25 case gfx::BufferFormat::RGBX_8888:
26 case gfx::BufferFormat::BGRA_8888:
27 return 1;
28 case gfx::BufferFormat::YUV_420: {
29 static size_t factor[] = {1, 2, 2};
30 DCHECK_LT(static_cast<size_t>(plane), arraysize(factor));
31 return factor[plane];
33 case gfx::BufferFormat::YUV_420_BIPLANAR: {
34 static size_t factor[] = {1, 2};
35 DCHECK_LT(static_cast<size_t>(plane), arraysize(factor));
36 return factor[plane];
39 NOTREACHED();
40 return 0;
43 size_t StrideInBytes(size_t width, gfx::BufferFormat format, int plane) {
44 switch (format) {
45 case gfx::BufferFormat::ATCIA:
46 case gfx::BufferFormat::DXT5:
47 DCHECK_EQ(plane, 0);
48 return width;
49 case gfx::BufferFormat::ATC:
50 case gfx::BufferFormat::DXT1:
51 case gfx::BufferFormat::ETC1:
52 DCHECK_EQ(plane, 0);
53 DCHECK_EQ(width % 2, 0u);
54 return width / 2;
55 case gfx::BufferFormat::R_8:
56 return (width + 3) & ~0x3;
57 case gfx::BufferFormat::RGBA_4444:
58 DCHECK_EQ(plane, 0);
59 return width * 2;
60 case gfx::BufferFormat::RGBA_8888:
61 case gfx::BufferFormat::RGBX_8888:
62 case gfx::BufferFormat::BGRA_8888:
63 DCHECK_EQ(plane, 0);
64 return width * 4;
65 case gfx::BufferFormat::YUV_420:
66 return width / SubsamplingFactor(format, plane);
67 case gfx::BufferFormat::YUV_420_BIPLANAR:
68 return width;
70 NOTREACHED();
71 return 0;
74 size_t BufferSizeInBytes(const gfx::Size& size, gfx::BufferFormat format) {
75 size_t size_in_bytes = 0;
76 int num_planes = static_cast<int>(gfx::NumberOfPlanesForBufferFormat(format));
77 for (int i = 0; i < num_planes; ++i) {
78 size_in_bytes += StrideInBytes(size.width(), format, i) *
79 (size.height() / SubsamplingFactor(format, i));
81 return size_in_bytes;
84 class GpuMemoryBufferImpl : public gfx::GpuMemoryBuffer {
85 public:
86 GpuMemoryBufferImpl(const gfx::Size& size,
87 gfx::BufferFormat format,
88 scoped_ptr<base::SharedMemory> shared_memory)
89 : size_(size),
90 format_(format),
91 shared_memory_(shared_memory.Pass()),
92 mapped_(false) {}
94 // Overridden from gfx::GpuMemoryBuffer:
95 bool Map(void** data) override {
96 DCHECK(!mapped_);
97 if (!shared_memory_->Map(BufferSizeInBytes(size_, format_)))
98 return false;
99 mapped_ = true;
100 size_t offset = 0;
101 int num_planes =
102 static_cast<int>(gfx::NumberOfPlanesForBufferFormat(format_));
103 for (int i = 0; i < num_planes; ++i) {
104 data[i] = reinterpret_cast<uint8*>(shared_memory_->memory()) + offset;
105 offset += StrideInBytes(size_.width(), format_, i) *
106 (size_.height() / SubsamplingFactor(format_, i));
108 return true;
110 void Unmap() override {
111 DCHECK(mapped_);
112 shared_memory_->Unmap();
113 mapped_ = false;
115 bool IsMapped() const override { return mapped_; }
116 gfx::BufferFormat GetFormat() const override { return format_; }
117 void GetStride(int* stride) const override {
118 int num_planes =
119 static_cast<int>(gfx::NumberOfPlanesForBufferFormat(format_));
120 for (int i = 0; i < num_planes; ++i)
121 stride[i] =
122 base::checked_cast<int>(StrideInBytes(size_.width(), format_, i));
124 gfx::GpuMemoryBufferId GetId() const override {
125 NOTREACHED();
126 return 0;
128 gfx::GpuMemoryBufferHandle GetHandle() const override {
129 gfx::GpuMemoryBufferHandle handle;
130 handle.type = gfx::SHARED_MEMORY_BUFFER;
131 handle.handle = shared_memory_->handle();
132 return handle;
134 ClientBuffer AsClientBuffer() override {
135 return reinterpret_cast<ClientBuffer>(this);
138 private:
139 const gfx::Size size_;
140 gfx::BufferFormat format_;
141 scoped_ptr<base::SharedMemory> shared_memory_;
142 bool mapped_;
145 } // namespace
147 TestGpuMemoryBufferManager::TestGpuMemoryBufferManager() {
150 TestGpuMemoryBufferManager::~TestGpuMemoryBufferManager() {
153 scoped_ptr<gfx::GpuMemoryBuffer>
154 TestGpuMemoryBufferManager::AllocateGpuMemoryBuffer(const gfx::Size& size,
155 gfx::BufferFormat format,
156 gfx::BufferUsage usage) {
157 scoped_ptr<base::SharedMemory> shared_memory(new base::SharedMemory);
158 if (!shared_memory->CreateAnonymous(BufferSizeInBytes(size, format)))
159 return nullptr;
160 return make_scoped_ptr<gfx::GpuMemoryBuffer>(
161 new GpuMemoryBufferImpl(size, format, shared_memory.Pass()));
164 gfx::GpuMemoryBuffer*
165 TestGpuMemoryBufferManager::GpuMemoryBufferFromClientBuffer(
166 ClientBuffer buffer) {
167 return reinterpret_cast<gfx::GpuMemoryBuffer*>(buffer);
170 void TestGpuMemoryBufferManager::SetDestructionSyncPoint(
171 gfx::GpuMemoryBuffer* buffer,
172 uint32 sync_point) {
175 } // namespace cc