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/gl_surface_egl.h" |
reveman
2014/03/17 01:13:38
why is gl_bindings.h not enough?
| |
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), |
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 |
98 void GLImageShm::Destroy() { | 99 void GLImageShm::CreateTexture2D() { |
100 glGenTextures(1, &external_texture_id_); | |
101 glBindTexture(GL_TEXTURE_2D, external_texture_id_); | |
102 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); | |
103 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); | |
104 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); | |
99 } | 105 } |
100 | 106 |
107 bool GLImageShm::CreateEGLImage() { | |
108 EGLint egl_attrib_list[] = {EGL_GL_TEXTURE_LEVEL_KHR, 0, // mip-level. | |
109 EGL_IMAGE_PRESERVED_KHR, EGL_TRUE, EGL_NONE}; | |
110 egl_image_ = | |
111 eglCreateImageKHR(eglGetCurrentDisplay(), | |
112 eglGetCurrentContext(), | |
113 EGL_GL_TEXTURE_2D_KHR, | |
114 reinterpret_cast<EGLClientBuffer>(external_texture_id_), | |
115 egl_attrib_list); | |
116 if (egl_image_ == EGL_NO_IMAGE_KHR) { | |
117 EGLint error = eglGetError(); | |
118 LOG(ERROR) << "Error creating EGLImage: " << error; | |
119 return false; | |
120 } | |
121 return true; | |
122 } | |
123 | |
124 void GLImageShm::ReleaseEGLImageandTexture() { | |
125 if (egl_image_ != EGL_NO_IMAGE_KHR) | |
126 eglDestroyImageKHR(eglGetCurrentDisplay(), egl_image_); | |
127 if (external_texture_id_) | |
128 glDeleteTextures(1, &external_texture_id_); | |
129 } | |
130 | |
131 void GLImageShm::Destroy() { ReleaseEGLImageandTexture(); } | |
132 | |
101 gfx::Size GLImageShm::GetSize() { | 133 gfx::Size GLImageShm::GetSize() { |
102 return size_; | 134 return size_; |
103 } | 135 } |
104 | 136 |
105 bool GLImageShm::BindTexImage(unsigned target) { | 137 bool GLImageShm::BindTexImage(unsigned target) { |
106 TRACE_EVENT0("gpu", "GLImageShm::BindTexImage"); | 138 TRACE_EVENT0("gpu", "GLImageShm::BindTexImage"); |
107 DCHECK(shared_memory_); | 139 DCHECK(shared_memory_); |
108 DCHECK(ValidFormat(internalformat_)); | 140 DCHECK(ValidFormat(internalformat_)); |
109 | 141 |
110 size_t size = size_.GetArea() * BytesPerPixel(internalformat_); | 142 size_t size = size_.GetArea() * BytesPerPixel(internalformat_); |
111 DCHECK(!shared_memory_->memory()); | 143 DCHECK(!shared_memory_->memory()); |
112 if (!shared_memory_->Map(size)) { | 144 if (!shared_memory_->Map(size)) { |
113 DVLOG(0) << "Failed to map shared memory."; | 145 DVLOG(0) << "Failed to map shared memory."; |
114 return false; | 146 return false; |
115 } | 147 } |
116 | 148 |
117 DCHECK(shared_memory_->memory()); | 149 if (target != GL_TEXTURE_EXTERNAL_OES) { |
118 glTexImage2D(target, | 150 DCHECK(shared_memory_->memory()); |
119 0, // mip level | 151 glTexImage2D(target, |
120 TextureFormat(internalformat_), | 152 0, // mip level |
121 size_.width(), | 153 TextureFormat(internalformat_), |
122 size_.height(), | 154 size_.width(), |
123 0, // border | 155 size_.height(), |
124 DataFormat(internalformat_), | 156 0, // border |
125 DataType(internalformat_), | 157 DataFormat(internalformat_), |
126 shared_memory_->memory()); | 158 DataType(internalformat_), |
159 shared_memory_->memory()); | |
160 } else { | |
161 // Save current GL state. | |
162 GLint current_texture_id = 0; | |
163 glGetIntegerv(GL_TEXTURE_BINDING_EXTERNAL_OES, ¤t_texture_id); | |
reveman
2014/03/15 14:08:45
EXTERNAL_OES binding is not the state you're chang
reveman
2014/03/17 01:13:38
Also, use ScopedTextureBinder here instead.
| |
164 CreateTexture2D(); | |
127 | 165 |
166 DCHECK(shared_memory_->memory()); | |
167 glTexImage2D(GL_TEXTURE_2D, | |
168 0, // mip level | |
169 TextureFormat(internalformat_), | |
170 size_.width(), | |
171 size_.height(), | |
172 0, // border | |
173 DataFormat(internalformat_), | |
174 DataType(internalformat_), | |
175 shared_memory_->memory()); | |
176 | |
177 if (!CreateEGLImage()) { | |
178 shared_memory_->Unmap(); | |
179 return false; | |
180 } | |
181 glBindTexture(GL_TEXTURE_2D, external_texture_id_); | |
182 glEGLImageTargetTexture2DOES(target, egl_image_); | |
183 | |
184 // Restore GL State and release. | |
185 ReleaseEGLImageandTexture(); | |
reveman
2014/03/15 14:08:45
what's the point of having egl_image_ and external
reveman
2014/03/17 01:13:38
Don't get hung up on reusing the egl image though.
| |
186 glBindTexture(target, current_texture_id); | |
187 } | |
128 shared_memory_->Unmap(); | 188 shared_memory_->Unmap(); |
129 return true; | 189 return true; |
130 } | 190 } |
131 | 191 |
132 void GLImageShm::ReleaseTexImage(unsigned target) { | 192 void GLImageShm::ReleaseTexImage(unsigned target) { |
133 } | 193 } |
134 | 194 |
135 void GLImageShm::WillUseTexImage() { | 195 void GLImageShm::WillUseTexImage() { |
136 } | 196 } |
137 | 197 |
138 void GLImageShm::DidUseTexImage() { | 198 void GLImageShm::DidUseTexImage() { |
139 } | 199 } |
140 | 200 |
141 } // namespace gfx | 201 } // namespace gfx |
OLD | NEW |