[graphite] Restructure tracked device cleanup in case its called multiple times

flushTrackedDevices() can be re-entered if a Device being flushed
has a picture shader that gets rendered to a new device image
and that inner rendering triggers a flush-the-world event.

A device's flushToRecorder() can be re-entered similarly even if
its flush was triggered by SkSurfaces::Flush() or by being marked
immutable. If the inner device flushed the world, the Recorder
would still re-flush the in-progress device.

Additionally, even if there was no flush-the-world event from an
inner device triggered by a picture shader, it will have
modifed `fTrackedDevices` when the device was registered. It can't
use a scratch device yet because the SkPicture may require atlases
to render. If the underlying array had to be re-allocated for this
new device, a for-each loop could access old device addresses.

This reworks the flushTrackedDevices() logic to use a shared index
stored in the Recorder so that even if it's re-entered, it will
only iterate over each Device once. Device also tracks whether or
not it's in the middle of a flush and does nothing (as noted in
the code comment, this isn't always perfect, but will avoid a
crash). By using indexed lookups, flushTrackedDevices() should not
care whether or not fTrackedDevices grew from a flush-triggered
registerDevice() call. deregisterDevice() still just sets the
element to null so there's no need to worry about it shrinking
while iterating.

With the Recorder tracking the loop index for all recursive
flushTrackedDevices(), it makes sure to only clean up the null
elements once at the end of the outermost call. Previously, any
recursive call to flushTrackedDevices() might end up shrinking
the array then breaking the iteration in the outer loop.

Switched to using SkTArray instead of std::vector and use index
looping to make it clearer that the array is being modified.

Bug: b/333073673
Bug: b/330864257
Bug: b/331782125
Change-Id: I38184a34d55ce80d9b839aee1796519112ff7f0c
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/841096
Commit-Queue: Michael Ludwig <michaelludwig@google.com>
Reviewed-by: Greg Daniel <egdaniel@google.com>
4 files changed