Redrawing from a thread

Hey All

I go a quick question about rendering OC from a thread. I’m using OC with QT and have created a plugin with in which I have the OC stuff which is loaded by a QT application.

Without any thread I can view a rendered window attaching my view to the QT window as follows,

Handle(WNT_Window) theWNTWindow;
Handle(WNT_GraphicDevice) theGraphicDevice;
Handle(V3d_View) theView;
Handle(V3d_Viewer) theViewer;
Handle(AIS_InteractiveContext) theContext;

TCollection_ExtendedString aName("View 3D");
theGraphicDevice = new Graphic3d_WNTGraphicDevice();

theViewer = new V3d_Viewer(theGraphicDevice, aName.ToExtString());
theContext = new AIS_InteractiveContext(theViewer);
theView = theContext->CurrentViewer()->CreateView();

theWNTWindow = new WNT_Window(theGraphicDevice, reinterpret_cast(theWindowHandle) );

if(theView) theView->SetWindow(theWNTWindow);

After this I just call the following code if anything changes,

if(theView) theView -> Redraw();

This works fine.
The Problem comes with the thread (QThread), here I attach the plugin in my thread

::start( QThread::Priority priority )
{
theMutex.lock();
ADT_INTERFACE::ADT_OCInterface::InitLib();
theMutex.unlock();

theDoEventLoop = true;
QThread::start(priority);
}

Where InitLib executes the first code block.

Then I run an event loop which should render every little amount of time

::run()
{
while(theDoEventLoop)
{
theMutex.lock();
ADT_INTERFACE::ADT_OCInterface::RedrawView();
theMutex.unlock();
QThread::msleep(40);
}

The view seems to attach to the window ok using the winId but the render operation (although called) doesn’t seem to go to the window.

Thus my window is filled in with a dark grey color, but nothing is ever rendered over the top of it. Thus if I change the background color of the window to white, I continue to see a dark grey region.

Do anyone have any ideas?

Thanks

Teo

Stoltz's picture

Hi Teo !

I have the same problem. I cannot set the background color to any other color and the objects are displayed on a little part of the window. I used the ocaf appwizard. I'm a newbie and it's very frustrating that there is a bug before writing any code. Please let me know if you've found the solution.
Thanks

Philippe.

Zoltan Gaal's picture

I've been thinking on how to make an OpenCascade program mutithreaded, but had no idea. OC has its own reference counting memory manager. I have two threads using the same memory manager Both thread uses the shapes just for reading, but they still share the referenc counting objects and are modified from both threads. And as the reference counting is not thread safe, it might be corrupted. Thus i haven't even tried to use multythreading.

But please correct me if i'm wrong.

François Lauzon's picture

Hello Teo,
with Qt, there is only one thread that could perform GUI operation and it's the main thread, so that's why it's not working. Here is part of the Qt Documentation:

"In Qt, one thread is always the GUI or event thread. This is the thread that creates a QApplication object and calls QApplication::exec(). This is also the initial thread that calls main() at program start. This thread is the only thread that is allowed to perform GUI operations, including generating and receiving events from the window system. Qt does not support creating QApplication and running the event loop (with QApplication::exec()) in a secondary thread. You must create the QApplication object and call QApplication::exec() from the main() function in your program."

Zoltan,
usually, to protect data from multiple access and possible corruption, you use Mutex, Semaphore,...

Good Luck,
Francois.

Zoltan Gaal's picture

Hello.

I've already made multithreaded programs and used mutex. I just said, OCC is not prepared for multihreading. Eventhough you solve the QT problem, i don't see any solution to protect the reference counters in OCC (without changing the OC source a lot), and thus you cannot access the shapes from different threads.

In QT this might be solved as follows ( i think it's not exactly what you've expected): http://apsy.gse.uni-magdeburg.de/main/index.php?sec=1&page=hm_threadedcu...
Or search the net for: QT opengl rendering thread, but it requires some more work.

Udv: Zoli

François Lauzon's picture

> ... i don't see any solution to protect the reference
> counters in OCC (without changing the OC source a lot)

Why don't you use mutex to protect access to your shape, and to protect reference counting by the same time? That's what we are doing...