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 namespace {
14
ValidFormat(unsigned internalformat)15 bool ValidFormat(unsigned internalformat) {
16 switch (internalformat) {
17 case GL_BGRA8_EXT:
18 case GL_RGBA8_OES:
19 return true;
20 default:
21 return false;
22 }
23 }
24
TextureFormat(unsigned internalformat)25 GLenum TextureFormat(unsigned internalformat) {
26 switch (internalformat) {
27 case GL_BGRA8_EXT:
28 return GL_BGRA_EXT;
29 case GL_RGBA8_OES:
30 return GL_RGBA;
31 default:
32 NOTREACHED();
33 return 0;
34 }
35 }
36
DataFormat(unsigned internalformat)37 GLenum DataFormat(unsigned internalformat) {
38 return TextureFormat(internalformat);
39 }
40
DataType(unsigned internalformat)41 GLenum DataType(unsigned internalformat) {
42 switch (internalformat) {
43 case GL_BGRA8_EXT:
44 case GL_RGBA8_OES:
45 return GL_UNSIGNED_BYTE;
46 default:
47 NOTREACHED();
48 return 0;
49 }
50 }
51
BytesPerPixel(unsigned internalformat)52 GLenum BytesPerPixel(unsigned internalformat) {
53 switch (internalformat) {
54 case GL_BGRA8_EXT:
55 case GL_RGBA8_OES:
56 return 4;
57 default:
58 NOTREACHED();
59 return 0;
60 }
61 }
62
63 } // namespace
64
GLImageShm(gfx::Size size,unsigned internalformat)65 GLImageShm::GLImageShm(gfx::Size size, unsigned internalformat)
66 : size_(size),
67 internalformat_(internalformat) {
68 }
69
~GLImageShm()70 GLImageShm::~GLImageShm() {
71 Destroy();
72 }
73
Initialize(gfx::GpuMemoryBufferHandle buffer)74 bool GLImageShm::Initialize(gfx::GpuMemoryBufferHandle buffer) {
75 if (!ValidFormat(internalformat_)) {
76 DVLOG(0) << "Invalid format: " << internalformat_;
77 return false;
78 }
79
80 if (!base::SharedMemory::IsHandleValid(buffer.handle))
81 return false;
82
83 base::SharedMemory shared_memory(buffer.handle, true);
84
85 // Duplicate the handle.
86 base::SharedMemoryHandle duped_shared_memory_handle;
87 if (!shared_memory.ShareToProcess(base::GetCurrentProcessHandle(),
88 &duped_shared_memory_handle)) {
89 DVLOG(0) << "Failed to duplicate shared memory handle.";
90 return false;
91 }
92
93 shared_memory_.reset(
94 new base::SharedMemory(duped_shared_memory_handle, true));
95 return true;
96 }
97
Destroy()98 void GLImageShm::Destroy() {
99 }
100
GetSize()101 gfx::Size GLImageShm::GetSize() {
102 return size_;
103 }
104
BindTexImage(unsigned target)105 bool GLImageShm::BindTexImage(unsigned target) {
106 TRACE_EVENT0("gpu", "GLImageShm::BindTexImage");
107 DCHECK(shared_memory_);
108 DCHECK(ValidFormat(internalformat_));
109
110 size_t size = size_.GetArea() * BytesPerPixel(internalformat_);
111 DCHECK(!shared_memory_->memory());
112 if (!shared_memory_->Map(size)) {
113 DVLOG(0) << "Failed to map shared memory.";
114 return false;
115 }
116
117 DCHECK(shared_memory_->memory());
118 glTexImage2D(target,
119 0, // mip level
120 TextureFormat(internalformat_),
121 size_.width(),
122 size_.height(),
123 0, // border
124 DataFormat(internalformat_),
125 DataType(internalformat_),
126 shared_memory_->memory());
127
128 shared_memory_->Unmap();
129 return true;
130 }
131
ReleaseTexImage(unsigned target)132 void GLImageShm::ReleaseTexImage(unsigned target) {
133 }
134
WillUseTexImage()135 void GLImageShm::WillUseTexImage() {
136 }
137
DidUseTexImage()138 void GLImageShm::DidUseTexImage() {
139 }
140
141 } // namespace gfx
142