QPainter::drawImage 在 Mac OS X 和 Windows 上的性能差异很大

发布于 2024-12-25 05:27:16 字数 2937 浏览 2 评论 0原文

目前,我正在将 Windows 上用 Qt 4.8 编写的应用程序移植到 Mac OS X 中。我发现 QPainter::drawImage 的性能在 Mac OS X 和 Windows 上有很大差异。

在我用 google 搜索了一堆文章并在 Mac OS X 上分析了我的 Qt 应用程序后,我发现原因是 Mac 版本的 drawImage 是通过调用 Quartz 函数 CGContextDrawImage 实现的,因此调用了 argb32_sample_argb32代码>. argb32_sample_argb32 浪费了大部分 CPU 周期。

最有可能的是,Mac OS X 在 API CGContextDrawImage 中将通用颜色空间转换为设备相关的颜色空间。不过我修改了QT 4.8的源代码,直接获取系统显示色彩空间。

结果还是不好。我的 mac 版本应用程序比 Windows 版本慢得多。

谁能告诉我为什么?

/// QT 4.8's source code modified by me

CGColorSpaceRef QCoreGraphicsPaintEngine::macGenericColorSpace()
{

#if 0
    if (!m_genericColorSpace) {
#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_4
        if (QSysInfo::MacintoshVersion >= QSysInfo::MV_10_4) {
            m_genericColorSpace = CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB);
        } else
#endif
        {
            m_genericColorSpace = CGColorSpaceCreateDeviceRGB();
        }
        if (!m_postRoutineRegistered) {
            m_postRoutineRegistered = true;
            qAddPostRoutine(QCoreGraphicsPaintEngine::cleanUpMacColorSpaces);
        }
    }
    return m_genericColorSpace;
#else
// Just return the main display colorspace for the moment.                                                                                                                                                                                                                  
//    return macDisplayColorSpace();                                                                                                                                                                                                                                        
    return CreateSystemColorSpace();
#endif
}


CGColorSpaceRef CreateSystemColorSpace () {
  CMProfileRef sysprof = NULL;
  CGColorSpaceRef dispColorSpace = NULL;

  // Get the Systems Profile for the main display                                                                                                                                                                                                                               
  if (CMGetSystemProfile(&sysprof) == noErr)
  {
      // Create a colorspace with the systems profile                                                                                                                                                                                                                           
      dispColorSpace = CGColorSpaceCreateWithPlatformColorSpace(sysprof);

      // Close the profile                                                                                                                                                                                                                                                      
      CMCloseProfile(sysprof);
  }

  return dispColorSpace;
}

Currently I'm porting an application written with Qt 4.8 on Windows into Mac OS X. I found the performance of QPainter::drawImage differs dramatically on Mac OS X and Windows.

After I googled a bunch of articles and profile my Qt application on Mac OS X, I found the reason is that the Mac version drawImage was implemented by calling Quartz function CGContextDrawImage which consequently calls argb32_sample_argb32. argb32_sample_argb32 wastes most of the cpu cycles.

Most probably, Mac OS X converts generic color space into device-dependent color space in the API CGContextDrawImage. However I modified the source code of QT 4.8 the obtain the system display color space directly.

The result is still not good. My mac version app is significantly slower than Windows version.

Anyone can tell me why?

/// QT 4.8's source code modified by me

CGColorSpaceRef QCoreGraphicsPaintEngine::macGenericColorSpace()
{

#if 0
    if (!m_genericColorSpace) {
#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_4
        if (QSysInfo::MacintoshVersion >= QSysInfo::MV_10_4) {
            m_genericColorSpace = CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB);
        } else
#endif
        {
            m_genericColorSpace = CGColorSpaceCreateDeviceRGB();
        }
        if (!m_postRoutineRegistered) {
            m_postRoutineRegistered = true;
            qAddPostRoutine(QCoreGraphicsPaintEngine::cleanUpMacColorSpaces);
        }
    }
    return m_genericColorSpace;
#else
// Just return the main display colorspace for the moment.                                                                                                                                                                                                                  
//    return macDisplayColorSpace();                                                                                                                                                                                                                                        
    return CreateSystemColorSpace();
#endif
}


CGColorSpaceRef CreateSystemColorSpace () {
  CMProfileRef sysprof = NULL;
  CGColorSpaceRef dispColorSpace = NULL;

  // Get the Systems Profile for the main display                                                                                                                                                                                                                               
  if (CMGetSystemProfile(&sysprof) == noErr)
  {
      // Create a colorspace with the systems profile                                                                                                                                                                                                                           
      dispColorSpace = CGColorSpaceCreateWithPlatformColorSpace(sysprof);

      // Close the profile                                                                                                                                                                                                                                                      
      CMCloseProfile(sysprof);
  }

  return dispColorSpace;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文