예제 #1
0
        public unsafe CefRect Draw(CefPaintEventArgs e)
        {
            float         ppd    = OffscreenGraphics.PixelsPerDip;
            VirtualDevice device = this.Device;

            CefRect[] dirtyRects = e.DirtyRects;
            if (dirtyRects.Length == 0)
            {
                return(new CefRect());
            }

            CefRect r           = dirtyRects[0];
            CefRect invalidRect = new CefRect(r.X, r.Y, r.Width, r.Height);

            for (int i = 1; i < dirtyRects.Length; i++)
            {
                invalidRect.Union(dirtyRects[i]);
            }

            if (device != null)
            {
                invalidRect.Scale(device.Scale * ppd / device.DevicePixelRatio);
            }

            if (e.PaintElementType == CefPaintElementType.Popup)
            {
                invalidRect.Offset(_popupBounds.X, _popupBounds.Y);
            }

            if (invalidRect.IsNullSize)
            {
                return(new CefRect());
            }

            lock (_syncRoot)
            {
                int width  = e.Width;
                int height = e.Height;

                if (device != null)
                {
                    if (e.PaintElementType == CefPaintElementType.View)
                    {
                        width  = (int)(_bounds.Width * device.Scale * ppd);
                        height = (int)(_bounds.Height * device.Scale * ppd);
                    }
                    else if (e.PaintElementType == CefPaintElementType.Popup)
                    {
                        width  = (int)(e.Width / device.DevicePixelRatio * device.Scale * ppd);
                        height = (int)(e.Height / device.DevicePixelRatio * device.Scale * ppd);
                    }
                }

                PixelBuffer pixelBuffer;
                if (e.PaintElementType == CefPaintElementType.View)
                {
                    if (ViewPixels == null || ViewPixels.Width != width || ViewPixels.Height != height)
                    {
                        if (ViewPixels != null)
                        {
                            ViewPixels.Dispose();
                        }

                        ViewPixels = new PixelBuffer(width, height);
                    }
                    pixelBuffer = ViewPixels;
                }
                else if (e.PaintElementType == CefPaintElementType.Popup)
                {
                    if (PopupPixels == null || PopupPixels.Width != width || PopupPixels.Height != height)
                    {
                        if (PopupPixels != null)
                        {
                            PopupPixels.Dispose();
                        }
                        PopupPixels = new PixelBuffer(width, height);
                    }
                    pixelBuffer = PopupPixels;
                }
                else
                {
                    return(new CefRect());
                }

                if (e.Width == pixelBuffer.Width && e.Height == pixelBuffer.Height)
                {
                    long bufferSize = pixelBuffer.Source.ByteCount;
                    Buffer.MemoryCopy(e.Buffer.ToPointer(), pixelBuffer.Source.GetPixels().ToPointer(), bufferSize, bufferSize);
                }
                else
                {
                    using (var canvas = new SKCanvas(pixelBuffer.Source))
                        using (var paint = new SKPaint())
                        {
                            canvas.Clear();
                            paint.FilterQuality = SKFilterQuality.Medium;
                            using (var source = new SKBitmap())
                            {
                                source.InstallPixels(new SKImageInfo(e.Width, e.Height, SKColorType.Bgra8888, SKAlphaType.Opaque), e.Buffer);
                                canvas.DrawBitmap(source, new SKRect(0, 0, pixelBuffer.Width, pixelBuffer.Height), paint);
                            }
                            canvas.Flush();
                        }
                }
            }

            invalidRect.Inflate(2, 2);
            return(invalidRect);
        }
예제 #2
0
        public CefRect Draw(CefPaintEventArgs e)
        {
            float         ppd    = OffscreenGraphics.PixelsPerDip;
            VirtualDevice device = this.Device;

            CefRect[] dirtyRects = e.DirtyRects;
            if (dirtyRects.Length == 0)
            {
                return(new CefRect());
            }

            CefRect r           = dirtyRects[0];
            CefRect invalidRect = new CefRect(r.X, r.Y, r.Width, r.Height);

            for (int i = 1; i < dirtyRects.Length; i++)
            {
                invalidRect.Union(dirtyRects[i]);
            }

            if (device != null)
            {
                invalidRect.Scale(device.Scale * ppd / device.DevicePixelRatio);
            }

            if (e.PaintElementType == CefPaintElementType.Popup)
            {
                invalidRect.Offset(_popupBounds.X, _popupBounds.Y);
            }

            if (invalidRect.IsNullSize)
            {
                return(new CefRect());
            }

            lock (_syncRoot)
            {
                int width  = e.Width;
                int height = e.Height;

                if (device != null)
                {
                    if (e.PaintElementType == CefPaintElementType.View)
                    {
                        width  = (int)(_bounds.Width * device.Scale * ppd);
                        height = (int)(_bounds.Height * device.Scale * ppd);
                    }
                    else if (e.PaintElementType == CefPaintElementType.Popup)
                    {
                        width  = (int)(e.Width / device.DevicePixelRatio * device.Scale * ppd);
                        height = (int)(e.Height / device.DevicePixelRatio * device.Scale * ppd);
                    }
                }

                PixelBuffer pixelBuffer;
                if (e.PaintElementType == CefPaintElementType.View)
                {
                    if (ViewPixels == null || ViewPixels.Width != width || ViewPixels.Height != height)
                    {
                        if (ViewPixels != null)
                        {
                            ViewPixels.Dispose();
                        }

                        ViewPixels = new PixelBuffer(width, height);
                    }
                    pixelBuffer = ViewPixels;
                }
                else if (e.PaintElementType == CefPaintElementType.Popup)
                {
                    if (PopupPixels == null || PopupPixels.Width != width || PopupPixels.Height != height)
                    {
                        if (PopupPixels != null)
                        {
                            PopupPixels.Dispose();
                        }
                        PopupPixels = new PixelBuffer(width, height);
                    }
                    pixelBuffer = PopupPixels;
                }
                else
                {
                    return(new CefRect());
                }

                using (Graphics g = Graphics.FromImage(pixelBuffer.Source))
                {
                    Color background = this.Background;
                    if (background.A > 0)
                    {
                        using (var brush = new SolidBrush(background))
                        {
                            g.FillRectangle(brush, 0, 0, width, height);
                        }
                    }

                    using (var bitmap = new Bitmap(e.Width, e.Height, e.Width << 2, PixelFormat.Format32bppArgb, e.Buffer))
                    {
                        if (e.Width != width || e.Height != height)
                        {
                            g.CompositingQuality = CompositingQuality.HighSpeed;
                            g.InterpolationMode  = this.InterpolationMode;
                            g.DrawImage(bitmap, 0, 0, width, height);
                        }
                        else
                        {
                            g.DrawImage(bitmap, 0, 0);
                        }
                        g.Flush();
                    }
                }
            }

            invalidRect.Inflate(2, 2);
            return(invalidRect);
        }