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"
13 size_t NumberOfPlanesForGpuMemoryBufferFormat(
14 gfx::GpuMemoryBuffer::Format 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
:
27 case gfx::GpuMemoryBuffer::YUV_420
:
34 size_t SubsamplingFactor(gfx::GpuMemoryBuffer::Format format
, int plane
) {
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
:
47 case gfx::GpuMemoryBuffer::YUV_420
: {
48 static size_t factor
[] = {1, 2, 2};
49 DCHECK_LT(static_cast<size_t>(plane
), arraysize(factor
));
57 size_t StrideInBytes(size_t width
,
58 gfx::GpuMemoryBuffer::Format format
,
61 case gfx::GpuMemoryBuffer::ATCIA
:
62 case gfx::GpuMemoryBuffer::DXT5
:
65 case gfx::GpuMemoryBuffer::ATC
:
66 case gfx::GpuMemoryBuffer::DXT1
:
67 case gfx::GpuMemoryBuffer::ETC1
:
69 DCHECK_EQ(width
% 2, 0u);
71 case gfx::GpuMemoryBuffer::R_8
:
72 return (width
+ 3) & ~0x3;
73 case gfx::GpuMemoryBuffer::RGBA_4444
:
76 case gfx::GpuMemoryBuffer::RGBA_8888
:
77 case gfx::GpuMemoryBuffer::RGBX_8888
:
78 case gfx::GpuMemoryBuffer::BGRA_8888
:
81 case gfx::GpuMemoryBuffer::YUV_420
:
82 return width
/ SubsamplingFactor(format
, plane
);
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
));
99 class GpuMemoryBufferImpl
: public gfx::GpuMemoryBuffer
{
101 GpuMemoryBufferImpl(const gfx::Size
& size
,
103 scoped_ptr
<base::SharedMemory
> shared_memory
)
106 shared_memory_(shared_memory
.Pass()),
109 // Overridden from gfx::GpuMemoryBuffer:
110 bool Map(void** data
) override
{
112 if (!shared_memory_
->Map(BufferSizeInBytes(size_
, format_
)))
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
));
124 void Unmap() override
{
126 shared_memory_
->Unmap();
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();
142 ClientBuffer
AsClientBuffer() override
{
143 return reinterpret_cast<ClientBuffer
>(this);
147 const gfx::Size size_
;
148 gfx::GpuMemoryBuffer::Format format_
;
149 scoped_ptr
<base::SharedMemory
> shared_memory_
;
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
)))
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
,