Fix WebsitePreference::compareTo.
[chromium-blink-merge.git] / ui / gl / gl_image_glx.cc
blob84c4d0c247e86e80abad99ee4d4eb13af7f92549
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 // 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); }
26 bool ValidFormat(unsigned internalformat) {
27 switch (internalformat) {
28 case GL_RGB:
29 case GL_RGBA:
30 return true;
31 default:
32 return false;
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;
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;
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;
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;
91 if (size)
92 *size = gfx::Size(width_return, height_return);
93 if (depth)
94 *depth = depth_return;
95 return true;
98 unsigned ActualPixmapDepth(XID pixmap) {
99 unsigned depth;
100 if (!ActualPixmapGeometry(pixmap, NULL, &depth))
101 return -1;
103 return depth;
106 gfx::Size ActualPixmapSize(XID pixmap) {
107 gfx::Size size;
108 if (!ActualPixmapGeometry(pixmap, &size, NULL))
109 return gfx::Size();
111 return size;
114 } // namespace anonymous
116 GLImageGLX::GLImageGLX(const gfx::Size& size, unsigned internalformat)
117 : glx_pixmap_(0), size_(size), internalformat_(internalformat) {
120 GLImageGLX::~GLImageGLX() {
121 DCHECK_EQ(0u, glx_pixmap_);
124 bool GLImageGLX::Initialize(XID pixmap) {
125 if (!GLSurfaceGLX::IsTextureFromPixmapSupported()) {
126 DVLOG(0) << "GLX_EXT_texture_from_pixmap not supported.";
127 return false;
130 if (!ValidFormat(internalformat_)) {
131 DVLOG(0) << "Invalid format: " << internalformat_;
132 return false;
135 DCHECK_EQ(PixmapDepth(internalformat_), ActualPixmapDepth(pixmap));
136 DCHECK_EQ(size_.ToString(), ActualPixmapSize(pixmap).ToString());
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,
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;
153 if (!num_elements) {
154 DVLOG(0) << "glXChooseFBConfig returned 0 elements.";
155 return false;
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;
168 return true;
171 void GLImageGLX::Destroy(bool have_context) {
172 if (glx_pixmap_) {
173 glXDestroyGLXPixmap(gfx::GetXDisplay(), glx_pixmap_);
174 glx_pixmap_ = 0;
178 gfx::Size GLImageGLX::GetSize() { return size_; }
180 bool GLImageGLX::BindTexImage(unsigned target) {
181 if (!glx_pixmap_)
182 return false;
184 // Requires TEXTURE_2D target.
185 if (target != GL_TEXTURE_2D)
186 return false;
188 glXBindTexImageEXT(gfx::GetXDisplay(), glx_pixmap_, GLX_FRONT_LEFT_EXT, 0);
189 return true;
192 void GLImageGLX::ReleaseTexImage(unsigned target) {
193 DCHECK_NE(0u, glx_pixmap_);
194 DCHECK_EQ(static_cast<GLenum>(GL_TEXTURE_2D), target);
196 glXReleaseTexImageEXT(gfx::GetXDisplay(), glx_pixmap_, GLX_FRONT_LEFT_EXT);
199 bool GLImageGLX::CopyTexImage(unsigned target) {
200 return false;
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;
211 } // namespace gfx