public SkiaSharp.SKSurface CreateSurface(CGRect contentsBounds, nfloat scale, out SkiaSharp.SKImageInfo info)
        {
            contentsBounds.Width  *= scale;
            contentsBounds.Height *= scale;
            // apply a scale
            // get context details
            this.info = info = new SkiaSharp.SKImageInfo((int)contentsBounds.Width, (int)contentsBounds.Height);

            // if there are no pixels, clean up and return
            if (info.Width == 0 || info.Height == 0)
            {
                FreeBitmap();
                this.info = SkiaSharp.SKImageInfo.Empty;
                return(null);
            }

            // if the memory size has changed, then reset the underlying memory
            if (bitmapData?.Length != (nuint)info.BytesSize)
            {
                FreeBitmap();
            }

            // allocate a memory block for the drawing process
            if (bitmapData == null)
            {
                bitmapData   = NSMutableData.FromLength(info.BytesSize);
                dataProvider = new CGDataProvider(bitmapData.MutableBytes, info.BytesSize, Dummy);
                Element.OnSizeChanged((float)contentsBounds.Width, (float)contentsBounds.Height);
            }

            return(SkiaSharp.SKSurface.Create(info, bitmapData.MutableBytes, info.RowBytes));
        }
Esempio n. 2
0
        public void Create(int width, int height, PixelFormat pixelFormat)
        {
            switch (pixelFormat)
            {
            case PixelFormat.Format32bppRgba:
            {
                const int numComponents    = 4;
                const int bitsPerComponent = 8;
                const int bitsPerPixel     = numComponents * bitsPerComponent;
                const int bytesPerPixel    = bitsPerPixel / 8;
                int       bytesPerRow      = bytesPerPixel * width;

                Data = NSMutableData.FromLength(bytesPerRow * height);

                provider = new CGDataProvider(Data.MutableBytes, (int)Data.Length, false);
                cgimage  = new CGImage(width, height, bitsPerComponent, bitsPerPixel, bytesPerRow, CGColorSpace.CreateDeviceRGB(), CGBitmapFlags.ByteOrder32Little | CGBitmapFlags.PremultipliedFirst, provider, null, true, CGColorRenderingIntent.Default);
                Control  = UIImage.FromImage(cgimage, 0f, UIImageOrientation.Up);

                break;
            }

            case PixelFormat.Format32bppRgb:
            {
                const int numComponents    = 4;
                const int bitsPerComponent = 8;
                const int bitsPerPixel     = numComponents * bitsPerComponent;
                const int bytesPerPixel    = bitsPerPixel / 8;
                int       bytesPerRow      = bytesPerPixel * width;
                Data = NSMutableData.FromLength(bytesPerRow * height);
                //Data = new NSMutableData ((uint)(bytesPerRow * height));

                provider = new CGDataProvider(Data.MutableBytes, (int)Data.Length, false);
                cgimage  = new CGImage(width, height, bitsPerComponent, bitsPerPixel, bytesPerRow, CGColorSpace.CreateDeviceRGB(), CGBitmapFlags.ByteOrder32Little | CGBitmapFlags.NoneSkipFirst, provider, null, true, CGColorRenderingIntent.Default);
                Control  = UIImage.FromImage(cgimage, 0f, UIImageOrientation.Up);

                break;
            }

            case PixelFormat.Format24bppRgb:
            {
                const int numComponents    = 3;
                const int bitsPerComponent = 8;
                const int bitsPerPixel     = numComponents * bitsPerComponent;
                const int bytesPerPixel    = bitsPerPixel / 8;
                int       bytesPerRow      = bytesPerPixel * width;
                Data = new NSMutableData((uint)(bytesPerRow * height));

                provider = new CGDataProvider(Data.MutableBytes, (int)Data.Length, false);
                cgimage  = new CGImage(width, height, bitsPerComponent, bitsPerPixel, bytesPerRow, CGColorSpace.CreateDeviceRGB(), CGBitmapFlags.ByteOrder32Little | CGBitmapFlags.PremultipliedFirst, provider, null, true, CGColorRenderingIntent.Default);
                Control  = UIImage.FromImage(cgimage, 0f, UIImageOrientation.Up);
                break;
            }

            default:
                throw new ArgumentOutOfRangeException("pixelFormat", pixelFormat, "Not supported");
            }
        }
Esempio n. 3
0
 public void FromLength()
 {
     Assert.Throws <ArgumentOutOfRangeException> (delegate {
         NSMutableData.FromLength(-1);
     }, "negative");
     using (var empty = NSMutableData.FromLength(0)) {
         Assert.That(empty.Length, Is.EqualTo((nuint)0), "Length");
     }
 }
Esempio n. 4
0
        public SKSurface CreateSurface(CGRect contentsBounds, nfloat scale, out SKImageInfo info)
        {
            // apply a scale
            contentsBounds.Width  *= scale;
            contentsBounds.Height *= scale;

            // get context details
            Info = info = CreateInfo((int)contentsBounds.Width, (int)contentsBounds.Height);

            // if there are no pixels, clean up and return
            if (info.Width == 0 || info.Height == 0)
            {
                Dispose();
                return(null);
            }

            // if the memory size has changed, then reset the underlying memory
            if (bitmapData?.Length != (nuint)info.BytesSize)
            {
                FreeBitmap();
            }

            // allocate a memory block for the drawing process
            if (bitmapData == null)
            {
                bitmapData = NSMutableData.FromLength(info.BytesSize);

                // in case allocation has failed
                if (bitmapData == null)
                {
                    Dispose();
                    info = Info;
                    return(null);
                }

                dataProvider = new CGDataProvider(bitmapData.MutableBytes, info.BytesSize, Dummy);

                void Dummy(IntPtr data)
                {
                    // do nothing as we manage the memory separately
                }
            }

            return(SKSurface.Create(info, bitmapData.MutableBytes, info.RowBytes));
        }