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(gfx::Size size, unsigned internalformat) |
47 : display_(gfx::GetXDisplay()), | 111 : glx_pixmap_(0), size_(size), internalformat_(internalformat) {} |
48 window_(window), | |
49 pixmap_(0), | |
50 glx_pixmap_(0) {} | |
51 | 112 |
52 GLImageGLX::~GLImageGLX() { Destroy(); } | 113 GLImageGLX::~GLImageGLX() { Destroy(); } |
53 | 114 |
54 bool GLImageGLX::Initialize() { | 115 bool GLImageGLX::Initialize(gfx::GpuMemoryBufferHandle buffer) { |
55 if (!GLSurfaceGLX::IsTextureFromPixmapSupported()) { | 116 if (!GLSurfaceGLX::IsTextureFromPixmapSupported()) { |
56 LOG(ERROR) << "GLX_EXT_texture_from_pixmap not supported."; | 117 DVLOG(0) << "GLX_EXT_texture_from_pixmap not supported."; |
57 return false; | 118 return false; |
58 } | 119 } |
59 | 120 |
60 XWindowAttributes attributes; | 121 if (!ValidFormat(internalformat_)) { |
61 if (!XGetWindowAttributes(display_, window_, &attributes)) { | 122 DVLOG(0) << "Invalid format: " << internalformat_; |
62 LOG(ERROR) << "XGetWindowAttributes failed for window " << window_ << "."; | |
63 return false; | 123 return false; |
64 } | 124 } |
65 | 125 |
66 XVisualInfo templ; | 126 DCHECK_EQ(PixmapDepth(internalformat_), ActualPixmapDepth(buffer.pixmap)); |
67 templ.visualid = XVisualIDFromVisual(attributes.visual); | 127 DCHECK_EQ(size_.ToString(), ActualPixmapSize(buffer.pixmap).ToString()); |
piman
2014/06/13 01:31:27
DCHECKs, or return failure instead?
reveman
2014/06/13 16:45:11
I'm using DCHECKs to avoid unnecessary round-trips
piman
2014/06/13 17:14:21
Ok.
| |
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 | 128 |
81 int config_attribs[] = { | 129 int config_attribs[] = { |
82 static_cast<int>(GLX_VISUAL_ID), static_cast<int>(visinfo->visualid), | 130 GLX_DRAWABLE_TYPE, GLX_PIXMAP_BIT, |
83 GLX_DRAWABLE_TYPE, GLX_PIXMAP_BIT, | 131 GLX_BIND_TO_TEXTURE_TARGETS_EXT, GLX_TEXTURE_2D_EXT, |
84 GLX_BIND_TO_TEXTURE_TARGETS_EXT, GLX_TEXTURE_2D_EXT, | 132 BindToTextureFormat(internalformat_), GL_TRUE, |
85 BindToTextureFormat(visinfo->depth), GL_TRUE, | |
86 0}; | 133 0}; |
87 int num_elements = 0; | 134 int num_elements = 0; |
88 scoped_ptr<GLXFBConfig, ScopedPtrXFree> config(glXChooseFBConfig( | 135 scoped_ptr<GLXFBConfig, ScopedPtrXFree> config( |
89 display_, DefaultScreen(display_), config_attribs, &num_elements)); | 136 glXChooseFBConfig(gfx::GetXDisplay(), |
137 DefaultScreen(gfx::GetXDisplay()), | |
138 config_attribs, | |
139 &num_elements)); | |
90 if (!config.get()) { | 140 if (!config.get()) { |
91 LOG(ERROR) << "glXChooseFBConfig failed."; | 141 DVLOG(0) << "glXChooseFBConfig failed."; |
92 return false; | 142 return false; |
93 } | 143 } |
94 if (!num_elements) { | 144 if (!num_elements) { |
95 LOG(ERROR) << "glXChooseFBConfig returned 0 elements."; | 145 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; | 146 return false; |
113 } | 147 } |
114 | 148 |
115 int pixmap_attribs[] = {GLX_TEXTURE_TARGET_EXT, GLX_TEXTURE_2D_EXT, | 149 int pixmap_attribs[] = {GLX_TEXTURE_TARGET_EXT, GLX_TEXTURE_2D_EXT, |
116 GLX_TEXTURE_FORMAT_EXT, TextureFormat(visinfo->depth), | 150 GLX_TEXTURE_FORMAT_EXT, |
117 0}; | 151 TextureFormat(internalformat_), 0}; |
118 glx_pixmap_ = | 152 glx_pixmap_ = glXCreatePixmap( |
119 glXCreatePixmap(display_, *config.get(), pixmap_, pixmap_attribs); | 153 gfx::GetXDisplay(), *config.get(), buffer.pixmap, pixmap_attribs); |
120 if (!glx_pixmap_) { | 154 if (!glx_pixmap_) { |
121 LOG(ERROR) << "glXCreatePixmap failed."; | 155 DVLOG(0) << "glXCreatePixmap failed."; |
122 return false; | 156 return false; |
123 } | 157 } |
124 | 158 |
125 size_ = gfx::Size(width, height); | |
126 return true; | 159 return true; |
127 } | 160 } |
128 | 161 |
129 void GLImageGLX::Destroy() { | 162 void GLImageGLX::Destroy() { |
130 if (glx_pixmap_) { | 163 if (glx_pixmap_) { |
131 glXDestroyGLXPixmap(display_, glx_pixmap_); | 164 glXDestroyGLXPixmap(gfx::GetXDisplay(), glx_pixmap_); |
132 glx_pixmap_ = 0; | 165 glx_pixmap_ = 0; |
133 } | 166 } |
134 if (pixmap_) { | |
135 XFreePixmap(display_, pixmap_); | |
136 pixmap_ = 0; | |
137 } | |
138 } | 167 } |
139 | 168 |
140 gfx::Size GLImageGLX::GetSize() { return size_; } | 169 gfx::Size GLImageGLX::GetSize() { return size_; } |
141 | 170 |
142 bool GLImageGLX::BindTexImage(unsigned target) { | 171 bool GLImageGLX::BindTexImage(unsigned target) { |
143 if (!glx_pixmap_) | 172 if (!glx_pixmap_) |
144 return false; | 173 return false; |
145 | 174 |
146 // Requires TEXTURE_2D target. | 175 // Requires TEXTURE_2D target. |
147 if (target != GL_TEXTURE_2D) | 176 if (target != GL_TEXTURE_2D) |
148 return false; | 177 return false; |
149 | 178 |
150 glXBindTexImageEXT(display_, glx_pixmap_, GLX_FRONT_LEFT_EXT, 0); | 179 glXBindTexImageEXT(gfx::GetXDisplay(), glx_pixmap_, GLX_FRONT_LEFT_EXT, 0); |
151 return true; | 180 return true; |
152 } | 181 } |
153 | 182 |
154 void GLImageGLX::ReleaseTexImage(unsigned target) { | 183 void GLImageGLX::ReleaseTexImage(unsigned target) { |
155 DCHECK(glx_pixmap_); | 184 DCHECK(glx_pixmap_); |
156 DCHECK_EQ(static_cast<GLenum>(GL_TEXTURE_2D), target); | 185 DCHECK_EQ(static_cast<GLenum>(GL_TEXTURE_2D), target); |
157 | 186 |
158 glXReleaseTexImageEXT(display_, glx_pixmap_, GLX_FRONT_LEFT_EXT); | 187 glXReleaseTexImageEXT(gfx::GetXDisplay(), glx_pixmap_, GLX_FRONT_LEFT_EXT); |
159 } | 188 } |
160 | 189 |
161 } // namespace gfx | 190 } // namespace gfx |
OLD | NEW |