Title: [287821] trunk/Source
Revision
287821
Author
wei...@apple.com
Date
2022-01-09 10:06:02 -0800 (Sun, 09 Jan 2022)

Log Message

Add helpers to access CoreGraphics color spaces more easily in generic contexts
https://bugs.webkit.org/show_bug.cgi?id=235004

Reviewed by Darin Adler.

Source/WebCore:

Add some structs and type trait helpers to allow accessing the CoreGraphics
CGColorSpaceRef associated with a ColorSpace enum value at compile time. This
will be useful for generalizing some of the gradient rendering code optimizations
I have planned.

* platform/graphics/cg/ColorCG.cpp:
(WebCore::Color::createAndLosslesslyConvertToSupportedColorSpace):
* platform/graphics/cg/ColorSpaceCG.cpp:
(WebCore::colorSpaceForCGColorSpace):
* platform/graphics/cg/ColorSpaceCG.h:
(WebCore::CGColorSpaceMapping<ColorSpace::SRGB>::colorSpace):
(WebCore::CGColorSpaceMapping<ColorSpace::A98RGB>::colorSpace):
(WebCore::CGColorSpaceMapping<ColorSpace::DisplayP3>::colorSpace):
(WebCore::CGColorSpaceMapping<ColorSpace::ExtendedA98RGB>::colorSpace):
(WebCore::CGColorSpaceMapping<ColorSpace::ExtendedDisplayP3>::colorSpace):
(WebCore::CGColorSpaceMapping<ColorSpace::ExtendedRec2020>::colorSpace):
(WebCore::CGColorSpaceMapping<ColorSpace::ExtendedLinearSRGB>::colorSpace):
(WebCore::CGColorSpaceMapping<ColorSpace::ExtendedProPhotoRGB>::colorSpace):
(WebCore::CGColorSpaceMapping<ColorSpace::ExtendedSRGB>::colorSpace):
(WebCore::CGColorSpaceMapping<ColorSpace::Rec2020>::colorSpace):
(WebCore::CGColorSpaceMapping<ColorSpace::LinearSRGB>::colorSpace):
(WebCore::CGColorSpaceMapping<ColorSpace::ProPhotoRGB>::colorSpace):
(WebCore::CGColorSpaceMapping<ColorSpace::XYZ_D50>::colorSpace):
(WebCore::CGColorSpaceMappingGetter::colorSpace):
(WebCore::cachedNullableCGColorSpace):

Source/WTF:

* wtf/PlatformHave.h:
Update have macro, HAVE_CORE_GRAPHICS_XYZ_COLOR_SPACE, to have a more specific name,
HAVE_CORE_GRAPHICS_XYZ_D50_COLOR_SPACE, which is really what it is.

Modified Paths

Diff

Modified: trunk/Source/WTF/ChangeLog (287820 => 287821)


--- trunk/Source/WTF/ChangeLog	2022-01-09 15:35:10 UTC (rev 287820)
+++ trunk/Source/WTF/ChangeLog	2022-01-09 18:06:02 UTC (rev 287821)
@@ -1,3 +1,14 @@
+2022-01-09  Sam Weinig  <wei...@apple.com>
+
+        Add helpers to access CoreGraphics color spaces more easily in generic contexts
+        https://bugs.webkit.org/show_bug.cgi?id=235004
+
+        Reviewed by Darin Adler.
+
+        * wtf/PlatformHave.h:
+        Update have macro, HAVE_CORE_GRAPHICS_XYZ_COLOR_SPACE, to have a more specific name,
+        HAVE_CORE_GRAPHICS_XYZ_D50_COLOR_SPACE, which is really what it is.
+
 2022-01-07  Tim Horton  <timothy_hor...@apple.com>
 
         Adopt linkedOnOrAfter() in more places

Modified: trunk/Source/WebCore/ChangeLog (287820 => 287821)


--- trunk/Source/WebCore/ChangeLog	2022-01-09 15:35:10 UTC (rev 287820)
+++ trunk/Source/WebCore/ChangeLog	2022-01-09 18:06:02 UTC (rev 287821)
@@ -1,3 +1,36 @@
+2022-01-09  Sam Weinig  <wei...@apple.com>
+
+        Add helpers to access CoreGraphics color spaces more easily in generic contexts
+        https://bugs.webkit.org/show_bug.cgi?id=235004
+
+        Reviewed by Darin Adler.
+
+        Add some structs and type trait helpers to allow accessing the CoreGraphics
+        CGColorSpaceRef associated with a ColorSpace enum value at compile time. This
+        will be useful for generalizing some of the gradient rendering code optimizations
+        I have planned.
+
+        * platform/graphics/cg/ColorCG.cpp:
+        (WebCore::Color::createAndLosslesslyConvertToSupportedColorSpace):
+        * platform/graphics/cg/ColorSpaceCG.cpp:
+        (WebCore::colorSpaceForCGColorSpace):
+        * platform/graphics/cg/ColorSpaceCG.h:
+        (WebCore::CGColorSpaceMapping<ColorSpace::SRGB>::colorSpace):
+        (WebCore::CGColorSpaceMapping<ColorSpace::A98RGB>::colorSpace):
+        (WebCore::CGColorSpaceMapping<ColorSpace::DisplayP3>::colorSpace):
+        (WebCore::CGColorSpaceMapping<ColorSpace::ExtendedA98RGB>::colorSpace):
+        (WebCore::CGColorSpaceMapping<ColorSpace::ExtendedDisplayP3>::colorSpace):
+        (WebCore::CGColorSpaceMapping<ColorSpace::ExtendedRec2020>::colorSpace):
+        (WebCore::CGColorSpaceMapping<ColorSpace::ExtendedLinearSRGB>::colorSpace):
+        (WebCore::CGColorSpaceMapping<ColorSpace::ExtendedProPhotoRGB>::colorSpace):
+        (WebCore::CGColorSpaceMapping<ColorSpace::ExtendedSRGB>::colorSpace):
+        (WebCore::CGColorSpaceMapping<ColorSpace::Rec2020>::colorSpace):
+        (WebCore::CGColorSpaceMapping<ColorSpace::LinearSRGB>::colorSpace):
+        (WebCore::CGColorSpaceMapping<ColorSpace::ProPhotoRGB>::colorSpace):
+        (WebCore::CGColorSpaceMapping<ColorSpace::XYZ_D50>::colorSpace):
+        (WebCore::CGColorSpaceMappingGetter::colorSpace):
+        (WebCore::cachedNullableCGColorSpace):
+
 2022-01-08  Antoine Quint  <grao...@webkit.org>
 
         [Web Animations] getKeyframes() for a CSS Animation should not use computed style for keyframes

Modified: trunk/Source/WebCore/platform/graphics/cg/ColorCG.cpp (287820 => 287821)


--- trunk/Source/WebCore/platform/graphics/cg/ColorCG.cpp	2022-01-09 15:35:10 UTC (rev 287820)
+++ trunk/Source/WebCore/platform/graphics/cg/ColorCG.cpp	2022-01-09 18:06:02 UTC (rev 287821)
@@ -91,7 +91,7 @@
 Color Color::createAndLosslesslyConvertToSupportedColorSpace(CGColorRef color, OptionSet<Flags> flags)
 {
     auto sourceCGColorSpace = CGColorGetColorSpace(color);
-#if HAVE(CORE_GRAPHICS_XYZ_COLOR_SPACE)
+#if HAVE(CORE_GRAPHICS_XYZ_D50_COLOR_SPACE)
     auto destinationCGColorSpace = xyzD50ColorSpaceRef();
     auto destinationColorSpace = ColorSpace::XYZ_D50;
 #else

Modified: trunk/Source/WebCore/platform/graphics/cg/ColorSpaceCG.cpp (287820 => 287821)


--- trunk/Source/WebCore/platform/graphics/cg/ColorSpaceCG.cpp	2022-01-09 15:35:10 UTC (rev 287820)
+++ trunk/Source/WebCore/platform/graphics/cg/ColorSpaceCG.cpp	2022-01-09 18:06:02 UTC (rev 287821)
@@ -141,7 +141,7 @@
 }
 #endif
 
-#if HAVE(CORE_GRAPHICS_XYZ_COLOR_SPACE)
+#if HAVE(CORE_GRAPHICS_XYZ_D50_COLOR_SPACE)
 CGColorSpaceRef xyzD50ColorSpaceRef()
 {
     return namedColorSpace<kCGColorSpaceGenericXYZ>();
@@ -216,7 +216,7 @@
         return ColorSpace::ProPhotoRGB;
 #endif
 
-#if HAVE(CORE_GRAPHICS_XYZ_COLOR_SPACE)
+#if HAVE(CORE_GRAPHICS_XYZ_D50_COLOR_SPACE)
     if (CGColorSpaceEqualToColorSpace(colorSpace, xyzD50ColorSpaceRef()))
         return ColorSpace::XYZ_D50;
 #endif

Modified: trunk/Source/WebCore/platform/graphics/cg/ColorSpaceCG.h (287820 => 287821)


--- trunk/Source/WebCore/platform/graphics/cg/ColorSpaceCG.h	2022-01-09 15:35:10 UTC (rev 287820)
+++ trunk/Source/WebCore/platform/graphics/cg/ColorSpaceCG.h	2022-01-09 18:06:02 UTC (rev 287821)
@@ -34,160 +34,170 @@
 
 namespace WebCore {
 
+template<ColorSpace> struct CGColorSpaceMapping;
+
 WEBCORE_EXPORT CGColorSpaceRef sRGBColorSpaceRef();
+template<> struct CGColorSpaceMapping<ColorSpace::SRGB> { static CGColorSpaceRef colorSpace() { return sRGBColorSpaceRef(); } };
 
 #if HAVE(CORE_GRAPHICS_ADOBE_RGB_1998_COLOR_SPACE)
 WEBCORE_EXPORT CGColorSpaceRef adobeRGB1998ColorSpaceRef();
+template<> struct CGColorSpaceMapping<ColorSpace::A98RGB> { static CGColorSpaceRef colorSpace() { return adobeRGB1998ColorSpaceRef(); } };
+#else
+template<> struct CGColorSpaceMapping<ColorSpace::A98RGB> { };
 #endif
 
 #if HAVE(CORE_GRAPHICS_DISPLAY_P3_COLOR_SPACE)
 WEBCORE_EXPORT CGColorSpaceRef displayP3ColorSpaceRef();
+template<> struct CGColorSpaceMapping<ColorSpace::DisplayP3> { static CGColorSpaceRef colorSpace() { return displayP3ColorSpaceRef(); } };
+#else
+template<> struct CGColorSpaceMapping<ColorSpace::DisplayP3> { };
 #endif
 
 #if HAVE(CORE_GRAPHICS_EXTENDED_ADOBE_RGB_1998_COLOR_SPACE)
 WEBCORE_EXPORT CGColorSpaceRef extendedAdobeRGB1998ColorSpaceRef();
+template<> struct CGColorSpaceMapping<ColorSpace::ExtendedA98RGB> { static CGColorSpaceRef colorSpace() { return extendedAdobeRGB1998ColorSpaceRef(); } };
+#else
+template<> struct CGColorSpaceMapping<ColorSpace::ExtendedA98RGB> { };
 #endif
 
 #if HAVE(CORE_GRAPHICS_EXTENDED_DISPLAY_P3_COLOR_SPACE)
 WEBCORE_EXPORT CGColorSpaceRef extendedDisplayP3ColorSpaceRef();
+template<> struct CGColorSpaceMapping<ColorSpace::ExtendedDisplayP3> { static CGColorSpaceRef colorSpace() { return extendedDisplayP3ColorSpaceRef(); } };
+#else
+template<> struct CGColorSpaceMapping<ColorSpace::ExtendedDisplayP3> { };
 #endif
 
 #if HAVE(CORE_GRAPHICS_EXTENDED_ITUR_2020_COLOR_SPACE)
 WEBCORE_EXPORT CGColorSpaceRef extendedITUR_2020ColorSpaceRef();
+template<> struct CGColorSpaceMapping<ColorSpace::ExtendedRec2020> { static CGColorSpaceRef colorSpace() { return extendedITUR_2020ColorSpaceRef(); } };
+#else
+template<> struct CGColorSpaceMapping<ColorSpace::ExtendedRec2020> { };
 #endif
 
 #if HAVE(CORE_GRAPHICS_EXTENDED_LINEAR_SRGB_COLOR_SPACE)
 WEBCORE_EXPORT CGColorSpaceRef extendedLinearSRGBColorSpaceRef();
+template<> struct CGColorSpaceMapping<ColorSpace::ExtendedLinearSRGB> { static CGColorSpaceRef colorSpace() { return extendedLinearSRGBColorSpaceRef(); } };
+#else
+template<> struct CGColorSpaceMapping<ColorSpace::ExtendedLinearSRGB> { };
 #endif
 
 #if HAVE(CORE_GRAPHICS_EXTENDED_ROMMRGB_COLOR_SPACE)
 WEBCORE_EXPORT CGColorSpaceRef extendedROMMRGBColorSpaceRef();
+template<> struct CGColorSpaceMapping<ColorSpace::ExtendedProPhotoRGB> { static CGColorSpaceRef colorSpace() { return extendedROMMRGBColorSpaceRef(); } };
+#else
+template<> struct CGColorSpaceMapping<ColorSpace::ExtendedProPhotoRGB> { };
 #endif
 
 #if HAVE(CORE_GRAPHICS_EXTENDED_SRGB_COLOR_SPACE)
 WEBCORE_EXPORT CGColorSpaceRef extendedSRGBColorSpaceRef();
+template<> struct CGColorSpaceMapping<ColorSpace::ExtendedSRGB> { static CGColorSpaceRef colorSpace() { return extendedSRGBColorSpaceRef(); } };
+#else
+template<> struct CGColorSpaceMapping<ColorSpace::ExtendedSRGB> { };
 #endif
 
 #if HAVE(CORE_GRAPHICS_ITUR_2020_COLOR_SPACE)
 WEBCORE_EXPORT CGColorSpaceRef ITUR_2020ColorSpaceRef();
+template<> struct CGColorSpaceMapping<ColorSpace::Rec2020> { static CGColorSpaceRef colorSpace() { return ITUR_2020ColorSpaceRef(); } };
+#else
+template<> struct CGColorSpaceMapping<ColorSpace::Rec2020> { };
 #endif
 
 #if HAVE(CORE_GRAPHICS_LINEAR_SRGB_COLOR_SPACE)
 WEBCORE_EXPORT CGColorSpaceRef linearSRGBColorSpaceRef();
+template<> struct CGColorSpaceMapping<ColorSpace::LinearSRGB> { static CGColorSpaceRef colorSpace() { return linearSRGBColorSpaceRef(); } };
+#else
+template<> struct CGColorSpaceMapping<ColorSpace::LinearSRGB> { };
 #endif
 
 #if HAVE(CORE_GRAPHICS_ROMMRGB_COLOR_SPACE)
 WEBCORE_EXPORT CGColorSpaceRef ROMMRGBColorSpaceRef();
+template<> struct CGColorSpaceMapping<ColorSpace::ProPhotoRGB> { static CGColorSpaceRef colorSpace() { return ROMMRGBColorSpaceRef(); } };
+#else
+template<> struct CGColorSpaceMapping<ColorSpace::ProPhotoRGB> { };
 #endif
 
-#if HAVE(CORE_GRAPHICS_XYZ_COLOR_SPACE)
+#if HAVE(CORE_GRAPHICS_XYZ_D50_COLOR_SPACE)
 WEBCORE_EXPORT CGColorSpaceRef xyzD50ColorSpaceRef();
+template<> struct CGColorSpaceMapping<ColorSpace::XYZ_D50> { static CGColorSpaceRef colorSpace() { return xyzD50ColorSpaceRef(); } };
+#else
+template<> struct CGColorSpaceMapping<ColorSpace::XYZ_D50> { };
 #endif
 
+// FIXME: Add support for these once/if CoreGraphics adds support for them.
+template<> struct CGColorSpaceMapping<ColorSpace::HSL> { };
+template<> struct CGColorSpaceMapping<ColorSpace::HWB> { };
+template<> struct CGColorSpaceMapping<ColorSpace::LCH> { };
+template<> struct CGColorSpaceMapping<ColorSpace::Lab> { };
+template<> struct CGColorSpaceMapping<ColorSpace::OKLab> { };
+template<> struct CGColorSpaceMapping<ColorSpace::OKLCH> { };
+template<> struct CGColorSpaceMapping<ColorSpace::XYZ_D65> { };
+
+
 std::optional<ColorSpace> colorSpaceForCGColorSpace(CGColorSpaceRef);
 
+
+template<ColorSpace, typename = void> inline constexpr bool HasCGColorSpaceMapping = false;
+template<ColorSpace space> inline constexpr bool HasCGColorSpaceMapping<space, std::void_t<decltype(CGColorSpaceMapping<space>::colorSpace())>> = true;
+static_assert(HasCGColorSpaceMapping<ColorSpace::SRGB>, "An SRGB color space mapping must be supported on all platforms.");
+
+template<ColorSpace space, bool = HasCGColorSpaceMapping<space>> struct CGColorSpaceMappingOrNullGetter { static CGColorSpaceRef colorSpace() { return nullptr; } };
+template<ColorSpace space> struct CGColorSpaceMappingOrNullGetter<space, true> { static CGColorSpaceRef colorSpace() { return CGColorSpaceMapping<space>::colorSpace(); } };
+
+template<ColorSpace space> CGColorSpaceRef cachedCGColorSpace()
+{
+    return CGColorSpaceMapping<space>::colorSpace();
+}
+
+template<ColorSpace space> CGColorSpaceRef cachedNullableCGColorSpace()
+{
+    return CGColorSpaceMappingOrNullGetter<space>::colorSpace();
+}
+
 static inline CGColorSpaceRef cachedNullableCGColorSpace(ColorSpace colorSpace)
 {
     switch (colorSpace) {
-    case ColorSpace::SRGB:
-        return sRGBColorSpaceRef();
-
     case ColorSpace::A98RGB:
-#if HAVE(CORE_GRAPHICS_ADOBE_RGB_1998_COLOR_SPACE)
-        return adobeRGB1998ColorSpaceRef();
-#else
-        return nullptr;
-#endif
-
+        return cachedNullableCGColorSpace<ColorSpace::A98RGB>();
     case ColorSpace::DisplayP3:
-#if HAVE(CORE_GRAPHICS_DISPLAY_P3_COLOR_SPACE)
-        return displayP3ColorSpaceRef();
-#else
-        return nullptr;
-#endif
-
+        return cachedNullableCGColorSpace<ColorSpace::DisplayP3>();
     case ColorSpace::ExtendedA98RGB:
-#if HAVE(CORE_GRAPHICS_EXTENDED_ADOBE_RGB_1998_COLOR_SPACE)
-        return extendedAdobeRGB1998ColorSpaceRef();
-#else
-        return nullptr;
-#endif
-
+        return cachedNullableCGColorSpace<ColorSpace::ExtendedA98RGB>();
     case ColorSpace::ExtendedDisplayP3:
-#if HAVE(CORE_GRAPHICS_EXTENDED_DISPLAY_P3_COLOR_SPACE)
-        return extendedDisplayP3ColorSpaceRef();
-#else
-        return nullptr;
-#endif
-
+        return cachedNullableCGColorSpace<ColorSpace::ExtendedDisplayP3>();
     case ColorSpace::ExtendedLinearSRGB:
-#if HAVE(CORE_GRAPHICS_EXTENDED_LINEAR_SRGB_COLOR_SPACE)
-        return extendedLinearSRGBColorSpaceRef();
-#else
-        return nullptr;
-#endif
-
+        return cachedNullableCGColorSpace<ColorSpace::ExtendedLinearSRGB>();
     case ColorSpace::ExtendedProPhotoRGB:
-#if HAVE(CORE_GRAPHICS_EXTENDED_ROMMRGB_COLOR_SPACE)
-        return extendedROMMRGBColorSpaceRef();
-#else
-        return nullptr;
-#endif
-
+        return cachedNullableCGColorSpace<ColorSpace::ExtendedProPhotoRGB>();
     case ColorSpace::ExtendedRec2020:
-#if HAVE(CORE_GRAPHICS_EXTENDED_ITUR_2020_COLOR_SPACE)
-        return extendedITUR_2020ColorSpaceRef();
-#else
-        return nullptr;
-#endif
-
+        return cachedNullableCGColorSpace<ColorSpace::ExtendedRec2020>();
     case ColorSpace::ExtendedSRGB:
-#if HAVE(CORE_GRAPHICS_EXTENDED_SRGB_COLOR_SPACE)
-        return extendedSRGBColorSpaceRef();
-#else
-        return nullptr;
-#endif
-
-    case ColorSpace::LinearSRGB:
-#if HAVE(CORE_GRAPHICS_LINEAR_SRGB_COLOR_SPACE)
-        return linearSRGBColorSpaceRef();
-#else
-        return nullptr;
-#endif
-
-    case ColorSpace::ProPhotoRGB:
-#if HAVE(CORE_GRAPHICS_ROMMRGB_COLOR_SPACE)
-        return ROMMRGBColorSpaceRef();
-#else
-        return nullptr;
-#endif
-
-    case ColorSpace::Rec2020:
-#if HAVE(CORE_GRAPHICS_ITUR_2020_COLOR_SPACE)
-        return ITUR_2020ColorSpaceRef();
-#else
-        return nullptr;
-#endif
-
-    case ColorSpace::XYZ_D50:
-#if HAVE(CORE_GRAPHICS_XYZ_COLOR_SPACE)
-        return xyzD50ColorSpaceRef();
-#else
-        return nullptr;
-#endif
-
-    // FIXME: Add support for these once/if CoreGraphics adds support for it.
+        return cachedNullableCGColorSpace<ColorSpace::ExtendedSRGB>();
     case ColorSpace::HSL:
+        return cachedNullableCGColorSpace<ColorSpace::HSL>();
     case ColorSpace::HWB:
+        return cachedNullableCGColorSpace<ColorSpace::HWB>();
     case ColorSpace::LCH:
+        return cachedNullableCGColorSpace<ColorSpace::LCH>();
     case ColorSpace::Lab:
+        return cachedNullableCGColorSpace<ColorSpace::Lab>();
+    case ColorSpace::LinearSRGB:
+        return cachedNullableCGColorSpace<ColorSpace::LinearSRGB>();
     case ColorSpace::OKLCH:
+        return cachedNullableCGColorSpace<ColorSpace::OKLCH>();
     case ColorSpace::OKLab:
+        return cachedNullableCGColorSpace<ColorSpace::OKLab>();
+    case ColorSpace::ProPhotoRGB:
+        return cachedNullableCGColorSpace<ColorSpace::ProPhotoRGB>();
+    case ColorSpace::Rec2020:
+        return cachedNullableCGColorSpace<ColorSpace::Rec2020>();
+    case ColorSpace::SRGB:
+        return cachedNullableCGColorSpace<ColorSpace::SRGB>();
+    case ColorSpace::XYZ_D50:
+        return cachedNullableCGColorSpace<ColorSpace::XYZ_D50>();
     case ColorSpace::XYZ_D65:
-        return nullptr;
+        return cachedNullableCGColorSpace<ColorSpace::XYZ_D65>();
+    }
 
-
-    }
     ASSERT_NOT_REACHED();
     return nullptr;
 }
_______________________________________________
webkit-changes mailing list
webkit-changes@lists.webkit.org
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to