I wrote a bit about "jank" in JUCE

I had assumed that when a CachedImageComponent is drawn, it would only draw the portion of the image that overlaps with the clip region of the graphics context.

Looking at the implementation of StandardCachedImageComponent:

That doesn’t seem to be the case… meaning if you only needed to draw a single pixel of a cached image, it would draw the entire image to the context, most of which would be ignored.

Unless I’m missing something, there’s an optimisation to be made there because drawing images is expensive so only drawing as few pixels as possible could be a huge improvement in some cases.

E.g. you could do something like:

const auto clipBounds = g.getClipBounds();
const auto areaToDraw = imageBounds.getIntersection (clipBounds);
g.drawImage (image,
             clipBounds.getX(), clipBounds.getY(), clipBounds.getWidth(), clipBounds.getHeight(),
             areaToDraw.getX(), areaToDraw.getY(), areaToDraw.getWidth(), areaToDraw().getHeight(),
             true);

Maybe this is already handled within the graphics implementation of drawing an image? I don’t know.

You can actually supply your own juce::CachedComponentImage to a juce::Component where you can customise the behaviour of invalidating and updating the cache, if you really wanted.

3 Likes