不幸的是,這些新功能真的沒有任何文檔。你會發現,現在最好的是在CVOpenGLESTextureCache.h
頭文件,在那裏你會看到函數參數的基本描述:
/*!
@function CVOpenGLESTextureCacheCreate
@abstract Creates a new Texture Cache.
@param allocator The CFAllocatorRef to use for allocating the cache. May be NULL.
@param cacheAttributes A CFDictionaryRef containing the attributes of the cache itself. May be NULL.
@param eaglContext The OpenGLES 2.0 context into which the texture objects will be created. OpenGLES 1.x contexts are not supported.
@param textureAttributes A CFDictionaryRef containing the attributes to be used for creating the CVOpenGLESTexture objects. May be NULL.
@param cacheOut The newly created texture cache will be placed here
@result Returns kCVReturnSuccess on success
*/
CV_EXPORT CVReturn CVOpenGLESTextureCacheCreate(
CFAllocatorRef allocator,
CFDictionaryRef cacheAttributes,
void *eaglContext,
CFDictionaryRef textureAttributes,
CVOpenGLESTextureCacheRef *cacheOut) __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_5_0);
更困難的元素屬性字典,遺憾的是你需要的找到正確使用這些功能的例子。 Apple擁有GLCameraRipple和RosyWriter示例,展示如何使用BGRA和YUV輸入色彩格式的快速紋理上傳路徑。 Apple還在WWDC上提供了ChromaKey示例(可能還會與視頻一起訪問該示例),演示瞭如何使用這些紋理緩存從OpenGL ES紋理中提取信息。
我剛剛得到這個快速紋理上傳我GPUImage框架(可在該鏈接的源代碼這)工作,所以我會制定出什麼我能解析出這一點。首先,我使用以下代碼創建紋理高速緩存:
CVReturn err = CVOpenGLESTextureCacheCreate(kCFAllocatorDefault, NULL, (__bridge void *)[[GPUImageOpenGLESContext sharedImageProcessingOpenGLESContext] context], NULL, &coreVideoTextureCache);
if (err)
{
NSAssert(NO, @"Error at CVOpenGLESTextureCacheCreate %d");
}
其中上下文稱爲是被配置用於OpenGL ES 2.0的一個EAGLContext。
我使用它來保持視頻幀從視頻存儲器中的iOS設備相機,我用下面的代碼來做到這一點:
CVPixelBufferLockBaseAddress(cameraFrame, 0);
CVOpenGLESTextureRef texture = NULL;
CVReturn err = CVOpenGLESTextureCacheCreateTextureFromImage(kCFAllocatorDefault, coreVideoTextureCache, cameraFrame, NULL, GL_TEXTURE_2D, GL_RGBA, bufferWidth, bufferHeight, GL_BGRA, GL_UNSIGNED_BYTE, 0, &texture);
if (!texture || err) {
NSLog(@"CVOpenGLESTextureCacheCreateTextureFromImage failed (error: %d)", err);
return;
}
outputTexture = CVOpenGLESTextureGetName(texture);
glBindTexture(GL_TEXTURE_2D, outputTexture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
// Do processing work on the texture data here
CVPixelBufferUnlockBaseAddress(cameraFrame, 0);
CVOpenGLESTextureCacheFlush(coreVideoTextureCache, 0);
CFRelease(texture);
outputTexture = 0;
這將創建一個新的CVOpenGLESTextureRef,代表一個OpenGL ES紋理,從紋理緩存。該紋理基於相機傳入的CVImageBufferRef。然後從CVOpenGLESTextureRef中檢索紋理,併爲其設置適當的參數(這在我的處理過程中似乎是必需的)。最後,我完成了關於紋理的工作,並在完成後進行清理。
這種快速上傳過程對iOS設備產生了真正的影響。它將iPhone 4S上的一個640x480幀的視頻上傳和處理時間從9.0 ms減少到1.8 ms。
我也有heard that this works in reverse,這可能允許在某些情況下替換glReadPixels()
,但我還沒有嘗試過。
對於圖像處理這種有效的方式沒有很好的文檔記錄確實令人遺憾。但是,像你這樣的人沒有經驗,開發人員也可以嘗試使用它。 – Pochi 2012-03-06 01:32:27
您是否能夠獲取用於照片預設的紋理緩存?我無法在'CFDictionaryRef cacheAttributes'的任何地方找到'CVOpenGLESTextureCacheCreate'的任何信息。 – Dex 2012-04-04 08:05:58
@Dex - 是的,這對照片預設有效,與我用於視頻幀的設置相同。使用我已使用的選項查看我的上面鏈接的框架。需要注意的是,在iPhone 4上,相機可以捕捉比該設備上最大紋理大小更大的幀。這些照片不能作爲紋理直接處理,因此需要進行某種平鋪處理。我正在努力。 – 2012-04-04 13:02:59