- add sources.
[platform/framework/web/crosswalk.git] / src / content / common / gpu / client / gpu_memory_buffer_impl.cc
1 // Copyright 2013 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.
4
5 #include "content/common/gpu/client/gpu_memory_buffer_impl.h"
6
7 #include "ui/gl/gl_bindings.h"
8
9 namespace content {
10
11 GpuMemoryBufferImpl::GpuMemoryBufferImpl(
12     scoped_ptr<base::SharedMemory> shared_memory,
13     size_t width,
14     size_t height,
15     unsigned internalformat)
16     : shared_memory_(shared_memory.Pass()),
17       size_(gfx::Size(width, height)),
18       internalformat_(internalformat),
19       mapped_(false) {
20   DCHECK(!shared_memory_->memory());
21   DCHECK(IsFormatValid(internalformat));
22 }
23
24 GpuMemoryBufferImpl::~GpuMemoryBufferImpl() {
25 }
26
27 void GpuMemoryBufferImpl::Map(AccessMode mode, void** vaddr) {
28   DCHECK(!mapped_);
29   *vaddr = NULL;
30   if (!shared_memory_->Map(size_.GetArea() * BytesPerPixel(internalformat_)))
31     return;
32   *vaddr = shared_memory_->memory();
33   mapped_ = true;
34 }
35
36 void GpuMemoryBufferImpl::Unmap() {
37   DCHECK(mapped_);
38   shared_memory_->Unmap();
39   mapped_ = false;
40 }
41
42 bool GpuMemoryBufferImpl::IsMapped() const {
43   return mapped_;
44 }
45
46 uint32 GpuMemoryBufferImpl::GetStride() const {
47   return size_.width() * BytesPerPixel(internalformat_);
48 }
49
50 gfx::GpuMemoryBufferHandle GpuMemoryBufferImpl::GetHandle() const {
51   gfx::GpuMemoryBufferHandle handle;
52   handle.type = gfx::SHARED_MEMORY_BUFFER;
53   handle.handle = shared_memory_->handle();
54   return handle;
55 }
56
57 // static
58 bool GpuMemoryBufferImpl::IsFormatValid(unsigned internalformat) {
59   // GL_RGBA8_OES is the only supported format at the moment.
60   switch (internalformat) {
61     case GL_RGBA8_OES:
62       return true;
63     default:
64       return false;
65   }
66 }
67
68 // static
69 size_t GpuMemoryBufferImpl::BytesPerPixel(unsigned internalformat) {
70   // GL_RGBA_OES has 4 bytes per pixel.
71   switch (internalformat) {
72     case GL_RGBA8_OES:
73       return 4;
74     default:
75       NOTREACHED();
76       return 0;
77   }
78 }
79
80 }  // namespace content