Best practices for working with large bitmaps

482 views
Skip to first unread message

Roman Petrenko

unread,
Feb 3, 2022, 6:57:58 PM2/3/22
to skia-discuss
Hi all,

I'm using Skia on my project to generate PNG and JPEG files, and sometimes they are very large - up to 45000*45000 pixels. Currently, I'm using `SkSurface::MakeRasterDirect` to create the surface to draw on. If the image size is larger that 20000-25000 pixels in one dimension (depends on the amount of free memory I have), the surface creation fails on my laptop. Are there any commonly used workarounds for this?

Thanks.

bungeman

unread,
Feb 4, 2022, 10:48:18 AM2/4/22
to skia-discuss
Usually one would do tiling. Either to draw only the parts that are currently seen, or to create the tiles and then have a way to stitch them together when creating one huge image at the end. For some example code of how this may be done you can take a look at where Skia tiles in the Viewer test application [0]. This doesn't quite do what you want, since it doesn't actually lazily create the backing for the tiles, but it does show how to break up your drawing into different parts, each of which could be targeted to a separate tile. We do have a few users that have used Skia with their own tiling management for similar reasons (with potentially very large output which doesn't all need to be in memory all the time), so this wouldn't be the first time someone has tried this. There may be other possible solutions as well.

That being said, 45000x45000 in 8bit per channel RGBA is getting up to around 8GB. Note that there is currently a hard limit of INT32_MAX of any given dimension, but it doesn't look like you're running into that.

Brian Osman

unread,
Feb 4, 2022, 10:59:01 AM2/4/22
to skia-d...@googlegroups.com
FYI: I actually made this slightly more difficult recently, when I limited the size of surfaces to 2GB, total. This was required to limit undefined behavior and other (similar) kinds of bugs.


--
You received this message because you are subscribed to the Google Groups "skia-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to skia-discuss...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/skia-discuss/725998aa-28e6-454e-a11b-ab00deec083cn%40googlegroups.com.

Roman Petrenko

unread,
Feb 6, 2022, 8:11:50 PM2/6/22
to skia-discuss
Thanks, I'll look into implementing tiling in my project.
Reply all
Reply to author
Forward
0 new messages