Share via


Accessing Surface Memory Directly

A version of this page is also available for

Windows Embedded CE 6.0 R3

4/8/2010

You can directly access the frame buffer or off-screen surface memory by using the IDirectDrawSurface::Lock method.

When you call this method, the lpDestRect parameter is a pointer to a RECT structure that describes the rectangle on the surface you want to access directly.

To request that the entire surface be locked, set lpDestRect to NULL. Also, you can specify a RECT that covers only a portion of the surface.

For each surface, only one rectangle can be locked by a thread or a process. This applies to either the whole surface or to a subset of the surface.

The Lock method fills a DDSURFACEDESC structure with all the information you need to properly access the surface memory.

The structure includes information about the pitch (or stride) and the pixel format of the surface, if different from the pixel format of the primary surface.

When you finish accessing the surface memory, call the IDirectDrawSurface::Unlock method to unlock it.

While you have a surface locked, you can directly manipulate the contents. The following list describes some tips for avoiding common problems with directly rendering surface memory:

  • Never assume a constant display pitch. Always examine the pitch information returned by the IDirectDrawSurface::Lock method. This pitch can vary for a number of reasons, including the location of the surface memory, the type of display card, or even the version of the DirectDraw driver. For more information, see Width vs. Pitch.
  • Make certain you blit to unlocked surfaces. DirectDraw blit methods will fail, returning DirectDraw Return Values or DDERR_LOCKEDSURFACES, if called on a locked surface. Similarly, GDI blit functions fail without returning error values if called on a locked surface that exists in display memory.
  • Limit your application's activity while a surface is locked. The IDirectDrawSurface::GetDC method implicitly calls Lock, and the IDirectDrawSurface::ReleaseDC implicitly calls Unlock.
  • Always copy data aligned to display memory. Copying data unaligned to display memory can cause the system to suspend operations if the copy spans memory banks.

If a blit is in progress when you call IDirectDrawSurface::Lock, the method will return immediately with an error, as a lock cannot be obtained. To prevent the error, use the DDLOCK_WAIT flag to cause the method to wait until a lock can be successfully obtained.

Locking portions of the primary surface can interfere with the display of a software cursor. If the cursor intersects the locked rectangle, it is hidden for the duration of the lock. If it does not intersect the rectangle, it is frozen for the duration of the lock. Neither of these effects occurs if the entire surface is locked.