Backed out 3 changesets (bug 1892041) for causing SM failures in test262. CLOSED...
[gecko.git] / image / decoders / nsWebPDecoder.cpp
blobf394cf306920957d3e727737ed7f4a30ca644e8c
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "ImageLogging.h" // Must appear first
8 #include "gfxPlatform.h"
9 #include "mozilla/TelemetryHistogramEnums.h"
10 #include "nsWebPDecoder.h"
12 #include "RasterImage.h"
13 #include "SurfacePipeFactory.h"
15 using namespace mozilla::gfx;
17 namespace mozilla {
18 namespace image {
20 static LazyLogModule sWebPLog("WebPDecoder");
22 nsWebPDecoder::nsWebPDecoder(RasterImage* aImage)
23 : Decoder(aImage),
24 mDecoder(nullptr),
25 mBlend(BlendMethod::OVER),
26 mDisposal(DisposalMethod::KEEP),
27 mTimeout(FrameTimeout::Forever()),
28 mFormat(SurfaceFormat::OS_RGBX),
29 mLastRow(0),
30 mCurrentFrame(0),
31 mData(nullptr),
32 mLength(0),
33 mIteratorComplete(false),
34 mNeedDemuxer(true),
35 mGotColorProfile(false) {
36 MOZ_LOG(sWebPLog, LogLevel::Debug,
37 ("[this=%p] nsWebPDecoder::nsWebPDecoder", this));
40 nsWebPDecoder::~nsWebPDecoder() {
41 MOZ_LOG(sWebPLog, LogLevel::Debug,
42 ("[this=%p] nsWebPDecoder::~nsWebPDecoder", this));
43 if (mDecoder) {
44 WebPIDelete(mDecoder);
45 WebPFreeDecBuffer(&mBuffer);
49 LexerResult nsWebPDecoder::ReadData() {
50 MOZ_ASSERT(mData);
51 MOZ_ASSERT(mLength > 0);
53 WebPDemuxer* demuxer = nullptr;
54 bool complete = mIteratorComplete;
56 if (mNeedDemuxer) {
57 WebPDemuxState state;
58 WebPData fragment;
59 fragment.bytes = mData;
60 fragment.size = mLength;
62 demuxer = WebPDemuxPartial(&fragment, &state);
63 if (state == WEBP_DEMUX_PARSE_ERROR) {
64 MOZ_LOG(
65 sWebPLog, LogLevel::Error,
66 ("[this=%p] nsWebPDecoder::ReadData -- demux parse error\n", this));
67 WebPDemuxDelete(demuxer);
68 return LexerResult(TerminalState::FAILURE);
71 if (state == WEBP_DEMUX_PARSING_HEADER) {
72 WebPDemuxDelete(demuxer);
73 return LexerResult(Yield::NEED_MORE_DATA);
76 if (!demuxer) {
77 MOZ_LOG(sWebPLog, LogLevel::Error,
78 ("[this=%p] nsWebPDecoder::ReadData -- no demuxer\n", this));
79 return LexerResult(TerminalState::FAILURE);
82 complete = complete || state == WEBP_DEMUX_DONE;
85 LexerResult rv(TerminalState::FAILURE);
86 if (!HasSize()) {
87 rv = ReadHeader(demuxer, complete);
88 } else {
89 rv = ReadPayload(demuxer, complete);
92 WebPDemuxDelete(demuxer);
93 return rv;
96 LexerResult nsWebPDecoder::DoDecode(SourceBufferIterator& aIterator,
97 IResumable* aOnResume) {
98 while (true) {
99 SourceBufferIterator::State state = SourceBufferIterator::COMPLETE;
100 if (!mIteratorComplete) {
101 state = aIterator.AdvanceOrScheduleResume(SIZE_MAX, aOnResume);
103 // We need to remember since we can't advance a complete iterator.
104 mIteratorComplete = state == SourceBufferIterator::COMPLETE;
107 if (state == SourceBufferIterator::WAITING) {
108 return LexerResult(Yield::NEED_MORE_DATA);
111 LexerResult rv = UpdateBuffer(aIterator, state);
112 if (rv.is<Yield>() && rv.as<Yield>() == Yield::NEED_MORE_DATA) {
113 // We need to check the iterator to see if more is available before
114 // giving up unless we are already complete.
115 if (mIteratorComplete) {
116 MOZ_LOG(sWebPLog, LogLevel::Error,
117 ("[this=%p] nsWebPDecoder::DoDecode -- read all data, "
118 "but needs more\n",
119 this));
120 return LexerResult(TerminalState::FAILURE);
122 continue;
125 return rv;
129 LexerResult nsWebPDecoder::UpdateBuffer(SourceBufferIterator& aIterator,
130 SourceBufferIterator::State aState) {
131 MOZ_ASSERT(!HasError(), "Shouldn't call DoDecode after error!");
133 switch (aState) {
134 case SourceBufferIterator::READY:
135 if (!aIterator.IsContiguous()) {
136 // We need to buffer. This should be rare, but expensive.
137 break;
139 if (!mData) {
140 // For as long as we hold onto an iterator, we know the data pointers
141 // to the chunks cannot change underneath us, so save the pointer to
142 // the first block.
143 MOZ_ASSERT(mLength == 0);
144 mData = reinterpret_cast<const uint8_t*>(aIterator.Data());
146 mLength += aIterator.Length();
147 return ReadData();
148 case SourceBufferIterator::COMPLETE:
149 if (!mData) {
150 // We must have hit an error, such as an OOM, when buffering the
151 // first set of encoded data.
152 MOZ_LOG(
153 sWebPLog, LogLevel::Error,
154 ("[this=%p] nsWebPDecoder::DoDecode -- complete no data\n", this));
155 return LexerResult(TerminalState::FAILURE);
157 return ReadData();
158 default:
159 MOZ_LOG(sWebPLog, LogLevel::Error,
160 ("[this=%p] nsWebPDecoder::DoDecode -- bad state\n", this));
161 return LexerResult(TerminalState::FAILURE);
164 // We need to buffer. If we have no data buffered, we need to get everything
165 // from the first chunk of the source buffer before appending the new data.
166 if (mBufferedData.empty()) {
167 MOZ_ASSERT(mData);
168 MOZ_ASSERT(mLength > 0);
170 if (!mBufferedData.append(mData, mLength)) {
171 MOZ_LOG(sWebPLog, LogLevel::Error,
172 ("[this=%p] nsWebPDecoder::DoDecode -- oom, initialize %zu\n",
173 this, mLength));
174 return LexerResult(TerminalState::FAILURE);
177 MOZ_LOG(sWebPLog, LogLevel::Debug,
178 ("[this=%p] nsWebPDecoder::DoDecode -- buffered %zu bytes\n", this,
179 mLength));
182 // Append the incremental data from the iterator.
183 if (!mBufferedData.append(aIterator.Data(), aIterator.Length())) {
184 MOZ_LOG(sWebPLog, LogLevel::Error,
185 ("[this=%p] nsWebPDecoder::DoDecode -- oom, append %zu on %zu\n",
186 this, aIterator.Length(), mBufferedData.length()));
187 return LexerResult(TerminalState::FAILURE);
190 MOZ_LOG(sWebPLog, LogLevel::Debug,
191 ("[this=%p] nsWebPDecoder::DoDecode -- buffered %zu -> %zu bytes\n",
192 this, aIterator.Length(), mBufferedData.length()));
193 mData = mBufferedData.begin();
194 mLength = mBufferedData.length();
195 return ReadData();
198 nsresult nsWebPDecoder::CreateFrame(const OrientedIntRect& aFrameRect) {
199 MOZ_ASSERT(HasSize());
200 MOZ_ASSERT(!mDecoder);
202 MOZ_LOG(
203 sWebPLog, LogLevel::Debug,
204 ("[this=%p] nsWebPDecoder::CreateFrame -- frame %u, (%d, %d) %d x %d\n",
205 this, mCurrentFrame, aFrameRect.x, aFrameRect.y, aFrameRect.width,
206 aFrameRect.height));
208 if (aFrameRect.width <= 0 || aFrameRect.height <= 0) {
209 MOZ_LOG(sWebPLog, LogLevel::Error,
210 ("[this=%p] nsWebPDecoder::CreateFrame -- bad frame rect\n", this));
211 return NS_ERROR_FAILURE;
214 // If this is our first frame in an animation and it doesn't cover the
215 // full frame, then we are transparent even if there is no alpha
216 if (mCurrentFrame == 0 && !aFrameRect.IsEqualEdges(FullFrame())) {
217 MOZ_ASSERT(HasAnimation());
218 mFormat = SurfaceFormat::OS_RGBA;
219 PostHasTransparency();
222 if (!WebPInitDecBuffer(&mBuffer)) {
223 MOZ_LOG(
224 sWebPLog, LogLevel::Error,
225 ("[this=%p] nsWebPDecoder::CreateFrame -- WebPInitDecBuffer failed\n",
226 this));
227 return NS_ERROR_FAILURE;
230 switch (SurfaceFormat::OS_RGBA) {
231 case SurfaceFormat::B8G8R8A8:
232 mBuffer.colorspace = MODE_BGRA;
233 break;
234 case SurfaceFormat::A8R8G8B8:
235 mBuffer.colorspace = MODE_ARGB;
236 break;
237 case SurfaceFormat::R8G8B8A8:
238 mBuffer.colorspace = MODE_RGBA;
239 break;
240 default:
241 MOZ_ASSERT_UNREACHABLE("Unknown OS_RGBA");
242 return NS_ERROR_FAILURE;
245 mDecoder = WebPINewDecoder(&mBuffer);
246 if (!mDecoder) {
247 MOZ_LOG(sWebPLog, LogLevel::Error,
248 ("[this=%p] nsWebPDecoder::CreateFrame -- create decoder error\n",
249 this));
250 return NS_ERROR_FAILURE;
253 // WebP doesn't guarantee that the alpha generated matches the hint in the
254 // header, so we always need to claim the input is BGRA. If the output is
255 // BGRX, swizzling will mask off the alpha channel.
256 SurfaceFormat inFormat = SurfaceFormat::OS_RGBA;
258 SurfacePipeFlags pipeFlags = SurfacePipeFlags();
259 if (mFormat == SurfaceFormat::OS_RGBA &&
260 !(GetSurfaceFlags() & SurfaceFlags::NO_PREMULTIPLY_ALPHA)) {
261 pipeFlags |= SurfacePipeFlags::PREMULTIPLY_ALPHA;
264 Maybe<AnimationParams> animParams;
265 if (!IsFirstFrameDecode()) {
266 animParams.emplace(aFrameRect.ToUnknownRect(), mTimeout, mCurrentFrame,
267 mBlend, mDisposal);
270 Maybe<SurfacePipe> pipe = SurfacePipeFactory::CreateSurfacePipe(
271 this, Size(), OutputSize(), aFrameRect, inFormat, mFormat, animParams,
272 mTransform, pipeFlags);
273 if (!pipe) {
274 MOZ_LOG(sWebPLog, LogLevel::Error,
275 ("[this=%p] nsWebPDecoder::CreateFrame -- no pipe\n", this));
276 return NS_ERROR_FAILURE;
279 mFrameRect = aFrameRect;
280 mPipe = std::move(*pipe);
281 return NS_OK;
284 void nsWebPDecoder::EndFrame() {
285 MOZ_ASSERT(HasSize());
286 MOZ_ASSERT(mDecoder);
288 auto opacity = mFormat == SurfaceFormat::OS_RGBA ? Opacity::SOME_TRANSPARENCY
289 : Opacity::FULLY_OPAQUE;
291 MOZ_LOG(sWebPLog, LogLevel::Debug,
292 ("[this=%p] nsWebPDecoder::EndFrame -- frame %u, opacity %d, "
293 "disposal %d, timeout %d, blend %d\n",
294 this, mCurrentFrame, (int)opacity, (int)mDisposal,
295 mTimeout.AsEncodedValueDeprecated(), (int)mBlend));
297 PostFrameStop(opacity);
298 WebPIDelete(mDecoder);
299 WebPFreeDecBuffer(&mBuffer);
300 mDecoder = nullptr;
301 mLastRow = 0;
302 ++mCurrentFrame;
305 void nsWebPDecoder::ApplyColorProfile(const char* aProfile, size_t aLength) {
306 MOZ_ASSERT(!mGotColorProfile);
307 mGotColorProfile = true;
309 if (mCMSMode == CMSMode::Off || !GetCMSOutputProfile() ||
310 (mCMSMode == CMSMode::TaggedOnly && !aProfile)) {
311 return;
314 if (!aProfile) {
315 MOZ_LOG(sWebPLog, LogLevel::Debug,
316 ("[this=%p] nsWebPDecoder::ApplyColorProfile -- not tagged, use "
317 "sRGB transform\n",
318 this));
319 mTransform = GetCMSsRGBTransform(SurfaceFormat::OS_RGBA);
320 return;
323 mInProfile = qcms_profile_from_memory(aProfile, aLength);
324 if (!mInProfile) {
325 MOZ_LOG(
326 sWebPLog, LogLevel::Error,
327 ("[this=%p] nsWebPDecoder::ApplyColorProfile -- bad color profile\n",
328 this));
329 return;
332 uint32_t profileSpace = qcms_profile_get_color_space(mInProfile);
333 if (profileSpace != icSigRgbData) {
334 // WebP doesn't produce grayscale data, this must be corrupt.
335 MOZ_LOG(sWebPLog, LogLevel::Error,
336 ("[this=%p] nsWebPDecoder::ApplyColorProfile -- ignoring non-rgb "
337 "color profile\n",
338 this));
339 return;
342 // Calculate rendering intent.
343 int intent = gfxPlatform::GetRenderingIntent();
344 if (intent == -1) {
345 intent = qcms_profile_get_rendering_intent(mInProfile);
348 // Create the color management transform.
349 qcms_data_type type = gfxPlatform::GetCMSOSRGBAType();
350 mTransform = qcms_transform_create(mInProfile, type, GetCMSOutputProfile(),
351 type, (qcms_intent)intent);
352 MOZ_LOG(sWebPLog, LogLevel::Debug,
353 ("[this=%p] nsWebPDecoder::ApplyColorProfile -- use tagged "
354 "transform\n",
355 this));
358 LexerResult nsWebPDecoder::ReadHeader(WebPDemuxer* aDemuxer, bool aIsComplete) {
359 MOZ_ASSERT(aDemuxer);
361 MOZ_LOG(
362 sWebPLog, LogLevel::Debug,
363 ("[this=%p] nsWebPDecoder::ReadHeader -- %zu bytes\n", this, mLength));
365 uint32_t flags = WebPDemuxGetI(aDemuxer, WEBP_FF_FORMAT_FLAGS);
367 if (!IsMetadataDecode() && !mGotColorProfile) {
368 if (flags & WebPFeatureFlags::ICCP_FLAG) {
369 WebPChunkIterator iter;
370 if (WebPDemuxGetChunk(aDemuxer, "ICCP", 1, &iter)) {
371 ApplyColorProfile(reinterpret_cast<const char*>(iter.chunk.bytes),
372 iter.chunk.size);
373 WebPDemuxReleaseChunkIterator(&iter);
375 } else {
376 if (!aIsComplete) {
377 return LexerResult(Yield::NEED_MORE_DATA);
380 MOZ_LOG(sWebPLog, LogLevel::Warning,
381 ("[this=%p] nsWebPDecoder::ReadHeader header specified ICCP "
382 "but no ICCP chunk found, ignoring\n",
383 this));
385 ApplyColorProfile(nullptr, 0);
387 } else {
388 ApplyColorProfile(nullptr, 0);
392 if (flags & WebPFeatureFlags::ANIMATION_FLAG) {
393 // A metadata decode expects to get the correct first frame timeout which
394 // sadly is not provided by the normal WebP header parsing.
395 WebPIterator iter;
396 if (!WebPDemuxGetFrame(aDemuxer, 1, &iter)) {
397 return aIsComplete ? LexerResult(TerminalState::FAILURE)
398 : LexerResult(Yield::NEED_MORE_DATA);
401 PostIsAnimated(FrameTimeout::FromRawMilliseconds(iter.duration));
402 WebPDemuxReleaseIterator(&iter);
403 } else {
404 // Single frames don't need a demuxer to be created.
405 mNeedDemuxer = false;
408 uint32_t width = WebPDemuxGetI(aDemuxer, WEBP_FF_CANVAS_WIDTH);
409 uint32_t height = WebPDemuxGetI(aDemuxer, WEBP_FF_CANVAS_HEIGHT);
410 if (width > INT32_MAX || height > INT32_MAX) {
411 return LexerResult(TerminalState::FAILURE);
414 PostSize(width, height);
416 bool alpha = flags & WebPFeatureFlags::ALPHA_FLAG;
417 if (alpha) {
418 mFormat = SurfaceFormat::OS_RGBA;
419 PostHasTransparency();
422 MOZ_LOG(sWebPLog, LogLevel::Debug,
423 ("[this=%p] nsWebPDecoder::ReadHeader -- %u x %u, alpha %d, "
424 "animation %d, metadata decode %d, first frame decode %d\n",
425 this, width, height, alpha, HasAnimation(), IsMetadataDecode(),
426 IsFirstFrameDecode()));
428 if (IsMetadataDecode()) {
429 return LexerResult(TerminalState::SUCCESS);
432 return ReadPayload(aDemuxer, aIsComplete);
435 LexerResult nsWebPDecoder::ReadPayload(WebPDemuxer* aDemuxer,
436 bool aIsComplete) {
437 if (!HasAnimation()) {
438 auto rv = ReadSingle(mData, mLength, FullFrame());
439 if (rv.is<TerminalState>() &&
440 rv.as<TerminalState>() == TerminalState::SUCCESS) {
441 PostDecodeDone();
443 return rv;
445 return ReadMultiple(aDemuxer, aIsComplete);
448 LexerResult nsWebPDecoder::ReadSingle(const uint8_t* aData, size_t aLength,
449 const OrientedIntRect& aFrameRect) {
450 MOZ_ASSERT(!IsMetadataDecode());
451 MOZ_ASSERT(aData);
452 MOZ_ASSERT(aLength > 0);
454 MOZ_LOG(
455 sWebPLog, LogLevel::Debug,
456 ("[this=%p] nsWebPDecoder::ReadSingle -- %zu bytes\n", this, aLength));
458 if (!mDecoder && NS_FAILED(CreateFrame(aFrameRect))) {
459 return LexerResult(TerminalState::FAILURE);
462 bool complete;
463 do {
464 VP8StatusCode status = WebPIUpdate(mDecoder, aData, aLength);
465 switch (status) {
466 case VP8_STATUS_OK:
467 complete = true;
468 break;
469 case VP8_STATUS_SUSPENDED:
470 complete = false;
471 break;
472 default:
473 MOZ_LOG(sWebPLog, LogLevel::Error,
474 ("[this=%p] nsWebPDecoder::ReadSingle -- append error %d\n",
475 this, status));
476 return LexerResult(TerminalState::FAILURE);
479 int lastRow = -1;
480 int width = 0;
481 int height = 0;
482 int stride = 0;
483 uint8_t* rowStart =
484 WebPIDecGetRGB(mDecoder, &lastRow, &width, &height, &stride);
486 MOZ_LOG(
487 sWebPLog, LogLevel::Debug,
488 ("[this=%p] nsWebPDecoder::ReadSingle -- complete %d, read %d rows, "
489 "has %d rows available\n",
490 this, complete, mLastRow, lastRow));
492 if (!rowStart || lastRow == -1 || lastRow == mLastRow) {
493 return LexerResult(Yield::NEED_MORE_DATA);
496 if (width != mFrameRect.width || height != mFrameRect.height ||
497 stride < mFrameRect.width * 4 || lastRow > mFrameRect.height) {
498 MOZ_LOG(sWebPLog, LogLevel::Error,
499 ("[this=%p] nsWebPDecoder::ReadSingle -- bad (w,h,s) = (%d, %d, "
500 "%d)\n",
501 this, width, height, stride));
502 return LexerResult(TerminalState::FAILURE);
505 for (int row = mLastRow; row < lastRow; row++) {
506 uint32_t* src = reinterpret_cast<uint32_t*>(rowStart + row * stride);
507 WriteState result = mPipe.WriteBuffer(src);
509 Maybe<SurfaceInvalidRect> invalidRect = mPipe.TakeInvalidRect();
510 if (invalidRect) {
511 PostInvalidation(invalidRect->mInputSpaceRect,
512 Some(invalidRect->mOutputSpaceRect));
515 if (result == WriteState::FAILURE) {
516 MOZ_LOG(sWebPLog, LogLevel::Error,
517 ("[this=%p] nsWebPDecoder::ReadSingle -- write pixels error\n",
518 this));
519 return LexerResult(TerminalState::FAILURE);
522 if (result == WriteState::FINISHED) {
523 MOZ_ASSERT(row == lastRow - 1, "There was more data to read?");
524 complete = true;
525 break;
529 mLastRow = lastRow;
530 } while (!complete);
532 if (!complete) {
533 return LexerResult(Yield::NEED_MORE_DATA);
536 EndFrame();
537 return LexerResult(TerminalState::SUCCESS);
540 LexerResult nsWebPDecoder::ReadMultiple(WebPDemuxer* aDemuxer,
541 bool aIsComplete) {
542 MOZ_ASSERT(!IsMetadataDecode());
543 MOZ_ASSERT(aDemuxer);
545 MOZ_LOG(sWebPLog, LogLevel::Debug,
546 ("[this=%p] nsWebPDecoder::ReadMultiple\n", this));
548 bool complete = aIsComplete;
549 WebPIterator iter;
550 auto rv = LexerResult(Yield::NEED_MORE_DATA);
551 if (WebPDemuxGetFrame(aDemuxer, mCurrentFrame + 1, &iter)) {
552 switch (iter.blend_method) {
553 case WEBP_MUX_BLEND:
554 mBlend = BlendMethod::OVER;
555 break;
556 case WEBP_MUX_NO_BLEND:
557 mBlend = BlendMethod::SOURCE;
558 break;
559 default:
560 MOZ_ASSERT_UNREACHABLE("Unhandled blend method");
561 break;
564 switch (iter.dispose_method) {
565 case WEBP_MUX_DISPOSE_NONE:
566 mDisposal = DisposalMethod::KEEP;
567 break;
568 case WEBP_MUX_DISPOSE_BACKGROUND:
569 mDisposal = DisposalMethod::CLEAR;
570 break;
571 default:
572 MOZ_ASSERT_UNREACHABLE("Unhandled dispose method");
573 break;
576 mFormat = iter.has_alpha || mCurrentFrame > 0 ? SurfaceFormat::OS_RGBA
577 : SurfaceFormat::OS_RGBX;
578 mTimeout = FrameTimeout::FromRawMilliseconds(iter.duration);
579 OrientedIntRect frameRect(iter.x_offset, iter.y_offset, iter.width,
580 iter.height);
582 rv = ReadSingle(iter.fragment.bytes, iter.fragment.size, frameRect);
583 complete = complete && !WebPDemuxNextFrame(&iter);
584 WebPDemuxReleaseIterator(&iter);
587 if (rv.is<TerminalState>() &&
588 rv.as<TerminalState>() == TerminalState::SUCCESS) {
589 // If we extracted one frame, and it is not the last, we need to yield to
590 // the lexer to allow the upper layers to acknowledge the frame.
591 if (!complete && !IsFirstFrameDecode()) {
592 rv = LexerResult(Yield::OUTPUT_AVAILABLE);
593 } else {
594 uint32_t loopCount = WebPDemuxGetI(aDemuxer, WEBP_FF_LOOP_COUNT);
596 MOZ_LOG(sWebPLog, LogLevel::Debug,
597 ("[this=%p] nsWebPDecoder::ReadMultiple -- loop count %u\n", this,
598 loopCount));
599 PostDecodeDone(loopCount - 1);
603 return rv;
606 Maybe<Telemetry::HistogramID> nsWebPDecoder::SpeedHistogram() const {
607 return Some(Telemetry::IMAGE_DECODE_SPEED_WEBP);
610 } // namespace image
611 } // namespace mozilla