OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "content/common/gpu/client/gpu_memory_buffer_impl_shm.h" | 5 #include "content/common/gpu/client/gpu_memory_buffer_impl_shm.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/numerics/safe_math.h" |
8 #include "ui/gl/gl_bindings.h" | 8 #include "ui/gl/gl_bindings.h" |
9 | 9 |
10 namespace content { | 10 namespace content { |
11 | 11 |
12 GpuMemoryBufferImplShm::GpuMemoryBufferImplShm(gfx::Size size, | 12 GpuMemoryBufferImplShm::GpuMemoryBufferImplShm(const gfx::Size& size, |
13 unsigned internalformat) | 13 unsigned internalformat) |
14 : GpuMemoryBufferImpl(size, internalformat) {} | 14 : GpuMemoryBufferImpl(size, internalformat) {} |
15 | 15 |
16 GpuMemoryBufferImplShm::~GpuMemoryBufferImplShm() {} | 16 GpuMemoryBufferImplShm::~GpuMemoryBufferImplShm() {} |
17 | 17 |
18 // static | 18 // static |
| 19 void GpuMemoryBufferImplShm::AllocateSharedMemoryForChildProcess( |
| 20 const gfx::Size& size, |
| 21 unsigned internalformat, |
| 22 base::ProcessHandle child_process, |
| 23 gfx::GpuMemoryBufferHandle* handle) { |
| 24 DCHECK(IsLayoutSupported(size, internalformat)); |
| 25 base::SharedMemory shared_memory; |
| 26 if (!shared_memory.CreateAnonymous(size.GetArea() * |
| 27 BytesPerPixel(internalformat))) { |
| 28 handle->type = gfx::EMPTY_BUFFER; |
| 29 return; |
| 30 } |
| 31 handle->type = gfx::SHARED_MEMORY_BUFFER; |
| 32 shared_memory.GiveToProcess(child_process, &handle->handle); |
| 33 } |
| 34 |
| 35 // static |
| 36 bool GpuMemoryBufferImplShm::IsLayoutSupported(const gfx::Size& size, |
| 37 unsigned internalformat) { |
| 38 base::CheckedNumeric<int> buffer_size = size.width(); |
| 39 buffer_size *= size.height(); |
| 40 buffer_size *= BytesPerPixel(internalformat); |
| 41 return buffer_size.IsValid(); |
| 42 } |
| 43 |
| 44 // static |
19 bool GpuMemoryBufferImplShm::IsUsageSupported(unsigned usage) { | 45 bool GpuMemoryBufferImplShm::IsUsageSupported(unsigned usage) { |
20 switch (usage) { | 46 switch (usage) { |
21 case GL_IMAGE_MAP_CHROMIUM: | 47 case GL_IMAGE_MAP_CHROMIUM: |
22 return true; | 48 return true; |
23 default: | 49 default: |
24 return false; | 50 return false; |
25 } | 51 } |
26 } | 52 } |
27 | 53 |
28 bool GpuMemoryBufferImplShm::Initialize(gfx::GpuMemoryBufferHandle handle) { | 54 // static |
| 55 bool GpuMemoryBufferImplShm::IsConfigurationSupported(const gfx::Size& size, |
| 56 unsigned internalformat, |
| 57 unsigned usage) { |
| 58 return IsLayoutSupported(size, internalformat) && IsUsageSupported(usage); |
| 59 } |
| 60 |
| 61 bool GpuMemoryBufferImplShm::Initialize() { |
| 62 DCHECK(IsLayoutSupported(size_, internalformat_)); |
| 63 scoped_ptr<base::SharedMemory> shared_memory(new base::SharedMemory()); |
| 64 if (!shared_memory->CreateAnonymous(size_.GetArea() * |
| 65 BytesPerPixel(internalformat_))) |
| 66 return false; |
| 67 shared_memory_ = shared_memory.Pass(); |
| 68 DCHECK(!shared_memory_->memory()); |
| 69 return true; |
| 70 } |
| 71 |
| 72 bool GpuMemoryBufferImplShm::InitializeFromHandle( |
| 73 gfx::GpuMemoryBufferHandle handle) { |
| 74 DCHECK(IsLayoutSupported(size_, internalformat_)); |
29 if (!base::SharedMemory::IsHandleValid(handle.handle)) | 75 if (!base::SharedMemory::IsHandleValid(handle.handle)) |
30 return false; | 76 return false; |
31 shared_memory_.reset(new base::SharedMemory(handle.handle, false)); | 77 shared_memory_.reset(new base::SharedMemory(handle.handle, false)); |
32 DCHECK(!shared_memory_->memory()); | 78 DCHECK(!shared_memory_->memory()); |
33 return true; | 79 return true; |
34 } | 80 } |
35 | |
36 bool GpuMemoryBufferImplShm::InitializeFromSharedMemory( | |
37 scoped_ptr<base::SharedMemory> shared_memory) { | |
38 shared_memory_ = shared_memory.Pass(); | |
39 DCHECK(!shared_memory_->memory()); | |
40 return true; | |
41 } | |
42 | 81 |
43 void* GpuMemoryBufferImplShm::Map() { | 82 void* GpuMemoryBufferImplShm::Map() { |
44 DCHECK(!mapped_); | 83 DCHECK(!mapped_); |
45 if (!shared_memory_->Map(size_.GetArea() * BytesPerPixel(internalformat_))) | 84 if (!shared_memory_->Map(size_.GetArea() * BytesPerPixel(internalformat_))) |
46 return NULL; | 85 return NULL; |
47 mapped_ = true; | 86 mapped_ = true; |
48 return shared_memory_->memory(); | 87 return shared_memory_->memory(); |
49 } | 88 } |
50 | 89 |
51 void GpuMemoryBufferImplShm::Unmap() { | 90 void GpuMemoryBufferImplShm::Unmap() { |
52 DCHECK(mapped_); | 91 DCHECK(mapped_); |
53 shared_memory_->Unmap(); | 92 shared_memory_->Unmap(); |
54 mapped_ = false; | 93 mapped_ = false; |
55 } | 94 } |
56 | 95 |
| 96 uint32 GpuMemoryBufferImplShm::GetStride() const { |
| 97 return size_.width() * BytesPerPixel(internalformat_); |
| 98 } |
| 99 |
57 gfx::GpuMemoryBufferHandle GpuMemoryBufferImplShm::GetHandle() const { | 100 gfx::GpuMemoryBufferHandle GpuMemoryBufferImplShm::GetHandle() const { |
58 gfx::GpuMemoryBufferHandle handle; | 101 gfx::GpuMemoryBufferHandle handle; |
59 handle.type = gfx::SHARED_MEMORY_BUFFER; | 102 handle.type = gfx::SHARED_MEMORY_BUFFER; |
60 handle.handle = shared_memory_->handle(); | 103 handle.handle = shared_memory_->handle(); |
61 return handle; | 104 return handle; |
62 } | 105 } |
63 | 106 |
64 } // namespace content | 107 } // namespace content |
OLD | NEW |