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/buffer_format_util.h"
10 #include "ui/gfx/gpu_memory_buffer.h"
15 size_t SubsamplingFactor(gfx::BufferFormat format
, int plane
) {
17 case gfx::BufferFormat::ATC
:
18 case gfx::BufferFormat::ATCIA
:
19 case gfx::BufferFormat::DXT1
:
20 case gfx::BufferFormat::DXT5
:
21 case gfx::BufferFormat::ETC1
:
22 case gfx::BufferFormat::R_8
:
23 case gfx::BufferFormat::RGBA_4444
:
24 case gfx::BufferFormat::RGBA_8888
:
25 case gfx::BufferFormat::RGBX_8888
:
26 case gfx::BufferFormat::BGRA_8888
:
28 case gfx::BufferFormat::YUV_420
: {
29 static size_t factor
[] = {1, 2, 2};
30 DCHECK_LT(static_cast<size_t>(plane
), arraysize(factor
));
33 case gfx::BufferFormat::YUV_420_BIPLANAR
: {
34 static size_t factor
[] = {1, 2};
35 DCHECK_LT(static_cast<size_t>(plane
), arraysize(factor
));
43 size_t StrideInBytes(size_t width
, gfx::BufferFormat format
, int plane
) {
45 case gfx::BufferFormat::ATCIA
:
46 case gfx::BufferFormat::DXT5
:
49 case gfx::BufferFormat::ATC
:
50 case gfx::BufferFormat::DXT1
:
51 case gfx::BufferFormat::ETC1
:
53 DCHECK_EQ(width
% 2, 0u);
55 case gfx::BufferFormat::R_8
:
56 return (width
+ 3) & ~0x3;
57 case gfx::BufferFormat::RGBA_4444
:
60 case gfx::BufferFormat::RGBA_8888
:
61 case gfx::BufferFormat::RGBX_8888
:
62 case gfx::BufferFormat::BGRA_8888
:
65 case gfx::BufferFormat::YUV_420
:
66 return width
/ SubsamplingFactor(format
, plane
);
67 case gfx::BufferFormat::YUV_420_BIPLANAR
:
74 size_t BufferSizeInBytes(const gfx::Size
& size
, gfx::BufferFormat format
) {
75 size_t size_in_bytes
= 0;
76 int num_planes
= static_cast<int>(gfx::NumberOfPlanesForBufferFormat(format
));
77 for (int i
= 0; i
< num_planes
; ++i
) {
78 size_in_bytes
+= StrideInBytes(size
.width(), format
, i
) *
79 (size
.height() / SubsamplingFactor(format
, i
));
84 class GpuMemoryBufferImpl
: public gfx::GpuMemoryBuffer
{
86 GpuMemoryBufferImpl(const gfx::Size
& size
,
87 gfx::BufferFormat format
,
88 scoped_ptr
<base::SharedMemory
> shared_memory
)
91 shared_memory_(shared_memory
.Pass()),
94 // Overridden from gfx::GpuMemoryBuffer:
95 bool Map(void** data
) override
{
97 if (!shared_memory_
->Map(BufferSizeInBytes(size_
, format_
)))
102 static_cast<int>(gfx::NumberOfPlanesForBufferFormat(format_
));
103 for (int i
= 0; i
< num_planes
; ++i
) {
104 data
[i
] = reinterpret_cast<uint8
*>(shared_memory_
->memory()) + offset
;
105 offset
+= StrideInBytes(size_
.width(), format_
, i
) *
106 (size_
.height() / SubsamplingFactor(format_
, i
));
110 void Unmap() override
{
112 shared_memory_
->Unmap();
115 bool IsMapped() const override
{ return mapped_
; }
116 gfx::BufferFormat
GetFormat() const override
{ return format_
; }
117 void GetStride(int* stride
) const override
{
119 static_cast<int>(gfx::NumberOfPlanesForBufferFormat(format_
));
120 for (int i
= 0; i
< num_planes
; ++i
)
122 base::checked_cast
<int>(StrideInBytes(size_
.width(), format_
, i
));
124 gfx::GpuMemoryBufferId
GetId() const override
{
128 gfx::GpuMemoryBufferHandle
GetHandle() const override
{
129 gfx::GpuMemoryBufferHandle handle
;
130 handle
.type
= gfx::SHARED_MEMORY_BUFFER
;
131 handle
.handle
= shared_memory_
->handle();
134 ClientBuffer
AsClientBuffer() override
{
135 return reinterpret_cast<ClientBuffer
>(this);
139 const gfx::Size size_
;
140 gfx::BufferFormat format_
;
141 scoped_ptr
<base::SharedMemory
> shared_memory_
;
147 TestGpuMemoryBufferManager::TestGpuMemoryBufferManager() {
150 TestGpuMemoryBufferManager::~TestGpuMemoryBufferManager() {
153 scoped_ptr
<gfx::GpuMemoryBuffer
>
154 TestGpuMemoryBufferManager::AllocateGpuMemoryBuffer(const gfx::Size
& size
,
155 gfx::BufferFormat format
,
156 gfx::BufferUsage usage
) {
157 scoped_ptr
<base::SharedMemory
> shared_memory(new base::SharedMemory
);
158 if (!shared_memory
->CreateAnonymous(BufferSizeInBytes(size
, format
)))
160 return make_scoped_ptr
<gfx::GpuMemoryBuffer
>(
161 new GpuMemoryBufferImpl(size
, format
, shared_memory
.Pass()));
164 gfx::GpuMemoryBuffer
*
165 TestGpuMemoryBufferManager::GpuMemoryBufferFromClientBuffer(
166 ClientBuffer buffer
) {
167 return reinterpret_cast<gfx::GpuMemoryBuffer
*>(buffer
);
170 void TestGpuMemoryBufferManager::SetDestructionSyncPoint(
171 gfx::GpuMemoryBuffer
* buffer
,