Calls to drawImage slow with DirectX

Hi,

we’re currently testing our plugins with JUCE 8 and hit a performance issue with DirectX.

Code something like the following:

  // Work out the source image bounds
  const auto clipBounds = normBoundsToBounds(_currentImageBounds, currentImage.getBounds());
  const auto clippedImage = currentImage.getClippedImage(clipBounds);

  jassert(clippedImage.isValid());

  g.setColour(juce::Colours::white);
  g.setOpacity(1.0f);
  g.drawImage(clippedImage, bounds, juce::RectanglePlacement::fillDestination);

g.drawImage is now very very slow. So I profiled it and this is the area in JUCE that seems to cause the slowdown which takes the same path on every call to drawImage.

void Direct2DGraphicsContext::drawImage (const Image& image, const AffineTransform& transform)
{

...

// Is this a Direct2D image already with the correct format?
ComSmartPtr<ID2D1Bitmap1> d2d1Bitmap;
Rectangle<int> imageClipArea;

if (auto direct2DPixelData = dynamic_cast<Direct2DPixelData*> (image.getPixelData()))
{
    d2d1Bitmap = direct2DPixelData->getAdapterD2D1Bitmap();
    imageClipArea = { direct2DPixelData->width, direct2DPixelData->height };
}

if (! d2d1Bitmap || d2d1Bitmap->GetPixelFormat().format != DXGI_FORMAT_B8G8R8A8_UNORM)
{
    JUCE_D2DMETRICS_SCOPED_ELAPSED_TIME (Direct2DMetricsHub::getInstance()->imageContextMetrics, createBitmapTime);

    d2d1Bitmap = Direct2DBitmap::toBitmap (image, deviceContext, Image::ARGB);

Specifically the call to Direct2DBitmap::toBitmap because d2d1Bitmap is null.

The comment seems to imply that we should have our images in the correct format. Is there something we should do to avoid this happening?

thanks!

I think this is happening because the D2D renderer isn’t checking for clipped images, which are internally represented as SubsectionPixelData instead of Direct2DPixelData. This means that the renderer ends up creating a new bitmap and copying the SubsectionPixelData into it, rather than rendering the bitmap backing the SubsectionPixelData directly. I believe this will be addressed by the change that’s currently being developed to better support very large images in the D2D renderer.

1 Like