以下是简要问题:
对于某些图层合成,我必须在CGContext中渲染OpenGL纹理。最快的方法是什么?
到目前为止的想法:
显然,调用renderInContext不会捕获OpenGL内容,并且glReadPixels太慢了。
对于某些“上下文”,我在一个图层的委托类中调用此方法:
- (void) drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx
我考虑使用CVOpenGLESTextureCache,但这需要额外的渲染,而且渲染后似乎需要进行一些复杂的转换。
现在这是我(可怕的)实施:
glBindRenderbuffer(GL_RENDERBUFFER, displayRenderbuffer);
NSInteger x = 0, y = 0, width = backingWidth, height = backingHeight;
NSInteger dataLength = width * height * 4;
GLubyte *data = (GLubyte *) malloc(dataLength * sizeof(GLubyte));
glPixelStorei(GL_PACK_ALIGNMENT, 4);
glReadPixels(x, y, width, height, GL_RGBA, GL_UNSIGNED_BYTE, data);
CGDataProviderRef ref = CGDataProviderCreateWithData(NULL, data, dataLength, NULL);
CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();
CGImageRef iref = CGImageCreate(width, height, 8, 32, width * 4, colorspace, kCGBitmapByteOrder32Big | kCGImageAlphaPremultipliedLast,
ref, NULL, true, kCGRenderingIntentDefault);
CGFloat scale = self.contentScaleFactor;
NSInteger widthInPoints, heightInPoints;
widthInPoints = width / scale;
heightInPoints = height / scale;
CGContextSetBlendMode(context, kCGBlendModeCopy);
CGContextDrawImage(context, CGRectMake(0.0, 0.0, widthInPoints, heightInPoints), iref);
// Clean up
free(data);
CFRelease(ref);
CFRelease(colorspace);
CGImageRelease(iref);
谢谢!
答案 0 :(得分:8)
确定。
对于任何好奇的人来说,上面显示的方法不是最快的方法。
当询问UIView的内容时,它会询问它的图层(通常是CALayer)为它绘制它们。例外:基于OpenGL的视图使用CAEAGLLayer(CALayer的子类),使用相同的方法但不返回任何内容。 没有画画。
所以,如果你打电话:
[someUIView.layer drawInContext:someContext];
它会起作用,而
[someOpenGLView.layer drawInContext:someContext];
不会。
如果您要求任何基于OpenGL的视图的超级视图的内容,这也会成为一个问题:它会递归地询问每个子视图的内容,而任何使用CAEAGLLayer的子视图都不会回传任何内容(你'我会看到一个黑色的矩形)。
我在上面列出了找到CALayer,drawLayer:inContext:的委托方法的实现,我可以在任何基于OpenGL的视图中使用它,以便视图对象本身提供其内容(而不是图层)。委托方法是自动调用的:Apple期望它以这种方式工作。
如果性能不是问题,您可以在视图中实现简单快照方法的变体。该方法如下所示:
- (void) drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx {
GLint backingWidth, backingHeight;
glBindRenderbufferOES(GL_RENDERBUFFER_OES, _colorRenderbuffer);
glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_WIDTH_OES, &backingWidth);
glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_HEIGHT_OES, &backingHeight);
NSInteger x = 0, y = 0, width = backingWidth, height = backingHeight;
NSInteger dataLength = width * height * 4;
GLubyte *data = (GLubyte*)malloc(dataLength * sizeof(GLubyte));
// Read pixel data from the framebuffer
glPixelStorei(GL_PACK_ALIGNMENT, 4);
glReadPixels(x, y, width, height, GL_RGBA, GL_UNSIGNED_BYTE, data);
CGDataProviderRef ref = CGDataProviderCreateWithData(NULL, data, dataLength, NULL);
CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();
CGImageRef iref = CGImageCreate(width, height, 8, 32, width * 4, colorspace, kCGBitmapByteOrder32Big | kCGImageAlphaPremultipliedLast,
ref, NULL, true, kCGRenderingIntentDefault);
CGContextDrawImage(ctx, self.bounds, iref);
}
但是!这不是性能效果。
几乎无处不在,glReadPixels并不是一个快速通话。从iOS 5开始,Apple公开了CVOpenGLESTextureCacheRef - 基本上是一个共享缓冲区,既可以用作CVPixelBufferRef,也可以用作OpenGL纹理。最初,它被设计用作从视频帧获取OpenGL纹理的一种方式:现在它更常用于反向,从纹理中获取视频帧。因此,上述想法的更好实现是使用从CVOpenGLESTextureCacheCreateTextureFromImage获得的CVPixelBufferRef,直接访问这些像素,将它们绘制到您缓存的CGImage中,并在上面的委托方法中绘制到您的上下文中。
代码在这里。在每个渲染过程中,您将纹理绘制到纹理缓存中,该纹理链接到CVPixelBuffer Ref:
- (void) renderToCGImage {
// Setup the drawing
[ochrContext useProcessingContext];
glBindFramebuffer(GL_FRAMEBUFFER, layerRenderingFramebuffer);
glViewport(0, 0, (int) self.frame.size.width, (int) self.frame.size.height);
[ochrContext setActiveShaderProgram:layerRenderingShaderProgram];
// Do the actual drawing
glActiveTexture(GL_TEXTURE4);
glBindTexture(GL_TEXTURE_2D, self.inputTexture);
glUniform1i(layerRenderingInputTextureUniform, 4);
glVertexAttribPointer(layerRenderingShaderPositionAttribute, 2, GL_FLOAT, 0, 0, kRenderTargetVertices);
glVertexAttribPointer(layerRenderingShaderTextureCoordinateAttribute, 2, GL_FLOAT, 0, 0, kRenderTextureVertices);
// Draw and finish up
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
glFinish();
// Try running this code asynchronously to improve performance
dispatch_async(PixelBufferReadingQueue, ^{
// Lock the base address (can't get the address without locking it).
CVPixelBufferLockBaseAddress(renderTarget, 0);
// Get a pointer to the pixels
uint32_t * pixels = (uint32_t*) CVPixelBufferGetBaseAddress(renderTarget);
// Wrap the pixel data in a data-provider object.
CGDataProviderRef pixelWrapper = CGDataProviderCreateWithData(NULL, pixels, CVPixelBufferGetDataSize(renderTarget), NULL);
// Get a color-space ref... can't this be done only once?
CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
// Release the exusting CGImage
CGImageRelease(currentCGImage);
// Get a CGImage from the data (the CGImage is used in the drawLayer: delegate method above)
currentCGImage = CGImageCreate(self.frame.size.width,
self.frame.size.height,
8,
32,
4 * self.frame.size.width,
colorSpaceRef,
kCGBitmapByteOrder32Big | kCGImageAlphaPremultipliedLast,
pixelWrapper,
NULL,
NO,
kCGRenderingIntentDefault);
// Clean up
CVPixelBufferUnlockBaseAddress(renderTarget, 0);
CGDataProviderRelease(pixelWrapper);
CGColorSpaceRelease(colorSpaceRef);
});
}
然后非常简单地实现委托方法:
- (void) drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx {
CGContextDrawImage(ctx, self.bounds, currentCGImage);
}
我希望这对你有所帮助,亲爱的互联网。