Chromium Code Reviews| 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 egl_texture_id_(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 } |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 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::Destroy() { |
| 100 if (egl_image_ != EGL_NO_IMAGE_KHR) | |
| 101 eglDestroyImageKHR(eglGetCurrentDisplay(), egl_image_); | |
|
reveman
2014/03/18 16:17:53
please reset egl_image_ if you want to do this in
| |
| 102 if (egl_texture_id_) | |
|
reveman
2014/03/18 16:17:53
and egl_texture_id_ = 0,
the idea is that Destroy
| |
| 103 glDeleteTextures(1, &egl_texture_id_); | |
| 99 } | 104 } |
| 100 | 105 |
| 101 gfx::Size GLImageShm::GetSize() { | 106 gfx::Size GLImageShm::GetSize() { |
| 102 return size_; | 107 return size_; |
| 103 } | 108 } |
| 104 | 109 |
| 105 bool GLImageShm::BindTexImage(unsigned target) { | 110 bool GLImageShm::BindTexImage(unsigned target) { |
| 106 TRACE_EVENT0("gpu", "GLImageShm::BindTexImage"); | 111 TRACE_EVENT0("gpu", "GLImageShm::BindTexImage"); |
| 107 DCHECK(shared_memory_); | 112 DCHECK(shared_memory_); |
| 108 DCHECK(ValidFormat(internalformat_)); | 113 DCHECK(ValidFormat(internalformat_)); |
| 109 | 114 |
| 110 size_t size = size_.GetArea() * BytesPerPixel(internalformat_); | 115 size_t size = size_.GetArea() * BytesPerPixel(internalformat_); |
| 111 DCHECK(!shared_memory_->memory()); | 116 DCHECK(!shared_memory_->memory()); |
| 112 if (!shared_memory_->Map(size)) { | 117 if (!shared_memory_->Map(size)) { |
| 113 DVLOG(0) << "Failed to map shared memory."; | 118 DVLOG(0) << "Failed to map shared memory."; |
| 114 return false; | 119 return false; |
| 115 } | 120 } |
| 116 | 121 |
| 117 DCHECK(shared_memory_->memory()); | 122 DCHECK(shared_memory_->memory()); |
| 118 glTexImage2D(target, | 123 if (target != GL_TEXTURE_EXTERNAL_OES) { |
| 119 0, // mip level | 124 glTexImage2D(target, |
| 120 TextureFormat(internalformat_), | 125 0, // mip level |
| 121 size_.width(), | 126 TextureFormat(internalformat_), |
| 122 size_.height(), | 127 size_.width(), |
| 123 0, // border | 128 size_.height(), |
| 124 DataFormat(internalformat_), | 129 0, // border |
| 125 DataType(internalformat_), | 130 DataFormat(internalformat_), |
| 126 shared_memory_->memory()); | 131 DataType(internalformat_), |
| 132 shared_memory_->memory()); | |
| 133 } else { | |
| 134 if (!egl_texture_id_) | |
| 135 glGenTextures(1, &egl_texture_id_); | |
| 127 | 136 |
| 137 ScopedTextureBinder texture_binder(GL_TEXTURE_2D, egl_texture_id_); | |
| 138 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); | |
| 139 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); | |
| 140 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); | |
| 141 | |
| 142 glTexImage2D(GL_TEXTURE_2D, | |
| 143 0, // mip level | |
| 144 TextureFormat(internalformat_), | |
| 145 size_.width(), | |
| 146 size_.height(), | |
| 147 0, // border | |
| 148 DataFormat(internalformat_), | |
| 149 DataType(internalformat_), | |
| 150 shared_memory_->memory()); | |
| 151 | |
| 152 if (!egl_image_) { | |
| 153 EGLint egl_attrib_list[] = {EGL_GL_TEXTURE_LEVEL_KHR, 0, // mip-level. | |
| 154 EGL_IMAGE_PRESERVED_KHR, EGL_TRUE, EGL_NONE}; | |
| 155 egl_image_ = | |
| 156 eglCreateImageKHR(eglGetCurrentDisplay(), | |
| 157 eglGetCurrentContext(), | |
| 158 EGL_GL_TEXTURE_2D_KHR, | |
| 159 reinterpret_cast<EGLClientBuffer>(egl_texture_id_), | |
| 160 egl_attrib_list); | |
| 161 EGLint error = eglGetError(); | |
|
reveman
2014/03/18 16:17:53
nit: avoid this temporary variable: "...EGLImage:
| |
| 162 DCHECK_NE(EGL_NO_IMAGE_KHR, egl_image_) | |
| 163 << "Error creating EGLImage: " << error; | |
| 164 } | |
| 165 glBindTexture(GL_TEXTURE_2D, egl_texture_id_); | |
| 166 glEGLImageTargetTexture2DOES(target, egl_image_); | |
| 167 } | |
| 128 shared_memory_->Unmap(); | 168 shared_memory_->Unmap(); |
| 129 return true; | 169 return true; |
| 130 } | 170 } |
| 131 | 171 |
| 132 void GLImageShm::ReleaseTexImage(unsigned target) { | 172 void GLImageShm::ReleaseTexImage(unsigned target) { |
| 133 } | 173 } |
| 134 | 174 |
| 135 void GLImageShm::WillUseTexImage() { | 175 void GLImageShm::WillUseTexImage() { |
| 136 } | 176 } |
| 137 | 177 |
| 138 void GLImageShm::DidUseTexImage() { | 178 void GLImageShm::DidUseTexImage() { |
| 139 } | 179 } |
| 140 | 180 |
| 141 } // namespace gfx | 181 } // namespace gfx |
| OLD | NEW |