GRAPHICS: Optimise PixelBuffer::getValueAt and PixelBuffer::setPixelAt.
Avoid doing a lot of operations just to move 2 or 4 bytes around. Inline these methods as they are hot-spots and do very little: call overhead becomes significant.
This commit is contained in:
parent
d467980499
commit
f4554c1541
2 changed files with 36 additions and 34 deletions
|
@ -86,20 +86,6 @@ void PixelBuffer::clear(int length) {
|
|||
memset(_buffer, 0, length);
|
||||
}
|
||||
|
||||
void PixelBuffer::setPixelAt(int pixel, uint32 value) {
|
||||
#if defined(SCUMM_BIG_ENDIAN)
|
||||
byte *buffer = _buffer + pixel * _format.bytesPerPixel;
|
||||
for (int i = 0; i < _format.bytesPerPixel; ++i) {
|
||||
buffer[i] = value >> ((_format.bytesPerPixel - i - 1) * 8) & 0xFF;
|
||||
}
|
||||
#elif defined(SCUMM_LITTLE_ENDIAN)
|
||||
byte *buffer = _buffer + pixel * _format.bytesPerPixel;
|
||||
for (int i = 0; i < _format.bytesPerPixel; ++i) {
|
||||
buffer[i] = value >> (i * 8) & 0xFF;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
void PixelBuffer::copyBuffer(int thisFrom, int otherFrom, int length, const PixelBuffer &buf) {
|
||||
if (buf._format == _format) {
|
||||
memcpy(_buffer + thisFrom * _format.bytesPerPixel, buf._buffer + otherFrom * _format.bytesPerPixel, length * _format.bytesPerPixel);
|
||||
|
@ -112,24 +98,6 @@ void PixelBuffer::copyBuffer(int thisFrom, int otherFrom, int length, const Pixe
|
|||
}
|
||||
}
|
||||
|
||||
uint32 PixelBuffer::getValueAt(int i) const {
|
||||
#if defined(SCUMM_BIG_ENDIAN)
|
||||
byte *buffer = _buffer + i * _format.bytesPerPixel;
|
||||
uint32 p = buffer[0] << ((_format.bytesPerPixel - 1) * 8);
|
||||
for (int l = 1; l < _format.bytesPerPixel; ++l) {
|
||||
p = p | (buffer[l] << (8 * (_format.bytesPerPixel - l - 1)));
|
||||
}
|
||||
return p;
|
||||
#elif defined(SCUMM_LITTLE_ENDIAN)
|
||||
byte *buffer = _buffer + i * _format.bytesPerPixel;
|
||||
uint32 p = buffer[0];
|
||||
for (int l = 1; l < _format.bytesPerPixel; ++l) {
|
||||
p = p | (buffer[l] << (8 * l));
|
||||
}
|
||||
return p;
|
||||
#endif
|
||||
}
|
||||
|
||||
PixelBuffer &PixelBuffer::operator=(const PixelBuffer &buf) {
|
||||
_buffer = buf._buffer;
|
||||
_format = buf._format;
|
||||
|
|
|
@ -24,6 +24,8 @@
|
|||
#define GRAPHICS_PIXELBUFFER_H
|
||||
|
||||
#include "common/types.h"
|
||||
#include "common/endian.h"
|
||||
#include "common/textconsole.h"
|
||||
|
||||
#include "graphics/colormasks.h"
|
||||
#include "graphics/pixelformat.h"
|
||||
|
@ -119,7 +121,24 @@ public:
|
|||
/**
|
||||
* Set the value of the pixel at index 'pixel' to 'value',
|
||||
*/
|
||||
void setPixelAt(int pixel, uint32 value);
|
||||
inline void setPixelAt(int pixel, uint32 value) {
|
||||
switch (_format.bytesPerPixel) {
|
||||
case 2:
|
||||
((uint16 *) _buffer)[pixel] = TO_LE_16((uint16) value);
|
||||
return;
|
||||
case 3:
|
||||
pixel *= 3;
|
||||
value = TO_LE_32(value);
|
||||
_buffer[pixel + 0] = value & 0xFF;
|
||||
_buffer[pixel + 1] = (value >> 8) & 0xFF;
|
||||
_buffer[pixel + 2] = (value >> 16) & 0xFF;
|
||||
return;
|
||||
case 4:
|
||||
((uint32 *) _buffer)[pixel] = TO_LE_32(value);
|
||||
return;
|
||||
}
|
||||
error("setPixelAt: Unhandled bytesPerPixel %i", _format.bytesPerPixel);
|
||||
}
|
||||
/**
|
||||
* Set the value of a pixel. The pixel will be converted from a pixel in another PixelBuffer,
|
||||
* at the same index.
|
||||
|
@ -180,7 +199,22 @@ public:
|
|||
/**
|
||||
* Return the encoded value of the pixel at the given index.
|
||||
*/
|
||||
uint32 getValueAt(int i) const;
|
||||
inline uint32 getValueAt(int i) const {
|
||||
switch (_format.bytesPerPixel) {
|
||||
case 2:
|
||||
return FROM_LE_16(((uint16 *) _buffer)[i]);
|
||||
case 3:
|
||||
i *= 3;
|
||||
#if defined(SCUMM_BIG_ENDIAN)
|
||||
return (_buffer[i + 0] << 16) | (_buffer[i + 1] << 8) | _buffer[i + 2];
|
||||
#elif defined(SCUMM_LITTLE_ENDIAN)
|
||||
return _buffer[i + 0] | (_buffer[i + 1] << 8) | (_buffer[i + 2] << 16);
|
||||
#endif
|
||||
case 4:
|
||||
return FROM_LE_32(((uint32 *) _buffer)[i]);
|
||||
}
|
||||
error("getValueAt: Unhandled bytesPerPixel %i", _format.bytesPerPixel);
|
||||
}
|
||||
/**
|
||||
* Return the RGB value of the pixel at the given index.
|
||||
*/
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue