Skip to content

Commit 79bf48a

Browse files
authored
Update SkiaGLFWExample-v1.py
Copilot Copilot navigation Copilot Chat New conversation You said: https://github.com/HinTak/skia-python-examples/blob/main/SkiaGLFWExample-v1.py does not seems to behave correctly https://github.com/HinTak/skia-python-examples/blob/main/SkiaGLFWExample-v1.py does not seems to behave correctly on window resizing. Can you suggest a fix? 21 references Copilot said: The retrieved code from `SkiaGLFWExample-v1.py` and other The retrieved code from SkiaGLFWExample-v1.py and other related files reveals the current implementation of handling window resizing. The key function responsible for resizing is framebuffer_size_callback: Python def framebuffer_size_callback(window, width, height): state.window_width, state.window_height = width, height glViewport(0, 0, width, height) This function is linked to GLFW's framebuffer size callback mechanism, ensuring the viewport is updated when the window is resized. However, the Skia surface and rendering target are not updated. This could lead to issues when the window is resized because the Skia rendering context may not match the new dimensions. Suggested Fix: To ensure proper resizing, the Skia surface and rendering context should also be updated within the framebuffer_size_callback function. Here's an updated version: Python def framebuffer_size_callback(window, width, height): state.window_width, state.window_height = width, height glViewport(0, 0, width, height) # Recreate Skia render target for the new window size info = skia.GrGLFramebufferInfo(0, GL_RGBA8) kMsaaSampleCount = 0 kStencilBits = 8 target = skia.GrBackendRenderTarget( width, height, kMsaaSampleCount, kStencilBits, info ) props = skia.SurfaceProps(skia.SurfaceProps.kUseDeviceIndependentFonts_Flag, skia.PixelGeometry.kUnknown_PixelGeometry) surface = skia.Surface.MakeFromBackendRenderTarget( grContext, target, skia.kBottomLeft_GrSurfaceOrigin, skia.kRGBA_8888_ColorType, None, props ) assert surface is not None state.surface = surface state.canvas = surface.getCanvas() Integration Steps: Replace the existing framebuffer_size_callback function with the updated version above. Ensure the ApplicationState class has properties surface and canvas to hold the Skia surface and canvas objects. Update any references to the Skia canvas in the rendering loop to use state.canvas. With these changes, the application should handle window resizing correctly by updating both the OpenGL viewport and the Skia rendering context.
1 parent b85dfa8 commit 79bf48a

File tree

1 file changed

+21
-3
lines changed

1 file changed

+21
-3
lines changed

SkiaGLFWExample-v1.py

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,27 @@ def main():
9393
rect_start = (0, 0)
9494
rotation = 0
9595

96-
def framebuffer_size_callback(window, width, height):
97-
state.window_width, state.window_height = width, height
98-
glViewport(0, 0, width, height)
96+
def framebuffer_size_callback(window, width, height):
97+
state.window_width, state.window_height = width, height
98+
glViewport(0, 0, width, height)
99+
100+
# Recreate Skia render target for the new window size
101+
info = skia.GrGLFramebufferInfo(0, GL_RGBA8)
102+
kMsaaSampleCount = 0
103+
kStencilBits = 8
104+
target = skia.GrBackendRenderTarget(
105+
width, height, kMsaaSampleCount, kStencilBits, info
106+
)
107+
props = skia.SurfaceProps(skia.SurfaceProps.kUseDeviceIndependentFonts_Flag,
108+
skia.PixelGeometry.kUnknown_PixelGeometry)
109+
surface = skia.Surface.MakeFromBackendRenderTarget(
110+
grContext, target,
111+
skia.kBottomLeft_GrSurfaceOrigin,
112+
skia.kRGBA_8888_ColorType, None, props
113+
)
114+
assert surface is not None
115+
state.surface = surface
116+
state.canvas = surface.getCanvas()
99117

100118
glfw.set_framebuffer_size_callback(window, framebuffer_size_callback)
101119

0 commit comments

Comments
 (0)