在firebreath(mac os)上编写一个插件,用于绘制视频 创建一个窗口来获取上下文,现在我希望在窗口中绘制我的库,它在另一个线程中运行。
我该怎么办?
答案 0 :(得分:2)
您可以使用来自多个线程的OpenGL上下文,只要您从不同时从多个线程同时使用它。 E.g。
主题A:
[myContext makeCurrentContext];
// Do something with the context...
// ... then release it on the thread.
[NSOpenGLContext clearCurrentContext];
// Tell Thread B that we released the context.
// Wait for Thread B to finish...
// Grab the context again.
[myContext makeCurrentContext];
// Do something with the context...
主题B:
// Wait for Thread A to release the context...
[myContext makeCurrentContext];
// Do something with the context...
// ... then release it on the thread.
[NSOpenGLContext clearCurrentContext];
// Let Thread A know, that we are done with the context.
另一种可能性是使用辅助共享上下文。共享上下文与其父上下文共享相同的资源,因此您可以在共享上下文中创建纹理(在辅助线程上使用),将视频渲染到辅助线程上的该纹理,然后使主线程呈现纹理(在将下一个帧渲染到辅助线程上的纹理之前,它也可以在主线程的父上下文中显示到屏幕上。
与CGL框架相同的代码:
主题A:
err = CGLSetCurrentContext(myContext);
// Do something with the context...
// ... then release it on the thread.
err = CGLSetCurrentContext(NULL);
// Tell Thread B that we released the context.
// Wait for Thread B to finish...
// Grab the context again.
err = CGLSetCurrentContext(myContext);
// Do something with the context...
主题B:
// Wait for Thread A to release the context...
err = CGLSetCurrentContext(myContext);
// Do something with the context...
// ... then release it on the thread.
err = CGLSetCurrentContext(NULL);
// Let Thread A know, that we are done with the context.