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::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
:
26 case gfx::GpuMemoryBuffer::Format::YUV_420
:
33 size_t SubsamplingFactor(gfx::GpuMemoryBuffer::Format format
, int plane
) {
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
:
45 case gfx::GpuMemoryBuffer::YUV_420
: {
46 static size_t factor
[] = {1, 2, 2};
47 DCHECK_LT(static_cast<size_t>(plane
), arraysize(factor
));
55 size_t StrideInBytes(size_t width
,
56 gfx::GpuMemoryBuffer::Format format
,
59 case gfx::GpuMemoryBuffer::ATCIA
:
60 case gfx::GpuMemoryBuffer::DXT5
:
63 case gfx::GpuMemoryBuffer::ATC
:
64 case gfx::GpuMemoryBuffer::DXT1
:
65 case gfx::GpuMemoryBuffer::ETC1
:
67 DCHECK_EQ(width
% 2, 0u);
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
:
76 case gfx::GpuMemoryBuffer::YUV_420
:
77 return width
/ SubsamplingFactor(format
, plane
);
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
));
94 class GpuMemoryBufferImpl
: public gfx::GpuMemoryBuffer
{
96 GpuMemoryBufferImpl(const gfx::Size
& size
,
98 scoped_ptr
<base::SharedMemory
> shared_memory
)
101 shared_memory_(shared_memory
.Pass()),
104 // Overridden from gfx::GpuMemoryBuffer:
105 bool Map(void** data
) override
{
107 if (!shared_memory_
->Map(BufferSizeInBytes(size_
, format_
)))
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
));
119 void Unmap() override
{
121 shared_memory_
->Unmap();
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();
137 ClientBuffer
AsClientBuffer() override
{
138 return reinterpret_cast<ClientBuffer
>(this);
142 const gfx::Size size_
;
143 gfx::GpuMemoryBuffer::Format format_
;
144 scoped_ptr
<base::SharedMemory
> shared_memory_
;
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
)))
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
,