Skip to content

Instantly share code, notes, and snippets.

@ccgus
Created November 30, 2017 22:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ccgus/0337933e6b5b079a4865ef77619da855 to your computer and use it in GitHub Desktop.
Save ccgus/0337933e6b5b079a4865ef77619da855 to your computer and use it in GitHub Desktop.
void TSCreateImageFromIOSurfaceReleaseCallback(void * __nullable info, const void *data, size_t size);
void TSCreateImageFromIOSurfaceReleaseCallback(void * __nullable info, const void *data, size_t size) {
CFRelease(info);
}
CGImageRef TSCreateImageFromIOSurface(IOSurfaceRef surface, CGColorSpaceRef cs) {
IOReturn result = IOSurfaceLock(surface, 0, nil);
FMAssert(result == kIOReturnSuccess);
CFRetain(surface); // will be released in TSCreateImageFromIOSurfaceReleaseCallback
CGDataProviderRef dp = CGDataProviderCreateWithData(surface, IOSurfaceGetBaseAddress(surface), IOSurfaceGetHeight(surface) * IOSurfaceGetBytesPerRow(surface), &TSCreateImageFromIOSurfaceReleaseCallback);
CGImageRef r = CGImageCreate(IOSurfaceGetWidth(surface), IOSurfaceGetHeight(surface), 8, 32, IOSurfaceGetBytesPerRow(surface), cs, (CGBitmapInfo)kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big, dp, nil, false, kCGRenderingIntentDefault);
CGDataProviderRelease(dp);
IOSurfaceUnlock(surface, 0, nil);
return r;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment