/// <summary> /// /// </summary> private void pollingThreadProc() { // Why bother to poll while there is no one to receive the updates while (DesktopChange == null) Thread.Sleep(1); while (true) { if (restartingDriver == false) { var getChangesBuffer = (GetChangesBuffer)Marshal.PtrToStructure(_getChangesBuffer, typeof(GetChangesBuffer)); // Moved it inside the loop in hope of fixing the hibernation bug var buffer = (ChangesBuffer)Marshal.PtrToStructure(getChangesBuffer.Buffer, typeof(ChangesBuffer)); // Initialize oldCounter if (oldCounter == long.MaxValue) oldCounter = buffer.counter; if (oldCounter != buffer.counter) { // Trace.WriteLine("Updates: " + (buffer.counter - oldCounter)); for (long currentChange = oldCounter; currentChange != buffer.counter; currentChange++) { if (currentChange >= ChangesBuffer.MAXCHANGES_BUF) currentChange = 0; #if PRODUCTION DesktopChange(this, new DesktopChangeEventArgs(buffer.pointrect[currentChange].rect.x1, buffer.pointrect[currentChange].rect.y1, buffer.pointrect[currentChange].rect.x2, buffer.pointrect[currentChange].rect.y2, (OperationType)buffer.pointrect[currentChange].type)); #else int x, y, w, h; x = buffer.pointrect[currentChange].rect.x1; y = buffer.pointrect[currentChange].rect.y1; w = (buffer.pointrect[currentChange].rect.x2 - buffer.pointrect[currentChange].rect.x1); h = (buffer.pointrect[currentChange].rect.y2 - buffer.pointrect[currentChange].rect.y1); #if DEBUG_EXP_1 if (log == null) { log = new StreamWriter("c:/mmsys_1.txt"); log.AutoFlush = true; } diffT = (DateTime.Now - startTime); log.WriteLine(diffT.TotalMilliseconds + " " + x + " " + y + " " + w + " " + h); continue; #endif #if DEBUG_EXP_2 if (log == null) { log = new StreamWriter("c:/3.txt"); log.AutoFlush = true; } int a0Len, a255Len, rgbLen, vanillaLen, origLen, bitmapLen; origLen = w * h * sizeof(UInt32); diffT = (DateTime.Now - startTime); GCbuf a0 = GetRect(buffer.pointrect[currentChange].rect.x1, buffer.pointrect[currentChange].rect.y1, (buffer.pointrect[currentChange].rect.x2 - buffer.pointrect[currentChange].rect.x1), (buffer.pointrect[currentChange].rect.y2 - buffer.pointrect[currentChange].rect.y1), CompressionType.ALPHA0); int same = 0, diff = 0; for (int i = 0; i < a0.Length; i+=sizeof(UInt32)) if (a0.buf[i+3] == 0) same++; else diff++; compressor a0C = new compressor(a0); Thread a0t = new Thread(new ThreadStart(a0C.compressData)); a0t.Start(); GCbuf bitmap = new GCbuf(); Buffer.BlockCopy(a0.buf, 0, bitmap.buf, 0, a0.Length); bitmap.Length = a0.Length; compressor bC = new compressor(bitmap); Thread bT = new Thread(new ThreadStart(bC.bitmapCompressData)); bT.Start(); GCbuf vanilla = GetRect(buffer.pointrect[currentChange].rect.x1, buffer.pointrect[currentChange].rect.y1, (buffer.pointrect[currentChange].rect.x2 - buffer.pointrect[currentChange].rect.x1), (buffer.pointrect[currentChange].rect.y2 - buffer.pointrect[currentChange].rect.y1), CompressionType.VANILLA); compressor vC = new compressor(vanilla); Thread vt = new Thread(new ThreadStart(vC.compressData)); vt.Start(); GCbuf rgb = GetRect(buffer.pointrect[currentChange].rect.x1, buffer.pointrect[currentChange].rect.y1, (buffer.pointrect[currentChange].rect.x2 - buffer.pointrect[currentChange].rect.x1), (buffer.pointrect[currentChange].rect.y2 - buffer.pointrect[currentChange].rect.y1), CompressionType.RGB16); compressor rC = new compressor(rgb); Thread rt = new Thread(new ThreadStart(rC.compressData)); rt.Start(); GCbuf a255 = GetRect(buffer.pointrect[currentChange].rect.x1, buffer.pointrect[currentChange].rect.y1, (buffer.pointrect[currentChange].rect.x2 - buffer.pointrect[currentChange].rect.x1), (buffer.pointrect[currentChange].rect.y2 - buffer.pointrect[currentChange].rect.y1), CompressionType.ALPHA255); compressor a2C = new compressor(a255); Thread a2t = new Thread(new ThreadStart(a2C.compressData)); a2t.Start(); a0t.Join(); a0Len = a0C.retvalue; vt.Join(); vanillaLen = vC.retvalue; rt.Join(); rgbLen = rC.retvalue; a2t.Join(); a255Len = a2C.retvalue; bT.Join(); bitmapLen = bC.retvalue; log.WriteLine(diffT.TotalMilliseconds + " S " + same + " D " + diff + " O " + (w * h * sizeof(UInt32)) + " R " + rgbLen + " A0 " + a0Len + " A255 " + a255Len + " V " + vanillaLen + " B " + bitmapLen); a0.Dispose(); a255.Dispose(); rgb.Dispose(); vanilla.Dispose(); bitmap.Dispose(); continue; #endif #if DEBUG_EXP_3 if (log == null) { log = new StreamWriter("c:/mmsys_3.txt"); log.AutoFlush = true; } diffT = (DateTime.Now - startTime); GCbuf bitmap = GetRect(buffer.pointrect[currentChange].rect.x1, buffer.pointrect[currentChange].rect.y1, (buffer.pointrect[currentChange].rect.x2 - buffer.pointrect[currentChange].rect.x1), (buffer.pointrect[currentChange].rect.y2 - buffer.pointrect[currentChange].rect.y1), CompressionType.ALPHA0); compressor bC = new compressor(bitmap); bC.bitmapCompressData(); log.WriteLine(diffT.TotalMilliseconds + " " + bC.retvalue); bitmap.Dispose(); #endif #endif } oldCounter = buffer.counter; idleCount = 0; } else { #if PRODUCTION if (idleCount++ == (1000 / PollInterval)) { // One second DesktopChange(this, new DesktopChangeEventArgs(0, 0, _bitmapWidth, _bitmapHeight, OperationType.dmf_dfo_IGNORE)); // Trace.WriteLine("DEBUG: Sending iFrame oldCount: " + oldCounter + " buffer " + buffer.counter); idleCount = 0; } #endif } } // Just to prevent 100-percent CPU load and to provide thread-safety use manual reset event instead of simple in-memory flag. if (_terminatePollingThread.WaitOne(PollInterval, false)) { Trace.WriteLine("The thread now exits"); break; } } // We can be sure that _pollingThreadTerminated exists _pollingThreadTerminated.Set(); }
public Writer(compressor d = default, slice <byte> dict = default) { this.d = d; this.dict = dict; }
/// <summary> /// /// </summary> private void pollingThreadProc() { // Why bother to poll while there is no one to receive the updates while (DesktopChange == null) { Thread.Sleep(1); } while (true) { if (restartingDriver == false) { var getChangesBuffer = (GetChangesBuffer)Marshal.PtrToStructure(_getChangesBuffer, typeof(GetChangesBuffer)); // Moved it inside the loop in hope of fixing the hibernation bug var buffer = (ChangesBuffer)Marshal.PtrToStructure(getChangesBuffer.Buffer, typeof(ChangesBuffer)); // Initialize oldCounter if (oldCounter == long.MaxValue) { oldCounter = buffer.counter; } if (oldCounter != buffer.counter) { // Trace.WriteLine("Updates: " + (buffer.counter - oldCounter)); for (long currentChange = oldCounter; currentChange != buffer.counter; currentChange++) { if (currentChange >= ChangesBuffer.MAXCHANGES_BUF) { currentChange = 0; } #if PRODUCTION DesktopChange(this, new DesktopChangeEventArgs(buffer.pointrect[currentChange].rect.x1, buffer.pointrect[currentChange].rect.y1, buffer.pointrect[currentChange].rect.x2, buffer.pointrect[currentChange].rect.y2, (OperationType)buffer.pointrect[currentChange].type)); #else int x, y, w, h; x = buffer.pointrect[currentChange].rect.x1; y = buffer.pointrect[currentChange].rect.y1; w = (buffer.pointrect[currentChange].rect.x2 - buffer.pointrect[currentChange].rect.x1); h = (buffer.pointrect[currentChange].rect.y2 - buffer.pointrect[currentChange].rect.y1); #if DEBUG_EXP_1 if (log == null) { log = new StreamWriter("c:/mmsys_1.txt"); log.AutoFlush = true; } diffT = (DateTime.Now - startTime); log.WriteLine(diffT.TotalMilliseconds + " " + x + " " + y + " " + w + " " + h); continue; #endif #if DEBUG_EXP_2 if (log == null) { log = new StreamWriter("c:/3.txt"); log.AutoFlush = true; } int a0Len, a255Len, rgbLen, vanillaLen, origLen, bitmapLen; origLen = w * h * sizeof(UInt32); diffT = (DateTime.Now - startTime); GCbuf a0 = GetRect(buffer.pointrect[currentChange].rect.x1, buffer.pointrect[currentChange].rect.y1, (buffer.pointrect[currentChange].rect.x2 - buffer.pointrect[currentChange].rect.x1), (buffer.pointrect[currentChange].rect.y2 - buffer.pointrect[currentChange].rect.y1), CompressionType.ALPHA0); int same = 0, diff = 0; for (int i = 0; i < a0.Length; i += sizeof(UInt32)) { if (a0.buf[i + 3] == 0) { same++; } else { diff++; } } compressor a0C = new compressor(a0); Thread a0t = new Thread(new ThreadStart(a0C.compressData)); a0t.Start(); GCbuf bitmap = new GCbuf(); Buffer.BlockCopy(a0.buf, 0, bitmap.buf, 0, a0.Length); bitmap.Length = a0.Length; compressor bC = new compressor(bitmap); Thread bT = new Thread(new ThreadStart(bC.bitmapCompressData)); bT.Start(); GCbuf vanilla = GetRect(buffer.pointrect[currentChange].rect.x1, buffer.pointrect[currentChange].rect.y1, (buffer.pointrect[currentChange].rect.x2 - buffer.pointrect[currentChange].rect.x1), (buffer.pointrect[currentChange].rect.y2 - buffer.pointrect[currentChange].rect.y1), CompressionType.VANILLA); compressor vC = new compressor(vanilla); Thread vt = new Thread(new ThreadStart(vC.compressData)); vt.Start(); GCbuf rgb = GetRect(buffer.pointrect[currentChange].rect.x1, buffer.pointrect[currentChange].rect.y1, (buffer.pointrect[currentChange].rect.x2 - buffer.pointrect[currentChange].rect.x1), (buffer.pointrect[currentChange].rect.y2 - buffer.pointrect[currentChange].rect.y1), CompressionType.RGB16); compressor rC = new compressor(rgb); Thread rt = new Thread(new ThreadStart(rC.compressData)); rt.Start(); GCbuf a255 = GetRect(buffer.pointrect[currentChange].rect.x1, buffer.pointrect[currentChange].rect.y1, (buffer.pointrect[currentChange].rect.x2 - buffer.pointrect[currentChange].rect.x1), (buffer.pointrect[currentChange].rect.y2 - buffer.pointrect[currentChange].rect.y1), CompressionType.ALPHA255); compressor a2C = new compressor(a255); Thread a2t = new Thread(new ThreadStart(a2C.compressData)); a2t.Start(); a0t.Join(); a0Len = a0C.retvalue; vt.Join(); vanillaLen = vC.retvalue; rt.Join(); rgbLen = rC.retvalue; a2t.Join(); a255Len = a2C.retvalue; bT.Join(); bitmapLen = bC.retvalue; log.WriteLine(diffT.TotalMilliseconds + " S " + same + " D " + diff + " O " + (w * h * sizeof(UInt32)) + " R " + rgbLen + " A0 " + a0Len + " A255 " + a255Len + " V " + vanillaLen + " B " + bitmapLen); a0.Dispose(); a255.Dispose(); rgb.Dispose(); vanilla.Dispose(); bitmap.Dispose(); continue; #endif #if DEBUG_EXP_3 if (log == null) { log = new StreamWriter("c:/mmsys_3.txt"); log.AutoFlush = true; } diffT = (DateTime.Now - startTime); GCbuf bitmap = GetRect(buffer.pointrect[currentChange].rect.x1, buffer.pointrect[currentChange].rect.y1, (buffer.pointrect[currentChange].rect.x2 - buffer.pointrect[currentChange].rect.x1), (buffer.pointrect[currentChange].rect.y2 - buffer.pointrect[currentChange].rect.y1), CompressionType.ALPHA0); compressor bC = new compressor(bitmap); bC.bitmapCompressData(); log.WriteLine(diffT.TotalMilliseconds + " " + bC.retvalue); bitmap.Dispose(); #endif #endif } oldCounter = buffer.counter; idleCount = 0; } else { #if PRODUCTION if (idleCount++ == (1000 / PollInterval)) // One second { DesktopChange(this, new DesktopChangeEventArgs(0, 0, _bitmapWidth, _bitmapHeight, OperationType.dmf_dfo_IGNORE)); // Trace.WriteLine("DEBUG: Sending iFrame oldCount: " + oldCounter + " buffer " + buffer.counter); idleCount = 0; } #endif } } // Just to prevent 100-percent CPU load and to provide thread-safety use manual reset event instead of simple in-memory flag. if (_terminatePollingThread.WaitOne(PollInterval, false)) { Trace.WriteLine("The thread now exits"); break; } } // We can be sure that _pollingThreadTerminated exists _pollingThreadTerminated.Set(); }