# Optimize MIDI clip UI rendering

**URL:** <https://forum.juce.com/t/optimize-midi-clip-ui-rendering/56541>\
**Category:** Tracktion Engine\
**Created:** [June 6, 2023, 6:16am UTC](https://forum.juce.com/t/optimize-midi-clip-ui-rendering/56541 "2023-06-06T06:16:22Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![eggnog](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/eggnog/32/12211_2.png) [@eggnog](https://forum.juce.com/u/eggnog)\
**Post date:** [June 6, 2023, 6:16am UTC](https://forum.juce.com/t/optimize-midi-clip-ui-rendering/56541/1 "2023-06-06T06:16:22Z")

</div>

Hi !

I’m looking to optimize the UI rendering code of MIDI clips in my app.

Rather than blindly drawing notes on `paint()` calls, I was thinking to pre-render them in an `juce::Image` and simply draw the image in `paint()`. The `Image` could re-render when the `te::MidiClip` state changes.

Another concern is having to many `ValueTree::Listeners` – I don’t think having one listener per MIDI clip component is ideal, so I’m happy to hear your suggestions on this too.

Thank you in advance,

Best,

E.

---

<div class="post-metadata">

**Author:** ![vtarasuk](https://avatars.discourse-cdn.com/v4/letter/v/7cd45c/32.png) [@vtarasuk](https://forum.juce.com/u/vtarasuk)\
**Post date:** [June 6, 2023, 7:06am UTC](https://forum.juce.com/t/optimize-midi-clip-ui-rendering/56541/2 "2023-06-06T07:06:14Z")

</div>

In one of my products, before the Juce framework, on the very first generation iPad, I also suffered with midi clips and midi editor, so that I could draw more than 6 thousand notes, because if, for example, you download and import a Beethoven sonata :), it may well exceed this amount.

1. I just came to the conclusion that I would draw notes through OpenGL
2. I don’t think drawing each clip individually is a good idea. It should be a single draw call, because user can have thousands of small clips (if your app allows it) I would draw the entire track. Or if it’s OpenGL, then whole midi view.

> I was thinking to pre-render them in an `juce::Image` and simply draw the image in `paint()` .

Why not to use simply Component::setBufferedToImage? And then if you need to change a state, call repaint();

```auto
this->setBufferedToImage(true); // set in the constructor

```

In any case when you need to change state, you will draw your notes to the Image, so you don’t win anything here and **setBufferedToImage** make it easy for you (in this case the draw call will not be called if you redraw other components)

---

<div class="post-metadata">

**Author:** ![dave96](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/dave96/32/8487_2.png) [@dave96](https://forum.juce.com/u/dave96)\
**Post date:** [June 6, 2023, 9:29am UTC](https://forum.juce.com/t/optimize-midi-clip-ui-rendering/56541/3 "2023-06-06T09:29:00Z")

</div>

Before you spend a long time rewriting things, make sure you profile a release build to find out where the time is actually being spent. For things that move around a lot (like clips on a timeline or a piano roll), buffering to an image could well be slower as you’ll constantly have to redraw the image and then blit that to the graphics context rather than the draw calls directly (which are async on some OSes like macOS).

FYI, we simply draw the `MidiList`s in Waveform.

---

<div class="post-metadata">

**Author:** ![eggnog](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/eggnog/32/12211_2.png) [@eggnog](https://forum.juce.com/u/eggnog)\
**Post date:** [June 6, 2023, 9:49am UTC](https://forum.juce.com/t/optimize-midi-clip-ui-rendering/56541/4 "2023-06-06T09:49:21Z")

</div>

Good call. I was looking to use Perfetto for profiling (not a big fan of Instruments on macOS), through this article [Using Perfetto with JUCE for dsp and UI performance tuning · Melatonin](https://melatonin.dev/blog/using-perfetto-with-juce-for-dsp-and-ui-performance-tuning/)

So … in Waveform you simply draw the notes everytime on `paint()` calls ?

---

<div class="post-metadata">

**Author:** ![dave96](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/dave96/32/8487_2.png) [@dave96](https://forum.juce.com/u/dave96)\
**Post date:** [June 6, 2023, 9:56am UTC](https://forum.juce.com/t/optimize-midi-clip-ui-rendering/56541/5 "2023-06-06T09:56:29Z")

</div>

Yes. Personally I find Instruments on macOS excellent.  
Even better if you combine it with some signposts.

But more generally it’s going to tell you where your CPU time is spent. If you focus all your effort on improving the drawing but it turns out your CPU is spending ages doing something else, you won’t have made any improvements.

---

<div class="post-metadata">

**Author:** ![eggnog](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/eggnog/32/12211_2.png) [@eggnog](https://forum.juce.com/u/eggnog)\
**Post date:** [June 6, 2023, 10:42am UTC](https://forum.juce.com/t/optimize-midi-clip-ui-rendering/56541/6 "2023-06-06T10:42:25Z")

</div>

Yeah, I guess Instruments would be OK to profile the drawing code, but I’m geniunly curious about Perfetto. I really miss Shark & VTune !

---

<div class="post-metadata">

**Author:** ![dave96](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/dave96/32/8487_2.png) [@dave96](https://forum.juce.com/u/dave96)\
**Post date:** [June 6, 2023, 10:44am UTC](https://forum.juce.com/t/optimize-midi-clip-ui-rendering/56541/7 "2023-06-06T10:44:54Z")

</div>

You can still use VTune can’t you? I’ve used that on Linux.

---

<div class="post-metadata">

**Author:** ![eggnog](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/eggnog/32/12211_2.png) [@eggnog](https://forum.juce.com/u/eggnog)\
**Post date:** [June 6, 2023, 3:52pm UTC](https://forum.juce.com/t/optimize-midi-clip-ui-rendering/56541/8 "2023-06-06T15:52:07Z")

</div>

Yeah, I just remember about it.

Anyway, I can comfirm `setBufferedToImage(true)` is far less optimal compared to simply drawing the events in `paint()`.

---

<div class="post-metadata">

**Author:** ![vtarasuk](https://avatars.discourse-cdn.com/v4/letter/v/7cd45c/32.png) [@vtarasuk](https://forum.juce.com/u/vtarasuk)\
**Post date:** [June 6, 2023, 6:04pm UTC](https://forum.juce.com/t/optimize-midi-clip-ui-rendering/56541/9 "2023-06-06T18:04:39Z")

</div>

> Anyway, I can comfirm `setBufferedToImage(true)` is far less optimal compared to simply drawing the events in `paint()`.

setBufferedToImage will not be optimal if you are redrawing a whole piece of a large view.  
For example, in a synthesizer, I use it by zones:  
-osc,  
-Envelope,  
-keys  
That is, when I change the values in Osc, then repaint is not called in Envelope and Keys.

```auto
oscView.setBufferedToImage(true);
envelopeView.setBufferedToImage(true);
keysView.setBufferedToImage(true);

```

then I can call for examle:

```auto
osc.getGainControl().setValue(newValue);

```

the buffer image will be updated only for osc (i.e. all child components in the osc trigger repaint method), while for the rest it will be drawn what is still in the “envelope” and “keys” buffers.

That is, if you try to set it for each midi clip or for the whole screen view - then yes it will not be optimal. Since in this case you are essentially drawing in Image and then this image is drawing to the component. Also it uses more memory.
