Setpixel and MUSTLOCK

Hello,

I wanted to know the meaning of “pitch” used in the setpixel function.
Also, why do we need to lock and unlock the surfaces?

Please help.

Regards,
Smitha.

Smitha Rathnam wrote:

Hello,
?
?I wanted to know the meaning of “pitch” used in the setpixel function.
Also, why do we need to lock and unlock the surfaces?
?
Please help.
?
Regards,
Smitha.

Pitch is a length in bytes of o single image row. It is important value because a length of a row is not always width * bytes_per_pixel due to various alignment memory requirements. When accessing pixel data pitch value should be always used to determine the distance between beginning of 2 subsequent rows.

Locking mechanism (aka mapping) is needed for 2 reasons:

  1. Ensuring exclusive access to pixel data in multi-threaded environment.
  2. Telling the video driver that access to pixel is needed in local memory space (especially when surface is stored in video memory).

That is why with hardware surfaces pixel pointer may be invalid until surface is locked and memory is mapped to local space.

So when modifying hw surface:

  1. lock to tell the driver you want access to pixel data exclusively
  2. access pixels using pitch (aka stride)
  3. unlock to tell the driver that you no longer use pixels

Hi.

SDL’s surface locking has nothing to do with multi-threading. If you want to
share surfaces between threads then you must take external precautions to
ensure consistent results.

Regards,
– Brian.On 23 June 2010 12:00, hardcoder wrote:

Locking mechanism (aka mapping) is needed for 2 reasons:

  1. Ensuring exclusive access to pixel data in multi-threaded environment.
  2. Telling the video driver that access to pixel is needed in local memory
    space (especially when surface is stored in video memory).

That is why with hardware surfaces pixel pointer may be invalid until
surface is locked and memory is mapped to local space.

So when modifying hw surface: