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(gfx::BufferFormat 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
:
27 case gfx::BufferFormat::YUV_420
:
34 size_t SubsamplingFactor(gfx::BufferFormat format
, int plane
) {
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
:
47 case gfx::BufferFormat::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
, gfx::BufferFormat format
, int plane
) {
59 case gfx::BufferFormat::ATCIA
:
60 case gfx::BufferFormat::DXT5
:
63 case gfx::BufferFormat::ATC
:
64 case gfx::BufferFormat::DXT1
:
65 case gfx::BufferFormat::ETC1
:
67 DCHECK_EQ(width
% 2, 0u);
69 case gfx::BufferFormat::R_8
:
70 return (width
+ 3) & ~0x3;
71 case gfx::BufferFormat::RGBA_4444
:
74 case gfx::BufferFormat::RGBA_8888
:
75 case gfx::BufferFormat::RGBX_8888
:
76 case gfx::BufferFormat::BGRA_8888
:
79 case gfx::BufferFormat::YUV_420
:
80 return width
/ SubsamplingFactor(format
, plane
);
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
));
96 class GpuMemoryBufferImpl
: public gfx::GpuMemoryBuffer
{
98 GpuMemoryBufferImpl(const gfx::Size
& size
,
99 gfx::BufferFormat format
,
100 scoped_ptr
<base::SharedMemory
> shared_memory
)
103 shared_memory_(shared_memory
.Pass()),
106 // Overridden from gfx::GpuMemoryBuffer:
107 bool Map(void** data
) override
{
109 if (!shared_memory_
->Map(BufferSizeInBytes(size_
, format_
)))
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
));
121 void Unmap() override
{
123 shared_memory_
->Unmap();
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
)
132 base::checked_cast
<int>(StrideInBytes(size_
.width(), format_
, i
));
134 gfx::GpuMemoryBufferId
GetId() const override
{
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::BufferFormat format_
;
151 scoped_ptr
<base::SharedMemory
> shared_memory_
;
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
)))
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
,