ozone: evdev: Sync caps lock LED state to evdev
[chromium-blink-merge.git] / ui / gl / gl_image_shared_memory.cc
blob363ebf5b96ee1c9858fa4628b5b62908a38dd4c6
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
3 // found in the LICENSE file.
5 #include "ui/gl/gl_image_shared_memory.h"
7 #include "base/logging.h"
8 #include "base/numerics/safe_math.h"
9 #include "base/process/process_handle.h"
11 namespace gfx {
12 namespace {
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;
21 if (!GLImageMemory::ValidSize(size, format))
22 return false;
24 size_t stride_in_bytes = 0;
25 if (!GLImageMemory::StrideInBytes(size.width(), format, &stride_in_bytes))
26 return false;
27 base::CheckedNumeric<size_t> s = stride_in_bytes;
28 s *= size.height();
29 if (!s.IsValid())
30 return false;
31 *size_in_bytes = s.ValueOrDie();
32 return true;
35 } // namespace
37 GLImageSharedMemory::GLImageSharedMemory(const gfx::Size& size,
38 unsigned internalformat)
39 : GLImageMemory(size, internalformat) {
42 GLImageSharedMemory::~GLImageSharedMemory() {
43 DCHECK(!shared_memory_);
46 bool GLImageSharedMemory::Initialize(const gfx::GpuMemoryBufferHandle& handle,
47 gfx::GpuMemoryBuffer::Format format) {
48 size_t size_in_bytes;
49 if (!SizeInBytes(GetSize(), format, &size_in_bytes))
50 return false;
52 if (!base::SharedMemory::IsHandleValid(handle.handle))
53 return false;
55 base::SharedMemory shared_memory(handle.handle, true);
57 // Duplicate the handle.
58 base::SharedMemoryHandle duped_shared_memory_handle;
59 if (!shared_memory.ShareToProcess(base::GetCurrentProcessHandle(),
60 &duped_shared_memory_handle)) {
61 DVLOG(0) << "Failed to duplicate shared memory handle.";
62 return false;
65 scoped_ptr<base::SharedMemory> duped_shared_memory(
66 new base::SharedMemory(duped_shared_memory_handle, true));
67 if (!duped_shared_memory->Map(size_in_bytes)) {
68 DVLOG(0) << "Failed to map shared memory.";
69 return false;
72 if (!GLImageMemory::Initialize(
73 static_cast<unsigned char*>(duped_shared_memory->memory()), format)) {
74 return false;
77 DCHECK(!shared_memory_);
78 shared_memory_ = duped_shared_memory.Pass();
79 return true;
82 void GLImageSharedMemory::Destroy(bool have_context) {
83 GLImageMemory::Destroy(have_context);
84 shared_memory_.reset();
87 } // namespace gfx