nsIconDecoder.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. #include "nsIconDecoder.h"
  6. #include "RasterImage.h"
  7. #include "SurfacePipeFactory.h"
  8. using namespace mozilla::gfx;
  9. namespace mozilla {
  10. namespace image {
  11. static const uint32_t ICON_HEADER_SIZE = 2;
  12. nsIconDecoder::nsIconDecoder(RasterImage* aImage)
  13. : Decoder(aImage)
  14. , mLexer(Transition::To(State::HEADER, ICON_HEADER_SIZE),
  15. Transition::TerminateSuccess())
  16. , mBytesPerRow() // set by ReadHeader()
  17. {
  18. // Nothing to do
  19. }
  20. nsIconDecoder::~nsIconDecoder()
  21. { }
  22. LexerResult
  23. nsIconDecoder::DoDecode(SourceBufferIterator& aIterator, IResumable* aOnResume)
  24. {
  25. MOZ_ASSERT(!HasError(), "Shouldn't call DoDecode after error!");
  26. return mLexer.Lex(aIterator, aOnResume,
  27. [=](State aState, const char* aData, size_t aLength) {
  28. switch (aState) {
  29. case State::HEADER:
  30. return ReadHeader(aData);
  31. case State::ROW_OF_PIXELS:
  32. return ReadRowOfPixels(aData, aLength);
  33. case State::FINISH:
  34. return Finish();
  35. default:
  36. MOZ_CRASH("Unknown State");
  37. }
  38. });
  39. }
  40. LexerTransition<nsIconDecoder::State>
  41. nsIconDecoder::ReadHeader(const char* aData)
  42. {
  43. // Grab the width and height.
  44. uint8_t width = uint8_t(aData[0]);
  45. uint8_t height = uint8_t(aData[1]);
  46. // The input is 32bpp, so we expect 4 bytes of data per pixel.
  47. mBytesPerRow = width * 4;
  48. // Post our size to the superclass.
  49. PostSize(width, height);
  50. // Icons have alpha.
  51. PostHasTransparency();
  52. // If we're doing a metadata decode, we're done.
  53. if (IsMetadataDecode()) {
  54. return Transition::TerminateSuccess();
  55. }
  56. MOZ_ASSERT(!mImageData, "Already have a buffer allocated?");
  57. Maybe<SurfacePipe> pipe =
  58. SurfacePipeFactory::CreateSurfacePipe(this, Size(), OutputSize(),
  59. FullFrame(), SurfaceFormat::B8G8R8A8,
  60. /* aAnimParams */ Nothing(),
  61. SurfacePipeFlags());
  62. if (!pipe) {
  63. return Transition::TerminateFailure();
  64. }
  65. mPipe = Move(*pipe);
  66. MOZ_ASSERT(mImageData, "Should have a buffer now");
  67. return Transition::To(State::ROW_OF_PIXELS, mBytesPerRow);
  68. }
  69. LexerTransition<nsIconDecoder::State>
  70. nsIconDecoder::ReadRowOfPixels(const char* aData, size_t aLength)
  71. {
  72. MOZ_ASSERT(aLength % 4 == 0, "Rows should contain a multiple of four bytes");
  73. auto result = mPipe.WritePixels<uint32_t>([&]() -> NextPixel<uint32_t> {
  74. if (aLength == 0) {
  75. return AsVariant(WriteState::NEED_MORE_DATA); // Done with this row.
  76. }
  77. uint32_t pixel;
  78. memcpy(&pixel, aData, 4);
  79. aData += 4;
  80. aLength -= 4;
  81. return AsVariant(pixel);
  82. });
  83. MOZ_ASSERT(result != WriteState::FAILURE);
  84. Maybe<SurfaceInvalidRect> invalidRect = mPipe.TakeInvalidRect();
  85. if (invalidRect) {
  86. PostInvalidation(invalidRect->mInputSpaceRect,
  87. Some(invalidRect->mOutputSpaceRect));
  88. }
  89. return result == WriteState::FINISHED
  90. ? Transition::To(State::FINISH, 0)
  91. : Transition::To(State::ROW_OF_PIXELS, mBytesPerRow);
  92. }
  93. LexerTransition<nsIconDecoder::State>
  94. nsIconDecoder::Finish()
  95. {
  96. PostFrameStop();
  97. PostDecodeDone();
  98. return Transition::TerminateSuccess();
  99. }
  100. } // namespace image
  101. } // namespace mozilla