Unnecessary paints when parent is setPaintingIsUnclipped


#1

I have 2 components : A and its child AA, both are opaque and setPaintingIsUnclipped (true).

Everytime AA is repainted, the parent A is also repainted. Any reason for that?
(If the parent is not setPaintingIsUnclipped, everything is ok)

here is a simple code to reproduce (basic MainComponent for a projucer generated gui app) tested on 10.12

#ifndef MAINCOMPONENT_H_INCLUDED
#define MAINCOMPONENT_H_INCLUDED

#include "../JuceLibraryCode/JuceHeader.h"

class ChildComponent : public Component, public Timer
{
public:
    ChildComponent()
    {
        setPaintingIsUnclipped (true);
        setOpaque (true);
        startTimerHz (4);
    }

    void paint (Graphics& g) override
    {
        g.setColour (Colours::red);
        g.fillRect (getLocalBounds());
        DBG ("child paint()");
    }

    void timerCallback() override
    {
        repaint();
    }
};


class MainContentComponent   : public Component
{
public:
    MainContentComponent()
    {
        setPaintingIsUnclipped (true);  // everything is ok if you remove this line
        setOpaque (true);
        addAndMakeVisible (child);
        setSize (600, 400);
    }

    void paint (Graphics& g) override
    {
        g.setColour (Colours::green);
        g.fillRect (getLocalBounds());
        DBG ("parent paint()");
    }

    void resized() override
    {
        child.setBounds (getLocalBounds().reduced (10));
    }

private:
    ChildComponent child;
};

#endif  // MAINCOMPONENT_H_INCLUDED

#2

Is any-scaling or transformation involved, if yes, keep in mind that the boundaries could be on sub-pixel positions, which result in a mix of the background and foreground component on the boundary pixel positions.

If not, try to debug and see whats going on


#3

also you may have a second redraw(dirty) region, on some other position, which on mac will be summarized to one big redraw region, which may intersects with areas which don’t have to be redrawed (you can use the JUCE_COREGRAPHICS_RENDER_WITH_MULTIPLE_PAINT_CALLS, to change this behavior)


#4

I’m on retina. I don’t have anything else that those 2 components, and I have this behaviour whatever the child bounds, even if it has the same bounds as the parent.


#5

standalone or plugin?, with some host/wrapper combination dirty regions are on the top all the time

If not, debug and see whats going on


#6

standalone (example code is above, I tested it within a projucer generated gui app).
I don’t have much time to look further into that at the moment unfortunately.
But I already wanted to know if it was really not expected or if I was missing something.


#7

So this is because in paintComponentAndChildren, clipObscuredRegions() is not called if dontClipGraphicsFlag is true.
As a consequence, if a component is setPaintingIsUnclipped(), it will always get repainted whenever its child is repainted, even if it’s unnecessary.

shouldn’t clipObscuredRegions() be called even if dontClipGraphicsFlag is true?


#8

or not? :slight_smile:


#9

(gentle bump). could this be fixed?


#10

We’ll have a look, but it’s fairly low priority for us, and actually may turn out to just be a case which we choose not to optimise. To do a 100% perfect cull of all obscured components is an operation that scales very badly to large numbers of components, so the algorithm we use doesn’t attempt to traverse the entire tree, it just does enough to cover the common use-cases.


#11

At the moment it does not do anything, as the function is not called at all :

The problem is not that it is not optimized, but that you will have worst performance with dontClipGraphicsFlag if your component has children


#12

Sure, but the dontClipGraphics flag was never supposed to be used for components with children. The purpose of that flag is to avoid clipping components which just contain some simple graphics.


setPaintingIsUnclipped question
#13

In the setPaintingIsUnclipped() documentation, it is written : Your component also can’t have any child components that may be placed beyond its bounds. So I assumed it could be called on components that have children (like a simple slider which has a label child).

We could simply add a numChildComponents condition (line 1969 in paintComponentAndChildren()):

if (flags.dontClipGraphicsFlag && getNumChildComponents() == 0)