• 0

WPF rendering with OpenGL in another thread


Question

I'm trying to make a video player in WPF, using libavcodec for decoding and OpenGL for rendering.

I host my own C++/CLI control derived from Windows.Forms.UserControl inside a WindowsFormsHost. I use its Handle to initialize an OpenGL context.

Here is the code for my override of OnPaint:

wglMakeCurrent(m_hDC, m_hRC);
glClearColor(1.0f, 0.0f, 0.0f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
SwapBuffers(m_hDC);

This works fine. When OnPaint is called, the control, which covers the whole window, is cleared to red.

Now, WPF controls when OnPaint is called, not me. When I'll be rendering video, I need to be able to draw to the screen when I decide to. So I've put the above code in an infinite loop and made it execute on a BackgroundWorker thread. Now, the problem is that wglMakeCurrent fails in this situation with"error 170: the requested resource is in use". Consequently, the call to glClear() (and any further rendering) does nothing.

I don't understand exactly which resource is in use and why, and I'd like to know what's wrong with my approach.

Thanks!

P.S. here is the initialization code in case this could be useful. It is hooked to the "Load" event of the control. And yes this is essentially copy-pasted from a tutorial on the net.

m_hWnd	= (HWND) this->Handle.ToPointer();

			if(m_hWnd)
			{
				m_hDC = GetDC(m_hWnd);
				if(!m_hDC)
				{
					Helper::ErrorExit(L"BuildWindowCore");
				}
				uint PixelFormat;

				BYTE iAlphaBits = 0;
				BYTE iColorBits = 32;
				BYTE iDepthBits = 16;
				BYTE iAccumBits = 0;
				BYTE iStencilBits = 0;

				static PIXELFORMATDESCRIPTOR pfd = 
				{
					sizeof(PIXELFORMATDESCRIPTOR),	//size
					1,								//version
					PFD_DRAW_TO_WINDOW|				//flags
					PFD_SUPPORT_OPENGL|
					PFD_DOUBLEBUFFER,
					PFD_TYPE_RGBA,					//pixeltype
					iColorBits,
					0, 0, 0, 0, 0, 0,				//color bits ignored
					iAlphaBits,						
					0,								//alpha shift ignored
					iAccumBits,						//accum. buffer
					0, 0, 0, 0,						//accum bits ignored
					iDepthBits,						//depth buffer
					iStencilBits,					//stencil buffer
					0,								//aux buffer
					PFD_MAIN_PLANE,					//layer type
					0,								//reserved
					0, 0, 0							//masks ignored
				};

				PixelFormat = ChoosePixelFormat(m_hDC, &pfd);
				if(!PixelFormat)
				{
					Helper::ErrorExit(L"BuildWindowCore");
				}

				if(!SetPixelFormat(m_hDC, PixelFormat, &pfd))
				{
					Helper::ErrorExit(L"BuildWindowCore");
				}

				m_hRC = wglCreateContext(m_hDC);
				if(!m_hRC)
				{
					Helper::ErrorExit(L"BuildWindowCore");
				}

				if(!wglMakeCurrent(m_hDC, m_hRC))
				{
					Helper::ErrorExit(L"BuildWindowCore");
				}

1 answer to this question

Recommended Posts

  • 0

I'm guessing I am not allowed to update it on a thread other than the UI thread, since it is part of the UI. But I don't want playing video to block the UI. So, I need to implement a scheme where I render one frame at a time, using something like a DispatcherTimer. Am I on the right track?

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • Amazon Deals: Samsung Q990F Q900F Q800F 2025 Dolby Atmos soundbars with wireless subwoofers by Sayan Sen While separate AV receivers with hi-fi speakers are generally the preferred way to listed to music and watch movies/shows by audiophiles, the more general folks often prefer soundbars instead as they offer a capable all in one solution that is still plenty good. Currently Nakamichi is running discounts on multiple products from its Dragaon lineup as well as its Shockwafe model. If you are looking for more options to choose from Samsung has its Q-series products at lowest prices (purchase links towards the end of article). Q990F The flagship Q990F is an 11.1.4 system and the single subwoofer unit on it houses two opposite-facing 8-inch subwoofer drivers. Thus, together they move around the same amount of air as a single 12-inch subwoofer unit. In addition to increasing the bass by +3 dB, dual opposing drivers are also said to help reduce vibrations of the subwoofer cabinet by cancelling out the resonance. Samsung also says that the bass is "AI-optimized" but we are not sure if it actually helps or if it's just a buzz term here. Aside from the bass, dialogue in movies is the second most important thing, and Samsung claims great vocal clarity from its front speakers thanks to AVA (Active Voice Amplifier) Pro feature that is said to detect noise disturbances and amplify dialogue to make it more audible over such surrounding noises. Q900F Feature wise the Q900F is similar to the 990F model except it has fewer channels and it is a 7.1.2 setup. Finally the Q800F is a 5.1.2 system and it has a passive radiator instead of the additonal subwoofer driver unit. Get the Samsung Q series soundbars at the links below: Samsung Q990F 11.1.4ch Wireless Dolby Atmos, Q-Symphony, Game Mode Pro, Adaptive Sound (HW-Q990F, 2025): $1497.99 (Shipped and Sold by Amazon US) Samsung HW-Q900F 7.1.2 ch Wireless Dolby Atmos, Q-Symphony: $997.99 (Shipped and Sold by Amazon US) Samsung Q800F 5.1.2ch Q Series Soundbar + Subwoofer, Wireless Dolby Atmos, Q-Symphony, Game Mode Pro, Smart Integration (HW-Q800F, 2025): $697.99 (Shipped and Sold by Amazon US) This Amazon deal is US-specific and not available in other regions unless specified. If you don't like it or want to look at more options, check out the Amazon US deals page here. Get Prime (SNAP), Prime Video, Audible Plus or Kindle / Music Unlimited. Free for 30 days. As an Amazon Associate, we earn from qualifying purchases.
    • They are shifting into AI now. Don't you see?
    • Exactly. No need to pay to rent a license. I'd rather own it.
  • Recent Achievements

    • One Month Later
      Helen Shafer earned a badge
      One Month Later
    • One Month Later
      ambani880 earned a badge
      One Month Later
    • Week One Done
      ambani880 earned a badge
      Week One Done
    • First Post
      artistro08 earned a badge
      First Post
    • First Post
      paul29 earned a badge
      First Post
  • Popular Contributors

    1. 1
      +primortal
      498
    2. 2
      ATLien_0
      223
    3. 3
      Michael Scrip
      196
    4. 4
      Xenon
      160
    5. 5
      +FloatingFatMan
      138
  • Tell a friend

    Love Neowin? Tell a friend!