• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4 
5 #include "base/memory/scoped_ptr.h"
6 #include "gpu/config/gpu_info.h"
7 #include "gpu/config/gpu_info_collector.h"
8 #include "testing/gmock/include/gmock/gmock.h"
9 #include "testing/gtest/include/gtest/gtest.h"
10 #include "ui/gl/gl_implementation.h"
11 #include "ui/gl/gl_mock.h"
12 
13 using ::gfx::MockGLInterface;
14 using ::testing::Return;
15 
16 namespace gpu {
17 
18 class GPUInfoCollectorTest : public testing::Test {
19  public:
GPUInfoCollectorTest()20   GPUInfoCollectorTest() {}
~GPUInfoCollectorTest()21   virtual ~GPUInfoCollectorTest() { }
22 
SetUp()23   virtual void SetUp() {
24     // TODO(kbr): make this setup robust in the case where
25     // GLSurface::InitializeOneOff() has already been called by
26     // another unit test. http://crbug.com/100285
27     gfx::InitializeGLBindings(gfx::kGLImplementationMockGL);
28     gl_.reset(new ::testing::StrictMock< ::gfx::MockGLInterface>());
29     ::gfx::GLInterface::SetGLInterface(gl_.get());
30 #if defined(OS_WIN)
31     const uint32 vendor_id = 0x10de;
32     const uint32 device_id = 0x0658;
33     const char* driver_vendor = "";  // not implemented
34     const char* driver_version = "";
35     const char* shader_version = "1.40";
36     const char* gl_version = "3.1";
37     const char* gl_renderer = "Quadro FX 380/PCI/SSE2";
38     const char* gl_vendor = "NVIDIA Corporation";
39     const char* gl_version_string = "3.1.0";
40     const char* gl_shading_language_version = "1.40 NVIDIA via Cg compiler";
41     const char* gl_extensions =
42         "GL_OES_packed_depth_stencil GL_EXT_texture_format_BGRA8888 "
43         "GL_EXT_read_format_bgra";
44 #elif defined(OS_MACOSX)
45     const uint32 vendor_id = 0x10de;
46     const uint32 device_id = 0x0640;
47     const char* driver_vendor = "";  // not implemented
48     const char* driver_version = "1.6.18";
49     const char* shader_version = "1.20";
50     const char* gl_version = "2.1";
51     const char* gl_renderer = "NVIDIA GeForce GT 120 OpenGL Engine";
52     const char* gl_vendor = "NVIDIA Corporation";
53     const char* gl_version_string = "2.1 NVIDIA-1.6.18";
54     const char* gl_shading_language_version = "1.20 ";
55     const char* gl_extensions =
56         "GL_OES_packed_depth_stencil GL_EXT_texture_format_BGRA8888 "
57         "GL_EXT_read_format_bgra";
58 #else  // defined (OS_LINUX)
59     const uint32 vendor_id = 0x10de;
60     const uint32 device_id = 0x0658;
61     const char* driver_vendor = "NVIDIA";
62     const char* driver_version = "195.36.24";
63     const char* shader_version = "1.50";
64     const char* gl_version = "3.2";
65     const char* gl_renderer = "Quadro FX 380/PCI/SSE2";
66     const char* gl_vendor = "NVIDIA Corporation";
67     const char* gl_version_string = "3.2.0 NVIDIA 195.36.24";
68     const char* gl_shading_language_version = "1.50 NVIDIA via Cg compiler";
69     const char* gl_extensions =
70         "GL_OES_packed_depth_stencil GL_EXT_texture_format_BGRA8888 "
71         "GL_EXT_read_format_bgra";
72 #endif
73     test_values_.gpu.vendor_id = vendor_id;
74     test_values_.gpu.device_id = device_id;
75     test_values_.driver_vendor = driver_vendor;
76     test_values_.driver_version =driver_version;
77     test_values_.pixel_shader_version = shader_version;
78     test_values_.vertex_shader_version = shader_version;
79     test_values_.gl_version = gl_version;
80     test_values_.gl_renderer = gl_renderer;
81     test_values_.gl_vendor = gl_vendor;
82     test_values_.gl_version_string = gl_version_string;
83     test_values_.gl_extensions = gl_extensions;
84     test_values_.can_lose_context = false;
85 
86     EXPECT_CALL(*gl_, GetString(GL_EXTENSIONS))
87         .WillRepeatedly(Return(reinterpret_cast<const GLubyte*>(
88             gl_extensions)));
89     EXPECT_CALL(*gl_, GetString(GL_SHADING_LANGUAGE_VERSION))
90         .WillRepeatedly(Return(reinterpret_cast<const GLubyte*>(
91             gl_shading_language_version)));
92     EXPECT_CALL(*gl_, GetString(GL_VERSION))
93         .WillRepeatedly(Return(reinterpret_cast<const GLubyte*>(
94             gl_version_string)));
95     EXPECT_CALL(*gl_, GetString(GL_VENDOR))
96         .WillRepeatedly(Return(reinterpret_cast<const GLubyte*>(
97             gl_vendor)));
98     EXPECT_CALL(*gl_, GetString(GL_RENDERER))
99         .WillRepeatedly(Return(reinterpret_cast<const GLubyte*>(
100             gl_renderer)));
101   }
102 
TearDown()103   virtual void TearDown() {
104     ::gfx::GLInterface::SetGLInterface(NULL);
105     gl_.reset();
106   }
107 
108  public:
109   // Use StrictMock to make 100% sure we know how GL will be called.
110   scoped_ptr< ::testing::StrictMock< ::gfx::MockGLInterface> > gl_;
111   GPUInfo test_values_;
112 };
113 
114 // TODO(rlp): Test the vendor and device id collection if deemed necessary as
115 //            it involves several complicated mocks for each platform.
116 
117 // TODO(kbr): re-enable these tests; see http://crbug.com/100285 .
118 
TEST_F(GPUInfoCollectorTest,DISABLED_DriverVendorGL)119 TEST_F(GPUInfoCollectorTest, DISABLED_DriverVendorGL) {
120   GPUInfo gpu_info;
121   CollectGraphicsInfoGL(&gpu_info);
122   EXPECT_EQ(test_values_.driver_vendor,
123             gpu_info.driver_vendor);
124 }
125 
126 // Skip Windows because the driver version is obtained from bot registry.
127 #if !defined(OS_WIN)
TEST_F(GPUInfoCollectorTest,DISABLED_DriverVersionGL)128 TEST_F(GPUInfoCollectorTest, DISABLED_DriverVersionGL) {
129   GPUInfo gpu_info;
130   CollectGraphicsInfoGL(&gpu_info);
131   EXPECT_EQ(test_values_.driver_version,
132             gpu_info.driver_version);
133 }
134 #endif
135 
TEST_F(GPUInfoCollectorTest,DISABLED_PixelShaderVersionGL)136 TEST_F(GPUInfoCollectorTest, DISABLED_PixelShaderVersionGL) {
137   GPUInfo gpu_info;
138   CollectGraphicsInfoGL(&gpu_info);
139   EXPECT_EQ(test_values_.pixel_shader_version,
140             gpu_info.pixel_shader_version);
141 }
142 
TEST_F(GPUInfoCollectorTest,DISABLED_VertexShaderVersionGL)143 TEST_F(GPUInfoCollectorTest, DISABLED_VertexShaderVersionGL) {
144   GPUInfo gpu_info;
145   CollectGraphicsInfoGL(&gpu_info);
146   EXPECT_EQ(test_values_.vertex_shader_version,
147             gpu_info.vertex_shader_version);
148 }
149 
TEST_F(GPUInfoCollectorTest,DISABLED_GLVersionGL)150 TEST_F(GPUInfoCollectorTest, DISABLED_GLVersionGL) {
151   GPUInfo gpu_info;
152   CollectGraphicsInfoGL(&gpu_info);
153   EXPECT_EQ(test_values_.gl_version,
154             gpu_info.gl_version);
155 }
156 
TEST_F(GPUInfoCollectorTest,DISABLED_GLVersionStringGL)157 TEST_F(GPUInfoCollectorTest, DISABLED_GLVersionStringGL) {
158   GPUInfo gpu_info;
159   CollectGraphicsInfoGL(&gpu_info);
160   EXPECT_EQ(test_values_.gl_version_string,
161             gpu_info.gl_version_string);
162 }
163 
TEST_F(GPUInfoCollectorTest,DISABLED_GLRendererGL)164 TEST_F(GPUInfoCollectorTest, DISABLED_GLRendererGL) {
165   GPUInfo gpu_info;
166   CollectGraphicsInfoGL(&gpu_info);
167   EXPECT_EQ(test_values_.gl_renderer,
168             gpu_info.gl_renderer);
169 }
170 
TEST_F(GPUInfoCollectorTest,DISABLED_GLVendorGL)171 TEST_F(GPUInfoCollectorTest, DISABLED_GLVendorGL) {
172   GPUInfo gpu_info;
173   CollectGraphicsInfoGL(&gpu_info);
174   EXPECT_EQ(test_values_.gl_vendor,
175             gpu_info.gl_vendor);
176 }
177 
TEST_F(GPUInfoCollectorTest,DISABLED_GLExtensionsGL)178 TEST_F(GPUInfoCollectorTest, DISABLED_GLExtensionsGL) {
179   GPUInfo gpu_info;
180   CollectGraphicsInfoGL(&gpu_info);
181   EXPECT_EQ(test_values_.gl_extensions,
182             gpu_info.gl_extensions);
183 }
184 
185 #if defined(OS_LINUX)
TEST(GPUInfoCollectorUtilTest,DetermineActiveGPU)186 TEST(GPUInfoCollectorUtilTest, DetermineActiveGPU) {
187   const uint32 kIntelVendorID = 0x8086;
188   const uint32 kIntelDeviceID = 0x0046;
189   GPUInfo::GPUDevice intel_gpu;
190   intel_gpu.vendor_id = kIntelVendorID;
191   intel_gpu.device_id = kIntelDeviceID;
192 
193   const uint32 kAMDVendorID = 0x1002;
194   const uint32 kAMDDeviceID = 0x68c1;
195   GPUInfo::GPUDevice amd_gpu;
196   amd_gpu.vendor_id = kAMDVendorID;
197   amd_gpu.device_id = kAMDDeviceID;
198 
199   // One GPU, do nothing.
200   {
201     GPUInfo gpu_info;
202     gpu_info.gpu = amd_gpu;
203     EXPECT_TRUE(DetermineActiveGPU(&gpu_info));
204   }
205 
206   // Two GPUs, switched.
207   {
208     GPUInfo gpu_info;
209     gpu_info.gpu = amd_gpu;
210     gpu_info.secondary_gpus.push_back(intel_gpu);
211     gpu_info.gl_vendor = "Intel Open Source Technology Center";
212     EXPECT_TRUE(DetermineActiveGPU(&gpu_info));
213     EXPECT_EQ(kIntelVendorID, gpu_info.gpu.vendor_id);
214     EXPECT_EQ(kIntelDeviceID, gpu_info.gpu.device_id);
215     EXPECT_EQ(kAMDVendorID, gpu_info.secondary_gpus[0].vendor_id);
216     EXPECT_EQ(kAMDDeviceID, gpu_info.secondary_gpus[0].device_id);
217   }
218 
219   // Two GPUs, no switch necessary.
220   {
221     GPUInfo gpu_info;
222     gpu_info.gpu = intel_gpu;
223     gpu_info.secondary_gpus.push_back(amd_gpu);
224     gpu_info.gl_vendor = "Intel Open Source Technology Center";
225     EXPECT_TRUE(DetermineActiveGPU(&gpu_info));
226     EXPECT_EQ(kIntelVendorID, gpu_info.gpu.vendor_id);
227     EXPECT_EQ(kIntelDeviceID, gpu_info.gpu.device_id);
228     EXPECT_EQ(kAMDVendorID, gpu_info.secondary_gpus[0].vendor_id);
229     EXPECT_EQ(kAMDDeviceID, gpu_info.secondary_gpus[0].device_id);
230   }
231 
232   // Two GPUs, empty GL_VENDOR string.
233   {
234     GPUInfo gpu_info;
235     gpu_info.gpu = intel_gpu;
236     gpu_info.secondary_gpus.push_back(amd_gpu);
237     EXPECT_FALSE(DetermineActiveGPU(&gpu_info));
238   }
239 
240   // Two GPUs, unhandled GL_VENDOR string.
241   {
242     GPUInfo gpu_info;
243     gpu_info.gpu = intel_gpu;
244     gpu_info.secondary_gpus.push_back(amd_gpu);
245     gpu_info.gl_vendor = "nouveau";
246     EXPECT_FALSE(DetermineActiveGPU(&gpu_info));
247   }
248 }
249 #endif
250 
251 }  // namespace gpu
252 
253