| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 extern "C" { | 5 extern "C" { |
| 6 #include <X11/extensions/Xcomposite.h> | 6 #include <X11/Xlib.h> |
| 7 } | 7 } |
| 8 | 8 |
| 9 #include "ui/gl/gl_image_glx.h" | 9 #include "ui/gl/gl_image_glx.h" |
| 10 | 10 |
| 11 #include "base/basictypes.h" | |
| 12 #include "base/logging.h" | 11 #include "base/logging.h" |
| 13 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
| 14 #include "base/message_loop/message_loop.h" | |
| 15 #include "ui/gfx/x/x11_types.h" | |
| 16 #include "ui/gl/gl_bindings.h" | 13 #include "ui/gl/gl_bindings.h" |
| 17 #include "ui/gl/gl_surface_glx.h" | 14 #include "ui/gl/gl_surface_glx.h" |
| 18 | 15 |
| 19 namespace gfx { | 16 namespace gfx { |
| 20 | 17 |
| 21 namespace { | 18 namespace { |
| 22 | 19 |
| 23 // scoped_ptr functor for XFree(). Use as follows: | 20 // scoped_ptr functor for XFree(). Use as follows: |
| 24 // scoped_ptr<XVisualInfo, ScopedPtrXFree> foo(...); | 21 // scoped_ptr<XVisualInfo, ScopedPtrXFree> foo(...); |
| 25 // where "XVisualInfo" is any X type that is freed with XFree. | 22 // where "XVisualInfo" is any X type that is freed with XFree. |
| 26 struct ScopedPtrXFree { | 23 struct ScopedPtrXFree { |
| 27 void operator()(void* x) const { ::XFree(x); } | 24 void operator()(void* x) const { ::XFree(x); } |
| 28 }; | 25 }; |
| 29 | 26 |
| 30 int BindToTextureFormat(int depth) { | 27 bool ValidFormat(unsigned internalformat) { |
| 31 if (depth == 32) | 28 switch (internalformat) { |
| 32 return GLX_BIND_TO_TEXTURE_RGBA_EXT; | 29 case GL_BGRA8_EXT: |
| 33 | 30 return true; |
| 34 return GLX_BIND_TO_TEXTURE_RGB_EXT; | 31 default: |
| 32 return false; |
| 33 } |
| 35 } | 34 } |
| 36 | 35 |
| 37 int TextureFormat(int depth) { | 36 int TextureFormat(unsigned internalformat) { |
| 38 if (depth == 32) | 37 switch (internalformat) { |
| 39 return GLX_TEXTURE_FORMAT_RGBA_EXT; | 38 case GL_BGRA8_EXT: |
| 39 return GLX_TEXTURE_FORMAT_RGBA_EXT; |
| 40 default: |
| 41 NOTREACHED(); |
| 42 return 0; |
| 43 } |
| 44 } |
| 40 | 45 |
| 41 return GLX_TEXTURE_FORMAT_RGB_EXT; | 46 int BindToTextureFormat(unsigned internalformat) { |
| 47 switch (internalformat) { |
| 48 case GL_BGRA8_EXT: |
| 49 return GLX_BIND_TO_TEXTURE_RGBA_EXT; |
| 50 default: |
| 51 NOTREACHED(); |
| 52 return 0; |
| 53 } |
| 54 } |
| 55 |
| 56 unsigned PixmapDepth(unsigned internalformat) { |
| 57 switch (internalformat) { |
| 58 case GL_BGRA8_EXT: |
| 59 return 32u; |
| 60 default: |
| 61 NOTREACHED(); |
| 62 return 0u; |
| 63 } |
| 64 } |
| 65 |
| 66 bool ActualPixmapGeometry(XID pixmap, gfx::Size* size, unsigned* depth) { |
| 67 XID root_return; |
| 68 int x_return; |
| 69 int y_return; |
| 70 unsigned width_return; |
| 71 unsigned height_return; |
| 72 unsigned border_width_return; |
| 73 unsigned depth_return; |
| 74 if (!XGetGeometry(gfx::GetXDisplay(), |
| 75 pixmap, |
| 76 &root_return, |
| 77 &x_return, |
| 78 &y_return, |
| 79 &width_return, |
| 80 &height_return, |
| 81 &border_width_return, |
| 82 &depth_return)) |
| 83 return false; |
| 84 |
| 85 if (size) |
| 86 *size = gfx::Size(width_return, height_return); |
| 87 if (depth) |
| 88 *depth = depth_return; |
| 89 return true; |
| 90 } |
| 91 |
| 92 unsigned ActualPixmapDepth(XID pixmap) { |
| 93 unsigned depth; |
| 94 if (!ActualPixmapGeometry(pixmap, NULL, &depth)) |
| 95 return -1; |
| 96 |
| 97 return depth; |
| 98 } |
| 99 |
| 100 gfx::Size ActualPixmapSize(XID pixmap) { |
| 101 gfx::Size size; |
| 102 if (!ActualPixmapGeometry(pixmap, &size, NULL)) |
| 103 return gfx::Size(); |
| 104 |
| 105 return size; |
| 42 } | 106 } |
| 43 | 107 |
| 44 } // namespace anonymous | 108 } // namespace anonymous |
| 45 | 109 |
| 46 GLImageGLX::GLImageGLX(gfx::PluginWindowHandle window) | 110 GLImageGLX::GLImageGLX(const gfx::Size& size, unsigned internalformat) |
| 47 : display_(gfx::GetXDisplay()), | 111 : glx_pixmap_(0), size_(size), internalformat_(internalformat) { |
| 48 window_(window), | 112 } |
| 49 pixmap_(0), | |
| 50 glx_pixmap_(0) {} | |
| 51 | 113 |
| 52 GLImageGLX::~GLImageGLX() { Destroy(); } | 114 GLImageGLX::~GLImageGLX() { Destroy(); } |
| 53 | 115 |
| 54 bool GLImageGLX::Initialize() { | 116 bool GLImageGLX::Initialize(XID pixmap) { |
| 55 if (!GLSurfaceGLX::IsTextureFromPixmapSupported()) { | 117 if (!GLSurfaceGLX::IsTextureFromPixmapSupported()) { |
| 56 LOG(ERROR) << "GLX_EXT_texture_from_pixmap not supported."; | 118 DVLOG(0) << "GLX_EXT_texture_from_pixmap not supported."; |
| 57 return false; | 119 return false; |
| 58 } | 120 } |
| 59 | 121 |
| 60 XWindowAttributes attributes; | 122 if (!ValidFormat(internalformat_)) { |
| 61 if (!XGetWindowAttributes(display_, window_, &attributes)) { | 123 DVLOG(0) << "Invalid format: " << internalformat_; |
| 62 LOG(ERROR) << "XGetWindowAttributes failed for window " << window_ << "."; | |
| 63 return false; | 124 return false; |
| 64 } | 125 } |
| 65 | 126 |
| 66 XVisualInfo templ; | 127 DCHECK_EQ(PixmapDepth(internalformat_), ActualPixmapDepth(pixmap)); |
| 67 templ.visualid = XVisualIDFromVisual(attributes.visual); | 128 DCHECK_EQ(size_.ToString(), ActualPixmapSize(pixmap).ToString()); |
| 68 int num_visinfo = 0; | |
| 69 scoped_ptr<XVisualInfo, ScopedPtrXFree> visinfo( | |
| 70 XGetVisualInfo(display_, VisualIDMask, &templ, &num_visinfo)); | |
| 71 if (!visinfo.get()) { | |
| 72 LOG(ERROR) << "XGetVisualInfo failed for visual id " << templ.visualid | |
| 73 << "."; | |
| 74 return false; | |
| 75 } | |
| 76 if (!num_visinfo) { | |
| 77 LOG(ERROR) << "XGetVisualInfo returned 0 elements."; | |
| 78 return false; | |
| 79 } | |
| 80 | 129 |
| 81 int config_attribs[] = { | 130 int config_attribs[] = { |
| 82 static_cast<int>(GLX_VISUAL_ID), static_cast<int>(visinfo->visualid), | 131 GLX_DRAWABLE_TYPE, GLX_PIXMAP_BIT, |
| 83 GLX_DRAWABLE_TYPE, GLX_PIXMAP_BIT, | 132 GLX_BIND_TO_TEXTURE_TARGETS_EXT, GLX_TEXTURE_2D_EXT, |
| 84 GLX_BIND_TO_TEXTURE_TARGETS_EXT, GLX_TEXTURE_2D_EXT, | 133 BindToTextureFormat(internalformat_), GL_TRUE, |
| 85 BindToTextureFormat(visinfo->depth), GL_TRUE, | |
| 86 0}; | 134 0}; |
| 87 int num_elements = 0; | 135 int num_elements = 0; |
| 88 scoped_ptr<GLXFBConfig, ScopedPtrXFree> config(glXChooseFBConfig( | 136 scoped_ptr<GLXFBConfig, ScopedPtrXFree> config( |
| 89 display_, DefaultScreen(display_), config_attribs, &num_elements)); | 137 glXChooseFBConfig(gfx::GetXDisplay(), |
| 138 DefaultScreen(gfx::GetXDisplay()), |
| 139 config_attribs, |
| 140 &num_elements)); |
| 90 if (!config.get()) { | 141 if (!config.get()) { |
| 91 LOG(ERROR) << "glXChooseFBConfig failed."; | 142 DVLOG(0) << "glXChooseFBConfig failed."; |
| 92 return false; | 143 return false; |
| 93 } | 144 } |
| 94 if (!num_elements) { | 145 if (!num_elements) { |
| 95 LOG(ERROR) << "glXChooseFBConfig returned 0 elements."; | 146 DVLOG(0) << "glXChooseFBConfig returned 0 elements."; |
| 96 return false; | |
| 97 } | |
| 98 | |
| 99 // Create backing pixmap reference. | |
| 100 pixmap_ = XCompositeNameWindowPixmap(display_, window_); | |
| 101 | |
| 102 XID root = 0; | |
| 103 int x = 0; | |
| 104 int y = 0; | |
| 105 unsigned int width = 0; | |
| 106 unsigned int height = 0; | |
| 107 unsigned int bw = 0; | |
| 108 unsigned int depth = 0; | |
| 109 if (!XGetGeometry( | |
| 110 display_, pixmap_, &root, &x, &y, &width, &height, &bw, &depth)) { | |
| 111 LOG(ERROR) << "XGetGeometry failed for pixmap " << pixmap_ << "."; | |
| 112 return false; | 147 return false; |
| 113 } | 148 } |
| 114 | 149 |
| 115 int pixmap_attribs[] = {GLX_TEXTURE_TARGET_EXT, GLX_TEXTURE_2D_EXT, | 150 int pixmap_attribs[] = {GLX_TEXTURE_TARGET_EXT, GLX_TEXTURE_2D_EXT, |
| 116 GLX_TEXTURE_FORMAT_EXT, TextureFormat(visinfo->depth), | 151 GLX_TEXTURE_FORMAT_EXT, |
| 117 0}; | 152 TextureFormat(internalformat_), 0}; |
| 118 glx_pixmap_ = | 153 glx_pixmap_ = glXCreatePixmap( |
| 119 glXCreatePixmap(display_, *config.get(), pixmap_, pixmap_attribs); | 154 gfx::GetXDisplay(), *config.get(), pixmap, pixmap_attribs); |
| 120 if (!glx_pixmap_) { | 155 if (!glx_pixmap_) { |
| 121 LOG(ERROR) << "glXCreatePixmap failed."; | 156 DVLOG(0) << "glXCreatePixmap failed."; |
| 122 return false; | 157 return false; |
| 123 } | 158 } |
| 124 | 159 |
| 125 size_ = gfx::Size(width, height); | |
| 126 return true; | 160 return true; |
| 127 } | 161 } |
| 128 | 162 |
| 129 void GLImageGLX::Destroy() { | 163 void GLImageGLX::Destroy() { |
| 130 if (glx_pixmap_) { | 164 if (glx_pixmap_) { |
| 131 glXDestroyGLXPixmap(display_, glx_pixmap_); | 165 glXDestroyGLXPixmap(gfx::GetXDisplay(), glx_pixmap_); |
| 132 glx_pixmap_ = 0; | 166 glx_pixmap_ = 0; |
| 133 } | 167 } |
| 134 if (pixmap_) { | |
| 135 XFreePixmap(display_, pixmap_); | |
| 136 pixmap_ = 0; | |
| 137 } | |
| 138 } | 168 } |
| 139 | 169 |
| 140 gfx::Size GLImageGLX::GetSize() { return size_; } | 170 gfx::Size GLImageGLX::GetSize() { return size_; } |
| 141 | 171 |
| 142 bool GLImageGLX::BindTexImage(unsigned target) { | 172 bool GLImageGLX::BindTexImage(unsigned target) { |
| 143 if (!glx_pixmap_) | 173 if (!glx_pixmap_) |
| 144 return false; | 174 return false; |
| 145 | 175 |
| 146 // Requires TEXTURE_2D target. | 176 // Requires TEXTURE_2D target. |
| 147 if (target != GL_TEXTURE_2D) | 177 if (target != GL_TEXTURE_2D) |
| 148 return false; | 178 return false; |
| 149 | 179 |
| 150 glXBindTexImageEXT(display_, glx_pixmap_, GLX_FRONT_LEFT_EXT, 0); | 180 glXBindTexImageEXT(gfx::GetXDisplay(), glx_pixmap_, GLX_FRONT_LEFT_EXT, 0); |
| 151 return true; | 181 return true; |
| 152 } | 182 } |
| 153 | 183 |
| 154 void GLImageGLX::ReleaseTexImage(unsigned target) { | 184 void GLImageGLX::ReleaseTexImage(unsigned target) { |
| 155 DCHECK(glx_pixmap_); | 185 DCHECK(glx_pixmap_); |
| 156 DCHECK_EQ(static_cast<GLenum>(GL_TEXTURE_2D), target); | 186 DCHECK_EQ(static_cast<GLenum>(GL_TEXTURE_2D), target); |
| 157 | 187 |
| 158 glXReleaseTexImageEXT(display_, glx_pixmap_, GLX_FRONT_LEFT_EXT); | 188 glXReleaseTexImageEXT(gfx::GetXDisplay(), glx_pixmap_, GLX_FRONT_LEFT_EXT); |
| 159 } | 189 } |
| 160 | 190 |
| 161 } // namespace gfx | 191 } // namespace gfx |
| OLD | NEW |