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"
14 int NumberOfPlanesForGpuMemoryBufferFormat(
15 gfx::GpuMemoryBuffer::Format format
) {
17 case gfx::GpuMemoryBuffer::ATC
:
18 case gfx::GpuMemoryBuffer::ATCIA
:
19 case gfx::GpuMemoryBuffer::DXT1
:
20 case gfx::GpuMemoryBuffer::DXT5
:
21 case gfx::GpuMemoryBuffer::ETC1
:
22 case gfx::GpuMemoryBuffer::R_8
:
23 case gfx::GpuMemoryBuffer::RGBA_4444
:
24 case gfx::GpuMemoryBuffer::RGBA_8888
:
25 case gfx::GpuMemoryBuffer::RGBX_8888
:
26 case gfx::GpuMemoryBuffer::BGRA_8888
:
28 case gfx::GpuMemoryBuffer::YUV_420
:
35 size_t SubsamplingFactor(gfx::GpuMemoryBuffer::Format format
, int plane
) {
37 case gfx::GpuMemoryBuffer::ATC
:
38 case gfx::GpuMemoryBuffer::ATCIA
:
39 case gfx::GpuMemoryBuffer::DXT1
:
40 case gfx::GpuMemoryBuffer::DXT5
:
41 case gfx::GpuMemoryBuffer::ETC1
:
42 case gfx::GpuMemoryBuffer::R_8
:
43 case gfx::GpuMemoryBuffer::RGBA_4444
:
44 case gfx::GpuMemoryBuffer::RGBA_8888
:
45 case gfx::GpuMemoryBuffer::RGBX_8888
:
46 case gfx::GpuMemoryBuffer::BGRA_8888
:
48 case gfx::GpuMemoryBuffer::YUV_420
: {
49 static size_t factor
[] = {1, 2, 2};
50 DCHECK_LT(static_cast<size_t>(plane
), arraysize(factor
));
58 size_t StrideInBytes(size_t width
,
59 gfx::GpuMemoryBuffer::Format format
,
62 case gfx::GpuMemoryBuffer::ATCIA
:
63 case gfx::GpuMemoryBuffer::DXT5
:
66 case gfx::GpuMemoryBuffer::ATC
:
67 case gfx::GpuMemoryBuffer::DXT1
:
68 case gfx::GpuMemoryBuffer::ETC1
:
70 DCHECK_EQ(width
% 2, 0u);
72 case gfx::GpuMemoryBuffer::R_8
:
73 return (width
+ 3) & ~0x3;
74 case gfx::GpuMemoryBuffer::RGBA_4444
:
77 case gfx::GpuMemoryBuffer::RGBA_8888
:
78 case gfx::GpuMemoryBuffer::RGBX_8888
:
79 case gfx::GpuMemoryBuffer::BGRA_8888
:
82 case gfx::GpuMemoryBuffer::YUV_420
:
83 return width
/ SubsamplingFactor(format
, plane
);
89 size_t BufferSizeInBytes(const gfx::Size
& size
,
90 gfx::GpuMemoryBuffer::Format format
) {
91 size_t size_in_bytes
= 0;
92 int num_planes
= NumberOfPlanesForGpuMemoryBufferFormat(format
);
93 for (int i
= 0; i
< num_planes
; ++i
) {
94 size_in_bytes
+= StrideInBytes(size
.width(), format
, i
) *
95 (size
.height() / SubsamplingFactor(format
, i
));
100 class GpuMemoryBufferImpl
: public gfx::GpuMemoryBuffer
{
102 GpuMemoryBufferImpl(const gfx::Size
& size
,
104 scoped_ptr
<base::SharedMemory
> shared_memory
)
107 shared_memory_(shared_memory
.Pass()),
110 // Overridden from gfx::GpuMemoryBuffer:
111 bool Map(void** data
) override
{
113 if (!shared_memory_
->Map(BufferSizeInBytes(size_
, format_
)))
117 int num_planes
= NumberOfPlanesForGpuMemoryBufferFormat(format_
);
118 for (int i
= 0; i
< num_planes
; ++i
) {
119 data
[i
] = reinterpret_cast<uint8
*>(shared_memory_
->memory()) + offset
;
120 offset
+= StrideInBytes(size_
.width(), format_
, i
) *
121 (size_
.height() / SubsamplingFactor(format_
, i
));
125 void Unmap() override
{
127 shared_memory_
->Unmap();
130 bool IsMapped() const override
{ return mapped_
; }
131 Format
GetFormat() const override
{ return format_
; }
132 void GetStride(int* stride
) const override
{
133 int num_planes
= NumberOfPlanesForGpuMemoryBufferFormat(format_
);
134 for (int i
= 0; i
< num_planes
; ++i
)
136 base::checked_cast
<int>(StrideInBytes(size_
.width(), format_
, i
));
138 gfx::GpuMemoryBufferHandle
GetHandle() const override
{
139 gfx::GpuMemoryBufferHandle handle
;
140 handle
.type
= gfx::SHARED_MEMORY_BUFFER
;
141 handle
.handle
= shared_memory_
->handle();
144 ClientBuffer
AsClientBuffer() override
{
145 return reinterpret_cast<ClientBuffer
>(this);
149 const gfx::Size size_
;
150 gfx::GpuMemoryBuffer::Format format_
;
151 scoped_ptr
<base::SharedMemory
> shared_memory_
;
157 TestGpuMemoryBufferManager::TestGpuMemoryBufferManager() {
160 TestGpuMemoryBufferManager::~TestGpuMemoryBufferManager() {
163 scoped_ptr
<gfx::GpuMemoryBuffer
>
164 TestGpuMemoryBufferManager::AllocateGpuMemoryBuffer(
165 const gfx::Size
& size
,
166 gfx::GpuMemoryBuffer::Format format
,
167 gfx::GpuMemoryBuffer::Usage usage
) {
168 scoped_ptr
<base::SharedMemory
> shared_memory(new base::SharedMemory
);
169 if (!shared_memory
->CreateAnonymous(BufferSizeInBytes(size
, format
)))
171 return make_scoped_ptr
<gfx::GpuMemoryBuffer
>(
172 new GpuMemoryBufferImpl(size
, format
, shared_memory
.Pass()));
175 gfx::GpuMemoryBuffer
*
176 TestGpuMemoryBufferManager::GpuMemoryBufferFromClientBuffer(
177 ClientBuffer buffer
) {
178 return reinterpret_cast<gfx::GpuMemoryBuffer
*>(buffer
);
181 void TestGpuMemoryBufferManager::SetDestructionSyncPoint(
182 gfx::GpuMemoryBuffer
* buffer
,