Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(714)

Side by Side Diff: ui/gl/gl_visual_picker_glx.cc

Issue 2347383002: X11: Use better visuals for OpenGL (Closed)
Patch Set: auto* Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « ui/gl/gl_visual_picker_glx.h ('k') | ui/views/test/views_test_base.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2016 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 #include "ui/gl/gl_visual_picker_glx.h"
6
7 #include <algorithm>
8 #include <numeric>
9 #include <vector>
10
11 #include "base/memory/singleton.h"
12 #include "ui/gfx/x/x11_types.h"
13 #include "ui/gl/gl_bindings.h"
14 #include "ui/gl/gl_context.h"
15 #include "ui/gl/gl_surface_glx.h"
16
17 namespace gl {
18
19 namespace {
20
21 bool IsArgbVisual(const XVisualInfo& visual) {
22 return visual.depth == 32 && visual.red_mask == 0xff0000 &&
23 visual.green_mask == 0x00ff00 && visual.blue_mask == 0x0000ff;
24 }
25
26 } // anonymous namespace
27
28 // static
29 GLVisualPickerGLX* GLVisualPickerGLX::GetInstance() {
30 return base::Singleton<GLVisualPickerGLX>::get();
31 }
32
33 XVisualInfo GLVisualPickerGLX::PickBestGlVisual(
34 const std::vector<XVisualInfo>& visuals,
35 bool want_alpha) const {
36 // Find the highest scoring visual and return it.
37 Visual* default_visual = DefaultVisual(display_, DefaultScreen(display_));
38 int highest_score = -1;
39 XVisualInfo best_visual{0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
40 for (const XVisualInfo& const_visual_info : visuals) {
41 int supports_gl, double_buffer, stereo, alpha_size, depth_size,
42 stencil_size, num_multisample, visual_caveat;
43 // glXGetConfig unfortunately doesn't use const.
44 XVisualInfo* visual_info = const_cast<XVisualInfo*>(&const_visual_info);
45 if (glXGetConfig(display_, visual_info, GLX_USE_GL, &supports_gl) ||
46 !supports_gl ||
47 glXGetConfig(display_, visual_info, GLX_DOUBLEBUFFER, &double_buffer) ||
48 !double_buffer ||
49 glXGetConfig(display_, visual_info, GLX_STEREO, &stereo) || stereo) {
50 continue;
51 }
52 if (has_glx_visual_rating_) {
53 if (glXGetConfig(display_, visual_info, GLX_VISUAL_CAVEAT_EXT,
54 &visual_caveat) ||
55 visual_caveat != GLX_NONE_EXT) {
56 continue;
57 }
58 }
59
60 // Give precedence to the root visual if it satisfies the basic requirements
61 // above. This can avoid an expensive copy-on-present.
62 if (const_visual_info.visual == default_visual)
63 return const_visual_info;
64
65 int score = 0;
66 if (!has_glx_multisample_ ||
67 (!glXGetConfig(display_, visual_info, GLX_SAMPLE_BUFFERS_ARB,
68 &num_multisample) &&
69 num_multisample == 0)) {
70 score++;
71 if (!glXGetConfig(display_, visual_info, GLX_DEPTH_SIZE, &depth_size) &&
72 depth_size == 0 &&
73 !glXGetConfig(display_, visual_info, GLX_STENCIL_SIZE,
74 &stencil_size) &&
75 stencil_size == 0) {
76 score++;
77 if (!glXGetConfig(display_, visual_info, GLX_ALPHA_SIZE, &alpha_size) &&
78 (alpha_size > 0) == want_alpha) {
79 score++;
80 }
81 }
82 }
83
84 if (score > highest_score) {
85 highest_score = score;
86 best_visual = const_visual_info;
87 }
88 }
89 return best_visual;
90 }
91
92 XVisualInfo GLVisualPickerGLX::PickBestSystemVisual(
93 const std::vector<XVisualInfo>& visuals) const {
94 Visual* default_visual = DefaultVisual(display_, DefaultScreen(display_));
95 auto it = std::find_if(visuals.begin(), visuals.end(),
96 [default_visual](const XVisualInfo& visual_info) {
97 return visual_info.visual == default_visual;
98 });
99 DCHECK(it != visuals.end());
100
101 const XVisualInfo& default_visual_info = *it;
102 std::vector<XVisualInfo> filtered_visuals;
103 std::copy_if(
104 visuals.begin(), visuals.end(), std::back_inserter(filtered_visuals),
105 [&default_visual_info](const XVisualInfo& visual_info) {
106 const XVisualInfo& v1 = visual_info;
107 const XVisualInfo& v2 = default_visual_info;
108 return v1.c_class == v2.c_class && v1.depth == v2.depth &&
109 v1.red_mask == v2.red_mask && v1.green_mask == v2.green_mask &&
110 v1.blue_mask == v2.blue_mask &&
111 v1.colormap_size == v2.colormap_size &&
112 v1.bits_per_rgb == v2.bits_per_rgb;
113 });
114 return PickBestGlVisual(filtered_visuals, IsArgbVisual(default_visual_info));
115 }
116
117 XVisualInfo GLVisualPickerGLX::PickBestRgbaVisual(
118 const std::vector<XVisualInfo>& visuals) const {
119 // Filter the visuals by the best class.
120 auto score = [](int c_class) {
121 // A higher score is more preferable.
122 switch (c_class) {
123 case TrueColor:
124 return 1;
125 case DirectColor:
126 return 0;
127 default:
128 return -1;
129 }
130 };
131 int best_class_score =
132 std::accumulate(visuals.begin(), visuals.end(), -1,
133 [&score](int acc, const XVisualInfo& visual_info) {
134 return std::max(acc, score(visual_info.c_class));
135 });
136 std::vector<XVisualInfo> filtered_visuals;
137 std::copy_if(visuals.begin(), visuals.end(),
138 std::back_inserter(filtered_visuals),
139 [best_class_score, &score](const XVisualInfo& visual_info) {
140 if (!IsArgbVisual(visual_info))
141 return false;
142 return score(visual_info.c_class) == best_class_score;
143 });
144 return PickBestGlVisual(filtered_visuals, true);
145 }
146
147 GLVisualPickerGLX::GLVisualPickerGLX() : display_(gfx::GetXDisplay()) {
148 has_glx_visual_rating_ =
149 GLSurfaceGLX::HasGLXExtension("GLX_EXT_visual_rating");
150 has_glx_multisample_ = GLSurfaceGLX::HasGLXExtension("GLX_EXT_multisample");
151
152 XVisualInfo visual_template;
153 visual_template.screen = DefaultScreen(display_);
154
155 // Get all of the visuals for the default screen.
156 int n_visuals;
157 gfx::XScopedPtr<XVisualInfo[]> x_visuals(
158 XGetVisualInfo(display_, VisualScreenMask, &visual_template, &n_visuals));
159 std::vector<XVisualInfo> visuals;
160 for (int i = 0; i < n_visuals; i++)
161 visuals.push_back(x_visuals[i]);
162
163 system_visual_ = PickBestSystemVisual(visuals);
164 rgba_visual_ = PickBestRgbaVisual(visuals);
165 }
166
167 GLVisualPickerGLX::~GLVisualPickerGLX() {}
168
169 } // namespace gl
OLDNEW
« no previous file with comments | « ui/gl/gl_visual_picker_glx.h ('k') | ui/views/test/views_test_base.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698