I think one ugly thing is that Compose for Desktop...
# compose-desktop
s
I think one ugly thing is that Compose for Desktop
Image.makeFromEncoded()
as well as Android
contentResolver.loadThumbnail()
give you a
Bitmap
, but Compose Image needs an
ImageBitmap
and as far as I can see the implementation of the extension
asImageBitmap()
is on both worlds a really expensive conversation. 😞 Why isn't there a way to directly load an ImageBitmap if that's the needed format? 🤔
1
r
What’s expensive about it? The Android implementation just wraps the 
Bitmap
 inside an 
ImageBitmap
I don’t know how the desktop implementation works but I assume it’s something similar
s
Dunno.. looks expensive.
Copy code
targetBitmap.getPixels(
    buffer,
    bufferOffset,
    stride,
    startX,
    startY,
    width,
    height
)
for Android
for Desktop Compose:
Copy code
val bytes = bitmap.readPixels(imageInfo, stride * bytesPerPixel.toLong(), startX, startY)!!

ByteBuffer.wrap(bytes)
    .order(ByteOrder.LITTLE_ENDIAN) // to return ARGB
    .asIntBuffer()
    .get(buffer, bufferOffset, bytes.size / bytesPerPixel)
So there is this "read all pixels into a new int/byte array" step which doubles the amount of data. Doesn't it?
And this is executed everytime a call to
readPixels()
, the main method of that
BitmapImage
interface, happens
I assume that means some extra GC on every image draw operation. Doesn't it?
I wonder if there is a
Painter
(the third possible argument beside
ImageBitmap
and
ImageVector
for a Compose
Image
) implementation that can draw a
Bitmap
directly without making that call to
readPixels()
that will create an extra array on every call.
looking at accompanist for Coil and Glide they have
rememberGlidePainter()
which I guess is a way around that. Only that there is no such thing as Glide or Coil available for Compose for Desktop... or is it?
r
For Android
getPixel()
is only invoked from
readPixels()
, which is not used for rendering
👍 1
I assume the same is true on desktop
👍 1
a
Yes, on both Android and desktop
Bitmap.asImageBitmap()
just wraps the bitmap with a new class without doing anything else.