• Chromium源码由浅入深(一)


    工作中需要对Chromium源码、尤其是源码中图形部分进行深入研究,所以借此机会边学习边写文章,分享一下我的实时学习研究Chromium源码的由浅入深的过程。

    闲言少叙,书归正传。

    通过命令行启动Chrome浏览器,命令及结果如下:

    1. $ google-chrome-stable
    2. MESA-INTEL: warning: Performance support disabled, consider sysctl dev.i915.perf_stream_paranoid=0

    在浏览器地址栏中输入“chrome://gpu”,得到以下结果:

    第一步工作就是要找到如上所示的信息在Chromium源码中的具体位置。

    经过查找定位,以上信息对应的代码在Chromium源码目录的content/browser/resources/gpu/info_view.js中,如下所示:

    1. appendFeatureInfo_(
    2. featureInfo, featureStatusList, problemsDiv, problemsList, workaroundsDiv,
    3. workaroundsList) {
    4. // Feature map
    5. const featureLabelMap = {
    6. '2d_canvas': 'Canvas',
    7. 'gpu_compositing': 'Compositing',
    8. 'webgl': 'WebGL',
    9. 'multisampling': 'WebGL multisampling',
    10. 'texture_sharing': 'Texture Sharing',
    11. 'video_decode': 'Video Decode',
    12. 'rasterization': 'Rasterization',
    13. 'opengl': 'OpenGL',
    14. 'metal': 'Metal',
    15. 'vulkan': 'Vulkan',
    16. 'multiple_raster_threads': 'Multiple Raster Threads',
    17. 'native_gpu_memory_buffers': 'Native GpuMemoryBuffers',
    18. 'protected_video_decode': 'Hardware Protected Video Decode',
    19. 'surface_control': 'Surface Control',
    20. 'vpx_decode': 'VPx Video Decode',
    21. 'webgl2': 'WebGL2',
    22. 'canvas_oop_rasterization': 'Canvas out-of-process rasterization',
    23. 'raw_draw': 'Raw Draw',
    24. 'video_encode': 'Video Encode',
    25. 'direct_rendering_display_compositor':
    26. 'Direct Rendering Display Compositor',
    27. 'webgpu': 'WebGPU',
    28. };
    29. const statusMap = {
    30. 'disabled_software': {
    31. 'label': 'Software only. Hardware acceleration disabled',
    32. 'class': 'feature-yellow',
    33. },
    34. 'disabled_off': {'label': 'Disabled', 'class': 'feature-red'},
    35. 'disabled_off_ok': {'label': 'Disabled', 'class': 'feature-yellow'},
    36. 'unavailable_software': {
    37. 'label': 'Software only, hardware acceleration unavailable',
    38. 'class': 'feature-yellow',
    39. },
    40. 'unavailable_off': {'label': 'Unavailable', 'class': 'feature-red'},
    41. 'unavailable_off_ok': {'label': 'Unavailable', 'class': 'feature-yellow'},
    42. 'enabled_readback': {
    43. 'label': 'Hardware accelerated but at reduced performance',
    44. 'class': 'feature-yellow',
    45. },
    46. 'enabled_force': {
    47. 'label': 'Hardware accelerated on all pages',
    48. 'class': 'feature-green',
    49. },
    50. 'enabled': {'label': 'Hardware accelerated', 'class': 'feature-green'},
    51. 'enabled_on': {'label': 'Enabled', 'class': 'feature-green'},
    52. 'enabled_force_on': {'label': 'Force enabled', 'class': 'feature-green'},
    53. };
    54. // feature status list
    55. featureStatusList.textContent = '';
    56. for (const featureName in featureInfo.featureStatus) {
    57. const featureStatus = featureInfo.featureStatus[featureName];
    58. const featureEl = document.createElement('li');
    59. const nameEl = document.createElement('span');
    60. if (!featureLabelMap[featureName]) {
    61. console.info('Missing featureLabel for', featureName);
    62. }
    63. nameEl.textContent = featureLabelMap[featureName] + ': ';
    64. featureEl.appendChild(nameEl);
    65. const statusEl = document.createElement('span');
    66. const statusInfo = statusMap[featureStatus];
    67. if (!statusInfo) {
    68. console.info('Missing status for ', featureStatus);
    69. statusEl.textContent = 'Unknown';
    70. statusEl.className = 'feature-red';
    71. } else {
    72. statusEl.textContent = statusInfo['label'];
    73. statusEl.className = statusInfo['class'];
    74. }
    75. featureEl.appendChild(statusEl);
    76. featureStatusList.appendChild(featureEl);
    77. }
    78. // problems list
    79. if (featureInfo.problems.length) {
    80. problemsDiv.hidden = false;
    81. problemsList.textContent = '';
    82. for (const problem of featureInfo.problems) {
    83. const problemEl = this.createProblemEl_(problem);
    84. problemsList.appendChild(problemEl);
    85. }
    86. } else {
    87. problemsDiv.hidden = true;
    88. }
    89. // driver bug workarounds list
    90. if (featureInfo.workarounds.length) {
    91. workaroundsDiv.hidden = false;
    92. workaroundsList.textContent = '';
    93. for (const workaround of featureInfo.workarounds) {
    94. const workaroundEl = document.createElement('li');
    95. workaroundEl.textContent = workaround;
    96. workaroundsList.appendChild(workaroundEl);
    97. }
    98. } else {
    99. workaroundsDiv.hidden = true;
    100. }
    101. }

    可以看到,上边网页中显示的内容大部分都能对应到代码中的featureLabelMap和statusMap中。比如:网页中“Graphics Feature Status”下的“Canvas: Hardware accelerated”、“Compositing: Hardware accelerated”、“OpenGL: Enabled”、“Video Decode: Hardware accelerated”、“Video Encode: Software only. Hardware acceleration disabled”、“Vulkan: Disabled”、“WebGPU: Disabled”等等。

    那么appendFeatureInfo_函数也就不难理解了。一段一段来看。

    首先来看以下代码片段:

    1. // feature status list
    2. featureStatusList.textContent = '';
    3. for (const featureName in featureInfo.featureStatus) {
    4. const featureStatus = featureInfo.featureStatus[featureName];
    5. const featureEl = document.createElement('li');
    6. const nameEl = document.createElement('span');
    7. if (!featureLabelMap[featureName]) {
    8. console.info('Missing featureLabel for', featureName);
    9. }
    10. nameEl.textContent = featureLabelMap[featureName] + ': ';
    11. featureEl.appendChild(nameEl);
    12. const statusEl = document.createElement('span');
    13. const statusInfo = statusMap[featureStatus];
    14. if (!statusInfo) {
    15. console.info('Missing status for ', featureStatus);
    16. statusEl.textContent = 'Unknown';
    17. statusEl.className = 'feature-red';
    18. } else {
    19. statusEl.textContent = statusInfo['label'];
    20. statusEl.className = statusInfo['class'];
    21. }
    22. featureEl.appendChild(statusEl);
    23. featureStatusList.appendChild(featureEl);
    24. }

    代码注释说得很清楚,这段是“feature status list”,从这里就可以看出来是对应Chrome网页中的“Graphics Feature Status”。

    featureInfo是调用appendFeatureInfo_函数时传下来的,暂时不管其实际内容。循环处理featureInfo.featureStatus中的每一项。

    先要获得每一项的featureStatus;然后调用document.createElement("li")和document.createElement("span")分别创建“li”标签和“span标签”节点(笔者对于JavaScript不熟,如有错误请指正);再往下根据每个项的featureLabelMap[featureName]和statusMap[featureStatus]向网页中添加该项的名称和状态,状态还要带上颜色。就是网页中的那些项。

    接下来是以下代码片段:

    1. // problems list
    2. if (featureInfo.problems.length) {
    3. problemsDiv.hidden = false;
    4. problemsList.textContent = '';
    5. for (const problem of featureInfo.problems) {
    6. const problemEl = this.createProblemEl_(problem);
    7. problemsList.appendChild(problemEl);
    8. }
    9. } else {
    10. problemsDiv.hidden = true;
    11. }

    代码注释说得也很明白,这段是“problems list”。

    featureInfo是调用appendFeatureInfo_函数时传下来的,暂时不管其实际内容。循环处理featureInfo.problems中的每一项。

    再往下是以下代码片段:

    1. // driver bug workarounds list
    2. if (featureInfo.workarounds.length) {
    3. workaroundsDiv.hidden = false;
    4. workaroundsList.textContent = '';
    5. for (const workaround of featureInfo.workarounds) {
    6. const workaroundEl = document.createElement('li');
    7. workaroundEl.textContent = workaround;
    8. workaroundsList.appendChild(workaroundEl);
    9. }
    10. } else {
    11. workaroundsDiv.hidden = true;
    12. }

    代码注释说得也很明白,这段是“driver bug workaround list”,从这里就可以看出来是对应Chrome网页中的“Driver Bug Workarounds”。

    featureInfo是调用appendFeatureInfo_函数时传下来的,暂时不管其实际内容。循环处理featureInfo.workaroundsList中的每一项。

    至此,网页中的gpu相关信息在代码中的定位以及appendFeatureInfo_函数就解析完了。

    预知后事如何,且看下回分解。

  • 相关阅读:
    初识Springboot
    ASEMI快恢复二极管ES5JB参数,ES5JB特性,ES5JB机械数据
    齿轮故障诊断的实验数据集及python处理
    Vot-Toolkit环境配置指南
    高企认定对研发费用的要求是什么?
    HMS Core Discovery第16期回顾|与虎墩一起,玩转AI新“声”态
    【minitab实战】--控制图制作
    【飞书ChatGPT机器人】飞书接入ChatGPT,打造智能问答助手
    记录一次线上fullgc问题排查过程
    Java多线程:waitnotify原理剖析
  • 原文地址:https://blog.csdn.net/phmatthaus/article/details/134029768