Bug 1707590 - Part 2: Implement nsJXLDecoder r=tnikkel

Differential Revision: https://phabricator.services.mozilla.com/D113359
This commit is contained in:
Kagami Sascha Rosylight 2021-05-04 15:24:54 +00:00
Родитель 4cf43df159
Коммит b28b7b020a
26 изменённых файлов: 325 добавлений и 5 удалений

Просмотреть файл

@ -23,6 +23,9 @@
#ifdef MOZ_AV1
# include "nsAVIFDecoder.h"
#endif
#ifdef MOZ_JXL
# include "nsJXLDecoder.h"
#endif
namespace mozilla {
@ -88,6 +91,11 @@ DecoderType DecoderFactory::GetDecoderType(const char* aMimeType) {
type = DecoderType::AVIF;
}
#endif
#ifdef MOZ_JXL
else if (!strcmp(aMimeType, IMAGE_JXL) && StaticPrefs::image_jxl_enabled()) {
type = DecoderType::JXL;
}
#endif
return type;
}
@ -130,6 +138,11 @@ already_AddRefed<Decoder> DecoderFactory::GetDecoder(DecoderType aType,
case DecoderType::AVIF:
decoder = new nsAVIFDecoder(aImage);
break;
#endif
#ifdef MOZ_JXL
case DecoderType::JXL:
decoder = new nsJXLDecoder(aImage);
break;
#endif
default:
MOZ_ASSERT_UNREACHABLE("Unknown decoder type");

Просмотреть файл

@ -15,8 +15,7 @@
#include "nsCOMPtr.h"
#include "SurfaceFlags.h"
namespace mozilla {
namespace image {
namespace mozilla::image {
class Decoder;
class IDecodingTask;
@ -39,6 +38,7 @@ enum class DecoderType {
ICON,
WEBP,
AVIF,
JXL,
UNKNOWN
};
@ -201,7 +201,6 @@ class DecoderFactory {
bool aIsRedecode);
};
} // namespace image
} // namespace mozilla
} // namespace mozilla::image
#endif // mozilla_image_DecoderFactory_h

Просмотреть файл

@ -58,10 +58,14 @@ nsresult mozilla::image::EnsureModuleInitialized() {
static ImageEnablementCookie kAVIFCookie = {
mozilla::StaticPrefs::image_avif_enabled, "image/avif"_ns};
static ImageEnablementCookie kJXLCookie = {
mozilla::StaticPrefs::image_jxl_enabled, "image/jxl"_ns};
static ImageEnablementCookie kWebPCookie = {
mozilla::StaticPrefs::image_webp_enabled, "image/webp"_ns};
Preferences::RegisterCallbackAndCall(UpdateContentViewerRegistration,
"image.avif.enabled", &kAVIFCookie);
Preferences::RegisterCallbackAndCall(UpdateContentViewerRegistration,
"image.jxl.enabled", &kJXLCookie);
Preferences::RegisterCallbackAndCall(UpdateContentViewerRegistration,
"image.webp.enabled", &kWebPCookie);

Просмотреть файл

@ -36,6 +36,11 @@ if CONFIG["MOZ_AV1"]:
"nsAVIFDecoder.cpp",
]
if CONFIG["MOZ_JXL"]:
UNIFIED_SOURCES += [
"nsJXLDecoder.cpp",
]
include("/ipc/chromium/chromium-config.mozbuild")
LOCAL_INCLUDES += [

Просмотреть файл

@ -0,0 +1,163 @@
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "ImageLogging.h" // Must appear first
#include "gfxPlatform.h"
#include "jxl/codestream_header.h"
#include "jxl/decode_cxx.h"
#include "jxl/types.h"
#include "mozilla/TelemetryHistogramEnums.h"
#include "mozilla/gfx/Point.h"
#include "nsJXLDecoder.h"
#include "RasterImage.h"
#include "SurfacePipeFactory.h"
using namespace mozilla::gfx;
namespace mozilla::image {
#define JXL_TRY(expr) \
do { \
JxlDecoderStatus status = (expr); \
if (status != JXL_DEC_SUCCESS) { \
return Transition::TerminateFailure(); \
} \
} while (0);
#define JXL_TRY_BOOL(expr) \
do { \
bool succeeded = (expr); \
if (!succeeded) { \
return Transition::TerminateFailure(); \
} \
} while (0);
static LazyLogModule sJXLLog("JXLDecoder");
nsJXLDecoder::nsJXLDecoder(RasterImage* aImage)
: Decoder(aImage),
mLexer(Transition::ToUnbuffered(State::FINISHED_JXL_DATA, State::JXL_DATA,
SIZE_MAX),
Transition::TerminateSuccess()),
mDecoder(JxlDecoderMake(nullptr)),
mParallelRunner(
JxlThreadParallelRunnerMake(nullptr, PreferredThreadCount())) {
JxlDecoderSubscribeEvents(mDecoder.get(),
JXL_DEC_BASIC_INFO | JXL_DEC_FULL_IMAGE);
JxlDecoderSetParallelRunner(mDecoder.get(), JxlThreadParallelRunner,
mParallelRunner.get());
MOZ_LOG(sJXLLog, LogLevel::Debug,
("[this=%p] nsJXLDecoder::nsJXLDecoder", this));
}
nsJXLDecoder::~nsJXLDecoder() {
MOZ_LOG(sJXLLog, LogLevel::Debug,
("[this=%p] nsJXLDecoder::~nsJXLDecoder", this));
}
size_t nsJXLDecoder::PreferredThreadCount() {
if (IsMetadataDecode()) {
return 0; // no additional worker thread
}
return JxlThreadParallelRunnerDefaultNumWorkerThreads();
}
LexerResult nsJXLDecoder::DoDecode(SourceBufferIterator& aIterator,
IResumable* aOnResume) {
// return LexerResult(TerminalState::FAILURE);
MOZ_ASSERT(!HasError(), "Shouldn't call DoDecode after error!");
return mLexer.Lex(aIterator, aOnResume,
[=](State aState, const char* aData, size_t aLength) {
switch (aState) {
case State::JXL_DATA:
return ReadJXLData(aData, aLength);
case State::FINISHED_JXL_DATA:
return FinishedJXLData();
}
MOZ_CRASH("Unknown State");
});
};
LexerTransition<nsJXLDecoder::State> nsJXLDecoder::ReadJXLData(
const char* aData, size_t aLength) {
const uint8_t* input = (const uint8_t*)aData;
size_t length = aLength;
if (mBuffer.length() != 0) {
JXL_TRY_BOOL(mBuffer.append(aData, aLength));
input = mBuffer.begin();
length = mBuffer.length();
}
JXL_TRY(JxlDecoderSetInput(mDecoder.get(), input, length));
while (true) {
JxlDecoderStatus status = JxlDecoderProcessInput(mDecoder.get());
switch (status) {
case JXL_DEC_ERROR:
default:
return Transition::TerminateFailure();
case JXL_DEC_NEED_MORE_INPUT: {
size_t remaining = JxlDecoderReleaseInput(mDecoder.get());
mBuffer.clear();
JXL_TRY_BOOL(mBuffer.append(aData + aLength - remaining, remaining));
return Transition::ContinueUnbuffered(State::JXL_DATA);
}
case JXL_DEC_BASIC_INFO: {
JXL_TRY(JxlDecoderGetBasicInfo(mDecoder.get(), &mInfo));
PostSize(mInfo.xsize, mInfo.ysize);
if (mInfo.alpha_bits > 0) {
PostHasTransparency();
}
if (IsMetadataDecode()) {
return Transition::TerminateSuccess();
}
break;
}
case JXL_DEC_NEED_IMAGE_OUT_BUFFER: {
size_t size = 0;
JxlPixelFormat format{4, JXL_TYPE_UINT8, JXL_LITTLE_ENDIAN, 0};
JXL_TRY(JxlDecoderImageOutBufferSize(mDecoder.get(), &format, &size));
mOutBuffer.clear();
JXL_TRY_BOOL(mOutBuffer.growBy(size));
JXL_TRY(JxlDecoderSetImageOutBuffer(mDecoder.get(), &format,
mOutBuffer.begin(), size));
break;
}
case JXL_DEC_FULL_IMAGE: {
gfx::IntSize size(mInfo.xsize, mInfo.ysize);
Maybe<SurfacePipe> pipe = SurfacePipeFactory::CreateSurfacePipe(
this, size, OutputSize(), FullFrame(), SurfaceFormat::R8G8B8A8,
SurfaceFormat::OS_RGBA, Nothing(), nullptr, SurfacePipeFlags());
for (uint8_t* rowPtr = mOutBuffer.begin(); rowPtr < mOutBuffer.end();
rowPtr += mInfo.xsize * 4) {
pipe->WriteBuffer(reinterpret_cast<uint32_t*>(rowPtr));
}
if (Maybe<SurfaceInvalidRect> invalidRect = pipe->TakeInvalidRect()) {
PostInvalidation(invalidRect->mInputSpaceRect,
Some(invalidRect->mOutputSpaceRect));
}
PostFrameStop();
PostDecodeDone();
return Transition::TerminateSuccess();
}
}
}
}
LexerTransition<nsJXLDecoder::State> nsJXLDecoder::FinishedJXLData() {
MOZ_ASSERT_UNREACHABLE("Read the entire address space?");
return Transition::TerminateFailure();
}
} // namespace mozilla::image

Просмотреть файл

@ -0,0 +1,55 @@
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#ifndef mozilla_image_decoders_nsJXLDecoder_h
#define mozilla_image_decoders_nsJXLDecoder_h
#include "Decoder.h"
#include "mp4parse.h"
#include "SurfacePipe.h"
#include "jxl/decode_cxx.h"
#include "jxl/thread_parallel_runner_cxx.h"
#include "mozilla/Telemetry.h"
namespace mozilla::image {
class RasterImage;
class nsJXLDecoder final : public Decoder {
public:
virtual ~nsJXLDecoder();
DecoderType GetType() const override { return DecoderType::JXL; }
protected:
LexerResult DoDecode(SourceBufferIterator& aIterator,
IResumable* aOnResume) override;
private:
friend class DecoderFactory;
// Decoders should only be instantiated via DecoderFactory.
explicit nsJXLDecoder(RasterImage* aImage);
size_t PreferredThreadCount();
enum class State { JXL_DATA, FINISHED_JXL_DATA };
LexerTransition<State> ReadJXLData(const char* aData, size_t aLength);
LexerTransition<State> FinishedJXLData();
StreamingLexer<State> mLexer;
JxlDecoderPtr mDecoder;
JxlThreadParallelRunnerPtr mParallelRunner;
Vector<uint8_t> mBuffer;
Vector<uint8_t> mOutBuffer;
JxlBasicInfo mInfo{};
};
} // namespace mozilla::image
#endif // mozilla_image_decoders_nsJXLDecoder_h

Просмотреть файл

@ -2766,6 +2766,11 @@ nsresult imgLoader::GetMimeTypeFromContent(const char* aContents,
detected) &&
detected.Equals(IMAGE_AVIF)) {
aContentType.AssignLiteral(IMAGE_AVIF);
} else if ((aLength >= 2 && !memcmp(aContents, "\xFF\x0A", 2)) ||
(aLength >= 12 &&
!memcmp(aContents, "\x00\x00\x00\x0CJXL \x0D\x0A\x87\x0A", 12))) {
// Each version is for containerless and containerful files respectively.
aContentType.AssignLiteral(IMAGE_JXL);
} else {
/* none of the above? I give up */
return NS_ERROR_NOT_AVAILABLE;

Просмотреть файл

@ -129,8 +129,13 @@ static int RunDecodeToSurfaceFuzzingAVIF(nsCOMPtr<nsIInputStream> inputStream) {
return RunDecodeToSurfaceFuzzing(inputStream, "image/avif");
}
static int RunDecodeToSurfaceFuzzingJXL(nsCOMPtr<nsIInputStream> inputStream) {
return RunDecodeToSurfaceFuzzing(inputStream, "image/jxl");
}
int FuzzingInitImage(int* argc, char*** argv) {
Preferences::SetBool("image.avif.enabled", true);
Preferences::SetBool("image.jxl.enabled", true);
nsCOMPtr<imgITools> imgTools =
do_CreateInstance("@mozilla.org/image/tools;1");
@ -162,3 +167,6 @@ MOZ_FUZZING_INTERFACE_STREAM(FuzzingInitImage, RunDecodeToSurfaceFuzzingWebP,
MOZ_FUZZING_INTERFACE_STREAM(FuzzingInitImage, RunDecodeToSurfaceFuzzingAVIF,
ImageAVIF);
MOZ_FUZZING_INTERFACE_STREAM(FuzzingInitImage, RunDecodeToSurfaceFuzzingJXL,
ImageJXL);

Просмотреть файл

@ -46,6 +46,10 @@ AutoInitializeImageLib::AutoInitializeImageLib() {
rv = Preferences::SetBool("image.avif.enabled", true);
EXPECT_TRUE(rv == NS_OK);
// Ensure JXL is enabled to run decoder tests.
rv = Preferences::SetBool("image.jxl.enabled", true);
EXPECT_TRUE(rv == NS_OK);
// Ensure that ImageLib services are initialized.
nsCOMPtr<imgITools> imgTools =
do_CreateInstance("@mozilla.org/image/tools;1");
@ -437,6 +441,10 @@ ImageTestCase GreenAVIFTestCase() {
.WithSurfaceFlags(SurfaceFlags::TO_SRGB_COLORSPACE);
}
ImageTestCase GreenJXLTestCase() {
return ImageTestCase("green.jxl", "image/jxl", IntSize(100, 100));
}
// Forcing sRGB is required until nsAVIFDecoder supports ICC profiles
// See bug 1634741
ImageTestCase Transparent10bit420AVIFTestCase() {
@ -564,6 +572,11 @@ ImageTestCase LargeAVIFTestCase() {
TEST_CASE_IGNORE_OUTPUT);
}
ImageTestCase LargeJXLTestCase() {
return ImageTestCase("large.jxl", "image/jxl", IntSize(1200, 660),
TEST_CASE_IGNORE_OUTPUT);
}
ImageTestCase GreenWebPIccSrgbTestCase() {
return ImageTestCase("green.icc_srgb.webp", "image/webp", IntSize(100, 100));
}
@ -660,6 +673,11 @@ ImageTestCase TransparentWebPTestCase() {
return test;
}
ImageTestCase TransparentJXLTestCase() {
return ImageTestCase("transparent.jxl", "image/jxl", IntSize(1200, 1200),
TEST_CASE_IS_TRANSPARENT);
}
ImageTestCase TransparentNoAlphaHeaderWebPTestCase() {
ImageTestCase test("transparent-no-alpha-header.webp", "image/webp",
IntSize(100, 100), TEST_CASE_IS_FUZZY);
@ -749,6 +767,11 @@ ImageTestCase DownscaledAVIFTestCase() {
IntSize(20, 20));
}
ImageTestCase DownscaledJXLTestCase() {
return ImageTestCase("downscaled.jxl", "image/jxl", IntSize(100, 100),
IntSize(20, 20));
}
ImageTestCase DownscaledTransparentICOWithANDMaskTestCase() {
// This test case is an ICO with AND mask transparency. We want to ensure that
// we can downscale it without crashing or triggering ASAN failures, but its

Просмотреть файл

@ -476,6 +476,7 @@ ImageTestCase GreenICOTestCase();
ImageTestCase GreenIconTestCase();
ImageTestCase GreenWebPTestCase();
ImageTestCase GreenAVIFTestCase();
ImageTestCase GreenJXLTestCase();
ImageTestCase Transparent10bit420AVIFTestCase();
ImageTestCase Transparent10bit422AVIFTestCase();
@ -490,6 +491,7 @@ ImageTestCase Transparent8bit444AVIFTestCase();
ImageTestCase StackCheckAVIFTestCase();
ImageTestCase LargeWebPTestCase();
ImageTestCase LargeJXLTestCase();
ImageTestCase GreenWebPIccSrgbTestCase();
ImageTestCase GreenFirstFrameAnimatedGIFTestCase();
@ -509,6 +511,7 @@ ImageTestCase CorruptICOWithBadBppTestCase();
ImageTestCase TransparentPNGTestCase();
ImageTestCase TransparentGIFTestCase();
ImageTestCase TransparentWebPTestCase();
ImageTestCase TransparentJXLTestCase();
ImageTestCase TransparentNoAlphaHeaderWebPTestCase();
ImageTestCase FirstFramePaddingGIFTestCase();
ImageTestCase TransparentIfWithinICOBMPTestCase(TestCaseFlags aFlags);
@ -526,6 +529,7 @@ ImageTestCase DownscaledBMPTestCase();
ImageTestCase DownscaledICOTestCase();
ImageTestCase DownscaledIconTestCase();
ImageTestCase DownscaledWebPTestCase();
ImageTestCase DownscaledJXLTestCase();
ImageTestCase DownscaledTransparentICOWithANDMaskTestCase();
ImageTestCase TruncatedSmallGIFTestCase();

Просмотреть файл

@ -680,6 +680,7 @@ IMAGE_GTEST_DECODER_BASE_F(BMP)
IMAGE_GTEST_DECODER_BASE_F(ICO)
IMAGE_GTEST_DECODER_BASE_F(Icon)
IMAGE_GTEST_DECODER_BASE_F(WebP)
IMAGE_GTEST_DECODER_BASE_F(JXL)
TEST_F(ImageDecoders, ICOWithANDMaskDownscaleDuringDecode) {
CheckDownscaleDuringDecode(DownscaledTransparentICOWithANDMaskTestCase());
@ -768,6 +769,10 @@ TEST_F(ImageDecoders, AVIFDownscaleDuringDecode) {
CheckDownscaleDuringDecode(DownscaledAVIFTestCase());
}
TEST_F(ImageDecoders, JXLLargeMultiChunk) {
CheckDecoderMultiChunk(LargeJXLTestCase(), /* aChunkSize */ 64);
}
TEST_F(ImageDecoders, AnimatedGIFSingleChunk) {
CheckDecoderSingleChunk(GreenFirstFrameAnimatedGIFTestCase());
}

Просмотреть файл

@ -87,6 +87,19 @@ TEST_F(ImageLoader, DetectAVIFCompatibleBrand) {
CheckMimeType(buffer, sizeof(buffer), IMAGE_AVIF);
}
TEST_F(ImageLoader, DetectJXLCodestream) {
const char buffer[] = "\xff\x0a";
CheckMimeType(buffer, sizeof(buffer), IMAGE_JXL);
}
TEST_F(ImageLoader, DetectJXLContainer) {
const char buffer[] =
"\x00\x00\x00\x0c"
"JXL "
"\x0d\x0a\x87\x0a";
CheckMimeType(buffer, sizeof(buffer), IMAGE_JXL);
}
TEST_F(ImageLoader, DetectNonImageMP4) {
const char buffer[] =
"\x00\x00\x00\x1c" // box length

Просмотреть файл

@ -153,6 +153,10 @@ TEST_F(ImageDecoderMetadata, BMP) { CheckMetadata(GreenBMPTestCase()); }
TEST_F(ImageDecoderMetadata, ICO) { CheckMetadata(GreenICOTestCase()); }
TEST_F(ImageDecoderMetadata, Icon) { CheckMetadata(GreenIconTestCase()); }
TEST_F(ImageDecoderMetadata, WebP) { CheckMetadata(GreenWebPTestCase()); }
TEST_F(ImageDecoderMetadata, JXL) { CheckMetadata(GreenJXLTestCase()); }
TEST_F(ImageDecoderMetadata, TransparentJXL) {
CheckMetadata(TransparentJXLTestCase());
}
TEST_F(ImageDecoderMetadata, AnimatedGIF) {
CheckMetadata(GreenFirstFrameAnimatedGIFTestCase());

Двоичные данные
image/test/gtest/downscaled.jxl Normal file

Двоичный файл не отображается.

Двоичные данные
image/test/gtest/green.jxl Normal file

Двоичный файл не отображается.

Двоичные данные
image/test/gtest/large.jxl Normal file

Двоичный файл не отображается.

Просмотреть файл

@ -60,6 +60,7 @@ TEST_HARNESS_FILES.gtest += [
"downscaled.ico",
"downscaled.icon",
"downscaled.jpg",
"downscaled.jxl",
"downscaled.png",
"downscaled.webp",
"first-frame-green.gif",
@ -77,10 +78,12 @@ TEST_HARNESS_FILES.gtest += [
"green.ico",
"green.icon",
"green.jpg",
"green.jxl",
"green.png",
"green.webp",
"invalid-truncated-metadata.bmp",
"large.avif",
"large.jxl",
"large.webp",
"multilayer.avif",
"no-frame-delay.gif",
@ -113,6 +116,7 @@ TEST_HARNESS_FILES.gtest += [
"transparent-no-alpha-header.webp",
"transparent.avif",
"transparent.gif",
"transparent.jxl",
"transparent.png",
"transparent.webp",
]

Двоичные данные
image/test/gtest/transparent.jxl Normal file

Двоичный файл не отображается.

Двоичные данные
image/test/reftest/jxl/jxl-size-33x33.jxl Normal file

Двоичный файл не отображается.

Двоичные данные
image/test/reftest/jxl/jxl-size-33x33.png Normal file

Двоичный файл не отображается.

После

Ширина:  |  Высота:  |  Размер: 3.2 KiB

Просмотреть файл

@ -0,0 +1,3 @@
# JXL tests
pref(image.jxl.enabled,true) == jxl-size-33x33.jxl jxl-size-33x33.png

Просмотреть файл

@ -28,6 +28,9 @@ skip-if(Android&&webrender) include ico/reftest.list
# JPEG tests
include jpeg/reftest.list
# JXL tests
skip-if(Android) include jxl/reftest.list
# GIF tests
include gif/reftest.list

Просмотреть файл

@ -5464,6 +5464,12 @@
value: true
mirror: always
# Whether we attempt to decode JXL images or not.
- name: image.jxl.enabled
type: RelaxedAtomicBool
value: false
mirror: always
#---------------------------------------------------------------------------
# Prefs starting with "intl."
#---------------------------------------------------------------------------

Просмотреть файл

@ -159,6 +159,7 @@
#define IMAGE_SVG_XML "image/svg+xml"
#define IMAGE_WEBP "image/webp"
#define IMAGE_AVIF "image/avif"
#define IMAGE_JXL "image/jxl"
#define MESSAGE_EXTERNAL_BODY "message/external-body"
#define MESSAGE_NEWS "message/news"

Просмотреть файл

@ -1136,6 +1136,7 @@ const kImageExtensions = new Set([
"svg",
"webp",
"avif",
"jxl",
]);
function getNormalizedLeafName(aFile, aDefaultExtension) {

Просмотреть файл

@ -535,6 +535,7 @@ static const nsExtraMimeTypeEntry extraMimeEntries[] = {
{IMAGE_SVG_XML, "svg", "Scalable Vector Graphics"},
{IMAGE_WEBP, "webp", "WebP Image"},
{IMAGE_AVIF, "avif", "AV1 Image File"},
{IMAGE_JXL, "jxl", "JPEG XL Image File"},
{MESSAGE_RFC822, "eml", "RFC-822 data"},
{TEXT_PLAIN, "txt,text", "Text File"},
@ -605,7 +606,7 @@ static const char* forcedExtensionMimetypes[] = {
* NOTE: These MUST be lower-case and ASCII.
*/
static const char* descriptionOverwriteExtensions[] = {
"avif", "pdf", "svg", "webp", "xml",
"avif", "jxl", "pdf", "svg", "webp", "xml",
};
static StaticRefPtr<nsExternalHelperAppService> sExtHelperAppSvcSingleton;