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 class GpuMemoryBufferImpl
: public gfx::GpuMemoryBuffer
{
17 GpuMemoryBufferImpl(const gfx::Size
& size
,
18 gfx::BufferFormat format
,
19 scoped_ptr
<base::SharedMemory
> shared_memory
)
22 shared_memory_(shared_memory
.Pass()),
25 // Overridden from gfx::GpuMemoryBuffer:
26 bool Map(void** data
) override
{
28 if (!shared_memory_
->Map(gfx::BufferSizeForBufferFormat(size_
, format_
)))
33 static_cast<int>(gfx::NumberOfPlanesForBufferFormat(format_
));
34 for (int i
= 0; i
< num_planes
; ++i
) {
35 data
[i
] = reinterpret_cast<uint8
*>(shared_memory_
->memory()) + offset
;
37 gfx::RowSizeForBufferFormat(size_
.width(), format_
, i
) *
38 (size_
.height() / gfx::SubsamplingFactorForBufferFormat(format_
, i
));
42 void Unmap() override
{
44 shared_memory_
->Unmap();
47 bool IsMapped() const override
{ return mapped_
; }
48 gfx::BufferFormat
GetFormat() const override
{ return format_
; }
49 void GetStride(int* stride
) const override
{
51 static_cast<int>(gfx::NumberOfPlanesForBufferFormat(format_
));
52 for (int i
= 0; i
< num_planes
; ++i
)
53 stride
[i
] = base::checked_cast
<int>(
54 gfx::RowSizeForBufferFormat(size_
.width(), format_
, i
));
56 gfx::GpuMemoryBufferId
GetId() const override
{
58 return gfx::GpuMemoryBufferId(0);
60 gfx::GpuMemoryBufferHandle
GetHandle() const override
{
61 gfx::GpuMemoryBufferHandle handle
;
62 handle
.type
= gfx::SHARED_MEMORY_BUFFER
;
63 handle
.handle
= shared_memory_
->handle();
66 ClientBuffer
AsClientBuffer() override
{
67 return reinterpret_cast<ClientBuffer
>(this);
71 const gfx::Size size_
;
72 gfx::BufferFormat format_
;
73 scoped_ptr
<base::SharedMemory
> shared_memory_
;
79 TestGpuMemoryBufferManager::TestGpuMemoryBufferManager() {
82 TestGpuMemoryBufferManager::~TestGpuMemoryBufferManager() {
85 scoped_ptr
<gfx::GpuMemoryBuffer
>
86 TestGpuMemoryBufferManager::AllocateGpuMemoryBuffer(const gfx::Size
& size
,
87 gfx::BufferFormat format
,
88 gfx::BufferUsage usage
) {
89 scoped_ptr
<base::SharedMemory
> shared_memory(new base::SharedMemory
);
90 const size_t buffer_size
= gfx::BufferSizeForBufferFormat(size
, format
);
91 if (!shared_memory
->CreateAnonymous(buffer_size
))
93 return make_scoped_ptr
<gfx::GpuMemoryBuffer
>(
94 new GpuMemoryBufferImpl(size
, format
, shared_memory
.Pass()));
98 TestGpuMemoryBufferManager::GpuMemoryBufferFromClientBuffer(
99 ClientBuffer buffer
) {
100 return reinterpret_cast<gfx::GpuMemoryBuffer
*>(buffer
);
103 void TestGpuMemoryBufferManager::SetDestructionSyncPoint(
104 gfx::GpuMemoryBuffer
* buffer
,