NicosiaPaintingContextCairo.cpp [plain text]
#include "config.h"
#include "NicosiaPaintingContextCairo.h"
#if USE(CAIRO)
#include "GraphicsContext.h"
#include "GraphicsContextImplCairo.h"
#include "NicosiaBuffer.h"
#include "NicosiaCairoOperationRecorder.h"
#include "NicosiaPaintingOperationReplayCairo.h"
#include "PlatformContextCairo.h"
#include "RefPtrCairo.h"
#include <cairo.h>
#include <utility>
namespace Nicosia {
PaintingContextCairo::ForPainting::ForPainting(Buffer& buffer)
{
buffer.ref();
m_cairo.surface = adoptRef(cairo_image_surface_create_for_data(buffer.data(),
CAIRO_FORMAT_ARGB32, buffer.size().width(), buffer.size().height(), buffer.stride()));
static cairo_user_data_key_t s_bufferKey;
cairo_surface_set_user_data(m_cairo.surface.get(), &s_bufferKey,
new std::pair<Buffer*, ForPainting*> { &buffer, this },
[](void* data)
{
auto* userData = static_cast<std::pair<Buffer*, ForPainting*>*>(data);
userData->first->deref();
#if !ASSERT_DISABLED
userData->second->m_deletionComplete = true;
#endif
delete userData;
});
m_cairo.context = adoptRef(cairo_create(m_cairo.surface.get()));
m_platformContext = std::make_unique<WebCore::PlatformContextCairo>(m_cairo.context.get());
m_graphicsContext = std::make_unique<WebCore::GraphicsContext>(WebCore::GraphicsContextImplCairo::createFactory(*m_platformContext));
}
PaintingContextCairo::ForPainting::~ForPainting()
{
cairo_surface_flush(m_cairo.surface.get());
m_graphicsContext = nullptr;
m_platformContext = nullptr;
m_cairo.context = nullptr;
m_cairo.surface = nullptr;
ASSERT(m_deletionComplete);
}
WebCore::GraphicsContext& PaintingContextCairo::ForPainting::graphicsContext()
{
return *m_graphicsContext;
}
void PaintingContextCairo::ForPainting::replay(const PaintingOperations& paintingOperations)
{
PaintingOperationReplayCairo operationReplay { *m_platformContext };
for (auto& operation : paintingOperations)
operation->execute(operationReplay);
}
PaintingContextCairo::ForRecording::ForRecording(PaintingOperations& paintingOperations)
{
m_graphicsContext = std::make_unique<WebCore::GraphicsContext>(
[&paintingOperations](WebCore::GraphicsContext& context)
{
return std::make_unique<CairoOperationRecorder>(context, paintingOperations);
});
}
PaintingContextCairo::ForRecording::~ForRecording() = default;
WebCore::GraphicsContext& PaintingContextCairo::ForRecording::graphicsContext()
{
return *m_graphicsContext;
}
void PaintingContextCairo::ForRecording::replay(const PaintingOperations&)
{
ASSERT_NOT_REACHED();
}
}
#endif // USE(CAIRO)