OLD | NEW |
---|---|
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "ui/gl/gl_image_shm.h" | 5 #include "ui/gl/gl_image_shm.h" |
6 | 6 |
7 #include "base/debug/trace_event.h" | 7 #include "base/debug/trace_event.h" |
8 #include "base/process/process_handle.h" | 8 #include "base/process/process_handle.h" |
9 #include "ui/gl/gl_bindings.h" | 9 #include "ui/gl/scoped_binders.h" |
10 | 10 |
11 namespace gfx { | 11 namespace gfx { |
12 | 12 |
13 namespace { | 13 namespace { |
14 | 14 |
15 bool ValidFormat(unsigned internalformat) { | 15 bool ValidFormat(unsigned internalformat) { |
16 switch (internalformat) { | 16 switch (internalformat) { |
17 case GL_BGRA8_EXT: | 17 case GL_BGRA8_EXT: |
18 case GL_RGBA8_OES: | 18 case GL_RGBA8_OES: |
19 return true; | 19 return true; |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
57 default: | 57 default: |
58 NOTREACHED(); | 58 NOTREACHED(); |
59 return 0; | 59 return 0; |
60 } | 60 } |
61 } | 61 } |
62 | 62 |
63 } // namespace | 63 } // namespace |
64 | 64 |
65 GLImageShm::GLImageShm(gfx::Size size, unsigned internalformat) | 65 GLImageShm::GLImageShm(gfx::Size size, unsigned internalformat) |
66 : size_(size), | 66 : size_(size), |
67 internalformat_(internalformat) { | 67 internalformat_(internalformat), |
68 } | 68 external_texture_id_(0), |
reveman
2014/03/18 10:41:04
nit: 0u
| |
69 egl_image_(EGL_NO_IMAGE_KHR) {} | |
69 | 70 |
70 GLImageShm::~GLImageShm() { | 71 GLImageShm::~GLImageShm() { |
71 Destroy(); | 72 Destroy(); |
72 } | 73 } |
73 | 74 |
74 bool GLImageShm::Initialize(gfx::GpuMemoryBufferHandle buffer) { | 75 bool GLImageShm::Initialize(gfx::GpuMemoryBufferHandle buffer) { |
75 if (!ValidFormat(internalformat_)) { | 76 if (!ValidFormat(internalformat_)) { |
76 DVLOG(0) << "Invalid format: " << internalformat_; | 77 DVLOG(0) << "Invalid format: " << internalformat_; |
77 return false; | 78 return false; |
78 } | 79 } |
79 | 80 |
80 if (!base::SharedMemory::IsHandleValid(buffer.handle)) | 81 if (!base::SharedMemory::IsHandleValid(buffer.handle)) |
81 return false; | 82 return false; |
82 | 83 |
83 base::SharedMemory shared_memory(buffer.handle, true); | 84 base::SharedMemory shared_memory(buffer.handle, true); |
84 | 85 |
85 // Duplicate the handle. | 86 // Duplicate the handle. |
86 base::SharedMemoryHandle duped_shared_memory_handle; | 87 base::SharedMemoryHandle duped_shared_memory_handle; |
87 if (!shared_memory.ShareToProcess(base::GetCurrentProcessHandle(), | 88 if (!shared_memory.ShareToProcess(base::GetCurrentProcessHandle(), |
88 &duped_shared_memory_handle)) { | 89 &duped_shared_memory_handle)) { |
89 DVLOG(0) << "Failed to duplicate shared memory handle."; | 90 DVLOG(0) << "Failed to duplicate shared memory handle."; |
90 return false; | 91 return false; |
91 } | 92 } |
92 | 93 |
93 shared_memory_.reset( | 94 shared_memory_.reset( |
94 new base::SharedMemory(duped_shared_memory_handle, true)); | 95 new base::SharedMemory(duped_shared_memory_handle, true)); |
95 return true; | 96 return true; |
96 } | 97 } |
97 | 98 |
99 void GLImageShm::SetGLParameters() { | |
100 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); | |
101 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); | |
102 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); | |
103 } | |
reveman
2014/03/18 10:41:04
I'd remove this utility function. Just makes the c
| |
104 | |
105 bool GLImageShm::CreateEGLImage() { | |
reveman
2014/03/18 10:41:04
I'm not sure this utility function is worth much e
| |
106 EGLint egl_attrib_list[] = {EGL_GL_TEXTURE_LEVEL_KHR, 0, // mip-level. | |
107 EGL_IMAGE_PRESERVED_KHR, EGL_TRUE, EGL_NONE}; | |
108 egl_image_ = | |
109 eglCreateImageKHR(eglGetCurrentDisplay(), | |
110 eglGetCurrentContext(), | |
111 EGL_GL_TEXTURE_2D_KHR, | |
112 reinterpret_cast<EGLClientBuffer>(external_texture_id_), | |
113 egl_attrib_list); | |
114 if (egl_image_ == EGL_NO_IMAGE_KHR) { | |
reveman
2014/03/18 10:41:04
To keep things simple, could this just be:
DCHECK_
| |
115 EGLint error = eglGetError(); | |
116 LOG(ERROR) << "Error creating EGLImage: " << error; | |
117 return false; | |
118 } | |
119 return true; | |
120 } | |
121 | |
98 void GLImageShm::Destroy() { | 122 void GLImageShm::Destroy() { |
123 if (egl_image_ != EGL_NO_IMAGE_KHR) | |
124 eglDestroyImageKHR(eglGetCurrentDisplay(), egl_image_); | |
125 if (external_texture_id_) | |
126 glDeleteTextures(1, &external_texture_id_); | |
99 } | 127 } |
100 | 128 |
101 gfx::Size GLImageShm::GetSize() { | 129 gfx::Size GLImageShm::GetSize() { |
102 return size_; | 130 return size_; |
103 } | 131 } |
104 | 132 |
105 bool GLImageShm::BindTexImage(unsigned target) { | 133 bool GLImageShm::BindTexImage(unsigned target) { |
106 TRACE_EVENT0("gpu", "GLImageShm::BindTexImage"); | 134 TRACE_EVENT0("gpu", "GLImageShm::BindTexImage"); |
107 DCHECK(shared_memory_); | 135 DCHECK(shared_memory_); |
108 DCHECK(ValidFormat(internalformat_)); | 136 DCHECK(ValidFormat(internalformat_)); |
109 | 137 |
110 size_t size = size_.GetArea() * BytesPerPixel(internalformat_); | 138 size_t size = size_.GetArea() * BytesPerPixel(internalformat_); |
111 DCHECK(!shared_memory_->memory()); | 139 DCHECK(!shared_memory_->memory()); |
112 if (!shared_memory_->Map(size)) { | 140 if (!shared_memory_->Map(size)) { |
113 DVLOG(0) << "Failed to map shared memory."; | 141 DVLOG(0) << "Failed to map shared memory."; |
114 return false; | 142 return false; |
115 } | 143 } |
116 | 144 |
117 DCHECK(shared_memory_->memory()); | 145 if (target != GL_TEXTURE_EXTERNAL_OES) { |
118 glTexImage2D(target, | 146 DCHECK(shared_memory_->memory()); |
reveman
2014/03/18 10:41:04
move this DCHECK above so it doesn't have to be du
| |
119 0, // mip level | 147 glTexImage2D(target, |
120 TextureFormat(internalformat_), | 148 0, // mip level |
121 size_.width(), | 149 TextureFormat(internalformat_), |
122 size_.height(), | 150 size_.width(), |
123 0, // border | 151 size_.height(), |
124 DataFormat(internalformat_), | 152 0, // border |
125 DataType(internalformat_), | 153 DataFormat(internalformat_), |
126 shared_memory_->memory()); | 154 DataType(internalformat_), |
155 shared_memory_->memory()); | |
156 } else { | |
157 if (!external_texture_id_) | |
158 glGenTextures(1, &external_texture_id_); | |
127 | 159 |
160 ScopedTextureBinder texture_binder(GL_TEXTURE_2D, external_texture_id_); | |
161 SetGLParameters(); | |
162 | |
163 DCHECK(shared_memory_->memory()); | |
164 glTexImage2D(GL_TEXTURE_2D, | |
165 0, // mip level | |
166 TextureFormat(internalformat_), | |
167 size_.width(), | |
168 size_.height(), | |
169 0, // border | |
170 DataFormat(internalformat_), | |
171 DataType(internalformat_), | |
172 shared_memory_->memory()); | |
173 | |
174 if (!egl_image_) { | |
175 if (!CreateEGLImage()) { | |
176 shared_memory_->Unmap(); | |
177 return false; | |
178 } | |
179 } | |
180 glBindTexture(GL_TEXTURE_2D, external_texture_id_); | |
181 glEGLImageTargetTexture2DOES(target, egl_image_); | |
182 } | |
128 shared_memory_->Unmap(); | 183 shared_memory_->Unmap(); |
129 return true; | 184 return true; |
130 } | 185 } |
131 | 186 |
132 void GLImageShm::ReleaseTexImage(unsigned target) { | 187 void GLImageShm::ReleaseTexImage(unsigned target) { |
133 } | 188 } |
134 | 189 |
135 void GLImageShm::WillUseTexImage() { | 190 void GLImageShm::WillUseTexImage() { |
136 } | 191 } |
137 | 192 |
138 void GLImageShm::DidUseTexImage() { | 193 void GLImageShm::DidUseTexImage() { |
139 } | 194 } |
140 | 195 |
141 } // namespace gfx | 196 } // namespace gfx |
OLD | NEW |