summaryrefslogtreecommitdiffstats
path: root/ui/gl/gl_image_shared_memory.cc
blob: 7c615f8efabc9f191267995c96b1a55092799d04 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ui/gl/gl_image_shared_memory.h"

#include "base/logging.h"
#include "base/process/process_handle.h"

namespace gfx {

GLImageSharedMemory::GLImageSharedMemory(const gfx::Size& size,
                                         unsigned internalformat)
    : GLImageMemory(size, internalformat) {
}

GLImageSharedMemory::~GLImageSharedMemory() {
  DCHECK(!shared_memory_);
}

bool GLImageSharedMemory::Initialize(const gfx::GpuMemoryBufferHandle& handle) {
  if (!HasValidFormat())
    return false;

  if (!base::SharedMemory::IsHandleValid(handle.handle))
    return false;

  base::SharedMemory shared_memory(handle.handle, true);

  // Duplicate the handle.
  base::SharedMemoryHandle duped_shared_memory_handle;
  if (!shared_memory.ShareToProcess(base::GetCurrentProcessHandle(),
                                    &duped_shared_memory_handle)) {
    DVLOG(0) << "Failed to duplicate shared memory handle.";
    return false;
  }

  scoped_ptr<base::SharedMemory> duped_shared_memory(
      new base::SharedMemory(duped_shared_memory_handle, true));

  if (!duped_shared_memory->Map(Bytes())) {
    DVLOG(0) << "Failed to map shared memory.";
    return false;
  }

  DCHECK(!shared_memory_);
  shared_memory_ = duped_shared_memory.Pass();
  GLImageMemory::Initialize(
      static_cast<unsigned char*>(shared_memory_->memory()));
  return true;
}

void GLImageSharedMemory::Destroy(bool have_context) {
  GLImageMemory::Destroy(have_context);
  shared_memory_.reset();
}

}  // namespace gfx