DS: Simplify the framebuffer code

This commit is contained in:
Cameron Cawley 2020-08-06 22:20:35 +01:00 committed by Eugene Sandulenko
parent 704deec031
commit 926ec07532
6 changed files with 56 additions and 688 deletions

View File

@ -1,391 +0,0 @@
/* ScummVM - Graphic Adventure Engine
*
* ScummVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
#include "blitters.h"
#define CHARSET_MASK_TRANSPARENCY 253
//#define PERFECT_5_TO_4_RESCALING
namespace DS {
void asmDrawStripToScreen(int height, int width, byte const *text, byte const *src, byte *dst,
int vsPitch, int vmScreenWidth, int textSurfacePitch) {
if (height <= 0)
height = 1;
if (width < 4)
return;
width &= ~4;
asm ( "mov r5, %0\n" // Height
"yLoop:\n"
"mov r3, #0\n" // X pos
"xLoop:\n"
"ldr r4, [%2, r3]\n" // Load text layer word
"cmp r4, %5\n"
"bne singleByteCompare\n"
"ldr r4, [%3, r3]\n"
"str r4, [%4, r3]\n"
"add r3, r3, #4\n"
"cmp r3, %1\n" // x == width?
"blt xLoop\n"
"add %2, %2, %8\n" // src += vs->pitch
"add %3, %3, %6\n" // dst += _vm->_screenWidth
"add %4, %4, %7\n" // text += _textSurface.pitch
"sub r5, r5, #1\n" // y -= 1
"cmp r5, #0\n" // y == 0?
"bne yLoop\n"
"b end\n"
"singleByteCompare:\n"
"ldrb r4, [%2, r3]\n" // Load text byte
"cmps r4, %5, lsr #24\n" // Compare with mask
"strneb r4, [%4, r3]\n" // Store if not equal
"ldreqb r4, [%3, r3]\n" // Otherwise Load src byte
"streqb r4, [%4, r3]\n" // Store it
"add r3, r3, #1\n"
"ldrb r4, [%2, r3]\n" // Load text byte
"cmps r4, %5, lsr #24\n" // Compare with mask
"strneb r4, [%4, r3]\n" // Store if not equal
"ldreqb r4, [%3, r3]\n" // Otherwise Load src byte
"streqb r4, [%4, r3]\n" // Store it
"add r3, r3, #1\n"
"ldrb r4, [%2, r3]\n" // Load text byte
"cmps r4, %5, lsr #24\n" // Compare with mask
"strneb r4, [%4, r3]\n" // Store if not equal
"ldreqb r4, [%3, r3]\n" // Otherwise Load src byte
"streqb r4, [%4, r3]\n" // Store it
"add r3, r3, #1\n"
"ldrb r4, [%2, r3]\n" // Load text byte
"cmps r4, %5, lsr #24\n" // Compare with mask
"strneb r4, [%4, r3]\n" // Store if not equal
"ldreqb r4, [%3, r3]\n" // Otherwise Load src byte
"streqb r4, [%4, r3]\n" // Store it
"add r3, r3, #1\n"
"cmps r3, %1\n" // x == width?
"blt xLoop\n" // Repeat
"add %2, %2, %8\n" // src += vs->pitch
"add %3, %3, %6\n" // dst += _vm->_screenWidth
"add %4, %4, %7\n" // text += _textSurface.pitch
"sub r5, r5, #1\n" // y -= 1
"cmp r5, #0\n" // y == 0?
"bne yLoop\n"
"end:\n"
: /* no output registers */
: "r" (height), "r" (width), "r" (text), "r" (src), "r" (dst), "r" (CHARSET_MASK_TRANSPARENCY | (CHARSET_MASK_TRANSPARENCY << 8) | (CHARSET_MASK_TRANSPARENCY << 16) | (CHARSET_MASK_TRANSPARENCY << 24)),
"r" (vsPitch), "r" (vmScreenWidth), "r" (textSurfacePitch)
: "r5", "r3", "r4", "%2", "%3", "%4", "memory");
}
void asmCopy8Col(byte *dst, int dstPitch, const byte *src, int height) {
asm("ands r0, %3, #1\n"
"addne %3, %3, #1\n"
"bne roll2\n"
"yLoop2:\n"
"ldr r0, [%2, #0]\n"
"str r0, [%0, #0]\n"
"ldr r0, [%2, #4]\n"
"str r0, [%0, #4]\n"
"add %0, %0, %1\n"
"add %2, %2, %1\n"
"roll2:\n"
"ldr r0, [%2, #0]\n"
"str r0, [%0, #0]\n"
"ldr r0, [%2, #4]\n"
"str r0, [%0, #4]\n"
"add %0, %0, %1\n"
"add %2, %2, %1\n"
"subs %3, %3, #2\n"
"bne yLoop2\n"
: /* no output registers */
: "r" (dst), "r" (dstPitch), "r" (src), "r" (height)
: "r0", "%0", "%2", "%3");
}
static bool isDivBy5Ready = false;
static u32 DIV_BY_5[160];
void ComputeDivBy5TableIFN() {
if (isDivBy5Ready)
return;
isDivBy5Ready = true;
for (int i = 0; i < 160; ++i) {
DIV_BY_5[i] = (2*i+5)/10;
}
}
#ifdef PERFECT_5_TO_4_RESCALING
static inline void RescaleBlock_5x1555_To_4x1555( u16 s0, u16 s1, u16 s2, u16 s3, u16 s4,
u16 *dest) {
u32 bs0 = s0 & 0x1F;
u32 bs1 = s1 & 0x1F;
u32 bs2 = s2 & 0x1F;
u32 bs3 = s3 & 0x1F;
u32 bs4 = s4 & 0x1F;
#if 0
u32 gs0 = (s0 >> 5) & 0x1F;
u32 gs1 = (s1 >> 5) & 0x1F;
u32 gs2 = (s2 >> 5) & 0x1F;
u32 gs3 = (s3 >> 5) & 0x1F;
u32 gs4 = (s4 >> 5) & 0x1F;
u32 rs0 = (s0 >> 10) & 0x1F;
u32 rs1 = (s1 >> 10) & 0x1F;
u32 rs2 = (s2 >> 10) & 0x1F;
u32 rs3 = (s3 >> 10) & 0x1F;
u32 rs4 = (s4 >> 10) & 0x1F;
#else
// The compiler absolutely wants to use 0x1F as an immediate, which makes it unable to fold the shift during the and
u32 mask = 0x1F;
u32 gs0, gs1, gs2, gs3, gs4;
asm("and %0, %2, %1, lsr #5" : "=r"(gs0) : "r"(s0), "r"(mask) : );
asm("and %0, %2, %1, lsr #5" : "=r"(gs1) : "r"(s1), "r"(mask) : );
asm("and %0, %2, %1, lsr #5" : "=r"(gs2) : "r"(s2), "r"(mask) : );
asm("and %0, %2, %1, lsr #5" : "=r"(gs3) : "r"(s3), "r"(mask) : );
asm("and %0, %2, %1, lsr #5" : "=r"(gs4) : "r"(s4), "r"(mask) : );
u32 rs0, rs1, rs2, rs3, rs4;
asm("and %0, %2, %1, lsr #10" : "=r"(rs0) : "r"(s0), "r"(mask) : );
asm("and %0, %2, %1, lsr #10" : "=r"(rs1) : "r"(s1), "r"(mask) : );
asm("and %0, %2, %1, lsr #10" : "=r"(rs2) : "r"(s2), "r"(mask) : );
asm("and %0, %2, %1, lsr #10" : "=r"(rs3) : "r"(s3), "r"(mask) : );
asm("and %0, %2, %1, lsr #10" : "=r"(rs4) : "r"(s4), "r"(mask) : );
#endif
u32 rd0 = 4*rs0 + rs1;
u32 rd1 = 2*rs1 + rs1 + 2*rs2;
u32 rd2 = 2*rs2 + 2*rs3 + rs3;
u32 rd3 = rs3 + 4*rs4;
u32 gd0 = 4*gs0 + gs1;
u32 gd1 = 2*gs1 + gs1 + 2*gs2;
u32 gd2 = 2*gs2 + 2*gs3 + gs3;
u32 gd3 = gs3 + 4*gs4;
u32 bd0 = 4*bs0 + bs1;
u32 bd1 = 2*bs1 + bs1 + 2*bs2;
u32 bd2 = 2*bs2 + 2*bs3 + bs3;
u32 bd3 = bs3 + 4*bs4;
#if 0
// Offsetting for correct rounding
rd0 = rd0*2+5; rd1 = rd1*2+5; rd2 = rd2*2+5; rd3 = rd3*2+5;
gd0 = gd0*2+5; gd1 = gd1*2+5; gd2 = gd2*2+5; gd3 = gd3*2+5;
bd0 = bd0*2+5; bd1 = bd1*2+5; bd2 = bd2*2+5; bd3 = bd3*2+5;
rd0 = (rd0 * 51) >> 9; rd1 = (rd1 * 51) >> 9; rd2 = (rd2 * 51) >> 9; rd3 = (rd3 * 51) >> 9;
gd0 = (gd0 * 51) >> 9; gd1 = (gd1 * 51) >> 9; gd2 = (gd2 * 51) >> 9; gd3 = (gd3 * 51) >> 9;
bd0 = (bd0 * 51) >> 9; bd1 = (bd1 * 51) >> 9; bd2 = (bd2 * 51) >> 9; bd3 = (bd3 * 51) >> 9;
#else
rd0 = DIV_BY_5[rd0]; rd1 = DIV_BY_5[rd1]; rd2 = DIV_BY_5[rd2]; rd3 = DIV_BY_5[rd3];
gd0 = DIV_BY_5[gd0]; gd1 = DIV_BY_5[gd1]; gd2 = DIV_BY_5[gd2]; gd3 = DIV_BY_5[gd3];
bd0 = DIV_BY_5[bd0]; bd1 = DIV_BY_5[bd1]; bd2 = DIV_BY_5[bd2]; bd3 = DIV_BY_5[bd3];
#endif
u32 d10 = 0x80008000 | (rd1 << 26) | (gd1 << 21) | (bd1 << 16) | (rd0 << 10) | (gd0 << 5) | bd0;
u32 d32 = 0x80008000 | (rd3 << 26) | (gd3 << 21) | (bd3 << 16) | (rd2 << 10) | (gd2 << 5) | bd2;
((u32 *)dest)[0] = d10;
((u32 *)dest)[1] = d32;
}
#else
static inline void RescaleBlock_5x1555_To_4x1555( u16 s0, u16 s1, u16 s2, u16 s3, u16 s4,
u16 *dest) {
static const u32 MASK = 0x03E07C1F;
u32 argbargbs0 = u32(s0) | (u32(s0) << 16);
u32 argbargbs1 = u32(s1) | (u32(s1) << 16);
u32 argbargbs2 = u32(s2) | (u32(s2) << 16);
u32 argbargbs3 = u32(s3) | (u32(s3) << 16);
u32 argbargbs4 = u32(s4) | (u32(s4) << 16);
u32 grbs0 = argbargbs0 & MASK;
u32 grbs1 = argbargbs1 & MASK;
u32 grbs2 = argbargbs2 & MASK;
u32 grbs3 = argbargbs3 & MASK;
u32 grbs4 = argbargbs4 & MASK;
u32 grbd0 = (3*grbs0 + grbs1) >> 2;
u32 grbd1 = ( grbs1 + grbs2) >> 1;
u32 grbd2 = ( grbs2 + grbs3) >> 1;
u32 grbd3 = ( grbs3 + 3*grbs4) >> 2;
grbd0 &= MASK;
grbd1 &= MASK;
grbd2 &= MASK;
grbd3 &= MASK;
u32 d0 = grbd0 | (grbd0 >> 16);
u32 d1 = grbd1 | (grbd1 >> 16);
u32 d2 = grbd2 | (grbd2 >> 16);
u32 d3 = grbd3 | (grbd3 >> 16);
d0 &= 0xFFFF;
d1 &= 0xFFFF;
d2 &= 0xFFFF;
d3 &= 0xFFFF;
d0 |= 0x8000;
d1 |= 0x8000;
d2 |= 0x8000;
d3 |= 0x8000;
dest[0] = d0;
dest[1] = d1;
dest[2] = d2;
dest[3] = d3;
}
#endif
static inline void RescaleBlock_5x8888_To_4x1555( u32 s0, u32 s1, u32 s2, u32 s3, u32 s4,
u16 *dest) {
u32 d0 = 4*s0 + s1;
u32 d1 = 2*s1 + s1 + 2*s2;
u32 bd0 = (d0 << 24) >> 24;
u32 bd1 = (d1 << 24) >> 24;
u32 gd0 = (d0 << 16) >> 24;
u32 gd1 = (d1 << 16) >> 24;
u32 rd0 = (d0 >> 16);
u32 rd1 = (d1 >> 16);
rd0 = DIV_BY_5[rd0]; rd1 = DIV_BY_5[rd1];
gd0 = DIV_BY_5[gd0]; gd1 = DIV_BY_5[gd1];
bd0 = DIV_BY_5[bd0]; bd1 = DIV_BY_5[bd1];
u32 d10 = 0x80008000 | (rd1 << 26) | (gd1 << 21) | (bd1 << 16) | (rd0 << 10) | (gd0 << 5) | bd0;
((u32 *)dest)[0] = d10;
u32 d2 = 2*s2 + 2*s3 + s3;
u32 d3 = s3 + 4*s4;
u32 bd2 = (d2 << 24) >> 24;
u32 bd3 = (d3 << 24) >> 24;
u32 gd2 = (d2 << 16) >> 24;
u32 gd3 = (d3 << 16) >> 24;
u32 rd2 = (d2 >> 16);
u32 rd3 = (d3 >> 16);
rd2 = DIV_BY_5[rd2]; rd3 = DIV_BY_5[rd3];
gd2 = DIV_BY_5[gd2]; gd3 = DIV_BY_5[gd3];
bd2 = DIV_BY_5[bd2]; bd3 = DIV_BY_5[bd3];
u32 d32 = 0x80008000 | (rd3 << 26) | (gd3 << 21) | (bd3 << 16) | (rd2 << 10) | (gd2 << 5) | bd2;
((u32 *)dest)[1] = d32;
}
// Can't work in place
#ifdef PERFECT_5_TO_4_RESCALING
static inline void Rescale_320xPAL8Scanline_To_256x1555Scanline(u16 *dest, const u8 *src, const u32 *palette) {
ComputeDivBy5TableIFN();
for (size_t i = 0; i < 64; ++i) {
u32 s0 = palette[src[5*i+0]];
u32 s1 = palette[src[5*i+1]];
u32 s2 = palette[src[5*i+2]];
u32 s3 = palette[src[5*i+3]];
u32 s4 = palette[src[5*i+4]];
RescaleBlock_5x8888_To_4x1555(s0, s1, s2, s3, s4, dest+4*i);
}
}
#else
static inline void Rescale_320xPAL8Scanline_To_256x1555Scanline(u16 *dest, const u8 *src, const u16 *palette) {
for (size_t i = 0; i < 64; ++i) {
u16 s0 = palette[src[5*i+0]];
u16 s1 = palette[src[5*i+1]];
u16 s2 = palette[src[5*i+2]];
u16 s3 = palette[src[5*i+3]];
u16 s4 = palette[src[5*i+4]];
RescaleBlock_5x1555_To_4x1555(s0, s1, s2, s3, s4, dest+4*i);
}
}
#endif
// Can work in place, because it's a contraction
static inline void Rescale_320x1555Scanline_To_256x1555Scanline(u16 *dest, const u16 *src) {
ComputeDivBy5TableIFN();
for (size_t i = 0; i < 64; ++i) {
u16 s0 = src[5*i+0];
u16 s1 = src[5*i+1];
u16 s2 = src[5*i+2];
u16 s3 = src[5*i+3];
u16 s4 = src[5*i+4];
RescaleBlock_5x1555_To_4x1555(s0, s1, s2, s3, s4, dest+4*i);
}
}
#ifdef PERFECT_5_TO_4_RESCALING
void Rescale_320x256xPAL8_To_256x256x1555(u16 *dest, const u8 *src, int destStride, int srcStride, const u16 *palette) {
u32 fastRam[768];
// Palette lookup -> 0_888
for (size_t i = 0; i < 256; ++i) {
u32 col = palette[i];
u32 result = col & 0x0000001F;
result |= (col << 3) & 0x00001F00;
result |= (col << 6) & 0x001F0000;
fastRam[i] = result;
}
for (size_t i = 0; i < 200; ++i) {
Rescale_320xPAL8Scanline_To_256x1555Scanline(dest + i*destStride, src + i *srcStride, fastRam);
}
}
#else
void Rescale_320x256xPAL8_To_256x256x1555(u16 *dest, const u8 *src, int destStride, int srcStride, const u16 *palette) {
u16 fastRam[256];
for (size_t i = 0; i < 128; ++i)
((u32 *)fastRam)[i] = ((const u32*)palette)[i];
for (size_t i = 0; i < 200; ++i) {
Rescale_320xPAL8Scanline_To_256x1555Scanline(dest + i*destStride, src + i *srcStride, fastRam);
}
}
#endif
void Rescale_320x256x1555_To_256x256x1555(u16 *dest, const u16 *src, int destStride, int srcStride) {
for (size_t i = 0; i < 200; ++i) {
Rescale_320x1555Scanline_To_256x1555Scanline(dest + i*destStride, src + i *srcStride);
}
}
} // End of namespace DS

View File

@ -20,35 +20,14 @@
*
*/
#ifndef _BLITTERS_H_
#define _BLITTERS_H_
#define USING_ARM_BLITTERS
#ifndef USING_ARM_BLITTERS
namespace DS {
void asmDrawStripToScreen(int height, int width, byte const *text, byte const *src, byte *dst,
int vsPitch, int vmScreenWidth, int textSurfacePitch);
void asmCopy8Col(byte *dst, int dstPitch, const byte *src, int height);
void Rescale_320x256xPAL8_To_256x256x1555(u16 *dest, const u8 *src, int destStride, int srcStride, const u16 *palette);
void Rescale_320x256x1555_To_256x256x1555(u16 *dest, const u16 *src, int destStride, int srcStride);
} // End of namespace DS
#else
#ifndef _BLITTERS_H_
#define _BLITTERS_H_
extern "C" {
void ITCM_CODE asmDrawStripToScreen(int height, int width, byte const *text, byte const *src, byte *dst,
int vsPitch, int vmScreenWidth, int textSurfacePitch);
void ITCM_CODE asmCopy8Col(byte *dst, int dstPitch, const byte *src, int height);
void ITCM_CODE Rescale_320x256xPAL8_To_256x256x1555(u16 *dest, const u8 *src, int destStride, int srcStride, const u16 *palette, u32 numLines);
void ITCM_CODE Rescale_320x256x1555_To_256x256x1555(u16 *dest, const u16 *src, int destStride, int srcStride);
}
#endif
#endif

View File

@ -80,7 +80,6 @@
#include "dsmain.h"
#include "osystem_ds.h"
#include "dsoptions.h"
#include "blitters.h"
#include "engines/engine.h"
#include "backends/plugins/ds/ds-provider.h"
@ -120,7 +119,6 @@ static int subScreenScale = 256;
// Saved buffers
static bool highBuffer;
static bool displayModeIs8Bit = false;
static bool gameScreenSwap = false;
@ -152,8 +150,6 @@ static int gameHeight = 200;
static bool twoHundredPercentFixedScale = false;
static bool cpuScalerEnable = false;
static u8 *scalerBackBuffer = NULL;
void setIcon(int num, int x, int y, int imageNum, int flags, bool enable);
void setIconMain(int num, int x, int y, int imageNum, int flags, bool enable);
@ -226,18 +222,6 @@ void initSprites() {
updateOAM();
}
void saveGameBackBuffer() {
// Sometimes the only copy of the game screen is in video memory.
// So, I lock the video memory here, as if I'm going to modify it. This
// forces OSystem_DS to create a system memory copy if one doesn't exist.
// This will be automatially restored by OSystem_DS::updateScreen().
OSystem_DS::instance()->lockScreen();
OSystem_DS::instance()->unlockScreen();
}
void set200PercentFixedScale(bool on) {
twoHundredPercentFixedScale = on;
}
@ -334,10 +318,6 @@ void setCursorIcon(const u8 *icon, uint w, uint h, byte keycolor, int hotspotX,
void displayMode16Bit() {
if (displayModeIs8Bit) {
saveGameBackBuffer();
}
videoSetMode(MODE_5_2D | DISPLAY_BG3_ACTIVE | DISPLAY_SPR_ACTIVE | DISPLAY_SPR_1D | DISPLAY_SPR_1D_BMP);
vramSetBankA(VRAM_A_MAIN_BG);
@ -346,7 +326,6 @@ void displayMode16Bit() {
vramSetBankD(VRAM_D_MAIN_BG);
REG_BG3CNT = BG_BMP16_512x256;
highBuffer = false;
memset(BG_GFX, 0, 512 * 256 * 2);
@ -361,30 +340,6 @@ void displayMode16Bit() {
}
void displayMode16BitFlipBuffer() {
if (!displayModeIs8Bit) {
u16 *back = get16BitBackBuffer();
if (isCpuScalerEnabled()) {
Rescale_320x256x1555_To_256x256x1555(BG_GFX, back, 512, 512);
} else {
for (int r = 0; r < 512 * 256; r++) {
*(BG_GFX + r) = *(back + r);
}
}
} else if (isCpuScalerEnabled()) {
const u8 *back = (const u8*)get8BitBackBuffer();
u16 *base = BG_GFX + 0x10000;
Rescale_320x256xPAL8_To_256x256x1555(
base,
back,
256,
get8BitBackBufferStride(),
BG_PALETTE,
getGameHeight() );
}
}
void setShakePos(int shakeXOffset, int shakeYOffset) {
s_shakeXOffset = shakeXOffset;
s_shakeYOffset = shakeYOffset;
@ -395,29 +350,6 @@ u16 *get16BitBackBuffer() {
return BG_GFX + 0x20000;
}
s32 get8BitBackBufferStride() {
// When the CPU scaler is enabled, the back buffer is in system RAM and is
// 320 pixels wide. When the CPU scaler is disabled, the back buffer is in
// video memory and therefore must have a 512 pixel stride.
if (isCpuScalerEnabled()){
return 320;
} else {
return 512;
}
}
u16 *getScalerBuffer() {
return (u16 *) scalerBackBuffer;
}
u16 *get8BitBackBuffer() {
if (isCpuScalerEnabled())
return (u16 *) scalerBackBuffer;
else
return BG_GFX + 0x10000; // 16bit qty!
}
void doTimerCallback() {
if (callback) {
if (callbackTimer <= 0) {
@ -673,7 +605,6 @@ void initHardware() {
// Allocate save buffer for game screen
displayMode16Bit();
memset(BG_GFX, 0, 512 * 256 * 2);
scaledMode = true;
scX = 0;
scY = 0;
@ -709,9 +640,6 @@ void initHardware() {
initSprites();
// If the software scaler's back buffer has not been allocated, do it now
scalerBackBuffer = (u8 *) malloc(320 * 256);
// This is a bodge to get around the fact that the cursor is turned on before it's image is set
// during startup in Sam & Max. This bodge moves the cursor offscreen so it is not seen.
sprites[1].attribute[1] = ATTR1_SIZE_64 | 192;

View File

@ -34,14 +34,8 @@ namespace DS {
void displayMode8Bit(); // Switch to 8-bit mode5
void displayMode16Bit(); // Switch to 16-bit mode5
// Flip double buffer
void displayMode16BitFlipBuffer();
// Get address of current back buffer
u16 * get16BitBackBuffer();
u16 * get8BitBackBuffer();
s32 get8BitBackBufferStride();
u16* getScalerBuffer();
void setTalkPos(int x, int y);
void setTopScreenTarget(int x, int y);

View File

@ -40,6 +40,7 @@
#include "osystem_ds.h"
#include "dsmain.h"
#include "blitters.h"
#include "common/config-manager.h"
#include "common/str.h"
#include "graphics/surface.h"
@ -56,7 +57,7 @@
OSystem_DS *OSystem_DS::_instance = NULL;
OSystem_DS::OSystem_DS()
: _eventSource(NULL), _mixer(NULL), _frameBufferExists(false),
: _eventSource(NULL), _mixer(NULL),
_disableCursorPalette(true), _graphicsEnable(true), _gammaValue(0)
{
_instance = this;
@ -111,6 +112,8 @@ bool OSystem_DS::getFeatureState(Feature f) {
}
void OSystem_DS::initSize(uint width, uint height, const Graphics::PixelFormat *format) {
_framebuffer.create(width, height, Graphics::PixelFormat::createFormatCLUT8());
// For Lost in Time, the title screen is displayed in 640x400.
// In order to support this game, the screen mode is set, but
// all draw calls are ignored until the game switches to 320x200.
@ -123,11 +126,11 @@ void OSystem_DS::initSize(uint width, uint height, const Graphics::PixelFormat *
}
int16 OSystem_DS::getHeight() {
return 200;
return _framebuffer.h;
}
int16 OSystem_DS::getWidth() {
return 320;
return _framebuffer.w;
}
void OSystem_DS::setPalette(const byte *colors, uint start, uint num) {
@ -187,155 +190,60 @@ void OSystem_DS::grabPalette(unsigned char *colors, uint start, uint num) const
}
}
#define MISALIGNED16(ptr) (((u32) (ptr) & 1) != 0)
void OSystem_DS::copyRectToScreen(const void *buf, int pitch, int x, int y, int w, int h) {
if (!_graphicsEnable) return;
if (w <= 1) return;
if (h < 0) return;
if (!DS::getIsDisplayMode8Bit()) return;
u16 *bg;
s32 stride;
#ifdef DISABLE_TEXT_CONSOLE
u16 *bgSub = (u16 *)BG_GFX_SUB;
#endif
// The DS video RAM doesn't support 8-bit writes because Nintendo wanted
// to save a few pennies/euro cents on the hardware.
if (_frameBufferExists) {
bg = (u16 *)_framebuffer.getPixels();
stride = _framebuffer.pitch;
} else {
bg = (u16 *)DS::get8BitBackBuffer();
stride = DS::get8BitBackBufferStride();
}
if (((pitch & 1) != 0) || ((w & 1) != 0) || (((int) (buf) & 1) != 0)) {
// Something is misaligned, so we have to use the slow but sure method
int by = 0;
for (int dy = y; dy < y + h; dy++) {
u8 *dest = ((u8 *) (bg)) + (dy * stride) + x;
#ifdef DISABLE_TEXT_CONSOLE
u8 *destSub = ((u8 *) (bgSub)) + (dy * 512) + x;
#endif
const u8 *src = (const u8 *) buf + (pitch * by);
u32 dx;
u32 pixelsLeft = w;
if (MISALIGNED16(dest)) {
// Read modify write
dest--;
u16 mix = *((u16 *) dest);
mix = (mix & 0x00FF) | (*src++ << 8);
*dest = mix;
#ifdef DISABLE_TEXT_CONSOLE
*destSub = mix;
#endif
dest += 2;
#ifdef DISABLE_TEXT_CONSOLE
destSub += 2;
#endif
pixelsLeft--;
}
// We can now assume dest is aligned
u16 *dest16 = (u16 *) dest;
#ifdef DISABLE_TEXT_CONSOLE
u16 *destSub16 = (u16 *) destSub;
#endif
for (dx = 0; dx < pixelsLeft; dx+=2) {
u16 mix;
mix = *src + (*(src + 1) << 8);
*dest16++ = mix;
#ifdef DISABLE_TEXT_CONSOLE
*destSub16++ = mix;
#endif
src += 2;
}
pixelsLeft -= dx;
// At the end we may have one pixel left over
if (pixelsLeft != 0) {
u16 mix = *dest16;
mix = (mix & 0x00FF) | ((*src++) << 8);
*dest16 = mix;
#ifdef DISABLE_TEXT_CONSOLE
*destSub16 = mix;
#endif
}
by++;
}
} else {
// Stuff is aligned to 16-bit boundaries, so it's safe to do DMA.
u16 *src = (u16 *) buf;
for (int dy = y; dy < y + h; dy++) {
u16 *dest1 = bg + (dy * (stride >> 1)) + (x >> 1);
#ifdef DISABLE_TEXT_CONSOLE
u16 *dest2 = bgSub + (dy << 8) + (x >> 1);
#endif
DC_FlushRange(src, w << 1);
DC_FlushRange(dest1, w << 1);
#ifdef DISABLE_TEXT_CONSOLE
DC_FlushRange(dest2, w << 1);
#endif
dmaCopyHalfWords(3, src, dest1, w);
#ifdef DISABLE_TEXT_CONSOLE
if ((!_frameBufferExists) || (buf == _framebuffer.getPixels())) {
dmaCopyHalfWords(2, src, dest2, w);
}
#endif
while (dmaBusy(2) || dmaBusy(3));
src += pitch >> 1;
}
}
_framebuffer.copyRectToSurface(buf, pitch, x, y, w, h);
}
void OSystem_DS::updateScreen() {
if ((_frameBufferExists) && (DS::getIsDisplayMode8Bit())) {
_frameBufferExists = false;
if (!DS::getIsDisplayMode8Bit()) {
u16 *back = DS::get16BitBackBuffer();
// Copy temp framebuffer back to screen
copyRectToScreen((byte *)_framebuffer.getPixels(), _framebuffer.pitch, 0, 0, _framebuffer.w, _framebuffer.h);
if (DS::isCpuScalerEnabled()) {
Rescale_320x256x1555_To_256x256x1555(BG_GFX, back, 512, 512);
} else {
for (int r = 0; r < 512 * 256; r++) {
*(BG_GFX + r) = *(back + r);
}
}
} else if (!_graphicsEnable) {
return;
} else if (DS::isCpuScalerEnabled()) {
u16 *base = BG_GFX + 0x10000;
Rescale_320x256xPAL8_To_256x256x1555(
base,
(const u8 *)_framebuffer.getPixels(),
256,
_framebuffer.pitch,
BG_PALETTE,
_framebuffer.h );
} else {
// The DS video RAM doesn't support 8-bit writes because Nintendo wanted
// to save a few pennies/euro cents on the hardware.
u16 *bg = BG_GFX + 0x10000;
s32 stride = 512;
u16 *src = (u16 *)_framebuffer.getPixels();
for (int dy = 0; dy < _framebuffer.h; dy++) {
DC_FlushRange(src, _framebuffer.w << 1);
u16 *dest1 = bg + (dy * (stride >> 1));
DC_FlushRange(dest1, _framebuffer.w << 1);
#ifdef DISABLE_TEXT_CONSOLE
u16 *dest2 = (u16 *)BG_GFX_SUB + (dy << 8);
DC_FlushRange(dest2, _framebuffer.w << 1);
dmaCopyHalfWords(2, src, dest2, _framebuffer.w);
#endif
dmaCopyHalfWords(3, src, dest1, _framebuffer.w);
while (dmaBusy(2) || dmaBusy(3));
src += _framebuffer.pitch >> 1;
}
}
DS::displayMode16BitFlipBuffer();
// FIXME: Evil game specific hack.
// Force back buffer usage for Nippon Safes, as it doesn't double buffer it's output
// if (DS::getControlType() == DS::CONT_NIPPON) {
// lockScreen();
// }
}
void OSystem_DS::setShakePos(int shakeXOffset, int shakeYOffset) {
@ -470,56 +378,7 @@ void OSystem_DS::deleteMutex(MutexRef mutex) {
void OSystem_DS::quit() {
}
Graphics::Surface *OSystem_DS::createTempFrameBuffer() {
// Ensure we copy using 16 bit quantities due to limitation of VRAM addressing
// If the scaler is enabled, we can just return the 8 bit back buffer,
// since it's in system memory anyway. Otherwise, we need to copy the back
// buffer into the memory normally used by the scaler buffer and then
// return it.
// We also must ensure that once the frame buffer is created, future calls
// to copyRectToScreen() copy to this buffer.
if (DS::isCpuScalerEnabled()) {
_framebuffer.init(DS::getGameWidth(), DS::getGameHeight(), DS::getGameWidth(),
DS::getScalerBuffer(), Graphics::PixelFormat::createFormatCLUT8());
} else {
s32 height = DS::getGameHeight();
s32 width = DS::getGameWidth();
s32 stride = DS::get8BitBackBufferStride();
u16 *src = DS::get8BitBackBuffer();
u16 *dest = DS::getScalerBuffer();
for (int y = 0; y < height; y++) {
u16 *destLine = dest + (y * (width / 2));
u16 *srcLine = src + (y * (stride / 2));
DC_FlushRange(srcLine, width);
dmaCopyHalfWords(3, srcLine, destLine, width);
}
_framebuffer.init(width, height, width, dest, Graphics::PixelFormat::createFormatCLUT8());
}
_frameBufferExists = true;
return &_framebuffer;
}
Graphics::Surface *OSystem_DS::lockScreen() {
if (!_frameBufferExists) {
createTempFrameBuffer();
}
return &_framebuffer;
}

View File

@ -39,7 +39,6 @@ class OSystem_DS : public BaseBackend, public PaletteManager {
protected:
Audio::MixerImpl *_mixer;
Graphics::Surface _framebuffer;
bool _frameBufferExists;
bool _graphicsEnable;
static OSystem_DS *_instance;