Skip to content

Commit

Permalink
Generate CGColorSpace serialization
Browse files Browse the repository at this point in the history
https://bugs.webkit.org/show_bug.cgi?id=268318
rdar://121873837

Reviewed by Brady Eidson.

Start using generated serialization for CGColorSpace instead of hand-rolled serializers.

* Source/WebKit/DerivedSources-input.xcfilelist:
* Source/WebKit/DerivedSources.make:
* Source/WebKit/Shared/cf/ArgumentCodersCF.cpp:
(IPC::ArgumentCoder<CGColorSpaceRef>::encode): Deleted.
(IPC::ArgumentCoder<RetainPtr<CGColorSpaceRef>>::decode): Deleted.
* Source/WebKit/Shared/cf/ArgumentCodersCF.h:
* Source/WebKit/Shared/cf/CFTypes.serialization.in:
* Source/WebKit/Shared/cf/CoreIPCCGColorSpace.h: Added.
(WebKit::CoreIPCCGColorSpace::CoreIPCCGColorSpace):
(WebKit::CoreIPCCGColorSpace::toCF const):
* Source/WebKit/Shared/cf/CoreIPCCGColorSpace.serialization.in: Copied from Source/WebKit/Shared/cf/CFTypes.serialization.in.
* Source/WebKit/WebKit.xcodeproj/project.pbxproj:
* Tools/TestWebKitAPI/Tests/IPC/IPCSerialization.mm:
(TEST):

Canonical link: https://commits.webkit.org/274188@main
  • Loading branch information
Pascoe committed Feb 7, 2024
1 parent f7b9c81 commit de7f3c3
Show file tree
Hide file tree
Showing 9 changed files with 128 additions and 56 deletions.
1 change: 1 addition & 0 deletions Source/WebKit/DerivedSources-input.xcfilelist
Expand Up @@ -441,6 +441,7 @@ $(PROJECT_DIR)/Shared/XR/XRSystem.serialization.in
$(PROJECT_DIR)/Shared/cf/CFTypes.serialization.in
$(PROJECT_DIR)/Shared/cf/CoreIPCBoolean.serialization.in
$(PROJECT_DIR)/Shared/cf/CoreIPCCFArray.serialization.in
$(PROJECT_DIR)/Shared/cf/CoreIPCCGColorSpace.serialization.in
$(PROJECT_DIR)/Shared/cf/CoreIPCNumber.serialization.in
$(PROJECT_DIR)/Shared/cf/CoreIPCSecCertificate.serialization.in
$(PROJECT_DIR)/Shared/cf/CoreIPCSecKeychainItem.serialization.in
Expand Down
1 change: 1 addition & 0 deletions Source/WebKit/DerivedSources.make
Expand Up @@ -683,6 +683,7 @@ SERIALIZATION_DESCRIPTION_FILES = \
Shared/cf/CFTypes.serialization.in \
Shared/cf/CoreIPCBoolean.serialization.in \
Shared/cf/CoreIPCCFArray.serialization.in \
Shared/cf/CoreIPCCGColorSpace.serialization.in \
Shared/cf/CoreIPCNumber.serialization.in \
Shared/cf/CoreIPCSecCertificate.serialization.in \
Shared/cf/CoreIPCSecKeychainItem.serialization.in \
Expand Down
49 changes: 0 additions & 49 deletions Source/WebKit/Shared/cf/ArgumentCodersCF.cpp
Expand Up @@ -408,55 +408,6 @@ std::optional<RetainPtr<CFDictionaryRef>> ArgumentCoder<RetainPtr<CFDictionaryRe
return WTFMove(dictionary);
}

namespace {
using CGColorSpaceSerialization = std::variant<WebCore::ColorSpace, RetainPtr<CFStringRef>, RetainPtr<CFTypeRef>>;
}

template<typename Encoder>
void ArgumentCoder<CGColorSpaceRef>::encode(Encoder& encoder, CGColorSpaceRef cgColorSpace)
{
if (auto colorSpace = colorSpaceForCGColorSpace(cgColorSpace)) {
encoder << CGColorSpaceSerialization { *colorSpace };
return;
}
if (RetainPtr<CFStringRef> name = CGColorSpaceGetName(cgColorSpace)) {
// This is a bit slow, hence we use the above if possible.
encoder << CGColorSpaceSerialization { WTFMove(name) };
return;
}
if (auto propertyList = adoptCF(CGColorSpaceCopyPropertyList(cgColorSpace))) {
encoder << CGColorSpaceSerialization { WTFMove(propertyList) };
return;
}
// FIXME: This should be removed once we can prove only non-null cgColorSpaces.
encoder << CGColorSpaceSerialization { WebCore::ColorSpace::SRGB };
ASSERT_NOT_REACHED(); // NOLINT
}

template void ArgumentCoder<CGColorSpaceRef>::encode<Encoder>(Encoder&, CGColorSpaceRef);
template void ArgumentCoder<CGColorSpaceRef>::encode<StreamConnectionEncoder>(StreamConnectionEncoder&, CGColorSpaceRef);

std::optional<RetainPtr<CGColorSpaceRef>> ArgumentCoder<RetainPtr<CGColorSpaceRef>>::decode(Decoder& decoder)
{
auto colorSpaceFormat = decoder.decode<CGColorSpaceSerialization>();
if (UNLIKELY(!decoder.isValid()))
return std::nullopt;
auto colorSpace = WTF::switchOn(*colorSpaceFormat,
[](WebCore::ColorSpace colorSpace) -> RetainPtr<CGColorSpaceRef> {
return RetainPtr { cachedNullableCGColorSpace(colorSpace) };
},
[](RetainPtr<CFStringRef> name) -> RetainPtr<CGColorSpaceRef> {
return adoptCF(CGColorSpaceCreateWithName(name.get()));
},
[](RetainPtr<CFTypeRef> propertyList) -> RetainPtr<CGColorSpaceRef> {
return adoptCF(CGColorSpaceCreateWithPropertyList(propertyList.get()));
}
);
if (UNLIKELY(!colorSpace))
return std::nullopt;
return colorSpace;
}

#if HAVE(SEC_ACCESS_CONTROL)
template<typename Encoder>
void ArgumentCoder<SecAccessControlRef>::encode(Encoder& encoder, SecAccessControlRef accessControl)
Expand Down
7 changes: 0 additions & 7 deletions Source/WebKit/Shared/cf/ArgumentCodersCF.h
Expand Up @@ -66,13 +66,6 @@ template<> struct ArgumentCoder<RetainPtr<CFDictionaryRef>> : CFRetainPtrArgumen
static std::optional<RetainPtr<CFDictionaryRef>> decode(Decoder&);
};

template<> struct ArgumentCoder<CGColorSpaceRef> {
template<typename Encoder> static void encode(Encoder&, CGColorSpaceRef);
};
template<> struct ArgumentCoder<RetainPtr<CGColorSpaceRef>> : CFRetainPtrArgumentCoder<CGColorSpaceRef> {
static std::optional<RetainPtr<CGColorSpaceRef>> decode(Decoder&);
};

#if HAVE(SEC_ACCESS_CONTROL)
template<> struct ArgumentCoder<SecAccessControlRef> {
template<typename Encoder> static void encode(Encoder&, SecAccessControlRef);
Expand Down
4 changes: 4 additions & 0 deletions Source/WebKit/Shared/cf/CFTypes.serialization.in
Expand Up @@ -59,6 +59,10 @@ additional_forward_declaration: typedef struct CGColor *CGColorRef
[WebKitPlatform, CustomHeader, AdditionalEncoder=StreamConnectionEncoder, FromCFMethod=WebCore::Color::createAndPreserveColorSpace, ToCFMethod=WebCore::cachedCGColor(*result)] CGColorRef wrapped by WebCore::Color {
}

additional_forward_declaration: typedef struct CF_BRIDGED_TYPE(id) CGColorSpace *CGColorSpaceRef
[WebKitPlatform, CustomHeader, AdditionalEncoder=StreamConnectionEncoder] CGColorSpaceRef wrapped by WebKit::CoreIPCCGColorSpace {
}

#endif

#if HAVE(SEC_KEYCHAIN)
Expand Down
82 changes: 82 additions & 0 deletions Source/WebKit/Shared/cf/CoreIPCCGColorSpace.h
@@ -0,0 +1,82 @@
/*
* Copyright (C) 2024 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.
*/

#pragma once

#if PLATFORM(COCOA)

#import <CoreGraphics/CoreGraphics.h>
#import <WebCore/Color.h>
#import <WebCore/ColorSpace.h>
#import <WebCore/ColorSpaceCG.h>
#import <wtf/RetainPtr.h>

using CGColorSpaceSerialization = std::variant<WebCore::ColorSpace, RetainPtr<CFStringRef>, RetainPtr<CFTypeRef>>;

namespace WebKit {
class CoreIPCCGColorSpace {
public:
CoreIPCCGColorSpace(CGColorSpaceRef cgColorSpace)
{
if (auto colorSpace = WebCore::colorSpaceForCGColorSpace(cgColorSpace))
m_cgColorSpace = *colorSpace;
else if (RetainPtr<CFStringRef> name = CGColorSpaceGetName(cgColorSpace))
m_cgColorSpace = WTFMove(name);
else if (auto propertyList = adoptCF(CGColorSpaceCopyPropertyList(cgColorSpace)))
m_cgColorSpace = WTFMove(propertyList);
else
// FIXME: This should be removed once we can prove only non-null cgColorSpaces.
m_cgColorSpace = WebCore::ColorSpace::SRGB;
}

CoreIPCCGColorSpace(CGColorSpaceSerialization data)
: m_cgColorSpace(data)
{
}

RetainPtr<CGColorSpaceRef> toCF() const
{
auto colorSpace = WTF::switchOn(m_cgColorSpace,
[](WebCore::ColorSpace colorSpace) -> RetainPtr<CGColorSpaceRef> {
return RetainPtr { cachedNullableCGColorSpace(colorSpace) };
},
[](RetainPtr<CFStringRef> name) -> RetainPtr<CGColorSpaceRef> {
return adoptCF(CGColorSpaceCreateWithName(name.get()));
},
[](RetainPtr<CFTypeRef> propertyList) -> RetainPtr<CGColorSpaceRef> {
return adoptCF(CGColorSpaceCreateWithPropertyList(propertyList.get()));
}
);
if (UNLIKELY(!colorSpace))
return nullptr;
return colorSpace;
}

CGColorSpaceSerialization m_cgColorSpace;
};

}

#endif
31 changes: 31 additions & 0 deletions Source/WebKit/Shared/cf/CoreIPCCGColorSpace.serialization.in
@@ -0,0 +1,31 @@
# Copyright (C) 2024 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.

#if PLATFORM(COCOA)

webkit_platform_headers: "CoreIPCCGColorSpace.h"

[WebKitPlatform, AdditionalEncoder=StreamConnectionEncoder] class WebKit::CoreIPCCGColorSpace {
CGColorSpaceSerialization m_cgColorSpace;
}

#endif
4 changes: 4 additions & 0 deletions Source/WebKit/WebKit.xcodeproj/project.pbxproj
Expand Up @@ -5635,6 +5635,8 @@
52F4B46727E1197700FFD129 /* VirtualAuthenticatorUtils.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = VirtualAuthenticatorUtils.h; sourceTree = "<group>"; };
52F4B46827E1197700FFD129 /* VirtualAuthenticatorUtils.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = VirtualAuthenticatorUtils.mm; sourceTree = "<group>"; };
52F4B46B27E125A800FFD129 /* VirtualCredential.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = VirtualCredential.h; sourceTree = "<group>"; };
52FB15422B646156000933CC /* CoreIPCCGColorSpace.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = CoreIPCCGColorSpace.h; sourceTree = "<group>"; };
52FB15432B64680B000933CC /* CoreIPCCGColorSpace.serialization.in */ = {isa = PBXFileReference; lastKnownFileType = text; path = CoreIPCCGColorSpace.serialization.in; sourceTree = "<group>"; };
5315876B2076B713004BF9F3 /* NetworkActivityTrackerCocoa.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = NetworkActivityTrackerCocoa.mm; sourceTree = "<group>"; };
5321594F1DBAE6D70054AA3C /* NetworkDataTaskCocoa.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = NetworkDataTaskCocoa.h; sourceTree = "<group>"; };
532159501DBAE6D70054AA3C /* NetworkSessionCocoa.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = NetworkSessionCocoa.h; sourceTree = "<group>"; };
Expand Down Expand Up @@ -9025,6 +9027,8 @@
5C57868D2B6EF92F005D51D5 /* CoreIPCCFArray.h */,
5C57868F2B6EF92F005D51D5 /* CoreIPCCFArray.mm */,
5C57868E2B6EF92F005D51D5 /* CoreIPCCFArray.serialization.in */,
52FB15422B646156000933CC /* CoreIPCCGColorSpace.h */,
52FB15432B64680B000933CC /* CoreIPCCGColorSpace.serialization.in */,
F4EFF36B2AF0267200479AB8 /* CoreIPCNumber.h */,
F4EFF36A2AF0267200479AB8 /* CoreIPCNumber.serialization.in */,
561A54512B61E49E00073A72 /* CoreIPCSecCertificate.h */,
Expand Down
5 changes: 5 additions & 0 deletions Tools/TestWebKitAPI/Tests/IPC/IPCSerialization.mm
Expand Up @@ -678,6 +678,11 @@ static void destroyTempKeychain(SecKeychainRef keychainRef)
auto cgColor = adoptCF(CGColorCreate(sRGBColorSpace.get(), testComponents));
runTestCF({ cgColor.get() });

// CGColorSpace
runTestCF({ sRGBColorSpace.get() });
auto grayColorSpace = adoptCF(CGColorSpaceCreateDeviceGray());
runTestCF({ grayColorSpace.get() });

auto runNumberTest = [&](NSNumber *number) {
ObjCHolderForTesting::ValueType numberVariant;
numberVariant.emplace<RetainPtr<NSNumber>>(number);
Expand Down

0 comments on commit de7f3c3

Please sign in to comment.