渲染到cpu与gpu共享内存的CVPixelBuffer的方式

2,749 阅读1分钟


OpenGL方式:

与CFDictionaryRef empty; // empty value for attr value.

CFMutableDictionaryRef attrs;

empty = CFDictionaryCreate(kCFAllocatorDefault, NULL, NULL, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);

attrs = CFDictionaryCreateMutable(kCFAllocatorDefault, 1, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);

CFDictionarySetValue(attrs, kCVPixelBufferIOSurfacePropertiesKey, empty);

CVReturn err = CVPixelBufferCreate(kCFAllocatorDefault, m_size.width, m_size.height, kCVPixelFormatType_32BGRA, attrs, &m_renderTarget);

if (err) {

}

err = CVOpenGLESTextureCacheCreateTextureFromImage (kCFAllocatorDefault, coreVideoTextureCache, m_renderTarget,

NULL, // texture attributes

GL_TEXTURE_2D,

GL_RGBA, 

m_size.width,

m_size.height,

GL_BGRA, 

m_option.type,

0,

&m_renderTexture);

metal方式:

CVMetalTextureCacheCreate(

kCFAllocatorDefault,

nil,

device!,

nil,

&textureCache)

let pixelBufferAttri = [kCVPixelBufferIOSurfacePropertiesKey: [:]]

let s = CVPixelBufferCreate(

kCFAllocatorDefault,

720,

1280,

kCVPixelFormatType_32BGRA,

pixelBufferAttri,

&pixelBuffer)

let result = CVMetalTextureCacheCreateTextureFromImage(

kCFAllocatorDefault,

textureCache!,

offlinePixelBuffer!,

nil,

self.colorPixelFormat,

1080,

1920,

0,

&texture)