protected virtual void Dispose(bool disposing) { if (disposing) { lock (this.lockObj) { ColorPlugin.DisposeData(this.monitorData, true); ColorPlugin.DisposeData(this.inspectionData, true); } } }
public void UninitializeRender(EventType eventType) { switch (eventType) { case EventType.Monitor: ColorPlugin.DisposeData(this.monitorData, false); break; case EventType.Inspection: ColorPlugin.DisposeData(this.inspectionData, false); break; } }
private void HandleEvent(KStudioEvent eventObj, EventTypePluginData data, EventType eventType) { Debug.Assert(data != null); if (eventObj != null) { if ((eventObj.EventStreamDataTypeId == KStudioEventStreamDataTypeIds.UncompressedColor) || (eventObj.EventStreamDataTypeId == HackKStudioEventStreamDataTypeIds.UncompressedColorMonitor) || (eventObj.EventStreamDataTypeId == KStudioEventStreamDataTypeIds.CompressedColor) || (eventObj.EventStreamDataTypeId == HackKStudioEventStreamDataTypeIds.CompressedColorMonitor)) { lock (this.lockObj) { data.needsUpdate = true; HGlobalBuffer newSharedFrame = eventObj.GetRetainableEventDataBuffer(); data.visibleChanged = (newSharedFrame == null) != (data.sharedColorFrame == null); data.sharedColorFrame = newSharedFrame; data.doDecompress = (eventObj.EventStreamDataTypeId == KStudioEventStreamDataTypeIds.CompressedColor) || (eventObj.EventStreamDataTypeId == HackKStudioEventStreamDataTypeIds.CompressedColorMonitor); uint newWidth = nui.Constants.STREAM_COLOR_WIDTH; uint newHeight = nui.Constants.STREAM_COLOR_HEIGHT; if ((newWidth != data.imageWidth) || (newHeight != data.imageHeight)) { data.imageWidth = newWidth; data.imageHeight = newHeight; viz.Context context = null; if (this.pluginService != null) { context = this.pluginService.GetContext(eventType); } ColorPlugin.CreateTextures(data, context); } if (!data.timer.IsEnabled) { data.timer.Start(); } } } } }
private void UpdatePropertyView(double x, double y, uint width, uint height, EventTypePluginData data) { Debug.Assert(data != null); bool doEvent = false; lock (this.lockObj) { if (this.selectedData != data) { doEvent = true; this.selectedData = data; } data.selected2DPixelX = 0; data.selected2DPixelY = 0; data.selected2DPixelColor = Colors.Black; if (width == data.imageWidth) { data.selected2DPixelX = (uint)x; } else { data.selected2DPixelX = (uint)((x / width) * data.imageWidth); } if (height == data.imageHeight) { data.selected2DPixelY = (uint)y; } else { data.selected2DPixelY = (uint)((y / height) * data.imageHeight); } ColorPlugin.UpdateSelectedPixelValue(data); } this.RaisePropertyChanged("Selected2DPixelX"); this.RaisePropertyChanged("Selected2DPixelY"); this.RaisePropertyChanged("Selected2DPixelColor"); if (doEvent) { this.RaisePropertyChanged("HasSelected2DPixelData"); } }
private viz.Texture UpdateData(EventTypePluginData data) { Debug.Assert(data != null); viz.Texture value = null; bool doDataEvent = false; bool doVisibleEvent = false; lock (this.lockObj) { bool updated = false; value = data.colorTexture; bool isSelectedData = (this.selectedData == data); data.visibleChanged = false; if (data.needsUpdate && (data.sharedColorFrame != null) && (data.sharedColorFrame.Buffer != IntPtr.Zero) && (data.colorTexture != null)) { try { if (data.doDecompress) { DateTime now = DateTime.Now; if (now > this.nextDecompress) { data.timer.Stop(); doVisibleEvent = isSelectedData && (!data.hasValidData || data.visibleChanged); data.needsUpdate = false; data.hasValidData = true; updated = true; if ((data.decompressMemoryStreamBuffer == null) || (data.decompressMemoryStreamBuffer.Length < data.sharedColorFrame.Size)) { data.decompressMemoryStreamBuffer = new byte[data.sharedColorFrame.Size]; } uint convertBufferSize = data.imageWidth * data.imageHeight * 3; if ((data.convertColorBuffer == null) || (data.convertColorBuffer.Length < convertBufferSize)) { data.convertColorBuffer = new byte[convertBufferSize]; } byte[] rgbBuffer = data.convertColorBuffer; if (this.averageDecompressCount > 10) { this.averageDecompressCount = 1; this.stopwatch.Reset(); } else { this.averageDecompressCount++; } this.stopwatch.Start(); Marshal.Copy(data.sharedColorFrame.Buffer, data.decompressMemoryStreamBuffer, 0, (int)data.sharedColorFrame.Size); using (Stream imageStream = new MemoryStream(data.decompressMemoryStreamBuffer)) { // this has to run on the UI thread JpegBitmapDecoder decoder = new JpegBitmapDecoder(imageStream, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default); BitmapSource bitmapSource = decoder.Frames[0]; bitmapSource.CopyPixels(rgbBuffer, (int)(data.imageWidth * 3), 0); // expand bgr to rgba uint imageSize = data.imageWidth * data.imageHeight; for (uint index = 0; index < imageSize; ++index) { uint rgbaIndex = index * 4; uint bgrIndex = index * 3; data.rgbaFrame[rgbaIndex] = rgbBuffer[bgrIndex + 2]; data.rgbaFrame[rgbaIndex + 1] = rgbBuffer[bgrIndex + 1]; data.rgbaFrame[rgbaIndex + 2] = rgbBuffer[bgrIndex]; data.rgbaFrame[rgbaIndex + 3] = 0xFF; } } this.stopwatch.Stop(); // color decompression takes a LONG time (sometimes upwards of 1/10 of second), so throttle it double averageDecompressTime = ((double)this.stopwatch.ElapsedMilliseconds) / this.averageDecompressCount; double sinceLastDecompress = (now - this.lastDecompress).TotalMilliseconds; if (sinceLastDecompress > 1000) { this.nextDecompress = now + TimeSpan.FromMilliseconds(averageDecompressTime * 2); } else { double minTime = Math.Max(sinceLastDecompress, averageDecompressTime * 2); this.nextDecompress = now + TimeSpan.FromMilliseconds(minTime); } this.lastDecompress = now; } } else { data.timer.Stop(); doVisibleEvent = isSelectedData && (!data.hasValidData || data.visibleChanged); data.needsUpdate = false; data.hasValidData = true; updated = true; unsafe { fixed(byte *p = data.rgbaFrame) { GCHandle rgbaFrameHandle = GCHandle.Alloc(data.rgbaFrame, GCHandleType.Pinned); NativeMethods.ConvertYUYVToRGBA( data.sharedColorFrame.Buffer, data.sharedColorFrame.Size, rgbaFrameHandle.AddrOfPinnedObject(), (uint)data.rgbaFrame.Length); } } } } catch (Exception) { // TODO_LOG } } if (updated) { if (ColorPlugin.UpdateSelectedPixelValue(data)) { doDataEvent = isSelectedData; } if ((data.colorTexture != null) && (data.rgbaFrame != null)) { unsafe { fixed(byte *pFrame = data.rgbaFrame) { data.colorTexture.UpdateData(pFrame, (uint)data.rgbaFrame.Length); } } } } } if (doVisibleEvent) { this.RaisePropertyChanged("HasSelected2DPixelData"); } if (doDataEvent) { this.RaisePropertyChanged("Selected2DPixelColor"); } return(value); }