views:

65

answers:

3

Hi,

I have a pointer to a COLORREF buffer, something like: COLORREF* buf = new COLORREF[x*y];

A subroutine fills this buffer with color-information. Each COLORREF represents one pixel.

Now I want to draw this buffer to a device context. My current approach works, but is pretty slow (== ~200ms per drawing, depending on the size of the image):

for (size_t i = 0; i < pixelpos; ++i) 
{
    // Get X and Y coordinates from 1-dimensional buffer.
    size_t y = i / wnd_size.cx;
    size_t x = i % wnd_size.cx;
    ::SetPixelV(hDC, x, y, buf[i]);
}

Is there a way to do this faster; all at once, not one pixel after another?
I am not really familiar with the GDI. I heard about al lot of APIs like CreateDIBitmap(), BitBlt(), HBITMAP, CImage and all that stuff, but have no idea how to apply it. It seems all pretty complicated...
MFC is also welcome.

Any ideas?

Thanks in advance.

(Background: the subroutine I mentioned above is an OpenCL kernel - the GPU calculates an Mandelbrot image and safes it in the COLORREF buffer.)

EDIT:
Thank you all for your suggestions. The answers (and links) gave me some insight into Windows graphics programming. The performance is now acceptable (semi-realtime-scrolling into the Mandelbrot works :)
I ended up with the following solution (MFC):

...
CDC dcMemory;
dcMemory.CreateCompatibleDC(pDC);

CBitmap mandelbrotBmp;
mandelbrotBmp.CreateBitmap(clientRect.Width(), clientRect.Height(), 1, 32, buf);

CBitmap* oldBmp = dcMemory.SelectObject(&mandelbrotBmp);
pDC->BitBlt(0, 0, clientRect.Width(), clientRect.Height(), &dcMemory, 0, 0, SRCCOPY);

dcMemory.SelectObject(oldBmp);
mandelbrotBmp.DeleteObject();

So basically CBitmap::CreateBitmap() saved me from using the raw API (which I still do not fully understand). The example in the documentation of CDC::CreateCompatibleDC was also helpful.
My Mandelbrot is now blue - using SetPixelV() it was red. But I guess that has something to do with CBitmap::CreateBitmap() interpreting my buffer, not really important.

I might try the OpenGL suggestion because it would have been the much more logical choice and I wanted to try OpenCL under Linux anyway.

+2  A: 

Yes, sure, that's slow. You are making a round-trip through the kernel and video device driver for each individual pixel. You make it fast by drawing to memory first, then update the screen in one fell swoop. That takes, say, CreateDIBitmap, CreateCompatibleDc() and BitBlt().

This isn't a good time and place for an extensive tutorial on graphics programming. It is well covered by any introductory text on GDI and/or Windows API programming. Everything you'll need to know you can find in Petzold's seminal Programming Windows.

Hans Passant
+2  A: 

Since you already have an array of pixels, you can directly use BitBlt to transfer it to the window's DC. See this link for a partial example: http://msdn.microsoft.com/en-us/library/aa928058.aspx

ivymike
Not so -- before you can use `BitBlt`, you have to get the pixel values into something that Windows recognizes as a Bitmap, and select the bitmap into a DC (then you BitBlt from one DC to another).
Jerry Coffin
Yup, that's correct. And hence the link for creating bitmap :).
ivymike
@ivymike: The problem you're leaving him with is equivalent to what he already has: transferring a block of raw data into the bitmap (which he could do by selecting the bitmap into a DC, then setting each pixel with `SetPixelV` -- exactly what he's already tried).
Jerry Coffin
@Jerrynow that I think of it, you are correct. He will have to use SetDIBitsToDevice() to actually get the pixel data chunk into memory (after Creating the DIB as mentioned in your answer).
ivymike
@ivymike: With a normal DIB that would be needed, but with a DIB section, you get a pointer to the actual memory so you normally just use something like `memcpy`.
Jerry Coffin
+2  A: 

Under the circumstances, I'd probably use a DIB section (which you create with CreateDIBSection). A DIB section is a bitmap that allows you to access the contents directly as an array, but still use it with all the usual GDI functions.

I think that'll give you the best performance of anything based on GDI. If you need better, then @Kornel is basically correct -- you'll need to switch to something that has more direct support for hardware acceleration (DirectX or OpenGL -- though IMO, OpenGL is a much better choice for the job than DirectX).

Given that you're currently doing the calculation in OpenCL and depositing the output in a color buffer, OpenGL would be the really obvious choice. In particular, you can have OpenCL deposit the output in an OpenGL texture, then you have OpenGL draw a quad using that texture. Alternatively, since you're just putting the output on screen anyway, you could just do the calculation in an OpenGL fragment shader (or, of course, a DirectX pixel shader), so you wouldn't put the output into memory off-screen just so you can copy the result onto the screen. If memory serves, the Orange book has a Mandelbrot shader as one of its examples.

Jerry Coffin