aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/library_sdl.js9
-rwxr-xr-xtests/runner.py3
-rw-r--r--tests/sdl_surface_refcount.c28
3 files changed, 40 insertions, 0 deletions
diff --git a/src/library_sdl.js b/src/library_sdl.js
index 7078aa9d..4477e457 100644
--- a/src/library_sdl.js
+++ b/src/library_sdl.js
@@ -276,6 +276,8 @@ var LibrarySDL = {
{{{ makeSetValue('surf+Runtime.QUANTUM_SIZE*5', '0', 'buffer', 'void*') }}} // SDL_Surface.pixels
{{{ makeSetValue('surf+Runtime.QUANTUM_SIZE*6', '0', '0', 'i32*') }}} // SDL_Surface.offset
+ {{{ makeSetValue('surf+Runtime.QUANTUM_SIZE*14', '0', '1', 'i32') }}}
+
{{{ makeSetValue('pixelFormat + SDL.structs.PixelFormat.format', '0', '-2042224636', 'i32') }}} // SDL_PIXELFORMAT_RGBA8888
{{{ makeSetValue('pixelFormat + SDL.structs.PixelFormat.palette', '0', '0', 'i32') }}} // TODO
{{{ makeSetValue('pixelFormat + SDL.structs.PixelFormat.BitsPerPixel', '0', 'bpp * 8', 'i8') }}}
@@ -362,6 +364,13 @@ var LibrarySDL = {
},
freeSurface: function(surf) {
+ var refcountPointer = surf + Runtime.QUANTUM_SIZE * 14;
+ var refcount = {{{ makeGetValue('refcountPointer', '0', 'i32') }}};
+ if (refcount > 1) {
+ {{{ makeSetValue('refcountPointer', '0', 'refcount - 1', 'i32') }}};
+ return;
+ }
+
var info = SDL.surfaces[surf];
if (!info.usePageCanvas && info.canvas) SDL.canvasPool.push(info.canvas);
_free(info.buffer);
diff --git a/tests/runner.py b/tests/runner.py
index fc41c8a1..d16472ad 100755
--- a/tests/runner.py
+++ b/tests/runner.py
@@ -13523,6 +13523,9 @@ Press any key to continue.'''
def test_sdl_alloctext(self):
self.btest('sdl_alloctext.c', expected='1', args=['-O2', '-s', 'TOTAL_MEMORY=' + str(1024*1024*8)])
+ def test_sdl_surface_refcount(self):
+ self.btest('sdl_surface_refcount.c', expected='1')
+
def test_glbegin_points(self):
shutil.copyfile(path_from_root('tests', 'screenshot.png'), os.path.join(self.get_dir(), 'screenshot.png'))
self.btest('glbegin_points.c', reference='glbegin_points.png', args=['--preload-file', 'screenshot.png'])
diff --git a/tests/sdl_surface_refcount.c b/tests/sdl_surface_refcount.c
new file mode 100644
index 00000000..5e0f30a9
--- /dev/null
+++ b/tests/sdl_surface_refcount.c
@@ -0,0 +1,28 @@
+#include <emscripten.h>
+#include <SDL.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+int is_surface_freed(SDL_Surface *surface)
+{
+ const char *template = "!SDL.surfaces[%d]";
+ int length = snprintf(NULL, 0, template, surface) + 1;
+ char *script = malloc(length * sizeof(char));
+ snprintf(script, length, template, surface);
+ int is_freed = emscripten_run_script_int(script);
+ free(script);
+ return is_freed;
+}
+
+int main(int argc, char *argv[])
+{
+ SDL_Surface *surface = SDL_CreateRGBSurface(SDL_SWSURFACE, 10, 10, 32,
+ 0, 0, 0, 0);
+ SDL_Surface *reference = surface;
+ reference->refcount++;
+ SDL_FreeSurface(surface);
+ SDL_FreeSurface(reference);
+ int result = is_surface_freed(surface);
+ REPORT_RESULT();
+ return 0;
+}