OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 extern "C" { |
| 6 #include <X11/Xlib.h> |
| 7 } |
| 8 |
| 9 #include "base/logging.h" |
| 10 #include "base/memory/scoped_ptr.h" |
| 11 #include "ui/gl/gl_bindings.h" |
| 12 #include "ui/gl/gl_image_glx.h" |
| 13 #include "ui/gl/gl_surface_glx.h" |
| 14 |
| 15 namespace gfx { |
| 16 |
| 17 namespace { |
| 18 |
| 19 // scoped_ptr functor for XFree(). Use as follows: |
| 20 // scoped_ptr<XVisualInfo, ScopedPtrXFree> foo(...); |
| 21 // where "XVisualInfo" is any X type that is freed with XFree. |
| 22 struct ScopedPtrXFree { |
| 23 void operator()(void* x) const { ::XFree(x); } |
| 24 }; |
| 25 |
| 26 bool ValidFormat(unsigned internalformat) { |
| 27 switch (internalformat) { |
| 28 case GL_RGB: |
| 29 case GL_RGBA: |
| 30 return true; |
| 31 default: |
| 32 return false; |
| 33 } |
| 34 } |
| 35 |
| 36 int TextureFormat(unsigned internalformat) { |
| 37 switch (internalformat) { |
| 38 case GL_RGB: |
| 39 return GLX_TEXTURE_FORMAT_RGB_EXT; |
| 40 case GL_RGBA: |
| 41 return GLX_TEXTURE_FORMAT_RGBA_EXT; |
| 42 default: |
| 43 NOTREACHED(); |
| 44 return 0; |
| 45 } |
| 46 } |
| 47 |
| 48 int BindToTextureFormat(unsigned internalformat) { |
| 49 switch (internalformat) { |
| 50 case GL_RGB: |
| 51 return GLX_BIND_TO_TEXTURE_RGB_EXT; |
| 52 case GL_RGBA: |
| 53 return GLX_BIND_TO_TEXTURE_RGBA_EXT; |
| 54 default: |
| 55 NOTREACHED(); |
| 56 return 0; |
| 57 } |
| 58 } |
| 59 |
| 60 unsigned PixmapDepth(unsigned internalformat) { |
| 61 switch (internalformat) { |
| 62 case GL_RGBA: |
| 63 return 32u; |
| 64 case GL_RGB: |
| 65 return 24u; |
| 66 default: |
| 67 NOTREACHED(); |
| 68 return 0u; |
| 69 } |
| 70 } |
| 71 |
| 72 bool ActualPixmapGeometry(XID pixmap, gfx::Size* size, unsigned* depth) { |
| 73 XID root_return; |
| 74 int x_return; |
| 75 int y_return; |
| 76 unsigned width_return; |
| 77 unsigned height_return; |
| 78 unsigned border_width_return; |
| 79 unsigned depth_return; |
| 80 if (!XGetGeometry(gfx::GetXDisplay(), |
| 81 pixmap, |
| 82 &root_return, |
| 83 &x_return, |
| 84 &y_return, |
| 85 &width_return, |
| 86 &height_return, |
| 87 &border_width_return, |
| 88 &depth_return)) |
| 89 return false; |
| 90 |
| 91 if (size) |
| 92 *size = gfx::Size(width_return, height_return); |
| 93 if (depth) |
| 94 *depth = depth_return; |
| 95 return true; |
| 96 } |
| 97 |
| 98 unsigned ActualPixmapDepth(XID pixmap) { |
| 99 unsigned depth; |
| 100 if (!ActualPixmapGeometry(pixmap, NULL, &depth)) |
| 101 return -1; |
| 102 |
| 103 return depth; |
| 104 } |
| 105 |
| 106 gfx::Size ActualPixmapSize(XID pixmap) { |
| 107 gfx::Size size; |
| 108 if (!ActualPixmapGeometry(pixmap, &size, NULL)) |
| 109 return gfx::Size(); |
| 110 |
| 111 return size; |
| 112 } |
| 113 |
| 114 } // namespace anonymous |
| 115 |
| 116 GLImageGLX::GLImageGLX(const gfx::Size& size, unsigned internalformat) |
| 117 : glx_pixmap_(0), size_(size), internalformat_(internalformat) { |
| 118 } |
| 119 |
| 120 GLImageGLX::~GLImageGLX() { |
| 121 DCHECK_EQ(0u, glx_pixmap_); |
| 122 } |
| 123 |
| 124 bool GLImageGLX::Initialize(XID pixmap) { |
| 125 if (!GLSurfaceGLX::IsTextureFromPixmapSupported()) { |
| 126 DVLOG(0) << "GLX_EXT_texture_from_pixmap not supported."; |
| 127 return false; |
| 128 } |
| 129 |
| 130 if (!ValidFormat(internalformat_)) { |
| 131 DVLOG(0) << "Invalid format: " << internalformat_; |
| 132 return false; |
| 133 } |
| 134 |
| 135 DCHECK_EQ(PixmapDepth(internalformat_), ActualPixmapDepth(pixmap)); |
| 136 DCHECK_EQ(size_.ToString(), ActualPixmapSize(pixmap).ToString()); |
| 137 |
| 138 int config_attribs[] = { |
| 139 GLX_DRAWABLE_TYPE, GLX_PIXMAP_BIT, |
| 140 GLX_BIND_TO_TEXTURE_TARGETS_EXT, GLX_TEXTURE_2D_EXT, |
| 141 BindToTextureFormat(internalformat_), GL_TRUE, |
| 142 0}; |
| 143 int num_elements = 0; |
| 144 scoped_ptr<GLXFBConfig, ScopedPtrXFree> config( |
| 145 glXChooseFBConfig(gfx::GetXDisplay(), |
| 146 DefaultScreen(gfx::GetXDisplay()), |
| 147 config_attribs, |
| 148 &num_elements)); |
| 149 if (!config.get()) { |
| 150 DVLOG(0) << "glXChooseFBConfig failed."; |
| 151 return false; |
| 152 } |
| 153 if (!num_elements) { |
| 154 DVLOG(0) << "glXChooseFBConfig returned 0 elements."; |
| 155 return false; |
| 156 } |
| 157 |
| 158 int pixmap_attribs[] = {GLX_TEXTURE_TARGET_EXT, GLX_TEXTURE_2D_EXT, |
| 159 GLX_TEXTURE_FORMAT_EXT, |
| 160 TextureFormat(internalformat_), 0}; |
| 161 glx_pixmap_ = glXCreatePixmap( |
| 162 gfx::GetXDisplay(), *config.get(), pixmap, pixmap_attribs); |
| 163 if (!glx_pixmap_) { |
| 164 DVLOG(0) << "glXCreatePixmap failed."; |
| 165 return false; |
| 166 } |
| 167 |
| 168 return true; |
| 169 } |
| 170 |
| 171 void GLImageGLX::Destroy(bool have_context) { |
| 172 if (glx_pixmap_) { |
| 173 glXDestroyGLXPixmap(gfx::GetXDisplay(), glx_pixmap_); |
| 174 glx_pixmap_ = 0; |
| 175 } |
| 176 } |
| 177 |
| 178 gfx::Size GLImageGLX::GetSize() { return size_; } |
| 179 |
| 180 bool GLImageGLX::BindTexImage(unsigned target) { |
| 181 if (!glx_pixmap_) |
| 182 return false; |
| 183 |
| 184 // Requires TEXTURE_2D target. |
| 185 if (target != GL_TEXTURE_2D) |
| 186 return false; |
| 187 |
| 188 glXBindTexImageEXT(gfx::GetXDisplay(), glx_pixmap_, GLX_FRONT_LEFT_EXT, 0); |
| 189 return true; |
| 190 } |
| 191 |
| 192 void GLImageGLX::ReleaseTexImage(unsigned target) { |
| 193 DCHECK_NE(0u, glx_pixmap_); |
| 194 DCHECK_EQ(static_cast<GLenum>(GL_TEXTURE_2D), target); |
| 195 |
| 196 glXReleaseTexImageEXT(gfx::GetXDisplay(), glx_pixmap_, GLX_FRONT_LEFT_EXT); |
| 197 } |
| 198 |
| 199 bool GLImageGLX::CopyTexImage(unsigned target) { |
| 200 return false; |
| 201 } |
| 202 |
| 203 bool GLImageGLX::ScheduleOverlayPlane(gfx::AcceleratedWidget widget, |
| 204 int z_order, |
| 205 OverlayTransform transform, |
| 206 const Rect& bounds_rect, |
| 207 const RectF& crop_rect) { |
| 208 return false; |
| 209 } |
| 210 |
| 211 } // namespace gfx |
OLD | NEW |