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 "ui/gl/gl_image_shared_memory.h" | 5 #include "ui/gl/gl_image_shared_memory.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/numerics/safe_math.h" |
8 #include "base/process/process_handle.h" | 9 #include "base/process/process_handle.h" |
9 | 10 |
10 namespace gfx { | 11 namespace gfx { |
| 12 namespace { |
| 13 |
| 14 // Returns true if the size is valid and false otherwise. |
| 15 bool SizeInBytes(const gfx::Size& size, |
| 16 gfx::GpuMemoryBuffer::Format format, |
| 17 size_t* size_in_bytes) { |
| 18 if (size.IsEmpty()) |
| 19 return false; |
| 20 base::CheckedNumeric<size_t> s = GLImageMemory::BytesPerPixel(format); |
| 21 s *= size.width(); |
| 22 s *= size.height(); |
| 23 if (!s.IsValid()) |
| 24 return false; |
| 25 *size_in_bytes = s.ValueOrDie(); |
| 26 return true; |
| 27 } |
| 28 |
| 29 } // namespace |
11 | 30 |
12 GLImageSharedMemory::GLImageSharedMemory(const gfx::Size& size, | 31 GLImageSharedMemory::GLImageSharedMemory(const gfx::Size& size, |
13 unsigned internalformat) | 32 unsigned internalformat) |
14 : GLImageMemory(size, internalformat) { | 33 : GLImageMemory(size, internalformat) { |
15 } | 34 } |
16 | 35 |
17 GLImageSharedMemory::~GLImageSharedMemory() { | 36 GLImageSharedMemory::~GLImageSharedMemory() { |
18 DCHECK(!shared_memory_); | 37 DCHECK(!shared_memory_); |
19 } | 38 } |
20 | 39 |
21 bool GLImageSharedMemory::Initialize(const gfx::GpuMemoryBufferHandle& handle) { | 40 bool GLImageSharedMemory::Initialize(const gfx::GpuMemoryBufferHandle& handle, |
22 if (!HasValidFormat()) | 41 gfx::GpuMemoryBuffer::Format format) { |
| 42 size_t size_in_bytes; |
| 43 if (!SizeInBytes(GetSize(), format, &size_in_bytes)) |
23 return false; | 44 return false; |
24 | 45 |
25 if (!base::SharedMemory::IsHandleValid(handle.handle)) | 46 if (!base::SharedMemory::IsHandleValid(handle.handle)) |
26 return false; | 47 return false; |
27 | 48 |
28 base::SharedMemory shared_memory(handle.handle, true); | 49 base::SharedMemory shared_memory(handle.handle, true); |
29 | 50 |
30 // Duplicate the handle. | 51 // Duplicate the handle. |
31 base::SharedMemoryHandle duped_shared_memory_handle; | 52 base::SharedMemoryHandle duped_shared_memory_handle; |
32 if (!shared_memory.ShareToProcess(base::GetCurrentProcessHandle(), | 53 if (!shared_memory.ShareToProcess(base::GetCurrentProcessHandle(), |
33 &duped_shared_memory_handle)) { | 54 &duped_shared_memory_handle)) { |
34 DVLOG(0) << "Failed to duplicate shared memory handle."; | 55 DVLOG(0) << "Failed to duplicate shared memory handle."; |
35 return false; | 56 return false; |
36 } | 57 } |
37 | 58 |
38 scoped_ptr<base::SharedMemory> duped_shared_memory( | 59 scoped_ptr<base::SharedMemory> duped_shared_memory( |
39 new base::SharedMemory(duped_shared_memory_handle, true)); | 60 new base::SharedMemory(duped_shared_memory_handle, true)); |
| 61 if (!duped_shared_memory->Map(size_in_bytes)) { |
| 62 DVLOG(0) << "Failed to map shared memory."; |
| 63 return false; |
| 64 } |
40 | 65 |
41 if (!duped_shared_memory->Map(Bytes())) { | 66 if (!GLImageMemory::Initialize( |
42 DVLOG(0) << "Failed to map shared memory."; | 67 static_cast<unsigned char*>(duped_shared_memory->memory()), format)) { |
43 return false; | 68 return false; |
44 } | 69 } |
45 | 70 |
46 DCHECK(!shared_memory_); | 71 DCHECK(!shared_memory_); |
47 shared_memory_ = duped_shared_memory.Pass(); | 72 shared_memory_ = duped_shared_memory.Pass(); |
48 GLImageMemory::Initialize( | |
49 static_cast<unsigned char*>(shared_memory_->memory())); | |
50 return true; | 73 return true; |
51 } | 74 } |
52 | 75 |
53 void GLImageSharedMemory::Destroy(bool have_context) { | 76 void GLImageSharedMemory::Destroy(bool have_context) { |
54 GLImageMemory::Destroy(have_context); | 77 GLImageMemory::Destroy(have_context); |
55 shared_memory_.reset(); | 78 shared_memory_.reset(); |
56 } | 79 } |
57 | 80 |
58 } // namespace gfx | 81 } // namespace gfx |
OLD | NEW |