Skip to content

Commit

Permalink
Port Image to the new IPC serialization format
Browse files Browse the repository at this point in the history
https://bugs.webkit.org/show_bug.cgi?id=268651

Reviewed by Alex Christensen.

* Source/WebCore/platform/graphics/Image.cpp:
(WebCore::Image::create):
(WebCore::Image::toShareableBitmap const):
* Source/WebCore/platform/graphics/Image.h:
* Source/WebKit/Shared/WebCoreArgumentCoders.cpp:
(IPC::ArgumentCoder<Image>::encode): Deleted.
(IPC::ArgumentCoder<Image>::decode): Deleted.
* Source/WebKit/Shared/WebCoreArgumentCoders.h:
* Source/WebKit/Shared/WebCoreArgumentCoders.serialization.in:

Canonical link: https://commits.webkit.org/274044@main
  • Loading branch information
cdumez committed Feb 3, 2024
1 parent edf8486 commit 79244f9
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 36 deletions.
22 changes: 22 additions & 0 deletions Source/WebCore/platform/graphics/Image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
#include "Length.h"
#include "MIMETypeRegistry.h"
#include "SVGImage.h"
#include "ShareableBitmap.h"
#include "SharedBuffer.h"
#include <math.h>
#include <wtf/MainThread.h>
Expand Down Expand Up @@ -111,6 +112,16 @@ RefPtr<Image> Image::create(ImageObserver& observer)
return BitmapImage::create(&observer);
}

std::optional<Ref<Image>> Image::create(RefPtr<ShareableBitmap>&& bitmap)
{
if (!bitmap)
return std::nullopt;
RefPtr image = bitmap->createImage();
if (!image)
return std::nullopt;
return image.releaseNonNull();
}

bool Image::supportsType(const String& type)
{
return MIMETypeRegistry::isSupportedImageMIMEType(type);
Expand Down Expand Up @@ -386,6 +397,17 @@ DestinationColorSpace Image::colorSpace()
return DestinationColorSpace::SRGB();
}

RefPtr<ShareableBitmap> Image::toShareableBitmap() const
{
RefPtr bitmap = ShareableBitmap::create({ IntSize(size()) });
std::unique_ptr graphicsContext = bitmap->createGraphicsContext();
if (!graphicsContext)
return nullptr;

graphicsContext->drawImage(const_cast<Image&>(*this), IntPoint());
return bitmap;
}

void Image::dump(TextStream& ts) const
{
if (isAnimated())
Expand Down
4 changes: 4 additions & 0 deletions Source/WebCore/platform/graphics/Image.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ class FloatPoint;
class FloatSize;
class GraphicsContext;
class FragmentedSharedBuffer;
class ShareableBitmap;
struct Length;

// This class gets notified when an image creates or destroys decoded frames and when it advances animation frames.
Expand All @@ -62,6 +63,7 @@ class Image : public RefCounted<Image>, public CanMakeWeakPtr<Image> {
virtual ~Image();

WEBCORE_EXPORT static RefPtr<Image> create(ImageObserver&);
WEBCORE_EXPORT static std::optional<Ref<Image>> create(RefPtr<ShareableBitmap>&&);
WEBCORE_EXPORT static bool supportsType(const String&);
static bool isPDFResource(const String& mimeType, const URL&);
static bool isPostScriptResource(const String& mimeType, const URL&);
Expand Down Expand Up @@ -159,6 +161,8 @@ class Image : public RefCounted<Image>, public CanMakeWeakPtr<Image> {

virtual void dump(WTF::TextStream&) const;

WEBCORE_EXPORT RefPtr<ShareableBitmap> toShareableBitmap() const;

protected:
WEBCORE_EXPORT Image(ImageObserver* = nullptr);

Expand Down
31 changes: 0 additions & 31 deletions Source/WebKit/Shared/WebCoreArgumentCoders.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -234,37 +234,6 @@ bool ArgumentCoder<Credential>::decode(Decoder& decoder, Credential& credential)
return true;
}

void ArgumentCoder<Image>::encode(Encoder& encoder, const Image& image)
{
RefPtr bitmap = ShareableBitmap::create({ IntSize(image.size()) });
auto graphicsContext = bitmap->createGraphicsContext();
encoder << !!graphicsContext;
if (!graphicsContext)
return;

graphicsContext->drawImage(const_cast<Image&>(image), IntPoint());

encoder << bitmap;
}

std::optional<Ref<Image>> ArgumentCoder<Image>::decode(Decoder& decoder)
{
std::optional<bool> didCreateGraphicsContext;
decoder >> didCreateGraphicsContext;
if (!didCreateGraphicsContext || !*didCreateGraphicsContext)
return std::nullopt;

std::optional<RefPtr<WebCore::ShareableBitmap>> bitmap;
decoder >> bitmap;
if (!bitmap)
return std::nullopt;

RefPtr image = bitmap.value()->createImage();
if (!image)
return std::nullopt;
return image.releaseNonNull();
}

void ArgumentCoder<WebCore::Font>::encode(Encoder& encoder, const WebCore::Font& font)
{
encoder << font.attributes();
Expand Down
5 changes: 0 additions & 5 deletions Source/WebKit/Shared/WebCoreArgumentCoders.h
Original file line number Diff line number Diff line change
Expand Up @@ -140,11 +140,6 @@ template<> struct ArgumentCoder<WebCore::Credential> {
static WARN_UNUSED_RETURN bool decodePlatformData(Decoder&, WebCore::Credential&);
};

template<> struct ArgumentCoder<WebCore::Image> {
static void encode(Encoder&, const WebCore::Image&);
static std::optional<Ref<WebCore::Image>> decode(Decoder&);
};

template<> struct ArgumentCoder<WebCore::Font> {
static void encode(Encoder&, const WebCore::Font&);
static std::optional<Ref<WebCore::Font>> decode(Decoder&);
Expand Down
5 changes: 5 additions & 0 deletions Source/WebKit/Shared/WebCoreArgumentCoders.serialization.in
Original file line number Diff line number Diff line change
Expand Up @@ -7268,6 +7268,11 @@ enum class WebCore::WindowProxyProperty : uint8_t {
PostMessage,
};

header: <WebCore/Image.h>
[RefCounted] class WebCore::Image {
RefPtr<WebCore::ShareableBitmap> toShareableBitmap();
};

struct WebCore::Length {
std::variant<WebCore::Length::AutoData, WebCore::Length::NormalData, WebCore::Length::RelativeData, WebCore::Length::PercentData, WebCore::Length::FixedData, WebCore::Length::IntrinsicData, WebCore::Length::MinIntrinsicData, WebCore::Length::MinContentData, WebCore::Length::MaxContentData, WebCore::Length::FillAvailableData, WebCore::Length::FitContentData, WebCore::Length::ContentData, WebCore::Length::UndefinedData> ipcData()
}
Expand Down

0 comments on commit 79244f9

Please sign in to comment.