Roll src/third_party/WebKit 37b70da:e3be828 (svn 200645:200646)
[chromium-blink-merge.git] / ui / gl / gl_image_glx.cc
blobf22ba0ff62293435e5b528b45a6bc422e8cc915e
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.
5 extern "C" {
6 #include <X11/Xlib.h>
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"
15 namespace gfx {
17 namespace {
19 bool ValidFormat(unsigned internalformat) {
20 switch (internalformat) {
21 case GL_RGB:
22 case GL_RGBA:
23 return true;
24 default:
25 return false;
29 int TextureFormat(unsigned internalformat) {
30 switch (internalformat) {
31 case GL_RGB:
32 return GLX_TEXTURE_FORMAT_RGB_EXT;
33 case GL_RGBA:
34 return GLX_TEXTURE_FORMAT_RGBA_EXT;
35 default:
36 NOTREACHED();
37 return 0;
41 int BindToTextureFormat(unsigned internalformat) {
42 switch (internalformat) {
43 case GL_RGB:
44 return GLX_BIND_TO_TEXTURE_RGB_EXT;
45 case GL_RGBA:
46 return GLX_BIND_TO_TEXTURE_RGBA_EXT;
47 default:
48 NOTREACHED();
49 return 0;
53 unsigned PixmapDepth(unsigned internalformat) {
54 switch (internalformat) {
55 case GL_RGBA:
56 return 32u;
57 case GL_RGB:
58 return 24u;
59 default:
60 NOTREACHED();
61 return 0u;
65 bool ActualPixmapGeometry(XID pixmap, gfx::Size* size, unsigned* depth) {
66 XID root_return;
67 int x_return;
68 int y_return;
69 unsigned width_return;
70 unsigned height_return;
71 unsigned border_width_return;
72 unsigned depth_return;
73 if (!XGetGeometry(gfx::GetXDisplay(),
74 pixmap,
75 &root_return,
76 &x_return,
77 &y_return,
78 &width_return,
79 &height_return,
80 &border_width_return,
81 &depth_return))
82 return false;
84 if (size)
85 *size = gfx::Size(width_return, height_return);
86 if (depth)
87 *depth = depth_return;
88 return true;
91 unsigned ActualPixmapDepth(XID pixmap) {
92 unsigned depth;
93 if (!ActualPixmapGeometry(pixmap, NULL, &depth))
94 return -1;
96 return depth;
99 gfx::Size ActualPixmapSize(XID pixmap) {
100 gfx::Size size;
101 if (!ActualPixmapGeometry(pixmap, &size, NULL))
102 return gfx::Size();
104 return size;
107 } // namespace anonymous
109 GLImageGLX::GLImageGLX(const gfx::Size& size, unsigned internalformat)
110 : glx_pixmap_(0), size_(size), internalformat_(internalformat) {
113 GLImageGLX::~GLImageGLX() {
114 DCHECK_EQ(0u, glx_pixmap_);
117 bool GLImageGLX::Initialize(XID pixmap) {
118 if (!GLSurfaceGLX::IsTextureFromPixmapSupported()) {
119 DVLOG(0) << "GLX_EXT_texture_from_pixmap not supported.";
120 return false;
123 if (!ValidFormat(internalformat_)) {
124 DVLOG(0) << "Invalid format: " << internalformat_;
125 return false;
128 DCHECK_EQ(PixmapDepth(internalformat_), ActualPixmapDepth(pixmap));
129 DCHECK_EQ(size_.ToString(), ActualPixmapSize(pixmap).ToString());
131 int config_attribs[] = {
132 GLX_DRAWABLE_TYPE, GLX_PIXMAP_BIT,
133 GLX_BIND_TO_TEXTURE_TARGETS_EXT, GLX_TEXTURE_2D_EXT,
134 BindToTextureFormat(internalformat_), GL_TRUE,
136 int num_elements = 0;
137 gfx::XScopedPtr<GLXFBConfig> config(
138 glXChooseFBConfig(gfx::GetXDisplay(), DefaultScreen(gfx::GetXDisplay()),
139 config_attribs, &num_elements));
140 if (!config.get()) {
141 DVLOG(0) << "glXChooseFBConfig failed.";
142 return false;
144 if (!num_elements) {
145 DVLOG(0) << "glXChooseFBConfig returned 0 elements.";
146 return false;
149 int pixmap_attribs[] = {GLX_TEXTURE_TARGET_EXT, GLX_TEXTURE_2D_EXT,
150 GLX_TEXTURE_FORMAT_EXT,
151 TextureFormat(internalformat_), 0};
152 glx_pixmap_ = glXCreatePixmap(
153 gfx::GetXDisplay(), *config.get(), pixmap, pixmap_attribs);
154 if (!glx_pixmap_) {
155 DVLOG(0) << "glXCreatePixmap failed.";
156 return false;
159 return true;
162 void GLImageGLX::Destroy(bool have_context) {
163 if (glx_pixmap_) {
164 glXDestroyGLXPixmap(gfx::GetXDisplay(), glx_pixmap_);
165 glx_pixmap_ = 0;
169 gfx::Size GLImageGLX::GetSize() { return size_; }
171 unsigned GLImageGLX::GetInternalFormat() { return internalformat_; }
173 bool GLImageGLX::BindTexImage(unsigned target) {
174 if (!glx_pixmap_)
175 return false;
177 // Requires TEXTURE_2D target.
178 if (target != GL_TEXTURE_2D)
179 return false;
181 glXBindTexImageEXT(gfx::GetXDisplay(), glx_pixmap_, GLX_FRONT_LEFT_EXT, 0);
182 return true;
185 void GLImageGLX::ReleaseTexImage(unsigned target) {
186 DCHECK_NE(0u, glx_pixmap_);
187 DCHECK_EQ(static_cast<GLenum>(GL_TEXTURE_2D), target);
189 glXReleaseTexImageEXT(gfx::GetXDisplay(), glx_pixmap_, GLX_FRONT_LEFT_EXT);
192 bool GLImageGLX::CopyTexSubImage(unsigned target,
193 const Point& offset,
194 const Rect& rect) {
195 return false;
198 bool GLImageGLX::ScheduleOverlayPlane(gfx::AcceleratedWidget widget,
199 int z_order,
200 OverlayTransform transform,
201 const Rect& bounds_rect,
202 const RectF& crop_rect) {
203 return false;
206 } // namespace gfx