Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
Static Public Member Functions | Protected Member Functions | Private Member Functions | List of all members
SkJpegxlCodec Class Reference

#include <SkJpegxlCodec.h>

Inheritance diagram for SkJpegxlCodec:
SkScalingCodec SkCodec SkNoncopyable

Static Public Member Functions

static bool IsJpegxl (const void *, size_t)
 
static std::unique_ptr< SkCodecMakeFromStream (std::unique_ptr< SkStream >, Result *)
 
- Static Public Member Functions inherited from SkCodec
static constexpr size_t MinBufferedBytesNeeded ()
 
static const char * ResultToString (Result)
 
static std::unique_ptr< SkCodecMakeFromStream (std::unique_ptr< SkStream >, SkSpan< const SkCodecs::Decoder > decoders, Result *=nullptr, SkPngChunkReader *=nullptr, SelectionPolicy selectionPolicy=SelectionPolicy::kPreferStillImage)
 
static std::unique_ptr< SkCodecMakeFromStream (std::unique_ptr< SkStream >, Result *=nullptr, SkPngChunkReader *=nullptr, SelectionPolicy selectionPolicy=SelectionPolicy::kPreferStillImage)
 
static std::unique_ptr< SkCodecMakeFromData (sk_sp< SkData >, SkSpan< const SkCodecs::Decoder > decoders, SkPngChunkReader *=nullptr)
 
static std::unique_ptr< SkCodecMakeFromData (sk_sp< SkData >, SkPngChunkReader *=nullptr)
 
static void Register (bool(*peek)(const void *, size_t), std::unique_ptr< SkCodec >(*make)(std::unique_ptr< SkStream >, SkCodec::Result *))
 

Protected Member Functions

SkEncodedImageFormat onGetEncodedFormat () const override
 
Result onGetPixels (const SkImageInfo &dstInfo, void *dst, size_t rowBytes, const Options &options, int *rowsDecodedPtr) override
 
bool onRewind () override
 
bool conversionSupported (const SkImageInfo &, bool, bool) override
 
int onGetFrameCount () override
 
bool onGetFrameInfo (int, FrameInfo *) const override
 
int onGetRepetitionCount () override
 
- Protected Member Functions inherited from SkScalingCodec
 SkScalingCodec (SkEncodedInfo &&info, XformFormat srcFormat, std::unique_ptr< SkStream > stream, SkEncodedOrigin origin=kTopLeft_SkEncodedOrigin)
 
SkISize onGetScaledDimensions (float desiredScale) const override
 
bool onDimensionsSupported (const SkISize &requested) override
 
- Protected Member Functions inherited from SkCodec
const SkEncodedInfogetEncodedInfo () const
 
 SkCodec (SkEncodedInfo &&, XformFormat srcFormat, std::unique_ptr< SkStream >, SkEncodedOrigin=kTopLeft_SkEncodedOrigin)
 
void setSrcXformFormat (XformFormat pixelFormat)
 
XformFormat getSrcXformFormat () const
 
virtual bool onGetGainmapInfo (SkGainmapInfo *, std::unique_ptr< SkStream > *)
 
virtual bool onQueryYUVAInfo (const SkYUVAPixmapInfo::SupportedDataTypes &, SkYUVAPixmapInfo *) const
 
virtual Result onGetYUVAPlanes (const SkYUVAPixmaps &)
 
virtual bool onGetValidSubset (SkIRect *) const
 
bool rewindIfNeeded ()
 
SkStreamstream ()
 
virtual SkScanlineOrder onGetScanlineOrder () const
 
const SkImageInfodstInfo () const
 
const Optionsoptions () const
 
int currScanline () const
 
virtual int onOutputScanline (int inputScanline) const
 
virtual bool usesColorXform () const
 
void applyColorXform (void *dst, const void *src, int count) const
 
bool colorXform () const
 
bool xformOnDecode () const
 

Private Member Functions

const SkFrameHoldergetFrameHolder () const override
 

Additional Inherited Members

- Public Types inherited from SkCodec
enum  Result {
  kSuccess , kIncompleteInput , kErrorInInput , kInvalidConversion ,
  kInvalidScale , kInvalidParameters , kInvalidInput , kCouldNotRewind ,
  kInternalError , kUnimplemented
}
 
enum class  SelectionPolicy { kPreferStillImage , kPreferAnimation }
 
enum  ZeroInitialized { kYes_ZeroInitialized , kNo_ZeroInitialized }
 
enum  SkScanlineOrder { kTopDown_SkScanlineOrder , kBottomUp_SkScanlineOrder }
 
- Public Member Functions inherited from SkCodec
virtual ~SkCodec ()
 
SkImageInfo getInfo () const
 
SkISize dimensions () const
 
SkIRect bounds () const
 
const skcms_ICCProfilegetICCProfile () const
 
SkEncodedOrigin getOrigin () const
 
SkISize getScaledDimensions (float desiredScale) const
 
bool getValidSubset (SkIRect *desiredSubset) const
 
SkEncodedImageFormat getEncodedFormat () const
 
virtual std::unique_ptr< SkStreamgetEncodedData () const
 
Result getPixels (const SkImageInfo &info, void *pixels, size_t rowBytes, const Options *)
 
Result getPixels (const SkImageInfo &info, void *pixels, size_t rowBytes)
 
Result getPixels (const SkPixmap &pm, const Options *opts=nullptr)
 
std::tuple< sk_sp< SkImage >, SkCodec::ResultgetImage (const SkImageInfo &info, const Options *opts=nullptr)
 
std::tuple< sk_sp< SkImage >, SkCodec::ResultgetImage ()
 
bool queryYUVAInfo (const SkYUVAPixmapInfo::SupportedDataTypes &supportedDataTypes, SkYUVAPixmapInfo *yuvaPixmapInfo) const
 
Result getYUVAPlanes (const SkYUVAPixmaps &yuvaPixmaps)
 
Result startIncrementalDecode (const SkImageInfo &dstInfo, void *dst, size_t rowBytes, const Options *)
 
Result startIncrementalDecode (const SkImageInfo &dstInfo, void *dst, size_t rowBytes)
 
Result incrementalDecode (int *rowsDecoded=nullptr)
 
Result startScanlineDecode (const SkImageInfo &dstInfo, const Options *options)
 
Result startScanlineDecode (const SkImageInfo &dstInfo)
 
int getScanlines (void *dst, int countLines, size_t rowBytes)
 
bool skipScanlines (int countLines)
 
SkScanlineOrder getScanlineOrder () const
 
int nextScanline () const
 
int outputScanline (int inputScanline) const
 
int getFrameCount ()
 
bool getFrameInfo (int index, FrameInfo *info) const
 
std::vector< FrameInfogetFrameInfo ()
 
int getRepetitionCount ()
 
- Static Public Attributes inherited from SkCodec
static constexpr int kNoFrame = -1
 
static constexpr int kRepetitionCountInfinite = -1
 
- Protected Types inherited from SkCodec
using XformFormat = skcms_PixelFormat
 

Detailed Description

Definition at line 31 of file SkJpegxlCodec.h.

Member Function Documentation

◆ conversionSupported()

bool SkJpegxlCodec::conversionSupported ( const SkImageInfo dst,
bool  srcIsOpaque,
bool  needsColorXform 
)
overrideprotectedvirtual

Return whether we can convert to dst.

Will be called for the appropriate frame, prior to initializing the colorXform.

Reimplemented from SkCodec.

Definition at line 296 of file SkJpegxlCodec.cpp.

297 {
298 fCodec->fDstColorType = dstInfo.colorType();
299 switch (dstInfo.colorType()) {
301 return true; // memcpy
303 return true; // rgba->bgra
304
306 SkASSERT(needsColorXform); // TODO(eustas): not necessary for JXL.
307 return true; // memcpy
308
309 // TODO(eustas): implement
311 return false;
313 return false;
315 return false;
316
317 default:
318 return false;
319 }
320 return true;
321}
#define SkASSERT(cond)
Definition SkAssert.h:116
@ kBGRA_8888_SkColorType
pixel with 8 bits for blue, green, red, alpha; in 32-bit word
Definition SkColorType.h:26
@ kRGBA_F16_SkColorType
pixel with half floats for red, green, blue, alpha;
Definition SkColorType.h:38
@ kAlpha_8_SkColorType
pixel with alpha in 8-bit byte
Definition SkColorType.h:21
@ kGray_8_SkColorType
pixel with grayscale level in 8-bit byte
Definition SkColorType.h:35
@ kRGB_565_SkColorType
pixel with 5 bits red, 6 bits green, 5 bits blue, in 16-bit word
Definition SkColorType.h:22
@ kRGBA_8888_SkColorType
pixel with 8 bits for red, green, blue, alpha; in 32-bit word
Definition SkColorType.h:24
const SkImageInfo & dstInfo() const
Definition SkCodec.h:878
SkColorType colorType() const

◆ getFrameHolder()

const SkFrameHolder * SkJpegxlCodec::getFrameHolder ( ) const
overrideprivatevirtual

For multi-framed images, return the object with information about the frames.

Reimplemented from SkCodec.

Definition at line 444 of file SkJpegxlCodec.cpp.

444 {
445 return fCodec.get();
446}

◆ IsJpegxl()

bool SkJpegxlCodec::IsJpegxl ( const void *  buffer,
size_t  bytesRead 
)
static

Definition at line 53 of file SkJpegxlCodec.cpp.

53 {
54 JxlSignature result = JxlSignatureCheck(reinterpret_cast<const uint8_t*>(buffer), bytesRead);
55 return (result == JXL_SIG_CODESTREAM) || (result == JXL_SIG_CONTAINER);
56}
static const uint8_t buffer[]
GAsyncResult * result

◆ MakeFromStream()

std::unique_ptr< SkCodec > SkJpegxlCodec::MakeFromStream ( std::unique_ptr< SkStream stream,
Result result 
)
static

Definition at line 86 of file SkJpegxlCodec.cpp.

87 {
89 if (!stream) {
91 return nullptr;
92 }
94 // Either wrap or copy stream data.
95 sk_sp<SkData> data = nullptr;
96 if (stream->getMemoryBase()) {
98 } else {
100 // Data is copied; stream can be released now.
101 stream.reset(nullptr);
102 }
103
104 auto priv = std::make_unique<SkJpegxlCodecPriv>();
105 JxlDecoder* dec = priv->fDecoder.get();
106
107 // Only query metadata this time.
108 auto status = JxlDecoderSubscribeEvents(dec, JXL_DEC_BASIC_INFO | JXL_DEC_COLOR_ENCODING);
109 if (status != JXL_DEC_SUCCESS) {
110 // Fresh instance must accept request for subscription.
111 SkDEBUGFAIL("libjxl returned unexpected status");
112 return nullptr;
113 }
114
115 status = JxlDecoderSetInput(dec, data->bytes(), data->size());
116 if (status != JXL_DEC_SUCCESS) {
117 // Fresh instance must accept first chunk of input.
118 SkDEBUGFAIL("libjxl returned unexpected status");
119 return nullptr;
120 }
121
122 status = JxlDecoderProcessInput(dec);
123 if (status == JXL_DEC_NEED_MORE_INPUT) {
125 return nullptr;
126 }
127 if (status != JXL_DEC_BASIC_INFO) {
129 return nullptr;
130 }
131 JxlBasicInfo& info = priv->fInfo;
132 status = JxlDecoderGetBasicInfo(dec, &info);
133 if (status != JXL_DEC_SUCCESS) {
134 // Current event is "JXL_DEC_BASIC_INFO" -> can't fail.
135 SkDEBUGFAIL("libjxl returned unexpected status");
136 return nullptr;
137 }
138
139 // Check that image dimensions are not too large.
140 if (!SkTFitsIn<int32_t>(info.xsize) || !SkTFitsIn<int32_t>(info.ysize)) {
142 return nullptr;
143 }
144 int32_t width = SkTo<int32_t>(info.xsize);
145 int32_t height = SkTo<int32_t>(info.ysize);
146
147 bool hasAlpha = (info.alpha_bits != 0);
148 bool isGray = (info.num_color_channels == 1);
152 if (hasAlpha) {
154 } else {
156 }
157
158 status = JxlDecoderProcessInput(dec);
159 if (status != JXL_DEC_COLOR_ENCODING) {
161 return nullptr;
162 }
163
164 size_t iccSize = 0;
165 // TODO(eustas): format field is currently ignored by decoder.
166 status = JxlDecoderGetICCProfileSize(
167 dec, /* format = */ nullptr, JXL_COLOR_PROFILE_TARGET_DATA, &iccSize);
168 if (status != JXL_DEC_SUCCESS) {
169 // Likely incompatible colorspace.
170 iccSize = 0;
171 }
172 std::unique_ptr<SkEncodedInfo::ICCProfile> profile = nullptr;
173 if (iccSize) {
174 auto icc = SkData::MakeUninitialized(iccSize);
175 // TODO(eustas): format field is currently ignored by decoder.
176 status = JxlDecoderGetColorAsICCProfile(dec,
177 /* format = */ nullptr,
178 JXL_COLOR_PROFILE_TARGET_DATA,
179 reinterpret_cast<uint8_t*>(icc->writable_data()),
180 iccSize);
181 if (status != JXL_DEC_SUCCESS) {
182 // Current event is JXL_DEC_COLOR_ENCODING -> can't fail.
183 SkDEBUGFAIL("libjxl returned unexpected status");
184 return nullptr;
185 }
187 }
188
189 int bitsPerChannel = 16;
190
191 *result = kSuccess;
192 SkEncodedInfo encodedInfo =
193 SkEncodedInfo::Make(width, height, color, alpha, bitsPerChannel, std::move(profile));
194
195 return std::unique_ptr<SkCodec>(new SkJpegxlCodec(
196 std::move(priv), std::move(encodedInfo), std::move(stream), std::move(data)));
197}
static void info(const char *fmt,...) SK_PRINTF_LIKE(1
Definition DM.cpp:213
SkColor4f color
#define SkDEBUGFAIL(message)
Definition SkAssert.h:118
sk_sp< SkData > SkCopyStreamToData(SkStream *stream)
Definition SkStream.cpp:937
SkStream * stream()
Definition SkCodec.h:865
@ kIncompleteInput
Definition SkCodec.h:84
@ kInvalidInput
Definition SkCodec.h:109
@ kInternalError
Definition SkCodec.h:118
@ kSuccess
Definition SkCodec.h:80
static sk_sp< SkData > MakeWithoutCopy(const void *data, size_t length)
Definition SkData.h:116
static sk_sp< SkData > MakeUninitialized(size_t length)
Definition SkData.cpp:116
static std::unique_ptr< ICCProfile > Make(sk_sp< SkData >)
virtual size_t getLength() const
Definition SkStream.h:137
virtual const void * getMemoryBase()
Definition SkStream.h:141
FlPixelBufferTexturePrivate * priv
DEF_SWITCHES_START aot vmservice shared library Name of the *so containing AOT compiled Dart assets for launching the service isolate vm snapshot data
Definition switches.h:41
int32_t height
int32_t width
static SkEncodedInfo Make(int width, int height, Color color, Alpha alpha, int bitsPerComponent)

◆ onGetEncodedFormat()

SkEncodedImageFormat SkJpegxlCodec::onGetEncodedFormat ( ) const
inlineoverrideprotectedvirtual

Implements SkCodec.

Definition at line 48 of file SkJpegxlCodec.h.

◆ onGetFrameCount()

int SkJpegxlCodec::onGetFrameCount ( )
overrideprotectedvirtual

Reimplemented from SkCodec.

Definition at line 403 of file SkJpegxlCodec.cpp.

403 {
404 if (!fCodec->fInfo.have_animation) {
405 return 1;
406 }
407
408 if (!fCodec->fSeenAllFrames) {
409 fCodec->fSeenAllFrames = scanFrames();
410 }
411
412 return fCodec->fFrames.size();
413}

◆ onGetFrameInfo()

bool SkJpegxlCodec::onGetFrameInfo ( int  index,
FrameInfo frameInfo 
) const
overrideprotectedvirtual

Reimplemented from SkCodec.

Definition at line 415 of file SkJpegxlCodec.cpp.

415 {
416 if (index < 0) {
417 return false;
418 }
419 if (static_cast<size_t>(index) >= fCodec->fFrames.size()) {
420 return false;
421 }
422 fCodec->fFrames[index].fillIn(frameInfo, true);
423 return true;
424}

◆ onGetPixels()

SkCodec::Result SkJpegxlCodec::onGetPixels ( const SkImageInfo info,
void *  pixels,
size_t  rowBytes,
const Options ,
int rowsDecoded 
)
overrideprotectedvirtual
Parameters
rowsDecodedWhen the encoded image stream is incomplete, this function will return kIncompleteInput and rowsDecoded will be set to the number of scanlines that were successfully decoded. This will allow getPixels() to fill the uninitialized memory.

Implements SkCodec.

Definition at line 199 of file SkJpegxlCodec.cpp.

200 {
201 // TODO(eustas): implement
202 if (options.fSubset) {
203 return kUnimplemented;
204 }
205 auto& codec = *fCodec.get();
206 const int index = options.fFrameIndex;
207 SkASSERT(0 == index || static_cast<size_t>(index) < codec.fFrames.size());
208 auto* dec = codec.fDecoder.get();
209 JxlDecoderStatus status;
210
211 if ((codec.fLastProcessedFrame >= index) || (codec.fLastProcessedFrame = SkCodec::kNoFrame)) {
212 codec.fLastProcessedFrame = SkCodec::kNoFrame;
213 JxlDecoderRewind(dec);
214 status = JxlDecoderSubscribeEvents(dec, JXL_DEC_FRAME | JXL_DEC_FULL_IMAGE);
215 if (status != JXL_DEC_SUCCESS) {
216 // Fresh decoder instance (after rewind) must accept subscription request.
217 SkDEBUGFAIL("libjxl returned unexpected status");
218 return kInternalError;
219 }
220 status = JxlDecoderSetInput(dec, fData->bytes(), fData->size());
221 if (status != JXL_DEC_SUCCESS) {
222 // Fresh decoder instance (after rewind) must accept first data chunk.
223 SkDEBUGFAIL("libjxl returned unexpected status");
224 return kInternalError;
225 }
226 SkASSERT(codec.fLastProcessedFrame + 1 == 0);
227 }
228
229 int nextFrame = codec.fLastProcessedFrame + 1;
230 if (nextFrame < index) {
231 JxlDecoderSkipFrames(dec, index - nextFrame);
232 }
233
234 // Decode till the frame start.
235 status = JxlDecoderProcessInput(dec);
236 // TODO(eustas): actually, frame is not completely processed; for streaming / partial decoding
237 // we should also add a flag that "last processed frame" is still incomplete, and
238 // flip that flag when frame decoding is over.
239 codec.fLastProcessedFrame = index;
240 if (status != JXL_DEC_FRAME) {
241 // TODO(eustas): check status: it might be either corrupted or incomplete input.
242 return kInternalError;
243 }
244
245 codec.fDst = dst;
246 codec.fRowBytes = rowBytes;
247
248 // TODO(eustas): consider grayscale.
249 uint32_t numColorChannels = 3;
250 // TODO(eustas): consider no-alpha.
251 uint32_t numAlphaChannels = 1;
252 // NB: SKIA works with little-endian F16s.
253 auto endianness = JXL_LITTLE_ENDIAN;
254
255 // Internally JXL does most processing in floats. By "default" we request
256 // output data type to be U8; it takes less memory, but results in some precision loss.
257 // We request F16 in two cases:
258 // - destination type is F16
259 // - color transformation is required; in this case values are remapped,
260 // and with 8-bit precision it is likely that visual artefact will appear
261 // (like banding, etc.)
262 bool halfFloatOutput = false;
263 if (fCodec->fDstColorType == kRGBA_F16_SkColorType) halfFloatOutput = true;
264 if (colorXform()) halfFloatOutput = true;
265 auto dataType = halfFloatOutput ? JXL_TYPE_FLOAT16 : JXL_TYPE_UINT8;
266
267 JxlPixelFormat format =
268 {numColorChannels + numAlphaChannels, dataType, endianness, /* align = */ 0};
269 status = JxlDecoderSetImageOutCallback(dec, &format, SkJpegxlCodec::imageOutCallback, this);
270 if (status != JXL_DEC_SUCCESS) {
271 // Current event is JXL_DEC_FRAME -> decoder must accept callback.
272 SkDEBUGFAIL("libjxl returned unexpected status");
273 return kInternalError;
274 }
275
276 // Decode till the frame start.
277 status = JxlDecoderProcessInput(dec);
278 if (status != JXL_DEC_FULL_IMAGE) {
279 // TODO(eustas): check status: it might be either corrupted or incomplete input.
280 return kInternalError;
281 }
282 // TODO(eustas): currently it is supposed that complete input is accessible;
283 // when streaming support is added JXL_DEC_NEED_MORE_INPUT would also
284 // become a legal outcome; amount of decoded scanlines should be calculated
285 // based on callback invocations / render-pipeline API.
286 *rowsDecodedPtr = dstInfo.height();
287
288 return kSuccess;
289}
@ kUnimplemented
Definition SkCodec.h:123
bool colorXform() const
Definition SkCodec.h:906
static constexpr int kNoFrame
Definition SkCodec.h:650
const Options & options() const
Definition SkCodec.h:880
const uint8_t * bytes() const
Definition SkData.h:43
size_t size() const
Definition SkData.h:30
uint32_t uint32_t * format
dst
Definition cp.py:12
const SkIRect * fSubset
Definition SkCodec.h:347
int height() const

◆ onGetRepetitionCount()

int SkJpegxlCodec::onGetRepetitionCount ( )
overrideprotectedvirtual

Reimplemented from SkCodec.

Definition at line 426 of file SkJpegxlCodec.cpp.

426 {
427 JxlBasicInfo& info = fCodec->fInfo;
428 if (!info.have_animation) {
429 return 0;
430 }
431
432 if (info.animation.num_loops == 0) {
434 }
435
436 if (SkTFitsIn<int>(info.animation.num_loops)) {
437 return info.animation.num_loops - 1;
438 }
439
440 // Largest "non-infinite" value.
441 return std::numeric_limits<int>::max();
442}
static constexpr int kRepetitionCountInfinite
Definition SkCodec.h:759

◆ onRewind()

bool SkJpegxlCodec::onRewind ( )
overrideprotectedvirtual

Called by rewindIfNeeded, if the stream needed to be rewound.

Subclasses should do any set up needed after a rewind.

Reimplemented from SkCodec.

Definition at line 291 of file SkJpegxlCodec.cpp.

291 {
292 JxlDecoderRewind(fCodec->fDecoder.get());
293 return true;
294}

The documentation for this class was generated from the following files: