Make sure the global toggle always updates for location.
[chromium-blink-merge.git] / cc / test / test_gpu_memory_buffer_manager.cc
blob154ead3ef6883c6a305947615ce6ebd18b0b413e
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/gpu_memory_buffer.h"
11 namespace cc {
12 namespace {
14 int NumberOfPlanesForGpuMemoryBufferFormat(
15 gfx::GpuMemoryBuffer::Format format) {
16 switch (format) {
17 case gfx::GpuMemoryBuffer::ATC:
18 case gfx::GpuMemoryBuffer::ATCIA:
19 case gfx::GpuMemoryBuffer::DXT1:
20 case gfx::GpuMemoryBuffer::DXT5:
21 case gfx::GpuMemoryBuffer::ETC1:
22 case gfx::GpuMemoryBuffer::R_8:
23 case gfx::GpuMemoryBuffer::RGBA_4444:
24 case gfx::GpuMemoryBuffer::RGBA_8888:
25 case gfx::GpuMemoryBuffer::RGBX_8888:
26 case gfx::GpuMemoryBuffer::BGRA_8888:
27 return 1;
28 case gfx::GpuMemoryBuffer::YUV_420:
29 return 3;
31 NOTREACHED();
32 return 0;
35 size_t SubsamplingFactor(gfx::GpuMemoryBuffer::Format format, int plane) {
36 switch (format) {
37 case gfx::GpuMemoryBuffer::ATC:
38 case gfx::GpuMemoryBuffer::ATCIA:
39 case gfx::GpuMemoryBuffer::DXT1:
40 case gfx::GpuMemoryBuffer::DXT5:
41 case gfx::GpuMemoryBuffer::ETC1:
42 case gfx::GpuMemoryBuffer::R_8:
43 case gfx::GpuMemoryBuffer::RGBA_4444:
44 case gfx::GpuMemoryBuffer::RGBA_8888:
45 case gfx::GpuMemoryBuffer::RGBX_8888:
46 case gfx::GpuMemoryBuffer::BGRA_8888:
47 return 1;
48 case gfx::GpuMemoryBuffer::YUV_420: {
49 static size_t factor[] = {1, 2, 2};
50 DCHECK_LT(static_cast<size_t>(plane), arraysize(factor));
51 return factor[plane];
54 NOTREACHED();
55 return 0;
58 size_t StrideInBytes(size_t width,
59 gfx::GpuMemoryBuffer::Format format,
60 int plane) {
61 switch (format) {
62 case gfx::GpuMemoryBuffer::ATCIA:
63 case gfx::GpuMemoryBuffer::DXT5:
64 DCHECK_EQ(plane, 0);
65 return width;
66 case gfx::GpuMemoryBuffer::ATC:
67 case gfx::GpuMemoryBuffer::DXT1:
68 case gfx::GpuMemoryBuffer::ETC1:
69 DCHECK_EQ(plane, 0);
70 DCHECK_EQ(width % 2, 0u);
71 return width / 2;
72 case gfx::GpuMemoryBuffer::R_8:
73 return (width + 3) & ~0x3;
74 case gfx::GpuMemoryBuffer::RGBA_4444:
75 DCHECK_EQ(plane, 0);
76 return width * 2;
77 case gfx::GpuMemoryBuffer::RGBA_8888:
78 case gfx::GpuMemoryBuffer::RGBX_8888:
79 case gfx::GpuMemoryBuffer::BGRA_8888:
80 DCHECK_EQ(plane, 0);
81 return width * 4;
82 case gfx::GpuMemoryBuffer::YUV_420:
83 return width / SubsamplingFactor(format, plane);
85 NOTREACHED();
86 return 0;
89 size_t BufferSizeInBytes(const gfx::Size& size,
90 gfx::GpuMemoryBuffer::Format format) {
91 size_t size_in_bytes = 0;
92 int num_planes = NumberOfPlanesForGpuMemoryBufferFormat(format);
93 for (int i = 0; i < num_planes; ++i) {
94 size_in_bytes += StrideInBytes(size.width(), format, i) *
95 (size.height() / SubsamplingFactor(format, i));
97 return size_in_bytes;
100 class GpuMemoryBufferImpl : public gfx::GpuMemoryBuffer {
101 public:
102 GpuMemoryBufferImpl(const gfx::Size& size,
103 Format format,
104 scoped_ptr<base::SharedMemory> shared_memory)
105 : size_(size),
106 format_(format),
107 shared_memory_(shared_memory.Pass()),
108 mapped_(false) {}
110 // Overridden from gfx::GpuMemoryBuffer:
111 bool Map(void** data) override {
112 DCHECK(!mapped_);
113 if (!shared_memory_->Map(BufferSizeInBytes(size_, format_)))
114 return false;
115 mapped_ = true;
116 size_t offset = 0;
117 int num_planes = NumberOfPlanesForGpuMemoryBufferFormat(format_);
118 for (int i = 0; i < num_planes; ++i) {
119 data[i] = reinterpret_cast<uint8*>(shared_memory_->memory()) + offset;
120 offset += StrideInBytes(size_.width(), format_, i) *
121 (size_.height() / SubsamplingFactor(format_, i));
123 return true;
125 void Unmap() override {
126 DCHECK(mapped_);
127 shared_memory_->Unmap();
128 mapped_ = false;
130 bool IsMapped() const override { return mapped_; }
131 Format GetFormat() const override { return format_; }
132 void GetStride(int* stride) const override {
133 int num_planes = NumberOfPlanesForGpuMemoryBufferFormat(format_);
134 for (int i = 0; i < num_planes; ++i)
135 stride[i] =
136 base::checked_cast<int>(StrideInBytes(size_.width(), format_, i));
138 gfx::GpuMemoryBufferHandle GetHandle() const override {
139 gfx::GpuMemoryBufferHandle handle;
140 handle.type = gfx::SHARED_MEMORY_BUFFER;
141 handle.handle = shared_memory_->handle();
142 return handle;
144 ClientBuffer AsClientBuffer() override {
145 return reinterpret_cast<ClientBuffer>(this);
148 private:
149 const gfx::Size size_;
150 gfx::GpuMemoryBuffer::Format format_;
151 scoped_ptr<base::SharedMemory> shared_memory_;
152 bool mapped_;
155 } // namespace
157 TestGpuMemoryBufferManager::TestGpuMemoryBufferManager() {
160 TestGpuMemoryBufferManager::~TestGpuMemoryBufferManager() {
163 scoped_ptr<gfx::GpuMemoryBuffer>
164 TestGpuMemoryBufferManager::AllocateGpuMemoryBuffer(
165 const gfx::Size& size,
166 gfx::GpuMemoryBuffer::Format format,
167 gfx::GpuMemoryBuffer::Usage usage) {
168 scoped_ptr<base::SharedMemory> shared_memory(new base::SharedMemory);
169 if (!shared_memory->CreateAnonymous(BufferSizeInBytes(size, format)))
170 return nullptr;
171 return make_scoped_ptr<gfx::GpuMemoryBuffer>(
172 new GpuMemoryBufferImpl(size, format, shared_memory.Pass()));
175 gfx::GpuMemoryBuffer*
176 TestGpuMemoryBufferManager::GpuMemoryBufferFromClientBuffer(
177 ClientBuffer buffer) {
178 return reinterpret_cast<gfx::GpuMemoryBuffer*>(buffer);
181 void TestGpuMemoryBufferManager::SetDestructionSyncPoint(
182 gfx::GpuMemoryBuffer* buffer,
183 uint32 sync_point) {
186 } // namespace cc