It seems that Direct2D effects is only available in ID2D1DeviceContext, but there is a need for me to use effects in HwndRenderTarget. Now the question is: can ID2D1DeviceContext and HwndRenderTarget interoperate between each other?
My test shows negative. (0x88990015, D2DERR_WRONG_RESOURCE_DOMAIN, The resource used was created by a render target in a different resource domain)
1. create ID2D1DeviceContext
hr = m_pDirect2dFactory->CreateDevice(dxgiDevice, &d2dDevice);
hr = d2dDevice->CreateDeviceContext(
D2D1_DEVICE_CONTEXT_OPTIONS_NONE,
&m_d2dContext
);
2. create HwndRenderTarget
hr = m_pDirect2dFactory->CreateHwndRenderTarget(
rtProps,
D2D1::HwndRenderTargetProperties(m_hwnd, pixelSize),
&m_pRenderTarget
);
3. create bitmaps in HwndRenderTarget and ID2D1DeviceContext, respectively:
ID2D1Bitmap1* pD2dBitmap;
hr = m_d2dContext->CreateBitmap(pixelSize, nullptr, 4 * pixelSize.width, &bitPropertiesTgt, &pD2dBitmap);
ID2D1Bitmap* pHwndRTBitmap;
hr = m_pRenderTarget->CreateBitmap(pixelSize, bitmapProperties2, &pHwndRTBitmap);
4. assume the bitmaps are filled with colors and make the below calls:
m_d2dContext->SetTarget(pD2dBitmap);
m_d2dContext->BeginDraw();
m_d2dContext->DrawBitmap(pHwndRTBitmap);
hr = m_d2dContext->EndDraw();
If they really cannot interoperate, the ms docs below is confusing to me:
ID2D1DeviceContext::GetTarget()
If the currently selected target is a bitmap rather than a command list, the application can gain access to the initial bitmaps created by using one of the following methods:
• CreateHwndRenderTarget
• CreateDxgiSurfaceRenderTarget
• CreateWicBitmapRenderTarget
• CreateDCRenderTarget
• CreateCompatibleRenderTarget