blob: b172e458ccb15365d3649269ecba3fb540a32606 [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
Leon Scroggins IIIa6161b12018-10-18 14:45:26 -040056 int fillWidth() const override {
57 return fAllocatedWidth;
msarette6dd0042015-10-09 11:07:34 -070058 }
59
msarettbe8216a2015-12-04 08:00:50 -080060 /**
61 * If fSampleX > 1, the swizzler is sampling every fSampleX'th pixel and
62 * discarding the rest.
63 *
64 * This getter is currently used by SkBmpStandardCodec for Bmp-in-Ico decodes.
65 * Ideally, the subclasses of SkCodec would have no knowledge of sampling, but
66 * this allows us to apply a transparency mask to pixels after swizzling.
67 */
68 int sampleX() const { return fSampleX; }
69
msarett35bb74b2016-08-22 07:41:28 -070070 /**
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
scroggo19b91532016-10-24 09:03:26 -070076 /**
77 * Returns the byte offset at which we write to destination memory, taking
78 * scaling, subsetting, and partial frames into account.
79 */
80 size_t swizzleOffsetBytes() const { return fDstOffsetBytes; }
81
scroggof24f2242015-03-03 08:59:20 -080082private:
msarett74114382015-03-16 11:55:18 -070083
scroggof24f2242015-03-03 08:59:20 -080084 /**
85 * Method for converting raw data to Skia pixels.
86 * @param dstRow Row in which to write the resulting pixels.
87 * @param src Row of src data, in format specified by SrcConfig
emmaleer8f4ba762015-08-14 07:44:46 -070088 * @param dstWidth Width in pixels of the destination
msarett5406d6f2015-08-31 06:55:13 -070089 * @param bpp if bitsPerPixel % 8 == 0, deltaSrc is bytesPerPixel
90 * else, deltaSrc is bitsPerPixel
91 * @param deltaSrc bpp * sampleX
scroggof24f2242015-03-03 08:59:20 -080092 * @param ctable Colors (used for kIndex source).
emmaleer8f4ba762015-08-14 07:44:46 -070093 * @param offset The offset before the first pixel to sample.
94 Is in bytes or bits based on what deltaSrc is in.
scroggof24f2242015-03-03 08:59:20 -080095 */
msaretta4970dc2016-01-11 07:23:23 -080096 typedef void (*RowProc)(void* SK_RESTRICT dstRow,
97 const uint8_t* SK_RESTRICT src,
98 int dstWidth, int bpp, int deltaSrc, int offset,
99 const SkPMColor ctable[]);
scroggof24f2242015-03-03 08:59:20 -0800100
mtklein8604ca22016-01-11 13:13:55 -0800101 template <RowProc Proc>
102 static void SkipLeading8888ZerosThen(void* SK_RESTRICT dstRow,
103 const uint8_t* SK_RESTRICT src,
104 int dstWidth, int bpp, int deltaSrc, int offset,
105 const SkPMColor ctable[]);
106
msarett93e613d2016-02-03 10:44:46 -0800107 template <RowProc Proc>
108 static void SkipLeadingGrayAlphaZerosThen(void* dst, const uint8_t* src, int width, int bpp,
109 int deltaSrc, int offset, const SkPMColor ctable[]);
110
msarett19032f72016-01-21 09:59:38 -0800111 // May be NULL. We have not implemented optimized functions for all supported transforms.
112 const RowProc fFastProc;
113 // Always non-NULL. Supports sampling.
114 const RowProc fSlowProc;
115 // The actual RowProc we are using. This depends on if fFastProc is non-NULL and
116 // whether or not we are sampling.
117 RowProc fActualProc;
118
msarett74114382015-03-16 11:55:18 -0700119 const SkPMColor* fColorTable; // Unowned pointer
msarett5af4e0b2015-11-17 11:18:03 -0800120
121 // Subset Swizzles
122 // There are two types of subset swizzles that we support. We do not
123 // support both at the same time.
124 // TODO: If we want to support partial scanlines for gifs (which may
125 // use frame subsets), we will need to support both subsetting
126 // modes at the same time.
127 // (1) Partial Scanlines
128 // The client only wants to write a subset of the source pixels
129 // to the destination. This subset is specified to CreateSwizzler
130 // using options->fSubset. We will store subset information in
131 // the following fields.
132 //
133 // fSrcOffset: The starting pixel of the source.
134 // fSrcOffsetUnits: Derived from fSrcOffset with two key
135 // differences:
136 // (1) This takes the size of source pixels into
137 // account by multiplying by fSrcBPP. This may
138 // be measured in bits or bytes depending on
139 // which is natural for the SrcConfig.
140 // (2) If we are sampling, this will be larger
141 // than fSrcOffset * fSrcBPP, since sampling
142 // implies that we will skip some pixels.
143 // fDstOffset: Will be zero. There is no destination offset
144 // for this type of subset.
145 // fDstOffsetBytes: Will be zero.
146 // fSrcWidth: The width of the desired subset of source
147 // pixels, before any sampling is performed.
148 // fDstWidth: Will be equal to fSrcWidth, since this is also
149 // calculated before any sampling is performed.
150 // For this type of subset, the destination width
151 // matches the desired subset of the source.
152 // fSwizzleWidth: The actual number of pixels that will be
153 // written by the RowProc. This is a scaled
154 // version of fSrcWidth/fDstWidth.
155 // fAllocatedWidth: Will be equal to fSwizzleWidth. For this type
156 // of subset, the number of pixels written is the
157 // same as the actual width of the destination.
158 // (2) Frame Subset
159 // The client will decode the entire width of the source into a
160 // subset of destination memory. This subset is specified to
161 // CreateSwizzler in the "frame" parameter. We store subset
162 // information in the following fields.
163 //
164 // fSrcOffset: Will be zero. The starting pixel of the source.
165 // fSrcOffsetUnits: Will only be non-zero if we are sampling,
166 // since sampling implies that we will skip some
167 // pixels. Note that this is measured in bits
168 // or bytes depending on which is natural for
169 // SrcConfig.
170 // fDstOffset: First pixel to write in destination.
171 // fDstOffsetBytes: fDstOffset * fDstBPP.
172 // fSrcWidth: The entire width of the source pixels, before
173 // any sampling is performed.
174 // fDstWidth: The entire width of the destination memory,
175 // before any sampling is performed.
176 // fSwizzleWidth: The actual number of pixels that will be
177 // written by the RowProc. This is a scaled
178 // version of fSrcWidth.
179 // fAllocatedWidth: The actual number of pixels in destination
180 // memory. This is a scaled version of
181 // fDstWidth.
182 //
183 // If we are not subsetting, these fields are more straightforward.
184 // fSrcOffset = fDstOffet = fDstOffsetBytes = 0
185 // fSrcOffsetUnits may be non-zero (we will skip the first few pixels when sampling)
186 // fSrcWidth = fDstWidth = Full original width
187 // fSwizzleWidth = fAllcoatedWidth = Scaled width (if we are sampling)
188 const int fSrcOffset;
189 const int fDstOffset;
190 int fSrcOffsetUnits;
191 int fDstOffsetBytes;
192 const int fSrcWidth;
193 const int fDstWidth;
194 int fSwizzleWidth;
195 int fAllocatedWidth;
196
197 int fSampleX; // Step between X samples
198 const int fSrcBPP; // Bits/bytes per pixel for the SrcConfig
199 // if bitsPerPixel % 8 == 0
msarettfdb47572015-10-13 12:50:14 -0700200 // fBPP is bytesPerPixel
201 // else
202 // fBPP is bitsPerPixel
msarett5af4e0b2015-11-17 11:18:03 -0800203 const int fDstBPP; // Bytes per pixel for the destination color type
scroggof24f2242015-03-03 08:59:20 -0800204
msaretta51e7782016-01-12 06:51:11 -0800205 SkSwizzler(RowProc fastProc, RowProc proc, const SkPMColor* ctable, int srcOffset,
206 int srcWidth, int dstOffset, int dstWidth, int srcBPP, int dstBPP);
scroggoe7fc14b2015-10-02 13:14:46 -0700207
208 int onSetSampleX(int) override;
msarette6dd0042015-10-09 11:07:34 -0700209
scroggof24f2242015-03-03 08:59:20 -0800210};
211#endif // SkSwizzler_DEFINED