| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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_memory.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/logging.h" |
| 9 #include "ui/gl/gl_bindings.h" |
| 9 #include "ui/gl/scoped_binders.h" | 10 #include "ui/gl/scoped_binders.h" |
| 10 | 11 |
| 11 #if defined(OS_WIN) || defined(USE_X11) || defined(OS_ANDROID) || \ | 12 #if defined(OS_WIN) || defined(USE_X11) || defined(OS_ANDROID) || \ |
| 12 defined(USE_OZONE) | 13 defined(USE_OZONE) |
| 13 #include "ui/gl/gl_surface_egl.h" | 14 #include "ui/gl/gl_surface_egl.h" |
| 14 #endif | 15 #endif |
| 15 | 16 |
| 16 namespace gfx { | 17 namespace gfx { |
| 17 | |
| 18 namespace { | 18 namespace { |
| 19 | 19 |
| 20 bool ValidFormat(unsigned internalformat) { | 20 bool ValidFormat(unsigned internalformat) { |
| 21 switch (internalformat) { | 21 switch (internalformat) { |
| 22 case GL_BGRA8_EXT: | 22 case GL_BGRA8_EXT: |
| 23 case GL_RGBA8_OES: | 23 case GL_RGBA8_OES: |
| 24 return true; | 24 return true; |
| 25 default: | 25 default: |
| 26 return false; | 26 return false; |
| 27 } | 27 } |
| (...skipping 19 matching lines...) Expand all Loading... |
| 47 switch (internalformat) { | 47 switch (internalformat) { |
| 48 case GL_BGRA8_EXT: | 48 case GL_BGRA8_EXT: |
| 49 case GL_RGBA8_OES: | 49 case GL_RGBA8_OES: |
| 50 return GL_UNSIGNED_BYTE; | 50 return GL_UNSIGNED_BYTE; |
| 51 default: | 51 default: |
| 52 NOTREACHED(); | 52 NOTREACHED(); |
| 53 return 0; | 53 return 0; |
| 54 } | 54 } |
| 55 } | 55 } |
| 56 | 56 |
| 57 GLenum BytesPerPixel(unsigned internalformat) { | 57 int BytesPerPixel(unsigned internalformat) { |
| 58 switch (internalformat) { | 58 switch (internalformat) { |
| 59 case GL_BGRA8_EXT: | 59 case GL_BGRA8_EXT: |
| 60 case GL_RGBA8_OES: | 60 case GL_RGBA8_OES: |
| 61 return 4; | 61 return 4; |
| 62 default: | 62 default: |
| 63 NOTREACHED(); | 63 NOTREACHED(); |
| 64 return 0; | 64 return 0; |
| 65 } | 65 } |
| 66 } | 66 } |
| 67 | 67 |
| 68 } // namespace | 68 } // namespace |
| 69 | 69 |
| 70 GLImageShm::GLImageShm(gfx::Size size, unsigned internalformat) | 70 GLImageMemory::GLImageMemory(const gfx::Size& size, unsigned internalformat) |
| 71 : size_(size), | 71 : memory_(NULL), |
| 72 size_(size), |
| 72 internalformat_(internalformat) | 73 internalformat_(internalformat) |
| 73 #if defined(OS_WIN) || defined(USE_X11) || defined(OS_ANDROID) || \ | 74 #if defined(OS_WIN) || defined(USE_X11) || defined(OS_ANDROID) || \ |
| 74 defined(USE_OZONE) | 75 defined(USE_OZONE) |
| 75 , | 76 , |
| 76 egl_texture_id_(0u), | 77 egl_texture_id_(0u), |
| 77 egl_image_(EGL_NO_IMAGE_KHR) | 78 egl_image_(EGL_NO_IMAGE_KHR) |
| 78 #endif | 79 #endif |
| 79 { | 80 { |
| 80 } | 81 } |
| 81 | 82 |
| 82 GLImageShm::~GLImageShm() { Destroy(); } | 83 GLImageMemory::~GLImageMemory() { |
| 84 } |
| 83 | 85 |
| 84 bool GLImageShm::Initialize(gfx::GpuMemoryBufferHandle buffer) { | 86 bool GLImageMemory::Initialize(const unsigned char* memory) { |
| 85 if (!ValidFormat(internalformat_)) { | 87 if (!ValidFormat(internalformat_)) { |
| 86 DVLOG(0) << "Invalid format: " << internalformat_; | 88 DVLOG(0) << "Invalid format: " << internalformat_; |
| 87 return false; | 89 return false; |
| 88 } | 90 } |
| 89 | 91 |
| 90 if (!base::SharedMemory::IsHandleValid(buffer.handle)) | 92 DCHECK(memory); |
| 91 return false; | 93 DCHECK(!memory_); |
| 92 | 94 memory_ = memory; |
| 93 base::SharedMemory shared_memory(buffer.handle, true); | |
| 94 | |
| 95 // Duplicate the handle. | |
| 96 base::SharedMemoryHandle duped_shared_memory_handle; | |
| 97 if (!shared_memory.ShareToProcess(base::GetCurrentProcessHandle(), | |
| 98 &duped_shared_memory_handle)) { | |
| 99 DVLOG(0) << "Failed to duplicate shared memory handle."; | |
| 100 return false; | |
| 101 } | |
| 102 | |
| 103 shared_memory_.reset( | |
| 104 new base::SharedMemory(duped_shared_memory_handle, true)); | |
| 105 return true; | 95 return true; |
| 106 } | 96 } |
| 107 | 97 |
| 108 void GLImageShm::Destroy() { | 98 void GLImageMemory::Destroy() { |
| 109 #if defined(OS_WIN) || defined(USE_X11) || defined(OS_ANDROID) || \ | 99 #if defined(OS_WIN) || defined(USE_X11) || defined(OS_ANDROID) || \ |
| 110 defined(USE_OZONE) | 100 defined(USE_OZONE) |
| 111 if (egl_image_ != EGL_NO_IMAGE_KHR) { | 101 if (egl_image_ != EGL_NO_IMAGE_KHR) { |
| 112 eglDestroyImageKHR(GLSurfaceEGL::GetHardwareDisplay(), egl_image_); | 102 eglDestroyImageKHR(GLSurfaceEGL::GetHardwareDisplay(), egl_image_); |
| 113 egl_image_ = EGL_NO_IMAGE_KHR; | 103 egl_image_ = EGL_NO_IMAGE_KHR; |
| 114 } | 104 } |
| 115 | 105 |
| 116 if (egl_texture_id_) { | 106 if (egl_texture_id_) { |
| 117 glDeleteTextures(1, &egl_texture_id_); | 107 glDeleteTextures(1, &egl_texture_id_); |
| 118 egl_texture_id_ = 0u; | 108 egl_texture_id_ = 0u; |
| 119 } | 109 } |
| 120 #endif | 110 #endif |
| 111 memory_ = NULL; |
| 121 } | 112 } |
| 122 | 113 |
| 123 gfx::Size GLImageShm::GetSize() { return size_; } | 114 gfx::Size GLImageMemory::GetSize() { |
| 115 return size_; |
| 116 } |
| 124 | 117 |
| 125 bool GLImageShm::BindTexImage(unsigned target) { | 118 bool GLImageMemory::BindTexImage(unsigned target) { |
| 126 TRACE_EVENT0("gpu", "GLImageShm::BindTexImage"); | 119 TRACE_EVENT0("gpu", "GLImageMemory::BindTexImage"); |
| 127 DCHECK(shared_memory_); | |
| 128 DCHECK(ValidFormat(internalformat_)); | |
| 129 | 120 |
| 130 size_t size = size_.GetArea() * BytesPerPixel(internalformat_); | 121 DCHECK(memory_); |
| 131 DCHECK(!shared_memory_->memory()); | |
| 132 if (!shared_memory_->Map(size)) { | |
| 133 DVLOG(0) << "Failed to map shared memory."; | |
| 134 return false; | |
| 135 } | |
| 136 | |
| 137 DCHECK(shared_memory_->memory()); | |
| 138 | |
| 139 #if defined(OS_WIN) || defined(USE_X11) || defined(OS_ANDROID) || \ | 122 #if defined(OS_WIN) || defined(USE_X11) || defined(OS_ANDROID) || \ |
| 140 defined(USE_OZONE) | 123 defined(USE_OZONE) |
| 141 if (target == GL_TEXTURE_EXTERNAL_OES) { | 124 if (target == GL_TEXTURE_EXTERNAL_OES) { |
| 142 if (egl_image_ == EGL_NO_IMAGE_KHR) { | 125 if (egl_image_ == EGL_NO_IMAGE_KHR) { |
| 143 DCHECK_EQ(0u, egl_texture_id_); | 126 DCHECK_EQ(0u, egl_texture_id_); |
| 144 glGenTextures(1, &egl_texture_id_); | 127 glGenTextures(1, &egl_texture_id_); |
| 145 | 128 |
| 146 { | 129 { |
| 147 ScopedTextureBinder texture_binder(GL_TEXTURE_2D, egl_texture_id_); | 130 ScopedTextureBinder texture_binder(GL_TEXTURE_2D, egl_texture_id_); |
| 148 | 131 |
| 149 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); | 132 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
| 150 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); | 133 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); |
| 151 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); | 134 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); |
| 152 glTexImage2D(GL_TEXTURE_2D, | 135 glTexImage2D(GL_TEXTURE_2D, |
| 153 0, // mip level | 136 0, // mip level |
| 154 TextureFormat(internalformat_), | 137 TextureFormat(internalformat_), |
| 155 size_.width(), | 138 size_.width(), |
| 156 size_.height(), | 139 size_.height(), |
| 157 0, // border | 140 0, // border |
| 158 DataFormat(internalformat_), | 141 DataFormat(internalformat_), |
| 159 DataType(internalformat_), | 142 DataType(internalformat_), |
| 160 shared_memory_->memory()); | 143 memory_); |
| 161 } | 144 } |
| 162 | 145 |
| 163 EGLint attrs[] = {EGL_IMAGE_PRESERVED_KHR, EGL_TRUE, EGL_NONE}; | 146 EGLint attrs[] = {EGL_IMAGE_PRESERVED_KHR, EGL_TRUE, EGL_NONE}; |
| 164 // Need to pass current EGL rendering context to eglCreateImageKHR for | 147 // Need to pass current EGL rendering context to eglCreateImageKHR for |
| 165 // target type EGL_GL_TEXTURE_2D_KHR. | 148 // target type EGL_GL_TEXTURE_2D_KHR. |
| 166 egl_image_ = | 149 egl_image_ = |
| 167 eglCreateImageKHR(GLSurfaceEGL::GetHardwareDisplay(), | 150 eglCreateImageKHR(GLSurfaceEGL::GetHardwareDisplay(), |
| 168 eglGetCurrentContext(), | 151 eglGetCurrentContext(), |
| 169 EGL_GL_TEXTURE_2D_KHR, | 152 EGL_GL_TEXTURE_2D_KHR, |
| 170 reinterpret_cast<EGLClientBuffer>(egl_texture_id_), | 153 reinterpret_cast<EGLClientBuffer>(egl_texture_id_), |
| 171 attrs); | 154 attrs); |
| 172 DCHECK_NE(EGL_NO_IMAGE_KHR, egl_image_) | 155 DCHECK_NE(EGL_NO_IMAGE_KHR, egl_image_) |
| 173 << "Error creating EGLImage: " << eglGetError(); | 156 << "Error creating EGLImage: " << eglGetError(); |
| 174 } else { | 157 } else { |
| 175 ScopedTextureBinder texture_binder(GL_TEXTURE_2D, egl_texture_id_); | 158 ScopedTextureBinder texture_binder(GL_TEXTURE_2D, egl_texture_id_); |
| 176 | 159 |
| 177 glTexSubImage2D(GL_TEXTURE_2D, | 160 glTexSubImage2D(GL_TEXTURE_2D, |
| 178 0, // mip level | 161 0, // mip level |
| 179 0, // x-offset | 162 0, // x-offset |
| 180 0, // y-offset | 163 0, // y-offset |
| 181 size_.width(), | 164 size_.width(), |
| 182 size_.height(), | 165 size_.height(), |
| 183 DataFormat(internalformat_), | 166 DataFormat(internalformat_), |
| 184 DataType(internalformat_), | 167 DataType(internalformat_), |
| 185 shared_memory_->memory()); | 168 memory_); |
| 186 } | 169 } |
| 187 | 170 |
| 188 glEGLImageTargetTexture2DOES(target, egl_image_); | 171 glEGLImageTargetTexture2DOES(target, egl_image_); |
| 189 DCHECK_EQ(static_cast<GLenum>(GL_NO_ERROR), glGetError()); | 172 DCHECK_EQ(static_cast<GLenum>(GL_NO_ERROR), glGetError()); |
| 190 | 173 |
| 191 shared_memory_->Unmap(); | |
| 192 return true; | 174 return true; |
| 193 } | 175 } |
| 194 #endif | 176 #endif |
| 195 | 177 |
| 196 DCHECK_NE(static_cast<GLenum>(GL_TEXTURE_EXTERNAL_OES), target); | 178 DCHECK_NE(static_cast<GLenum>(GL_TEXTURE_EXTERNAL_OES), target); |
| 197 glTexImage2D(target, | 179 glTexImage2D(target, |
| 198 0, // mip level | 180 0, // mip level |
| 199 TextureFormat(internalformat_), | 181 TextureFormat(internalformat_), |
| 200 size_.width(), | 182 size_.width(), |
| 201 size_.height(), | 183 size_.height(), |
| 202 0, // border | 184 0, // border |
| 203 DataFormat(internalformat_), | 185 DataFormat(internalformat_), |
| 204 DataType(internalformat_), | 186 DataType(internalformat_), |
| 205 shared_memory_->memory()); | 187 memory_); |
| 206 | 188 |
| 207 shared_memory_->Unmap(); | |
| 208 return true; | 189 return true; |
| 209 } | 190 } |
| 210 | 191 |
| 192 bool GLImageMemory::HasValidFormat() const { |
| 193 return ValidFormat(internalformat_); |
| 194 } |
| 195 |
| 196 size_t GLImageMemory::Bytes() const { |
| 197 return size_.GetArea() * BytesPerPixel(internalformat_); |
| 198 } |
| 199 |
| 211 } // namespace gfx | 200 } // namespace gfx |
| OLD | NEW |