是什么导致D2DERR_INVALID_GRAPH_CONFIGURATION?

时间:2014-11-25 18:13:09

标签: winrt-xaml direct2d winrt-component

当我playing with this MSDN sample时,我无法为DXGISurface创建的ID2D1Image添加模糊效果,并填充了一些矩形。

这是我的EndDraw方法:

void Scenario1ImageSource::EndDraw()
{
    // Remove the transform and clip applied in BeginDraw since
    // the target area can change on every update.
    m_d2dContext->SetTransform(D2D1::IdentityMatrix());
    m_d2dContext->PopAxisAlignedClip();

    DX::ThrowIfFailed(
        m_d2dContext->Flush()
        );

    ComPtr<ID2D1Image> bitmap;
    m_d2dContext->GetTarget(&bitmap);

    DX::ThrowIfFailed(
        m_d2dContext->CreateEffect(CLSID_D2D1GaussianBlur, &m_gaussianBlurEffect)
        );

    m_gaussianBlurEffect->SetInput(0, bitmap.Get());

    DX::ThrowIfFailed(
        m_gaussianBlurEffect->SetValue(D2D1_GAUSSIANBLUR_PROP_STANDARD_DEVIATION, 3.0f)
        );
    DX::ThrowIfFailed(
        m_gaussianBlurEffect->SetValue(D2D1_GAUSSIANBLUR_PROP_BORDER_MODE, D2D1_BORDER_MODE_HARD)
        );

    m_d2dContext->BeginDraw();

    m_d2dContext->DrawImage(m_gaussianBlurEffect.Get());


    // Remove the render target and end drawing.
    DX::ThrowIfFailed(
        m_d2dContext->EndDraw()
        );

    m_d2dContext->SetTarget(nullptr);

    // Query for ISurfaceImageSourceNative interface.
    Microsoft::WRL::ComPtr<ISurfaceImageSourceNative> sisNative;    
    DX::ThrowIfFailed(
        reinterpret_cast<IUnknown*>(this)->QueryInterface(IID_PPV_ARGS(&sisNative))
        );

    DX::ThrowIfFailed(
        sisNative->EndDraw()
        );
}

我想在绘制图像后模糊我的矩形。为什么通过抛出错误代码D2DERR_INVALID_GRAPH_CONFIGURATION失败?

1 个答案:

答案 0 :(得分:1)

导致问题的原因是,当BeginDraw/EndDraw将此资源用作目标时,您试图将效果应用于DeviceContext块中的资源(位图/表面)。粗略地说,当你致电ID2D1RenderTarget.BeginDraw时,它的目标会被锁定,除了TARGET之外你无法使用它。

一个简单的解决方案是使用中间位图作为屏幕外位图。例如(伪代码):

// somewhere at some initialization point
deviceContext.CreateBitmap(size, nil, 0, @bitmapProps, newBitmap);

// drawing routine
deviceContext.GetTarget(yourInitialTarget);
deviceContext.SetTarget(newBitmap);

deviceContext.BeginDraw;
try
  fBrush.SetColor(getRandomColor);
  deviceContext.FillRect(getRect, fBrush);
finally
  deviceContext.EndDraw;
end;

deviceContext.CreateEffect(@CLSID_D2D1GaussianBlur, fx);
fx.SetInput(0, newBitmap);
fx.GetOutput(newBitmap);

deviceContext.SetTarget(yourInitialTarget);

deviceContext.BeginDraw;
try
  deviceContext.Clear(getRandomColor);
  deviceContext.DrawImage(newBitmap);
finally
  deviceContext.EndDraw;
end;

请记住,内置GaussianBlur效果的输出位图可能比输入的大。 Gaussian blur effect