Twinside / Rasterific

A drawing engine in Haskell
BSD 3-Clause "New" or "Revised" License
140 stars 11 forks source link

Provide a `Graphics.Rasterific.Immediate.runDrawContextOnIOVector` API #51

Closed RaminHAL9001 closed 5 years ago

RaminHAL9001 commented 5 years ago

I would like it if I could allocate my own MutableImage with newMutableImage and retain a copy of the mutable image between calls to runDrawContext. This will allow me to allocate an IOVector within my Haskell program that I can pass to a low-level C language API just one time. This would also allow users of this library to manage their own memory, without being forced to allocate+freeze an image after every rendering step, taking pressure off of the allocator and garbage collector.

So I propose a runDrawContextOnIOVector function which takes a MutableImage which stores an IOVector internally, and basically works the same as runDrawContext but does not freeze the vector, rather it simply updates the IOVector in place.

I may be able to write this code myself, if I have time, and submit a pull request. But I thought I would open an issue first to see if you would be interested in including such a feature. I am not sure if the types in the JuicyPixels library would allow for this feature as I described it, but I can investigate.

RaminHAL9001 commented 5 years ago

Sorry, I accidentally created this issue,

RaminHAL9001 commented 5 years ago

I have edited and re-opened this issue. Sorry about the confusion.

Twinside commented 5 years ago

The function you seek is execStateT, runStateT or evalStateT. runDrawContext is just a convenience function that allocate and freeze the image, if you don't need theses steps, just execute the monad transformer.

For context, the current implementation of runDrawContext:

runDrawContext width height background drawing = do
  buff <- M.new (width * height * componentCount background)
  let mutable = MutableImage width height buff
  fillImageWith mutable background
  img <- execStateT drawing mutable
  unsafeFreezeImage img