OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2006-2010 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 "chrome/gpu/gpu_info_collector.h" |
| 6 |
| 7 #include <string> |
| 8 #include <vector> |
| 9 |
| 10 #include "app/gfx/gl/gl_bindings.h" |
| 11 #include "app/gfx/gl/gl_context.h" |
| 12 #include "base/logging.h" |
| 13 #include "base/string_number_conversions.h" |
| 14 #include "base/string_piece.h" |
| 15 #include "base/string_split.h" |
| 16 |
| 17 namespace { |
| 18 |
| 19 // This creates an offscreen GL context for gl queries. Returned GLContext |
| 20 // should be deleted in FinalizeGLContext. |
| 21 gfx::GLContext* InitializeGLContext() { |
| 22 if (!gfx::GLContext::InitializeOneOff()) { |
| 23 LOG(ERROR) << "gfx::GLContext::InitializeOneOff() failed"; |
| 24 return NULL; |
| 25 } |
| 26 gfx::GLContext* context = gfx::GLContext::CreateOffscreenGLContext(NULL); |
| 27 if (context == NULL) { |
| 28 LOG(ERROR) << "gfx::GLContext::CreateOffscreenGLContext(NULL) failed"; |
| 29 return NULL; |
| 30 } |
| 31 if (!context->MakeCurrent()) { |
| 32 LOG(ERROR) << "gfx::GLContext::MakeCurrent() failed"; |
| 33 context->Destroy(); |
| 34 delete context; |
| 35 return NULL; |
| 36 } |
| 37 return context; |
| 38 } |
| 39 |
| 40 // This destroy and delete the GL context. |
| 41 void FinalizeGLContext(gfx::GLContext** context) { |
| 42 DCHECK(context); |
| 43 if (*context) { |
| 44 (*context)->Destroy(); |
| 45 delete *context; |
| 46 *context = NULL; |
| 47 } |
| 48 } |
| 49 |
| 50 std::string GetGLString(unsigned int pname) { |
| 51 const char* gl_string = |
| 52 reinterpret_cast<const char*>(glGetString(pname)); |
| 53 if (gl_string) |
| 54 return std::string(gl_string); |
| 55 return ""; |
| 56 } |
| 57 |
| 58 uint32 GetVersionNumberFromString(const std::string& version_string) { |
| 59 int major = 0, minor = 0; |
| 60 size_t begin = version_string.find_first_of("0123456789"); |
| 61 if (begin != std::string::npos) { |
| 62 size_t end = version_string.find_first_not_of("01234567890.", begin); |
| 63 std::string sub_string; |
| 64 if (end != std::string::npos) |
| 65 sub_string = version_string.substr(begin, end - begin); |
| 66 else |
| 67 sub_string = version_string.substr(begin); |
| 68 std::vector<std::string> pieces; |
| 69 base::SplitString(sub_string, '.', &pieces); |
| 70 if (pieces.size() >= 2) { |
| 71 base::StringToInt(pieces[0], &major); |
| 72 base::StringToInt(pieces[1], &minor); |
| 73 } |
| 74 } |
| 75 return ((major << 8) + minor); |
| 76 } |
| 77 |
| 78 } // namespace anonymous |
| 79 |
| 80 namespace gpu_info_collector { |
| 81 |
| 82 bool CollectGraphicsInfoGL(GPUInfo* gpu_info) { |
| 83 DCHECK(gpu_info); |
| 84 |
| 85 gfx::GLContext* context = InitializeGLContext(); |
| 86 if (context == NULL) |
| 87 return false; |
| 88 |
| 89 gpu_info->SetGLRenderer(GetGLString(GL_RENDERER)); |
| 90 gpu_info->SetGLVendor(GetGLString(GL_VENDOR)); |
| 91 gpu_info->SetGLVersionString(GetGLString(GL_VERSION)); |
| 92 |
| 93 bool validGLVersionInfo = CollectGLVersionInfo(gpu_info); |
| 94 bool validVideoCardInfo = CollectVideoCardInfo(gpu_info); |
| 95 bool validDriverInfo = CollectDriverInfo(gpu_info); |
| 96 |
| 97 FinalizeGLContext(&context); |
| 98 |
| 99 return (validGLVersionInfo && validVideoCardInfo && validDriverInfo); |
| 100 } |
| 101 |
| 102 bool CollectGLVersionInfo(GPUInfo* gpu_info) { |
| 103 DCHECK(gpu_info); |
| 104 |
| 105 std::string gl_version_string = gpu_info->gl_version_string(); |
| 106 std::string glsl_version_string = |
| 107 GetGLString(GL_SHADING_LANGUAGE_VERSION); |
| 108 |
| 109 uint32 gl_version = GetVersionNumberFromString(gl_version_string); |
| 110 gpu_info->SetGLVersion(gl_version); |
| 111 |
| 112 uint32 glsl_version = GetVersionNumberFromString(glsl_version_string); |
| 113 gpu_info->SetShaderVersion(glsl_version, glsl_version); |
| 114 |
| 115 return true; |
| 116 } |
| 117 |
| 118 } // namespace gpu_info_collector |
| 119 |
OLD | NEW |