2018-09-10 02:41:13 +02:00
|
|
|
// Copyright © 2018 The CefSharp Authors. All rights reserved.
|
2018-01-18 11:29:12 +10:00
|
|
|
//
|
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
using System.Drawing;
|
|
|
|
|
using System.Drawing.Imaging;
|
|
|
|
|
using System.Runtime.InteropServices;
|
2018-09-17 00:46:21 +02:00
|
|
|
using CefSharp.Structs;
|
2018-01-18 11:29:12 +10:00
|
|
|
|
|
|
|
|
namespace CefSharp.OffScreen
|
|
|
|
|
{
|
2018-12-17 13:41:05 +10:00
|
|
|
/// <summary>
|
|
|
|
|
/// BitmapBuffer contains a byte[] used to store the Bitmap generated from <see cref="IRenderHandler.OnPaint"/>
|
|
|
|
|
/// and associated methods for updating that buffer and creating a <see cref="Bitmap"/> from the actaual Buffer
|
|
|
|
|
/// </summary>
|
2018-08-24 19:13:59 +10:00
|
|
|
public class BitmapBuffer
|
|
|
|
|
{
|
|
|
|
|
private const int BytesPerPixel = 4;
|
|
|
|
|
private const PixelFormat Format = PixelFormat.Format32bppPArgb;
|
|
|
|
|
|
|
|
|
|
private byte[] buffer;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Number of bytes
|
|
|
|
|
/// </summary>
|
|
|
|
|
public int NumberOfBytes { get; private set; }
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Width
|
|
|
|
|
/// </summary>
|
|
|
|
|
public int Width { get; private set; }
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Height
|
|
|
|
|
/// </summary>
|
|
|
|
|
public int Height { get; private set; }
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Dirty Rect - unified region containing th
|
|
|
|
|
/// </summary>
|
|
|
|
|
public Rect DirtyRect { get; private set; }
|
2018-09-10 02:41:13 +02:00
|
|
|
|
2018-12-17 13:41:05 +10:00
|
|
|
/// <summary>
|
|
|
|
|
/// Locking object used to syncronise access to the underlying buffer
|
|
|
|
|
/// </summary>
|
2018-08-24 19:13:59 +10:00
|
|
|
public object BitmapLock { get; private set; }
|
|
|
|
|
|
2018-12-17 13:41:05 +10:00
|
|
|
/// <summary>
|
|
|
|
|
/// Create a new instance of BitmapBuffer
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="bitmapLock">Reference to the bitmapLock, a shared lock object is expected</param>
|
2018-08-24 19:13:59 +10:00
|
|
|
public BitmapBuffer(object bitmapLock)
|
|
|
|
|
{
|
|
|
|
|
BitmapLock = bitmapLock;
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-17 13:41:05 +10:00
|
|
|
/// <summary>
|
|
|
|
|
/// Get the byte[] array that represents the Bitmap
|
|
|
|
|
/// </summary>
|
2018-08-24 19:13:59 +10:00
|
|
|
public byte[] Buffer
|
|
|
|
|
{
|
|
|
|
|
get { return buffer; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//TODO: May need to Pin the buffer in memory using GCHandle.Alloc(this.buffer, GCHandleType.Pinned);
|
|
|
|
|
private void ResizeBuffer(int width, int height)
|
|
|
|
|
{
|
|
|
|
|
if (buffer == null || width != Width || height != Height)
|
|
|
|
|
{
|
|
|
|
|
//No of Pixels (width * height) * BytesPerPixel
|
|
|
|
|
NumberOfBytes = width * height * BytesPerPixel;
|
|
|
|
|
|
|
|
|
|
buffer = new byte[NumberOfBytes];
|
|
|
|
|
|
|
|
|
|
Width = width;
|
|
|
|
|
Height = height;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-17 13:41:05 +10:00
|
|
|
/// <summary>
|
|
|
|
|
/// Copy data from the unmanaged buffer (IntPtr) into our managed buffer.
|
|
|
|
|
/// Locks BitmapLock before performing any update
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="width">width</param>
|
|
|
|
|
/// <param name="height">height</param>
|
|
|
|
|
/// <param name="buffer">pointer to unmanaged buffer (void*)</param>
|
|
|
|
|
/// <param name="dirtyRect">rectangle to be updated</param>
|
2018-08-24 19:13:59 +10:00
|
|
|
public void UpdateBuffer(int width, int height, IntPtr buffer, Rect dirtyRect)
|
|
|
|
|
{
|
|
|
|
|
lock (BitmapLock)
|
|
|
|
|
{
|
|
|
|
|
DirtyRect = dirtyRect;
|
|
|
|
|
ResizeBuffer(width, height);
|
|
|
|
|
Marshal.Copy(buffer, this.buffer, 0, NumberOfBytes);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-17 13:41:05 +10:00
|
|
|
/// <summary>
|
|
|
|
|
/// Creates a new Bitmap given with the current Width/Height and <see cref="Format"/>
|
|
|
|
|
/// then copies the buffer that represents the bitmap.
|
|
|
|
|
/// Locks <see cref="BitmapLock"/> before creating the <see cref="Bitmap"/>
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns>A new bitmap</returns>
|
2018-08-24 19:13:59 +10:00
|
|
|
public Bitmap CreateBitmap()
|
|
|
|
|
{
|
|
|
|
|
lock (BitmapLock)
|
|
|
|
|
{
|
|
|
|
|
if (Width == 0 || Height == 0 || buffer.Length == 0)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var bitmap = new Bitmap(Width, Height, Format);
|
|
|
|
|
|
|
|
|
|
var bitmapData = bitmap.LockBits(new Rectangle(0, 0, Width, Height), ImageLockMode.WriteOnly, Format);
|
|
|
|
|
|
|
|
|
|
Marshal.Copy(Buffer, 0, bitmapData.Scan0, NumberOfBytes);
|
|
|
|
|
|
|
|
|
|
bitmap.UnlockBits(bitmapData);
|
2018-09-10 02:41:13 +02:00
|
|
|
|
2018-08-24 19:13:59 +10:00
|
|
|
return bitmap;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-01-18 11:29:12 +10:00
|
|
|
}
|