Пример #1
0
		//#region "Update in a BeginInvoke"
		//public void ProcessUpdate(ElementIntentStates elementStates)
		//{
		//    renderTimer.Reset();
		//    renderTimer.Start();
		//    if (!_paused)
		//    {
		//        FastPixel fp = new FastPixel(new Bitmap(_alphaBackground));

		//        fp.Lock();
		//        Color c;
		//        foreach (var channelIntentState in elementStates)
		//        {
		//            var elementId = channelIntentState.Key;
		//            Element element = VixenSystem.Elements.GetElement(elementId);
		//            if (element == null) continue;
		//            ElementNode node = VixenSystem.Elements.GetElementNodeForElement(element);
		//            if (node == null) continue;

		//            foreach (IIntentState<LightingValue> intentState in channelIntentState.Value)
		//            {
		//                c = intentState.GetValue().GetAlphaChannelIntensityAffectedColor();
		//                if (_background != null)
		//                {
		//                    List<PreviewPixel> pixels;
		//                    if (NodeToPixel.TryGetValue(node, out pixels))
		//                    {
		//                        foreach (PreviewPixel pixel in pixels)
		//                        {
		//                            pixel.Draw(fp, c);
		//                        }
		//                    }
		//                }
		//            }
		//        }

		//        fp.Unlock(true);

		//        // First, draw our background image opaque
		//        bufferedGraphics.Graphics.CompositingMode = System.Drawing.Drawing2D.CompositingMode.SourceCopy;
		//        bufferedGraphics.Graphics.DrawImage(fp.Bitmap, 0, 0, fp.Width, fp.Height);

		//        if (!this.Disposing && bufferedGraphics != null)
		//            bufferedGraphics.Render(Graphics.FromHwnd(this.Handle));

		//        fp = null;
		//    }

		//    renderTimer.Stop();
		//    lastRenderUpdateTime = renderTimer.ElapsedMilliseconds;
		//}
		//#endregion

		public void ProcessUpdateParallel( /*Vixen.Preview.PreviewElementIntentStates elementStates*/)
		{
			renderTimer.Reset();
			renderTimer.Start();
			CancellationTokenSource tokenSource = new CancellationTokenSource();
			if (!_paused)
			{
				Bitmap clone = (Bitmap) _alphaBackground.Clone();
				using (FastPixel.FastPixel fp = new FastPixel.FastPixel(clone))
				{
					try
					{
						fp.Lock();

						Vixen.Preview.PreviewElementIntentStates elementStates =
							new Vixen.Preview.PreviewElementIntentStates(VixenSystem.Elements.ToDictionary(x => x, x => x.State));

						elementStates.AsParallel().WithCancellation(tokenSource.Token).ForAll(channelIntentState =>
						{
							Element element = channelIntentState.Key;
							if (element != null)
							{
								ElementNode node = VixenSystem.Elements.GetElementNodeForElement(element);
								if (node != null)
								{
									List<PreviewPixel> pixels;
									if (NodeToPixel.TryGetValue(node, out pixels))
									{
										foreach (PreviewPixel pixel in pixels)
										{
											pixel.Draw(fp, channelIntentState.Value);
										}
									}
								}
							}
						});
						fp.Unlock(true);
						RenderBufferedGraphics(fp);
					}
					catch (Exception)
					{
						tokenSource.Cancel();
					}
				}
			}

			renderTimer.Stop();
			lastRenderUpdateTime = renderTimer.ElapsedMilliseconds;
		}
Пример #2
0
        //#region "Update in a BeginInvoke"
        //public void ProcessUpdate(ElementIntentStates elementStates)
        //{
        //    renderTimer.Reset();
        //    renderTimer.Start();
        //    if (!_paused)
        //    {
        //        FastPixel fp = new FastPixel(new Bitmap(_alphaBackground));
        //        fp.Lock();
        //        Color c;
        //        foreach (var channelIntentState in elementStates)
        //        {
        //            var elementId = channelIntentState.Key;
        //            Element element = VixenSystem.Elements.GetElement(elementId);
        //            if (element == null) continue;
        //            ElementNode node = VixenSystem.Elements.GetElementNodeForElement(element);
        //            if (node == null) continue;
        //            foreach (IIntentState<LightingValue> intentState in channelIntentState.Value)
        //            {
        //                c = intentState.GetValue().GetAlphaChannelIntensityAffectedColor();
        //                if (_background != null)
        //                {
        //                    List<PreviewPixel> pixels;
        //                    if (NodeToPixel.TryGetValue(node, out pixels))
        //                    {
        //                        foreach (PreviewPixel pixel in pixels)
        //                        {
        //                            pixel.Draw(fp, c);
        //                        }
        //                    }
        //                }
        //            }
        //        }
        //        fp.Unlock(true);
        //        // First, draw our background image opaque
        //        bufferedGraphics.Graphics.CompositingMode = System.Drawing.Drawing2D.CompositingMode.SourceCopy;
        //        bufferedGraphics.Graphics.DrawImage(fp.Bitmap, 0, 0, fp.Width, fp.Height);
        //        if (!this.Disposing && bufferedGraphics != null)
        //            bufferedGraphics.Render(Graphics.FromHwnd(this.Handle));
        //        fp = null;
        //    }
        //    renderTimer.Stop();
        //    lastRenderUpdateTime = renderTimer.ElapsedMilliseconds;
        //}
        //#endregion
        public void ProcessUpdateParallel(/*Vixen.Preview.PreviewElementIntentStates elementStates*/)
        {
            renderTimer.Reset();
            renderTimer.Start();
            CancellationTokenSource tokenSource = new CancellationTokenSource();
            if (!_paused)
            {
                Bitmap clone = (Bitmap)_alphaBackground.Clone();
                //BitmapData odata = _alphaBackground.LockBits(new Rectangle(0, 0, _alphaBackground.Width, _alphaBackground.Height), ImageLockMode.ReadWrite, _alphaBackground.PixelFormat);
                //BitmapData cdata = clone.LockBits(new Rectangle(0, 0, clone.Width, clone.Height), ImageLockMode.ReadWrite, clone.PixelFormat);
                //Assert.AreNotEqual(odata.Scan0, cdata.Scan0);
                //using (FastPixel fp = new FastPixel(new Bitmap(_alphaBackground)))
                using (FastPixel.FastPixel fp = new FastPixel.FastPixel(clone))
                {
                    try
                    {
                        fp.Lock();

                        Vixen.Preview.PreviewElementIntentStates elementStates =
                            new Vixen.Preview.PreviewElementIntentStates(VixenSystem.Elements.ToDictionary(x => x, x => x.State));

                        elementStates.AsParallel().WithCancellation(tokenSource.Token).ForAll(channelIntentState =>
                        {
                            //var elementId = channelIntentState.Key;
                            //Element element = VixenSystem.Elements.GetElement(elementId);
                            Element element = channelIntentState.Key;
                            if (element != null)
                            {
                                ElementNode node = VixenSystem.Elements.GetElementNodeForElement(element);
                                if (node != null)
                                {
                                    List<PreviewPixel> pixels;
                                    if (NodeToPixel.TryGetValue(node, out pixels))
                                    {
                                        foreach (PreviewPixel pixel in pixels)
                                        {
                                            pixel.Draw(fp, channelIntentState.Value);
                                        }
                                    }

                                    //foreach (IIntentState<LightingValue> intentState in channelIntentState.Value)
                                    //{
                                    //    Color c = ((IIntentState<LightingValue>)intentState).GetValue().GetAlphaChannelIntensityAffectedColor();
                                    //    if (_background != null)
                                    //    {
                                    //        List<PreviewPixel> pixels;
                                    //        if (NodeToPixel.TryGetValue(node, out pixels))
                                    //        {
                                    //            foreach (PreviewPixel pixel in pixels)
                                    //            {
                                    //                pixel.Draw(fp, c);
                                    //            }
                                    //        }
                                    //    }
                                    //}
                                }
                            }
                        });
                        //Console.WriteLine("2: " + renderTimer.ElapsedMilliseconds);renderTimer.Reset();
                        fp.Unlock(true);
                        RenderBufferedGraphics(fp);
                    }
                    catch (Exception)
                    {
                        tokenSource.Cancel();
                        //Console.WriteLine(e.Message);
                    }
                }
            }

            renderTimer.Stop();
            lastRenderUpdateTime = renderTimer.ElapsedMilliseconds;
        }