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::RGBA_8888
:
16 case gfx::GpuMemoryBuffer::RGBX_8888
:
17 case gfx::GpuMemoryBuffer::BGRA_8888
:
25 class GpuMemoryBufferImpl
: public gfx::GpuMemoryBuffer
{
27 GpuMemoryBufferImpl(const gfx::Size
& size
,
29 scoped_ptr
<base::SharedMemory
> shared_memory
)
32 shared_memory_(shared_memory
.Pass()),
35 // Overridden from gfx::GpuMemoryBuffer:
36 void* Map() override
{
38 if (!shared_memory_
->Map(StrideInBytes(size_
.width(), format_
) *
42 return shared_memory_
->memory();
44 void Unmap() override
{
46 shared_memory_
->Unmap();
49 bool IsMapped() const override
{ return mapped_
; }
50 Format
GetFormat() const override
{ return format_
; }
51 uint32
GetStride() const override
{
52 return StrideInBytes(size_
.width(), format_
);
54 gfx::GpuMemoryBufferHandle
GetHandle() const override
{
55 gfx::GpuMemoryBufferHandle handle
;
56 handle
.type
= gfx::SHARED_MEMORY_BUFFER
;
57 handle
.handle
= shared_memory_
->handle();
60 ClientBuffer
AsClientBuffer() override
{
61 return reinterpret_cast<ClientBuffer
>(this);
65 const gfx::Size size_
;
66 gfx::GpuMemoryBuffer::Format format_
;
67 scoped_ptr
<base::SharedMemory
> shared_memory_
;
73 TestGpuMemoryBufferManager::TestGpuMemoryBufferManager() {
76 TestGpuMemoryBufferManager::~TestGpuMemoryBufferManager() {
79 scoped_ptr
<gfx::GpuMemoryBuffer
>
80 TestGpuMemoryBufferManager::AllocateGpuMemoryBuffer(
81 const gfx::Size
& size
,
82 gfx::GpuMemoryBuffer::Format format
,
83 gfx::GpuMemoryBuffer::Usage usage
) {
84 scoped_ptr
<base::SharedMemory
> shared_memory(new base::SharedMemory
);
85 if (!shared_memory
->CreateAnonymous(StrideInBytes(size
.width(), format
) *
88 return make_scoped_ptr
<gfx::GpuMemoryBuffer
>(
89 new GpuMemoryBufferImpl(size
, format
, shared_memory
.Pass()));
93 TestGpuMemoryBufferManager::GpuMemoryBufferFromClientBuffer(
94 ClientBuffer buffer
) {
95 return reinterpret_cast<gfx::GpuMemoryBuffer
*>(buffer
);
98 void TestGpuMemoryBufferManager::SetDestructionSyncPoint(
99 gfx::GpuMemoryBuffer
* buffer
,