Exemple #1
0
        private IBitmap <ColorAlpha8> CreateMaskBitmap()
        {
            IBitmap <ColorAlpha8> dstMask = BitmapAllocator.Alpha8.Allocate(this.Size, AllocationOptions.Default);

            this.OnRender(dstMask);
            IBitmapLock <ColorAlpha8> keepAlive = dstMask.Lock <ColorAlpha8>(BitmapLockOptions.Read);

            return(new SharedBitmap <ColorAlpha8>(keepAlive, keepAlive.Size, keepAlive.Scan0, keepAlive.Stride, 96.0, 96.0));
        }
        private bool TryUpdateDeviceBitmap(int tileColumn, int tileRow)
        {
            IRenderTarget renderTarget = this.canvasView.RenderTarget;

            if (renderTarget == null)
            {
                return(false);
            }
            bool flag = renderTarget.IsSupported(RenderTargetType.Software, null, null, null);

            if (!flag && (!this.IsVisible || !this.IsActive))
            {
                throw new PaintDotNet.InternalErrorException();
            }
            if (!this.isDeviceBitmapCurrent[tileRow][tileColumn])
            {
                using (IBitmap <ColorPbgra32> bitmap = this.tileCache.TryGetTileBufferRef(tileColumn, tileRow))
                {
                    if (bitmap != null)
                    {
                        if (bitmap.IsDisposed)
                        {
                            throw new ObjectDisposedException("tileBufferRef");
                        }
                        if (flag)
                        {
                            DisposableUtil.Free <IDeviceBitmap>(ref this.deviceBitmaps[tileRow][tileColumn]);
                            DisposableUtil.Free <IBitmap <ColorPbgra32> >(ref this.tileBuffers[tileRow][tileColumn]);
                            IBitmapLock   bitmapLock = bitmap.Lock <ColorPbgra32>(BitmapLockOptions.Read);
                            IDeviceBitmap bitmap2    = renderTarget.CreateSharedBitmap(bitmapLock, null);
                            this.deviceBitmaps[tileRow][tileColumn] = bitmap2;
                            this.tileBuffers[tileRow][tileColumn]   = bitmap.CreateRef <ColorPbgra32>();
                        }
                        else
                        {
                            ObjectPoolTicket <IDeviceBitmap> ticket = this.deviceBitmapTickets[tileRow][tileColumn];
                            if (ticket == null)
                            {
                                try
                                {
                                    ticket = this.owner.DeviceBitmapPool.Get(bitmap.Size);
                                }
                                catch (RecreateTargetException)
                                {
                                    return(false);
                                }
                                this.deviceBitmapTickets[tileRow][tileColumn] = ticket;
                                this.deviceBitmaps[tileRow][tileColumn]       = ticket.Value;
                            }
                            ticket.Value.CopyFromBitmap(null, bitmap, null);
                        }
                        this.isDeviceBitmapCurrent[tileRow][tileColumn] = true;
                    }
                }
            }
            return(true);
        }
Exemple #3
0
        private IBitmap CreateBitmap()
        {
            SizeInt32 size = this.Size;
            IBitmap <ColorPbgra32> bitmap = BitmapAllocator.Pbgra32.Allocate(size, AllocationOptions.Default);

            using (IBitmapLock <ColorPbgra32> @lock = bitmap.Lock <ColorPbgra32>(BitmapLockOptions.Write))
            {
                using (System.Drawing.Bitmap bitmap2 = new System.Drawing.Bitmap(size.Width, size.Height, @lock.Stride, System.Drawing.Imaging.PixelFormat.Format32bppPArgb, @lock.Scan0))
                {
                    using (Graphics graphics = Graphics.FromImage(bitmap2))
                    {
                        this.Draw(graphics);
                    }
                }
            }
            return(bitmap);
        }
 private IDeviceBitmap InitializeDeviceBitmap(IDeviceResourceFactory factory, IBitmap bitmap, ref IDeviceBitmap deviceBitmap)
 {
     if (bitmap == null)
     {
         this.ReturnOrFreeDeviceBitmap(ref deviceBitmap);
         return(deviceBitmap);
     }
     if ((bitmap != null) && (deviceBitmap == null))
     {
         if (factory.IsSupported(RenderTargetType.Software, null, null, null))
         {
             SizeInt32   num        = bitmap.Size;
             IBitmapLock bitmapLock = bitmap.Lock(BitmapLockOptions.Read);
             deviceBitmap = factory.CreateSharedBitmap(bitmapLock, new BitmapProperties?(maskBitmapProperties));
             return(deviceBitmap);
         }
         SizeInt32 size = bitmap.Size;
         deviceBitmap = this.GetOrCreateDeviceBitmap(factory, size);
         deviceBitmap.CopyFromBitmap(new PointInt32?(PointInt32.Zero), bitmap, new RectInt32(PointInt32.Zero, size));
     }
     return(deviceBitmap);
 }
 public IDeviceBitmap CreateSharedBitmap(IBitmapLock bitmapLock, BitmapProperties?bitmapProperties) =>
 base.innerRefT.CreateSharedBitmap(bitmapLock, bitmapProperties);
Exemple #6
0
 public BitmapLockProxy(IBitmapLock <TPixel> objectRef, ObjectRefProxyOptions proxyOptions) : base(objectRef, proxyOptions)
 {
 }
        private unsafe void RenderTileWorkItem(PointInt32 tileOffset)
        {
            IBitmap <ColorPbgra32> bitmap;
            bool      isCancelled = false;
            bool      flag        = false;
            Exception error       = null;

            isCancelled |= this.IsTileRenderingCancelled(tileOffset);
            if (isCancelled)
            {
                bitmap = null;
            }
            else
            {
                RectInt32 tileSourceRect = this.tileMathHelper.GetTileSourceRect(tileOffset);
                SizeInt32 tileBufferSize = this.GetTileBufferSize(tileOffset);
                bitmap = RetryManager.Eval <IBitmap <ColorPbgra32> >(3, () => BitmapAllocator.Pbgra32.Allocate(tileBufferSize, AllocationOptions.Default), delegate(Exception _) {
                    CleanupManager.RequestCleanup();
                    Thread.Sleep(200);
                    CleanupManager.WaitForPendingCleanup();
                }, delegate(AggregateException ex) {
                    throw new AggregateException($"could not allocate a bitmap of size {tileBufferSize.Width} x {tileBufferSize.Height}", ex).Flatten();
                });
                if (this.source != null)
                {
                    try
                    {
                        isCancelled |= this.IsTileRenderingCancelled(tileOffset);
                        if (!isCancelled)
                        {
                            using (IBitmapLock <ColorPbgra32> @lock = bitmap.Lock <ColorPbgra32>(BitmapLockOptions.ReadWrite))
                            {
                                if (this.mipLevel == 0)
                                {
                                    this.source.CopyPixels(new RectInt32?(tileSourceRect), @lock);
                                    RenderingKernels.ConvertBgra32ToPbgra32((uint *)@lock.Scan0, tileBufferSize.Width, tileBufferSize.Height, @lock.Stride);
                                    flag = true;
                                }
                                else
                                {
                                    BitmapInterpolationMode linear;
                                    if (!this.isHighQuality)
                                    {
                                        linear = BitmapInterpolationMode.Linear;
                                    }
                                    else if (this.mipLevel == 1)
                                    {
                                        linear = BitmapInterpolationMode.Linear;
                                    }
                                    else
                                    {
                                        linear = BitmapInterpolationMode.Fant;
                                    }
                                    IImagingFactory    instance    = ImagingFactory.Instance;
                                    ICancellationToken cancelToken = CancellationTokenUtil.Create((Func <bool>)(() => (isCancelled | this.IsTileRenderingCancelled(tileOffset))));
                                    int copyHeightLog2             = Math.Max(3, 7 - this.mipLevel);
                                    using (ClippedBitmapSource <ColorBgra32> source2 = new ClippedBitmapSource <ColorBgra32>(this.source, tileSourceRect))
                                    {
                                        using (CancellableBitmapSource <ColorBgra32> source3 = new CancellableBitmapSource <ColorBgra32>(source2, r => this.tileMathHelper.EnumerateTilesClippedToSourceRect(r), null, cancelToken))
                                        {
                                            using (IBitmapSource <ColorPbgra32> source4 = CreateBufferedTileScaler(instance, source3, tileBufferSize.Width, tileBufferSize.Height, linear))
                                            {
                                                using (CancellableBitmapSource <ColorPbgra32> source5 = new CancellableBitmapSource <ColorPbgra32>(source4, r => TileRectSplitter(r, ((int)1) << copyHeightLog2), null, cancelToken))
                                                {
                                                    try
                                                    {
                                                        source5.CopyPixels <ColorPbgra32>(@lock);
                                                        flag = true;
                                                    }
                                                    catch (OperationCanceledException exception2)
                                                    {
                                                        error       = exception2;
                                                        isCancelled = true;
                                                    }
                                                    catch (Exception exception3)
                                                    {
                                                        error = exception3;
                                                        throw;
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                                isCancelled |= this.IsTileRenderingCancelled(tileOffset);
                                if (isCancelled)
                                {
                                    flag = false;
                                }
                            }
                            if (!flag)
                            {
                                DisposableUtil.Free <IBitmap <ColorPbgra32> >(ref bitmap);
                            }
                        }
                    }
                    catch (OperationCanceledException exception4)
                    {
                        error       = exception4;
                        isCancelled = true;
                    }
                    catch (Exception exception5)
                    {
                        error        = exception5;
                        isCancelled |= this.IsTileRenderingCancelled(tileOffset);
                        if (!isCancelled)
                        {
                            using (IDrawingContext context = DrawingContext.FromBitmap(bitmap, FactorySource.PerThread))
                            {
                                context.Clear(new ColorRgba128Float?((ColorRgba128Float)Colors.White));
                                string text = exception5.ToString();
                                using (ISystemFonts fonts = new SystemFonts(true))
                                {
                                    TextLayout textLayout = UIText.CreateLayout(context, text, fonts.Caption, null, HotkeyRenderMode.Ignore, (double)bitmap.Size.Width, 65535.0);
                                    textLayout.FontSize    *= 0.6;
                                    textLayout.WordWrapping = WordWrapping.Wrap;
                                    context.DrawTextLayout(PointDouble.Zero, textLayout, SolidColorBrushCache.Get((ColorRgba128Float)Colors.Black), DrawTextOptions.None);
                                }
                            }
                            flag = true;
                        }
                    }
                }
            }
            isCancelled |= this.IsTileRenderingCancelled(tileOffset);
            if (isCancelled)
            {
                DisposableUtil.Free <IBitmap <ColorPbgra32> >(ref bitmap);
            }
            RenderedTileInfo info = new RenderedTileInfo(bitmap, !isCancelled && (bitmap > null), error);

            if (!this.tilesRenderedQueue.TryEnqueue(tileOffset, info))
            {
                ExceptionUtil.ThrowInternalErrorException("Could not enqueue to this.tilesRenderedQueue");
            }
            if (Interlocked.Exchange(ref this.isProcessTileRenderedQueueQueued, 1) == 0)
            {
                this.syncContext.Post(this.processTileRenderedQueueCallback);
            }
        }
Exemple #8
0
 public static IBitmapLock <TPixel> CreateRef <TPixel>(this IBitmapLock <TPixel> objectRef) where TPixel : struct, INaturalPixelInfo =>
 ((IBitmapLock <TPixel>)objectRef.CreateRef(typeof(IBitmapLock <TPixel>)));
Exemple #9
0
 public static IBitmapLock CreateRef(this IBitmapLock objectRef) =>
 ((IBitmapLock)objectRef.CreateRef(typeof(IBitmapLock)));