SurfacePipe.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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 "SurfacePipe.h"
  6. #include <utility>
  7. #include "mozilla/ClearOnShutdown.h"
  8. #include "mozilla/DebugOnly.h"
  9. #include "Decoder.h"
  10. namespace mozilla {
  11. namespace image {
  12. using namespace gfx;
  13. using std::min;
  14. /* static */ UniquePtr<NullSurfaceSink> NullSurfaceSink::sSingleton;
  15. /* static */ NullSurfaceSink*
  16. NullSurfaceSink::Singleton()
  17. {
  18. if (!sSingleton) {
  19. MOZ_ASSERT(NS_IsMainThread());
  20. sSingleton = MakeUnique<NullSurfaceSink>();
  21. ClearOnShutdown(&sSingleton);
  22. DebugOnly<nsresult> rv = sSingleton->Configure(NullSurfaceConfig { });
  23. MOZ_ASSERT(NS_SUCCEEDED(rv), "Couldn't configure a NullSurfaceSink?");
  24. }
  25. return sSingleton.get();
  26. }
  27. nsresult
  28. NullSurfaceSink::Configure(const NullSurfaceConfig& aConfig)
  29. {
  30. // Note that the choice of uint32_t as the pixel size here is more or less
  31. // arbitrary, since you cannot write to a NullSurfaceSink anyway, but uint32_t
  32. // is a natural choice since most SurfacePipes will be for BGRA/BGRX surfaces.
  33. ConfigureFilter(IntSize(), sizeof(uint32_t));
  34. return NS_OK;
  35. }
  36. Maybe<SurfaceInvalidRect>
  37. AbstractSurfaceSink::TakeInvalidRect()
  38. {
  39. if (mInvalidRect.IsEmpty()) {
  40. return Nothing();
  41. }
  42. SurfaceInvalidRect invalidRect;
  43. invalidRect.mInputSpaceRect = invalidRect.mOutputSpaceRect = mInvalidRect;
  44. // Forget about the invalid rect we're returning.
  45. mInvalidRect = IntRect();
  46. return Some(invalidRect);
  47. }
  48. uint8_t*
  49. AbstractSurfaceSink::DoResetToFirstRow()
  50. {
  51. mRow = 0;
  52. return GetRowPointer();
  53. }
  54. uint8_t*
  55. AbstractSurfaceSink::DoAdvanceRow()
  56. {
  57. if (mRow >= uint32_t(InputSize().height)) {
  58. return nullptr;
  59. }
  60. // If we're vertically flipping the output, we need to flip the invalid rect. Since we're
  61. // dealing with an axis-aligned rect, only the y coordinate needs to change.
  62. int32_t invalidY = mFlipVertically
  63. ? InputSize().height - (mRow + 1)
  64. : mRow;
  65. mInvalidRect.UnionRect(mInvalidRect,
  66. IntRect(0, invalidY, InputSize().width, 1));
  67. mRow = min(uint32_t(InputSize().height), mRow + 1);
  68. return mRow < uint32_t(InputSize().height) ? GetRowPointer()
  69. : nullptr;
  70. }
  71. nsresult
  72. SurfaceSink::Configure(const SurfaceConfig& aConfig)
  73. {
  74. // For non-paletted surfaces, the surface size is just the output size.
  75. IntSize surfaceSize = aConfig.mOutputSize;
  76. // Non-paletted surfaces should not have frame rects, so we just pass
  77. // AllocateFrame() a frame rect which covers the entire surface.
  78. IntRect frameRect(0, 0, surfaceSize.width, surfaceSize.height);
  79. // Allocate the frame.
  80. // XXX(seth): Once every Decoder subclass uses SurfacePipe, we probably want
  81. // to allocate the frame directly here and get rid of Decoder::AllocateFrame
  82. // altogether.
  83. nsresult rv = aConfig.mDecoder->AllocateFrame(surfaceSize,
  84. frameRect,
  85. aConfig.mFormat,
  86. /* aPaletteDepth */ 0,
  87. aConfig.mAnimParams);
  88. if (NS_FAILED(rv)) {
  89. return rv;
  90. }
  91. mImageData = aConfig.mDecoder->mImageData;
  92. mImageDataLength = aConfig.mDecoder->mImageDataLength;
  93. mFlipVertically = aConfig.mFlipVertically;
  94. MOZ_ASSERT(mImageData);
  95. MOZ_ASSERT(mImageDataLength ==
  96. uint32_t(surfaceSize.width * surfaceSize.height * sizeof(uint32_t)));
  97. ConfigureFilter(surfaceSize, sizeof(uint32_t));
  98. return NS_OK;
  99. }
  100. uint8_t*
  101. SurfaceSink::GetRowPointer() const
  102. {
  103. // If we're flipping vertically, reverse the order in which we traverse the
  104. // rows.
  105. uint32_t row = mFlipVertically
  106. ? InputSize().height - (mRow + 1)
  107. : mRow;
  108. uint8_t* rowPtr = mImageData + row * InputSize().width * sizeof(uint32_t);
  109. MOZ_ASSERT(rowPtr >= mImageData);
  110. MOZ_ASSERT(rowPtr < mImageData + mImageDataLength);
  111. MOZ_ASSERT(rowPtr + InputSize().width * sizeof(uint32_t) <=
  112. mImageData + mImageDataLength);
  113. return rowPtr;
  114. }
  115. nsresult
  116. PalettedSurfaceSink::Configure(const PalettedSurfaceConfig& aConfig)
  117. {
  118. // For paletted surfaces, the surface size is the size of the frame rect.
  119. IntSize surfaceSize = aConfig.mFrameRect.Size();
  120. // Allocate the frame.
  121. // XXX(seth): Once every Decoder subclass uses SurfacePipe, we probably want
  122. // to allocate the frame directly here and get rid of Decoder::AllocateFrame
  123. // altogether.
  124. nsresult rv = aConfig.mDecoder->AllocateFrame(aConfig.mOutputSize,
  125. aConfig.mFrameRect,
  126. aConfig.mFormat,
  127. aConfig.mPaletteDepth,
  128. aConfig.mAnimParams);
  129. if (NS_FAILED(rv)) {
  130. return rv;
  131. }
  132. mImageData = aConfig.mDecoder->mImageData;
  133. mImageDataLength = aConfig.mDecoder->mImageDataLength;
  134. mFlipVertically = aConfig.mFlipVertically;
  135. mFrameRect = aConfig.mFrameRect;
  136. MOZ_ASSERT(mImageData);
  137. MOZ_ASSERT(mImageDataLength ==
  138. uint32_t(mFrameRect.width * mFrameRect.height * sizeof(uint8_t)));
  139. ConfigureFilter(surfaceSize, sizeof(uint8_t));
  140. return NS_OK;
  141. }
  142. uint8_t*
  143. PalettedSurfaceSink::GetRowPointer() const
  144. {
  145. // If we're flipping vertically, reverse the order in which we traverse the
  146. // rows.
  147. uint32_t row = mFlipVertically
  148. ? InputSize().height - (mRow + 1)
  149. : mRow;
  150. uint8_t* rowPtr = mImageData + row * InputSize().width * sizeof(uint8_t);
  151. MOZ_ASSERT(rowPtr >= mImageData);
  152. MOZ_ASSERT(rowPtr < mImageData + mImageDataLength);
  153. MOZ_ASSERT(rowPtr + InputSize().width * sizeof(uint8_t) <=
  154. mImageData + mImageDataLength);
  155. return rowPtr;
  156. }
  157. } // namespace image
  158. } // namespace mozilla