-
Notifications
You must be signed in to change notification settings - Fork 0
Commit 60a2dab
authored
Update SkiaGLFWExample-v4.py
Copilot wasn't entirely correct.
Skip to content
Navigation Menu
Copilot
Copilot navigation
Copilot Chat
New conversation
You said: https://github.com/HinTak/skia-python-examples/blob/main/SkiaGLFWExample-v4.py does not seems to behave correctly
https://github.com/HinTak/skia-python-examples/blob/main/SkiaGLFWExample-v4.py does not seems to behave correctly on window resizing. Can you suggest a fix?
2/2
1 reference
Copilot said: The issue with window resizing lies in
The issue with window resizing lies in the fact that the GrBackendRenderTarget and associated Surface for Skia are not updated when the window size changes. While the OpenGL viewport is resized using glViewport() in the on_window_size callback, the Skia rendering target is not re-created to match the new size, which results in rendering issues.
Here’s how you can fix this:
Update the on_window_size callback to re-create the GrBackendRenderTarget and Surface whenever the window size changes.
Add logic to properly clean up the old surface before creating the new one.
Here's the modified on_window_size callback and additional changes:
Python
def on_window_size(window, w, h):
state.window_width = w
state.window_height = h
glViewport(0, 0, w, h)
# Recreate Skia surface
global surface, canvas, grContext, target
grContext.reset() # Reset the context to ensure no lingering states
target = GrBackendRenderTarget(w, h, kMsaaSampleCount, kStencilBits, GrGLFramebufferInfo(0, fFormat))
surface = Surface.MakeFromBackendRenderTarget(grContext, target, kBottomLeft_GrSurfaceOrigin, colorType, None, props)
if surface is None:
print("Failed to recreate Skia surface")
return
canvas = surface.getCanvas()
Explanation of Changes
Re-create GrBackendRenderTarget: When the window is resized, a new backend render target is created with updated dimensions (w and h).
Re-create Surface: The Skia surface is recreated using the new render target.
Reset Context: The Skia GrDirectContext is reset to avoid any lingering states from the previous surface.
Update Global Variables: The new surface and canvas are updated globally, so they are used in rendering.
Integration
Replace the existing on_window_size callback with the updated one, and ensure global variables like surface, canvas, grContext, and target are declared at the top of the script.
With these updates, the application should handle window resizing properly without rendering artifacts or errors.
You said: Your suggested change has a problem at
Your suggested change has a problem at the "grContext.reset()" line. The error is "NameError: name 'grContext' is not refined" on resize. Can you fix it?
Copilot said: The issue occurs because `grContext` is not
The issue occurs because grContext is not globally declared or defined properly before being referenced in the on_window_size callback. Let's refine the implementation to ensure that grContext and other necessary global variables are properly initialized and accessible.
Here's the corrected implementation:
SkiaGLFWExample-v4.py
def on_window_size(window, w, h):
state.window_width = w
state.window_height = h
glViewport(0, 0, w, h)
# Recreate Skia surface
Key Fixes:
Global Variables Declaration: Ensure all variables used for surface recreation (grContext, target, fFormat, colorType, and props) are declared as global at the beginning of the on_window_size function.
Remove grContext.reset(): The reset() method is not suitable here since the grContext object is already initialized and does not need resetting. Instead, directly use the existing grContext.
Context:
grContext is created during the initial setup in the main() function and remains valid as long as the application is running.
The on_window_size function now correctly reuses the grContext and other necessary objects to recreate the Skia Surface with the new window dimensions.
Integration:
Replace the existing on_window_size callback with this updated implementation, and ensure other parts of the code (like the global declarations) are consistent with this fix.
With this correction, the resizing issue should be resolved without any NameError.
Ask Copilot
Copilot said: The issue occurs because `grContext` is not The issue occurs because grContext is not globally declared or defined properly before being referenced in the on_window_size callback. Let's refine the implementation to ensure that grContext and other necessary global variables are properly initialized and accessible. Here's the corrected implementation: SkiaGLFWExample-v4.py def on_window_size(window, w, h): state.window_width = w state.window_height = h glViewport(0, 0, w, h) # Recreate Skia surface Key Fixes: Global Variables Declaration: Ensure all variables used for surface recreation (grContext, target, fFormat, colorType, and props) are declared as global at the beginning of the on_window_size function. Remove grContext.reset(): The reset() method is not suitable here since the grContext object is already initialized and does not need resetting. Instead, directly use the existing grContext. Context: grContext is created during the initial setup in the main() function and remains valid as long as the application is running. The on_window_size function now correctly reuses the grContext and other necessary objects to recreate the Skia Surface with the new window dimensions. Integration: Replace the existing on_window_size callback with this updated implementation, and ensure other parts of the code (like the global declarations) are consistent with this fix. With this correction, the resizing issue should be resolved without any NameError.1 parent 7a39f07 commit 60a2dabCopy full SHA for 60a2dab
1 file changed
+1
-1
lines changed+1-1
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
6 | 6 |
| |
7 | 7 |
| |
8 | 8 |
| |
9 |
| - | |
| 9 | + | |
10 | 10 |
| |
11 | 11 |
| |
12 | 12 |
| |
|
0 commit comments