-->

How to detect hardware acceleration for Core Image

2020-07-30 04:16发布

问题:

In my app I'm using a "CIMotionBlur" CIFilter during CALayer animation. The problem is that the filter does not work properly when hardware acceleration is not available:

  • In OS X Safe Mode the layer becomes invisible during animation;
  • When using VMWare Fusion the animation is unbearably slow and makes testing the app harder;

Animation works fine without the filter. I'd like to apply filter only when hardware acceleration is available.

What's the highest level API that would let me know when to disable the filter? I'm going to look for clues in IOKit.

回答1:

I found the answer in Technical Q&A QA1218
"How do I tell if a particular display is being hardware accelerated by Quartz Extreme?"

It's as simple as this:

NSNumber* curScreenNum = [self.window.screen.deviceDescription objectForKey:@"NSScreenNumber"];
if (CGDisplayUsesOpenGLAcceleration(curScreenNum.integerValue))
{
    // Do accelerated stuff
}

Works as expected in my cases.



回答2:

https://developer.apple.com/library/ios/documentation/graphicsimaging/Conceptual/CoreImaging/ci_concepts/ci_concepts.html#//apple_ref/doc/uid/TP30001185-CH2-TPXREF101

I guess you could go with this to determine whether your desired filters are available. They probably shouldn't be available if Core Image is not available.



回答3:

If filterNamesInCategory returns the CIMotionBlur filter's name even when it is not available then you should file a bug report with Apple's bug reporter.

As a further test, you could call filterWithName and check for a non-nil result. If you get back nil, you know the filter is available.

Or are you saying that on platforms without hardware acceleration, you get back a filter but it doesn't work? If that's the case then again, time to file a bug report.

Or, are you saying that on non-hardware-accellerated platforms, you get back a filter that works, but it is too slow to be useful?

Poking around in the docs I don't see any way to tell if Core Image is hardware-accellerated for a given platform or not.

However, CI filters are built on top of OpenGL, and you CAN query the OpenGL driver to see if it's hardware-accellerated or not. My guess is that the result of such a query would also tell you if CIFilters were hardware-accellerated or not.