public compressor(GCbuf data) { this.data = data; }
/// <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(); }
/// <summary> /// Get the pixels within the update rectangle in CompressionType encoding /// </summary> /// <param name="x"></param> /// <param name="y"></param> /// <param name="w"></param> /// <param name="h"></param> /// <param name="type"></param> /// <returns></returns> private GCbuf GetRect(int x, int y, int w, int h, CompressionType type) { if (loaded == false) { return(null); } GCbuf retRect = null; try { retRect = new GCbuf(); } catch (Exception e) { // One reason for failure is when the client stops accepting new updates MessageBox.Show("FATAL: Memory allocation failed " + e.Message); Environment.Exit(1); } try { GetChangesBuffer getChangesBuffer = (GetChangesBuffer)Marshal.PtrToStructure(_getChangesBuffer, typeof(GetChangesBuffer)); IntPtr start; byte[] membuf = new byte[w * sizeof(UInt32)]; int bmCount = 0; int dtCount = w * h; if (type == CompressionType.RGB16) { retRect.Length = w * h * sizeof(UInt16); } else if (type == CompressionType.BITMAP) { retRect.Length = w * h * sizeof(byte); // Just the space for the bitmap } else { retRect.Length = w * h * sizeof(UInt32); // buffers are always width*height - we waste the remaining to ease in memory management and fragmentation } unsafe { start = new IntPtr(((Int32 *)getChangesBuffer.UserBuffer) + ((y * _bitmapWidth) + x)); } for (int j = 0; j < h; j++) { try { switch (type) { case CompressionType.BITMAP: Marshal.Copy(start, membuf, 0, w * sizeof(UInt32)); int indx = ((y + j) * _bitmapWidth + x) * sizeof(UInt32); for (int i = 0; i < (w * sizeof(UInt32)); i += sizeof(UInt32), indx += sizeof(UInt32)) { if ((membuf[i] == screen[indx]) && (membuf[i + 1] == screen[indx + 1]) && (membuf[i + 2] == screen[indx + 2])) { retRect.buf[bmCount++] = 0x00; } else { retRect.buf[bmCount++] = 0xFF; retRect.buf[dtCount++] = screen[indx] = membuf[i]; retRect.buf[dtCount++] = screen[indx + 1] = membuf[i + 1]; retRect.buf[dtCount++] = screen[indx + 2] = membuf[i + 2]; retRect.Length += 3; } } break; #if !PRODUCTION case CompressionType.RGB16: Marshal.Copy(start, membuf, 0, w * sizeof(UInt32)); for (int i = 0; i < (w * sizeof(UInt32)); i += sizeof(UInt32)) { UInt16 rgb; rgb = (ushort)(membuf[i] & 0xF8); // Blue rgb += (ushort)((membuf[i + 1] & 0xFE) << 5); // Green rgb += (ushort)((membuf[i + 2] * 0xF8) << 11); // Red retRect.buf[j * w * sizeof(UInt16) + i] = (byte)((rgb & 0xFF00) >> 8); retRect.buf[j * w * sizeof(UInt16) + i + 1] = (byte)(rgb & 0x00FF); } break; case CompressionType.ALPHA0: case CompressionType.ALPHA255: Marshal.Copy(start, retRect.buf, j * w * sizeof(UInt32), w * sizeof(UInt32)); int indx = ((y + j) * _bitmapWidth + x) * sizeof(UInt32); int ind = (j * w) * sizeof(UInt32); for (int i = 0; i < (w * sizeof(UInt32)); i += sizeof(UInt32)) { if ((retRect.buf[ind] == screen[indx]) && (retRect.buf[ind + 1] == screen[indx + 1]) && (retRect.buf[ind + 2] == screen[indx + 2])) { if (type == CompressionType.ALPHA0) { retRect.buf[ind] = 0x00; retRect.buf[ind + 1] = 0x00; retRect.buf[ind + 2] = 0x00; } if (type == CompressionType.ALPHA255) { retRect.buf[ind] = 0xFF; retRect.buf[ind + 1] = 0xFF; retRect.buf[ind + 2] = 0xFF; } retRect.buf[ind + 3] = 0; } else { if (type == CompressionType.ALPHA255) { screen[indx] = retRect.buf[ind]; screen[indx + 1] = retRect.buf[ind + 1]; screen[indx + 2] = retRect.buf[ind + 2]; } retRect.buf[ind + 3] = 0xFF; } indx += sizeof(UInt32); ind += sizeof(UInt32); } break; #endif } } catch (Exception e) { MessageBox.Show("FATAL: Failure to copy framebuffer data " + e.StackTrace); Environment.Exit(1); } unsafe { start = new IntPtr((Int32 *)start + _bitmapWidth); } } return(retRect); } catch (Exception e) { // Debug.WriteLine("FATAL: Failed to capture pixmap. Please report to Surendar Chandra. Ignoring for now"); MessageBox.Show("FATAL: Failed to capture pixmap. Please report to [email protected] " + e.Message); // myNotifyIcon.ShowBalloonTip(500, "Title", "Tip text", ToolTipIcon.Info); } return(retRect); }