I need to serialize a PaintRecord and send it from the browser process to a service process over mojo. I found several places in Chrome code that do this by converting it to SkPicture first, but it would make my code more maintainable if I can avoid this. Here's what I tried:
// Browser process
const cc::PaintOpBuffer& buffer = browser_paint_record.buffer();
size_t buffer_size = buffer.bytes_used();
std::unique_ptr<uint8_t, base::AlignedFreeDeleter> memory =
cc::PaintOpWriter::AllocateAlignedBuffer<uint8_t>(buffer_size);
cc::SimpleBufferSerializer serializer(memory.get(), buffer_size,
cc::PaintOp::SerializeOptions());
serializer.Serialize(buffer);
// memory should now contain the paint op buffer.
// Serialize to a memory region to pass via mojo
base::MappedReadOnlyRegion region_mapping =
base::ReadOnlySharedMemoryRegion::Create(buffer_size);
std::memcpy(region_mapping.mapping.memory(), memory.get(), buffer_size);
base::ReadOnlySharedMemoryRegion region = std::move(region_mapping.region);
// Service process
base::ReadOnlySharedMemoryMapping mapping = region.Map();
auto serialized_content = mapping.GetMemoryAsSpan<uint8_t>();
std::vector<uint8_t> scratch_buffer;
cc::PaintOpBuffer::DeserializeOptions de_options{.scratch_buffer =
scratch_buffer};
sk_sp<cc::PaintOpBuffer> out_buffer = cc::PaintOpBuffer::MakeFromMemory(
serialized_content.data(), serialized_content.size(), de_options);
cc::PaintRecord service_paint_record = out_buffer->ReleaseAsRecord();
This code fails at `MakeFromMemory`, where out_buffer returned is null. Can someone let me know what I'm doing wrong?
Best,
Nasser