Răsfoiți Sursa

cleanup hxd.impl.Tmp: will rethink efficient mem management wrt hl/js

Nicolas Cannasse 7 ani în urmă
părinte
comite
2c4fc920e7

+ 2 - 4
h3d/prim/HMDModel.hx

@@ -55,10 +55,9 @@ class HMDModel extends MeshPrimitive {
 
 		entry.skip(dataPosition + data.vertexPosition);
 		var size = data.vertexCount * data.vertexStride * 4;
-		var bytes = hxd.impl.Tmp.getBytes(size);
+		var bytes = haxe.io.Bytes.alloc(size);
 		entry.read(bytes, 0, size);
 		buffer.uploadBytes(bytes, 0, data.vertexCount);
-		hxd.impl.Tmp.saveBytes(bytes);
 
 		indexCount = 0;
 		indexesTriPos = [];
@@ -69,10 +68,9 @@ class HMDModel extends MeshPrimitive {
 		indexes = new h3d.Indexes(indexCount);
 
 		entry.skip(data.indexPosition - (data.vertexPosition + size));
-		var bytes = hxd.impl.Tmp.getBytes(indexCount * 2);
+		var bytes = haxe.io.Bytes.alloc(indexCount * 2);
 		entry.read(bytes, 0, indexCount * 2);
 		indexes.uploadBytes(bytes, 0, indexCount);
-		hxd.impl.Tmp.saveBytes(bytes);
 
 		entry.close();
 

+ 1 - 1
hxd/BitmapData.hx

@@ -418,7 +418,7 @@ class BitmapData {
 		var p = new Pixels(width, height, this.data.data.buffer, RGBA);
 		return p;
 		#else
-		var out = hxd.impl.Tmp.getBytes(data.width * data.height * 4);
+		var out = haxe.io.Bytes.alloc(data.width * data.height * 4);
 		for( i in 0...data.width*data.height )
 			out.setInt32(i << 2, data.pixels[i]);
 		return new Pixels(data.width, data.height, out, BGRA);

+ 12 - 16
hxd/Pixels.hx

@@ -100,7 +100,7 @@ class Pixels {
 	public function sub( x : Int, y : Int, width : Int, height : Int ) {
 		if( x < 0 || y < 0 || x + width > this.width || y + height > this.height )
 			throw "Pixels.sub() outside bounds";
-		var out = hxd.impl.Tmp.getBytes(width * height * bpp);
+		var out = haxe.io.Bytes.alloc(width * height * bpp);
 		var stride = width * bpp;
 		var outP = 0;
 		for( dy in 0...height ) {
@@ -210,24 +210,23 @@ class Pixels {
 		while( tw < w ) tw <<= 1;
 		while( th < h ) th <<= 1;
 		if( w == tw && h == th ) return this;
-		var out = hxd.impl.Tmp.getBytes(tw * th * 4);
+		var out = haxe.io.Bytes.alloc(tw * th * bpp);
 		var p = 0, b = offset;
 		for( y in 0...h ) {
-			out.blit(p, bytes, b, w * 4);
-			p += w * 4;
-			b += w * 4;
-			for( i in 0...tw - w ) {
+			out.blit(p, bytes, b, w * bpp);
+			p += w * bpp;
+			b += w * bpp;
+			for( i in 0...((tw - w) * bpp) >> 2 ) {
 				out.setInt32(p, 0);
 				p += 4;
 			}
 		}
-		for( i in 0...(th - h) * tw ) {
+		for( i in 0...((th - h) * tw * bpp) >> 2 ) {
 			out.setInt32(p, 0);
 			p += 4;
 		}
 		if( copy )
 			return new Pixels(tw, th, out, format);
-		if( !flags.has(ReadOnly) ) hxd.impl.Tmp.saveBytes(bytes);
 		bytes = out;
 		width = tw;
 		height = th;
@@ -236,8 +235,8 @@ class Pixels {
 
 	function copyInner() {
 		var old = bytes;
-		bytes = hxd.impl.Tmp.getBytes(width * height * 4);
-		bytes.blit(0, old, offset, width * height * 4);
+		bytes = haxe.io.Bytes.alloc(width * height * bpp);
+		bytes.blit(0, old, offset, width * height * bpp);
 		offset = 0;
 		flags.unset(ReadOnly);
 	}
@@ -353,10 +352,7 @@ class Pixels {
 	}
 
 	public function dispose() {
-		if( bytes != null ) {
-			if( !flags.has(ReadOnly) ) hxd.impl.Tmp.saveBytes(bytes);
-			bytes = null;
-		}
+		bytes = null;
 	}
 
 	public function toPNG( ?level = 9 ) {
@@ -380,7 +376,7 @@ class Pixels {
 		p.flags.unset(ReadOnly);
 		if( bytes != null ) {
 			var size = width * height * bpp;
-			p.bytes = hxd.impl.Tmp.getBytes(size);
+			p.bytes = haxe.io.Bytes.alloc(size);
 			p.bytes.blit(0, bytes, offset, size);
 		}
 		return p;
@@ -419,7 +415,7 @@ class Pixels {
 	}
 
 	public static function alloc( width, height, format : PixelFormat ) {
-		return new Pixels(width, height, hxd.impl.Tmp.getBytes(width * height * bytesPerPixel(format)), format);
+		return new Pixels(width, height, haxe.io.Bytes.alloc(width * height * bytesPerPixel(format)), format);
 	}
 
 }

+ 6 - 12
hxd/fmt/hmd/Library.hx

@@ -141,7 +141,7 @@ class Library {
 		}
 
 		var vsize = geom.vertexCount * geom.vertexStride * 4;
-		var vbuf = hxd.impl.Tmp.getBytes(vsize);
+		var vbuf = haxe.io.Bytes.alloc(vsize);
 		var entry = resource.entry;
 		entry.open();
 		entry.skip(header.dataPosition + geom.vertexPosition);
@@ -159,7 +159,7 @@ class Library {
 			entry.skip(ipos * 2);
 			isize = geom.indexCounts[material] * 2;
 		}
-		var ibuf = hxd.impl.Tmp.getBytes(isize);
+		var ibuf = haxe.io.Bytes.alloc(isize);
 		entry.read(ibuf, 0, isize);
 
 		var buf = new GeometryBuffer();
@@ -247,8 +247,6 @@ class Library {
 		}
 
 		entry.close();
-		hxd.impl.Tmp.saveBytes(ibuf);
-		hxd.impl.Tmp.saveBytes(vbuf);
 		return buf;
 	}
 
@@ -412,7 +410,7 @@ class Library {
 					frameCount = 1;
 				var fl = new haxe.ds.Vector<h3d.anim.LinearAnimation.LinearFrame>(frameCount);
 				var size = ((pos ? 3 : 0) + (rot ? 3 : 0) + (scale?3:0)) * 4 * frameCount;
-				var data = hxd.impl.Tmp.getBytes(size);
+				var data = haxe.io.Bytes.alloc(size);
 				entry.read(data, 0, size);
 				var p = 0;
 				for( i in 0...frameCount ) {
@@ -450,38 +448,34 @@ class Library {
 					fl[i] = f;
 				}
 				l.addCurve(o.name, fl, rot, scale);
-				hxd.impl.Tmp.saveBytes(data);
 			}
 			if( o.flags.has(HasUV) ) {
 				var fl = new haxe.ds.Vector(a.frames*2);
 				var size = 2 * 4 * a.frames;
-				var data = hxd.impl.Tmp.getBytes(size);
+				var data = haxe.io.Bytes.alloc(size);
 				entry.read(data, 0, size);
 				for( i in 0...fl.length )
 					fl[i] = data.getFloat(i * 4);
 				l.addUVCurve(o.name, fl);
-				hxd.impl.Tmp.saveBytes(data);
 			}
 			if( o.flags.has(HasAlpha) ) {
 				var fl = new haxe.ds.Vector(a.frames);
 				var size = 4 * a.frames;
-				var data = hxd.impl.Tmp.getBytes(size);
+				var data = haxe.io.Bytes.alloc(size);
 				entry.read(data, 0, size);
 				for( i in 0...fl.length )
 					fl[i] = data.getFloat(i * 4);
 				l.addAlphaCurve(o.name, fl);
-				hxd.impl.Tmp.saveBytes(data);
 			}
 			if( o.flags.has(HasProps) ) {
 				for( p in o.props ) {
 					var fl = new haxe.ds.Vector(a.frames);
 					var size = 4 * a.frames;
-					var data = hxd.impl.Tmp.getBytes(size);
+					var data = haxe.io.Bytes.alloc(size);
 					entry.read(data, 0, size);
 					for( i in 0...fl.length )
 						fl[i] = data.getFloat(i * 4);
 					l.addPropCurve(o.name, p, fl);
-					hxd.impl.Tmp.saveBytes(data);
 				}
 			}
 		}

+ 1 - 11
hxd/fmt/pak/FileSystem.hx

@@ -76,23 +76,13 @@ private class PakEntry extends FileEntry {
 		return pak.read(file.dataSize);
 	}
 
-	override function getTmpBytes() {
-		// don't use cacheBytes
-		pak.seek(file.dataPosition, SeekBegin);
-		var tmp = hxd.impl.Tmp.getBytes(file.dataSize);
-		fs.totalReadBytes += file.dataSize;
-		fs.totalReadCount++;
-		pak.readFullBytes(tmp, 0, file.dataSize);
-		return tmp;
-	}
-
 	override function open() {
 		if( openedBytes == null )
 			openedBytes = fs.getCached(this);
 		if( openedBytes == null ) {
 			fs.totalReadBytes += file.dataSize;
 			fs.totalReadCount++;
-			openedBytes = hxd.impl.Tmp.getBytes(file.dataSize);
+			openedBytes = haxe.io.Bytes.alloc(file.dataSize);
 			pak.seek(file.dataPosition, SeekBegin);
 			pak.readBytes(openedBytes, 0, file.dataSize);
 		}

+ 0 - 2
hxd/fs/FileEntry.hx

@@ -17,8 +17,6 @@ class FileEntry {
 
 	public function getText() return getBytes().toString();
 
-	public function getTmpBytes() return getBytes();
-
 	public function open() { }
 	public function skip( nbytes : Int ) { }
 	public function readByte() : Int return 0;

+ 0 - 15
hxd/fs/LocalFileSystem.hx

@@ -41,21 +41,6 @@ private class LocalEntry extends FileEntry {
 		#end
 	}
 
-	override function getTmpBytes() {
-		#if flash
-		if( checkExists && !file.exists )
-			return haxe.io.Bytes.alloc(0);
-		var fs = new flash.filesystem.FileStream();
-		fs.open(file, flash.filesystem.FileMode.READ);
-		var bytes = hxd.impl.Tmp.getBytes(fs.bytesAvailable);
-		fs.readBytes(bytes.getData());
-		fs.close();
-		return bytes;
-		#else
-		return sys.io.File.getBytes(file);
-		#end
-	}
-
 	override function getBytes() : haxe.io.Bytes {
 		#if flash
 		if( checkExists && !file.exists )

+ 0 - 56
hxd/impl/Tmp.hx

@@ -1,56 +0,0 @@
-package hxd.impl;
-
-class Tmp {
-
-	static var bytes = new Array<haxe.io.Bytes>();
-
-	public static dynamic function outOfMemory() {
-	}
-
-	public static function getBytes( size : Int ) {
-		#if hl return haxe.io.Bytes.alloc(size); #end
-		var found = -1;
-		for( i in 0...bytes.length ) {
-			var b = bytes[i];
-			if( b.length >= size ) found = i;
-		}
-		if( found >= 0 ) {
-			var b = bytes[found];
-			bytes.splice(found, 1);
-			return b;
-		}
-		var sz = 1024;
-		while( sz < size )
-			sz = (sz * 3) >> 1;
-		return allocBytes(sz);
-	}
-
-	public static function freeMemory() {
-		bytes = [];
-		outOfMemory();
-	}
-
-	public static function allocBytes( size : Int ) {
-		try {
-			return haxe.io.Bytes.alloc(size);
-		} catch( e : Dynamic ) {
-			// out of memory
-			freeMemory();
-			return haxe.io.Bytes.alloc(size);
-		}
-	}
-
-	public static function saveBytes( b : haxe.io.Bytes ) {
-		#if hl return; #end
-		for( i in 0...bytes.length ) {
-			if( bytes[i].length <= b.length ) {
-				bytes.insert(i, b);
-				if( bytes.length > 8 )
-					bytes.pop();
-				return;
-			}
-		}
-		bytes.push(b);
-	}
-
-}

+ 5 - 15
hxd/res/Image.hx

@@ -115,13 +115,7 @@ class Image extends Resource {
 			pixels = Pixels.alloc(inf.width, inf.height, BGRA);
 			#if( format >= "3.1.3" )
 			var pdata = png.read();
-			try {
-				format.png.Tools.extract32(pdata, pixels.bytes, flipY);
-			} catch( e : Dynamic ) {
-				// most likely, out of memory
-				hxd.impl.Tmp.freeMemory();
-				format.png.Tools.extract32(pdata, pixels.bytes, flipY);
-			}
+			format.png.Tools.extract32(pdata, pixels.bytes, flipY);
 			if( flipY ) pixels.flags.set(FlipY);
 			#else
 			format.png.Tools.extract32(png.read(), pixels.bytes);
@@ -157,11 +151,9 @@ class Image extends Resource {
 			fmt = BGRA;
 			BGRA;
 		};
-		var dst = hxd.impl.Tmp.getBytes(width * height * 4);
-		if( !hl.Format.decodeJPG(src.getData(), src.length, dst.getData(), width, height, width * 4, ifmt, (flipY?1:0)) ) {
-			hxd.impl.Tmp.saveBytes(dst);
+		var dst = haxe.io.Bytes.alloc(width * height * 4);
+		if( !hl.Format.decodeJPG(src.getData(), src.length, dst.getData(), width, height, width * 4, ifmt, (flipY?1:0)) )
 			return null;
-		}
 		var pix = new hxd.Pixels(width, height, dst, fmt);
 		if( flipY ) pix.flags.set(FlipY);
 		return pix;
@@ -176,11 +168,9 @@ class Image extends Resource {
 			fmt = BGRA;
 			BGRA;
 		};
-		var dst = hxd.impl.Tmp.getBytes(width * height * 4);
-		if( !hl.Format.decodePNG(src.getData(), src.length, dst.getData(), width, height, width * 4, ifmt, (flipY?1:0)) ) {
-			hxd.impl.Tmp.saveBytes(dst);
+		var dst = haxe.io.Bytes.alloc(width * height * 4);
+		if( !hl.Format.decodePNG(src.getData(), src.length, dst.getData(), width, height, width * 4, ifmt, (flipY?1:0)) )
 			return null;
-		}
 		var pix = new hxd.Pixels(width, height, dst, fmt);
 		if( flipY ) pix.flags.set(FlipY);
 		return pix;

+ 1 - 2
hxd/res/NanoJpeg.hx

@@ -104,11 +104,10 @@ class NanoJpeg {
 	}
 
 	inline function alloc( nbytes : Int ) {
-		return hxd.impl.Tmp.getBytes(nbytes);
+		return haxe.io.Bytes.alloc(nbytes);
 	}
 
 	inline function free( bytes : haxe.io.Bytes ) {
-		hxd.impl.Tmp.saveBytes(bytes);
 	}
 
 	function njInit( bytes, pos, size, filter ) {

+ 1 - 1
hxd/snd/Manager.hx

@@ -511,7 +511,7 @@ class Manager {
 		var bpp = data.getBytesPerSample();
 		var reqSize = STREAM_BUFFER_SAMPLE_COUNT * bpp;
 		if( s.streamBuffer == null || s.streamBuffer.length < reqSize ) {
-			s.streamBuffer = hxd.impl.Tmp.getBytes(reqSize);
+			s.streamBuffer = haxe.io.Bytes.alloc(reqSize);
 			s.streamPos = start;
 		}
 		var remain = end - s.streamPos;