Skip to content
Permalink
Browse files
[macOS Monterey and Big Sur] Enable AVIF image decoding using libavif…
… and dav1d

https://bugs.webkit.org/show_bug.cgi?id=246925
rdar://101481679

Reviewed by Said Abou-Hallawa.

We checked in libavif and dav1d sources in 494a012.

This patch hooks up image decoding to use them, on macOS Monterey and Big Sur. It does
so by using the same glue code that non-Apple platforms use: ScalableImageDecoder and
AVIFImageDecoder. This patch includes those files in Sources.txt for all ports, and
refactors ScalableImageDecoder on Cocoa platforms to only support AVIF, as per
https://webkit.slack.com/archives/CU64U6FDW/p1666556573569359?thread_ts=1666555605.109619&cid=CU64U6FDW.

This patch re-uses the same USE(AVIF) flag that the non-Apple ports have been using.
There are currently 2 AVIF flags:
- USE(AVIF) indicates that libavif will be used to decode AVIF images, using the
      ScalableImageDecoder infrastructure. This is enabled on the non-Apple ports,
      and on macOS Monterey and Big Sur.
- HAVE(AVIF) indicates that the platform can decode AVIF images directly. This is enabled
      on macOS Ventura and later (and other Cocoa ports).
These flags are mutually exclusive; no port has both enabled at the same time. The Apple
Windows port has neither of these flags enabled.

* Source/WTF/wtf/PlatformUse.h:
* Source/WebCore/Configurations/WebCore.xcconfig:
* Source/WebCore/PAL/libavif/Configurations/libavif.xcconfig:
* Source/WebCore/Sources.txt:
* Source/WebCore/SourcesCocoa.txt:
* Source/WebCore/WebCore.xcodeproj/project.pbxproj:
* Source/WebCore/platform/graphics/ImageDecoder.cpp:
(WebCore::ImageDecoder::create):
* Source/WebCore/platform/graphics/cg/ImageBackingStoreCG.cpp: Copied from Source/WebCore/platform/image-decoders/avif/AVIFImageDecoder.h.
(WebCore::ImageBackingStore::image const):
* Source/WebCore/platform/graphics/cg/ImageDecoderCG.cpp:
(WebCore::ImageDecoderCG::decodeUTI const):
(WebCore::ImageDecoderCG::decodeUTI):
* Source/WebCore/platform/graphics/cg/ImageDecoderCG.h:
* Source/WebCore/platform/image-decoders/ScalableImageDecoder.cpp:
(WebCore::ScalableImageDecoder::create):
* Source/WebCore/platform/image-decoders/avif/AVIFImageDecoder.h:
* Source/WebCore/platform/image-decoders/avif/AVIFImageReader.cpp:
(WebCore::AVIFImageReader::decodeFrame):

Canonical link: https://commits.webkit.org/255984@main
  • Loading branch information
litherum committed Oct 25, 2022
1 parent b137ee4 commit 5300e66a81df105c2dd2aefe87f42e95a27c027d
Show file tree
Hide file tree
Showing 14 changed files with 180 additions and 49 deletions.
@@ -105,8 +105,9 @@ fast/images/animated-heics-verify.html [ Pass ]
fast/images/heic-as-background-image.html [ Pass ]

[ Ventura+ ] fast/images/avif-heif-container-as-image.html [ Pass ]
[ Ventura+ ] http/tests/images/avif-partial-load-crash.html [ Pass ]
[ Ventura+ ] fast/images/avif-image-decoding.html [ Pass ]
http/tests/images/avif-partial-load-crash.html [ Pass ]
fast/images/avif-image-decoding.html [ Pass ]
[ Monterey BigSur ] fast/images/avif-as-image.html [ Pass ]

# <rdar://problem/5647952> fast/events/mouseout-on-window.html needs mac DRT to issue mouse out events
fast/events/mouseout-on-window.html [ Failure ]
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2006-2020 Apple Inc. All rights reserved.
* Copyright (C) 2006-2022 Apple Inc. All rights reserved.
* Copyright (C) 2007-2009 Torch Mobile, Inc.
* Copyright (C) 2010, 2011 Research In Motion Limited. All rights reserved.
*
@@ -367,3 +367,7 @@
#if PLATFORM(IOS_FAMILY) || (PLATFORM(MAC) && __MAC_OS_X_VERSION_MIN_REQUIRED >= 130000)
#define USE_RUNNINGBOARD 1
#endif

#if (PLATFORM(MAC) && __MAC_OS_X_VERSION_MIN_REQUIRED < 130000)
#define USE_AVIF 1
#endif
@@ -78,7 +78,7 @@ ANGLE_HEADER_SEARCH_PATHS = $(BUILT_PRODUCTS_DIR)$(WK_ALTERNATE_FRAMEWORKS_DIR)$
LIBWEBRTC_HEADER_SEARCH_PATHS = $(BUILT_PRODUCTS_DIR)$(WK_LIBRARY_HEADERS_FOLDER_PATH)/webrtc $(SDKROOT)$(WK_LIBRARY_HEADERS_FOLDER_PATH)/webrtc $(BUILT_PRODUCTS_DIR)$(WK_LIBRARY_HEADERS_FOLDER_PATH)/webrtc/sdk/objc/Framework/Headers $(SDKROOT)$(WK_LIBRARY_HEADERS_FOLDER_PATH)/webrtc/sdk/objc/Framework/Headers;

HEADER_SEARCH_PATHS = PAL ForwardingHeaders /usr/include/libxslt /usr/include/libxml2 "$(BUILT_PRODUCTS_DIR)/DerivedSources/WebCore" "$(BUILT_PRODUCTS_DIR)$(WK_LIBRARY_HEADERS_FOLDER_PATH)" $(WEBKITADDITIONS_HEADER_SEARCH_PATHS) $(ANGLE_HEADER_SEARCH_PATHS) $(LIBWEBRTC_HEADER_SEARCH_PATHS) $(HEADER_SEARCH_PATHS) $(SRCROOT);
SYSTEM_HEADER_SEARCH_PATHS = $(SDK_DIR)$(WK_ALTERNATE_WEBKIT_SDK_PATH)$(WK_LIBRARY_HEADERS_FOLDER_PATH) $(inherited);
SYSTEM_HEADER_SEARCH_PATHS = $(PROJECT_DIR)/PAL/libavif/include $(SDK_DIR)$(WK_ALTERNATE_WEBKIT_SDK_PATH)$(WK_LIBRARY_HEADERS_FOLDER_PATH) $(inherited);
LIBRARY_SEARCH_PATHS = $(SDK_DIR)$(WK_LIBRARY_INSTALL_PATH) $(inherited);

INFOPLIST_FILE = Info.plist;
@@ -25,7 +25,7 @@
#include "../../../../Configurations/Version.xcconfig"
#include "../../../../Configurations/WebKitTargetConditionals.xcconfig"

GCC_PREPROCESSOR_DEFINITIONS = $(DEBUG_DEFINES) $(inherited);
GCC_PREPROCESSOR_DEFINITIONS = $(DEBUG_DEFINES) $(inherited) AVIF_CODEC_DAV1D;

FRAMEWORK_SEARCH_PATHS[sdk=iphone*] = $(FRAMEWORK_SEARCH_PATHS_ios_$(CONFIGURATION));
FRAMEWORK_SEARCH_PATHS_ios_Debug = $(BUILT_PRODUCTS_DIR);
@@ -373,6 +373,7 @@ platform/graphics/cg/GradientRendererCG.cpp
platform/graphics/cg/GraphicsContextCG.cpp
platform/graphics/cg/GraphicsContextGLCG.cpp
platform/graphics/cg/IOSurfacePool.cpp
platform/graphics/cg/ImageBackingStoreCG.cpp
platform/graphics/cg/ImageBufferCGBackend.cpp
platform/graphics/cg/ImageBufferCGBitmapBackend.cpp
platform/graphics/cg/ImageBufferIOSurfaceBackend.cpp
@@ -458,6 +459,10 @@ platform/graphics/mac/SwitchingGPUClient.cpp
platform/graphics/mac/WebKitNSImageExtras.mm
platform/graphics/mac/WebLayer.mm
platform/graphics/opentype/OpenTypeCG.cpp
platform/image-decoders/avif/AVIFImageDecoder.cpp
platform/image-decoders/avif/AVIFImageReader.cpp
platform/image-decoders/ScalableImageDecoder.cpp
platform/image-decoders/ScalableImageDecoderFrame.cpp
platform/ios/ColorIOS.mm
platform/ios/Device.cpp
platform/ios/DeviceMotionClientIOS.mm
@@ -7403,6 +7403,16 @@
1C4D0DD124D9F0DB003D7498 /* GlyphBufferMembers.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = GlyphBufferMembers.h; sourceTree = "<group>"; };
1C4DB02427339E5E007B0AD1 /* ShouldLocalizeAxisNames.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = ShouldLocalizeAxisNames.h; sourceTree = "<group>"; };
1C50C49522C84F2400A6E4BE /* WHLSLStandardLibraryFunctionMap.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = WHLSLStandardLibraryFunctionMap.cpp; sourceTree = "<group>"; };
1C5B71712905D07D00973EF5 /* ScalableImageDecoder.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = ScalableImageDecoder.cpp; sourceTree = "<group>"; };
1C5B717D2905D07D00973EF5 /* ScalableImageDecoder.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = ScalableImageDecoder.h; sourceTree = "<group>"; };
1C5B717F2905D07D00973EF5 /* AVIFImageDecoder.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = AVIFImageDecoder.cpp; sourceTree = "<group>"; };
1C5B71802905D07D00973EF5 /* AVIFImageReader.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = AVIFImageReader.cpp; sourceTree = "<group>"; };
1C5B71812905D07D00973EF5 /* AVIFUniquePtr.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = AVIFUniquePtr.h; sourceTree = "<group>"; };
1C5B71822905D07D00973EF5 /* AVIFImageDecoder.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = AVIFImageDecoder.h; sourceTree = "<group>"; };
1C5B71832905D07D00973EF5 /* AVIFImageReader.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = AVIFImageReader.h; sourceTree = "<group>"; };
1C5B718F2905D07D00973EF5 /* ScalableImageDecoderFrame.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = ScalableImageDecoderFrame.cpp; sourceTree = "<group>"; };
1C5B71932905D07D00973EF5 /* ScalableImageDecoderFrame.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = ScalableImageDecoderFrame.h; sourceTree = "<group>"; };
1C5B71A72905E53000973EF5 /* ImageBackingStoreCG.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = ImageBackingStoreCG.cpp; sourceTree = "<group>"; };
1C5E1DA626F949B900E07AF1 /* FontPaletteValues.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = FontPaletteValues.h; sourceTree = "<group>"; };
1C66260E1C6E7CA600AB527C /* FontFace.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = FontFace.cpp; sourceTree = "<group>"; };
1C66260F1C6E7CA600AB527C /* FontFace.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = FontFace.h; sourceTree = "<group>"; };
@@ -20977,6 +20987,30 @@
path = WebGPU;
sourceTree = "<group>";
};
1C5B71702905D07D00973EF5 /* image-decoders */ = {
isa = PBXGroup;
children = (
1C5B717E2905D07D00973EF5 /* avif */,
1C5B71712905D07D00973EF5 /* ScalableImageDecoder.cpp */,
1C5B717D2905D07D00973EF5 /* ScalableImageDecoder.h */,
1C5B718F2905D07D00973EF5 /* ScalableImageDecoderFrame.cpp */,
1C5B71932905D07D00973EF5 /* ScalableImageDecoderFrame.h */,
);
path = "image-decoders";
sourceTree = "<group>";
};
1C5B717E2905D07D00973EF5 /* avif */ = {
isa = PBXGroup;
children = (
1C5B717F2905D07D00973EF5 /* AVIFImageDecoder.cpp */,
1C5B71822905D07D00973EF5 /* AVIFImageDecoder.h */,
1C5B71802905D07D00973EF5 /* AVIFImageReader.cpp */,
1C5B71832905D07D00973EF5 /* AVIFImageReader.h */,
1C5B71812905D07D00973EF5 /* AVIFUniquePtr.h */,
);
path = avif;
sourceTree = "<group>";
};
1C5FAECA0DCFD8C900D58F78 /* Inspector */ = {
isa = PBXGroup;
children = (
@@ -29637,6 +29671,7 @@
934907E3125BBBC8007F23A0 /* GraphicsContextCG.h */,
6E21C6C11126339900A7BE02 /* GraphicsContextGLCG.cpp */,
A80D67070E9E9DEB00E420F0 /* GraphicsContextPlatformPrivateCG.h */,
1C5B71A72905E53000973EF5 /* ImageBackingStoreCG.cpp */,
72BAC3AC23E1E545008D741C /* ImageBufferCGBackend.cpp */,
72BAC3AD23E1E545008D741C /* ImageBufferCGBackend.h */,
72BAC3A823E1E543008D741C /* ImageBufferCGBitmapBackend.cpp */,
@@ -30893,6 +30928,7 @@
CD94A5CB1F71CB4600F525C5 /* encryptedmedia */,
515BE1871D54F5DB00DD7C68 /* gamepad */,
B2A015910AF6CD53006BCE0E /* graphics */,
1C5B71702905D07D00973EF5 /* image-decoders */,
A59E3C1B11580F340072928E /* ios */,
6582A14809999D6C00BEEB6D /* mac */,
9AC6F02021148F1E00CBDA06 /* mediacapabilities */,
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2017 Apple Inc. All rights reserved.
* Copyright (C) 2017-2022 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
@@ -30,7 +30,9 @@

#if USE(CG)
#include "ImageDecoderCG.h"
#else
#endif

#if !USE(CG) || USE(AVIF)
#include "ScalableImageDecoder.h"
#endif

@@ -104,6 +106,11 @@ RefPtr<ImageDecoder> ImageDecoder::create(FragmentedSharedBuffer& data, const St
#endif

#if USE(CG)
#if USE(AVIF)
// ScalableImageDecoder is used on CG ports for some specific image formats which the platform doesn't support directly.
if (auto imageDecoder = ScalableImageDecoder::create(data, alphaOption, gammaAndColorProfileOption))
return imageDecoder;
#endif
return ImageDecoderCG::create(data, alphaOption, gammaAndColorProfileOption);
#else
return ScalableImageDecoder::create(data, alphaOption, gammaAndColorProfileOption);
@@ -0,0 +1,44 @@
/*
* Copyright (C) 2022 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. ``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
* 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 "ImageBackingStore.h"

namespace WebCore {

PlatformImagePtr ImageBackingStore::image() const
{
static const size_t bytesPerPixel = 4;
static const size_t bitsPerComponent = 8;
size_t width = size().width();
size_t height = size().height();
size_t bytesPerRow = bytesPerPixel * width;

auto colorSpace = adoptCF(CGColorSpaceCreateWithName(kCGColorSpaceSRGB));
auto dataProvider = adoptCF(CGDataProviderCreateWithData(nullptr, m_pixelsPtr, height * bytesPerRow, nullptr));
return adoptCF(CGImageCreate(width, height, bitsPerComponent, bytesPerPixel * 8, bytesPerRow, colorSpace.get(), (m_premultiplyAlpha ? kCGImageAlphaPremultipliedFirst : kCGImageAlphaFirst) | kCGImageByteOrder32Little, dataProvider.get(), nullptr, true, kCGRenderingIntentDefault));
}

} // namespace WebCore
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2016 Apple Inc. All rights reserved.
* Copyright (C) 2016-2022 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
@@ -588,7 +588,12 @@ PlatformImagePtr ImageDecoderCG::createFrameImageAtIndex(size_t index, Subsampli

String ImageDecoderCG::decodeUTI(const SharedBuffer& data) const
{
auto uti = String(CGImageSourceGetType(m_nativeDecoder.get()));
return decodeUTI(m_nativeDecoder.get(), data);
}

String ImageDecoderCG::decodeUTI(CGImageSourceRef imageSource, const SharedBuffer& data)
{
auto uti = String(CGImageSourceGetType(imageSource));
if (uti != "public.heif"_s && uti != "public.heic"_s)
return uti;

@@ -1,5 +1,5 @@
/*
* Copyright (C) 2016 Apple Inc. All rights reserved.
* Copyright (C) 2016-2022 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
@@ -72,6 +72,8 @@ class ImageDecoderCG final : public ImageDecoder {
bool isAllDataReceived() const final { return m_isAllDataReceived; }
void clearFrameBufferCache(size_t) final { }

static String decodeUTI(CGImageSourceRef, const SharedBuffer&);

private:
String decodeUTI(const SharedBuffer&) const;

@@ -1,5 +1,5 @@
/*
* Copyright (C) 2017-2020 Apple Inc. All rights reserved.
* Copyright (C) 2017-2022 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
@@ -58,7 +58,7 @@ const MemoryCompactLookupOnlyRobinHoodHashSet<String>& defaultSupportedImageType
"com.google.webp"_s,
"org.webmproject.webp"_s,
#endif
#if HAVE(AVIF)
#if HAVE(AVIF) || USE(AVIF)
"public.avif"_s,
"public.avis"_s,
#endif

0 comments on commit 5300e66

Please sign in to comment.