/// <summary> /// Called after deserialization occurs so that certain things that are non-serializable /// can be set up. /// </summary> /// <param name="sender"></param> public void OnDeserialization(object sender) { this.updateRegion = new Vector <Rectangle>(); this.updateRegion.Add(this.Bounds); this.threadPool = new PixelDotNet.Threading.ThreadPool(); SetupEvents(); Dirty = true; }
public void OnDeserialization(object sender) { threadPool = new PixelDotNet.Threading.ThreadPool(); if (this.path != null) { this.shadowPath = this.path.Clone(); } }
void IDeferredSerializable.FinishSerialization(Stream output, DeferredFormatter context) { bool useCompression = context.UseCompression; // formatVersion = 0 for GZIP, or 1 for uncompressed if (useCompression) { output.WriteByte(0); } else { output.WriteByte(1); } // chunkSize WriteUInt(output, serializationChunkSize); uint chunkCount = (uint)((this.length + (long)serializationChunkSize - 1) / (long)serializationChunkSize); PixelDotNet.Threading.ThreadPool threadPool = new PixelDotNet.Threading.ThreadPool(Processor.LogicalCpuCount); ArrayList exceptions = ArrayList.Synchronized(new ArrayList(Processor.LogicalCpuCount)); WaitCallback callback = new WaitCallback(SerializeChunk); object previousLock = null; for (uint chunk = 0; chunk < chunkCount; ++chunk) { long chunkOffset = (long)chunk * (long)serializationChunkSize; uint chunkSize = Math.Min((uint)serializationChunkSize, (uint)(this.length - chunkOffset)); SerializeChunkParms parms = new SerializeChunkParms(output, chunk, chunkOffset, chunkSize, previousLock, context, exceptions); threadPool.QueueUserWorkItem(callback, parms); previousLock = parms; } threadPool.Drain(); output.Flush(); if (exceptions.Count > 0) { throw new SerializationException("Exception thrown by worker thread", (Exception)exceptions[0]); } return; }
void IDeferredSerializable.FinishDeserialization(Stream input, DeferredFormatter context) { // Allocate the memory if (this.bitmapWidth != 0 && this.bitmapHeight != 0) { this.voidStar = Allocate(this.bitmapWidth, this.bitmapHeight, out this.bitmapHandle).ToPointer(); this.valid = true; } else { this.voidStar = Allocate(this.length).ToPointer(); this.valid = true; } // formatVersion should equal 0 int formatVersion = input.ReadByte(); if (formatVersion == -1) { throw new EndOfStreamException(); } if (formatVersion != 0 && formatVersion != 1) { throw new SerializationException("formatVersion was neither zero nor one"); } // chunkSize uint chunkSize = ReadUInt(input); PixelDotNet.Threading.ThreadPool threadPool = new PixelDotNet.Threading.ThreadPool(Processor.LogicalCpuCount); ArrayList exceptions = new ArrayList(Processor.LogicalCpuCount); WaitCallback callback = new WaitCallback(DecompressChunk); // calculate chunkCount uint chunkCount = (uint)((this.length + (long)chunkSize - 1) / (long)chunkSize); bool[] chunksFound = new bool[chunkCount]; for (uint i = 0; i < chunkCount; ++i) { // chunkNumber uint chunkNumber = ReadUInt(input); if (chunkNumber >= chunkCount) { throw new SerializationException("chunkNumber read from stream is out of bounds"); } if (chunksFound[chunkNumber]) { throw new SerializationException("already encountered chunk #" + chunkNumber.ToString()); } chunksFound[chunkNumber] = true; // dataSize uint dataSize = ReadUInt(input); // calculate chunkOffset long chunkOffset = (long)chunkNumber * (long)chunkSize; // calculate decompressed chunkSize uint thisChunkSize = Math.Min(chunkSize, (uint)(this.length - chunkOffset)); // bounds checking if (chunkOffset < 0 || chunkOffset >= this.length || chunkOffset + thisChunkSize > this.length) { throw new SerializationException("data was specified to be out of bounds"); } // read compressed data byte[] compressedBytes = new byte[dataSize]; Utility.ReadFromStream(input, compressedBytes, 0, compressedBytes.Length); // decompress data if (formatVersion == 0) { DecompressChunkParms parms = new DecompressChunkParms(compressedBytes, thisChunkSize, chunkOffset, context, exceptions); threadPool.QueueUserWorkItem(callback, parms); } else { fixed(byte *pbSrc = compressedBytes) { Memory.Copy((void *)((byte *)this.VoidStar + chunkOffset), (void *)pbSrc, thisChunkSize); } } } threadPool.Drain(); if (exceptions.Count > 0) { throw new SerializationException("Exception thrown by worker thread", (Exception)exceptions[0]); } }