1

write a plugin on firebreath (mac os), which draws a video Creates a window to get the context, now I would want that in the window drew my library, which is running in another thread.

How do I do?

1 Answer 1

2

You can use an OpenGL context from multiple threads, as long as you never use it simultaneously from more than one thread at a time. E.g.

Thread 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...

Thread 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.

Another possibility is to use a secondary shared context. A shared context shares the same resources with its parent context, so you can create a texture in a shared context (used on the secondary thread), render your video to that texture on the secondary thread, then make the main thread render the texture (which is also available in the parent context on the main thread) to the screen, before you render the next frame to the texture on the secondary thread.

Update

Same Code as above with CGL framework:

Thread 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...

Thread 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.
Sign up to request clarification or add additional context in comments.

5 Comments

And in the stream I create only the window, more of that does not do. Thanks, I'll try your option. But I do not object c code. I have CGLContextObj
after CGLSetCurrentContext (dh-> share); any method drops the opengl plugin
@user I added a CGL sample code; clearCurrentThread should have been clear current context (sorry, my mistake). What does "drops the OpenGL plugin" mean? What is "drop" in this context? It is very hard to help you without having any code; setting the wrong context at the wrong time will of course lead to trouble. Also before you set an own context, you may want to save the currently set context and restore it later on, since other code may rely that its context is never changed.
@user Also setting the same context on more than one thread is not prevented by the system (e.g. the context is not "unset" on Thread A, just because it becomes set on Thread B) and it also causes no error message, however using a context set on more than one thread may cause trouble, since a context is not designed to be set on more than one thread at the same time (depends on the implementation, though).
I corrected the fall. but that does not draw a second thread in the window, only the first one! ERR = CGLSetCurrentContext (NULL) - have not tried it, as I will try - I'll write. thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.