Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.