plasmashell: cap the scene graph's texture atlas

Original commit message(s)

  • plasmashell: cap the scene graph's texture atlas

Qt sizes the atlas from the window whose render context creates it, rounding each side up to a power of two, so the desktop's full screen window reserves a screen sized sheet: 4096x4096, 64 MB, to hold small images. A shell puts icons in there. There is one such sheet per window, and Qt never resizes it, so nothing recovers it either.

Capping it to 1024x1024 leaves 4 MB a window. An image which no longer fits the sheet gets a texture of its own instead of being batched with the others, which is what Qt already does once a sheet is full, so the fallback is the ordinary path rather than a new one. The inclusion limit follows the sheet, half its side by default, so images below 512 pixels still batch, which covers every icon a panel or a desktop draws.

Measured on plasmashell over the same actions, reading resident less purgeable from the drm accounting the way libksysguard does, on an integrated Intel graphics with a 4K screen:

as it comes 347.3 MB idle, atlas sheets up to 4096x4096 1024x1024 269.9 MB idle 512x512 261.2 MB idle

So 77 MB less at idle, and the 9 MB that 512 would add comes with an inclusion limit of 256 pixels, which starts leaving ordinary icons out of the batch.

The environment is set on the unit rather than in startplasma, so it applies to the shell alone and not to every application started in the session, some of which do want a large sheet.

Reason

plasmashell reserves graphics memory in proportion to the screen, for holding icons. Capping it saves 74 MB at idle on this machine, and 10 MB of resident memory with it.

Where it comes from

Qt Quick keeps a texture atlas per render context, a sheet that small images are packed into so they can be drawn in one batch instead of one draw call each. The sheet's size is not chosen from what goes into it but from the window it belongs to, qsgrhiatlastexture.cpp:

const int widthHint  = qMax(1, surfacePixelSize.width());
const int heightHint = qMax(1, surfacePixelSize.height());
int w = qMin(maxSize, qt_sg_envInt("QSG_ATLAS_WIDTH",  qMax(512U, qNextPowerOfTwo(widthHint  - 1))));
int h = qMin(maxSize, qt_sg_envInt("QSG_ATLAS_HEIGHT", qMax(512U, qNextPowerOfTwo(heightHint - 1))));
m_atlas_size_limit = qt_sg_envInt("QSG_ATLAS_SIZE_LIMIT", qMax(w, h) / 2);

Each side is rounded up to a power of two, so a full screen window reserves a screen sized sheet. The shell's windows produce, on this machine:

rhi texture atlas dimensions: 4096x4096      the desktop, 64 MB
rhi texture atlas dimensions: 2048x2048      16 MB
rhi texture atlas dimensions: 1024x4096      16 MB
rhi texture atlas dimensions: 512x4096        8 MB
rhi texture atlas dimensions: 512x512         1 MB

That is 105 MB of sheets for drawing icons, on a process whose whole graphics figure sat around 350 MB. The heuristic dates to Qt 5.2 and has never been revisited for a program which opens many windows, which is what a shell is: an application with one window per screen and one per panel and popup pays for a screen sized sheet each time.

What the cap changes

QSG_ATLAS_WIDTH=1024 and QSG_ATLAS_HEIGHT=1024 on the unit, so 4 MB a sheet.

The one behavioural consequence is the inclusion limit, which follows the sheet at half its side: images under 512 pixels still get batched, where before it was anything under 2048. An image which does not fit the sheet gets a texture of its own and is drawn on its own, which is exactly what Qt already does when a sheet fills up, so this is the ordinary fallback rather than a new path. Nothing a panel or a desktop draws is anywhere near 512 pixels, and a wallpaper was never a candidate.

Qt never grows or shrinks a sheet, so nothing recovers this memory later:

Texture *Atlas::create(const QImage &image) {
    QRect rect = m_allocator.allocate(QSize(image.width() + 2, image.height() + 2));
    if (rect.width() > 0 && rect.height() > 0) { ... }
    return nullptr;   // no room: the caller makes a standalone texture
}

Measured

The graphics memory the kernel accounts to the process, resident less purgeable, which is how libksysguard computes what plasma-systemmonitor shows, on integrated Intel graphics:

graphics memory at idle resident memory sheets
as it comes 349.5 MB 404.2 MB up to 4096x4096
1024x1024 275.0 MB 393.9 MB four of 1024x1024
512x512 261.2 MB

Both readings were taken with the shell restarted by systemd through this unit, and the value read back out of /proc/PID/environ to be sure it had arrived, since an earlier attempt measured a hand started shell which the unit did not own and reported no change at all.

512x512 would add 14 MB, at an inclusion limit of 256 pixels, which starts leaving ordinary icons out of the batch. 2048x2048 would be the conservative choice, 16 MB a sheet and a 1024 pixel limit, saving about 50 MB instead of 74. I went with 1024 because nothing in the shell is near the limit it implies, but I am happy to change it.

Worth deciding in review

  • Whether to raise it upstream too. The heuristic is a guess made once in 2013 and the three environment variables exist because of that. A program with many windows is a case Qt could handle better than by each context reserving a screen's worth. This change stands on its own either way.
  • Whether 1024 or 2048 is the value to ship, per the numbers above.
Edited by Méven Car

Merge request reports

Loading
Loading