2015-10-06 00:29:01 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
2012-05-21 15:12:37 +04:00
|
|
|
* 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/. */
|
2001-04-02 08:56:18 +04:00
|
|
|
|
|
|
|
#include "nsIconDecoder.h"
|
2014-11-14 20:59:00 +03:00
|
|
|
#include "RasterImage.h"
|
2016-03-11 05:45:29 +03:00
|
|
|
#include "SurfacePipeFactory.h"
|
2007-07-04 05:09:10 +04:00
|
|
|
|
2015-09-09 08:07:18 +03:00
|
|
|
using namespace mozilla::gfx;
|
|
|
|
|
2010-08-23 06:30:46 +04:00
|
|
|
namespace mozilla {
|
2012-01-06 20:02:27 +04:00
|
|
|
namespace image {
|
2009-09-13 02:44:18 +04:00
|
|
|
|
2015-10-06 00:29:01 +03:00
|
|
|
static const uint32_t ICON_HEADER_SIZE = 2;
|
|
|
|
|
2015-01-16 02:11:35 +03:00
|
|
|
nsIconDecoder::nsIconDecoder(RasterImage* aImage)
|
2015-09-09 08:07:18 +03:00
|
|
|
: Decoder(aImage)
|
2016-07-18 08:51:19 +03:00
|
|
|
, mLexer(Transition::To(State::HEADER, ICON_HEADER_SIZE),
|
|
|
|
Transition::TerminateSuccess())
|
2015-10-06 00:29:01 +03:00
|
|
|
, mBytesPerRow() // set by ReadHeader()
|
2001-04-02 08:56:18 +04:00
|
|
|
{
|
2009-09-13 02:44:18 +04:00
|
|
|
// Nothing to do
|
2001-04-02 08:56:18 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsIconDecoder::~nsIconDecoder()
|
|
|
|
{ }
|
|
|
|
|
2016-07-16 08:27:12 +03:00
|
|
|
LexerResult
|
2016-07-15 08:39:39 +03:00
|
|
|
nsIconDecoder::DoDecode(SourceBufferIterator& aIterator, IResumable* aOnResume)
|
2001-04-02 08:56:18 +04:00
|
|
|
{
|
2016-07-11 10:05:51 +03:00
|
|
|
MOZ_ASSERT(!HasError(), "Shouldn't call DoDecode after error!");
|
2015-10-06 00:29:01 +03:00
|
|
|
|
2016-07-15 08:39:39 +03:00
|
|
|
return mLexer.Lex(aIterator, aOnResume,
|
2016-07-11 10:07:07 +03:00
|
|
|
[=](State aState, const char* aData, size_t aLength) {
|
|
|
|
switch (aState) {
|
|
|
|
case State::HEADER:
|
|
|
|
return ReadHeader(aData);
|
|
|
|
case State::ROW_OF_PIXELS:
|
|
|
|
return ReadRowOfPixels(aData, aLength);
|
|
|
|
case State::FINISH:
|
|
|
|
return Finish();
|
|
|
|
default:
|
|
|
|
MOZ_CRASH("Unknown State");
|
|
|
|
}
|
|
|
|
});
|
2015-10-06 00:29:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
LexerTransition<nsIconDecoder::State>
|
|
|
|
nsIconDecoder::ReadHeader(const char* aData)
|
|
|
|
{
|
|
|
|
// Grab the width and height.
|
2016-03-11 05:45:29 +03:00
|
|
|
uint8_t width = uint8_t(aData[0]);
|
|
|
|
uint8_t height = uint8_t(aData[1]);
|
2015-10-06 00:29:01 +03:00
|
|
|
|
|
|
|
// The input is 32bpp, so we expect 4 bytes of data per pixel.
|
2016-03-11 05:45:29 +03:00
|
|
|
mBytesPerRow = width * 4;
|
2007-02-06 04:59:19 +03:00
|
|
|
|
2015-10-06 00:29:01 +03:00
|
|
|
// Post our size to the superclass.
|
2016-03-11 05:45:29 +03:00
|
|
|
PostSize(width, height);
|
2009-09-13 02:44:18 +04:00
|
|
|
|
2015-10-06 00:29:01 +03:00
|
|
|
// Icons have alpha.
|
|
|
|
PostHasTransparency();
|
2009-09-13 02:44:18 +04:00
|
|
|
|
2015-10-06 00:29:01 +03:00
|
|
|
// If we're doing a metadata decode, we're done.
|
|
|
|
if (IsMetadataDecode()) {
|
2015-10-28 11:30:20 +03:00
|
|
|
return Transition::TerminateSuccess();
|
2015-10-06 00:29:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ASSERT(!mImageData, "Already have a buffer allocated?");
|
2016-03-11 05:45:29 +03:00
|
|
|
Maybe<SurfacePipe> pipe =
|
2016-07-30 01:45:58 +03:00
|
|
|
SurfacePipeFactory::CreateSurfacePipe(this, 0, GetSize(), OutputSize(),
|
|
|
|
IntRect(IntPoint(), GetSize()),
|
|
|
|
SurfaceFormat::B8G8R8A8,
|
2016-03-11 05:45:29 +03:00
|
|
|
SurfacePipeFlags());
|
|
|
|
if (!pipe) {
|
2015-10-28 11:30:20 +03:00
|
|
|
return Transition::TerminateFailure();
|
2015-10-06 00:29:01 +03:00
|
|
|
}
|
|
|
|
|
2016-03-11 05:45:29 +03:00
|
|
|
mPipe = Move(*pipe);
|
|
|
|
|
|
|
|
MOZ_ASSERT(mImageData, "Should have a buffer now");
|
2015-10-06 00:29:01 +03:00
|
|
|
|
|
|
|
return Transition::To(State::ROW_OF_PIXELS, mBytesPerRow);
|
|
|
|
}
|
|
|
|
|
|
|
|
LexerTransition<nsIconDecoder::State>
|
|
|
|
nsIconDecoder::ReadRowOfPixels(const char* aData, size_t aLength)
|
|
|
|
{
|
2016-03-11 05:45:29 +03:00
|
|
|
MOZ_ASSERT(aLength % 4 == 0, "Rows should contain a multiple of four bytes");
|
|
|
|
|
|
|
|
auto result = mPipe.WritePixels<uint32_t>([&]() -> NextPixel<uint32_t> {
|
|
|
|
if (aLength == 0) {
|
|
|
|
return AsVariant(WriteState::NEED_MORE_DATA); // Done with this row.
|
2015-10-06 00:29:01 +03:00
|
|
|
}
|
|
|
|
|
2016-03-11 05:45:29 +03:00
|
|
|
uint32_t pixel = *reinterpret_cast<const uint32_t*>(aData);
|
|
|
|
aData += 4;
|
|
|
|
aLength -= 4;
|
|
|
|
|
|
|
|
return AsVariant(pixel);
|
|
|
|
});
|
|
|
|
|
|
|
|
MOZ_ASSERT(result != WriteState::FAILURE);
|
|
|
|
|
|
|
|
Maybe<SurfaceInvalidRect> invalidRect = mPipe.TakeInvalidRect();
|
|
|
|
if (invalidRect) {
|
|
|
|
PostInvalidation(invalidRect->mInputSpaceRect,
|
|
|
|
Some(invalidRect->mOutputSpaceRect));
|
2015-10-06 00:29:01 +03:00
|
|
|
}
|
|
|
|
|
2016-03-11 05:45:29 +03:00
|
|
|
return result == WriteState::FINISHED
|
|
|
|
? Transition::To(State::FINISH, 0)
|
|
|
|
: Transition::To(State::ROW_OF_PIXELS, mBytesPerRow);
|
2015-10-06 00:29:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
LexerTransition<nsIconDecoder::State>
|
|
|
|
nsIconDecoder::Finish()
|
|
|
|
{
|
|
|
|
PostFrameStop();
|
|
|
|
PostDecodeDone();
|
|
|
|
|
2015-10-28 11:30:20 +03:00
|
|
|
return Transition::TerminateSuccess();
|
2009-09-13 02:44:18 +04:00
|
|
|
}
|
|
|
|
|
2012-01-06 20:02:27 +04:00
|
|
|
} // namespace image
|
2010-08-23 06:30:46 +04:00
|
|
|
} // namespace mozilla
|