summaryrefslogtreecommitdiffstats
path: root/content/common/gpu/client/gpu_memory_buffer_impl.cc
blob: e5f6bc5d59215138c08f7bb70b994c719edea03b (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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// Copyright 2013 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 "content/common/gpu/client/gpu_memory_buffer_impl.h"

#include "ui/gl/gl_bindings.h"

namespace content {

GpuMemoryBufferImpl::GpuMemoryBufferImpl(
    scoped_ptr<base::SharedMemory> shared_memory,
    size_t width,
    size_t height,
    unsigned internalformat)
    : shared_memory_(shared_memory.Pass()),
      size_(gfx::Size(width, height)),
      internalformat_(internalformat),
      mapped_(false) {
  DCHECK(!shared_memory_->memory());
  DCHECK(IsFormatValid(internalformat));
}

GpuMemoryBufferImpl::~GpuMemoryBufferImpl() {
}

void GpuMemoryBufferImpl::Map(AccessMode mode, void** vaddr) {
  DCHECK(!mapped_);
  *vaddr = NULL;
  if (!shared_memory_->Map(size_.GetArea() * BytesPerPixel(internalformat_)))
    return;
  *vaddr = shared_memory_->memory();
  mapped_ = true;
}

void GpuMemoryBufferImpl::Unmap() {
  DCHECK(mapped_);
  shared_memory_->Unmap();
  mapped_ = false;
}

bool GpuMemoryBufferImpl::IsMapped() const {
  return mapped_;
}

uint32 GpuMemoryBufferImpl::GetStride() const {
  return size_.width() * BytesPerPixel(internalformat_);
}

gfx::GpuMemoryBufferHandle GpuMemoryBufferImpl::GetHandle() const {
  gfx::GpuMemoryBufferHandle handle;
  handle.type = gfx::SHARED_MEMORY_BUFFER;
  handle.handle = shared_memory_->handle();
  return handle;
}

// static
bool GpuMemoryBufferImpl::IsFormatValid(unsigned internalformat) {
  // GL_RGBA8_OES is the only supported format at the moment.
  switch (internalformat) {
    case GL_RGBA8_OES:
      return true;
    default:
      return false;
  }
}

// static
size_t GpuMemoryBufferImpl::BytesPerPixel(unsigned internalformat) {
  // GL_RGBA_OES has 4 bytes per pixel.
  switch (internalformat) {
    case GL_RGBA8_OES:
      return 4;
    default:
      NOTREACHED();
      return 0;
  }
}

}  // namespace content