我正在通过以下方式从id<MTLTexture>
创建CVPixelBufferRef
对象:
id<MTLTexture> CreateMTLTexrure(CVMetalTextureCacheRef texture_cache,
CVPixelBufferRef pixel_buffer,
MTLPixelFormat metal_pixel_format, size_t plane,
int height, int width) {
CVMetalTextureRef texture_ref;
CVReturn err = CVMetalTextureCacheCreateTextureFromImage(
kCFAllocatorDefault, texture_cache, pixel_buffer, NULL,
metal_pixel_format, width, height, plane, &texture_ref);
if (err != kCVReturnSuccess) {
// throw error
return nil;
}
id<MTLTexture> texture = CVMetalTextureGetTexture(texture_ref);
//
// Q: is it safe to do CVBufferRelease(texture_ref) here?
//
return texture;
}
何时应释放CVMetalTextureRef对象? 获得MTLTexture后可以安全释放它吗?
答案 0 :(得分:0)
是的,它很安全。 根据Apple文档存档中的sample code,它是在分配后立即发布的,就像这样:
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.3/js/bootstrap.min.js"></script>
<div class="container">
<div class="row">
<div class="col-4" style="background-color: #7fff7f;">
<div class="star-rating">
<span style="width: 65%;"></span>
</div>
</div>
<div class="col-8" style="background-color: #ff7f7f;">
Something something
</div>
</div>
</div>
您还可以查看Google Chromium存储库中的WebRTC source code,他们也以相同的方式发布CVMetalTextureRef。
- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection
{
CVReturn error;
CVImageBufferRef sourceImageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
size_t width = CVPixelBufferGetWidth(sourceImageBuffer);
size_t height = CVPixelBufferGetHeight(sourceImageBuffer);
CVMetalTextureRef textureRef;
error = CVMetalTextureCacheCreateTextureFromImage(kCFAllocatorDefault, _videoTextureCache, sourceImageBuffer, NULL, MTLPixelFormatBGRA8Unorm, width, height, 0, &textureRef);
if (error)
{
NSLog(@">> ERROR: Couldnt create texture from image");
assert(0);
}
_videoTexture[_constantDataBufferIndex] = CVMetalTextureGetTexture(textureRef);
if (!_videoTexture[_constantDataBufferIndex]) {
NSLog(@">> ERROR: Couldn't get texture from texture ref");
assert(0);
}
CVBufferRelease(textureRef);
}