title: CreateCompatibleBitmap and CreateDIBSection (Memory DC's)
body: <p>from what I've read <a href="http://blogs.msdn.com/b/e7/archive/2009/04/25/engineering-windows-7-for-graphics-performance.aspx" rel="noreferrer">here</a> it seems that most of the Windows GDI functions are accelerated. So for instance a call to <code>BitBlt</code> or <code>AlphaBlend</code> uses hardware acceleration if available. It also mentions that the contents of a window are kept only in video memory. Now this is all good and true for a <strong>window DC</strong>, but how can I use a <strong>memory DC</strong> that resides in video card memory? And once we've accomplished that how to obtain direct access to the pixels, I think that would involve 1. temporary copying the data to system memory 2. alter the pixel data 3. copy back to video memory.</p>
  
  <p>I've tried two approaches, both allocate system memory as I can see in the task manager...</p>
  
  <ol>
  <li><p>CreateCompatibleBitmap</p>
  
  <pre><code>HDC hDC = GetDC(NULL);
  m_hDC = CreateCompatibleDC(hDC);
  m_hBmp = CreateCompatibleBitmap(hDC, cx, cy);
  ReleaseDC(NULL, hDC);
  
  m_hOldBmp = (HBITMAP)SelectObject(m_hDC, m_hBmp);
  </code></pre>
  
  <p>and then call to obtain the bits</p>
  
  <pre><code>GetBitmapBits(...)
  </code></pre>
  
  <p>according to various comments this should indeed create the compatible bitmap in video memory, but why can I still see an increase in system memory (even when I don't call <code>GetBitmapBits</code>)?</p></li>
  <li><p>CreateDIBSection</p>
  
  <pre><code>HDC hDC = GetDC(NULL);
  m_hDC = CreateCompatibleDC(hDC);
  
  BITMAPINFO bmi;
  memset(&amp;bmi, 0, sizeof(BITMAPINFO));
  bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
  bmi.bmiHeader.biWidth = cx;
  bmi.bmiHeader.biHeight = -cy; // top-down
  bmi.bmiHeader.biPlanes = 1;
  bmi.bmiHeader.biBitCount = 32;
  bmi.bmiHeader.biCompression = BI_RGB;
  m_hBmp = CreateDIBSection(hDC, &amp;bmi, DIB_RGB_COLORS, (void**)&amp;m_pBits, NULL, NULL);
  
  ReleaseDC(NULL, hDC);
  
  m_hOldBmp = (HBITMAP)SelectObject(m_hDC, m_hBmp);
  </code></pre>
  
  <p>in this case we receive the pointer to the bits immediately (<code>m_pBits</code>) so it's obvious that these reside in system memory...</p></li>
  </ol>
  
  <p>Or is it a copy that is kept in system memory for both methods? But if I change the bits in system memory a call to <code>BitBlt</code> would still have to check/copy from system memory again... not very optimized IMHO.</p>
  
  <p>EDIT: I've also tried creating memory DC's by using the <code>BeginBufferedPaint</code> and <code>GetBufferedPaintBits</code>. It allocates system memory as well, so in that respect I suppose it's just a wrapper for the above methods but caches the DC's so a next call doesn't necessarily has to recreate a memory DC. See Raymond Chen's <a href="http://blogs.msdn.com/b/oldnewthing/archive/2011/05/20/10166505.aspx" rel="noreferrer">article</a>.</p>
  
  <p>EDIT #2: I guess the actual question is: <em>Am I doing the memory DC creation correct in method 1 or 2 to get hardware accelerated GDI operations?</em> To me it all seems fast, and both methods provide the same speed too, so there's not really a way to check it...</p>