OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "cc/test/test_gpu_memory_buffer_manager.h" | 5 #include "cc/test/test_gpu_memory_buffer_manager.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "ui/gfx/gpu_memory_buffer.h" | 8 #include "ui/gfx/gpu_memory_buffer.h" |
9 | 9 |
10 namespace cc { | 10 namespace cc { |
11 namespace { | 11 namespace { |
12 | 12 |
13 size_t StrideInBytes(size_t width, gfx::GpuMemoryBuffer::Format format) { | 13 size_t BytesPerPixel(gfx::GpuMemoryBuffer::Format format) { |
14 switch (format) { | 14 switch (format) { |
15 case gfx::GpuMemoryBuffer::RGBA_8888: | 15 case gfx::GpuMemoryBuffer::RGBA_8888: |
16 case gfx::GpuMemoryBuffer::RGBX_8888: | 16 case gfx::GpuMemoryBuffer::RGBX_8888: |
17 case gfx::GpuMemoryBuffer::BGRA_8888: | 17 case gfx::GpuMemoryBuffer::BGRA_8888: |
18 return width * 4; | 18 return 4; |
19 } | 19 } |
20 | 20 |
21 NOTREACHED(); | 21 NOTREACHED(); |
22 return 0; | 22 return 0; |
23 } | 23 } |
24 | 24 |
25 class GpuMemoryBufferImpl : public gfx::GpuMemoryBuffer { | 25 class GpuMemoryBufferImpl : public gfx::GpuMemoryBuffer { |
26 public: | 26 public: |
27 GpuMemoryBufferImpl(const gfx::Size& size, | 27 GpuMemoryBufferImpl(const gfx::Size& size, |
28 Format format, | 28 Format format, |
29 scoped_ptr<base::SharedMemory> shared_memory) | 29 scoped_ptr<base::SharedMemory> shared_memory) |
30 : size_(size), | 30 : size_(size), |
31 format_(format), | 31 format_(format), |
32 shared_memory_(shared_memory.Pass()), | 32 shared_memory_(shared_memory.Pass()), |
33 mapped_(false) {} | 33 mapped_(false) {} |
34 | 34 |
35 // Overridden from gfx::GpuMemoryBuffer: | 35 // Overridden from gfx::GpuMemoryBuffer: |
36 void* Map() override { | 36 void* Map() override { |
37 DCHECK(!mapped_); | 37 DCHECK(!mapped_); |
38 if (!shared_memory_->Map(StrideInBytes(size_.width(), format_) * | 38 if (!shared_memory_->Map(size_.GetArea() * BytesPerPixel(format_))) |
39 size_.height())) | |
40 return NULL; | 39 return NULL; |
41 mapped_ = true; | 40 mapped_ = true; |
42 return shared_memory_->memory(); | 41 return shared_memory_->memory(); |
43 } | 42 } |
44 void Unmap() override { | 43 void Unmap() override { |
45 DCHECK(mapped_); | 44 DCHECK(mapped_); |
46 shared_memory_->Unmap(); | 45 shared_memory_->Unmap(); |
47 mapped_ = false; | 46 mapped_ = false; |
48 } | 47 } |
49 bool IsMapped() const override { return mapped_; } | 48 bool IsMapped() const override { return mapped_; } |
50 Format GetFormat() const override { return format_; } | 49 Format GetFormat() const override { return format_; } |
51 uint32 GetStride() const override { | 50 uint32 GetStride() const override { |
52 return StrideInBytes(size_.width(), format_); | 51 return size_.width() * BytesPerPixel(format_); |
53 } | 52 } |
54 gfx::GpuMemoryBufferHandle GetHandle() const override { | 53 gfx::GpuMemoryBufferHandle GetHandle() const override { |
55 gfx::GpuMemoryBufferHandle handle; | 54 gfx::GpuMemoryBufferHandle handle; |
56 handle.type = gfx::SHARED_MEMORY_BUFFER; | 55 handle.type = gfx::SHARED_MEMORY_BUFFER; |
57 handle.handle = shared_memory_->handle(); | 56 handle.handle = shared_memory_->handle(); |
58 return handle; | 57 return handle; |
59 } | 58 } |
60 ClientBuffer AsClientBuffer() override { | 59 ClientBuffer AsClientBuffer() override { |
61 return reinterpret_cast<ClientBuffer>(this); | 60 return reinterpret_cast<ClientBuffer>(this); |
62 } | 61 } |
(...skipping 12 matching lines...) Loading... |
75 | 74 |
76 TestGpuMemoryBufferManager::~TestGpuMemoryBufferManager() { | 75 TestGpuMemoryBufferManager::~TestGpuMemoryBufferManager() { |
77 } | 76 } |
78 | 77 |
79 scoped_ptr<gfx::GpuMemoryBuffer> | 78 scoped_ptr<gfx::GpuMemoryBuffer> |
80 TestGpuMemoryBufferManager::AllocateGpuMemoryBuffer( | 79 TestGpuMemoryBufferManager::AllocateGpuMemoryBuffer( |
81 const gfx::Size& size, | 80 const gfx::Size& size, |
82 gfx::GpuMemoryBuffer::Format format, | 81 gfx::GpuMemoryBuffer::Format format, |
83 gfx::GpuMemoryBuffer::Usage usage) { | 82 gfx::GpuMemoryBuffer::Usage usage) { |
84 scoped_ptr<base::SharedMemory> shared_memory(new base::SharedMemory); | 83 scoped_ptr<base::SharedMemory> shared_memory(new base::SharedMemory); |
85 if (!shared_memory->CreateAnonymous(StrideInBytes(size.width(), format) * | 84 if (!shared_memory->CreateAnonymous(size.GetArea() * BytesPerPixel(format))) |
86 size.height())) | |
87 return nullptr; | 85 return nullptr; |
88 return make_scoped_ptr<gfx::GpuMemoryBuffer>( | 86 return make_scoped_ptr<gfx::GpuMemoryBuffer>( |
89 new GpuMemoryBufferImpl(size, format, shared_memory.Pass())); | 87 new GpuMemoryBufferImpl(size, format, shared_memory.Pass())); |
90 } | 88 } |
91 | 89 |
92 gfx::GpuMemoryBuffer* | 90 gfx::GpuMemoryBuffer* |
93 TestGpuMemoryBufferManager::GpuMemoryBufferFromClientBuffer( | 91 TestGpuMemoryBufferManager::GpuMemoryBufferFromClientBuffer( |
94 ClientBuffer buffer) { | 92 ClientBuffer buffer) { |
95 return reinterpret_cast<gfx::GpuMemoryBuffer*>(buffer); | 93 return reinterpret_cast<gfx::GpuMemoryBuffer*>(buffer); |
96 } | 94 } |
97 | 95 |
98 void TestGpuMemoryBufferManager::SetDestructionSyncPoint( | 96 void TestGpuMemoryBufferManager::SetDestructionSyncPoint( |
99 gfx::GpuMemoryBuffer* buffer, | 97 gfx::GpuMemoryBuffer* buffer, |
100 uint32 sync_point) { | 98 uint32 sync_point) { |
101 } | 99 } |
102 | 100 |
103 } // namespace cc | 101 } // namespace cc |
OLD | NEW |