CEF OffScreen Render with Direct2D

Having problems with building or using CEF's C/C++ APIs? This forum is here to help. Please do not post bug reports or feature requests here.

CEF OffScreen Render with Direct2D

Postby toxicated101 » Thu May 25, 2017 7:32 am

Hi

I am trying to Offscreen Render to a Direct2D surface using CEF3. Th eOnPaint call gets fired and I can save the buffer to a hHitmap but I'm not having much look getting the buffer to a D2D1Bitmap. Here is what I have but it doesn't work (blank output to the direct2d surface). The pixel format could be wrong or any of the direct2d stuff. Direct2D is a bit new for me.


void RenderHandler::OnPaint(CefRefPtr<CefBrowser> browser, PaintElementType type, const RectList &dirtyRects, const void *buffer, int width, int height)
{
//SaveBrowserImage(browser, L"C:\\Temp\\MyTest.bmp", buffer, width, height);
ID2D1Bitmap * pBitmap = NULL;

D2D1_PIXEL_FORMAT desc2D = D2D1::PixelFormat();
desc2D.format = DXGI_FORMAT_R8G8B8A8_UNORM;
desc2D.alphaMode = D2D1_ALPHA_MODE_PREMULTIPLIED;

D2D1_BITMAP_PROPERTIES bmpProperties = D2D1::BitmapProperties();
bmpProperties.dpiX = 96.0f;
bmpProperties.dpiY = 96.0f;
bmpProperties.pixelFormat = desc2D;

D2D1_RECT_F rect = D2D1::RectF(0.0,0.0,width, height);
D2D1_SIZE_U size = D2D1::SizeU(width, height);

HRESULT hr = m_pRenderTarget->CreateBitmap(size, bmpProperties, &pBitmap);
if (hr == S_OK && pBitmap)
{
hr = pBitmap->CopyFromMemory(NULL, buffer, width * 4);
if (hr == S_OK)
{
m_pRenderTarget->DrawBitmap(pBitmap, rect);
}
pBitmap->Release();
}
}

Thanks
toxicated101
Newbie
 
Posts: 2
Joined: Thu May 25, 2017 7:22 am

Re: CEF OffScreen Render with Direct2D

Postby toxicated101 » Fri May 26, 2017 3:09 am

I have found the issue, it was because I had forgot my BeginDraw and EndDraw also the colour format is BGR.

Here is my working code

void RenderHandler::OnPaint(CefRefPtr<CefBrowser> browser, PaintElementType type, const RectList &dirtyRects, const void *buffer, int width, int height)
{
//SaveBrowserImage(browser, L"C:\\Temp\\MyTest.bmp", buffer, width, height);
ID2D1Bitmap * pBitmap = NULL;

D2D1_PIXEL_FORMAT desc2D = D2D1::PixelFormat();
desc2D.format = DXGI_FORMAT_B8G8R8A8_UNORM_SRGB;//DXGI_FORMAT_R8G8B8A8_UNORM;
desc2D.alphaMode = D2D1_ALPHA_MODE_PREMULTIPLIED;

D2D1_BITMAP_PROPERTIES bmpProperties = D2D1::BitmapProperties();
m_pRenderTarget->GetDpi(&bmpProperties.dpiX, &bmpProperties.dpiY);
bmpProperties.pixelFormat = desc2D;

D2D1_RECT_F rect = D2D1::RectF(0.0,0.0,width, height);
D2D1_SIZE_U size = D2D1::SizeU(width, height);

m_pRenderTarget->BeginDraw();

HRESULT hr = m_pRenderTarget->CreateBitmap(size, bmpProperties, &pBitmap);
if (hr == S_OK && pBitmap)
{
hr = pBitmap->CopyFromMemory(NULL, buffer, width * 4);
if (hr == S_OK)
{
m_pRenderTarget->DrawBitmap(pBitmap, rect);
}
pBitmap->Release();
}

m_pRenderTarget->EndDraw();
}
toxicated101
Newbie
 
Posts: 2
Joined: Thu May 25, 2017 7:22 am


Return to Support Forum

Who is online

Users browsing this forum: Google [Bot], Majestic-12 [Bot] and 45 guests