Add CairoImageSurface, properly separate image surface methods from the CairoSurface type

This commit is contained in:
Joshua Granick
2015-07-15 16:40:53 -07:00
parent 6622ac818d
commit 0b4c553622
6 changed files with 193 additions and 77 deletions

View File

@@ -3,6 +3,7 @@ package lime._backend.native;
import lime.graphics.cairo.Cairo; import lime.graphics.cairo.Cairo;
import lime.graphics.cairo.CairoFormat; import lime.graphics.cairo.CairoFormat;
import lime.graphics.cairo.CairoImageSurface;
import lime.graphics.cairo.CairoSurface; import lime.graphics.cairo.CairoSurface;
import lime.graphics.CairoRenderContext; import lime.graphics.CairoRenderContext;
import lime.graphics.ConsoleRenderContext; 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 (cacheLock == null || cacheLock.pixels != lock.pixels || cacheLock.width != lock.width || cacheLock.height != lock.height) {
if ( primarySurface != null ) if (primarySurface != null) {
primarySurface.destroy (); 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) { if (cairo != null) {
cairo.recreate( primarySurface ); cairo.recreate (primarySurface);
} else { } else {
@@ -112,7 +116,6 @@ class NativeRenderer {
} }
} }
cacheLock = lock; cacheLock = lock;

View File

@@ -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
}

View File

@@ -11,32 +11,6 @@ import lime.utils.ByteArray;
abstract CairoSurface(Dynamic) { 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 { public function destroy ():Void {
#if lime_cairo #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 // Native Methods
@@ -103,11 +37,6 @@ abstract CairoSurface(Dynamic) {
#if lime_cairo #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_destroy = System.load ("lime", "lime_cairo_surface_destroy", 1);
private static var lime_cairo_surface_flush = System.load ("lime", "lime_cairo_surface_flush", 1); private static var lime_cairo_surface_flush = System.load ("lime", "lime_cairo_surface_flush", 1);
#end #end

View File

@@ -905,6 +905,16 @@ class ByteArray #if !js extends Bytes implements ArrayAccess<Int> 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 { @:keep public inline function __get (pos:Int):Int {
#if js #if js
@@ -1092,6 +1102,7 @@ class ByteArray #if !js extends Bytes implements ArrayAccess<Int> 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_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); private static var lime_bytes_read_file = System.load ("lime", "lime_bytes_read_file", 1);

View File

@@ -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) { value lime_bytes_get_data_pointer (value bytes) {
Bytes data = Bytes (bytes); Bytes data = Bytes (bytes);
@@ -1111,6 +1128,7 @@ namespace lime {
DEFINE_PRIM (lime_application_set_frame_rate, 2); DEFINE_PRIM (lime_application_set_frame_rate, 2);
DEFINE_PRIM (lime_application_update, 1); DEFINE_PRIM (lime_application_update, 1);
DEFINE_PRIM (lime_audio_load, 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_get_data_pointer, 1);
DEFINE_PRIM (lime_bytes_read_file, 1); DEFINE_PRIM (lime_bytes_read_file, 1);
DEFINE_PRIM (lime_font_get_ascender, 1); DEFINE_PRIM (lime_font_get_ascender, 1);

View File

@@ -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) { 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) { 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_identity_matrix, 1);
DEFINE_PRIM (lime_cairo_image_surface_create, 3); DEFINE_PRIM (lime_cairo_image_surface_create, 3);
DEFINE_PRIM (lime_cairo_image_surface_create_for_data, 5); 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_height, 1);
DEFINE_PRIM (lime_cairo_image_surface_get_stride, 1);
DEFINE_PRIM (lime_cairo_image_surface_get_width, 1); DEFINE_PRIM (lime_cairo_image_surface_get_width, 1);
DEFINE_PRIM (lime_cairo_in_clip, 3); DEFINE_PRIM (lime_cairo_in_clip, 3);
DEFINE_PRIM (lime_cairo_in_fill, 3); DEFINE_PRIM (lime_cairo_in_fill, 3);