- add sources.
[platform/framework/web/crosswalk.git] / src / ui / gl / gl_image_shm.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 "ui/gl/gl_image_shm.h"
6
7 #include "base/debug/trace_event.h"
8 #include "base/process/process_handle.h"
9 #include "ui/gl/gl_bindings.h"
10
11 namespace gfx {
12
13 GLImageShm::GLImageShm(gfx::Size size, unsigned internalformat)
14     : size_(size),
15       internalformat_(internalformat) {
16   // GL_RGBA8_OES is currently the only supported internalformat.
17   DCHECK_EQ(static_cast<GLenum>(GL_RGBA8_OES), internalformat);
18 }
19
20 GLImageShm::~GLImageShm() {
21   Destroy();
22 }
23
24 bool GLImageShm::Initialize(gfx::GpuMemoryBufferHandle buffer) {
25   if (!base::SharedMemory::IsHandleValid(buffer.handle))
26     return false;
27
28   base::SharedMemory shared_memory(buffer.handle, true);
29
30   // Duplicate the handle.
31   base::SharedMemoryHandle duped_shared_memory_handle;
32   if (!shared_memory.ShareToProcess(base::GetCurrentProcessHandle(),
33                                     &duped_shared_memory_handle)) {
34     DVLOG(0) << "Failed to duplicate shared memory handle.";
35     return false;
36   }
37
38   shared_memory_.reset(
39       new base::SharedMemory(duped_shared_memory_handle, true));
40   return true;
41 }
42
43 void GLImageShm::Destroy() {
44 }
45
46 gfx::Size GLImageShm::GetSize() {
47   return size_;
48 }
49
50 bool GLImageShm::BindTexImage() {
51   TRACE_EVENT0("gpu", "GLImageShm::BindTexImage");
52   DCHECK(shared_memory_);
53
54   GLenum internalformat;
55   GLenum format;
56   GLenum type;
57   int bytes_per_pixel;
58   switch (internalformat_) {
59     case GL_RGBA8_OES:
60       internalformat = GL_RGBA;
61       format = GL_RGBA;
62       type = GL_UNSIGNED_BYTE;
63       bytes_per_pixel = 4;
64       break;
65     default:
66       DVLOG(0) << "Invalid format: " << internalformat_;
67       return false;
68   }
69
70   size_t size = size_.GetArea() * bytes_per_pixel;
71   DCHECK(!shared_memory_->memory());
72   if (!shared_memory_->Map(size)) {
73     DVLOG(0) << "Failed to map shared memory.";
74     return false;
75   }
76
77   DCHECK(shared_memory_->memory());
78   glTexImage2D(GL_TEXTURE_2D,
79                0,  // mip level
80                internalformat,
81                size_.width(),
82                size_.height(),
83                0,  // border
84                format,
85                type,
86                shared_memory_->memory());
87
88   shared_memory_->Unmap();
89   return true;
90 }
91
92 void GLImageShm::ReleaseTexImage() {
93 }
94
95 void GLImageShm::WillUseTexImage() {
96 }
97
98 void GLImageShm::DidUseTexImage() {
99 }
100
101 }  // namespace gfx