Don't show supervised user as "already on this device" while they're being imported.
[chromium-blink-merge.git] / cc / test / test_gpu_memory_buffer_manager.cc
blobcda1d61148c3c0bc71aea41529ffea1eb844d8db
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::ATC:
17 case gfx::GpuMemoryBuffer::ATCIA:
18 case gfx::GpuMemoryBuffer::DXT1:
19 case gfx::GpuMemoryBuffer::DXT5:
20 case gfx::GpuMemoryBuffer::ETC1:
21 case gfx::GpuMemoryBuffer::R_8:
22 case gfx::GpuMemoryBuffer::RGBA_4444:
23 case gfx::GpuMemoryBuffer::RGBA_8888:
24 case gfx::GpuMemoryBuffer::RGBX_8888:
25 case gfx::GpuMemoryBuffer::BGRA_8888:
26 return 1;
27 case gfx::GpuMemoryBuffer::YUV_420:
28 return 3;
30 NOTREACHED();
31 return 0;
34 size_t SubsamplingFactor(gfx::GpuMemoryBuffer::Format format, int plane) {
35 switch (format) {
36 case gfx::GpuMemoryBuffer::ATC:
37 case gfx::GpuMemoryBuffer::ATCIA:
38 case gfx::GpuMemoryBuffer::DXT1:
39 case gfx::GpuMemoryBuffer::DXT5:
40 case gfx::GpuMemoryBuffer::ETC1:
41 case gfx::GpuMemoryBuffer::R_8:
42 case gfx::GpuMemoryBuffer::RGBA_4444:
43 case gfx::GpuMemoryBuffer::RGBA_8888:
44 case gfx::GpuMemoryBuffer::RGBX_8888:
45 case gfx::GpuMemoryBuffer::BGRA_8888:
46 return 1;
47 case gfx::GpuMemoryBuffer::YUV_420: {
48 static size_t factor[] = {1, 2, 2};
49 DCHECK_LT(static_cast<size_t>(plane), arraysize(factor));
50 return factor[plane];
53 NOTREACHED();
54 return 0;
57 size_t StrideInBytes(size_t width,
58 gfx::GpuMemoryBuffer::Format format,
59 int plane) {
60 switch (format) {
61 case gfx::GpuMemoryBuffer::ATCIA:
62 case gfx::GpuMemoryBuffer::DXT5:
63 DCHECK_EQ(plane, 0);
64 return width;
65 case gfx::GpuMemoryBuffer::ATC:
66 case gfx::GpuMemoryBuffer::DXT1:
67 case gfx::GpuMemoryBuffer::ETC1:
68 DCHECK_EQ(plane, 0);
69 DCHECK_EQ(width % 2, 0u);
70 return width / 2;
71 case gfx::GpuMemoryBuffer::R_8:
72 return (width + 3) & ~0x3;
73 case gfx::GpuMemoryBuffer::RGBA_4444:
74 DCHECK_EQ(plane, 0);
75 return width * 2;
76 case gfx::GpuMemoryBuffer::RGBA_8888:
77 case gfx::GpuMemoryBuffer::RGBX_8888:
78 case gfx::GpuMemoryBuffer::BGRA_8888:
79 DCHECK_EQ(plane, 0);
80 return width * 4;
81 case gfx::GpuMemoryBuffer::YUV_420:
82 return width / SubsamplingFactor(format, plane);
84 NOTREACHED();
85 return 0;
88 size_t BufferSizeInBytes(const gfx::Size& size,
89 gfx::GpuMemoryBuffer::Format format) {
90 size_t size_in_bytes = 0;
91 size_t num_planes = NumberOfPlanesForGpuMemoryBufferFormat(format);
92 for (size_t i = 0; i < num_planes; ++i) {
93 size_in_bytes += StrideInBytes(size.width(), format, i) *
94 (size.height() / SubsamplingFactor(format, i));
96 return size_in_bytes;
99 class GpuMemoryBufferImpl : public gfx::GpuMemoryBuffer {
100 public:
101 GpuMemoryBufferImpl(const gfx::Size& size,
102 Format format,
103 scoped_ptr<base::SharedMemory> shared_memory)
104 : size_(size),
105 format_(format),
106 shared_memory_(shared_memory.Pass()),
107 mapped_(false) {}
109 // Overridden from gfx::GpuMemoryBuffer:
110 bool Map(void** data) override {
111 DCHECK(!mapped_);
112 if (!shared_memory_->Map(BufferSizeInBytes(size_, format_)))
113 return false;
114 mapped_ = true;
115 size_t offset = 0;
116 size_t num_planes = NumberOfPlanesForGpuMemoryBufferFormat(format_);
117 for (size_t i = 0; i < num_planes; ++i) {
118 data[i] = reinterpret_cast<uint8*>(shared_memory_->memory()) + offset;
119 offset += StrideInBytes(size_.width(), format_, i) *
120 (size_.height() / SubsamplingFactor(format_, i));
122 return true;
124 void Unmap() override {
125 DCHECK(mapped_);
126 shared_memory_->Unmap();
127 mapped_ = false;
129 bool IsMapped() const override { return mapped_; }
130 Format GetFormat() const override { return format_; }
131 void GetStride(int* stride) const override {
132 size_t num_planes = NumberOfPlanesForGpuMemoryBufferFormat(format_);
133 for (size_t i = 0; i < num_planes; ++i)
134 stride[i] = StrideInBytes(size_.width(), format_, i);
136 gfx::GpuMemoryBufferHandle GetHandle() const override {
137 gfx::GpuMemoryBufferHandle handle;
138 handle.type = gfx::SHARED_MEMORY_BUFFER;
139 handle.handle = shared_memory_->handle();
140 return handle;
142 ClientBuffer AsClientBuffer() override {
143 return reinterpret_cast<ClientBuffer>(this);
146 private:
147 const gfx::Size size_;
148 gfx::GpuMemoryBuffer::Format format_;
149 scoped_ptr<base::SharedMemory> shared_memory_;
150 bool mapped_;
153 } // namespace
155 TestGpuMemoryBufferManager::TestGpuMemoryBufferManager() {
158 TestGpuMemoryBufferManager::~TestGpuMemoryBufferManager() {
161 scoped_ptr<gfx::GpuMemoryBuffer>
162 TestGpuMemoryBufferManager::AllocateGpuMemoryBuffer(
163 const gfx::Size& size,
164 gfx::GpuMemoryBuffer::Format format,
165 gfx::GpuMemoryBuffer::Usage usage) {
166 scoped_ptr<base::SharedMemory> shared_memory(new base::SharedMemory);
167 if (!shared_memory->CreateAnonymous(BufferSizeInBytes(size, format)))
168 return nullptr;
169 return make_scoped_ptr<gfx::GpuMemoryBuffer>(
170 new GpuMemoryBufferImpl(size, format, shared_memory.Pass()));
173 gfx::GpuMemoryBuffer*
174 TestGpuMemoryBufferManager::GpuMemoryBufferFromClientBuffer(
175 ClientBuffer buffer) {
176 return reinterpret_cast<gfx::GpuMemoryBuffer*>(buffer);
179 void TestGpuMemoryBufferManager::SetDestructionSyncPoint(
180 gfx::GpuMemoryBuffer* buffer,
181 uint32 sync_point) {
184 } // namespace cc