Beispiel #1
0
        protected override void OnDrawFrame()
        {
            if (Size.Width <= 0 || Size.Height <= 0)
            {
                return;
            }

            int width  = (int)Size.Width;
            int height = (int)Size.Height;
            int stride = 4 * (int)Size.Width;

            using var pixelBuffer = new PixelBuffer((uint)width, (uint)height, PixelFormat.BGRA8888);
            var buffer = pixelBuffer.GetBuffer();

            var info = new SKImageInfo(width, height, SKColorType.Bgra8888);

            using (var surface = SKSurface.Create(info, buffer, stride))
            {
                if (surface == null)
                {
                    Invalidate();
                    return;
                }

                // draw using SkiaSharp
                SendPaintSurface(new SKPaintSurfaceEventArgs(surface, info));
                surface.Canvas.Flush();
            }

            using var pixelData = PixelBuffer.Convert(pixelBuffer);
            using var url       = pixelData.GenerateUrl();
            SetImage(url.ToString());
        }
Beispiel #2
0
        public void PixelBufferGetBuffer()
        {
            tlog.Debug(tag, $"PixelBufferGetBuffer START");

            var testingTarget = new PixelBuffer(100, 50, PixelFormat.BGR8888);

            Assert.IsNotNull(testingTarget, "Can't create success object PixelData");
            Assert.IsInstanceOf <PixelBuffer>(testingTarget, "Should be an instance of PixelData type.");

            var result = testingTarget.GetBuffer();

            Assert.IsNotNull(result);

            testingTarget.Dispose();
            tlog.Debug(tag, $"PixelBufferGetBuffer END (OK)");
        }
        public override View Run()
        {
            var view = new View
            {
                Layout = new LinearLayout
                {
                    LinearAlignment   = LinearLayout.Alignment.Center,
                    LinearOrientation = LinearLayout.Orientation.Vertical,
                }
            };

            view.UpdateBackgroundColor(Color.FromHex("#618833"));

            var test1 = new Button
            {
                Text = "PixelBuffer Create"
            };

            view.Add(test1);

            var test2 = new Button
            {
                Text = "NativeImageQueue Create"
            };

            view.Add(test2);

            var label = new Label();

            view.Add(label);


            test1.Clicked += (s, e) =>
            {
                Stopwatch stopwatch = Stopwatch.StartNew();
                for (int i = 0; i < 1000; i++)
                {
                    using var buffer = new PixelBuffer(500, 500, PixelFormat.BGRA8888);
                    buffer.GetBuffer();
                    using var pixelData = PixelBuffer.Convert(buffer);
                    using var url       = pixelData.GenerateUrl();
                }
                stopwatch.Stop();
                label.Text = $"PixelBuffer create time : {stopwatch.ElapsedMilliseconds} ms";
            };

            test2.Clicked += (s, e) =>
            {
                Stopwatch stopwatch = Stopwatch.StartNew();
                for (int i = 0; i < 1000; i++)
                {
                    using var queue = new NativeImageQueue(500, 500, NativeImageQueue.ColorFormat.RGBA8888);
                    int width  = 0;
                    int height = 0;
                    int stride = 0;
                    var buffer = queue.DequeueBuffer(ref width, ref height, ref stride);
                    queue.EnqueueBuffer(buffer);
                }
                stopwatch.Stop();
                label.Text = $"NativeImageQueue create time : {stopwatch.ElapsedMilliseconds} ms";
            };

            return(view);
        }
Beispiel #4
0
        TextureSet CreateTextureSet(RendererParameters textParameters, List <string> embeddedItems)
        {
            EmbeddedItemInfo[] embeddedItemLayout = new EmbeddedItemInfo[0];
            PixelBuffer        pixelBuffer        = TextUtils.Render(textParameters, ref embeddedItemLayout);

            uint dstWidth  = pixelBuffer.GetWidth();
            uint dstHeight = pixelBuffer.GetHeight();

            int index  = 0;
            int length = embeddedItemLayout.Length;

            for (int i = 0; i < length; i++)
            {
                EmbeddedItemInfo itemLayout = embeddedItemLayout[i];
                int width  = (int)itemLayout.Size.Width;
                int height = (int)itemLayout.Size.Height;

                int x = (int)itemLayout.Position.X;
                int y = (int)itemLayout.Position.Y;

                PixelBuffer itemPixelBuffer = ImageLoading.LoadImageFromFile(embeddedItems[index++]);

                if (itemPixelBuffer == null)
                {
                    continue;
                }

                itemPixelBuffer.Resize((ushort)width, (ushort)height);
                itemPixelBuffer.Rotate(itemLayout.Angle);


                width  = (int)itemPixelBuffer.GetWidth();
                height = (int)itemPixelBuffer.GetHeight();

                PixelFormat itemPixelFormat = itemPixelBuffer.GetPixelFormat();

                // Check if the item is out of the buffer.
                if ((x + width < 0) ||
                    (x > dstWidth) ||
                    (y < 0) ||
                    (y - height > dstHeight))
                {
                    // The embedded item is completely out of the buffer.
                    continue;
                }

                // Crop if it exceeds the boundaries of the destination buffer.
                int layoutX   = 0;
                int layoutY   = 0;
                int cropX     = 0;
                int cropY     = 0;
                int newWidth  = width;
                int newHeight = height;

                bool crop = false;

                if (0 > x)
                {
                    newWidth += x;
                    cropX     = Math.Abs(x);
                    crop      = true;
                }
                else
                {
                    layoutX = x;
                }

                if (cropX + newWidth > dstWidth)
                {
                    crop      = true;
                    newWidth -= (int)((cropX + newWidth) - dstWidth);
                }

                layoutY = y;
                if (0 > layoutY)
                {
                    newHeight += layoutY;
                    cropY      = Math.Abs(layoutY);
                    crop       = true;
                }

                if (cropY + newHeight > dstHeight)
                {
                    crop       = true;
                    newHeight -= (int)((cropY + newHeight) - dstHeight);
                }

                int uiCropX     = cropX;
                int uiCropY     = cropY;
                int uiNewWidth  = newWidth;
                int uiNewHeight = newHeight;

                if (crop)
                {
                    itemPixelBuffer.Crop((ushort)uiCropX, (ushort)uiCropY, (ushort)uiNewWidth, (ushort)uiNewHeight);
                }

                // Blend the item pixel buffer with the text's color according its blending mode.
                if (ColorBlendingMode.Multiply == itemLayout.ColorBlendingMode)
                {
                    PixelBuffer buffer = new PixelBuffer((uint)uiNewWidth, (uint)uiNewHeight, itemPixelFormat);

                    IntPtr bufferIntPtr     = buffer.GetBuffer();
                    IntPtr itemBufferIntPtr = itemPixelBuffer.GetBuffer();

                    uint bytesPerPixel = GetBytesPerPixel(itemPixelFormat);
                    uint size          = (uint)(uiNewWidth * uiNewHeight * bytesPerPixel);


                    unsafe {
                        byte *bufferPtr     = (byte *)bufferIntPtr.ToPointer();
                        byte *itemBufferPtr = (byte *)itemBufferIntPtr.ToPointer();

                        for (uint j = 0; j < size; j += bytesPerPixel)
                        {
                            *(bufferPtr + 0) = (byte)((*(itemBufferPtr + 0u)) * textParameters.TextColor.R);
                            *(bufferPtr + 1) = (byte)((*(itemBufferPtr + 1u)) * textParameters.TextColor.G);
                            *(bufferPtr + 2) = (byte)((*(itemBufferPtr + 2u)) * textParameters.TextColor.B);
                            *(bufferPtr + 3) = (byte)((*(itemBufferPtr + 3u)) * textParameters.TextColor.A);

                            itemBufferPtr += bytesPerPixel;
                            bufferPtr     += bytesPerPixel;
                        }
                    }

                    itemPixelBuffer = buffer;
                }

                TextUtils.UpdateBuffer(itemPixelBuffer, pixelBuffer, (uint)layoutX, (uint)layoutY, true);
            }

            PixelData pixelData = PixelBuffer.Convert(pixelBuffer);

            Texture texture = new Texture(TextureType.TEXTURE_2D,
                                          pixelData.GetPixelFormat(),
                                          pixelData.GetWidth(),
                                          pixelData.GetHeight());

            texture.Upload(pixelData);

            TextureSet textureSet = new TextureSet();

            textureSet.SetTexture(0u, texture);

            return(textureSet);
        }