Skip to content

Commit

Permalink
GRAPHICS: ATARI: Align surface on a 16-byte boundary
Browse files Browse the repository at this point in the history
  • Loading branch information
mikrosk committed Mar 12, 2023
1 parent 6f87f19 commit 4b95038
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 12 deletions.
2 changes: 1 addition & 1 deletion backends/graphics/atari/atari-graphics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1001,7 +1001,7 @@ void AtariGraphicsManager::Cursor::setSurface(const void *buf, int w, int h, int
if (surface.w != w || surface.h != h || surface.format != format)
surface.create(w, h, format);

surface.copyRectToSurface(buf, surface.pitch, 0, 0, w, h);
surface.copyRectToSurface(buf, w * format.bytesPerPixel, 0, 0, w, h);

hotspotX = _hotspotX;
hotspotY = _hotspotY;
Expand Down
44 changes: 33 additions & 11 deletions graphics/blit-atari.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,13 @@
#include "graphics/blit.h"
#include "graphics/surface.h"

#include <cstdlib> // calloc
#include <cstring> // memcpy
#include <cstdlib> // malloc
#include <cstring> // memcpy, memset
#include <mint/cookie.h>
#include <mint/falcon.h>

#include "backends/graphics/atari/atari-graphics-superblitter.h"
#include "common/textconsole.h" // error

namespace Graphics {

Expand All @@ -37,27 +38,46 @@ void Surface::create(int16 width, int16 height, const PixelFormat &f) {
assert(width >= 0 && height >= 0);
free();

constexpr size_t ALIGN = 16; // 16 bytes

w = width;
h = height;
format = f;
pitch = w * format.bytesPerPixel;
// align pitch to a 16-byte boundary for a possible C2P optimization
pitch = (w * format.bytesPerPixel + ALIGN - 1) & (-ALIGN);

if (width && height) {
if (VgetMonitor() == MON_VGA && Getcookie(C_SupV, NULL) == C_FOUND)
pixels = (void*)ct60_vmalloc(width * height * format.bytesPerPixel);
else
pixels = calloc(width * height, format.bytesPerPixel);
assert(pixels);
if (VgetMonitor() == MON_VGA && Getcookie(C_SupV, NULL) == C_FOUND) {
pixels = (void *)ct60_vmalloc(height * pitch);

if (!pixels)
error("Not enough SVRAM to allocate a surface");

assert((uintptr)pixels >= 0xA0000000);
} else {
// align buffer to a 16-byte boundary for move16
void *pixelsUnaligned = ::malloc(sizeof(uintptr) + (height * pitch) + ALIGN - 1);

if (!pixelsUnaligned)
error("Not enough memory to allocate a surface");

pixels = (void *)(((uintptr)pixelsUnaligned + sizeof(uintptr) + ALIGN - 1) & (-ALIGN));

// store the unaligned pointer for later free()
*((uintptr *)pixels - 1) = (uintptr)pixelsUnaligned;
}

memset(pixels, 0, height * pitch);
}
}

void Surface::free() {
if (((uintptr)pixels & 0xFF000000) >= 0xA0000000)
ct60_vmfree(pixels);
else
::free(pixels);
else if (pixels)
::free((void *)*((uintptr *)pixels - 1));

pixels = 0;
pixels = nullptr;
w = h = pitch = 0;
format = PixelFormat();
}
Expand Down Expand Up @@ -87,9 +107,11 @@ void copyBlit(byte *dst, const byte *src,
// wait until we finish otherwise we may overwrite pixels written manually afterwards
while (*SV_BLITTER_CONTROL & 1);
} else if (dstPitch == srcPitch && ((w * bytesPerPixel) == dstPitch)) {
// TODO: replace with move16
memcpy(dst, src, dstPitch * h);
} else {
for (uint i = 0; i < h; ++i) {
// TODO: replace with move16
memcpy(dst, src, w * bytesPerPixel);
dst += dstPitch;
src += srcPitch;
Expand Down

0 comments on commit 4b95038

Please sign in to comment.