CustomPaintCanvas.cpp [plain text]
#include "config.h"
#include "CustomPaintCanvas.h"
#if ENABLE(CSS_PAINTING_API)
#include "CanvasRenderingContext.h"
#include "ImageBitmap.h"
#include "PaintRenderingContext2D.h"
namespace WebCore {
Ref<CustomPaintCanvas> CustomPaintCanvas::create(ScriptExecutionContext& context, unsigned width, unsigned height)
{
return adoptRef(*new CustomPaintCanvas(context, width, height));
}
CustomPaintCanvas::CustomPaintCanvas(ScriptExecutionContext& context, unsigned width, unsigned height)
: CanvasBase(IntSize(width, height))
, ContextDestructionObserver(&context)
{
}
CustomPaintCanvas::~CustomPaintCanvas()
{
notifyObserversCanvasDestroyed();
m_context = nullptr; setImageBuffer(nullptr);
}
ExceptionOr<RefPtr<PaintRenderingContext2D>> CustomPaintCanvas::getContext()
{
if (m_context)
return { RefPtr<PaintRenderingContext2D> { &downcast<PaintRenderingContext2D>(*m_context) } };
m_context = PaintRenderingContext2D::create(*this);
downcast<PaintRenderingContext2D>(*m_context).setUsesDisplayListDrawing(true);
return { RefPtr<PaintRenderingContext2D> { &downcast<PaintRenderingContext2D>(*m_context) } };
}
void CustomPaintCanvas::replayDisplayList(GraphicsContext* ctx) const
{
ASSERT(!m_destinationGraphicsContext);
if (!width() || !height())
return;
auto clipBounds = ctx->clipBounds();
auto image = ImageBuffer::createCompatibleBuffer(clipBounds.size(), *ctx);
if (!image)
return;
m_destinationGraphicsContext = &image->context();
m_destinationGraphicsContext->translate(-clipBounds.location());
if (m_context)
m_context->paintRenderingResultsToCanvas();
m_destinationGraphicsContext = nullptr;
ctx->drawImageBuffer(*image, clipBounds);
}
Image* CustomPaintCanvas::copiedImage() const
{
ASSERT(!m_destinationGraphicsContext);
if (!width() || !height())
return nullptr;
m_copiedBuffer = ImageBuffer::create(size(), RenderingMode::Unaccelerated, 1, ColorSpace::SRGB, nullptr);
if (!m_copiedBuffer)
return nullptr;
m_destinationGraphicsContext = &m_copiedBuffer->context();
if (m_context)
m_context->paintRenderingResultsToCanvas();
m_destinationGraphicsContext = nullptr;
m_copiedImage = m_copiedBuffer->copyImage(DontCopyBackingStore, PreserveResolution::Yes);
return m_copiedImage.get();
}
GraphicsContext* CustomPaintCanvas::drawingContext() const
{
return m_destinationGraphicsContext;
}
GraphicsContext* CustomPaintCanvas::existingDrawingContext() const
{
return drawingContext();
}
}
#endif