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/logging.h" |
8 #include "base/numerics/safe_conversions.h" | |
9 #include "content/common/child_process_host_impl.h" | |
8 | 10 |
9 namespace content { | 11 namespace content { |
10 | 12 |
11 GpuMemoryBufferImplShm::GpuMemoryBufferImplShm(gfx::Size size, | 13 GpuMemoryBufferImplShm::GpuMemoryBufferImplShm(const gfx::Size& size, |
12 unsigned internalformat) | 14 unsigned internalformat) |
13 : GpuMemoryBufferImpl(size, internalformat) {} | 15 : GpuMemoryBufferImpl(size, internalformat) {} |
14 | 16 |
15 GpuMemoryBufferImplShm::~GpuMemoryBufferImplShm() {} | 17 GpuMemoryBufferImplShm::~GpuMemoryBufferImplShm() {} |
16 | 18 |
17 bool GpuMemoryBufferImplShm::Initialize(gfx::GpuMemoryBufferHandle handle) { | 19 // static |
20 void GpuMemoryBufferImplShm::AllocateSharedMemoryForChildProcess( | |
21 const gfx::Size& size, | |
22 unsigned internalformat, | |
23 base::ProcessHandle child_process, | |
24 gfx::GpuMemoryBufferHandle* handle) { | |
25 if (!IsFormatValid(internalformat)) { | |
26 handle->type = gfx::EMPTY_BUFFER; | |
27 return; | |
28 } | |
29 | |
30 uint64 stride = | |
31 static_cast<uint64>(size.width()) * BytesPerPixel(internalformat); | |
piman
2014/05/02 22:26:51
use base::CheckedNumeric here and below
reveman
2014/05/03 00:59:48
Done. And moved some of these checks to where I th
| |
32 if (!base::IsValueInRangeForNumericType<uint32>(stride)) { | |
33 handle->type = gfx::EMPTY_BUFFER; | |
34 return; | |
35 } | |
36 | |
37 uint64 buffer_size = stride * static_cast<uint64>(size.height()); | |
38 if (!base::IsValueInRangeForNumericType<size_t>(buffer_size)) { | |
39 handle->type = gfx::EMPTY_BUFFER; | |
40 return; | |
41 } | |
42 | |
43 handle->type = gfx::SHARED_MEMORY_BUFFER; | |
44 ChildProcessHostImpl::AllocateSharedMemory( | |
piman
2014/05/02 22:26:51
This /looks/ wrong, because this class is in a dir
reveman
2014/05/03 00:59:48
Yes, we shouldn't be using ChildProcessHostImpl he
| |
45 static_cast<size_t>(buffer_size), child_process, &handle->handle); | |
46 } | |
47 | |
48 bool GpuMemoryBufferImplShm::Initialize() { | |
49 size_t size = size_.GetArea() * BytesPerPixel(internalformat_); | |
50 scoped_ptr<base::SharedMemory> shared_memory(new base::SharedMemory()); | |
51 if (!shared_memory->CreateAnonymous(size)) | |
52 return false; | |
53 shared_memory_ = shared_memory.Pass(); | |
54 DCHECK(!shared_memory_->memory()); | |
55 return true; | |
56 } | |
57 | |
58 bool GpuMemoryBufferImplShm::InitializeFromHandle( | |
59 gfx::GpuMemoryBufferHandle handle) { | |
18 if (!base::SharedMemory::IsHandleValid(handle.handle)) | 60 if (!base::SharedMemory::IsHandleValid(handle.handle)) |
19 return false; | 61 return false; |
20 shared_memory_.reset(new base::SharedMemory(handle.handle, false)); | 62 shared_memory_.reset(new base::SharedMemory(handle.handle, false)); |
21 DCHECK(!shared_memory_->memory()); | 63 DCHECK(!shared_memory_->memory()); |
22 return true; | 64 return true; |
23 } | 65 } |
24 | |
25 bool GpuMemoryBufferImplShm::InitializeFromSharedMemory( | |
26 scoped_ptr<base::SharedMemory> shared_memory) { | |
27 shared_memory_ = shared_memory.Pass(); | |
28 DCHECK(!shared_memory_->memory()); | |
29 return true; | |
30 } | |
31 | 66 |
32 void* GpuMemoryBufferImplShm::Map(AccessMode mode) { | 67 void* GpuMemoryBufferImplShm::Map(AccessMode mode) { |
33 DCHECK(!mapped_); | 68 DCHECK(!mapped_); |
34 if (!shared_memory_->Map(size_.GetArea() * BytesPerPixel(internalformat_))) | 69 if (!shared_memory_->Map(size_.GetArea() * BytesPerPixel(internalformat_))) |
35 return NULL; | 70 return NULL; |
36 mapped_ = true; | 71 mapped_ = true; |
37 return shared_memory_->memory(); | 72 return shared_memory_->memory(); |
38 } | 73 } |
39 | 74 |
40 void GpuMemoryBufferImplShm::Unmap() { | 75 void GpuMemoryBufferImplShm::Unmap() { |
41 DCHECK(mapped_); | 76 DCHECK(mapped_); |
42 shared_memory_->Unmap(); | 77 shared_memory_->Unmap(); |
43 mapped_ = false; | 78 mapped_ = false; |
44 } | 79 } |
45 | 80 |
46 gfx::GpuMemoryBufferHandle GpuMemoryBufferImplShm::GetHandle() const { | 81 gfx::GpuMemoryBufferHandle GpuMemoryBufferImplShm::GetHandle() const { |
47 gfx::GpuMemoryBufferHandle handle; | 82 gfx::GpuMemoryBufferHandle handle; |
48 handle.type = gfx::SHARED_MEMORY_BUFFER; | 83 handle.type = gfx::SHARED_MEMORY_BUFFER; |
49 handle.handle = shared_memory_->handle(); | 84 handle.handle = shared_memory_->handle(); |
50 return handle; | 85 return handle; |
51 } | 86 } |
52 | 87 |
53 } // namespace content | 88 } // namespace content |
OLD | NEW |