Add ICU message format support
[chromium-blink-merge.git] / cc / test / test_gpu_memory_buffer_manager.cc
blobd70b74083c9ddef19ffc3f48653654935191e616
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(gfx::BufferFormat format) {
15 switch (format) {
16 case gfx::BufferFormat::ATC:
17 case gfx::BufferFormat::ATCIA:
18 case gfx::BufferFormat::DXT1:
19 case gfx::BufferFormat::DXT5:
20 case gfx::BufferFormat::ETC1:
21 case gfx::BufferFormat::R_8:
22 case gfx::BufferFormat::RGBA_4444:
23 case gfx::BufferFormat::RGBA_8888:
24 case gfx::BufferFormat::RGBX_8888:
25 case gfx::BufferFormat::BGRA_8888:
26 return 1;
27 case gfx::BufferFormat::YUV_420:
28 return 3;
30 NOTREACHED();
31 return 0;
34 size_t SubsamplingFactor(gfx::BufferFormat format, int plane) {
35 switch (format) {
36 case gfx::BufferFormat::ATC:
37 case gfx::BufferFormat::ATCIA:
38 case gfx::BufferFormat::DXT1:
39 case gfx::BufferFormat::DXT5:
40 case gfx::BufferFormat::ETC1:
41 case gfx::BufferFormat::R_8:
42 case gfx::BufferFormat::RGBA_4444:
43 case gfx::BufferFormat::RGBA_8888:
44 case gfx::BufferFormat::RGBX_8888:
45 case gfx::BufferFormat::BGRA_8888:
46 return 1;
47 case gfx::BufferFormat::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, gfx::BufferFormat format, int plane) {
58 switch (format) {
59 case gfx::BufferFormat::ATCIA:
60 case gfx::BufferFormat::DXT5:
61 DCHECK_EQ(plane, 0);
62 return width;
63 case gfx::BufferFormat::ATC:
64 case gfx::BufferFormat::DXT1:
65 case gfx::BufferFormat::ETC1:
66 DCHECK_EQ(plane, 0);
67 DCHECK_EQ(width % 2, 0u);
68 return width / 2;
69 case gfx::BufferFormat::R_8:
70 return (width + 3) & ~0x3;
71 case gfx::BufferFormat::RGBA_4444:
72 DCHECK_EQ(plane, 0);
73 return width * 2;
74 case gfx::BufferFormat::RGBA_8888:
75 case gfx::BufferFormat::RGBX_8888:
76 case gfx::BufferFormat::BGRA_8888:
77 DCHECK_EQ(plane, 0);
78 return width * 4;
79 case gfx::BufferFormat::YUV_420:
80 return width / SubsamplingFactor(format, plane);
82 NOTREACHED();
83 return 0;
86 size_t BufferSizeInBytes(const gfx::Size& size, gfx::BufferFormat format) {
87 size_t size_in_bytes = 0;
88 int num_planes = NumberOfPlanesForGpuMemoryBufferFormat(format);
89 for (int i = 0; i < num_planes; ++i) {
90 size_in_bytes += StrideInBytes(size.width(), format, i) *
91 (size.height() / SubsamplingFactor(format, i));
93 return size_in_bytes;
96 class GpuMemoryBufferImpl : public gfx::GpuMemoryBuffer {
97 public:
98 GpuMemoryBufferImpl(const gfx::Size& size,
99 gfx::BufferFormat format,
100 scoped_ptr<base::SharedMemory> shared_memory)
101 : size_(size),
102 format_(format),
103 shared_memory_(shared_memory.Pass()),
104 mapped_(false) {}
106 // Overridden from gfx::GpuMemoryBuffer:
107 bool Map(void** data) override {
108 DCHECK(!mapped_);
109 if (!shared_memory_->Map(BufferSizeInBytes(size_, format_)))
110 return false;
111 mapped_ = true;
112 size_t offset = 0;
113 int num_planes = NumberOfPlanesForGpuMemoryBufferFormat(format_);
114 for (int i = 0; i < num_planes; ++i) {
115 data[i] = reinterpret_cast<uint8*>(shared_memory_->memory()) + offset;
116 offset += StrideInBytes(size_.width(), format_, i) *
117 (size_.height() / SubsamplingFactor(format_, i));
119 return true;
121 void Unmap() override {
122 DCHECK(mapped_);
123 shared_memory_->Unmap();
124 mapped_ = false;
126 bool IsMapped() const override { return mapped_; }
127 gfx::BufferFormat GetFormat() const override { return format_; }
128 void GetStride(int* stride) const override {
129 int num_planes = NumberOfPlanesForGpuMemoryBufferFormat(format_);
130 for (int i = 0; i < num_planes; ++i)
131 stride[i] =
132 base::checked_cast<int>(StrideInBytes(size_.width(), format_, i));
134 gfx::GpuMemoryBufferId GetId() const override {
135 NOTREACHED();
136 return 0;
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::BufferFormat 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(const gfx::Size& size,
165 gfx::BufferFormat format,
166 gfx::BufferUsage usage) {
167 scoped_ptr<base::SharedMemory> shared_memory(new base::SharedMemory);
168 if (!shared_memory->CreateAnonymous(BufferSizeInBytes(size, format)))
169 return nullptr;
170 return make_scoped_ptr<gfx::GpuMemoryBuffer>(
171 new GpuMemoryBufferImpl(size, format, shared_memory.Pass()));
174 gfx::GpuMemoryBuffer*
175 TestGpuMemoryBufferManager::GpuMemoryBufferFromClientBuffer(
176 ClientBuffer buffer) {
177 return reinterpret_cast<gfx::GpuMemoryBuffer*>(buffer);
180 void TestGpuMemoryBufferManager::SetDestructionSyncPoint(
181 gfx::GpuMemoryBuffer* buffer,
182 uint32 sync_point) {
185 } // namespace cc