[Do not revert] Roll-back V8 to version 4.4.63.
[chromium-blink-merge.git] / cc / test / test_gpu_memory_buffer_manager.cc
blob8339c6257c2194d6f03bc38d3ec85e22aae5d806
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 "ui/gfx/gpu_memory_buffer.h"
10 namespace cc {
11 namespace {
13 size_t NumberOfPlanesForGpuMemoryBufferFormat(
14 gfx::GpuMemoryBuffer::Format format) {
15 switch (format) {
16 case gfx::GpuMemoryBuffer::Format::ATC:
17 case gfx::GpuMemoryBuffer::Format::ATCIA:
18 case gfx::GpuMemoryBuffer::Format::DXT1:
19 case gfx::GpuMemoryBuffer::Format::DXT5:
20 case gfx::GpuMemoryBuffer::Format::ETC1:
21 case gfx::GpuMemoryBuffer::Format::R_8:
22 case gfx::GpuMemoryBuffer::Format::RGBA_8888:
23 case gfx::GpuMemoryBuffer::Format::RGBX_8888:
24 case gfx::GpuMemoryBuffer::Format::BGRA_8888:
25 return 1;
26 case gfx::GpuMemoryBuffer::Format::YUV_420:
27 return 3;
29 NOTREACHED();
30 return 0;
33 size_t SubsamplingFactor(gfx::GpuMemoryBuffer::Format format, int plane) {
34 switch (format) {
35 case gfx::GpuMemoryBuffer::ATC:
36 case gfx::GpuMemoryBuffer::ATCIA:
37 case gfx::GpuMemoryBuffer::DXT1:
38 case gfx::GpuMemoryBuffer::DXT5:
39 case gfx::GpuMemoryBuffer::ETC1:
40 case gfx::GpuMemoryBuffer::R_8:
41 case gfx::GpuMemoryBuffer::RGBA_8888:
42 case gfx::GpuMemoryBuffer::RGBX_8888:
43 case gfx::GpuMemoryBuffer::BGRA_8888:
44 return 1;
45 case gfx::GpuMemoryBuffer::YUV_420: {
46 static size_t factor[] = {1, 2, 2};
47 DCHECK_LT(static_cast<size_t>(plane), arraysize(factor));
48 return factor[plane];
51 NOTREACHED();
52 return 0;
55 size_t StrideInBytes(size_t width,
56 gfx::GpuMemoryBuffer::Format format,
57 int plane) {
58 switch (format) {
59 case gfx::GpuMemoryBuffer::ATCIA:
60 case gfx::GpuMemoryBuffer::DXT5:
61 DCHECK_EQ(plane, 0);
62 return width;
63 case gfx::GpuMemoryBuffer::ATC:
64 case gfx::GpuMemoryBuffer::DXT1:
65 case gfx::GpuMemoryBuffer::ETC1:
66 DCHECK_EQ(plane, 0);
67 DCHECK_EQ(width % 2, 0u);
68 return width / 2;
69 case gfx::GpuMemoryBuffer::R_8:
70 return (width + 3) & ~0x3;
71 case gfx::GpuMemoryBuffer::RGBA_8888:
72 case gfx::GpuMemoryBuffer::RGBX_8888:
73 case gfx::GpuMemoryBuffer::BGRA_8888:
74 DCHECK_EQ(plane, 0);
75 return width * 4;
76 case gfx::GpuMemoryBuffer::YUV_420:
77 return width / SubsamplingFactor(format, plane);
79 NOTREACHED();
80 return 0;
83 size_t BufferSizeInBytes(const gfx::Size& size,
84 gfx::GpuMemoryBuffer::Format format) {
85 size_t size_in_bytes = 0;
86 size_t num_planes = NumberOfPlanesForGpuMemoryBufferFormat(format);
87 for (size_t i = 0; i < num_planes; ++i) {
88 size_in_bytes += StrideInBytes(size.width(), format, i) *
89 (size.height() / SubsamplingFactor(format, i));
91 return size_in_bytes;
94 class GpuMemoryBufferImpl : public gfx::GpuMemoryBuffer {
95 public:
96 GpuMemoryBufferImpl(const gfx::Size& size,
97 Format format,
98 scoped_ptr<base::SharedMemory> shared_memory)
99 : size_(size),
100 format_(format),
101 shared_memory_(shared_memory.Pass()),
102 mapped_(false) {}
104 // Overridden from gfx::GpuMemoryBuffer:
105 bool Map(void** data) override {
106 DCHECK(!mapped_);
107 if (!shared_memory_->Map(BufferSizeInBytes(size_, format_)))
108 return false;
109 mapped_ = true;
110 size_t offset = 0;
111 size_t num_planes = NumberOfPlanesForGpuMemoryBufferFormat(format_);
112 for (size_t i = 0; i < num_planes; ++i) {
113 data[i] = reinterpret_cast<uint8*>(shared_memory_->memory()) + offset;
114 offset += StrideInBytes(size_.width(), format_, i) *
115 (size_.height() / SubsamplingFactor(format_, i));
117 return true;
119 void Unmap() override {
120 DCHECK(mapped_);
121 shared_memory_->Unmap();
122 mapped_ = false;
124 bool IsMapped() const override { return mapped_; }
125 Format GetFormat() const override { return format_; }
126 void GetStride(int* stride) const override {
127 size_t num_planes = NumberOfPlanesForGpuMemoryBufferFormat(format_);
128 for (size_t i = 0; i < num_planes; ++i)
129 stride[i] = StrideInBytes(size_.width(), format_, i);
131 gfx::GpuMemoryBufferHandle GetHandle() const override {
132 gfx::GpuMemoryBufferHandle handle;
133 handle.type = gfx::SHARED_MEMORY_BUFFER;
134 handle.handle = shared_memory_->handle();
135 return handle;
137 ClientBuffer AsClientBuffer() override {
138 return reinterpret_cast<ClientBuffer>(this);
141 private:
142 const gfx::Size size_;
143 gfx::GpuMemoryBuffer::Format format_;
144 scoped_ptr<base::SharedMemory> shared_memory_;
145 bool mapped_;
148 } // namespace
150 TestGpuMemoryBufferManager::TestGpuMemoryBufferManager() {
153 TestGpuMemoryBufferManager::~TestGpuMemoryBufferManager() {
156 scoped_ptr<gfx::GpuMemoryBuffer>
157 TestGpuMemoryBufferManager::AllocateGpuMemoryBuffer(
158 const gfx::Size& size,
159 gfx::GpuMemoryBuffer::Format format,
160 gfx::GpuMemoryBuffer::Usage usage) {
161 scoped_ptr<base::SharedMemory> shared_memory(new base::SharedMemory);
162 if (!shared_memory->CreateAnonymous(BufferSizeInBytes(size, format)))
163 return nullptr;
164 return make_scoped_ptr<gfx::GpuMemoryBuffer>(
165 new GpuMemoryBufferImpl(size, format, shared_memory.Pass()));
168 gfx::GpuMemoryBuffer*
169 TestGpuMemoryBufferManager::GpuMemoryBufferFromClientBuffer(
170 ClientBuffer buffer) {
171 return reinterpret_cast<gfx::GpuMemoryBuffer*>(buffer);
174 void TestGpuMemoryBufferManager::SetDestructionSyncPoint(
175 gfx::GpuMemoryBuffer* buffer,
176 uint32 sync_point) {
179 } // namespace cc