| 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/numerics/safe_math.h" |
| 9 #include "base/process/process_handle.h" | 9 #include "base/process/process_handle.h" |
| 10 | 10 |
| 11 namespace gfx { | 11 namespace gfx { |
| 12 namespace { | 12 namespace { |
| 13 | 13 |
| 14 // Returns true if the size is valid and false otherwise. | 14 // Returns true if the size is valid and false otherwise. |
| 15 bool SizeInBytes(const gfx::Size& size, | 15 bool SizeInBytes(const gfx::Size& size, |
| 16 gfx::GpuMemoryBuffer::Format format, | 16 gfx::BufferFormat format, |
| 17 size_t* size_in_bytes) { | 17 size_t* size_in_bytes) { |
| 18 if (size.IsEmpty()) | 18 if (size.IsEmpty()) |
| 19 return false; | 19 return false; |
| 20 | 20 |
| 21 size_t stride_in_bytes = 0; | 21 size_t stride_in_bytes = 0; |
| 22 if (!GLImageMemory::StrideInBytes(size.width(), format, &stride_in_bytes)) | 22 if (!GLImageMemory::StrideInBytes(size.width(), format, &stride_in_bytes)) |
| 23 return false; | 23 return false; |
| 24 base::CheckedNumeric<size_t> s = stride_in_bytes; | 24 base::CheckedNumeric<size_t> s = stride_in_bytes; |
| 25 s *= size.height(); | 25 s *= size.height(); |
| 26 if (!s.IsValid()) | 26 if (!s.IsValid()) |
| 27 return false; | 27 return false; |
| 28 *size_in_bytes = s.ValueOrDie(); | 28 *size_in_bytes = s.ValueOrDie(); |
| 29 return true; | 29 return true; |
| 30 } | 30 } |
| 31 | 31 |
| 32 } // namespace | 32 } // namespace |
| 33 | 33 |
| 34 GLImageSharedMemory::GLImageSharedMemory(const gfx::Size& size, | 34 GLImageSharedMemory::GLImageSharedMemory(const gfx::Size& size, |
| 35 unsigned internalformat) | 35 unsigned internalformat) |
| 36 : GLImageMemory(size, internalformat) { | 36 : GLImageMemory(size, internalformat) { |
| 37 } | 37 } |
| 38 | 38 |
| 39 GLImageSharedMemory::~GLImageSharedMemory() { | 39 GLImageSharedMemory::~GLImageSharedMemory() { |
| 40 DCHECK(!shared_memory_); | 40 DCHECK(!shared_memory_); |
| 41 } | 41 } |
| 42 | 42 |
| 43 bool GLImageSharedMemory::Initialize(const gfx::GpuMemoryBufferHandle& handle, | 43 bool GLImageSharedMemory::Initialize(const gfx::GpuMemoryBufferHandle& handle, |
| 44 gfx::GpuMemoryBuffer::Format format) { | 44 gfx::BufferFormat format) { |
| 45 size_t size_in_bytes; | 45 size_t size_in_bytes; |
| 46 if (!SizeInBytes(GetSize(), format, &size_in_bytes)) | 46 if (!SizeInBytes(GetSize(), format, &size_in_bytes)) |
| 47 return false; | 47 return false; |
| 48 | 48 |
| 49 if (!base::SharedMemory::IsHandleValid(handle.handle)) | 49 if (!base::SharedMemory::IsHandleValid(handle.handle)) |
| 50 return false; | 50 return false; |
| 51 | 51 |
| 52 base::SharedMemory shared_memory(handle.handle, true); | 52 base::SharedMemory shared_memory(handle.handle, true); |
| 53 | 53 |
| 54 // Duplicate the handle. | 54 // Duplicate the handle. |
| (...skipping 20 matching lines...) Expand all Loading... |
| 75 shared_memory_ = duped_shared_memory.Pass(); | 75 shared_memory_ = duped_shared_memory.Pass(); |
| 76 return true; | 76 return true; |
| 77 } | 77 } |
| 78 | 78 |
| 79 void GLImageSharedMemory::Destroy(bool have_context) { | 79 void GLImageSharedMemory::Destroy(bool have_context) { |
| 80 GLImageMemory::Destroy(have_context); | 80 GLImageMemory::Destroy(have_context); |
| 81 shared_memory_.reset(); | 81 shared_memory_.reset(); |
| 82 } | 82 } |
| 83 | 83 |
| 84 } // namespace gfx | 84 } // namespace gfx |
| OLD | NEW |