Skip to content

OpenGL Widget: Proof of concept#4263

Closed
corranwebster wants to merge 5 commits intobeeware:mainfrom
corranwebster:opengl-widget
Closed

OpenGL Widget: Proof of concept#4263
corranwebster wants to merge 5 commits intobeeware:mainfrom
corranwebster:opengl-widget

Conversation

@corranwebster
Copy link
Copy Markdown
Contributor

@corranwebster corranwebster commented Mar 21, 2026

This is a first cut at seeing what an OpenGLView widget might look like. Goals for this are:

  • to put something out there so people can try it out
  • to work out how to get an OpenGL view up and running on each platform and do something trivial
  • to start experimenting with the interface to see what that would look like as part of the design process

Currently supports:

  • Android
  • Cocoa
  • Gtk
  • iOS
  • Qt
  • Web
  • Winforms

Ref #51

PR Checklist:

  • All new features have been tested
  • All new features have been documented
  • I have read the CONTRIBUTING.md file
  • I will abide by the code of conduct

Comment thread examples/opengl/README.md
@@ -0,0 +1,19 @@
# Button
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Flagging that this file needs updating


def callback_render(self, widget, context, **kwargs):
context.clear_color(1.0, 1.0, 0.0, 1.0)
context.clear(context.COLOR_BUFFER_BIT)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we have an OpenGL example app that doesn't draw a teapot, I'm going to be very disappointed... 😝

More seriously - I presume this is "first light" for the API, rather than anything close to a final API?

Copy link
Copy Markdown
Contributor Author

@corranwebster corranwebster Mar 23, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, very much - this is in the 'investigation' phase - the focus so far is on "what does it involve to get an OpenGL view on various platforms", which I think I've mostly got a handle on, so I'm now moving on to "what are the differences/commonalities between the platform OpenGL implementations."

I've been making notes and recording thoughts in #51.

Edit: I'm at the "how do I draw a cross-platform triangle" stage. But yes, there should be a teapot at some point.

@corranwebster
Copy link
Copy Markdown
Contributor Author

I've run into a serious problem:

  • testbed tests do not run in the main thread
  • OpenGL widgets on both MacOS and Qt do not like this (segfaults on both platforms in tests). The Qt error explicitly says "Cannot make QOpenGLContext current in a different thread"

I don't think we can test OpenGL stuff using testbed.

@freakboy3742
Copy link
Copy Markdown
Member

I've run into a serious problem:

  • testbed tests do not run in the main thread
  • OpenGL widgets on both MacOS and Qt do not like this (segfaults on both platforms in tests). The Qt error explicitly says "Cannot make QOpenGLContext current in a different thread"

Hrm... the test suite executes in a separate thread, but the actual app should be entirely on the main thread, and the way pytest-asyncio operates, all the test cases should be "run_coroutine_threadsafe" on the main event loop...

@corranwebster
Copy link
Copy Markdown
Contributor Author

corranwebster commented Mar 27, 2026

Hrm... the test suite executes in a separate thread, but the actual app should be entirely on the main thread, and the way pytest-asyncio operates, all the test cases should be "run_coroutine_threadsafe" on the main event loop...

A little bit more fiddling, and it's the fixtures which are the problem. In particular, the widget fixtures create the widget off of the main thread and then it seems like they get passed to the main thread as parameters of the actual test.

By avoiding passing the widget in as a fixture I can stop the segfaults. Still getting test failures on cocoa, but not hard crashes.

Edit: and if I make the widget fixture async that seems to fix the problem as well - I guess an async fixture defers the creation and gets run on the main thread. It all feels a bit fragile, but hopefully it'll work now.

@corranwebster
Copy link
Copy Markdown
Contributor Author

I'm going to close this in favour of #4279. There are things in this branch which may eventually make their way into Toga, but I think it needs to be done in more organised steps.

@johnzhou721
Copy link
Copy Markdown
Contributor

Hrm... the test suite executes in a separate thread, but the actual app should be entirely on the main thread, and the way pytest-asyncio operates, all the test cases should be "run_coroutine_threadsafe" on the main event loop...

A little bit more fiddling, and it's the fixtures which are the problem. In particular, the widget fixtures create the widget off of the main thread and then it seems like they get passed to the main thread as parameters of the actual test.

By avoiding passing the widget in as a fixture I can stop the segfaults. Still getting test failures on cocoa, but not hard crashes.

Edit: and if I make the widget fixture async that seems to fix the problem as well - I guess an async fixture defers the creation and gets run on the main thread. It all feels a bit fragile, but hopefully it'll work now.

Yes this was my advice on the Qt tree view issue. async functions are ran — the way it happens is it’s created as a task, which is explicitly forwarded by us to main thread in our testing setup.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants