1

在firebreath(mac os)上编写一个插件,它绘制一个视频创建一个窗口来获取上下文,现在我希望在窗口中绘制我的库,它在另一个线程中运行。

我该怎么办?

4

1 回答 1

2

您可以从多个线程使用 OpenGL 上下文,只要您从不一次从多个线程同时使用它。例如

线程 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.
于 2013-02-19T14:17:24.483 回答