Expand description
Pictures from the web: avatars, the images in notes, and the pictures across profiles.
A view asks the ImageStore for a picture by its address. If it is
there, the view gets it; if not, the view gets told it is loading, and the
store asks for it (see Requests): the app hears of it as a
Message::LoadImage. Only rows on
screen are ever built, so only pictures on screen are ever asked for.
An avatar is asked for as a ImageShape::Circle: cut to its middle
square and made round in its own pixels when it is decoded. The renderer
could round and crop it at draw time, but iced’s CPU renderer does neither
reliably for a picture that isn’t square. The picture across a profile is
asked for as a ImageShape::Strip and the one beside a link’s preview
as a ImageShape::Thumbnail, and they are cut the same way for the same
reason.
A picture is kept at the size it is shown at, in the screen’s own
pixels, and not as it came (see sized). What draws a frame is a CPU
renderer, and scaling a picture is the dearest thing it does: a
photograph of twelve million pixels, scaled into a row on every frame of
a scrolling list, costs more than everything else in the frame put
together. Scaled once, when it is decoded, it is copied into the frame
pixel for pixel (see Picture).
Nothing shows a picture larger than its row, so nothing is lost by it.
What a picture takes is bounded three ways: by that size, by a limit on how large one may decode before it is made smaller, and by a budget on the bytes kept, with the pictures least recently shown dropped first. A dropped picture is fetched again if it comes back into view; there is no disk cache yet.
Addresses come from whoever wrote a profile or a note. They are fetched
as everything is (see crate::http), and both the download and the
decoded picture are capped.
flowchart TD
url["An address, and the shape wanted"] --> fetch["Fetched: https only, so many bytes at the most"]
fetch --> decode["Decoded off the main thread, unless it would decode too large"]
decode --> shape{"The shape wanted"}
shape -->|"Original"| size
shape -->|"Circle"| circle["Cut to its middle square, and made round"]
shape -->|"Strip"| strip["Cut to its middle, three times as wide as tall"]
shape -->|"Thumbnail"| thumb["Cut to its middle, in a thumbnail's proportions"]
circle --> size
strip --> size
thumb --> size["Made the size it is shown at, in the screen's pixels"]
size --> keep["Kept, by address and shape"]
keep --> budget{"Over the budget of decoded bytes?"}
budget -->|"yes"| drop["The ones least recently shown are dropped, until it is met"]
budget -->|"no"| done["Shown"]
drop --> done| Limit | Value | Constant |
|---|---|---|
| One download | 16 MiB | MAX_DOWNLOAD_BYTES |
| One picture, decoded | 64 MiB | MAX_DECODED_BYTES |
| All pictures, as kept | 256 MiB | DEFAULT_BUDGET_BYTES |
| Fetches at once | 8 | MAX_IN_FLIGHT |
A picture that failed is remembered as failed for the run, and is not asked for again.
Structs§
- Entry 🔒
- Image
Request - A picture a view wants: where it is, and in what form. The same address in two forms is two pictures.
- Image
Store - The pictures loaded so far, by address and form. See the module docs.
- Loaded
Image - A picture ready to draw.
Enums§
- Image
Shape - The form a picture is wanted in.
- Image
State - What the store has to say about a picture.
Constants§
- DEFAULT_
BUDGET_ BYTES - How many decoded bytes the store keeps before dropping pictures.
- LARGEST_
AVATAR - How wide the largest avatar is shown, in points: the one on a profile.
- MAX_
DECODED_ 🔒BYTES - How large a picture may be once decoded (four bytes a pixel): 4096×4096.
- MAX_
DOWNLOAD_ 🔒BYTES - How large a picture may be to download.
- MAX_
IN_ 🔒FLIGHT - How many pictures may be on their way at once.
- REQUEST_
QUEUE 🔒 - How many requests may wait between a view asking and the app answering. A request that doesn’t fit is dropped: a picture still on screen is asked for again the next time the view is built.
- SCALE_
UNKNOWN - How many of the screen’s pixels a point is taken to be while the screen has not said: more than most are, so that a picture made for it is sharp on any. It is then scaled as it is drawn, which costs what sizing it is there to save, until the screen has answered.
- STRIP_
ASPECT - How many times as wide as it is tall a
ImageShape::Stripis: what such pictures are made for. - THUMBNAIL_
ASPECT - How wide a
ImageShape::Thumbnailis for how tall: near what pages make the pictures they are shared with. - WIDEST_
THUMBNAIL 🔒 - How wide the picture beside a link’s preview is shown at the most, in points. It is as wide as its card is tall allows, which goes by the size text is set in: this is past what any size makes it.
Functions§
- as_png
- The picture
bytesare, as a PNG, which is what a picture on a clipboard is taken as everywhere: itself, if it is one already; otherwise decoded, turned the way up it says it is, and encoded. An animated picture is its first frame. - circle 🔒
- The middle square of
pixels, with everything outside the circle that fits it made transparent, and the circle’s edge softened over one pixel. - decode 🔒
- Decode
bytesinto pixels in the formshape, at the size they are shown at on a screen ofscalepixels to the point, refusing a picture that would decode larger thanmax_decoded_bytes. The format is told from the bytes themselves, not from the address or what the server called it. - encoded_
png 🔒 pictureas a PNG, written quickly rather than small: it is on its way to a clipboard, not to a disk.- fetch
- Fetch and decode the picture
requestasks for, for a screen ofscalepixels to the point. - fetch_
as_ png - What is at
url, a picture, as a PNG (seeas_png). - kept_
size 🔒 - How many pixels a picture of
widthbyheightpixels, of the shapeshape, is kept at on a screen ofscalepixels to the point: as many as it takes where it is shown, in its own proportions. - middle 🔒
- The middle of
pixels,acrosswide for everydowntall: the sides cut off a picture that is wider than that, the top and bottom off one that is taller. - shown_
as_ png - The picture
loadedshows, as a PNG: the pixels kept of it, which are as many as it takes where it is shown, and may be fewer than it has. - sized 🔒
pixels, a picture of the shapeshape, made the size it is shown at on a screen ofscalepixels to the point (seekept_size).