Remove opaque Pixels type; add safe updateTexture and zero-copy updateTextureFromSurface
The opaque Pixels type wrapped a void* pointing into an SDL_Surface's pixel buffer, but nothing tied the Pixels lifetime to the surface. If the GC collected the surface first, the Pixels pointer would dangle, causing use-after-free. Replace with two safe alternatives: - updateTexture now takes a ByteArray (Lean-owned, GC-managed buffer) plus pitch, matching how Haskell's sdl2 uses ByteString and Rust's rust-sdl2 uses &[u8]. This is faithful to SDL_UpdateTexture's general void* interface but requires copying pixels into a ByteArray first. - updateTextureFromSurface is a new zero-copy convenience that takes an SDLSurface directly. The C side extracts both pixels and pitch from the surface internally. Because the surface is borrowed (@&) for the call's duration, the GC cannot collect it mid-use. This is not strictly faithful to SDL_UpdateTexture's C API, but is the common case and avoids an unnecessary memcpy. Also removes: Pixels type, Pixels.nonemptyType, sdl_pixels_external_class, sdl_Surface_get_pixels, and the pixels_external_class registration in sdl_init. WebcamApp.lean is updated to use updateTextureFromSurface. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
1 parent
3ec33e9a27
commit
1830995cc5
3 files changed
+27
-29
No files matched your search
@@ -132,10 +132,6 @@ instance SDLSurface.instNonempty : Nonempty SDLSurface := SDLSurface.nonemptyTyp
|
||||
|
||||
namespace SDLSurface
|
||||
|
||||
private opaque Pixels.nonemptyType : NonemptyType
|
||||
def Pixels: Type := Pixels.nonemptyType.type
|
||||
instance PIxels.instNonempty : Nonempty Pixels := Pixels.nonemptyType.property
|
||||
|
||||
@[extern "sdl_Surface_get_format"]
|
||||
opaque format : @& SDLSurface -> UInt32
|
||||
|
||||
@@ -145,9 +141,6 @@ opaque w : @& SDLSurface -> Int32
|
||||
@[extern "sdl_Surface_get_h"]
|
||||
opaque h : @& SDLSurface -> Int32
|
||||
|
||||
@[extern "sdl_Surface_get_pixels"]
|
||||
opaque pixels : @& SDLSurface -> Pixels
|
||||
|
||||
@[extern "sdl_Surface_get_pitch"]
|
||||
opaque pitch : @& SDLSurface -> Int32
|
||||
|
||||
@@ -167,7 +160,16 @@ opaque createTextureFromSurface
|
||||
|
||||
-- TODO handle SDLRect argument
|
||||
@[extern "sdl_update_texture"]
|
||||
opaque updateTexture (texture: @& SDLTexture) (pixels: @& SDLSurface.Pixels) (pitch: Int32): SDLIO Bool
|
||||
opaque updateTexture (texture: @& SDLTexture) (pixels: @& ByteArray) (pitch: Int32): SDLIO Bool
|
||||
|
||||
-- Zero-copy optimization: updates a texture directly from an SDL_Surface's
|
||||
-- pixel buffer, avoiding the intermediate ByteArray copy required by
|
||||
-- updateTexture. This is not strictly faithful to the SDL_UpdateTexture C API
|
||||
-- (which takes a generic void* for pixels), but is safe because the surface
|
||||
-- is borrowed (@&) for the duration of the call, preventing the GC from
|
||||
-- collecting the surface (and its pixel data) while the update is in progress.
|
||||
@[extern "sdl_update_texture_from_surface"]
|
||||
opaque updateTextureFromSurface (texture: @& SDLTexture) (surface: @& SDLSurface): SDLIO Bool
|
||||
|
||||
|
||||
def loadImageTexture
|
||||
|
||||
Reference in new issue
Block a user