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/. */
6 
7 #include "nsIconDecoder.h"
8 #include "RasterImage.h"
9 #include "SurfacePipeFactory.h"
10 #include "gfxPlatform.h"
11 
12 using namespace mozilla::gfx;
13 
14 namespace mozilla {
15 namespace image {
16 
17 static const uint32_t ICON_HEADER_SIZE = 4;
18 
nsIconDecoder(RasterImage * aImage)19 nsIconDecoder::nsIconDecoder(RasterImage* aImage)
20     : Decoder(aImage),
21       mLexer(Transition::To(State::HEADER, ICON_HEADER_SIZE),
22              Transition::TerminateSuccess()),
23       mBytesPerRow()  // set by ReadHeader()
24 {
25   // Nothing to do
26 }
27 
~nsIconDecoder()28 nsIconDecoder::~nsIconDecoder() {}
29 
DoDecode(SourceBufferIterator & aIterator,IResumable * aOnResume)30 LexerResult nsIconDecoder::DoDecode(SourceBufferIterator& aIterator,
31                                     IResumable* aOnResume) {
32   MOZ_ASSERT(!HasError(), "Shouldn't call DoDecode after error!");
33 
34   return mLexer.Lex(aIterator, aOnResume,
35                     [=](State aState, const char* aData, size_t aLength) {
36                       switch (aState) {
37                         case State::HEADER:
38                           return ReadHeader(aData);
39                         case State::ROW_OF_PIXELS:
40                           return ReadRowOfPixels(aData, aLength);
41                         case State::FINISH:
42                           return Finish();
43                         default:
44                           MOZ_CRASH("Unknown State");
45                       }
46                     });
47 }
48 
ReadHeader(const char * aData)49 LexerTransition<nsIconDecoder::State> nsIconDecoder::ReadHeader(
50     const char* aData) {
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]);
56 
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();
60   if (transform) {
61     if (mCMSMode == CMSMode::All) {
62       mTransform = GetCMSsRGBTransform(format);
63     }
64 
65     if (!(GetSurfaceFlags() & SurfaceFlags::NO_PREMULTIPLY_ALPHA)) {
66       pipeFlags |= SurfacePipeFlags::PREMULTIPLY_ALPHA;
67     }
68   }
69 
70   // The input is 32bpp, so we expect 4 bytes of data per pixel.
71   mBytesPerRow = width * 4;
72 
73   // Post our size to the superclass.
74   PostSize(width, height);
75 
76   // Icons have alpha.
77   PostHasTransparency();
78 
79   // If we're doing a metadata decode, we're done.
80   if (IsMetadataDecode()) {
81     return Transition::TerminateSuccess();
82   }
83 
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);
88   if (!pipe) {
89     return Transition::TerminateFailure();
90   }
91 
92   mPipe = std::move(*pipe);
93 
94   MOZ_ASSERT(mImageData, "Should have a buffer now");
95 
96   return Transition::To(State::ROW_OF_PIXELS, mBytesPerRow);
97 }
98 
ReadRowOfPixels(const char * aData,size_t aLength)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");
102 
103   auto result = mPipe.WriteBuffer(reinterpret_cast<const uint32_t*>(aData));
104   MOZ_ASSERT(result != WriteState::FAILURE);
105 
106   Maybe<SurfaceInvalidRect> invalidRect = mPipe.TakeInvalidRect();
107   if (invalidRect) {
108     PostInvalidation(invalidRect->mInputSpaceRect,
109                      Some(invalidRect->mOutputSpaceRect));
110   }
111 
112   return result == WriteState::FINISHED
113              ? Transition::To(State::FINISH, 0)
114              : Transition::To(State::ROW_OF_PIXELS, mBytesPerRow);
115 }
116 
Finish()117 LexerTransition<nsIconDecoder::State> nsIconDecoder::Finish() {
118   PostFrameStop();
119   PostDecodeDone();
120 
121   return Transition::TerminateSuccess();
122 }
123 
124 }  // namespace image
125 }  // namespace mozilla
126