Chromium Code Reviews| 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 "content/common/gpu/client/gpu_memory_buffer_impl_shared_memory.h" | 5 #include "content/common/gpu/client/gpu_memory_buffer_impl_shared_memory.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/numerics/safe_math.h" | 8 #include "base/numerics/safe_math.h" |
| 9 #include "ui/gl/gl_bindings.h" | 9 #include "ui/gl/gl_bindings.h" |
| 10 | 10 |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 28 | 28 |
| 29 GpuMemoryBufferImplSharedMemory::~GpuMemoryBufferImplSharedMemory() { | 29 GpuMemoryBufferImplSharedMemory::~GpuMemoryBufferImplSharedMemory() { |
| 30 } | 30 } |
| 31 | 31 |
| 32 // static | 32 // static |
| 33 scoped_ptr<GpuMemoryBufferImpl> GpuMemoryBufferImplSharedMemory::Create( | 33 scoped_ptr<GpuMemoryBufferImpl> GpuMemoryBufferImplSharedMemory::Create( |
| 34 gfx::GpuMemoryBufferId id, | 34 gfx::GpuMemoryBufferId id, |
| 35 const gfx::Size& size, | 35 const gfx::Size& size, |
| 36 Format format) { | 36 Format format) { |
| 37 scoped_ptr<base::SharedMemory> shared_memory(new base::SharedMemory()); | 37 scoped_ptr<base::SharedMemory> shared_memory(new base::SharedMemory()); |
| 38 if (!shared_memory->CreateAnonymous(size.GetArea() * BytesPerPixel(format))) | 38 |
| 39 size_t stride_in_bytes = 0; | |
| 40 if (!StrideInBytes(size.width(), format, &stride_in_bytes)) | |
| 41 return scoped_ptr<GpuMemoryBufferImpl>(); | |
| 42 | |
| 43 base::CheckedNumeric<size_t> size_in_bytes = stride_in_bytes; | |
| 44 size_in_bytes *= size.height(); | |
| 45 if (!size_in_bytes.IsValid()) | |
| 46 return scoped_ptr<GpuMemoryBufferImpl>(); | |
| 47 | |
| 48 if (!shared_memory->CreateAnonymous(size_in_bytes.ValueOrDie())) | |
| 39 return scoped_ptr<GpuMemoryBufferImpl>(); | 49 return scoped_ptr<GpuMemoryBufferImpl>(); |
| 40 | 50 |
| 41 return make_scoped_ptr(new GpuMemoryBufferImplSharedMemory( | 51 return make_scoped_ptr(new GpuMemoryBufferImplSharedMemory( |
| 42 id, size, format, base::Bind(&Noop), shared_memory.Pass())); | 52 id, size, format, base::Bind(&Noop), shared_memory.Pass())); |
| 43 } | 53 } |
| 44 | 54 |
| 45 // static | 55 // static |
| 46 gfx::GpuMemoryBufferHandle | 56 gfx::GpuMemoryBufferHandle |
| 47 GpuMemoryBufferImplSharedMemory::AllocateForChildProcess( | 57 GpuMemoryBufferImplSharedMemory::AllocateForChildProcess( |
| 48 gfx::GpuMemoryBufferId id, | 58 gfx::GpuMemoryBufferId id, |
| 49 const gfx::Size& size, | 59 const gfx::Size& size, |
| 50 Format format, | 60 Format format, |
| 51 base::ProcessHandle child_process) { | 61 base::ProcessHandle child_process) { |
| 52 base::CheckedNumeric<int> buffer_size = size.width(); | 62 size_t stride_in_bytes = 0; |
| 63 if (!StrideInBytes(size.width(), format, &stride_in_bytes)) | |
| 64 return gfx::GpuMemoryBufferHandle(); | |
| 65 | |
| 66 base::CheckedNumeric<int> buffer_size = stride_in_bytes; | |
| 53 buffer_size *= size.height(); | 67 buffer_size *= size.height(); |
| 54 buffer_size *= BytesPerPixel(format); | |
| 55 if (!buffer_size.IsValid()) | 68 if (!buffer_size.IsValid()) |
| 56 return gfx::GpuMemoryBufferHandle(); | 69 return gfx::GpuMemoryBufferHandle(); |
| 57 | 70 |
| 58 base::SharedMemory shared_memory; | 71 base::SharedMemory shared_memory; |
| 59 if (!shared_memory.CreateAnonymous(buffer_size.ValueOrDie())) | 72 if (!shared_memory.CreateAnonymous(buffer_size.ValueOrDie())) |
| 60 return gfx::GpuMemoryBufferHandle(); | 73 return gfx::GpuMemoryBufferHandle(); |
| 61 | 74 |
| 62 gfx::GpuMemoryBufferHandle handle; | 75 gfx::GpuMemoryBufferHandle handle; |
| 63 handle.type = gfx::SHARED_MEMORY_BUFFER; | 76 handle.type = gfx::SHARED_MEMORY_BUFFER; |
| 64 handle.id = id; | 77 handle.id = id; |
| (...skipping 29 matching lines...) Expand all Loading... | |
| 94 case RGBX_8888: | 107 case RGBX_8888: |
| 95 return false; | 108 return false; |
| 96 } | 109 } |
| 97 | 110 |
| 98 NOTREACHED(); | 111 NOTREACHED(); |
| 99 return false; | 112 return false; |
| 100 } | 113 } |
| 101 | 114 |
| 102 void* GpuMemoryBufferImplSharedMemory::Map() { | 115 void* GpuMemoryBufferImplSharedMemory::Map() { |
| 103 DCHECK(!mapped_); | 116 DCHECK(!mapped_); |
| 104 if (!shared_memory_->Map(size_.GetArea() * BytesPerPixel(format_))) | 117 |
| 118 size_t stride_in_bytes = 0; | |
| 119 if (!StrideInBytes(size_.width(), format_, &stride_in_bytes)) | |
| 120 return NULL; | |
| 121 | |
| 122 base::CheckedNumeric<size_t> size_in_bytes = stride_in_bytes; | |
| 123 size_in_bytes *= size_.height(); | |
| 124 if (!size_in_bytes.IsValid()) | |
| 125 return NULL; | |
| 126 | |
| 127 if (!shared_memory_->Map(size_in_bytes.ValueOrDie())) | |
| 105 return NULL; | 128 return NULL; |
| 106 mapped_ = true; | 129 mapped_ = true; |
| 107 return shared_memory_->memory(); | 130 return shared_memory_->memory(); |
| 108 } | 131 } |
| 109 | 132 |
| 110 void GpuMemoryBufferImplSharedMemory::Unmap() { | 133 void GpuMemoryBufferImplSharedMemory::Unmap() { |
| 111 DCHECK(mapped_); | 134 DCHECK(mapped_); |
| 112 shared_memory_->Unmap(); | 135 shared_memory_->Unmap(); |
| 113 mapped_ = false; | 136 mapped_ = false; |
| 114 } | 137 } |
| 115 | 138 |
| 116 uint32 GpuMemoryBufferImplSharedMemory::GetStride() const { | 139 uint32 GpuMemoryBufferImplSharedMemory::GetStride() const { |
| 117 return size_.width() * BytesPerPixel(format_); | 140 size_t stride_in_bytes = 0; |
| 141 if (!StrideInBytes(size_.width(), format_, &stride_in_bytes)) | |
| 142 NOTREACHED(); | |
|
reveman
2015/01/21 18:18:20
nit: I would prefer:
bool valid_stride = StrideInB
| |
| 143 return stride_in_bytes; | |
| 118 } | 144 } |
| 119 | 145 |
| 120 gfx::GpuMemoryBufferHandle GpuMemoryBufferImplSharedMemory::GetHandle() const { | 146 gfx::GpuMemoryBufferHandle GpuMemoryBufferImplSharedMemory::GetHandle() const { |
| 121 gfx::GpuMemoryBufferHandle handle; | 147 gfx::GpuMemoryBufferHandle handle; |
| 122 handle.type = gfx::SHARED_MEMORY_BUFFER; | 148 handle.type = gfx::SHARED_MEMORY_BUFFER; |
| 123 handle.id = id_; | 149 handle.id = id_; |
| 124 handle.handle = shared_memory_->handle(); | 150 handle.handle = shared_memory_->handle(); |
| 125 return handle; | 151 return handle; |
| 126 } | 152 } |
| 127 | 153 |
| 128 } // namespace content | 154 } // namespace content |
| OLD | NEW |