1
0
Fork 0
mirror of https://github.com/juce-framework/JUCE.git synced 2026-01-09 23:34:20 +00:00

CameraDevice: Simplify data shared across threads, to reduce potential for data races

This commit is contained in:
reuk 2024-05-01 20:32:24 +01:00
parent 047f7aaad2
commit d9a9356267
No known key found for this signature in database
GPG key ID: FCB43929F012EE5C

View file

@ -144,9 +144,6 @@ struct CameraDevice::Pimpl : public ChangeBroadcaster
if (connectFilters (sampleGrabberBase, nullFilter)
&& addGraphToRot())
{
activeImage = Image (Image::RGB, width, height, true);
loadingImage = Image (Image::RGB, width, height, true);
openedSuccessfully = true;
}
}
@ -293,39 +290,36 @@ struct CameraDevice::Pimpl : public ChangeBroadcaster
}
}
Image loadingImage (Image::RGB, width, height, true);
const int lineStride = width * 3;
{
const int lineStride = width * 3;
const ScopedLock sl (imageSwapLock);
const Image::BitmapData destData (loadingImage, 0, 0, width, height, Image::BitmapData::writeOnly);
{
loadingImage.duplicateIfShared();
const Image::BitmapData destData (loadingImage, 0, 0, width, height, Image::BitmapData::writeOnly);
for (int i = 0; i < height; ++i)
memcpy (destData.getLinePointer ((height - 1) - i),
buffer + lineStride * i,
(size_t) lineStride);
}
imageNeedsFlipping = true;
for (int i = 0; i < height; ++i)
memcpy (destData.getLinePointer ((height - 1) - i),
buffer + lineStride * i,
(size_t) lineStride);
}
if (listeners.size() > 0)
if (! listeners.isEmpty())
callListeners (loadingImage);
notifyPictureTakenIfNeeded (loadingImage);
sendChangeMessage();
const ScopedLock sl (imageSwapLock);
activeImage = loadingImage;
}
void drawCurrentImage (Graphics& g, Rectangle<int> area)
{
if (imageNeedsFlipping)
const auto imageToDraw = [this]
{
const ScopedLock sl (imageSwapLock);
std::swap (loadingImage, activeImage);
imageNeedsFlipping = false;
}
return activeImage;
}();
Rectangle<int> centred (RectanglePlacement (RectanglePlacement::centred)
.appliedTo (Rectangle<int> (width, height), area));
@ -335,7 +329,7 @@ struct CameraDevice::Pimpl : public ChangeBroadcaster
g.setColour (Colours::black);
g.fillRectList (borders);
g.drawImage (activeImage, centred.getX(), centred.getY(),
g.drawImage (imageToDraw, centred.getX(), centred.getY(),
centred.getWidth(), centred.getHeight(), 0, 0, width, height);
}
@ -589,16 +583,15 @@ struct CameraDevice::Pimpl : public ChangeBroadcaster
Array<int> widths, heights;
DWORD graphRegistrationID;
CriticalSection imageSwapLock;
bool imageNeedsFlipping = false;
Image loadingImage, activeImage;
bool recordNextFrameTime = false;
int previewMaxFPS = 60;
JUCE_DECLARE_WEAK_REFERENCEABLE (Pimpl)
private:
CriticalSection imageSwapLock;
Image activeImage;
void getVideoSizes (ComTypes::IAMStreamConfig* const streamConfig)
{
widths.clear();