Skip to content
Permalink
Browse files
[Cocoa] Support AVIF images for macOS Ventura and iOS 16
https://bugs.webkit.org/show_bug.cgi?id=241904
rdar://95742091

Reviewed by Myles C. Maxfield.

Add the mime type and the UTI of the AVIF to the list of the allowed image
formats. The system frameworks will be used to render the AVIF images on macOS
Ventura and iOS 16. Because of sand-boxing limitations, software decoding has
to be used for AVIF images.

* LayoutTests/platform/mac/TestExpectations:
* Source/WTF/wtf/PlatformHave.h:
* Source/WebCore/loader/cache/CachedResourceRequest.cpp:
(WebCore::acceptHeaderValueForImageResource):
* Source/WebCore/platform/MIMETypeRegistry.cpp:
* Source/WebCore/platform/graphics/cg/ImageDecoderCG.cpp:
(WebCore::createImageSourceOptions):
* Source/WebCore/platform/graphics/cg/UTIRegistry.cpp:
(WebCore::defaultSupportedImageTypes):

Canonical link: https://commits.webkit.org/251850@main
  • Loading branch information
shallawa authored and Said Abou-Hallawa committed Jun 25, 2022
1 parent 1cebed9 commit 59d774a32243ae8ba59de9178f3b029fd28c4477
Show file tree
Hide file tree
Showing 10 changed files with 79 additions and 11 deletions.
@@ -1273,6 +1273,14 @@
#define HAVE_APPLE_PUSH_SERVICE_URL_TOKEN_SUPPORT 1
#endif

#if (PLATFORM(MAC) && __MAC_OS_X_VERSION_MIN_REQUIRED >= 130000) \
|| (PLATFORM(MACCATALYST) && __IPHONE_OS_VERSION_MIN_REQUIRED >= 160000) \
|| (PLATFORM(IOS) && __IPHONE_OS_VERSION_MIN_REQUIRED >= 160000) \
|| (PLATFORM(APPLETV) && __TV_OS_VERSION_MAX_ALLOWED >= 160000) \
|| (PLATFORM(WATCHOS) && __WATCH_OS_VERSION_MIN_REQUIRED >= 90000)
#define HAVE_AVIF 1
#endif

#if !PLATFORM(IOS_FAMILY)
#define HAVE_MEDIA_VOLUME_PER_ELEMENT 1
#endif
@@ -126,17 +126,37 @@ void CachedResourceRequest::setDomainForCachePartition(const String& domain)
m_resourceRequest.setDomainForCachePartition(domain);
}

static inline constexpr ASCIILiteral acceptHeaderValueForImageResource(bool supportsVideoImage)
static constexpr ASCIILiteral acceptHeaderValueForWebPImageResource()
{
#if HAVE(WEBP) || USE(WEBP)
#define WEBP_HEADER_PART "image/webp,"
return "image/webp,"_s;
#else
#define WEBP_HEADER_PART ""
return ""_s;
#endif
}

static constexpr ASCIILiteral acceptHeaderValueForAVIFImageResource()
{
#if HAVE(AVIF) || USE(AVIF)
return "image/avif,"_s;
#else
return ""_s;
#endif
}

static constexpr ASCIILiteral acceptHeaderValueForVideoImageResource(bool supportsVideoImage)
{
if (supportsVideoImage)
return WEBP_HEADER_PART "image/png,image/svg+xml,image/*;q=0.8,video/*;q=0.8,*/*;q=0.5"_s;
return WEBP_HEADER_PART "image/png,image/svg+xml,image/*;q=0.8,*/*;q=0.5"_s;
#undef WEBP_HEADER_PART
return "video/*;q=0.8,"_s;
return ""_s;
}

static String acceptHeaderValueForImageResource()
{
return String(acceptHeaderValueForWebPImageResource())
+ acceptHeaderValueForAVIFImageResource()
+ acceptHeaderValueForVideoImageResource(ImageDecoder::supportsMediaType(ImageDecoder::MediaType::Video))
+ "image/png,image/svg+xml,image/*;q=0.8,*/*;q=0.5"_s;
}

String CachedResourceRequest::acceptHeaderValueFromType(CachedResource::Type type)
@@ -145,7 +165,7 @@ String CachedResourceRequest::acceptHeaderValueFromType(CachedResource::Type typ
case CachedResource::Type::MainResource:
return "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"_s;
case CachedResource::Type::ImageResource:
return acceptHeaderValueForImageResource(ImageDecoder::supportsMediaType(ImageDecoder::MediaType::Video));
return acceptHeaderValueForImageResource();
case CachedResource::Type::CSSStyleSheet:
return "text/css,*/*;q=0.1"_s;
case CachedResource::Type::SVGDocumentResource:
@@ -93,7 +93,7 @@ constexpr ComparableCaseFoldingASCIILiteral supportedImageMIMETypeArray[] = {
#if USE(CG) || ENABLE(APNG)
"image/apng",
#endif
#if USE(AVIF)
#if HAVE(AVIF) || USE(AVIF)
"image/avif",
#endif
"image/bmp",
@@ -133,7 +133,7 @@ constexpr ComparableCaseFoldingASCIILiteral supportedImageMIMETypeArray[] = {
#if PLATFORM(IOS_FAMILY)
"image/vnd.switfview-jpeg",
#endif
#if (USE(CG) && HAVE(WEBP)) || (!USE(CG) && USE(WEBP))
#if HAVE(WEBP) || USE(WEBP)
"image/webp",
#endif
#if PLATFORM(IOS_FAMILY)
@@ -76,7 +76,7 @@ static RetainPtr<CFMutableDictionaryRef> createImageSourceOptions()
CFDictionarySetValue(options.get(), kCGImageSourceUseHardwareAcceleration, kCFBooleanFalse);

#if HAVE(IMAGE_RESTRICTED_DECODING) && USE(APPLE_INTERNAL_SDK)
if (ImageDecoderCG::decodingHEICEnabled())
if (ImageDecoderCG::decodingHEICEnabled() || ImageDecoderCG::decodingAVIFEnabled())
CFDictionarySetValue(options.get(), kCGImageSourceEnableRestrictedDecoding, kCFBooleanTrue);
#endif

@@ -269,6 +269,7 @@ void sharedBufferRelease(void* info)
#endif

bool ImageDecoderCG::s_enableDecodingHEIC = false;
bool ImageDecoderCG::s_enableDecodingAVIF = false;
bool ImageDecoderCG::s_hardwareAcceleratedDecodingDisabled = false;

ImageDecoderCG::ImageDecoderCG(FragmentedSharedBuffer& data, AlphaOption, GammaAndColorProfileOption)
@@ -621,6 +622,16 @@ bool ImageDecoderCG::decodingHEICEnabled()
return s_enableDecodingHEIC;
}

void ImageDecoderCG::enableDecodingAVIF()
{
s_enableDecodingAVIF = true;
}

bool ImageDecoderCG::decodingAVIFEnabled()
{
return s_enableDecodingAVIF;
}

void ImageDecoderCG::disableHardwareAcceleratedDecoding()
{
s_hardwareAcceleratedDecodingDisabled = true;
@@ -73,6 +73,9 @@ class ImageDecoderCG final : public ImageDecoder {
WEBCORE_EXPORT static void enableDecodingHEIC();
static bool decodingHEICEnabled();

WEBCORE_EXPORT static void enableDecodingAVIF();
static bool decodingAVIFEnabled();

WEBCORE_EXPORT static void disableHardwareAcceleratedDecoding();
static bool hardwareAcceleratedDecodingDisabled();

@@ -81,6 +84,7 @@ class ImageDecoderCG final : public ImageDecoder {
mutable EncodedDataStatus m_encodedDataStatus { EncodedDataStatus::Unknown };
RetainPtr<CGImageSourceRef> m_nativeDecoder;
static bool s_enableDecodingHEIC;
static bool s_enableDecodingAVIF;
static bool s_hardwareAcceleratedDecodingDisabled;
};

@@ -57,6 +57,9 @@ const MemoryCompactLookupOnlyRobinHoodHashSet<String>& defaultSupportedImageType
"public.webp"_s,
"com.google.webp"_s,
"org.webmproject.webp"_s,
#endif
#if HAVE(AVIF)
"public.avif"_s,
#endif
};

@@ -164,6 +164,7 @@ void WebProcessCreationParameters::encode(IPC::Encoder& encoder) const
encoder << trustdExtensionHandle;
#endif
encoder << enableDecodingHEIC;
encoder << enableDecodingAVIF;
#endif

#if PLATFORM(IOS_FAMILY)
@@ -455,6 +456,12 @@ bool WebProcessCreationParameters::decode(IPC::Decoder& decoder, WebProcessCreat
if (!enableDecodingHEIC)
return false;
parameters.enableDecodingHEIC = *enableDecodingHEIC;

std::optional<bool> enableDecodingAVIF;
decoder >> enableDecodingAVIF;
if (!enableDecodingAVIF)
return false;
parameters.enableDecodingAVIF = *enableDecodingAVIF;
#endif

#if PLATFORM(IOS_FAMILY)
@@ -205,6 +205,7 @@ struct WebProcessCreationParameters {
SandboxExtension::Handle trustdExtensionHandle;
#endif
bool enableDecodingHEIC { false };
bool enableDecodingAVIF { false };
#endif

#if PLATFORM(IOS_FAMILY)
@@ -402,9 +402,11 @@ static AccessibilityPreferences accessibilityPreferences()
if (auto trustdExtensionHandle = SandboxExtension::createHandleForMachLookup("com.apple.trustd.agent"_s, std::nullopt))
parameters.trustdExtensionHandle = WTFMove(*trustdExtensionHandle);
parameters.enableDecodingHEIC = true;
parameters.enableDecodingAVIF = true;
}
#else
parameters.enableDecodingHEIC = true;
parameters.enableDecodingAVIF = true;
#endif // PLATFORM(MAC)
#endif // HAVE(VIDEO_RESTRICTED_DECODING)

@@ -270,9 +270,21 @@ static void softlinkDataDetectorsFrameworks()
SandboxExtension::consumePermanently(parameters.trustdExtensionHandle);
#endif // PLATFORM(MAC)
#if USE(APPLE_INTERNAL_SDK)
OptionSet<VideoDecoderBehavior> videoDecoderBehavior;

if (parameters.enableDecodingHEIC) {
ImageDecoderCG::enableDecodingHEIC();
setVideoDecoderBehaviors({ VideoDecoderBehavior::AvoidIOSurface, VideoDecoderBehavior::AvoidHardware, VideoDecoderBehavior::EnableHEIC });
videoDecoderBehavior.add(VideoDecoderBehavior::EnableHEIC);
}

if (parameters.enableDecodingAVIF) {
ImageDecoderCG::enableDecodingAVIF();
videoDecoderBehavior.add(VideoDecoderBehavior::EnableAVIF);
}

if (videoDecoderBehavior) {
videoDecoderBehavior.add({ VideoDecoderBehavior::AvoidIOSurface, VideoDecoderBehavior::AvoidHardware });
setVideoDecoderBehaviors(videoDecoderBehavior);
}
#endif
#endif // HAVE(VIDEO_RESTRICTED_DECODING)

0 comments on commit 59d774a

Please sign in to comment.