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