1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
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 "nsIconDecoder.h"
8 #include "RasterImage.h"
9 #include "SurfacePipeFactory.h"
10 #include "gfxPlatform.h"
12 using namespace mozilla::gfx
;
17 static const uint32_t ICON_HEADER_SIZE
= 4;
19 nsIconDecoder::nsIconDecoder(RasterImage
* aImage
)
21 mLexer(Transition::To(State::HEADER
, ICON_HEADER_SIZE
),
22 Transition::TerminateSuccess()),
23 mBytesPerRow() // set by ReadHeader()
28 nsIconDecoder::~nsIconDecoder() {}
30 LexerResult
nsIconDecoder::DoDecode(SourceBufferIterator
& aIterator
,
31 IResumable
* aOnResume
) {
32 MOZ_ASSERT(!HasError(), "Shouldn't call DoDecode after error!");
34 return mLexer
.Lex(aIterator
, aOnResume
,
35 [=](State aState
, const char* aData
, size_t aLength
) {
38 return ReadHeader(aData
);
39 case State::ROW_OF_PIXELS
:
40 return ReadRowOfPixels(aData
, aLength
);
44 MOZ_CRASH("Unknown State");
49 LexerTransition
<nsIconDecoder::State
> nsIconDecoder::ReadHeader(
51 // Grab the width and height.
52 uint8_t width
= uint8_t(aData
[0]);
53 uint8_t height
= uint8_t(aData
[1]);
54 SurfaceFormat format
= SurfaceFormat(aData
[2]);
55 bool transform
= bool(aData
[3]);
57 // FIXME(aosmond): On OSX we get the icon in device space and already
58 // premultiplied, so we can't support the surface flags with icons right now.
59 SurfacePipeFlags pipeFlags
= SurfacePipeFlags();
61 if (mCMSMode
== CMSMode::All
) {
62 mTransform
= GetCMSsRGBTransform(format
);
65 if (!(GetSurfaceFlags() & SurfaceFlags::NO_PREMULTIPLY_ALPHA
)) {
66 pipeFlags
|= SurfacePipeFlags::PREMULTIPLY_ALPHA
;
70 // The input is 32bpp, so we expect 4 bytes of data per pixel.
71 mBytesPerRow
= width
* 4;
73 // Post our size to the superclass.
74 PostSize(width
, height
);
77 PostHasTransparency();
79 // If we're doing a metadata decode, we're done.
80 if (IsMetadataDecode()) {
81 return Transition::TerminateSuccess();
84 MOZ_ASSERT(!mImageData
, "Already have a buffer allocated?");
85 Maybe
<SurfacePipe
> pipe
= SurfacePipeFactory::CreateSurfacePipe(
86 this, Size(), OutputSize(), FullFrame(), format
, SurfaceFormat::OS_RGBA
,
87 /* aAnimParams */ Nothing(), mTransform
, pipeFlags
);
89 return Transition::TerminateFailure();
92 mPipe
= std::move(*pipe
);
94 MOZ_ASSERT(mImageData
, "Should have a buffer now");
96 return Transition::To(State::ROW_OF_PIXELS
, mBytesPerRow
);
99 LexerTransition
<nsIconDecoder::State
> nsIconDecoder::ReadRowOfPixels(
100 const char* aData
, size_t aLength
) {
101 MOZ_ASSERT(aLength
% 4 == 0, "Rows should contain a multiple of four bytes");
103 auto result
= mPipe
.WriteBuffer(reinterpret_cast<const uint32_t*>(aData
));
104 MOZ_ASSERT(result
!= WriteState::FAILURE
);
106 Maybe
<SurfaceInvalidRect
> invalidRect
= mPipe
.TakeInvalidRect();
108 PostInvalidation(invalidRect
->mInputSpaceRect
,
109 Some(invalidRect
->mOutputSpaceRect
));
112 return result
== WriteState::FINISHED
113 ? Transition::To(State::FINISH
, 0)
114 : Transition::To(State::ROW_OF_PIXELS
, mBytesPerRow
);
117 LexerTransition
<nsIconDecoder::State
> nsIconDecoder::Finish() {
121 return Transition::TerminateSuccess();
125 } // namespace mozilla