blob: 6f6ab6e4aedebbe14149eb2ed8f52f264db28b80 [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.
Leon Scroggins III65f4aea2018-10-24 12:17:22 -040024 * @param options Contains partial scanline information and whether the dst is zero-
25 * initialized.
msarett5af4e0b2015-11-17 11:18:03 -080026 * @param frame Is non-NULL if the source pixels are part of an image
27 * frame that is a subset of the full image.
28 *
29 * Note that a deeper discussion of partial scanline subsets and image frame
30 * subsets is below. Currently, we do not support both simultaneously. If
31 * options->fSubset is non-NULL, frame must be NULL.
32 *
halcanary96fcdcc2015-08-27 07:41:13 -070033 * @return A new SkSwizzler or nullptr on failure.
scroggof24f2242015-03-03 08:59:20 -080034 */
Leon Scroggins III65f4aea2018-10-24 12:17:22 -040035 static std::unique_ptr<SkSwizzler> Make(const SkEncodedInfo& encodedInfo,
36 const SkPMColor* ctable, const SkImageInfo& dstInfo, const SkCodec::Options&,
37 const SkIRect* frame = nullptr);
38
39 /**
40 * Create a simplified swizzler that does not need to do format conversion. The swizzler
41 * only needs to sample and/or subset.
42 *
43 * @param srcBPP Bytes per pixel of the source.
44 * @param dstInfo Describes the destination.
45 * @param options Contains partial scanline information and whether the dst is zero-
46 * initialized.
47 * @return A new SkSwizzler or nullptr on failure.
48 */
49 static std::unique_ptr<SkSwizzler> MakeSimple(int srcBPP, const SkImageInfo& dstInfo,
50 const SkCodec::Options&);
scroggoe7fc14b2015-10-02 13:14:46 -070051
msarett438b2ad2015-04-09 12:43:10 -070052 /**
msarett614aa072015-07-27 15:13:17 -070053 * Swizzle a line. Generally this will be called height times, once
54 * for each row of source.
55 * By allowing the caller to pass in the dst pointer, we give the caller
56 * flexibility to use the swizzler even when the encoded data does not
57 * store the rows in order. This also improves usability for scaled and
58 * subset decodes.
59 * @param dst Where we write the output.
scroggof24f2242015-03-03 08:59:20 -080060 * @param src The next row of the source data.
scroggof24f2242015-03-03 08:59:20 -080061 */
msaretta4970dc2016-01-11 07:23:23 -080062 void swizzle(void* dst, const uint8_t* SK_RESTRICT src);
msarett3c309db2015-04-10 14:36:48 -070063
Leon Scroggins IIIa6161b12018-10-18 14:45:26 -040064 int fillWidth() const override {
65 return fAllocatedWidth;
msarette6dd0042015-10-09 11:07:34 -070066 }
67
msarettbe8216a2015-12-04 08:00:50 -080068 /**
69 * If fSampleX > 1, the swizzler is sampling every fSampleX'th pixel and
70 * discarding the rest.
71 *
72 * This getter is currently used by SkBmpStandardCodec for Bmp-in-Ico decodes.
73 * Ideally, the subclasses of SkCodec would have no knowledge of sampling, but
74 * this allows us to apply a transparency mask to pixels after swizzling.
75 */
76 int sampleX() const { return fSampleX; }
77
msarett35bb74b2016-08-22 07:41:28 -070078 /**
79 * Returns the actual number of pixels written to destination memory, taking
80 * scaling, subsetting, and partial frames into account.
81 */
82 int swizzleWidth() const { return fSwizzleWidth; }
83
scroggo19b91532016-10-24 09:03:26 -070084 /**
85 * Returns the byte offset at which we write to destination memory, taking
86 * scaling, subsetting, and partial frames into account.
87 */
88 size_t swizzleOffsetBytes() const { return fDstOffsetBytes; }
89
scroggof24f2242015-03-03 08:59:20 -080090private:
msarett74114382015-03-16 11:55:18 -070091
scroggof24f2242015-03-03 08:59:20 -080092 /**
93 * Method for converting raw data to Skia pixels.
94 * @param dstRow Row in which to write the resulting pixels.
95 * @param src Row of src data, in format specified by SrcConfig
emmaleer8f4ba762015-08-14 07:44:46 -070096 * @param dstWidth Width in pixels of the destination
msarett5406d6f2015-08-31 06:55:13 -070097 * @param bpp if bitsPerPixel % 8 == 0, deltaSrc is bytesPerPixel
98 * else, deltaSrc is bitsPerPixel
99 * @param deltaSrc bpp * sampleX
scroggof24f2242015-03-03 08:59:20 -0800100 * @param ctable Colors (used for kIndex source).
emmaleer8f4ba762015-08-14 07:44:46 -0700101 * @param offset The offset before the first pixel to sample.
102 Is in bytes or bits based on what deltaSrc is in.
scroggof24f2242015-03-03 08:59:20 -0800103 */
msaretta4970dc2016-01-11 07:23:23 -0800104 typedef void (*RowProc)(void* SK_RESTRICT dstRow,
105 const uint8_t* SK_RESTRICT src,
106 int dstWidth, int bpp, int deltaSrc, int offset,
107 const SkPMColor ctable[]);
scroggof24f2242015-03-03 08:59:20 -0800108
mtklein8604ca22016-01-11 13:13:55 -0800109 template <RowProc Proc>
110 static void SkipLeading8888ZerosThen(void* SK_RESTRICT dstRow,
111 const uint8_t* SK_RESTRICT src,
112 int dstWidth, int bpp, int deltaSrc, int offset,
113 const SkPMColor ctable[]);
114
msarett93e613d2016-02-03 10:44:46 -0800115 template <RowProc Proc>
116 static void SkipLeadingGrayAlphaZerosThen(void* dst, const uint8_t* src, int width, int bpp,
117 int deltaSrc, int offset, const SkPMColor ctable[]);
118
msarett19032f72016-01-21 09:59:38 -0800119 // May be NULL. We have not implemented optimized functions for all supported transforms.
120 const RowProc fFastProc;
121 // Always non-NULL. Supports sampling.
122 const RowProc fSlowProc;
123 // The actual RowProc we are using. This depends on if fFastProc is non-NULL and
124 // whether or not we are sampling.
125 RowProc fActualProc;
126
msarett74114382015-03-16 11:55:18 -0700127 const SkPMColor* fColorTable; // Unowned pointer
msarett5af4e0b2015-11-17 11:18:03 -0800128
129 // Subset Swizzles
130 // There are two types of subset swizzles that we support. We do not
131 // support both at the same time.
132 // TODO: If we want to support partial scanlines for gifs (which may
133 // use frame subsets), we will need to support both subsetting
134 // modes at the same time.
135 // (1) Partial Scanlines
136 // The client only wants to write a subset of the source pixels
137 // to the destination. This subset is specified to CreateSwizzler
138 // using options->fSubset. We will store subset information in
139 // the following fields.
140 //
141 // fSrcOffset: The starting pixel of the source.
142 // fSrcOffsetUnits: Derived from fSrcOffset with two key
143 // differences:
144 // (1) This takes the size of source pixels into
145 // account by multiplying by fSrcBPP. This may
146 // be measured in bits or bytes depending on
147 // which is natural for the SrcConfig.
148 // (2) If we are sampling, this will be larger
149 // than fSrcOffset * fSrcBPP, since sampling
150 // implies that we will skip some pixels.
151 // fDstOffset: Will be zero. There is no destination offset
152 // for this type of subset.
153 // fDstOffsetBytes: Will be zero.
154 // fSrcWidth: The width of the desired subset of source
155 // pixels, before any sampling is performed.
156 // fDstWidth: Will be equal to fSrcWidth, since this is also
157 // calculated before any sampling is performed.
158 // For this type of subset, the destination width
159 // matches the desired subset of the source.
160 // fSwizzleWidth: The actual number of pixels that will be
161 // written by the RowProc. This is a scaled
162 // version of fSrcWidth/fDstWidth.
163 // fAllocatedWidth: Will be equal to fSwizzleWidth. For this type
164 // of subset, the number of pixels written is the
165 // same as the actual width of the destination.
166 // (2) Frame Subset
167 // The client will decode the entire width of the source into a
168 // subset of destination memory. This subset is specified to
169 // CreateSwizzler in the "frame" parameter. We store subset
170 // information in the following fields.
171 //
172 // fSrcOffset: Will be zero. The starting pixel of the source.
173 // fSrcOffsetUnits: Will only be non-zero if we are sampling,
174 // since sampling implies that we will skip some
175 // pixels. Note that this is measured in bits
176 // or bytes depending on which is natural for
177 // SrcConfig.
178 // fDstOffset: First pixel to write in destination.
179 // fDstOffsetBytes: fDstOffset * fDstBPP.
180 // fSrcWidth: The entire width of the source pixels, before
181 // any sampling is performed.
182 // fDstWidth: The entire width of the destination memory,
183 // before any sampling is performed.
184 // fSwizzleWidth: The actual number of pixels that will be
185 // written by the RowProc. This is a scaled
186 // version of fSrcWidth.
187 // fAllocatedWidth: The actual number of pixels in destination
188 // memory. This is a scaled version of
189 // fDstWidth.
190 //
191 // If we are not subsetting, these fields are more straightforward.
192 // fSrcOffset = fDstOffet = fDstOffsetBytes = 0
193 // fSrcOffsetUnits may be non-zero (we will skip the first few pixels when sampling)
194 // fSrcWidth = fDstWidth = Full original width
195 // fSwizzleWidth = fAllcoatedWidth = Scaled width (if we are sampling)
196 const int fSrcOffset;
197 const int fDstOffset;
198 int fSrcOffsetUnits;
199 int fDstOffsetBytes;
200 const int fSrcWidth;
201 const int fDstWidth;
202 int fSwizzleWidth;
203 int fAllocatedWidth;
204
205 int fSampleX; // Step between X samples
206 const int fSrcBPP; // Bits/bytes per pixel for the SrcConfig
207 // if bitsPerPixel % 8 == 0
msarettfdb47572015-10-13 12:50:14 -0700208 // fBPP is bytesPerPixel
209 // else
210 // fBPP is bitsPerPixel
msarett5af4e0b2015-11-17 11:18:03 -0800211 const int fDstBPP; // Bytes per pixel for the destination color type
scroggof24f2242015-03-03 08:59:20 -0800212
msaretta51e7782016-01-12 06:51:11 -0800213 SkSwizzler(RowProc fastProc, RowProc proc, const SkPMColor* ctable, int srcOffset,
214 int srcWidth, int dstOffset, int dstWidth, int srcBPP, int dstBPP);
Leon Scroggins III65f4aea2018-10-24 12:17:22 -0400215 static std::unique_ptr<SkSwizzler> Make(const SkImageInfo& dstInfo, RowProc fastProc,
216 RowProc proc, const SkPMColor* ctable, int srcBPP, int dstBPP,
217 const SkCodec::Options& options, const SkIRect* frame);
scroggoe7fc14b2015-10-02 13:14:46 -0700218
219 int onSetSampleX(int) override;
msarette6dd0042015-10-09 11:07:34 -0700220
scroggof24f2242015-03-03 08:59:20 -0800221};
222#endif // SkSwizzler_DEFINED