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