blob: c1692881dc28bb8c6f0b9ba1de27218a6fb38560 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001/*
2 * Copyright 2011 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 */
reed04617132014-08-21 09:46:49 -07007
8#include "SkBitmapCache.h"
reed64045422015-06-04 06:31:31 -07009#include "SkBitmapController.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000010#include "SkBitmapProcState.h"
11#include "SkColorPriv.h"
12#include "SkFilterProc.h"
13#include "SkPaint.h"
14#include "SkShader.h" // for tilemodes
digit@google.com3ada0ef2012-08-13 14:06:34 +000015#include "SkUtilsArm.h"
humper@google.com138ebc32013-07-19 20:20:04 +000016#include "SkBitmapScaler.h"
reed@google.comd94697c2013-07-24 14:31:33 +000017#include "SkMipMap.h"
reed@google.comcee9dcb2013-09-13 16:04:49 +000018#include "SkPixelRef.h"
commit-bot@chromium.orgf4491562014-05-28 17:30:02 +000019#include "SkImageEncoder.h"
reed011f39a2014-08-28 13:35:23 -070020#include "SkResourceCache.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000021
digit@google.com3ada0ef2012-08-13 14:06:34 +000022#if !SK_ARM_NEON_IS_NONE
23// These are defined in src/opts/SkBitmapProcState_arm_neon.cpp
digit@google.com3ada0ef2012-08-13 14:06:34 +000024extern const SkBitmapProcState::SampleProc32 gSkBitmapProcStateSample32_neon[];
25extern void S16_D16_filter_DX_neon(const SkBitmapProcState&, const uint32_t*, int, uint16_t*);
herbc7a784c2015-12-18 09:52:15 -080026extern void Clamp_S16_D16_filter_DX_shaderproc_neon(const void *, int, int, uint16_t*, int);
27extern void Repeat_S16_D16_filter_DX_shaderproc_neon(const void *, int, int, uint16_t*, int);
digit@google.com3ada0ef2012-08-13 14:06:34 +000028extern void SI8_opaque_D32_filter_DX_neon(const SkBitmapProcState&, const uint32_t*, int, SkPMColor*);
herbc7a784c2015-12-18 09:52:15 -080029extern void SI8_opaque_D32_filter_DX_shaderproc_neon(const void *, int, int, uint32_t*, int);
30extern void Clamp_SI8_opaque_D32_filter_DX_shaderproc_neon(const void*, int, int, uint32_t*, int);
digit@google.com3ada0ef2012-08-13 14:06:34 +000031#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +000032
herbc7a784c2015-12-18 09:52:15 -080033extern void Clamp_S32_opaque_D32_nofilter_DX_shaderproc(const void*, int, int, uint32_t*, int);
mtklein14e4d392014-10-23 14:35:01 -070034
digit@google.com3ada0ef2012-08-13 14:06:34 +000035#define NAME_WRAP(x) x
36#include "SkBitmapProcState_filter.h"
37#include "SkBitmapProcState_procs.h"
reed@android.comb577b412009-10-27 17:49:32 +000038
reed05a56472016-03-02 09:49:02 -080039SkBitmapProcInfo::SkBitmapProcInfo(const SkBitmapProvider& provider,
40 SkShader::TileMode tmx, SkShader::TileMode tmy)
reed7a4d8472015-09-15 13:33:58 -070041 : fProvider(provider)
reed05a56472016-03-02 09:49:02 -080042 , fTileModeX(tmx)
43 , fTileModeY(tmy)
reed7a4d8472015-09-15 13:33:58 -070044 , fBMState(nullptr)
reed05a56472016-03-02 09:49:02 -080045{}
reed7a4d8472015-09-15 13:33:58 -070046
reed05a56472016-03-02 09:49:02 -080047SkBitmapProcInfo::SkBitmapProcInfo(const SkBitmap& bm,
48 SkShader::TileMode tmx, SkShader::TileMode tmy)
reed7a4d8472015-09-15 13:33:58 -070049 : fProvider(SkBitmapProvider(bm))
reed05a56472016-03-02 09:49:02 -080050 , fTileModeX(tmx)
51 , fTileModeY(tmy)
reed7a4d8472015-09-15 13:33:58 -070052 , fBMState(nullptr)
reed05a56472016-03-02 09:49:02 -080053{}
reed64045422015-06-04 06:31:31 -070054
reed05a56472016-03-02 09:49:02 -080055SkBitmapProcInfo::~SkBitmapProcInfo() {
reed64045422015-06-04 06:31:31 -070056 SkInPlaceDeleteCheck(fBMState, fBMStateStorage.get());
57}
58
reed@android.coma44b4cc2009-07-16 02:03:58 +000059///////////////////////////////////////////////////////////////////////////////
60
reed@google.comee056a82013-04-18 15:33:27 +000061// true iff the matrix contains, at most, scale and translate elements
62static bool matrix_only_scale_translate(const SkMatrix& m) {
63 return m.getType() <= (SkMatrix::kScale_Mask | SkMatrix::kTranslate_Mask);
64}
65
reed@google.comc0e88e02012-10-17 21:11:56 +000066/**
67 * For the purposes of drawing bitmaps, if a matrix is "almost" translate
68 * go ahead and treat it as if it were, so that subsequent code can go fast.
69 */
reedad7ae6c2015-06-04 14:12:25 -070070static bool just_trans_clamp(const SkMatrix& matrix, const SkPixmap& pixmap) {
reed@google.comee056a82013-04-18 15:33:27 +000071 SkASSERT(matrix_only_scale_translate(matrix));
reed@google.comc0e88e02012-10-17 21:11:56 +000072
reed@google.comee056a82013-04-18 15:33:27 +000073 if (matrix.getType() & SkMatrix::kScale_Mask) {
reedad7ae6c2015-06-04 14:12:25 -070074 SkRect dst;
75 SkRect src = SkRect::Make(pixmap.bounds());
reed@google.comf707adc2013-04-18 15:37:14 +000076
77 // Can't call mapRect(), since that will fix up inverted rectangles,
78 // e.g. when scale is negative, and we don't want to return true for
79 // those.
80 matrix.mapPoints(SkTCast<SkPoint*>(&dst),
81 SkTCast<const SkPoint*>(&src),
82 2);
reed@google.comee056a82013-04-18 15:33:27 +000083
84 // Now round all 4 edges to device space, and then compare the device
85 // width/height to the original. Note: we must map all 4 and subtract
86 // rather than map the "width" and compare, since we care about the
87 // phase (in pixel space) that any translate in the matrix might impart.
88 SkIRect idst;
89 dst.round(&idst);
reedad7ae6c2015-06-04 14:12:25 -070090 return idst.width() == pixmap.width() && idst.height() == pixmap.height();
reed@google.comc0e88e02012-10-17 21:11:56 +000091 }
92 // if we got here, we're either kTranslate_Mask or identity
93 return true;
94}
95
96static bool just_trans_general(const SkMatrix& matrix) {
reed@google.comee056a82013-04-18 15:33:27 +000097 SkASSERT(matrix_only_scale_translate(matrix));
reed@google.comc0e88e02012-10-17 21:11:56 +000098
reed@google.comee056a82013-04-18 15:33:27 +000099 if (matrix.getType() & SkMatrix::kScale_Mask) {
reed@google.comc0e88e02012-10-17 21:11:56 +0000100 const SkScalar tol = SK_Scalar1 / 32768;
skia.committer@gmail.com989a95e2012-10-18 02:01:23 +0000101
reed@google.comc0e88e02012-10-17 21:11:56 +0000102 if (!SkScalarNearlyZero(matrix[SkMatrix::kMScaleX] - SK_Scalar1, tol)) {
103 return false;
104 }
105 if (!SkScalarNearlyZero(matrix[SkMatrix::kMScaleY] - SK_Scalar1, tol)) {
106 return false;
107 }
108 }
109 // if we got here, treat us as either kTranslate_Mask or identity
110 return true;
111}
112
reed@android.com8a1c16f2008-12-17 15:59:43 +0000113static bool valid_for_filtering(unsigned dimension) {
114 // for filtering, width and height must fit in 14bits, since we use steal
115 // 2 bits from each to store our 4bit subpixel data
116 return (dimension & ~0x3FFF) == 0;
117}
118
reed05a56472016-03-02 09:49:02 -0800119bool SkBitmapProcInfo::init(const SkMatrix& inv, const SkPaint& paint) {
reed2252eda2016-01-20 11:04:40 -0800120 const int origW = fProvider.info().width();
121 const int origH = fProvider.info().height();
reed05a56472016-03-02 09:49:02 -0800122
123 fPixmap.reset();
124 fInvMatrix = inv;
125 fFilterQuality = paint.getFilterQuality();
126
reed2252eda2016-01-20 11:04:40 -0800127 bool allow_ignore_fractional_translate = true; // historical default
reed05a56472016-03-02 09:49:02 -0800128 if (kMedium_SkFilterQuality == fFilterQuality) {
reed2252eda2016-01-20 11:04:40 -0800129 allow_ignore_fractional_translate = false;
130 }
reed2252eda2016-01-20 11:04:40 -0800131
reed64045422015-06-04 06:31:31 -0700132 SkDefaultBitmapController controller;
reed7a4d8472015-09-15 13:33:58 -0700133 fBMState = controller.requestBitmap(fProvider, inv, paint.getFilterQuality(),
reed64045422015-06-04 06:31:31 -0700134 fBMStateStorage.get(), fBMStateStorage.size());
reedad7ae6c2015-06-04 14:12:25 -0700135 // Note : we allow the controller to return an empty (zero-dimension) result. Should we?
halcanary96fcdcc2015-08-27 07:41:13 -0700136 if (nullptr == fBMState || fBMState->pixmap().info().isEmpty()) {
reed64045422015-06-04 06:31:31 -0700137 return false;
reedf7094c42015-01-16 12:05:19 -0800138 }
reedad7ae6c2015-06-04 14:12:25 -0700139 fPixmap = fBMState->pixmap();
reed64045422015-06-04 06:31:31 -0700140 fInvMatrix = fBMState->invMatrix();
reed05a56472016-03-02 09:49:02 -0800141 fPaintColor = paint.getColor();
142 fFilterQuality = fBMState->quality();
reedad7ae6c2015-06-04 14:12:25 -0700143 SkASSERT(fPixmap.addr());
reed05a56472016-03-02 09:49:02 -0800144
reed@google.comef0e3192013-09-09 13:42:39 +0000145 bool trivialMatrix = (fInvMatrix.getType() & ~SkMatrix::kTranslate_Mask) == 0;
146 bool clampClamp = SkShader::kClamp_TileMode == fTileModeX &&
147 SkShader::kClamp_TileMode == fTileModeY;
skia.committer@gmail.com9a070f22013-09-10 07:01:44 +0000148
humper535e3b22014-10-27 10:32:06 -0700149 // Most of the scanline procs deal with "unit" texture coordinates, as this
150 // makes it easy to perform tiling modes (repeat = (x & 0xFFFF)). To generate
151 // those, we divide the matrix by its dimensions here.
152 //
153 // We don't do this if we're either trivial (can ignore the matrix) or clamping
154 // in both X and Y since clamping to width,height is just as easy as to 0xFFFF.
155
156 if (!(clampClamp || trivialMatrix)) {
reedad7ae6c2015-06-04 14:12:25 -0700157 fInvMatrix.postIDiv(fPixmap.width(), fPixmap.height());
reed@google.comef0e3192013-09-09 13:42:39 +0000158 }
159
humper@google.com9c96d4b2013-07-14 01:44:59 +0000160 // Now that all possible changes to the matrix have taken place, check
161 // to see if we're really close to a no-scale matrix. If so, explicitly
162 // set it to be so. Subsequent code may inspect this matrix to choose
163 // a faster path in this case.
164
165 // This code will only execute if the matrix has some scale component;
166 // if it's already pure translate then we won't do this inversion.
167
168 if (matrix_only_scale_translate(fInvMatrix)) {
reed@google.comee056a82013-04-18 15:33:27 +0000169 SkMatrix forward;
humper@google.com9c96d4b2013-07-14 01:44:59 +0000170 if (fInvMatrix.invert(&forward)) {
reed2252eda2016-01-20 11:04:40 -0800171 if ((clampClamp && allow_ignore_fractional_translate)
reed05a56472016-03-02 09:49:02 -0800172 ? just_trans_clamp(forward, fPixmap)
173 : just_trans_general(forward)) {
reedf2e2ad02016-01-19 13:33:32 -0800174 fInvMatrix.setTranslate(-forward.getTranslateX(), -forward.getTranslateY());
reed@google.comce1f3cc2013-01-05 14:37:48 +0000175 }
reed@google.comc0e88e02012-10-17 21:11:56 +0000176 }
177 }
178
reed05a56472016-03-02 09:49:02 -0800179 fInvType = fInvMatrix.getType();
humper@google.com9c96d4b2013-07-14 01:44:59 +0000180
reed2252eda2016-01-20 11:04:40 -0800181 // If our target pixmap is the same as the original, then we revert back to legacy behavior
182 // and allow the code to ignore fractional translate.
183 //
184 // The width/height check allows allow_ignore_fractional_translate to stay false if we
185 // previously set it that way (e.g. we started in kMedium).
186 //
187 if (fPixmap.width() == origW && fPixmap.height() == origH) {
188 allow_ignore_fractional_translate = true;
189 }
190
reed05a56472016-03-02 09:49:02 -0800191 if (kLow_SkFilterQuality == fFilterQuality && allow_ignore_fractional_translate) {
humper@google.com9c96d4b2013-07-14 01:44:59 +0000192 // Only try bilerp if the matrix is "interesting" and
193 // the image has a suitable size.
194
195 if (fInvType <= SkMatrix::kTranslate_Mask ||
reedad7ae6c2015-06-04 14:12:25 -0700196 !valid_for_filtering(fPixmap.width() | fPixmap.height()))
reedf7094c42015-01-16 12:05:19 -0800197 {
reed05a56472016-03-02 09:49:02 -0800198 fFilterQuality = kNone_SkFilterQuality;
humper@google.com9c96d4b2013-07-14 01:44:59 +0000199 }
200 }
reed05a56472016-03-02 09:49:02 -0800201
202 return true;
reed26feeba2015-01-14 12:28:22 -0800203}
humper@google.com9c96d4b2013-07-14 01:44:59 +0000204
reed05a56472016-03-02 09:49:02 -0800205/*
206 * Analyze filter-quality and matrix, and decide how to implement that.
207 *
208 * In general, we cascade down the request level [ High ... None ]
209 * - for a given level, if we can fulfill it, fine, else
210 * - else we downgrade to the next lower level and try again.
211 * We can always fulfill requests for Low and None
212 * - sometimes we will "ignore" Low and give None, but this is likely a legacy perf hack
213 * and may be removed.
214 */
215bool SkBitmapProcState::chooseProcs() {
216 fInvProc = fInvMatrix.getMapXYProc();
217 fInvSx = SkScalarToFixed(fInvMatrix.getScaleX());
218 fInvSxFractionalInt = SkScalarToFractionalInt(fInvMatrix.getScaleX());
219 fInvKy = SkScalarToFixed(fInvMatrix.getSkewY());
220 fInvKyFractionalInt = SkScalarToFractionalInt(fInvMatrix.getSkewY());
221
222 fAlphaScale = SkAlpha255To256(SkColorGetA(fPaintColor));
223
224 fShaderProc32 = nullptr;
225 fShaderProc16 = nullptr;
226 fSampleProc32 = nullptr;
227
228 const bool trivialMatrix = (fInvMatrix.getType() & ~SkMatrix::kTranslate_Mask) == 0;
229 const bool clampClamp = SkShader::kClamp_TileMode == fTileModeX &&
230 SkShader::kClamp_TileMode == fTileModeY;
231
232 return this->chooseScanlineProcs(trivialMatrix, clampClamp);
233}
234
235bool SkBitmapProcState::chooseScanlineProcs(bool trivialMatrix, bool clampClamp) {
humper@google.com9c96d4b2013-07-14 01:44:59 +0000236 fMatrixProc = this->chooseMatrixProc(trivialMatrix);
halcanary96fcdcc2015-08-27 07:41:13 -0700237 // TODO(dominikg): SkASSERT(fMatrixProc) instead? chooseMatrixProc never returns nullptr.
238 if (nullptr == fMatrixProc) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000239 return false;
240 }
241
242 ///////////////////////////////////////////////////////////////////////
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000243
reedad7ae6c2015-06-04 14:12:25 -0700244 const SkAlphaType at = fPixmap.alphaType();
reed00adc752014-07-12 21:10:52 -0700245
humper@google.com9c96d4b2013-07-14 01:44:59 +0000246 // No need to do this if we're doing HQ sampling; if filter quality is
247 // still set to HQ by the time we get here, then we must have installed
mtklein@google.com0dc546c2013-08-26 16:21:35 +0000248 // the shader procs above and can skip all this.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000249
reed05a56472016-03-02 09:49:02 -0800250 if (fFilterQuality < kHigh_SkFilterQuality) {
skia.committer@gmail.comfa1bd5f2013-07-13 07:00:56 +0000251
humper@google.com9c96d4b2013-07-14 01:44:59 +0000252 int index = 0;
253 if (fAlphaScale < 256) { // note: this distinction is not used for D16
254 index |= 1;
reed@android.comaa9152a2009-07-17 21:24:56 +0000255 }
humper@google.com9c96d4b2013-07-14 01:44:59 +0000256 if (fInvType <= (SkMatrix::kTranslate_Mask | SkMatrix::kScale_Mask)) {
257 index |= 2;
258 }
reed05a56472016-03-02 09:49:02 -0800259 if (fFilterQuality > kNone_SkFilterQuality) {
humper@google.com9c96d4b2013-07-14 01:44:59 +0000260 index |= 4;
261 }
262 // bits 3,4,5 encoding the source bitmap format
reedad7ae6c2015-06-04 14:12:25 -0700263 switch (fPixmap.colorType()) {
commit-bot@chromium.orgcba73782014-05-29 15:57:47 +0000264 case kN32_SkColorType:
reed00adc752014-07-12 21:10:52 -0700265 if (kPremul_SkAlphaType != at && kOpaque_SkAlphaType != at) {
266 return false;
267 }
humper@google.com9c96d4b2013-07-14 01:44:59 +0000268 index |= 0;
269 break;
commit-bot@chromium.orgcba73782014-05-29 15:57:47 +0000270 case kRGB_565_SkColorType:
humper@google.com9c96d4b2013-07-14 01:44:59 +0000271 index |= 8;
272 break;
commit-bot@chromium.orgcba73782014-05-29 15:57:47 +0000273 case kIndex_8_SkColorType:
reed00adc752014-07-12 21:10:52 -0700274 if (kPremul_SkAlphaType != at && kOpaque_SkAlphaType != at) {
275 return false;
276 }
humper@google.com9c96d4b2013-07-14 01:44:59 +0000277 index |= 16;
278 break;
commit-bot@chromium.orgcba73782014-05-29 15:57:47 +0000279 case kARGB_4444_SkColorType:
reed00adc752014-07-12 21:10:52 -0700280 if (kPremul_SkAlphaType != at && kOpaque_SkAlphaType != at) {
281 return false;
282 }
humper@google.com9c96d4b2013-07-14 01:44:59 +0000283 index |= 24;
284 break;
commit-bot@chromium.orgcba73782014-05-29 15:57:47 +0000285 case kAlpha_8_SkColorType:
humper@google.com9c96d4b2013-07-14 01:44:59 +0000286 index |= 32;
reed05a56472016-03-02 09:49:02 -0800287 fPaintPMColor = SkPreMultiplyColor(fPaintColor);
humper@google.com9c96d4b2013-07-14 01:44:59 +0000288 break;
reed0c9b1a82015-03-17 17:44:06 -0700289 case kGray_8_SkColorType:
290 index |= 40;
reed05a56472016-03-02 09:49:02 -0800291 fPaintPMColor = SkPreMultiplyColor(fPaintColor);
reed0c9b1a82015-03-17 17:44:06 -0700292 break;
humper@google.com9c96d4b2013-07-14 01:44:59 +0000293 default:
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000294 // TODO(dominikg): Should we ever get here? SkASSERT(false) instead?
humper@google.com9c96d4b2013-07-14 01:44:59 +0000295 return false;
296 }
reed@android.comc9a1d4b2009-08-03 15:05:55 +0000297
reed26feeba2015-01-14 12:28:22 -0800298#if !SK_ARM_NEON_IS_ALWAYS
humper@google.com9c96d4b2013-07-14 01:44:59 +0000299 static const SampleProc32 gSkBitmapProcStateSample32[] = {
300 S32_opaque_D32_nofilter_DXDY,
301 S32_alpha_D32_nofilter_DXDY,
302 S32_opaque_D32_nofilter_DX,
303 S32_alpha_D32_nofilter_DX,
304 S32_opaque_D32_filter_DXDY,
305 S32_alpha_D32_filter_DXDY,
306 S32_opaque_D32_filter_DX,
307 S32_alpha_D32_filter_DX,
skia.committer@gmail.com3e2345a2013-05-24 07:01:26 +0000308
humper@google.com9c96d4b2013-07-14 01:44:59 +0000309 S16_opaque_D32_nofilter_DXDY,
310 S16_alpha_D32_nofilter_DXDY,
311 S16_opaque_D32_nofilter_DX,
312 S16_alpha_D32_nofilter_DX,
313 S16_opaque_D32_filter_DXDY,
314 S16_alpha_D32_filter_DXDY,
315 S16_opaque_D32_filter_DX,
316 S16_alpha_D32_filter_DX,
317
318 SI8_opaque_D32_nofilter_DXDY,
319 SI8_alpha_D32_nofilter_DXDY,
320 SI8_opaque_D32_nofilter_DX,
321 SI8_alpha_D32_nofilter_DX,
322 SI8_opaque_D32_filter_DXDY,
323 SI8_alpha_D32_filter_DXDY,
324 SI8_opaque_D32_filter_DX,
325 SI8_alpha_D32_filter_DX,
326
327 S4444_opaque_D32_nofilter_DXDY,
328 S4444_alpha_D32_nofilter_DXDY,
329 S4444_opaque_D32_nofilter_DX,
330 S4444_alpha_D32_nofilter_DX,
331 S4444_opaque_D32_filter_DXDY,
332 S4444_alpha_D32_filter_DXDY,
333 S4444_opaque_D32_filter_DX,
334 S4444_alpha_D32_filter_DX,
reed0c9b1a82015-03-17 17:44:06 -0700335
humper@google.com9c96d4b2013-07-14 01:44:59 +0000336 // A8 treats alpha/opaque the same (equally efficient)
337 SA8_alpha_D32_nofilter_DXDY,
338 SA8_alpha_D32_nofilter_DXDY,
339 SA8_alpha_D32_nofilter_DX,
340 SA8_alpha_D32_nofilter_DX,
341 SA8_alpha_D32_filter_DXDY,
342 SA8_alpha_D32_filter_DXDY,
343 SA8_alpha_D32_filter_DX,
reed0c9b1a82015-03-17 17:44:06 -0700344 SA8_alpha_D32_filter_DX,
345
346 // todo: possibly specialize on opaqueness
347 SG8_alpha_D32_nofilter_DXDY,
348 SG8_alpha_D32_nofilter_DXDY,
349 SG8_alpha_D32_nofilter_DX,
350 SG8_alpha_D32_nofilter_DX,
351 SG8_alpha_D32_filter_DXDY,
352 SG8_alpha_D32_filter_DXDY,
353 SG8_alpha_D32_filter_DX,
354 SG8_alpha_D32_filter_DX
humper@google.com9c96d4b2013-07-14 01:44:59 +0000355 };
reed26feeba2015-01-14 12:28:22 -0800356#endif
humper@google.com9c96d4b2013-07-14 01:44:59 +0000357
358 fSampleProc32 = SK_ARM_NEON_WRAP(gSkBitmapProcStateSample32)[index];
359 index >>= 1; // shift away any opaque/alpha distinction
humper@google.com9c96d4b2013-07-14 01:44:59 +0000360
361 // our special-case shaderprocs
reed4e5a7582016-01-05 05:10:33 -0800362 if (SK_ARM_NEON_WRAP(SI8_opaque_D32_filter_DX) == fSampleProc32 && clampClamp) {
humper@google.com9c96d4b2013-07-14 01:44:59 +0000363 fShaderProc32 = SK_ARM_NEON_WRAP(Clamp_SI8_opaque_D32_filter_DX_shaderproc);
mtklein14e4d392014-10-23 14:35:01 -0700364 } else if (S32_opaque_D32_nofilter_DX == fSampleProc32 && clampClamp) {
365 fShaderProc32 = Clamp_S32_opaque_D32_nofilter_DX_shaderproc;
humper@google.com9c96d4b2013-07-14 01:44:59 +0000366 }
367
halcanary96fcdcc2015-08-27 07:41:13 -0700368 if (nullptr == fShaderProc32) {
humper@google.com9c96d4b2013-07-14 01:44:59 +0000369 fShaderProc32 = this->chooseShaderProc32();
370 }
humper@google.comb0889472013-07-09 21:37:14 +0000371 }
mtklein96d68b72015-02-20 12:40:40 -0800372
reed@android.comc9a1d4b2009-08-03 15:05:55 +0000373 // see if our platform has any accelerated overrides
374 this->platformProcs();
mtklein96d68b72015-02-20 12:40:40 -0800375
reed@android.com8a1c16f2008-12-17 15:59:43 +0000376 return true;
377}
378
herbc7a784c2015-12-18 09:52:15 -0800379static void Clamp_S32_D32_nofilter_trans_shaderproc(const void* sIn,
reed@google.com9a4c7462012-10-12 18:21:37 +0000380 int x, int y,
381 SkPMColor* SK_RESTRICT colors,
382 int count) {
herbc7a784c2015-12-18 09:52:15 -0800383 const SkBitmapProcState& s = *static_cast<const SkBitmapProcState*>(sIn);
reed@google.com9a4c7462012-10-12 18:21:37 +0000384 SkASSERT(((s.fInvType & ~SkMatrix::kTranslate_Mask)) == 0);
385 SkASSERT(s.fInvKy == 0);
halcanary96fcdcc2015-08-27 07:41:13 -0700386 SkASSERT(count > 0 && colors != nullptr);
reed05a56472016-03-02 09:49:02 -0800387 SkASSERT(kNone_SkFilterQuality == s.fFilterQuality);
skia.committer@gmail.com20c301b2012-10-17 02:01:13 +0000388
reedad7ae6c2015-06-04 14:12:25 -0700389 const int maxX = s.fPixmap.width() - 1;
390 const int maxY = s.fPixmap.height() - 1;
reed@google.comf7698de2012-10-12 20:50:24 +0000391 int ix = s.fFilterOneX + x;
392 int iy = SkClampMax(s.fFilterOneY + y, maxY);
reedad7ae6c2015-06-04 14:12:25 -0700393 const SkPMColor* row = s.fPixmap.addr32(0, iy);
skia.committer@gmail.com20c301b2012-10-17 02:01:13 +0000394
reed@google.com9a4c7462012-10-12 18:21:37 +0000395 // clamp to the left
396 if (ix < 0) {
397 int n = SkMin32(-ix, count);
398 sk_memset32(colors, row[0], n);
399 count -= n;
400 if (0 == count) {
401 return;
402 }
403 colors += n;
404 SkASSERT(-ix == n);
405 ix = 0;
406 }
407 // copy the middle
408 if (ix <= maxX) {
409 int n = SkMin32(maxX - ix + 1, count);
410 memcpy(colors, row + ix, n * sizeof(SkPMColor));
411 count -= n;
412 if (0 == count) {
413 return;
414 }
415 colors += n;
416 }
417 SkASSERT(count > 0);
418 // clamp to the right
419 sk_memset32(colors, row[maxX], count);
420}
421
reed@google.coma8d99302012-10-16 20:23:25 +0000422static inline int sk_int_mod(int x, int n) {
423 SkASSERT(n > 0);
424 if ((unsigned)x >= (unsigned)n) {
425 if (x < 0) {
426 x = n + ~(~x % n);
427 } else {
428 x = x % n;
429 }
430 }
431 return x;
432}
433
robertphillips@google.com8b8bf4d2013-01-18 16:38:40 +0000434static inline int sk_int_mirror(int x, int n) {
435 x = sk_int_mod(x, 2 * n);
436 if (x >= n) {
437 x = n + ~(x - n);
438 }
439 return x;
440}
441
herbc7a784c2015-12-18 09:52:15 -0800442static void Repeat_S32_D32_nofilter_trans_shaderproc(const void* sIn,
reed@google.coma8d99302012-10-16 20:23:25 +0000443 int x, int y,
444 SkPMColor* SK_RESTRICT colors,
445 int count) {
herbc7a784c2015-12-18 09:52:15 -0800446 const SkBitmapProcState& s = *static_cast<const SkBitmapProcState*>(sIn);
reed@google.coma8d99302012-10-16 20:23:25 +0000447 SkASSERT(((s.fInvType & ~SkMatrix::kTranslate_Mask)) == 0);
448 SkASSERT(s.fInvKy == 0);
halcanary96fcdcc2015-08-27 07:41:13 -0700449 SkASSERT(count > 0 && colors != nullptr);
reed05a56472016-03-02 09:49:02 -0800450 SkASSERT(kNone_SkFilterQuality == s.fFilterQuality);
skia.committer@gmail.com20c301b2012-10-17 02:01:13 +0000451
reedad7ae6c2015-06-04 14:12:25 -0700452 const int stopX = s.fPixmap.width();
453 const int stopY = s.fPixmap.height();
reed@google.coma8d99302012-10-16 20:23:25 +0000454 int ix = s.fFilterOneX + x;
455 int iy = sk_int_mod(s.fFilterOneY + y, stopY);
reedad7ae6c2015-06-04 14:12:25 -0700456 const SkPMColor* row = s.fPixmap.addr32(0, iy);
reed@google.coma8d99302012-10-16 20:23:25 +0000457
458 ix = sk_int_mod(ix, stopX);
459 for (;;) {
460 int n = SkMin32(stopX - ix, count);
461 memcpy(colors, row + ix, n * sizeof(SkPMColor));
462 count -= n;
463 if (0 == count) {
464 return;
465 }
466 colors += n;
467 ix = 0;
468 }
469}
470
herbc7a784c2015-12-18 09:52:15 -0800471static void S32_D32_constX_shaderproc(const void* sIn,
robertphillips@google.com8b8bf4d2013-01-18 16:38:40 +0000472 int x, int y,
473 SkPMColor* SK_RESTRICT colors,
474 int count) {
herbc7a784c2015-12-18 09:52:15 -0800475 const SkBitmapProcState& s = *static_cast<const SkBitmapProcState*>(sIn);
robertphillips@google.com8b8bf4d2013-01-18 16:38:40 +0000476 SkASSERT((s.fInvType & ~(SkMatrix::kTranslate_Mask | SkMatrix::kScale_Mask)) == 0);
477 SkASSERT(s.fInvKy == 0);
halcanary96fcdcc2015-08-27 07:41:13 -0700478 SkASSERT(count > 0 && colors != nullptr);
reedad7ae6c2015-06-04 14:12:25 -0700479 SkASSERT(1 == s.fPixmap.width());
robertphillips@google.com8b8bf4d2013-01-18 16:38:40 +0000480
scroggo@google.comad511322013-02-22 15:50:37 +0000481 int iY0;
482 int iY1 SK_INIT_TO_AVOID_WARNING;
483 int iSubY SK_INIT_TO_AVOID_WARNING;
robertphillips@google.com8b8bf4d2013-01-18 16:38:40 +0000484
reed05a56472016-03-02 09:49:02 -0800485 if (kNone_SkFilterQuality != s.fFilterQuality) {
robertphillips@google.com8b8bf4d2013-01-18 16:38:40 +0000486 SkBitmapProcState::MatrixProc mproc = s.getMatrixProc();
487 uint32_t xy[2];
488
489 mproc(s, xy, 1, x, y);
490
491 iY0 = xy[0] >> 18;
492 iY1 = xy[0] & 0x3FFF;
493 iSubY = (xy[0] >> 14) & 0xF;
494 } else {
495 int yTemp;
496
497 if (s.fInvType > SkMatrix::kTranslate_Mask) {
fmalitaeb543072016-02-02 10:17:24 -0800498 const SkBitmapProcStateAutoMapper mapper(s, x, y);
499
robertphillips@google.com1e305232013-01-22 20:29:16 +0000500 // When the matrix has a scale component the setup code in
skia.committer@gmail.com98ded842013-01-23 07:06:17 +0000501 // chooseProcs multiples the inverse matrix by the inverse of the
robertphillips@google.com1e305232013-01-22 20:29:16 +0000502 // bitmap's width and height. Since this method is going to do
503 // its own tiling and sampling we need to undo that here.
robertphillips@google.comd5077752013-01-23 00:36:02 +0000504 if (SkShader::kClamp_TileMode != s.fTileModeX ||
505 SkShader::kClamp_TileMode != s.fTileModeY) {
fmalitabe5cfa92016-02-03 10:21:33 -0800506 yTemp = SkFractionalIntToInt(mapper.fractionalIntY() * s.fPixmap.height());
robertphillips@google.comd5077752013-01-23 00:36:02 +0000507 } else {
fmalitabe5cfa92016-02-03 10:21:33 -0800508 yTemp = mapper.intY();
robertphillips@google.comd5077752013-01-23 00:36:02 +0000509 }
robertphillips@google.com8b8bf4d2013-01-18 16:38:40 +0000510 } else {
511 yTemp = s.fFilterOneY + y;
512 }
513
reedad7ae6c2015-06-04 14:12:25 -0700514 const int stopY = s.fPixmap.height();
robertphillips@google.com8b8bf4d2013-01-18 16:38:40 +0000515 switch (s.fTileModeY) {
516 case SkShader::kClamp_TileMode:
517 iY0 = SkClampMax(yTemp, stopY-1);
518 break;
519 case SkShader::kRepeat_TileMode:
520 iY0 = sk_int_mod(yTemp, stopY);
521 break;
522 case SkShader::kMirror_TileMode:
523 default:
524 iY0 = sk_int_mirror(yTemp, stopY);
525 break;
526 }
527
fmalitad2a42712016-02-01 04:48:39 -0800528#ifdef SK_DEBUG
robertphillips@google.com8b8bf4d2013-01-18 16:38:40 +0000529 {
fmalitaeb543072016-02-02 10:17:24 -0800530 const SkBitmapProcStateAutoMapper mapper(s, x, y);
robertphillips@google.com8b8bf4d2013-01-18 16:38:40 +0000531 int iY2;
fmalitaeb543072016-02-02 10:17:24 -0800532
533 if (s.fInvType > SkMatrix::kTranslate_Mask &&
534 (SkShader::kClamp_TileMode != s.fTileModeX ||
535 SkShader::kClamp_TileMode != s.fTileModeY)) {
fmalitabe5cfa92016-02-03 10:21:33 -0800536 iY2 = SkFractionalIntToInt(mapper.fractionalIntY() * s.fPixmap.height());
fmalitad2a42712016-02-01 04:48:39 -0800537 } else {
fmalitabe5cfa92016-02-03 10:21:33 -0800538 iY2 = mapper.intY();
fmalitad2a42712016-02-01 04:48:39 -0800539 }
robertphillips@google.com8b8bf4d2013-01-18 16:38:40 +0000540
541 switch (s.fTileModeY) {
542 case SkShader::kClamp_TileMode:
fmalitad2a42712016-02-01 04:48:39 -0800543 iY2 = SkClampMax(iY2, stopY-1);
robertphillips@google.com8b8bf4d2013-01-18 16:38:40 +0000544 break;
545 case SkShader::kRepeat_TileMode:
fmalitad2a42712016-02-01 04:48:39 -0800546 iY2 = sk_int_mod(iY2, stopY);
robertphillips@google.com8b8bf4d2013-01-18 16:38:40 +0000547 break;
548 case SkShader::kMirror_TileMode:
549 default:
fmalitad2a42712016-02-01 04:48:39 -0800550 iY2 = sk_int_mirror(iY2, stopY);
robertphillips@google.com8b8bf4d2013-01-18 16:38:40 +0000551 break;
552 }
553
554 SkASSERT(iY0 == iY2);
555 }
556#endif
557 }
558
reedad7ae6c2015-06-04 14:12:25 -0700559 const SkPMColor* row0 = s.fPixmap.addr32(0, iY0);
robertphillips@google.com8b8bf4d2013-01-18 16:38:40 +0000560 SkPMColor color;
561
reed05a56472016-03-02 09:49:02 -0800562 if (kNone_SkFilterQuality != s.fFilterQuality) {
reedad7ae6c2015-06-04 14:12:25 -0700563 const SkPMColor* row1 = s.fPixmap.addr32(0, iY1);
robertphillips@google.com8b8bf4d2013-01-18 16:38:40 +0000564
565 if (s.fAlphaScale < 256) {
566 Filter_32_alpha(iSubY, *row0, *row1, &color, s.fAlphaScale);
567 } else {
568 Filter_32_opaque(iSubY, *row0, *row1, &color);
569 }
570 } else {
571 if (s.fAlphaScale < 256) {
572 color = SkAlphaMulQ(*row0, s.fAlphaScale);
573 } else {
574 color = *row0;
575 }
576 }
577
578 sk_memset32(colors, color, count);
579}
580
herbc7a784c2015-12-18 09:52:15 -0800581static void DoNothing_shaderproc(const void*, int x, int y,
reed@google.com6bb92bc2012-11-20 19:45:16 +0000582 SkPMColor* SK_RESTRICT colors, int count) {
583 // if we get called, the matrix is too tricky, so we just draw nothing
584 sk_memset32(colors, 0, count);
585}
586
587bool SkBitmapProcState::setupForTranslate() {
reed@google.coma8d99302012-10-16 20:23:25 +0000588 SkPoint pt;
fmalita8a8eb522016-01-30 18:56:34 -0800589 const SkBitmapProcStateAutoMapper mapper(*this, 0, 0, &pt);
fmalita653c12d2016-01-30 10:06:46 -0800590
reed@google.com6bb92bc2012-11-20 19:45:16 +0000591 /*
592 * if the translate is larger than our ints, we can get random results, or
593 * worse, we might get 0x80000000, which wreaks havoc on us, since we can't
594 * negate it.
595 */
fmalita8a8eb522016-01-30 18:56:34 -0800596 const SkScalar too_big = SkIntToScalar(1 << 30);
597 if (SkScalarAbs(pt.fX) > too_big || SkScalarAbs(pt.fY) > too_big) {
reed@google.com6bb92bc2012-11-20 19:45:16 +0000598 return false;
599 }
600
reed@google.coma8d99302012-10-16 20:23:25 +0000601 // Since we know we're not filtered, we re-purpose these fields allow
602 // us to go from device -> src coordinates w/ just an integer add,
603 // rather than running through the inverse-matrix
fmalitabe5cfa92016-02-03 10:21:33 -0800604 fFilterOneX = mapper.intX();
605 fFilterOneY = mapper.intY();
fmalita653c12d2016-01-30 10:06:46 -0800606
reed@google.com6bb92bc2012-11-20 19:45:16 +0000607 return true;
reed@google.coma8d99302012-10-16 20:23:25 +0000608}
609
reed@google.com9a4c7462012-10-12 18:21:37 +0000610SkBitmapProcState::ShaderProc32 SkBitmapProcState::chooseShaderProc32() {
robertphillips@google.com8b8bf4d2013-01-18 16:38:40 +0000611
reedad7ae6c2015-06-04 14:12:25 -0700612 if (kN32_SkColorType != fPixmap.colorType()) {
halcanary96fcdcc2015-08-27 07:41:13 -0700613 return nullptr;
robertphillips@google.com8b8bf4d2013-01-18 16:38:40 +0000614 }
615
616 static const unsigned kMask = SkMatrix::kTranslate_Mask | SkMatrix::kScale_Mask;
617
reedad7ae6c2015-06-04 14:12:25 -0700618 if (1 == fPixmap.width() && 0 == (fInvType & ~kMask)) {
reed05a56472016-03-02 09:49:02 -0800619 if (kNone_SkFilterQuality == fFilterQuality &&
humper@google.com9c96d4b2013-07-14 01:44:59 +0000620 fInvType <= SkMatrix::kTranslate_Mask &&
621 !this->setupForTranslate()) {
robertphillips@google.com8b8bf4d2013-01-18 16:38:40 +0000622 return DoNothing_shaderproc;
623 }
624 return S32_D32_constX_shaderproc;
625 }
626
reed@google.com9a4c7462012-10-12 18:21:37 +0000627 if (fAlphaScale < 256) {
halcanary96fcdcc2015-08-27 07:41:13 -0700628 return nullptr;
reed@google.com9a4c7462012-10-12 18:21:37 +0000629 }
630 if (fInvType > SkMatrix::kTranslate_Mask) {
halcanary96fcdcc2015-08-27 07:41:13 -0700631 return nullptr;
reed@google.com9a4c7462012-10-12 18:21:37 +0000632 }
reed05a56472016-03-02 09:49:02 -0800633 if (kNone_SkFilterQuality != fFilterQuality) {
halcanary96fcdcc2015-08-27 07:41:13 -0700634 return nullptr;
reed@google.com9a4c7462012-10-12 18:21:37 +0000635 }
reed@google.com9a4c7462012-10-12 18:21:37 +0000636
reed@google.coma8d99302012-10-16 20:23:25 +0000637 SkShader::TileMode tx = (SkShader::TileMode)fTileModeX;
638 SkShader::TileMode ty = (SkShader::TileMode)fTileModeY;
639
640 if (SkShader::kClamp_TileMode == tx && SkShader::kClamp_TileMode == ty) {
reed@google.com6bb92bc2012-11-20 19:45:16 +0000641 if (this->setupForTranslate()) {
642 return Clamp_S32_D32_nofilter_trans_shaderproc;
643 }
644 return DoNothing_shaderproc;
reed@google.com9a4c7462012-10-12 18:21:37 +0000645 }
reed@google.coma8d99302012-10-16 20:23:25 +0000646 if (SkShader::kRepeat_TileMode == tx && SkShader::kRepeat_TileMode == ty) {
reed@google.com6bb92bc2012-11-20 19:45:16 +0000647 if (this->setupForTranslate()) {
648 return Repeat_S32_D32_nofilter_trans_shaderproc;
649 }
650 return DoNothing_shaderproc;
reed@google.coma8d99302012-10-16 20:23:25 +0000651 }
halcanary96fcdcc2015-08-27 07:41:13 -0700652 return nullptr;
reed@google.com9a4c7462012-10-12 18:21:37 +0000653}
654
reed@android.com4c128c42009-08-14 13:54:37 +0000655///////////////////////////////////////////////////////////////////////////////
reed@google.com9fe287b2012-03-27 15:54:28 +0000656
657#ifdef SK_DEBUG
658
659static void check_scale_nofilter(uint32_t bitmapXY[], int count,
660 unsigned mx, unsigned my) {
661 unsigned y = *bitmapXY++;
662 SkASSERT(y < my);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000663
reed@google.com9fe287b2012-03-27 15:54:28 +0000664 const uint16_t* xptr = reinterpret_cast<const uint16_t*>(bitmapXY);
665 for (int i = 0; i < count; ++i) {
666 SkASSERT(xptr[i] < mx);
667 }
668}
669
670static void check_scale_filter(uint32_t bitmapXY[], int count,
671 unsigned mx, unsigned my) {
672 uint32_t YY = *bitmapXY++;
673 unsigned y0 = YY >> 18;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000674 unsigned y1 = YY & 0x3FFF;
reed@google.com9fe287b2012-03-27 15:54:28 +0000675 SkASSERT(y0 < my);
676 SkASSERT(y1 < my);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000677
reed@google.com9fe287b2012-03-27 15:54:28 +0000678 for (int i = 0; i < count; ++i) {
679 uint32_t XX = bitmapXY[i];
680 unsigned x0 = XX >> 18;
681 unsigned x1 = XX & 0x3FFF;
682 SkASSERT(x0 < mx);
683 SkASSERT(x1 < mx);
684 }
685}
686
687static void check_affine_nofilter(uint32_t bitmapXY[], int count,
688 unsigned mx, unsigned my) {
689 for (int i = 0; i < count; ++i) {
690 uint32_t XY = bitmapXY[i];
691 unsigned x = XY & 0xFFFF;
692 unsigned y = XY >> 16;
693 SkASSERT(x < mx);
694 SkASSERT(y < my);
695 }
696}
697
698static void check_affine_filter(uint32_t bitmapXY[], int count,
699 unsigned mx, unsigned my) {
700 for (int i = 0; i < count; ++i) {
701 uint32_t YY = *bitmapXY++;
702 unsigned y0 = YY >> 18;
703 unsigned y1 = YY & 0x3FFF;
704 SkASSERT(y0 < my);
705 SkASSERT(y1 < my);
706
707 uint32_t XX = *bitmapXY++;
708 unsigned x0 = XX >> 18;
709 unsigned x1 = XX & 0x3FFF;
710 SkASSERT(x0 < mx);
711 SkASSERT(x1 < mx);
712 }
713}
714
715void SkBitmapProcState::DebugMatrixProc(const SkBitmapProcState& state,
716 uint32_t bitmapXY[], int count,
717 int x, int y) {
718 SkASSERT(bitmapXY);
719 SkASSERT(count > 0);
720
721 state.fMatrixProc(state, bitmapXY, count, x, y);
722
723 void (*proc)(uint32_t bitmapXY[], int count, unsigned mx, unsigned my);
724
725 // There are four formats possible:
726 // scale -vs- affine
727 // filter -vs- nofilter
728 if (state.fInvType <= (SkMatrix::kTranslate_Mask | SkMatrix::kScale_Mask)) {
reed05a56472016-03-02 09:49:02 -0800729 proc = state.fFilterQuality != kNone_SkFilterQuality ?
730 check_scale_filter : check_scale_nofilter;
reed@google.com9fe287b2012-03-27 15:54:28 +0000731 } else {
reed05a56472016-03-02 09:49:02 -0800732 proc = state.fFilterQuality != kNone_SkFilterQuality ?
733 check_affine_filter : check_affine_nofilter;
reed@google.com9fe287b2012-03-27 15:54:28 +0000734 }
reedad7ae6c2015-06-04 14:12:25 -0700735 proc(bitmapXY, count, state.fPixmap.width(), state.fPixmap.height());
reed@google.com9fe287b2012-03-27 15:54:28 +0000736}
737
738SkBitmapProcState::MatrixProc SkBitmapProcState::getMatrixProc() const {
739 return DebugMatrixProc;
740}
741
742#endif
743
744///////////////////////////////////////////////////////////////////////////////
reed@android.com4c128c42009-08-14 13:54:37 +0000745/*
746 The storage requirements for the different matrix procs are as follows,
747 where each X or Y is 2 bytes, and N is the number of pixels/elements:
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000748
reed@android.com4c128c42009-08-14 13:54:37 +0000749 scale/translate nofilter Y(4bytes) + N * X
750 affine/perspective nofilter N * (X Y)
751 scale/translate filter Y Y + N * (X X)
752 affine/perspective filter N * (Y Y X X)
753 */
754int SkBitmapProcState::maxCountForBufferSize(size_t bufferSize) const {
755 int32_t size = static_cast<int32_t>(bufferSize);
reed@android.com4c128c42009-08-14 13:54:37 +0000756
757 size &= ~3; // only care about 4-byte aligned chunks
758 if (fInvType <= (SkMatrix::kTranslate_Mask | SkMatrix::kScale_Mask)) {
759 size -= 4; // the shared Y (or YY) coordinate
760 if (size < 0) {
761 size = 0;
762 }
reed@android.com258cb222010-04-14 13:36:33 +0000763 size >>= 1;
reed@android.com4c128c42009-08-14 13:54:37 +0000764 } else {
reed@android.com258cb222010-04-14 13:36:33 +0000765 size >>= 2;
reed@android.com4c128c42009-08-14 13:54:37 +0000766 }
767
reed05a56472016-03-02 09:49:02 -0800768 if (fFilterQuality != kNone_SkFilterQuality) {
reed@android.com258cb222010-04-14 13:36:33 +0000769 size >>= 1;
770 }
771
772 return size;
reed@android.com4c128c42009-08-14 13:54:37 +0000773}
mtklein14e4d392014-10-23 14:35:01 -0700774
775///////////////////////
776
herbc7a784c2015-12-18 09:52:15 -0800777void Clamp_S32_opaque_D32_nofilter_DX_shaderproc(const void* sIn, int x, int y,
mtklein14e4d392014-10-23 14:35:01 -0700778 SkPMColor* SK_RESTRICT dst, int count) {
herbc7a784c2015-12-18 09:52:15 -0800779 const SkBitmapProcState& s = *static_cast<const SkBitmapProcState*>(sIn);
mtklein14e4d392014-10-23 14:35:01 -0700780 SkASSERT((s.fInvType & ~(SkMatrix::kTranslate_Mask |
781 SkMatrix::kScale_Mask)) == 0);
782
reedad7ae6c2015-06-04 14:12:25 -0700783 const unsigned maxX = s.fPixmap.width() - 1;
mtklein14e4d392014-10-23 14:35:01 -0700784 SkFractionalInt fx;
785 int dstY;
786 {
fmalitab3a83582016-01-04 10:28:11 -0800787 const SkBitmapProcStateAutoMapper mapper(s, x, y);
reedad7ae6c2015-06-04 14:12:25 -0700788 const unsigned maxY = s.fPixmap.height() - 1;
fmalitabe5cfa92016-02-03 10:21:33 -0800789 dstY = SkClampMax(mapper.intY(), maxY);
790 fx = mapper.fractionalIntX();
mtklein14e4d392014-10-23 14:35:01 -0700791 }
792
reedad7ae6c2015-06-04 14:12:25 -0700793 const SkPMColor* SK_RESTRICT src = s.fPixmap.addr32(0, dstY);
mtklein14e4d392014-10-23 14:35:01 -0700794 const SkFractionalInt dx = s.fInvSxFractionalInt;
795
796 // Check if we're safely inside [0...maxX] so no need to clamp each computed index.
797 //
798 if ((uint64_t)SkFractionalIntToInt(fx) <= maxX &&
799 (uint64_t)SkFractionalIntToInt(fx + dx * (count - 1)) <= maxX)
800 {
801 int count4 = count >> 2;
802 for (int i = 0; i < count4; ++i) {
803 SkPMColor src0 = src[SkFractionalIntToInt(fx)]; fx += dx;
804 SkPMColor src1 = src[SkFractionalIntToInt(fx)]; fx += dx;
805 SkPMColor src2 = src[SkFractionalIntToInt(fx)]; fx += dx;
806 SkPMColor src3 = src[SkFractionalIntToInt(fx)]; fx += dx;
807 dst[0] = src0;
808 dst[1] = src1;
809 dst[2] = src2;
810 dst[3] = src3;
811 dst += 4;
812 }
813 for (int i = (count4 << 2); i < count; ++i) {
814 unsigned index = SkFractionalIntToInt(fx);
815 SkASSERT(index <= maxX);
816 *dst++ = src[index];
817 fx += dx;
818 }
819 } else {
820 for (int i = 0; i < count; ++i) {
821 dst[i] = src[SkClampMax(SkFractionalIntToInt(fx), maxX)];
822 fx += dx;
823 }
824 }
825}
fmalita3e6be162015-12-18 09:36:18 -0800826