GraphicsContextGLCairo.cpp [plain text]
#include "config.h"
#include "GraphicsContextGLOpenGL.h"
#if ENABLE(GRAPHICS_CONTEXT_GL) && USE(CAIRO)
#include "CairoUtilities.h"
#include "Image.h"
#include "ImageSource.h"
#include "PlatformContextCairo.h"
#include "RefPtrCairo.h"
#include <cairo.h>
namespace WebCore {
GraphicsContextGLOpenGL::ImageExtractor::~ImageExtractor() = default;
bool GraphicsContextGLOpenGL::ImageExtractor::extractImage(bool premultiplyAlpha, bool ignoreGammaAndColorProfile, bool)
{
if (!m_image)
return false;
AlphaOption alphaOption = premultiplyAlpha ? AlphaOption::Premultiplied : AlphaOption::NotPremultiplied;
GammaAndColorProfileOption gammaAndColorProfileOption = ignoreGammaAndColorProfile ? GammaAndColorProfileOption::Ignored : GammaAndColorProfileOption::Applied;
auto source = ImageSource::create(nullptr, alphaOption, gammaAndColorProfileOption);
m_alphaOp = AlphaOp::DoNothing;
if (m_image->data()) {
source->setData(m_image->data(), true);
if (!source->frameCount())
return false;
m_imageSurface = source->createFrameImageAtIndex(0);
} else {
m_imageSurface = m_image->nativeImageForCurrentFrame();
if (!premultiplyAlpha && m_imageHtmlDomSource != DOMSource::Video)
m_alphaOp = AlphaOp::DoUnmultiply;
if (m_imageSurface && cairo_surface_get_type(m_imageSurface.get()) != CAIRO_SURFACE_TYPE_IMAGE) {
IntSize surfaceSize = cairoSurfaceSize(m_imageSurface.get());
auto tmpSurface = adoptRef(cairo_image_surface_create(CAIRO_FORMAT_ARGB32, surfaceSize.width(), surfaceSize.height()));
copyRectFromOneSurfaceToAnother(m_imageSurface.get(), tmpSurface.get(), IntSize(), IntRect(IntPoint(), surfaceSize), IntSize());
m_imageSurface = WTFMove(tmpSurface);
}
}
if (!m_imageSurface)
return false;
ASSERT(cairo_surface_get_type(m_imageSurface.get()) == CAIRO_SURFACE_TYPE_IMAGE);
IntSize imageSize = cairoSurfaceSize(m_imageSurface.get());
m_imageWidth = imageSize.width();
m_imageHeight = imageSize.height();
if (!m_imageWidth || !m_imageHeight)
return false;
if (cairo_image_surface_get_format(m_imageSurface.get()) != CAIRO_FORMAT_ARGB32)
return false;
unsigned srcUnpackAlignment = 1;
size_t bytesPerRow = cairo_image_surface_get_stride(m_imageSurface.get());
size_t bitsPerPixel = 32;
unsigned padding = bytesPerRow - bitsPerPixel / 8 * m_imageWidth;
if (padding) {
srcUnpackAlignment = padding + 1;
while (bytesPerRow % srcUnpackAlignment)
++srcUnpackAlignment;
}
m_imagePixelData = cairo_image_surface_get_data(m_imageSurface.get());
m_imageSourceFormat = DataFormat::BGRA8;
m_imageSourceUnpackAlignment = srcUnpackAlignment;
return true;
}
void GraphicsContextGLOpenGL::paintToCanvas(const unsigned char* imagePixels, const IntSize& imageSize, const IntSize& canvasSize, GraphicsContext& context)
{
if (!imagePixels || imageSize.isEmpty() || canvasSize.isEmpty())
return;
PlatformContextCairo* platformContext = context.platformContext();
if (!platformContext)
return;
cairo_t* cr = platformContext->cr();
platformContext->save();
cairo_rectangle(cr, 0, 0, canvasSize.width(), canvasSize.height());
cairo_set_operator(cr, CAIRO_OPERATOR_CLEAR);
cairo_paint(cr);
RefPtr<cairo_surface_t> imageSurface = adoptRef(cairo_image_surface_create_for_data(
const_cast<unsigned char*>(imagePixels), CAIRO_FORMAT_ARGB32, imageSize.width(), imageSize.height(), imageSize.width() * 4));
cairo_translate(cr, 0, imageSize.height());
cairo_scale(cr, 1, -1);
cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
cairo_set_source_surface(cr, imageSurface.get(), 0, 0);
cairo_rectangle(cr, 0, 0, canvasSize.width(), -canvasSize.height());
cairo_fill(cr);
platformContext->restore();
}
}
#endif // ENABLE(GRAPHICS_CONTEXT_GL) && USE(CAIRO)