blob: ebaed7ea33fe77e3e316159ed8d4d0acc5466349 [file] [log] [blame]
scroggof24f2242015-03-03 08:59:20 -08001/*
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
scroggo95526622015-03-17 05:02:17 -070011#include "SkCodec.h"
scroggof24f2242015-03-03 08:59:20 -080012#include "SkColor.h"
13#include "SkImageInfo.h"
scroggoe7fc14b2015-10-02 13:14:46 -070014#include "SkSampler.h"
scroggof24f2242015-03-03 08:59:20 -080015
scroggoe7fc14b2015-10-02 13:14:46 -070016class SkSwizzler : public SkSampler {
scroggof24f2242015-03-03 08:59:20 -080017public:
18 /**
scroggof24f2242015-03-03 08:59:20 -080019 * Create a new SkSwizzler.
msaretta45a6682016-04-22 13:18:37 -070020 * @param encodedInfo Description of the format of the encoded data.
scroggoe7fc14b2015-10-02 13:14:46 -070021 * @param ctable Unowned pointer to an array of up to 256 colors for an
22 * index source.
23 * @param dstInfo Describes the destination.
msarettfdb47572015-10-13 12:50:14 -070024 * @param options Indicates if dst is zero-initialized. The
scroggoe7fc14b2015-10-02 13:14:46 -070025 * implementation may choose to skip writing zeroes
scroggo95526622015-03-17 05:02:17 -070026 * if set to kYes_ZeroInitialized.
msarett5af4e0b2015-11-17 11:18:03 -080027 * 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 Sarett9bf39c22016-12-13 13:29:54 -050030 * @param skipFormatConversion Indicates that we should skip format conversion.
31 * The swizzler only needs to sample and/or subset.
msarett5af4e0b2015-11-17 11:18:03 -080032 *
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 *
halcanary96fcdcc2015-08-27 07:41:13 -070037 * @return A new SkSwizzler or nullptr on failure.
scroggof24f2242015-03-03 08:59:20 -080038 */
msaretta45a6682016-04-22 13:18:37 -070039 static SkSwizzler* CreateSwizzler(const SkEncodedInfo& encodedInfo, const SkPMColor* ctable,
msarett5af4e0b2015-11-17 11:18:03 -080040 const SkImageInfo& dstInfo, const SkCodec::Options&,
Matt Sarett9bf39c22016-12-13 13:29:54 -050041 const SkIRect* frame = nullptr,
42 bool skipFormatConversion = false);
scroggoe7fc14b2015-10-02 13:14:46 -070043
msarett438b2ad2015-04-09 12:43:10 -070044 /**
msarett614aa072015-07-27 15:13:17 -070045 * 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.
scroggof24f2242015-03-03 08:59:20 -080052 * @param src The next row of the source data.
scroggof24f2242015-03-03 08:59:20 -080053 */
msaretta4970dc2016-01-11 07:23:23 -080054 void swizzle(void* dst, const uint8_t* SK_RESTRICT src);
msarett3c309db2015-04-10 14:36:48 -070055
msarette6dd0042015-10-09 11:07:34 -070056 /**
57 * Implement fill using a custom width.
58 */
msarettf7eb6fc2016-09-13 09:04:11 -070059 void fill(const SkImageInfo& info, void* dst, size_t rowBytes, uint64_t colorOrIndex,
msarette6dd0042015-10-09 11:07:34 -070060 SkCodec::ZeroInitialized zeroInit) override {
msarett5af4e0b2015-11-17 11:18:03 -080061 const SkImageInfo fillInfo = info.makeWH(fAllocatedWidth, info.height());
msarette6dd0042015-10-09 11:07:34 -070062 SkSampler::Fill(fillInfo, dst, rowBytes, colorOrIndex, zeroInit);
63 }
64
msarettbe8216a2015-12-04 08:00:50 -080065 /**
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
msarett35bb74b2016-08-22 07:41:28 -070075 /**
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
scroggo19b91532016-10-24 09:03:26 -070081 /**
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
scroggof24f2242015-03-03 08:59:20 -080087private:
msarett74114382015-03-16 11:55:18 -070088
scroggof24f2242015-03-03 08:59:20 -080089 /**
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
emmaleer8f4ba762015-08-14 07:44:46 -070093 * @param dstWidth Width in pixels of the destination
msarett5406d6f2015-08-31 06:55:13 -070094 * @param bpp if bitsPerPixel % 8 == 0, deltaSrc is bytesPerPixel
95 * else, deltaSrc is bitsPerPixel
96 * @param deltaSrc bpp * sampleX
scroggof24f2242015-03-03 08:59:20 -080097 * @param ctable Colors (used for kIndex source).
emmaleer8f4ba762015-08-14 07:44:46 -070098 * @param offset The offset before the first pixel to sample.
99 Is in bytes or bits based on what deltaSrc is in.
scroggof24f2242015-03-03 08:59:20 -0800100 */
msaretta4970dc2016-01-11 07:23:23 -0800101 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[]);
scroggof24f2242015-03-03 08:59:20 -0800105
mtklein8604ca22016-01-11 13:13:55 -0800106 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
msarett93e613d2016-02-03 10:44:46 -0800112 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
msarett19032f72016-01-21 09:59:38 -0800116 // 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
msarett74114382015-03-16 11:55:18 -0700124 const SkPMColor* fColorTable; // Unowned pointer
msarett5af4e0b2015-11-17 11:18:03 -0800125
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
msarettfdb47572015-10-13 12:50:14 -0700205 // fBPP is bytesPerPixel
206 // else
207 // fBPP is bitsPerPixel
msarett5af4e0b2015-11-17 11:18:03 -0800208 const int fDstBPP; // Bytes per pixel for the destination color type
scroggof24f2242015-03-03 08:59:20 -0800209
msaretta51e7782016-01-12 06:51:11 -0800210 SkSwizzler(RowProc fastProc, RowProc proc, const SkPMColor* ctable, int srcOffset,
211 int srcWidth, int dstOffset, int dstWidth, int srcBPP, int dstBPP);
scroggoe7fc14b2015-10-02 13:14:46 -0700212
213 int onSetSampleX(int) override;
msarette6dd0042015-10-09 11:07:34 -0700214
scroggof24f2242015-03-03 08:59:20 -0800215};
216#endif // SkSwizzler_DEFINED