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