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 StrideInBytes(size_t width
, gfx::GpuMemoryBuffer::Format format
) {
15 case gfx::GpuMemoryBuffer::ATCIA
:
16 case gfx::GpuMemoryBuffer::DXT5
:
18 case gfx::GpuMemoryBuffer::ATC
:
19 case gfx::GpuMemoryBuffer::DXT1
:
20 case gfx::GpuMemoryBuffer::ETC1
:
21 DCHECK_EQ(width
% 2, 0U);
23 case gfx::GpuMemoryBuffer::RGBA_8888
:
24 case gfx::GpuMemoryBuffer::RGBX_8888
:
25 case gfx::GpuMemoryBuffer::BGRA_8888
:
33 class GpuMemoryBufferImpl
: public gfx::GpuMemoryBuffer
{
35 GpuMemoryBufferImpl(const gfx::Size
& size
,
37 scoped_ptr
<base::SharedMemory
> shared_memory
)
40 shared_memory_(shared_memory
.Pass()),
43 // Overridden from gfx::GpuMemoryBuffer:
44 void* Map() override
{
46 if (!shared_memory_
->Map(StrideInBytes(size_
.width(), format_
) *
50 return shared_memory_
->memory();
52 void Unmap() override
{
54 shared_memory_
->Unmap();
57 bool IsMapped() const override
{ return mapped_
; }
58 Format
GetFormat() const override
{ return format_
; }
59 uint32
GetStride() const override
{
60 return StrideInBytes(size_
.width(), format_
);
62 gfx::GpuMemoryBufferHandle
GetHandle() const override
{
63 gfx::GpuMemoryBufferHandle handle
;
64 handle
.type
= gfx::SHARED_MEMORY_BUFFER
;
65 handle
.handle
= shared_memory_
->handle();
68 ClientBuffer
AsClientBuffer() override
{
69 return reinterpret_cast<ClientBuffer
>(this);
73 const gfx::Size size_
;
74 gfx::GpuMemoryBuffer::Format format_
;
75 scoped_ptr
<base::SharedMemory
> shared_memory_
;
81 TestGpuMemoryBufferManager::TestGpuMemoryBufferManager() {
84 TestGpuMemoryBufferManager::~TestGpuMemoryBufferManager() {
87 scoped_ptr
<gfx::GpuMemoryBuffer
>
88 TestGpuMemoryBufferManager::AllocateGpuMemoryBuffer(
89 const gfx::Size
& size
,
90 gfx::GpuMemoryBuffer::Format format
,
91 gfx::GpuMemoryBuffer::Usage usage
) {
92 scoped_ptr
<base::SharedMemory
> shared_memory(new base::SharedMemory
);
93 if (!shared_memory
->CreateAnonymous(StrideInBytes(size
.width(), format
) *
96 return make_scoped_ptr
<gfx::GpuMemoryBuffer
>(
97 new GpuMemoryBufferImpl(size
, format
, shared_memory
.Pass()));
100 gfx::GpuMemoryBuffer
*
101 TestGpuMemoryBufferManager::GpuMemoryBufferFromClientBuffer(
102 ClientBuffer buffer
) {
103 return reinterpret_cast<gfx::GpuMemoryBuffer
*>(buffer
);
106 void TestGpuMemoryBufferManager::SetDestructionSyncPoint(
107 gfx::GpuMemoryBuffer
* buffer
,