Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1797)

Side by Side Diff: content/common/gpu/client/gpu_memory_buffer_impl_shm.cc

Issue 263553009: content: Cleanup GpuMemoryBuffer allocation. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Remove use of ChildProcessHostImpl and use base::CheckedNumeric Created 6 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 8
9 namespace content { 9 namespace content {
10 10
11 GpuMemoryBufferImplShm::GpuMemoryBufferImplShm(gfx::Size size, 11 GpuMemoryBufferImplShm::GpuMemoryBufferImplShm(const gfx::Size& size,
12 unsigned internalformat) 12 unsigned internalformat)
13 : GpuMemoryBufferImpl(size, internalformat) {} 13 : GpuMemoryBufferImpl(size, internalformat) {}
14 14
15 GpuMemoryBufferImplShm::~GpuMemoryBufferImplShm() {} 15 GpuMemoryBufferImplShm::~GpuMemoryBufferImplShm() {}
16 16
17 bool GpuMemoryBufferImplShm::Initialize(gfx::GpuMemoryBufferHandle handle) { 17 // static
18 void GpuMemoryBufferImplShm::AllocateSharedMemoryForChildProcess(
19 const gfx::Size& size,
20 unsigned internalformat,
21 base::ProcessHandle child_process,
22 gfx::GpuMemoryBufferHandle* handle) {
23 base::CheckedNumeric<size_t> buffer_size = size.GetArea();
piman 2014/05/05 19:23:51 Actually, GetArea can overflow too (int*int -> int
danakj 2014/05/05 19:27:25 We have some various helpers here: https://code.go
reveman 2014/05/05 20:41:23 I made the caller responsible for passing a valid
piman 2014/05/05 21:02:16 It's not obvious that it's the only caller (now, f
reveman 2014/05/06 12:26:27 Added a IsConfigurationSupported() function to lat
24 buffer_size *= BytesPerPixel(internalformat);
25 if (!buffer_size.IsValid()) {
26 handle->type = gfx::EMPTY_BUFFER;
27 return;
28 }
29 base::SharedMemory shared_memory;
30 if (!shared_memory.CreateAnonymous(buffer_size.ValueOrDie())) {
31 handle->type = gfx::EMPTY_BUFFER;
32 return;
33 }
34 handle->type = gfx::SHARED_MEMORY_BUFFER;
35 shared_memory.GiveToProcess(child_process, &handle->handle);
36 }
37
38 bool GpuMemoryBufferImplShm::Initialize() {
39 scoped_ptr<base::SharedMemory> shared_memory(new base::SharedMemory());
40 if (!shared_memory->CreateAnonymous(size_.GetArea() *
41 BytesPerPixel(internalformat_)))
42 return false;
43 shared_memory_ = shared_memory.Pass();
44 DCHECK(!shared_memory_->memory());
45 return true;
46 }
47
48 bool GpuMemoryBufferImplShm::InitializeFromHandle(
49 gfx::GpuMemoryBufferHandle handle) {
18 if (!base::SharedMemory::IsHandleValid(handle.handle)) 50 if (!base::SharedMemory::IsHandleValid(handle.handle))
19 return false; 51 return false;
20 shared_memory_.reset(new base::SharedMemory(handle.handle, false)); 52 shared_memory_.reset(new base::SharedMemory(handle.handle, false));
21 DCHECK(!shared_memory_->memory()); 53 DCHECK(!shared_memory_->memory());
22 return true; 54 return true;
23 } 55 }
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 56
32 void* GpuMemoryBufferImplShm::Map(AccessMode mode) { 57 void* GpuMemoryBufferImplShm::Map(AccessMode mode) {
33 DCHECK(!mapped_); 58 DCHECK(!mapped_);
34 if (!shared_memory_->Map(size_.GetArea() * BytesPerPixel(internalformat_))) 59 if (!shared_memory_->Map(size_.GetArea() * BytesPerPixel(internalformat_)))
35 return NULL; 60 return NULL;
36 mapped_ = true; 61 mapped_ = true;
37 return shared_memory_->memory(); 62 return shared_memory_->memory();
38 } 63 }
39 64
40 void GpuMemoryBufferImplShm::Unmap() { 65 void GpuMemoryBufferImplShm::Unmap() {
41 DCHECK(mapped_); 66 DCHECK(mapped_);
42 shared_memory_->Unmap(); 67 shared_memory_->Unmap();
43 mapped_ = false; 68 mapped_ = false;
44 } 69 }
45 70
46 gfx::GpuMemoryBufferHandle GpuMemoryBufferImplShm::GetHandle() const { 71 gfx::GpuMemoryBufferHandle GpuMemoryBufferImplShm::GetHandle() const {
47 gfx::GpuMemoryBufferHandle handle; 72 gfx::GpuMemoryBufferHandle handle;
48 handle.type = gfx::SHARED_MEMORY_BUFFER; 73 handle.type = gfx::SHARED_MEMORY_BUFFER;
49 handle.handle = shared_memory_->handle(); 74 handle.handle = shared_memory_->handle();
50 return handle; 75 return handle;
51 } 76 }
52 77
53 } // namespace content 78 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698