From ad339aa8271c1696fe69f8e60b564b85fbb1b221 Mon Sep 17 00:00:00 2001 From: Joshua Granick Date: Mon, 19 Dec 2016 19:50:56 -0800 Subject: [PATCH] Revert Bytes changes, prevents ppremature GC on audio decoding --- lime/_backend/native/NativeRenderer.hx | 8 +- lime/audio/AudioBuffer.hx | 26 ++++- lime/graphics/Image.hx | 14 ++- lime/graphics/format/JPEG.hx | 25 +++- lime/graphics/format/PNG.hx | 24 +++- lime/utils/Bytes.hx | 8 +- lime/utils/compress/Deflate.hx | 16 ++- lime/utils/compress/GZip.hx | 16 ++- lime/utils/compress/LZMA.hx | 16 ++- lime/utils/compress/Zlib.hx | 16 ++- project/include/audio/AudioBuffer.h | 2 +- project/include/graphics/Image.h | 2 +- project/include/graphics/ImageBuffer.h | 3 +- project/include/utils/ArrayBufferView.h | 3 +- project/include/utils/Bytes.h | 7 +- project/src/ExternalInterface.cpp | 102 ++++++++-------- project/src/audio/AudioBuffer.cpp | 11 +- project/src/audio/format/OGG.cpp | 28 +++-- project/src/audio/format/WAV.cpp | 8 +- project/src/backend/sdl/SDLRenderer.cpp | 4 +- project/src/backend/sdl/SDLWindow.cpp | 2 +- project/src/graphics/Image.cpp | 4 +- project/src/graphics/ImageBuffer.cpp | 100 ++++++++-------- project/src/graphics/format/JPEG.cpp | 22 +++- project/src/graphics/format/PNG.cpp | 11 +- project/src/graphics/utils/ImageDataUtil.cpp | 116 +++++++++---------- project/src/utils/ArrayBufferView.cpp | 113 +++++++++--------- project/src/utils/Bytes.cpp | 46 ++++++-- 28 files changed, 451 insertions(+), 302 deletions(-) diff --git a/lime/_backend/native/NativeRenderer.hx b/lime/_backend/native/NativeRenderer.hx index 78ca51801..6cfc571d7 100644 --- a/lime/_backend/native/NativeRenderer.hx +++ b/lime/_backend/native/NativeRenderer.hx @@ -138,11 +138,15 @@ class NativeRenderer { var imageBuffer:ImageBuffer = null; #if !macro - var data:Dynamic = lime_renderer_read_pixels (handle, rect); + #if !cs + imageBuffer = lime_renderer_read_pixels (handle, rect, new ImageBuffer (new UInt8Array (Bytes.alloc (0)))); + #else + var data:Dynamic = lime_renderer_read_pixels (handle, rect, null); if (data != null) { imageBuffer = new ImageBuffer (new UInt8Array (@:privateAccess new Bytes (data.data.length, data.data.b)), data.width, data.height, data.bitsPerPixel); } #end + #end if (imageBuffer != null) { @@ -208,7 +212,7 @@ class NativeRenderer { @:cffi private static function lime_renderer_get_type (handle:Dynamic):Dynamic; @:cffi private static function lime_renderer_lock (handle:Dynamic):Dynamic; @:cffi private static function lime_renderer_make_current (handle:Dynamic):Void; - @:cffi private static function lime_renderer_read_pixels (handle:Dynamic, rect:Dynamic):Dynamic; + @:cffi private static function lime_renderer_read_pixels (handle:Dynamic, rect:Dynamic, imageBuffer:Dynamic):Dynamic; @:cffi private static function lime_renderer_unlock (handle:Dynamic):Void; #end diff --git a/lime/audio/AudioBuffer.hx b/lime/audio/AudioBuffer.hx index 113f1e5f5..38f0491c1 100644 --- a/lime/audio/AudioBuffer.hx +++ b/lime/audio/AudioBuffer.hx @@ -95,8 +95,16 @@ class AudioBuffer { lime.Lib.notImplemented ("AudioBuffer.fromBytes"); #elseif (lime_cffi && !macro) + #if !cs - var data:Dynamic = lime_audio_load (bytes); + var audioBuffer = new AudioBuffer (); + audioBuffer.data = new UInt8Array (Bytes.alloc (0)); + + return lime_audio_load (bytes, audioBuffer); + + #else + + var data:Dynamic = lime_audio_load (bytes, null); if (data != null) { @@ -109,6 +117,7 @@ class AudioBuffer { } + #end #end return null; @@ -165,15 +174,23 @@ class AudioBuffer { } #elseif (lime_cffi && !macro) + #if !cs - var data:Dynamic = lime_audio_load (path); + var audioBuffer = new AudioBuffer (); + audioBuffer.data = new UInt8Array (Bytes.alloc (0)); + + return lime_audio_load (path, audioBuffer); + + #else + + var data:Dynamic = lime_audio_load (path, null); if (data != null) { var audioBuffer = new AudioBuffer (); audioBuffer.bitsPerSample = data.bitsPerSample; audioBuffer.channels = data.channels; - audioBuffer.data = new UInt8Array (@:privateAccess new Bytes (data.data.byteLength, data.data.buffer.b)); + audioBuffer.data = new UInt8Array (@:privateAccess new Bytes (data.data.length, data.data.b)); audioBuffer.sampleRate = data.sampleRate; return audioBuffer; @@ -181,6 +198,7 @@ class AudioBuffer { return null; + #end #else return null; @@ -443,7 +461,7 @@ class AudioBuffer { #if (lime_cffi && !macro) - @:cffi private static function lime_audio_load (data:Dynamic):Dynamic; + @:cffi private static function lime_audio_load (data:Dynamic, buffer:Dynamic):Dynamic; #end diff --git a/lime/graphics/Image.hx b/lime/graphics/Image.hx index 40073d954..4b441b563 100644 --- a/lime/graphics/Image.hx +++ b/lime/graphics/Image.hx @@ -1237,11 +1237,15 @@ class Image { #elseif (lime_cffi && !macro) var imageBuffer:ImageBuffer = null; - var data = lime_image_load (bytes); + #if !cs + imageBuffer = lime_image_load (bytes, new ImageBuffer (new UInt8Array (Bytes.alloc (0)))); + #else + var data = lime_image_load (bytes, null); if (data != null) { imageBuffer = new ImageBuffer (new UInt8Array (@:privateAccess new Bytes (data.data.buffer.length, data.data.buffer.b)), data.width, data.height, data.bitsPerPixel); } + #end if (imageBuffer != null) { @@ -1361,10 +1365,14 @@ class Image { #else if (CFFI.enabled) { - var data = lime_image_load (path); + #if !cs + buffer = lime_image_load (path, new ImageBuffer (new UInt8Array (Bytes.alloc (0)))); + #else + var data = lime_image_load (path, null); if (data != null) { buffer = new ImageBuffer (new UInt8Array (@:privateAccess new Bytes (data.data.buffer.length, data.data.buffer.b)), data.width, data.height, data.bitsPerPixel); } + #end } #end @@ -1707,7 +1715,7 @@ class Image { #if (lime_cffi && !macro) - @:cffi private static function lime_image_load (data:Dynamic):Dynamic; + @:cffi private static function lime_image_load (data:Dynamic, buffer:Dynamic):Dynamic; #end diff --git a/lime/graphics/format/JPEG.hx b/lime/graphics/format/JPEG.hx index 3b8238f3b..ed10314da 100644 --- a/lime/graphics/format/JPEG.hx +++ b/lime/graphics/format/JPEG.hx @@ -31,7 +31,10 @@ class JPEG { #if (lime_cffi && !macro) - var bufferData:Dynamic = lime_jpeg_decode_bytes (bytes, decodeData); + #if !cs + return lime_jpeg_decode_bytes (bytes, decodeData, new ImageBuffer (new UInt8Array (Bytes.alloc (0)))); + #else + var bufferData:Dynamic = lime_jpeg_decode_bytes (bytes, decodeData, null); if (bufferData != null) { @@ -40,6 +43,7 @@ class JPEG { return new Image (buffer); } + #end #end @@ -52,7 +56,10 @@ class JPEG { #if (lime_cffi && !macro) - var bufferData:Dynamic = lime_jpeg_decode_file (path, decodeData); + #if !cs + return lime_jpeg_decode_file (path, decodeData, new ImageBuffer (new UInt8Array (Bytes.alloc (0)))); + #else + var bufferData:Dynamic = lime_jpeg_decode_file (path, decodeData, null); if (bufferData != null) { @@ -63,6 +70,8 @@ class JPEG { } #end + #end + return null; } @@ -84,8 +93,12 @@ class JPEG { #elseif (sys && (!disable_cffi || !format) && !macro) - var data:Dynamic = lime_image_encode (image.buffer, 1, quality); + #if !cs + return lime_image_encode (image.buffer, 1, quality, Bytes.alloc (0)); + #else + var data:Dynamic = lime_image_encode (image.buffer, 1, quality, null); return @:privateAccess new Bytes (data.length, data.b); + #end #elseif (js && html5) @@ -122,9 +135,9 @@ class JPEG { #if (lime_cffi && !macro) - @:cffi private static function lime_jpeg_decode_bytes (data:Dynamic, decodeData:Bool):Dynamic; - @:cffi private static function lime_jpeg_decode_file (path:String, decodeData:Bool):Dynamic; - @:cffi private static function lime_image_encode (data:Dynamic, type:Int, quality:Int):Dynamic; + @:cffi private static function lime_jpeg_decode_bytes (data:Dynamic, decodeData:Bool, buffer:Dynamic):Dynamic; + @:cffi private static function lime_jpeg_decode_file (path:String, decodeData:Bool, buffer:Dynamic):Dynamic; + @:cffi private static function lime_image_encode (data:Dynamic, type:Int, quality:Int, bytes:Dynamic):Dynamic; #end diff --git a/lime/graphics/format/PNG.hx b/lime/graphics/format/PNG.hx index 0ebff18e9..5347064dd 100644 --- a/lime/graphics/format/PNG.hx +++ b/lime/graphics/format/PNG.hx @@ -38,7 +38,10 @@ class PNG { #if (lime_cffi && !macro) - var bufferData:Dynamic = lime_png_decode_bytes (bytes, decodeData); + #if !cs + return lime_png_decode_bytes (bytes, decodeData, new ImageBuffer (new UInt8Array (Bytes.alloc (0)))); + #else + var bufferData:Dynamic = lime_png_decode_bytes (bytes, decodeData, null); if (bufferData != null) { @@ -47,6 +50,7 @@ class PNG { return new Image (buffer); } + #end #end @@ -59,7 +63,10 @@ class PNG { #if (lime_cffi && !macro) - var bufferData:Dynamic = lime_png_decode_file (path, decodeData); + #if !cs + return lime_png_decode_file (path, decodeData, new ImageBuffer (new UInt8Array (Bytes.alloc (0)))); + #else + var bufferData:Dynamic = lime_png_decode_file (path, decodeData, null); if (bufferData != null) { @@ -68,6 +75,7 @@ class PNG { return new Image (buffer); } + #end #end @@ -94,8 +102,12 @@ class PNG { if (CFFI.enabled) { - var data:Dynamic = lime_image_encode (image.buffer, 0, 0); + #if !cs + return lime_image_encode (image.buffer, 0, 0, Bytes.alloc (0)); + #else + var data:Dynamic = lime_image_encode (image.buffer, 0, 0, null); return @:privateAccess new Bytes (data.length, data.b); + #end } #end @@ -171,9 +183,9 @@ class PNG { #if (lime_cffi && !macro) - @:cffi private static function lime_png_decode_bytes (data:Dynamic, decodeData:Bool):Dynamic; - @:cffi private static function lime_png_decode_file (path:String, decodeData:Bool):Dynamic; - @:cffi private static function lime_image_encode (data:Dynamic, type:Int, quality:Int):Dynamic; + @:cffi private static function lime_png_decode_bytes (data:Dynamic, decodeData:Bool, buffer:Dynamic):Dynamic; + @:cffi private static function lime_png_decode_file (path:String, decodeData:Bool, buffer:Dynamic):Dynamic; + @:cffi private static function lime_image_encode (data:Dynamic, type:Int, quality:Int, bytes:Dynamic):Dynamic; #end diff --git a/lime/utils/Bytes.hx b/lime/utils/Bytes.hx index aa455d365..0f954099a 100644 --- a/lime/utils/Bytes.hx +++ b/lime/utils/Bytes.hx @@ -53,9 +53,15 @@ abstract Bytes(HaxeBytes) from HaxeBytes to HaxeBytes { public static function fromFile (path:String):Bytes { #if (!html5 && !macro) + #if !cs + var bytes = Bytes.alloc (0); + lime_bytes_read_file (path, bytes); + if (bytes.length > 0) return bytes; + #else var data:Dynamic = lime_bytes_read_file (path); if (data != null) return new Bytes (data.length, data.b); #end + #end return null; } @@ -112,7 +118,7 @@ abstract Bytes(HaxeBytes) from HaxeBytes to HaxeBytes { #if !macro @:cffi private static function lime_bytes_from_data_pointer (data:Float, length:Int):Dynamic; @:cffi private static function lime_bytes_get_data_pointer (data:Dynamic):Float; - @:cffi private static function lime_bytes_read_file (path:String):Dynamic; + @:cffi private static function lime_bytes_read_file (path:String, bytes:Dynamic):Dynamic; #end diff --git a/lime/utils/compress/Deflate.hx b/lime/utils/compress/Deflate.hx index 559a392a9..4fb9543da 100644 --- a/lime/utils/compress/Deflate.hx +++ b/lime/utils/compress/Deflate.hx @@ -24,9 +24,13 @@ class Deflate { #if (lime_cffi && !macro) - var data:Dynamic = lime_deflate_compress (bytes); + #if !cs + return lime_deflate_compress (bytes, Bytes.alloc (0)); + #else + var data:Dynamic = lime_deflate_compress (bytes, null); if (data == null) return null; return @:privateAccess new Bytes (data.length, data.b); + #end #elseif (js && html5) @@ -56,9 +60,13 @@ class Deflate { #if (lime_cffi && !macro) - var data:Dynamic = lime_deflate_decompress (bytes); + #if !cs + return lime_deflate_decompress (bytes, Bytes.alloc (0)); + #else + var data:Dynamic = lime_deflate_decompress (bytes, null); if (data == null) return null; return @:privateAccess new Bytes (data.length, data.b); + #end #elseif (js && html5) @@ -92,8 +100,8 @@ class Deflate { #if (lime_cffi && !macro) - @:cffi private static function lime_deflate_compress (data:Dynamic):Dynamic; - @:cffi private static function lime_deflate_decompress (data:Dynamic):Dynamic; + @:cffi private static function lime_deflate_compress (data:Dynamic, bytes:Dynamic):Dynamic; + @:cffi private static function lime_deflate_decompress (data:Dynamic, bytes:Dynamic):Dynamic; #end diff --git a/lime/utils/compress/GZip.hx b/lime/utils/compress/GZip.hx index 087b9869a..571ae9367 100644 --- a/lime/utils/compress/GZip.hx +++ b/lime/utils/compress/GZip.hx @@ -20,9 +20,13 @@ class GZip { #if (lime_cffi && !macro) - var data:Dynamic = lime_gzip_compress (bytes); + #if !cs + return lime_gzip_compress (bytes, Bytes.alloc (0)); + #else + var data:Dynamic = lime_gzip_compress (bytes, null); if (data == null) return null; return @:privateAccess new Bytes (data.length, data.b); + #end #elseif (js && html5) @@ -42,9 +46,13 @@ class GZip { #if (lime_cffi && !macro) - var data:Dynamic = lime_gzip_decompress (bytes); + #if !cs + return lime_gzip_decompress (bytes, Bytes.alloc (0)); + #else + var data:Dynamic = lime_gzip_decompress (bytes, null); if (data == null) return null; return @:privateAccess new Bytes (data.length, data.b); + #end #elseif (js && html5) @@ -68,8 +76,8 @@ class GZip { #if (lime_cffi && !macro) - @:cffi private static function lime_gzip_compress (data:Dynamic):Dynamic; - @:cffi private static function lime_gzip_decompress (data:Dynamic):Dynamic; + @:cffi private static function lime_gzip_compress (data:Dynamic, bytes:Dynamic):Dynamic; + @:cffi private static function lime_gzip_decompress (data:Dynamic, bytes:Dynamic):Dynamic; #end diff --git a/lime/utils/compress/LZMA.hx b/lime/utils/compress/LZMA.hx index 968fcddc0..6aebf4d20 100644 --- a/lime/utils/compress/LZMA.hx +++ b/lime/utils/compress/LZMA.hx @@ -25,9 +25,13 @@ class LZMA { #if (lime_cffi && !macro) - var data:Dynamic = lime_lzma_compress (bytes); + #if !cs + return lime_lzma_compress (bytes, Bytes.alloc (0)); + #else + var data:Dynamic = lime_lzma_compress (bytes, null); if (data == null) return null; return @:privateAccess new Bytes (data.length, data.b); + #end #elseif flash @@ -52,9 +56,13 @@ class LZMA { #if (lime_cffi && !macro) - var data:Dynamic = lime_lzma_decompress (bytes); + #if !cs + return lime_lzma_decompress (bytes, Bytes.alloc (0)); + #else + var data:Dynamic = lime_lzma_decompress (bytes, null); if (data == null) return null; return @:privateAccess new Bytes (data.length, data.b); + #end #elseif flash @@ -83,8 +91,8 @@ class LZMA { #if (lime_cffi && !macro) - @:cffi private static function lime_lzma_compress (data:Dynamic):Dynamic; - @:cffi private static function lime_lzma_decompress (data:Dynamic):Dynamic; + @:cffi private static function lime_lzma_compress (data:Dynamic, bytes:Dynamic):Dynamic; + @:cffi private static function lime_lzma_decompress (data:Dynamic, bytes:Dynamic):Dynamic; #end diff --git a/lime/utils/compress/Zlib.hx b/lime/utils/compress/Zlib.hx index 386748e47..09c70ba20 100644 --- a/lime/utils/compress/Zlib.hx +++ b/lime/utils/compress/Zlib.hx @@ -24,9 +24,13 @@ class Zlib { #if (lime_cffi && !macro) - var data:Dynamic = lime_zlib_compress (bytes); + #if !cs + return lime_zlib_compress (bytes, Bytes.alloc (0)); + #else + var data:Dynamic = lime_zlib_compress (bytes, null); if (data == null) return null; return @:privateAccess new Bytes (data.length, data.b); + #end #elseif (js && html5) @@ -56,9 +60,13 @@ class Zlib { #if (lime_cffi && !macro) - var data:Dynamic = lime_zlib_decompress (bytes); + #if !cs + return lime_zlib_decompress (bytes, Bytes.alloc (0)); + #else + var data:Dynamic = lime_zlib_decompress (bytes, null); if (data == null) return null; return @:privateAccess new Bytes (data.length, data.b); + #end #elseif (js && html5) @@ -92,8 +100,8 @@ class Zlib { #if (lime_cffi && !macro) - @:cffi private static function lime_zlib_compress (data:Dynamic):Dynamic; - @:cffi private static function lime_zlib_decompress (data:Dynamic):Dynamic; + @:cffi private static function lime_zlib_compress (data:Dynamic, bytes:Dynamic):Dynamic; + @:cffi private static function lime_zlib_decompress (data:Dynamic, bytes:Dynamic):Dynamic; #end diff --git a/project/include/audio/AudioBuffer.h b/project/include/audio/AudioBuffer.h index 840e0633a..154261047 100644 --- a/project/include/audio/AudioBuffer.h +++ b/project/include/audio/AudioBuffer.h @@ -43,7 +43,7 @@ namespace lime { int bitsPerSample; int channels; int sampleRate; - ArrayBufferView data; + ArrayBufferView *data; private: diff --git a/project/include/graphics/Image.h b/project/include/graphics/Image.h index dfd0f406e..ef8bd6eb9 100644 --- a/project/include/graphics/Image.h +++ b/project/include/graphics/Image.h @@ -18,7 +18,7 @@ namespace lime { Image (value image); ~Image (); - ImageBuffer buffer; + ImageBuffer *buffer; int height; int offsetX; int offsetY; diff --git a/project/include/graphics/ImageBuffer.h b/project/include/graphics/ImageBuffer.h index beb62c6be..d286a2cc2 100644 --- a/project/include/graphics/ImageBuffer.h +++ b/project/include/graphics/ImageBuffer.h @@ -21,12 +21,11 @@ namespace lime { void Blit (const unsigned char *data, int x, int y, int width, int height); void Resize (int width, int height, int bitsPerPixel = 32); - void Set (value imageBuffer); int Stride (); value Value (); int bitsPerPixel; - ArrayBufferView data; + ArrayBufferView *data; PixelFormat format; int height; bool premultiplied; diff --git a/project/include/utils/ArrayBufferView.h b/project/include/utils/ArrayBufferView.h index c4e90b177..b24d6f4af 100644 --- a/project/include/utils/ArrayBufferView.h +++ b/project/include/utils/ArrayBufferView.h @@ -19,7 +19,6 @@ namespace lime { ArrayBufferView (value arrayBufferView); ~ArrayBufferView (); - void Clear (); unsigned char *Data (); const unsigned char *Data () const; int Length () const; @@ -28,7 +27,7 @@ namespace lime { void Set (const QuickVec data); value Value (); - Bytes buffer; + Bytes *buffer; int byteLength; int length; diff --git a/project/include/utils/Bytes.h b/project/include/utils/Bytes.h index 574135081..57017ca15 100644 --- a/project/include/utils/Bytes.h +++ b/project/include/utils/Bytes.h @@ -9,10 +9,7 @@ namespace lime { - class Bytes { - - - public: + struct Bytes { Bytes (); @@ -22,7 +19,6 @@ namespace lime { Bytes (const QuickVec data); ~Bytes (); - void Clear (); unsigned char *Data (); const unsigned char *Data () const; int Length () const; @@ -34,6 +30,7 @@ namespace lime { unsigned char *_data; int _length; + AutoGCRoot *_root; value _value; diff --git a/project/src/ExternalInterface.cpp b/project/src/ExternalInterface.cpp index 19b260b6f..d6e6bc28d 100644 --- a/project/src/ExternalInterface.cpp +++ b/project/src/ExternalInterface.cpp @@ -155,12 +155,12 @@ namespace lime { } - value lime_audio_load (value data) { + value lime_audio_load (value data, value buffer) { Resource resource; Bytes bytes; - AudioBuffer audioBuffer; + AudioBuffer audioBuffer = AudioBuffer (buffer); if (val_is_string (data)) { @@ -216,9 +216,9 @@ namespace lime { } - value lime_bytes_read_file (HxString path) { + value lime_bytes_read_file (HxString path, value bytes) { - Bytes data; + Bytes data (bytes); data.ReadFile (path.c_str ()); return data.Value (); @@ -276,11 +276,11 @@ namespace lime { } - value lime_deflate_compress (value buffer) { + value lime_deflate_compress (value buffer, value bytes) { #ifdef LIME_ZLIB Bytes data (buffer); - Bytes result; + Bytes result (bytes); Zlib::Compress (DEFLATE, &data, &result); @@ -292,11 +292,11 @@ namespace lime { } - value lime_deflate_decompress (value buffer) { + value lime_deflate_decompress (value buffer, value bytes) { #ifdef LIME_ZLIB Bytes data (buffer); - Bytes result; + Bytes result (bytes); Zlib::Decompress (DEFLATE, &data, &result); @@ -666,11 +666,11 @@ namespace lime { } - value lime_gzip_compress (value buffer) { + value lime_gzip_compress (value buffer, value bytes) { #ifdef LIME_ZLIB Bytes data (buffer); - Bytes result; + Bytes result (bytes); Zlib::Compress (GZIP, &data, &result); @@ -682,11 +682,11 @@ namespace lime { } - value lime_gzip_decompress (value buffer) { + value lime_gzip_decompress (value buffer, value bytes) { #ifdef LIME_ZLIB Bytes data (buffer); - Bytes result; + Bytes result (bytes); Zlib::Decompress (GZIP, &data, &result); @@ -707,10 +707,10 @@ namespace lime { } - value lime_image_encode (value buffer, int type, int quality) { + value lime_image_encode (value buffer, int type, int quality, value bytes) { ImageBuffer imageBuffer = ImageBuffer (buffer); - Bytes data; + Bytes data = Bytes (bytes); switch (type) { @@ -745,12 +745,12 @@ namespace lime { } - value lime_image_load (value data) { + value lime_image_load (value data, value buffer) { Resource resource; Bytes bytes; - ImageBuffer imageBuffer; + ImageBuffer imageBuffer = ImageBuffer (buffer); if (val_is_string (data)) { @@ -989,9 +989,9 @@ namespace lime { } - value lime_jpeg_decode_bytes (value data, bool decodeData) { + value lime_jpeg_decode_bytes (value data, bool decodeData, value buffer) { - ImageBuffer imageBuffer; + ImageBuffer imageBuffer (buffer); Bytes bytes (data); Resource resource = Resource (&bytes); @@ -1009,9 +1009,9 @@ namespace lime { } - value lime_jpeg_decode_file (HxString path, bool decodeData) { + value lime_jpeg_decode_file (HxString path, bool decodeData, value buffer) { - ImageBuffer imageBuffer; + ImageBuffer imageBuffer (buffer); Resource resource = Resource (path.c_str ()); #ifdef LIME_JPEG @@ -1054,11 +1054,11 @@ namespace lime { } - value lime_lzma_compress (value buffer) { + value lime_lzma_compress (value buffer, value bytes) { #ifdef LIME_LZMA Bytes data (buffer); - Bytes result; + Bytes result (bytes); LZMA::Compress (&data, &result); @@ -1070,11 +1070,11 @@ namespace lime { } - value lime_lzma_decompress (value buffer) { + value lime_lzma_decompress (value buffer, value bytes) { #ifdef LIME_LZMA Bytes data (buffer); - Bytes result; + Bytes result (bytes); LZMA::Decompress (&data, &result); @@ -1146,9 +1146,9 @@ namespace lime { } - value lime_png_decode_bytes (value data, bool decodeData) { + value lime_png_decode_bytes (value data, bool decodeData, value buffer) { - ImageBuffer imageBuffer; + ImageBuffer imageBuffer (buffer); Bytes bytes (data); Resource resource = Resource (&bytes); @@ -1165,9 +1165,9 @@ namespace lime { } - value lime_png_decode_file (HxString path, bool decodeData) { + value lime_png_decode_file (HxString path, bool decodeData, value buffer) { - ImageBuffer imageBuffer; + ImageBuffer imageBuffer (buffer); Resource resource = Resource (path.c_str ()); #ifdef LIME_PNG @@ -1245,10 +1245,10 @@ namespace lime { } - value lime_renderer_read_pixels (value renderer, value rect) { + value lime_renderer_read_pixels (value renderer, value rect, value imageBuffer) { Renderer* targetRenderer = (Renderer*)val_data (renderer); - ImageBuffer buffer; + ImageBuffer buffer (imageBuffer); if (!val_is_null (rect)) { @@ -1627,11 +1627,11 @@ namespace lime { } - value lime_zlib_compress (value buffer) { + value lime_zlib_compress (value buffer, value bytes) { #ifdef LIME_ZLIB Bytes data (buffer); - Bytes result; + Bytes result (bytes); Zlib::Compress (ZLIB, &data, &result); @@ -1643,11 +1643,11 @@ namespace lime { } - value lime_zlib_decompress (value buffer) { + value lime_zlib_decompress (value buffer, value bytes) { #ifdef LIME_ZLIB Bytes data (buffer); - Bytes result; + Bytes result (bytes); Zlib::Decompress (ZLIB, &data, &result); @@ -1666,16 +1666,16 @@ namespace lime { DEFINE_PRIME1 (lime_application_quit); DEFINE_PRIME2v (lime_application_set_frame_rate); DEFINE_PRIME1 (lime_application_update); - DEFINE_PRIME1 (lime_audio_load); + DEFINE_PRIME2 (lime_audio_load); DEFINE_PRIME2 (lime_bytes_from_data_pointer); DEFINE_PRIME1 (lime_bytes_get_data_pointer); - DEFINE_PRIME1 (lime_bytes_read_file); + DEFINE_PRIME2 (lime_bytes_read_file); DEFINE_PRIME1 (lime_cffi_get_native_pointer); DEFINE_PRIME1 (lime_cffi_set_finalizer); DEFINE_PRIME0 (lime_clipboard_get_text); DEFINE_PRIME1v (lime_clipboard_set_text); - DEFINE_PRIME1 (lime_deflate_compress); - DEFINE_PRIME1 (lime_deflate_decompress); + DEFINE_PRIME2 (lime_deflate_compress); + DEFINE_PRIME2 (lime_deflate_decompress); DEFINE_PRIME2v (lime_drop_event_manager_register); DEFINE_PRIME2 (lime_file_dialog_open_directory); DEFINE_PRIME2 (lime_file_dialog_open_file); @@ -1701,8 +1701,8 @@ namespace lime { DEFINE_PRIME2v (lime_gamepad_event_manager_register); DEFINE_PRIME1 (lime_gamepad_get_device_guid); DEFINE_PRIME1 (lime_gamepad_get_device_name); - DEFINE_PRIME1 (lime_gzip_compress); - DEFINE_PRIME1 (lime_gzip_decompress); + DEFINE_PRIME2 (lime_gzip_compress); + DEFINE_PRIME2 (lime_gzip_decompress); DEFINE_PRIME2v (lime_haptic_vibrate); DEFINE_PRIME3v (lime_image_data_util_color_transform); DEFINE_PRIME6v (lime_image_data_util_copy_channel); @@ -1717,8 +1717,8 @@ namespace lime { DEFINE_PRIME4v (lime_image_data_util_set_pixels); DEFINE_PRIME12 (lime_image_data_util_threshold); DEFINE_PRIME1v (lime_image_data_util_unmultiply_alpha); - DEFINE_PRIME3 (lime_image_encode); - DEFINE_PRIME1 (lime_image_load); + DEFINE_PRIME4 (lime_image_encode); + DEFINE_PRIME2 (lime_image_load); DEFINE_PRIME0 (lime_jni_getenv); DEFINE_PRIME2v (lime_joystick_event_manager_register); DEFINE_PRIME1 (lime_joystick_get_device_guid); @@ -1727,12 +1727,12 @@ namespace lime { DEFINE_PRIME1 (lime_joystick_get_num_buttons); DEFINE_PRIME1 (lime_joystick_get_num_hats); DEFINE_PRIME1 (lime_joystick_get_num_trackballs); - DEFINE_PRIME2 (lime_jpeg_decode_bytes); - DEFINE_PRIME2 (lime_jpeg_decode_file); + DEFINE_PRIME3 (lime_jpeg_decode_bytes); + DEFINE_PRIME3 (lime_jpeg_decode_file); DEFINE_PRIME2v (lime_key_event_manager_register); DEFINE_PRIME0 (lime_locale_get_system_locale); - DEFINE_PRIME1 (lime_lzma_compress); - DEFINE_PRIME1 (lime_lzma_decompress); + DEFINE_PRIME2 (lime_lzma_compress); + DEFINE_PRIME2 (lime_lzma_decompress); DEFINE_PRIME2v (lime_mouse_event_manager_register); DEFINE_PRIME0v (lime_mouse_hide); DEFINE_PRIME1v (lime_mouse_set_cursor); @@ -1740,8 +1740,8 @@ namespace lime { DEFINE_PRIME0v (lime_mouse_show); DEFINE_PRIME3v (lime_mouse_warp); DEFINE_PRIME1v (lime_neko_execute); - DEFINE_PRIME2 (lime_png_decode_bytes); - DEFINE_PRIME2 (lime_png_decode_file); + DEFINE_PRIME3 (lime_png_decode_bytes); + DEFINE_PRIME3 (lime_png_decode_file); DEFINE_PRIME1 (lime_renderer_create); DEFINE_PRIME1v (lime_renderer_flip); DEFINE_PRIME1 (lime_renderer_get_context); @@ -1749,7 +1749,7 @@ namespace lime { DEFINE_PRIME1 (lime_renderer_get_type); DEFINE_PRIME1 (lime_renderer_lock); DEFINE_PRIME1v (lime_renderer_make_current); - DEFINE_PRIME2 (lime_renderer_read_pixels); + DEFINE_PRIME3 (lime_renderer_read_pixels); DEFINE_PRIME1v (lime_renderer_unlock); DEFINE_PRIME2v (lime_render_event_manager_register); DEFINE_PRIME2v (lime_sensor_event_manager_register); @@ -1790,8 +1790,8 @@ namespace lime { DEFINE_PRIME2 (lime_window_set_minimized); DEFINE_PRIME2 (lime_window_set_resizable); DEFINE_PRIME2 (lime_window_set_title); - DEFINE_PRIME1 (lime_zlib_compress); - DEFINE_PRIME1 (lime_zlib_decompress); + DEFINE_PRIME2 (lime_zlib_compress); + DEFINE_PRIME2 (lime_zlib_decompress); } diff --git a/project/src/audio/AudioBuffer.cpp b/project/src/audio/AudioBuffer.cpp index d4cb8b2da..14beaf791 100644 --- a/project/src/audio/AudioBuffer.cpp +++ b/project/src/audio/AudioBuffer.cpp @@ -15,6 +15,7 @@ namespace lime { bitsPerSample = 0; channels = 0; + data = new ArrayBufferView (); sampleRate = 0; mValue = 0; @@ -37,14 +38,14 @@ namespace lime { bitsPerSample = val_int (val_field (audioBuffer, id_bitsPerSample)); channels = val_int (val_field (audioBuffer, id_channels)); - data.Set (val_field (audioBuffer, id_data)); + data = new ArrayBufferView (val_field (audioBuffer, id_data)); sampleRate = val_int (val_field (audioBuffer, id_sampleRate)); } else { bitsPerSample = 0; channels = 0; - data.Clear (); + data = new ArrayBufferView (); sampleRate = 0; } @@ -56,6 +57,8 @@ namespace lime { AudioBuffer::~AudioBuffer () { + delete data; + } @@ -71,7 +74,7 @@ namespace lime { } - if (mValue == 0 || val_is_null (mValue)) { + if (val_is_null (mValue)) { mValue = alloc_empty_object (); @@ -79,7 +82,7 @@ namespace lime { alloc_field (mValue, id_bitsPerSample, alloc_int (bitsPerSample)); alloc_field (mValue, id_channels, alloc_int (channels)); - alloc_field (mValue, id_data, data.Value ()); + alloc_field (mValue, id_data, data ? data->Value () : alloc_null ()); alloc_field (mValue, id_sampleRate, alloc_int (sampleRate)); return mValue; diff --git a/project/src/audio/format/OGG.cpp b/project/src/audio/format/OGG.cpp index d40d9bb75..62278c8fa 100644 --- a/project/src/audio/format/OGG.cpp +++ b/project/src/audio/format/OGG.cpp @@ -107,7 +107,7 @@ namespace lime { bool OGG::Decode (Resource *resource, AudioBuffer *audioBuffer) { OggVorbis_File oggFile; - Bytes data; + Bytes *data = NULL; OAL_OggMemoryFile fakeFile; if (resource->path) { @@ -132,15 +132,16 @@ namespace lime { } else { lime::fclose (file); - data.ReadFile (resource->path); + data = new Bytes (resource->path); fakeFile = OAL_OggMemoryFile (); - fakeFile.data = data.Data (); - fakeFile.size = data.Length (); + fakeFile.data = data->Data (); + fakeFile.size = data->Length (); fakeFile.pos = 0; if (ov_open_callbacks (&fakeFile, &oggFile, NULL, 0, OAL_CALLBACKS_BUFFER) != 0) { + delete data; return false; } @@ -181,6 +182,13 @@ namespace lime { //LOG_SOUND("FAILED TO READ OGG SOUND INFO, IS THIS EVEN AN OGG FILE?\n"); ov_clear (&oggFile); + + if (data) { + + delete data; + + } + return false; } @@ -191,18 +199,18 @@ namespace lime { audioBuffer->bitsPerSample = 16; int dataLength = ov_pcm_total (&oggFile, -1) * audioBuffer->channels * audioBuffer->bitsPerSample / 8; - audioBuffer->data.Resize (dataLength); + audioBuffer->data->Resize (dataLength); while (bytes > 0) { - bytes = ov_read (&oggFile, (char *)audioBuffer->data.Data () + totalBytes, BUFFER_SIZE, BUFFER_READ_TYPE, 2, 1, &bitStream); + bytes = ov_read (&oggFile, (char *)audioBuffer->data->Data () + totalBytes, BUFFER_SIZE, BUFFER_READ_TYPE, 2, 1, &bitStream); totalBytes += bytes; } if (dataLength != totalBytes) { - audioBuffer->data.Resize (totalBytes); + audioBuffer->data->Resize (totalBytes); } @@ -210,6 +218,12 @@ namespace lime { #undef BUFFER_READ_TYPE + if (data) { + + delete data; + + } + return true; } diff --git a/project/src/audio/format/WAV.cpp b/project/src/audio/format/WAV.cpp index 524228896..dc809490d 100644 --- a/project/src/audio/format/WAV.cpp +++ b/project/src/audio/format/WAV.cpp @@ -125,9 +125,9 @@ namespace lime { } - audioBuffer->data.Resize (wave_data.subChunkSize); + audioBuffer->data->Resize (wave_data.subChunkSize); - if (!lime::fread (audioBuffer->data.Data (), wave_data.subChunkSize, 1, file)) { + if (!lime::fread (audioBuffer->data->Data (), wave_data.subChunkSize, 1, file)) { LOG_SOUND ("error loading WAVE data into struct!\n"); lime::fclose (file); @@ -186,7 +186,7 @@ namespace lime { } - audioBuffer->data.Resize (wave_data.subChunkSize); + audioBuffer->data->Resize (wave_data.subChunkSize); size_t size = wave_data.subChunkSize; @@ -196,7 +196,7 @@ namespace lime { } - unsigned char* bytes = audioBuffer->data.Data (); + unsigned char* bytes = audioBuffer->data->Data (); memcpy (bytes, base, size); } diff --git a/project/src/backend/sdl/SDLRenderer.cpp b/project/src/backend/sdl/SDLRenderer.cpp index ee4ab0e54..89da79574 100644 --- a/project/src/backend/sdl/SDLRenderer.cpp +++ b/project/src/backend/sdl/SDLRenderer.cpp @@ -219,9 +219,9 @@ namespace lime { buffer->Resize (bounds.w, bounds.h, 32); - SDL_RenderReadPixels (sdlRenderer, &bounds, SDL_PIXELFORMAT_ABGR8888, buffer->data.Data (), buffer->Stride ()); + SDL_RenderReadPixels (sdlRenderer, &bounds, SDL_PIXELFORMAT_ABGR8888, buffer->data->Data (), buffer->Stride ()); - for (unsigned char *it=buffer->data.Data () + 3; it < (buffer->data.Data () + buffer->data.Length ()); it += 4) { + for (unsigned char *it=buffer->data->Data () + 3; it < (buffer->data->Data () + buffer->data->Length ()); it += 4) { *it = 0xff; diff --git a/project/src/backend/sdl/SDLWindow.cpp b/project/src/backend/sdl/SDLWindow.cpp index a5b5088d1..8a9eb729f 100644 --- a/project/src/backend/sdl/SDLWindow.cpp +++ b/project/src/backend/sdl/SDLWindow.cpp @@ -321,7 +321,7 @@ namespace lime { void SDLWindow::SetIcon (ImageBuffer *imageBuffer) { - SDL_Surface *surface = SDL_CreateRGBSurfaceFrom (imageBuffer->data.Data (), imageBuffer->width, imageBuffer->height, imageBuffer->bitsPerPixel, imageBuffer->Stride (), 0x000000FF, 0x0000FF00, 0x00FF0000, 0xFF000000); + SDL_Surface *surface = SDL_CreateRGBSurfaceFrom (imageBuffer->data->Data (), imageBuffer->width, imageBuffer->height, imageBuffer->bitsPerPixel, imageBuffer->Stride (), 0x000000FF, 0x0000FF00, 0x00FF0000, 0xFF000000); if (surface) { diff --git a/project/src/graphics/Image.cpp b/project/src/graphics/Image.cpp index 029c9a1f0..d7d3f318f 100644 --- a/project/src/graphics/Image.cpp +++ b/project/src/graphics/Image.cpp @@ -38,7 +38,7 @@ namespace lime { width = val_int (val_field (image, id_width)); height = val_int (val_field (image, id_height)); - buffer.Set (val_field (image, id_buffer)); + buffer = new ImageBuffer (val_field (image, id_buffer)); offsetX = val_int (val_field (image, id_offsetX)); offsetY = val_int (val_field (image, id_offsetY)); @@ -47,6 +47,8 @@ namespace lime { Image::~Image () { + delete buffer; + } diff --git a/project/src/graphics/ImageBuffer.cpp b/project/src/graphics/ImageBuffer.cpp index 401e21df4..b168856fb 100644 --- a/project/src/graphics/ImageBuffer.cpp +++ b/project/src/graphics/ImageBuffer.cpp @@ -30,51 +30,6 @@ namespace lime { ImageBuffer::ImageBuffer (value imageBuffer) { - Set (imageBuffer); - - } - - - ImageBuffer::~ImageBuffer () { - - } - - - void ImageBuffer::Blit (const unsigned char *data, int x, int y, int width, int height) { - - if (x < 0 || x + width > this->width || y < 0 || y + height > this->height) { - - return; - - } - - int stride = Stride (); - unsigned char *bytes = this->data.buffer.Data (); - - for (int i = 0; i < height; i++) { - - memcpy (&bytes[(i + y) * this->width + x], &data[i * width], stride); - - } - - } - - - void ImageBuffer::Resize (int width, int height, int bitsPerPixel) { - - this->bitsPerPixel = bitsPerPixel; - this->width = width; - this->height = height; - - int stride = Stride (); - - this->data.Resize (height * stride); - - } - - - void ImageBuffer::Set (value imageBuffer) { - if (!init) { id_bitsPerPixel = val_id ("bitsPerPixel"); @@ -96,7 +51,7 @@ namespace lime { format = (PixelFormat)val_int (val_field (imageBuffer, id_format)); transparent = val_bool (val_field (imageBuffer, id_transparent)); premultiplied = val_bool (val_field (imageBuffer, id_premultiplied)); - data.Set (val_field (imageBuffer, id_data)); + data = new ArrayBufferView (val_field (imageBuffer, id_data)); } else { @@ -104,6 +59,7 @@ namespace lime { height = 0; bitsPerPixel = 32; format = RGBA32; + data = 0; premultiplied = false; transparent = false; @@ -114,6 +70,54 @@ namespace lime { } + ImageBuffer::~ImageBuffer () { + + delete data; + + } + + + void ImageBuffer::Blit (const unsigned char *data, int x, int y, int width, int height) { + + if (x < 0 || x + width > this->width || y < 0 || y + height > this->height) { + + return; + + } + + int stride = Stride (); + unsigned char *bytes = this->data->buffer->Data (); + + for (int i = 0; i < height; i++) { + + memcpy (&bytes[(i + y) * this->width + x], &data[i * width], stride); + + } + + } + + + void ImageBuffer::Resize (int width, int height, int bitsPerPixel) { + + this->bitsPerPixel = bitsPerPixel; + this->width = width; + this->height = height; + + int stride = Stride (); + + if (!this->data) { + + this->data = new ArrayBufferView (height * stride); + + } else { + + this->data->Resize (height * stride); + + } + + } + + int ImageBuffer::Stride () { return width * (((bitsPerPixel + 3) & ~0x3) >> 3); @@ -136,7 +140,7 @@ namespace lime { } - if (mValue == 0 || val_is_null (mValue)) { + if (val_is_null (mValue)) { mValue = alloc_empty_object (); @@ -145,7 +149,7 @@ namespace lime { alloc_field (mValue, id_width, alloc_int (width)); alloc_field (mValue, id_height, alloc_int (height)); alloc_field (mValue, id_bitsPerPixel, alloc_int (bitsPerPixel)); - alloc_field (mValue, id_data, data.Value ()); + alloc_field (mValue, id_data, data ? data->Value () : alloc_null ()); alloc_field (mValue, id_transparent, alloc_bool (transparent)); alloc_field (mValue, id_format, alloc_int (format)); alloc_field (mValue, id_premultiplied, alloc_bool (premultiplied)); diff --git a/project/src/graphics/format/JPEG.cpp b/project/src/graphics/format/JPEG.cpp index 89e9d64e2..bb3319a33 100644 --- a/project/src/graphics/format/JPEG.cpp +++ b/project/src/graphics/format/JPEG.cpp @@ -191,7 +191,7 @@ namespace lime { jpegError.base.output_message = OnOutput; FILE_HANDLE *file = NULL; - Bytes data; + Bytes *data = NULL; MySrcManager *manager = NULL; if (resource->path) { @@ -220,6 +220,12 @@ namespace lime { } + if (data) { + + delete data; + + } + jpeg_destroy_decompress (&cinfo); return false; @@ -235,8 +241,8 @@ namespace lime { } else { - data.ReadFile (resource->path); - manager = new MySrcManager (data.Data (), data.Length ()); + data = new Bytes (resource->path); + manager = new MySrcManager (data->Data (), data->Length ()); cinfo.src = &manager->pub; } @@ -260,7 +266,7 @@ namespace lime { int components = cinfo.output_components; imageBuffer->Resize (cinfo.output_width, cinfo.output_height, 32); - unsigned char *bytes = imageBuffer->data.Data (); + unsigned char *bytes = imageBuffer->data->Data (); unsigned char *scanline = new unsigned char [imageBuffer->width * components]; while (cinfo.output_scanline < cinfo.output_height) { @@ -309,6 +315,12 @@ namespace lime { } + if (data) { + + delete data; + + } + jpeg_destroy_decompress (&cinfo); return decoded; @@ -351,7 +363,7 @@ namespace lime { jpeg_start_compress (&cinfo, true); JSAMPROW row_pointer = &row_buf[0]; - unsigned char* imageData = imageBuffer->data.Data(); + unsigned char* imageData = imageBuffer->data->Data(); int stride = imageBuffer->Stride (); while (cinfo.next_scanline < cinfo.image_height) { diff --git a/project/src/graphics/format/PNG.cpp b/project/src/graphics/format/PNG.cpp index f0173d27b..79eb42821 100644 --- a/project/src/graphics/format/PNG.cpp +++ b/project/src/graphics/format/PNG.cpp @@ -84,7 +84,7 @@ namespace lime { int bit_depth, color_type, interlace_type; FILE_HANDLE *file = NULL; - Bytes data; + Bytes *data = NULL; if (resource->path) { @@ -144,8 +144,8 @@ namespace lime { } else { - data.ReadFile (resource->path); - ReadBuffer buffer (data.Data (), data.Length ()); + data = new Bytes (resource->path); + ReadBuffer buffer (data->Data (), data->Length ()); png_set_read_fn (png_ptr, &buffer, user_read_data_fn); } @@ -185,7 +185,7 @@ namespace lime { imageBuffer->Resize (width, height, 32); const unsigned int stride = imageBuffer->Stride (); - unsigned char *bytes = imageBuffer->data.Data (); + unsigned char *bytes = imageBuffer->data->Data (); int number_of_passes = png_set_interlace_handling (png_ptr); @@ -212,6 +212,7 @@ namespace lime { png_destroy_read_struct (&png_ptr, &info_ptr, (png_infopp)NULL); if (file) lime::fclose (file); + if (data) delete data; return true; @@ -258,7 +259,7 @@ namespace lime { png_write_info (png_ptr, info_ptr); bool do_alpha = (color_type == PNG_COLOR_TYPE_RGBA); - unsigned char* imageData = imageBuffer->data.Data(); + unsigned char* imageData = imageBuffer->data->Data(); int stride = imageBuffer->Stride (); { diff --git a/project/src/graphics/utils/ImageDataUtil.cpp b/project/src/graphics/utils/ImageDataUtil.cpp index ce0b5e815..d019cc8e7 100644 --- a/project/src/graphics/utils/ImageDataUtil.cpp +++ b/project/src/graphics/utils/ImageDataUtil.cpp @@ -16,9 +16,9 @@ namespace lime { void ImageDataUtil::ColorTransform (Image* image, Rectangle* rect, ColorMatrix* colorMatrix) { - PixelFormat format = image->buffer.format; - bool premultiplied = image->buffer.premultiplied; - uint8_t* data = (uint8_t*)image->buffer.data.Data (); + PixelFormat format = image->buffer->format; + bool premultiplied = image->buffer->premultiplied; + uint8_t* data = (uint8_t*)image->buffer->data->Data (); ImageDataView dataView = ImageDataView (image, rect); @@ -51,17 +51,17 @@ namespace lime { void ImageDataUtil::CopyChannel (Image* image, Image* sourceImage, Rectangle* sourceRect, Vector2* destPoint, int srcChannel, int destChannel) { - uint8_t* srcData = (uint8_t*)sourceImage->buffer.data.Data (); - uint8_t* destData = (uint8_t*)image->buffer.data.Data (); + uint8_t* srcData = (uint8_t*)sourceImage->buffer->data->Data (); + uint8_t* destData = (uint8_t*)image->buffer->data->Data (); ImageDataView srcView = ImageDataView (sourceImage, sourceRect); Rectangle destRect = Rectangle (destPoint->x, destPoint->y, srcView.width, srcView.height); ImageDataView destView = ImageDataView (image, &destRect); - PixelFormat srcFormat = sourceImage->buffer.format; - PixelFormat destFormat = image->buffer.format; - bool srcPremultiplied = sourceImage->buffer.premultiplied; - bool destPremultiplied = image->buffer.premultiplied; + PixelFormat srcFormat = sourceImage->buffer->format; + PixelFormat destFormat = image->buffer->format; + bool srcPremultiplied = sourceImage->buffer->premultiplied; + bool destPremultiplied = image->buffer->premultiplied; int srcPosition, destPosition; RGBA srcPixel, destPixel; @@ -109,22 +109,22 @@ namespace lime { void ImageDataUtil::CopyPixels (Image* image, Image* sourceImage, Rectangle* sourceRect, Vector2* destPoint, Image* alphaImage, Vector2* alphaPoint, bool mergeAlpha) { - uint8_t* sourceData = (uint8_t*)sourceImage->buffer.data.Data (); - uint8_t* destData = (uint8_t*)image->buffer.data.Data (); + uint8_t* sourceData = (uint8_t*)sourceImage->buffer->data->Data (); + uint8_t* destData = (uint8_t*)image->buffer->data->Data (); ImageDataView sourceView = ImageDataView (sourceImage, sourceRect); Rectangle destRect = Rectangle (destPoint->x, destPoint->y, sourceView.width, sourceView.height); ImageDataView destView = ImageDataView (image, &destRect); - PixelFormat sourceFormat = sourceImage->buffer.format; - PixelFormat destFormat = image->buffer.format; - bool sourcePremultiplied = sourceImage->buffer.premultiplied; - bool destPremultiplied = image->buffer.premultiplied; + PixelFormat sourceFormat = sourceImage->buffer->format; + PixelFormat destFormat = image->buffer->format; + bool sourcePremultiplied = sourceImage->buffer->premultiplied; + bool destPremultiplied = image->buffer->premultiplied; int sourcePosition, destPosition; RGBA sourcePixel; - if (!mergeAlpha || !sourceImage->buffer.transparent) { + if (!mergeAlpha || !sourceImage->buffer->transparent) { for (int y = 0; y < destView.height; y++) { @@ -189,9 +189,9 @@ namespace lime { } else { - uint8_t* alphaData = (uint8_t*)alphaImage->buffer.data.Data (); - PixelFormat alphaFormat = alphaImage->buffer.format; - bool alphaPremultiplied = alphaImage->buffer.premultiplied; + uint8_t* alphaData = (uint8_t*)alphaImage->buffer->data->Data (); + PixelFormat alphaFormat = alphaImage->buffer->format; + bool alphaPremultiplied = alphaImage->buffer->premultiplied; Rectangle alphaRect = Rectangle (alphaPoint->x, alphaPoint->y, destView.width, destView.height); ImageDataView alphaView = ImageDataView (alphaImage, &alphaRect); @@ -246,9 +246,9 @@ namespace lime { void ImageDataUtil::FillRect (Image* image, Rectangle* rect, int32_t color) { - uint8_t* data = (uint8_t*)image->buffer.data.Data (); - PixelFormat format = image->buffer.format; - bool premultiplied = image->buffer.premultiplied; + uint8_t* data = (uint8_t*)image->buffer->data->Data (); + PixelFormat format = image->buffer->format; + bool premultiplied = image->buffer->premultiplied; ImageDataView dataView = ImageDataView (image, rect); int row; @@ -273,18 +273,18 @@ namespace lime { void ImageDataUtil::FloodFill (Image* image, int x, int y, int32_t color) { - uint8_t* data = (uint8_t*)image->buffer.data.Data (); - PixelFormat format = image->buffer.format; - bool premultiplied = image->buffer.premultiplied; + uint8_t* data = (uint8_t*)image->buffer->data->Data (); + PixelFormat format = image->buffer->format; + bool premultiplied = image->buffer->premultiplied; RGBA fillColor (color); if (premultiplied) fillColor.MultiplyAlpha (); RGBA hitColor; - hitColor.ReadUInt8 (data, ((y + image->offsetY) * (image->buffer.width * 4)) + ((x + image->offsetX) * 4), format, premultiplied); + hitColor.ReadUInt8 (data, ((y + image->offsetY) * (image->buffer->width * 4)) + ((x + image->offsetX) * 4), format, premultiplied); - if (!image->buffer.transparent) { + if (!image->buffer->transparent) { fillColor.a = 0xFF; hitColor.a = 0xFF; @@ -348,11 +348,11 @@ namespace lime { int length = int (rect->width * rect->height); pixels->Resize (length * 4); - uint8_t* data = (uint8_t*)image->buffer.data.Data (); + uint8_t* data = (uint8_t*)image->buffer->data->Data (); uint8_t* destData = (uint8_t*)pixels->Data (); - PixelFormat sourceFormat = image->buffer.format; - bool premultiplied = image->buffer.premultiplied; + PixelFormat sourceFormat = image->buffer->format; + bool premultiplied = image->buffer->premultiplied; ImageDataView dataView = ImageDataView (image, rect); int position, destPosition = 0; @@ -383,12 +383,12 @@ namespace lime { Rectangle destRect = Rectangle (destPoint->x, destPoint->y, sourceView.width, sourceView.height); ImageDataView destView = ImageDataView (image, &destRect); - uint8_t* sourceData = (uint8_t*)sourceImage->buffer.data.Data (); - uint8_t* destData = (uint8_t*)image->buffer.data.Data (); - PixelFormat sourceFormat = sourceImage->buffer.format; - PixelFormat destFormat = image->buffer.format; - bool sourcePremultiplied = sourceImage->buffer.premultiplied; - bool destPremultiplied = image->buffer.premultiplied; + uint8_t* sourceData = (uint8_t*)sourceImage->buffer->data->Data (); + uint8_t* destData = (uint8_t*)image->buffer->data->Data (); + PixelFormat sourceFormat = sourceImage->buffer->format; + PixelFormat destFormat = image->buffer->format; + bool sourcePremultiplied = sourceImage->buffer->premultiplied; + bool destPremultiplied = image->buffer->premultiplied; int sourcePosition, destPosition; RGBA sourcePixel, destPixel; @@ -422,9 +422,9 @@ namespace lime { void ImageDataUtil::MultiplyAlpha (Image* image) { - PixelFormat format = image->buffer.format; - uint8_t* data = (uint8_t*)image->buffer.data.Data (); - int length = int (image->buffer.data.Length () / 4); + PixelFormat format = image->buffer->format; + uint8_t* data = (uint8_t*)image->buffer->data->Data (); + int length = int (image->buffer->data->Length () / 4); RGBA pixel; for (int i = 0; i < length; i++) { @@ -442,8 +442,8 @@ namespace lime { int imageWidth = image->width; int imageHeight = image->height; - uint8_t* data = (uint8_t*)image->buffer.data.Data (); - uint8_t* newData = (uint8_t*)buffer->data.Data (); + uint8_t* data = (uint8_t*)image->buffer->data->Data (); + uint8_t* newData = (uint8_t*)buffer->data->Data (); int sourceIndex, sourceIndexX, sourceIndexY, sourceIndexXY, index; int sourceX, sourceY; @@ -497,11 +497,11 @@ namespace lime { void ImageDataUtil::SetFormat (Image* image, PixelFormat format) { int index, a16; - int length = image->buffer.data.Length () / 4; + int length = image->buffer->data->Length () / 4; int r1, g1, b1, a1, r2, g2, b2, a2; int r, g, b, a; - switch (image->buffer.format) { + switch (image->buffer->format) { case RGBA32: @@ -557,7 +557,7 @@ namespace lime { } - unsigned char* data = image->buffer.data.Data (); + unsigned char* data = image->buffer->data->Data (); for (int i = 0; i < length; i++) { @@ -580,9 +580,9 @@ namespace lime { void ImageDataUtil::SetPixels (Image* image, Rectangle* rect, Bytes* bytes, PixelFormat format) { - uint8_t* data = (uint8_t*)image->buffer.data.Data (); - PixelFormat sourceFormat = image->buffer.format; - bool premultiplied = image->buffer.premultiplied; + uint8_t* data = (uint8_t*)image->buffer->data->Data (); + PixelFormat sourceFormat = image->buffer->format; + bool premultiplied = image->buffer->premultiplied; ImageDataView dataView = ImageDataView (image, rect); int row; RGBA pixel; @@ -590,7 +590,7 @@ namespace lime { uint8_t* byteArray = (uint8_t*)bytes->Data (); int srcPosition = 0; - bool transparent = image->buffer.transparent; + bool transparent = image->buffer->transparent; for (int y = 0; y < dataView.height; y++) { @@ -672,17 +672,17 @@ namespace lime { RGBA _color (color); int hits = 0; - uint8_t* srcData = (uint8_t*)sourceImage->buffer.data.Data (); - uint8_t* destData = (uint8_t*)image->buffer.data.Data (); + uint8_t* srcData = (uint8_t*)sourceImage->buffer->data->Data (); + uint8_t* destData = (uint8_t*)image->buffer->data->Data (); ImageDataView srcView = ImageDataView (sourceImage, sourceRect); Rectangle destRect = Rectangle (destPoint->x, destPoint->y, srcView.width, srcView.height); ImageDataView destView = ImageDataView (image, &destRect); - PixelFormat srcFormat = sourceImage->buffer.format; - PixelFormat destFormat = image->buffer.format; - bool srcPremultiplied = sourceImage->buffer.premultiplied; - bool destPremultiplied = image->buffer.premultiplied; + PixelFormat srcFormat = sourceImage->buffer->format; + PixelFormat destFormat = image->buffer->format; + bool srcPremultiplied = sourceImage->buffer->premultiplied; + bool destPremultiplied = image->buffer->premultiplied; int srcPosition, destPosition, value; RGBA srcPixel, destPixel; @@ -738,9 +738,9 @@ namespace lime { void ImageDataUtil::UnmultiplyAlpha (Image* image) { - PixelFormat format = image->buffer.format; - uint8_t* data = (uint8_t*)image->buffer.data.Data (); - int length = int (image->buffer.data.Length () / 4); + PixelFormat format = image->buffer->format; + uint8_t* data = (uint8_t*)image->buffer->data->Data (); + int length = int (image->buffer->data->Length () / 4); RGBA pixel; for (int i = 0; i < length; i++) { @@ -765,7 +765,7 @@ namespace lime { if (rect->height < 0) rect->height = 0; this->rect = rect; - stride = image->buffer.Stride (); + stride = image->buffer->Stride (); x = ceil (this->rect->x); y = ceil (this->rect->y); diff --git a/project/src/utils/ArrayBufferView.cpp b/project/src/utils/ArrayBufferView.cpp index 8a13a411d..5a35b2364 100644 --- a/project/src/utils/ArrayBufferView.cpp +++ b/project/src/utils/ArrayBufferView.cpp @@ -12,6 +12,7 @@ namespace lime { ArrayBufferView::ArrayBufferView () { + buffer = new Bytes (); byteLength = 0; length = 0; mValue = 0; @@ -21,7 +22,7 @@ namespace lime { ArrayBufferView::ArrayBufferView (int size) { - buffer.Resize (size); + buffer = new Bytes (size); byteLength = size; length = size; mValue = 0; @@ -31,58 +32,6 @@ namespace lime { ArrayBufferView::ArrayBufferView (value arrayBufferView) { - Set (arrayBufferView); - - } - - - ArrayBufferView::~ArrayBufferView () { - - } - - - void ArrayBufferView::Clear () { - - buffer.Clear (); - byteLength = 0; - length = 0; - mValue = 0; - - } - - - unsigned char *ArrayBufferView::Data () { - - return buffer.Data (); - - } - - - const unsigned char *ArrayBufferView::Data () const { - - return buffer.Data (); - - } - - - int ArrayBufferView::Length () const { - - return buffer.Length (); - - } - - - void ArrayBufferView::Resize (int size) { - - buffer.Resize (size); - byteLength = size; - length = size; - - } - - - void ArrayBufferView::Set (value arrayBufferView) { - if (!init) { id_buffer = val_id ("buffer"); @@ -94,13 +43,13 @@ namespace lime { if (!val_is_null (arrayBufferView)) { - buffer.Set (val_field (arrayBufferView, id_buffer)); + buffer = new Bytes (val_field (arrayBufferView, id_buffer)); byteLength = val_int (val_field (arrayBufferView, id_byteLength)); length = val_int (val_field (arrayBufferView, id_length)); } else { - buffer.Clear (); + buffer = new Bytes (); byteLength = 0; length = 0; @@ -111,10 +60,56 @@ namespace lime { } + ArrayBufferView::~ArrayBufferView () { + + delete buffer; + + } + + + unsigned char *ArrayBufferView::Data () { + + return buffer->Data (); + + } + + + const unsigned char *ArrayBufferView::Data () const { + + return buffer->Data (); + + } + + + int ArrayBufferView::Length () const { + + return buffer->Length (); + + } + + + void ArrayBufferView::Resize (int size) { + + buffer->Resize (size); + byteLength = size; + length = size; + + } + + + void ArrayBufferView::Set (value bytes) { + + buffer->Set (bytes); + byteLength = buffer->Length (); + length = byteLength; + + } + + void ArrayBufferView::Set (const QuickVec data) { - buffer.Set (data); - byteLength = buffer.Length (); + buffer->Set (data); + byteLength = buffer->Length (); length = byteLength; } @@ -131,13 +126,13 @@ namespace lime { } - if (mValue == 0 || val_is_null (mValue)) { + if (val_is_null (mValue)) { mValue = alloc_empty_object (); } - alloc_field (mValue, id_buffer, buffer.Value ()); + alloc_field (mValue, id_buffer, buffer ? buffer->Value () : alloc_null ()); alloc_field (mValue, id_byteLength, alloc_int (byteLength)); alloc_field (mValue, id_length, alloc_int (length)); return mValue; diff --git a/project/src/utils/Bytes.cpp b/project/src/utils/Bytes.cpp index 9c8683770..4b9e1f2d0 100644 --- a/project/src/utils/Bytes.cpp +++ b/project/src/utils/Bytes.cpp @@ -39,6 +39,7 @@ namespace lime { _data = 0; _length = 0; + _root = 0; _value = 0; } @@ -50,6 +51,7 @@ namespace lime { _data = 0; _length = 0; + _root = 0; _value = 0; Resize (size); @@ -63,6 +65,7 @@ namespace lime { _data = 0; _length = 0; + _root = 0; _value = 0; Set (bytes); @@ -76,6 +79,7 @@ namespace lime { _data = 0; _length = 0; + _root = 0; _value = 0; ReadFile (path); @@ -89,6 +93,7 @@ namespace lime { _data = 0; _length = 0; + _root = 0; _value = 0; Set (data); @@ -98,14 +103,11 @@ namespace lime { Bytes::~Bytes () { - } - - - void Bytes::Clear () { - - _data = 0; - _length = 0; - _value = 0; + if (_root) { + + delete _root; + + } } @@ -164,6 +166,7 @@ namespace lime { if (!_value) { _value = alloc_empty_object (); + _root = new AutoGCRoot (_value); } @@ -222,9 +225,28 @@ namespace lime { _data = 0; _value = 0; + if (_root) { + + delete _root; + + } + + _root = 0; + } else { _value = bytes; + + if (!_root) { + + _root = new AutoGCRoot (_value); + + } else { + + _root->set (_value); + + } + _length = val_int (val_field (bytes, id_length)); if (_length > 0) { @@ -266,6 +288,14 @@ namespace lime { _data = 0; _length = 0; + if (_root) { + + delete _root; + + } + + _root = 0; + } }