Skip to content

Commit

Permalink
[GTK] Implement WebAutomationSession::platformGetBase64EncodedPNGData…
Browse files Browse the repository at this point in the history
… on GTK4 and Skia

https://bugs.webkit.org/show_bug.cgi?id=271985

Reviewed by Adrian Perez de Castro.

For the variant of platformGetBase64EncodedPNGData that takes a ShareableBitmap
this adds a Skia implementation.

For the variant that takes a ViewSnapshot a GTK4 implementation was added.

* Source/WebKit/PlatformWPE.cmake:
* Source/WebKit/SourcesGTK.txt:
* Source/WebKit/UIProcess/Automation/WebAutomationSession.cpp:
* Source/WebKit/UIProcess/Automation/cairo/WebAutomationSessionCairo.cpp:
(WebKit::WebAutomationSession::platformGetBase64EncodedPNGData):
* Source/WebKit/UIProcess/Automation/gtk/WebAutomationSessionGtk.cpp:
(WebKit::base64EncodedPNGData):
(WebKit::WebAutomationSession::platformGetBase64EncodedPNGData):
* Source/WebKit/UIProcess/Automation/skia/WebAutomationSessionSkia.cpp: Copied from Source/WebKit/UIProcess/Automation/cairo/WebAutomationSessionCairo.cpp.
(WebKit::base64EncodedPNGData):
(WebKit::WebAutomationSession::platformGetBase64EncodedPNGData):

Canonical link: https://commits.webkit.org/276982@main
  • Loading branch information
TingPing committed Apr 3, 2024
1 parent d159d13 commit 6a7c5c1
Show file tree
Hide file tree
Showing 6 changed files with 125 additions and 9 deletions.
4 changes: 2 additions & 2 deletions Source/WebKit/SourcesGTK.txt
Expand Up @@ -229,8 +229,8 @@ UIProcess/API/gtk/WebKitWebViewGtk4.cpp @no-unify
UIProcess/API/soup/HTTPCookieStoreSoup.cpp

UIProcess/Automation/cairo/WebAutomationSessionCairo.cpp

UIProcess/Automation/gtk/WebAutomationSessionGtk.cpp
UIProcess/Automation/gtk/WebAutomationSessionGtk.cpp @no-unify
UIProcess/Automation/skia/WebAutomationSessionSkia.cpp

UIProcess/CoordinatedGraphics/DrawingAreaProxyCoordinatedGraphics.cpp

Expand Down
1 change: 1 addition & 0 deletions Source/WebKit/SourcesWPE.txt
Expand Up @@ -217,6 +217,7 @@ UIProcess/API/wpe/WebKitWebViewWPE.cpp @no-unify
UIProcess/API/wpe/WPEWebView.cpp @no-unify

UIProcess/Automation/libwpe/WebAutomationSessionLibWPE.cpp
UIProcess/Automation/skia/WebAutomationSessionSkia.cpp

UIProcess/CoordinatedGraphics/DrawingAreaProxyCoordinatedGraphics.cpp

Expand Down
4 changes: 2 additions & 2 deletions Source/WebKit/UIProcess/Automation/WebAutomationSession.cpp
Expand Up @@ -2456,7 +2456,7 @@ void WebAutomationSession::didTakeScreenshot(uint64_t callbackID, std::optional<
callback->sendSuccess(base64EncodedData.value());
}

#if !PLATFORM(COCOA) && !USE(CAIRO)
#if !PLATFORM(COCOA) && !USE(CAIRO) && !USE(SKIA)
std::optional<String> WebAutomationSession::platformGetBase64EncodedPNGData(ShareableBitmap::Handle&&)
{
return std::nullopt;
Expand All @@ -2466,7 +2466,7 @@ std::optional<String> WebAutomationSession::platformGetBase64EncodedPNGData(cons
{
return std::nullopt;
}
#endif // !PLATFORM(COCOA) && !USE(CAIRO)
#endif // !PLATFORM(COCOA) && !USE(CAIRO) && !USE(SKIA)

#if !PLATFORM(COCOA)
std::optional<String> WebAutomationSession::platformGenerateLocalFilePathForRemoteFile(const String&, const String&)
Expand Down
Expand Up @@ -29,6 +29,7 @@
#if USE(CAIRO)

#include "ViewSnapshotStore.h"
#include <WebCore/NotImplemented.h>
#include <WebCore/RefPtrCairo.h>
#include <cairo.h>
#include <wtf/text/Base64.h>
Expand Down Expand Up @@ -64,14 +65,13 @@ std::optional<String> WebAutomationSession::platformGetBase64EncodedPNGData(Shar
return base64EncodedPNGData(surface.get());
}

std::optional<String> WebAutomationSession::platformGetBase64EncodedPNGData(const ViewSnapshot& snapshot)
#if !PLATFORM(GTK)
std::optional<String> WebAutomationSession::platformGetBase64EncodedPNGData(const ViewSnapshot&)
{
#if PLATFORM(GTK) && !USE(GTK4)
return base64EncodedPNGData(snapshot.surface());
#else
notImplemented();
return std::nullopt;
#endif
}
#endif

} // namespace WebKit

Expand Down
43 changes: 43 additions & 0 deletions Source/WebKit/UIProcess/Automation/gtk/WebAutomationSessionGtk.cpp
Expand Up @@ -26,12 +26,14 @@
#include "config.h"
#include "WebAutomationSession.h"

#include "ViewSnapshotStore.h"
#include "WebAutomationSessionMacros.h"
#include "WebKitWebViewBaseInternal.h"
#include "WebPageProxy.h"
#include <WebCore/GtkUtilities.h>
#include <WebCore/GtkVersioning.h>
#include <WebCore/Scrollbar.h>
#include <wtf/text/Base64.h>

namespace WebKit {
using namespace WebCore;
Expand Down Expand Up @@ -336,4 +338,45 @@ void WebAutomationSession::platformSimulateWheelInteraction(WebPageProxy& page,
}
#endif // ENABLE(WEBDRIVER_WHEEL_INTERACTIONS)

#if USE(GTK4)
static std::optional<String> base64EncodedPNGData(GdkTexture* texture)
{
if (!texture)
return std::nullopt;

GRefPtr<GBytes> pngBytes = adoptGRef(gdk_texture_save_to_png_bytes(texture));
size_t pngSize;
auto* pngData = static_cast<const uint8_t*>(g_bytes_get_data(pngBytes.get(), &pngSize));

return base64EncodeToString(std::span<const uint8_t>(pngData, pngSize));
}
#else
static std::optional<String> base64EncodedPNGData(cairo_surface_t* surface)
{
if (!surface)
return std::nullopt;

Vector<unsigned char> pngData;
cairo_surface_write_to_png_stream(surface, [](void* userData, const unsigned char* data, unsigned length) -> cairo_status_t {
auto* pngData = static_cast<Vector<unsigned char>*>(userData);
pngData->append(std::span { data, length });
return CAIRO_STATUS_SUCCESS;
}, &pngData);

if (pngData.isEmpty())
return std::nullopt;

return base64EncodeToString(pngData);
}
#endif // USE(GTK4)

std::optional<String> WebAutomationSession::platformGetBase64EncodedPNGData(const ViewSnapshot& snapshot)
{
#if USE(GTK4)
return base64EncodedPNGData(snapshot.texture());
#else
return base64EncodedPNGData(snapshot.surface());
#endif
}

} // namespace WebKit
@@ -0,0 +1,72 @@
/*
* Copyright (C) 2024 Igalia S.L.
*
* 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"
#include "WebAutomationSession.h"

#if USE(SKIA)

#include "ViewSnapshotStore.h"
#include <WebCore/NotImplemented.h>
#include <skia/core/SkData.h>
IGNORE_CLANG_WARNINGS_BEGIN("cast-align")
#include <skia/encode/SkPngEncoder.h>
IGNORE_CLANG_WARNINGS_END
#include <span>
#include <wtf/text/Base64.h>

namespace WebKit {
using namespace WebCore;

static std::optional<String> base64EncodedPNGData(SkImage& image)
{
auto data = SkPngEncoder::Encode(nullptr, &image, { });
if (!data)
return std::nullopt;

return base64EncodeToString(std::span<const uint8_t>(data->bytes(), data->size()));
}

std::optional<String> WebAutomationSession::platformGetBase64EncodedPNGData(ShareableBitmap::Handle&& handle)
{
auto bitmap = ShareableBitmap::create(WTFMove(handle), SharedMemory::Protection::ReadOnly);
if (!bitmap)
return std::nullopt;

auto image = bitmap->createPlatformImage();
return base64EncodedPNGData(*image.get());
}

#if !PLATFORM(GTK)
std::optional<String> WebAutomationSession::platformGetBase64EncodedPNGData(const ViewSnapshot&)
{
notImplemented();
return std::nullopt;
}
#endif

} // namespace WebKit

#endif // USE(SKIA)

0 comments on commit 6a7c5c1

Please sign in to comment.