3

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;
}
kopischke
  • 3,393
  • 1
  • 21
  • 40
glacierx
  • 61
  • 4
  • 1
    http://stackoverflow.com/questions/3915490/cgcontextdrawimage-very-slow-on-iphone-4 http://benoitgirard.wordpress.com/2010/03/09/optimizing-cgcontextdrawimage/ http://lists.apple.com/archives/quartz-dev/2008/Feb/msg00047.html – glacierx Jan 07 '12 at 19:18
  • some related links are provided above. – glacierx Jan 07 '12 at 19:18

0 Answers0