SkSwizzler.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /*
  2. * Copyright 2015 Google Inc.
  3. *
  4. * Use of this source code is governed by a BSD-style license that can be
  5. * found in the LICENSE file.
  6. */
  7. #ifndef SkSwizzler_DEFINED
  8. #define SkSwizzler_DEFINED
  9. #include "include/codec/SkCodec.h"
  10. #include "include/core/SkColor.h"
  11. #include "include/core/SkImageInfo.h"
  12. #include "src/codec/SkSampler.h"
  13. class SkSwizzler : public SkSampler {
  14. public:
  15. /**
  16. * Create a new SkSwizzler.
  17. * @param encodedInfo Description of the format of the encoded data.
  18. * @param ctable Unowned pointer to an array of up to 256 colors for an
  19. * index source.
  20. * @param dstInfo Describes the destination.
  21. * @param options Contains partial scanline information and whether the dst is zero-
  22. * initialized.
  23. * @param frame Is non-NULL if the source pixels are part of an image
  24. * frame that is a subset of the full image.
  25. *
  26. * Note that a deeper discussion of partial scanline subsets and image frame
  27. * subsets is below. Currently, we do not support both simultaneously. If
  28. * options->fSubset is non-NULL, frame must be NULL.
  29. *
  30. * @return A new SkSwizzler or nullptr on failure.
  31. */
  32. static std::unique_ptr<SkSwizzler> Make(const SkEncodedInfo& encodedInfo,
  33. const SkPMColor* ctable, const SkImageInfo& dstInfo, const SkCodec::Options&,
  34. const SkIRect* frame = nullptr);
  35. /**
  36. * Create a simplified swizzler that does not need to do format conversion. The swizzler
  37. * only needs to sample and/or subset.
  38. *
  39. * @param srcBPP Bytes per pixel of the source.
  40. * @param dstInfo Describes the destination.
  41. * @param options Contains partial scanline information and whether the dst is zero-
  42. * initialized.
  43. * @return A new SkSwizzler or nullptr on failure.
  44. */
  45. static std::unique_ptr<SkSwizzler> MakeSimple(int srcBPP, const SkImageInfo& dstInfo,
  46. const SkCodec::Options&);
  47. /**
  48. * Swizzle a line. Generally this will be called height times, once
  49. * for each row of source.
  50. * By allowing the caller to pass in the dst pointer, we give the caller
  51. * flexibility to use the swizzler even when the encoded data does not
  52. * store the rows in order. This also improves usability for scaled and
  53. * subset decodes.
  54. * @param dst Where we write the output.
  55. * @param src The next row of the source data.
  56. */
  57. void swizzle(void* dst, const uint8_t* SK_RESTRICT src);
  58. int fillWidth() const override {
  59. return fAllocatedWidth;
  60. }
  61. /**
  62. * If fSampleX > 1, the swizzler is sampling every fSampleX'th pixel and
  63. * discarding the rest.
  64. *
  65. * This getter is currently used by SkBmpStandardCodec for Bmp-in-Ico decodes.
  66. * Ideally, the subclasses of SkCodec would have no knowledge of sampling, but
  67. * this allows us to apply a transparency mask to pixels after swizzling.
  68. */
  69. int sampleX() const { return fSampleX; }
  70. /**
  71. * Returns the actual number of pixels written to destination memory, taking
  72. * scaling, subsetting, and partial frames into account.
  73. */
  74. int swizzleWidth() const { return fSwizzleWidth; }
  75. /**
  76. * Returns the byte offset at which we write to destination memory, taking
  77. * scaling, subsetting, and partial frames into account.
  78. */
  79. size_t swizzleOffsetBytes() const { return fDstOffsetBytes; }
  80. private:
  81. /**
  82. * Method for converting raw data to Skia pixels.
  83. * @param dstRow Row in which to write the resulting pixels.
  84. * @param src Row of src data, in format specified by SrcConfig
  85. * @param dstWidth Width in pixels of the destination
  86. * @param bpp if bitsPerPixel % 8 == 0, deltaSrc is bytesPerPixel
  87. * else, deltaSrc is bitsPerPixel
  88. * @param deltaSrc bpp * sampleX
  89. * @param ctable Colors (used for kIndex source).
  90. * @param offset The offset before the first pixel to sample.
  91. Is in bytes or bits based on what deltaSrc is in.
  92. */
  93. typedef void (*RowProc)(void* SK_RESTRICT dstRow,
  94. const uint8_t* SK_RESTRICT src,
  95. int dstWidth, int bpp, int deltaSrc, int offset,
  96. const SkPMColor ctable[]);
  97. template <RowProc Proc>
  98. static void SkipLeading8888ZerosThen(void* SK_RESTRICT dstRow,
  99. const uint8_t* SK_RESTRICT src,
  100. int dstWidth, int bpp, int deltaSrc, int offset,
  101. const SkPMColor ctable[]);
  102. template <RowProc Proc>
  103. static void SkipLeadingGrayAlphaZerosThen(void* dst, const uint8_t* src, int width, int bpp,
  104. int deltaSrc, int offset, const SkPMColor ctable[]);
  105. // May be NULL. We have not implemented optimized functions for all supported transforms.
  106. const RowProc fFastProc;
  107. // Always non-NULL. Supports sampling.
  108. const RowProc fSlowProc;
  109. // The actual RowProc we are using. This depends on if fFastProc is non-NULL and
  110. // whether or not we are sampling.
  111. RowProc fActualProc;
  112. const SkPMColor* fColorTable; // Unowned pointer
  113. // Subset Swizzles
  114. // There are two types of subset swizzles that we support. We do not
  115. // support both at the same time.
  116. // TODO: If we want to support partial scanlines for gifs (which may
  117. // use frame subsets), we will need to support both subsetting
  118. // modes at the same time.
  119. // (1) Partial Scanlines
  120. // The client only wants to write a subset of the source pixels
  121. // to the destination. This subset is specified to CreateSwizzler
  122. // using options->fSubset. We will store subset information in
  123. // the following fields.
  124. //
  125. // fSrcOffset: The starting pixel of the source.
  126. // fSrcOffsetUnits: Derived from fSrcOffset with two key
  127. // differences:
  128. // (1) This takes the size of source pixels into
  129. // account by multiplying by fSrcBPP. This may
  130. // be measured in bits or bytes depending on
  131. // which is natural for the SrcConfig.
  132. // (2) If we are sampling, this will be larger
  133. // than fSrcOffset * fSrcBPP, since sampling
  134. // implies that we will skip some pixels.
  135. // fDstOffset: Will be zero. There is no destination offset
  136. // for this type of subset.
  137. // fDstOffsetBytes: Will be zero.
  138. // fSrcWidth: The width of the desired subset of source
  139. // pixels, before any sampling is performed.
  140. // fDstWidth: Will be equal to fSrcWidth, since this is also
  141. // calculated before any sampling is performed.
  142. // For this type of subset, the destination width
  143. // matches the desired subset of the source.
  144. // fSwizzleWidth: The actual number of pixels that will be
  145. // written by the RowProc. This is a scaled
  146. // version of fSrcWidth/fDstWidth.
  147. // fAllocatedWidth: Will be equal to fSwizzleWidth. For this type
  148. // of subset, the number of pixels written is the
  149. // same as the actual width of the destination.
  150. // (2) Frame Subset
  151. // The client will decode the entire width of the source into a
  152. // subset of destination memory. This subset is specified to
  153. // CreateSwizzler in the "frame" parameter. We store subset
  154. // information in the following fields.
  155. //
  156. // fSrcOffset: Will be zero. The starting pixel of the source.
  157. // fSrcOffsetUnits: Will only be non-zero if we are sampling,
  158. // since sampling implies that we will skip some
  159. // pixels. Note that this is measured in bits
  160. // or bytes depending on which is natural for
  161. // SrcConfig.
  162. // fDstOffset: First pixel to write in destination.
  163. // fDstOffsetBytes: fDstOffset * fDstBPP.
  164. // fSrcWidth: The entire width of the source pixels, before
  165. // any sampling is performed.
  166. // fDstWidth: The entire width of the destination memory,
  167. // before any sampling is performed.
  168. // fSwizzleWidth: The actual number of pixels that will be
  169. // written by the RowProc. This is a scaled
  170. // version of fSrcWidth.
  171. // fAllocatedWidth: The actual number of pixels in destination
  172. // memory. This is a scaled version of
  173. // fDstWidth.
  174. //
  175. // If we are not subsetting, these fields are more straightforward.
  176. // fSrcOffset = fDstOffet = fDstOffsetBytes = 0
  177. // fSrcOffsetUnits may be non-zero (we will skip the first few pixels when sampling)
  178. // fSrcWidth = fDstWidth = Full original width
  179. // fSwizzleWidth = fAllcoatedWidth = Scaled width (if we are sampling)
  180. const int fSrcOffset;
  181. const int fDstOffset;
  182. int fSrcOffsetUnits;
  183. int fDstOffsetBytes;
  184. const int fSrcWidth;
  185. const int fDstWidth;
  186. int fSwizzleWidth;
  187. int fAllocatedWidth;
  188. int fSampleX; // Step between X samples
  189. const int fSrcBPP; // Bits/bytes per pixel for the SrcConfig
  190. // if bitsPerPixel % 8 == 0
  191. // fBPP is bytesPerPixel
  192. // else
  193. // fBPP is bitsPerPixel
  194. const int fDstBPP; // Bytes per pixel for the destination color type
  195. SkSwizzler(RowProc fastProc, RowProc proc, const SkPMColor* ctable, int srcOffset,
  196. int srcWidth, int dstOffset, int dstWidth, int srcBPP, int dstBPP);
  197. static std::unique_ptr<SkSwizzler> Make(const SkImageInfo& dstInfo, RowProc fastProc,
  198. RowProc proc, const SkPMColor* ctable, int srcBPP, int dstBPP,
  199. const SkCodec::Options& options, const SkIRect* frame);
  200. int onSetSampleX(int) override;
  201. };
  202. #endif // SkSwizzler_DEFINED