1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
|
/*
* Copyright (C) 2025 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "config.h"
#if ENABLE(GPU_PROCESS)
#include "RemoteSnapshot.h"
#include "WebImage.h"
#include <wtf/TZoneMallocInlines.h>
namespace WebKit {
using namespace WebCore;
WTF_MAKE_TZONE_ALLOCATED_IMPL(RemoteSnapshot);
Ref<RemoteSnapshot> RemoteSnapshot::create()
{
return adoptRef(*new RemoteSnapshot);
}
RemoteSnapshot::RemoteSnapshot() = default;
RemoteSnapshot::~RemoteSnapshot() = default;
bool RemoteSnapshot::addFrameReference(FrameIdentifier frameIdentifier)
{
Locker locker(m_lock);
m_referencedFrames++;
auto result = m_frameDisplayLists.add(frameIdentifier, std::nullopt);
if (result.isNewEntry)
return true;
// It is ok to setFrame win the race. It is not ok to have two addFrameReferences.
return !result.iterator->value;
}
bool RemoteSnapshot::setFrame(FrameIdentifier frameIdentifier, Ref<const DisplayList::DisplayList>&& displayList, SerialFunctionDispatcher& releaseDispatcher)
{
Locker locker(m_lock);
m_completedFrames++;
auto iterator = m_frameDisplayLists.find(frameIdentifier);
if (iterator == m_frameDisplayLists.end()) {
m_frameDisplayLists.add(frameIdentifier, DisplayListAndReleaseDispatcher { WTFMove(displayList), releaseDispatcher });
return true;
}
// It is ok to addFrameReference to win the race. It's not ok to have two setFrames.
if (iterator->value)
return false;
iterator->value = DisplayListAndReleaseDispatcher { WTFMove(displayList), releaseDispatcher };
return true;
}
bool RemoteSnapshot::applyFrame(FrameIdentifier frameIdentifier, GraphicsContext& context) const
{
RefPtr<const DisplayList::DisplayList> displayList;
{
Locker locker(m_lock);
auto iterator = m_frameDisplayLists.find(frameIdentifier);
if (iterator != m_frameDisplayLists.end())
displayList = iterator->value->displayList();
}
if (!displayList)
return false;
context.drawDisplayList(*displayList);
return true;
}
bool RemoteSnapshot::isComplete() const
{
Locker locker(m_lock);
return m_completedFrames == m_referencedFrames; // Duplicates are handled when the values are updated.
}
RemoteSnapshot::DisplayListAndReleaseDispatcher::DisplayListAndReleaseDispatcher(Ref<const WebCore::DisplayList::DisplayList>&& displayList, SerialFunctionDispatcher& dispatcher)
: m_displayList(WTFMove(displayList))
, m_dispatcher(dispatcher)
{
}
RemoteSnapshot::DisplayListAndReleaseDispatcher::~DisplayListAndReleaseDispatcher()
{
if (m_displayList)
m_dispatcher->dispatch([displayList = WTFMove(m_displayList)]() mutable { });
}
#if PLATFORM(COCOA)
std::optional<RefPtr<SharedBuffer>> RemoteSnapshot::drawToPDF(const FloatSize& size, FrameIdentifier rootIdentifier)
{
ASSERT(isComplete());
RefPtr buffer = ImageBuffer::create(size, RenderingMode::PDFDocument, RenderingPurpose::Snapshot, 1, DestinationColorSpace::SRGB(), PixelFormat::BGRA8);
if (!buffer)
return nullptr;
auto& context = buffer->context();
if (!applyFrame(rootIdentifier, context))
return std::nullopt;
return ImageBuffer::sinkIntoPDFDocument(WTFMove(buffer));
}
#endif
std::optional<ShareableBitmap::Handle> RemoteSnapshot::drawToBitmap(const FloatSize& size, FrameIdentifier rootFrameIdentifier)
{
ASSERT(isComplete());
RefPtr image = WebImage::create(size, ImageOption::Shareable, DestinationColorSpace::SRGB());
if (!image)
return std::nullopt;
auto& context = *image->context();
if (!applyFrame(rootFrameIdentifier, context))
return std::nullopt;
return image->createHandle(SharedMemory::Protection::ReadOnly);
}
}
#endif
|