diff --git a/lime/_backend/native/NativeRenderer.hx b/lime/_backend/native/NativeRenderer.hx index ace13fdf1..1447db885 100644 --- a/lime/_backend/native/NativeRenderer.hx +++ b/lime/_backend/native/NativeRenderer.hx @@ -3,6 +3,7 @@ package lime._backend.native; import lime.graphics.cairo.Cairo; import lime.graphics.cairo.CairoFormat; +import lime.graphics.cairo.CairoImageSurface; import lime.graphics.cairo.CairoSurface; import lime.graphics.CairoRenderContext; import lime.graphics.ConsoleRenderContext; @@ -97,14 +98,17 @@ class NativeRenderer { if (cacheLock == null || cacheLock.pixels != lock.pixels || cacheLock.width != lock.width || cacheLock.height != lock.height) { - if ( primarySurface != null ) + if (primarySurface != null) { + primarySurface.destroy (); + + } - primarySurface = CairoSurface.createForData (lock.pixels, CairoFormat.ARGB32, lock.width, lock.height, lock.pitch); + primarySurface = CairoImageSurface.create (lock.pixels, CairoFormat.ARGB32, lock.width, lock.height, lock.pitch); if (cairo != null) { - cairo.recreate( primarySurface ); + cairo.recreate (primarySurface); } else { @@ -112,7 +116,6 @@ class NativeRenderer { } - } cacheLock = lock; diff --git a/lime/graphics/cairo/CairoImageSurface.hx b/lime/graphics/cairo/CairoImageSurface.hx new file mode 100644 index 000000000..930791e1a --- /dev/null +++ b/lime/graphics/cairo/CairoImageSurface.hx @@ -0,0 +1,131 @@ +package lime.graphics.cairo; + + +import lime.system.System; + + +@:forward abstract CairoImageSurface(CairoSurface) from CairoSurface to CairoSurface { + + + public var data (get, never):Dynamic; + public var format (get, never):CairoFormat; + public var height (get, never):Int; + public var stride (get, never):Int; + public var width (get, never):Int; + + + public function new (format:CairoFormat, width:Int, height:Int):CairoSurface { + + #if lime_cairo + this = lime_cairo_image_surface_create (format, width, height); + #else + this = 0; + #end + + } + + + public static function create (data:Dynamic, format:CairoFormat, width:Int, height:Int, stride:Int):CairoSurface { + + #if lime_cairo + return lime_cairo_image_surface_create_for_data (data, format, width, height, stride); + #else + return cast 0; + #end + + } + + + public static function fromImage (image:Image):CairoSurface { + + #if lime_cairo + return create (lime_bytes_get_data_pointer (#if nodejs image.data #else image.data.buffer #end), CairoFormat.ARGB32, image.width, image.height, image.buffer.stride); + #else + return null; + #end + + } + + + + + // Get & Set Methods + + + + + @:noCompletion private function get_data ():Dynamic { + + #if lime_cairo + return lime_cairo_image_surface_get_data (this); + #else + return null; + #end + + } + + + @:noCompletion private function get_format ():CairoFormat { + + #if lime_cairo + return lime_cairo_image_surface_get_format (this); + #else + return 0; + #end + + } + + + @:noCompletion private function get_height ():Int { + + #if lime_cairo + return lime_cairo_image_surface_get_height (this); + #else + return 0; + #end + + } + + + @:noCompletion private function get_stride ():Int { + + #if lime_cairo + return lime_cairo_image_surface_get_stride (this); + #else + return 0; + #end + + } + + + @:noCompletion private function get_width ():Int { + + #if lime_cairo + return lime_cairo_image_surface_get_width (this); + #else + return 0; + #end + + } + + + + + // Native Methods + + + + + #if lime_cairo + private static var lime_bytes_get_data_pointer = System.load ("lime", "lime_bytes_get_data_pointer", 1); + private static var lime_cairo_image_surface_create = System.load ("lime", "lime_cairo_image_surface_create", 3); + private static var lime_cairo_image_surface_create_for_data = System.load ("lime", "lime_cairo_image_surface_create_for_data", 5); + private static var lime_cairo_image_surface_get_data = System.load ("lime", "lime_cairo_image_surface_get_data", 1); + private static var lime_cairo_image_surface_get_format = System.load ("lime", "lime_cairo_image_surface_get_format", 1); + private static var lime_cairo_image_surface_get_height = System.load ("lime", "lime_cairo_image_surface_get_height", 1); + private static var lime_cairo_image_surface_get_stride = System.load ("lime", "lime_cairo_image_surface_get_stride", 1); + private static var lime_cairo_image_surface_get_width = System.load ("lime", "lime_cairo_image_surface_get_width", 1); + #end + + +} \ No newline at end of file diff --git a/lime/graphics/cairo/CairoSurface.hx b/lime/graphics/cairo/CairoSurface.hx index 725ef20d1..5ef637b19 100644 --- a/lime/graphics/cairo/CairoSurface.hx +++ b/lime/graphics/cairo/CairoSurface.hx @@ -11,32 +11,6 @@ import lime.utils.ByteArray; abstract CairoSurface(Dynamic) { - public var height (get, never):Int; - public var width (get, never):Int; - - - public function new (format:CairoFormat, width:Int, height:Int):CairoSurface { - - #if lime_cairo - this = lime_cairo_image_surface_create (format, width, height); - #else - this = 0; - #end - - } - - - public static function createForData (data:Dynamic, format:CairoFormat, width:Int, height:Int, stride:Int):CairoSurface { - - #if lime_cairo - return lime_cairo_image_surface_create_for_data (data, format, width, height, stride); - #else - return cast 0; - #end - - } - - public function destroy ():Void { #if lime_cairo @@ -55,46 +29,6 @@ abstract CairoSurface(Dynamic) { } - public static function fromImage (image:Image):CairoSurface { - - #if lime_cairo - return createForData (lime_bytes_get_data_pointer (#if nodejs image.data #else image.data.buffer #end), CairoFormat.ARGB32, image.width, image.height, image.buffer.stride); - #else - return null; - #end - - } - - - - - // Get & Set Methods - - - - - @:noCompletion private function get_height ():Int { - - #if lime_cairo - return lime_cairo_image_surface_get_height (this); - #else - return 0; - #end - - } - - - @:noCompletion private function get_width ():Int { - - #if lime_cairo - return lime_cairo_image_surface_get_width (this); - #else - return 0; - #end - - } - - // Native Methods @@ -103,11 +37,6 @@ abstract CairoSurface(Dynamic) { #if lime_cairo - private static var lime_bytes_get_data_pointer = System.load ("lime", "lime_bytes_get_data_pointer", 1); - private static var lime_cairo_image_surface_create = System.load ("lime", "lime_cairo_image_surface_create", 3); - private static var lime_cairo_image_surface_create_for_data = System.load ("lime", "lime_cairo_image_surface_create_for_data", 5); - private static var lime_cairo_image_surface_get_height = System.load ("lime", "lime_cairo_image_surface_get_height", 1); - private static var lime_cairo_image_surface_get_width = System.load ("lime", "lime_cairo_image_surface_get_width", 1); private static var lime_cairo_surface_destroy = System.load ("lime", "lime_cairo_surface_destroy", 1); private static var lime_cairo_surface_flush = System.load ("lime", "lime_cairo_surface_flush", 1); #end diff --git a/lime/utils/ByteArray.hx b/lime/utils/ByteArray.hx index c64da9d1d..712f2b95c 100644 --- a/lime/utils/ByteArray.hx +++ b/lime/utils/ByteArray.hx @@ -905,6 +905,16 @@ class ByteArray #if !js extends Bytes implements ArrayAccess implements IDa } + #if (cpp || neko || nodejs) + public static function __fromNativePointer (data:Dynamic, length:Int):ByteArray { + + var bytes = lime_bytes_from_data_pointer (data, length); + return ByteArray.fromBytes (@:privateAccess new Bytes (bytes.length, bytes.b)); + + } + #end + + @:keep public inline function __get (pos:Int):Int { #if js @@ -1092,6 +1102,7 @@ class ByteArray #if !js extends Bytes implements ArrayAccess implements IDa + private static var lime_bytes_from_data_pointer = System.load ("lime", "lime_bytes_from_data_pointer", 2); private static var lime_bytes_get_data_pointer = System.load ("lime", "lime_bytes_get_data_pointer", 1); private static var lime_bytes_read_file = System.load ("lime", "lime_bytes_read_file", 1); diff --git a/project/src/ExternalInterface.cpp b/project/src/ExternalInterface.cpp index 8835cc269..764b204d0 100644 --- a/project/src/ExternalInterface.cpp +++ b/project/src/ExternalInterface.cpp @@ -127,6 +127,23 @@ namespace lime { } + value lime_bytes_from_data_pointer (value data, value length) { + + int size = val_int (length); + intptr_t ptr = (intptr_t)val_float (data); + Bytes bytes = Bytes (size); + + if (ptr) { + + memcpy (bytes.Data (), (const void*)ptr, size); + + } + + return bytes.Value (); + + } + + value lime_bytes_get_data_pointer (value bytes) { Bytes data = Bytes (bytes); @@ -1111,6 +1128,7 @@ namespace lime { DEFINE_PRIM (lime_application_set_frame_rate, 2); DEFINE_PRIM (lime_application_update, 1); DEFINE_PRIM (lime_audio_load, 1); + DEFINE_PRIM (lime_bytes_from_data_pointer, 2); DEFINE_PRIM (lime_bytes_get_data_pointer, 1); DEFINE_PRIM (lime_bytes_read_file, 1); DEFINE_PRIM (lime_font_get_ascender, 1); diff --git a/project/src/graphics/cairo/CairoBindings.cpp b/project/src/graphics/cairo/CairoBindings.cpp index 09b2d63ef..ec3ac8a79 100644 --- a/project/src/graphics/cairo/CairoBindings.cpp +++ b/project/src/graphics/cairo/CairoBindings.cpp @@ -368,16 +368,37 @@ namespace lime { } + value lime_cairo_image_surface_get_data (value handle) { + + return alloc_float ((intptr_t)cairo_image_surface_get_data ((cairo_surface_t*)(intptr_t)val_float (handle))); + + } + + + value lime_cairo_image_surface_get_format (value handle) { + + return alloc_int ((int)cairo_image_surface_get_format ((cairo_surface_t*)(intptr_t)val_float (handle))); + + } + + value lime_cairo_image_surface_get_height (value handle) { - return alloc_int ((intptr_t)cairo_image_surface_get_height ((cairo_surface_t*)(intptr_t)val_float (handle))); + return alloc_int (cairo_image_surface_get_height ((cairo_surface_t*)(intptr_t)val_float (handle))); + + } + + + value lime_cairo_image_surface_get_stride (value handle) { + + return alloc_int (cairo_image_surface_get_stride ((cairo_surface_t*)(intptr_t)val_float (handle))); } value lime_cairo_image_surface_get_width (value handle) { - return alloc_int ((intptr_t)cairo_image_surface_get_width ((cairo_surface_t*)(intptr_t)val_float (handle))); + return alloc_int (cairo_image_surface_get_width ((cairo_surface_t*)(intptr_t)val_float (handle))); } @@ -993,7 +1014,10 @@ namespace lime { DEFINE_PRIM (lime_cairo_identity_matrix, 1); DEFINE_PRIM (lime_cairo_image_surface_create, 3); DEFINE_PRIM (lime_cairo_image_surface_create_for_data, 5); + DEFINE_PRIM (lime_cairo_image_surface_get_data, 1); + DEFINE_PRIM (lime_cairo_image_surface_get_format, 1); DEFINE_PRIM (lime_cairo_image_surface_get_height, 1); + DEFINE_PRIM (lime_cairo_image_surface_get_stride, 1); DEFINE_PRIM (lime_cairo_image_surface_get_width, 1); DEFINE_PRIM (lime_cairo_in_clip, 3); DEFINE_PRIM (lime_cairo_in_fill, 3);